-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnode_helper.js
131 lines (104 loc) · 2.94 KB
/
node_helper.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* Magic Mirror
* Node Helper: MMM-Roomba
*
* By Reagan Elm
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const Dorita980 = require('dorita980');
const REQUIRED_ROOT_FIELDS = ['robots'];
const REQUIRED_ROBOTS_FIELDS = ['username', 'password', 'ipAddress'];
const ROOMBA_STATS = ['bin', 'name', 'batPct', 'cleanMissionStatus'];
module.exports = NodeHelper.create({
start: function () {
const self = this;
self.started = false;
self.config = [];
self.stats = [];
},
socketNotificationReceived: function (notification, payload) {
const self = this;
switch (notification) {
case 'START':
self.handleStartNotification(payload);
}
},
handleStartNotification: function (payload) {
const self = this;
if (self.started) {
return;
}
self.config = payload;
if (self.isInvalidConfig()) {
return;
}
self.scheduleUpdates();
self.started = true;
},
updateStats: function (index) {
const self = this;
const { ipAddress, password, username } = self.config.robots[index];
const roomba = new Dorita980.Local(username, password, ipAddress);
roomba
.getRobotState(ROOMBA_STATS)
.then(({ batPct, bin, cleanMissionStatus, name }) => {
self.stats[index] = self.stats[index] || {};
Object.assign(self.stats[index], {
name,
binFull: bin.full,
batteryPercent: batPct,
phase: cleanMissionStatus.phase,
});
roomba.end();
self.sendSocketNotification('STATS', self.stats);
})
.catch((err) => {
console.error('Error occurred while fetching stats', err);
roomba.end();
});
},
isInvalidConfig: function () {
const self = this;
const missingRootField = REQUIRED_ROOT_FIELDS.find((field) => {
return !self.config[field];
});
if (missingRootField) {
self.sendSocketNotification(
'ERROR',
`<i>Confg.${missingRootField}</i> is required for module: ${self.name}.`
);
return true;
}
if (!Array.isArray(self.config.robots)) {
self.sendSocketNotification(
'ERROR',
`<i>Confg.robots</i> is required to be an array for module: ${self.name}.`
);
return true;
}
let missingRobotsField;
self.config.robots.forEach((robot) => {
const missingField = REQUIRED_ROBOTS_FIELDS.find(
(field) => !robot[field]
);
if (missingField) {
missingRobotsField = missingField;
}
});
if (missingRobotsField) {
self.sendSocketNotification(
'ERROR',
`<i>Confg.robots[].${missingRobotsField}</i> is required for module: ${self.name}.`
);
return true;
}
return false;
},
scheduleUpdates() {
const self = this;
self.config.robots.forEach((_, index) => {
self.updateStats(index);
setInterval(() => self.updateStats(index), self.config.updateInterval);
});
},
});