-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnode_helper.js
More file actions
61 lines (44 loc) · 1.28 KB
/
Copy pathnode_helper.js
File metadata and controls
61 lines (44 loc) · 1.28 KB
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
59
60
61
'use strict';
/* eslint-disable no-console */
// eslint-disable-next-line import/no-unresolved
const NodeHelper = require('node_helper');
const hub = require('./src');
module.exports = NodeHelper.create({
config: {},
started: false,
devices: {},
dongle: null,
start() {
console.log(`Starting node helper for: ${this.name}`);
},
startHub(config) {
if (this.started) {
return;
}
this.started = true;
console.log(`${this.name} starting hub`);
this.config = config;
this.dongle = hub.initialize(this.name, this.config);
this.dongle.on('setupCompleted', () => {
console.log(`${this.name} hub successfully started`);
});
this.dongle.on('deviceUpdate', ({ device, data }) => {
this.devices[device.name] = { device, data };
this.sendSocketNotification('FETCH_TOOTHBRUSHES_RESULTS', this.devices);
});
},
async stop() {
if (!this.started) {
return;
}
console.log(`${this.name} stopping hub`);
await this.dongle.destroy();
console.log(`${this.name} hub stopped`);
},
socketNotificationReceived(notification, payload) {
if (notification === 'FETCH_TOOTHBRUSHES') {
this.startHub(payload);
this.sendSocketNotification('FETCH_TOOTHBRUSHES_RESULTS', this.devices);
}
},
});