-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_getip.js
58 lines (44 loc) · 1.66 KB
/
io_getip.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
class io_getip {
constructor( config ) {
this.conf = config || {};
};
getip() {
//~ GET IP ADDRESS WITH JSON IP SERVER
let exec = require( 'child_process' ).exec;
let ipAddr = '';
return new Promise( ( resolve, reject ) => {
if ( Object.keys( this.conf ).length <= 0 ) return reject( new Error("No configuration file given") );
if ( typeof this.conf.getip === "undefined" ) return reject( new Error("No getip settings in config.json") );
if ( typeof this.conf.getip.options === "undefined" ) return reject( new Error("No http request options set in config.json") );
exec( `curl https://${this.conf.getip.options.hostname}`, (err, stout, sterr) => {
if ( err ) return reject( err );
return resolve( JSON.parse( stout ).ip );
});
});
};
publish( topic, ipAddr ) {
// PUBLISH TO MQTT BROKER
return new Promise( ( resolve, reject ) => {
if ( Object.keys( this.conf ).length <= 0 ) return reject( new Error("No configuration file given") );
if ( typeof this.conf.mqtt === "undefined" ) return reject( new Error("No mqtt settings in config.json") );
if ( typeof this.conf.mqtt.url === "undefined" ) return reject( new Error("No url given for mqtt broker") );
let mqtt = require( 'mqtt' );
let client = mqtt.connect( "mqtt://" + this.conf.mqtt.url );
client.on('connect', () => {
client.publish( topic, ipAddr, {
qos: 0,
retain: true
}, (err, result) => {
client.end();
if ( err ) return reject( err );
return resolve(`published ${ipAddr} to ${topic} topic.`);
});
});
client.on('error', ( e ) => {
return reject( e );
});
});
};
};
module.exports = io_getip;