-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt-scout.js
48 lines (37 loc) · 1.07 KB
/
mqtt-scout.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
const config = require('./config');
const Scout = require(process.lynxari.scout);
const MqttDevice = require('./lib/mqtt_device');
module.exports = class MqttScout extends Scout {
constructor(opts) {
super();
if (typeof opts !== 'undefined') {
// copy all config options defined in the server
for (const key in opts) {
if (typeof opts[key] !== 'undefined') {
config[key] = opts[key];
}
}
}
if (config.name === undefined) { config.name = "MQTT" }
this.name = config.name;
this.mqtt = new MqttDevice(config);
}
init(next) {
const query = this.server.where({name: this.name});
this.server.find(query, (err, results) => {
if (!err) {
if (results[0]) {
this.provision(results[0], this.mqtt);
this.server.info('Provisioned known device ' + this.name);
} else {
this.discover(this.mqtt);
this.server.info('Discovered new device ' + this.name);
}
}
else {
this.server.error(err);
}
});
next();
}
}