forked from naofireblade/homebridge-neato
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
305 lines (280 loc) · 8.34 KB
/
index.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
"use strict";
let inherits = require('util').inherits,
debug = require('debug')('homebridge-neato'),
botvac = require('node-botvac'),
Service,
Characteristic,
NeatoVacuumRobotAccessory;
module.exports = function (homebridge)
{
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
NeatoVacuumRobotAccessory = require('./accessories/neatoVacuumRobot')(Service, Characteristic);
homebridge.registerPlatform("homebridge-neato", "NeatoVacuumRobot", NeatoVacuumRobotPlatform);
};
function NeatoVacuumRobotPlatform(log, config)
{
this.log = log;
this.serial = "1-3-3-7";
this.email = config['email'];
this.password = config['password'];
this.hiddenServices = '';
this.hiddenServices = ('disabled' in config ? config['disabled'] : this.hiddenServices);
this.hiddenServices = ('hidden' in config ? config['hidden'] : this.hiddenServices);
// Array of real robots and associated robot accessories (incl rooms)
this.robots = [];
this.nextRoom = null;
if ('refresh' in config && config['refresh'] !== 'auto')
{
// parse config parameter
this.refresh = parseInt(config['refresh']);
// must be integer and positive
this.refresh = (typeof this.refresh !== 'number' || (this.refresh % 1) !== 0 || this.refresh < 0) ? 60 : this.refresh;
// minimum 60s to save some load on the neato servers
if (this.refresh > 0 && this.refresh < 60)
{
this.log.warn("Minimum refresh time is 60 seconds to not overload the neato servers");
this.refresh = (this.refresh > 0 && this.refresh < 60) ? 60 : this.refresh;
}
}
// default auto
else
{
this.refresh = 'auto';
}
this.log("Refresh is set to: " + this.refresh + (this.refresh !== 'auto' ? ' seconds' : ''));
}
NeatoVacuumRobotPlatform.prototype = {
accessories: function (callback)
{
debug("Get robots");
let accessories = [];
this.boundaryNames = [];
this.getRobots(() =>
{
// // MOCK MULTIPLE ROBOTS START
// let client = new botvac.Client();
// client.authorize(this.email, this.password, false, (error) =>
// {
// client.getRobots((error, robs) =>
// {
// let testRobot = robs[0];
// testRobot.getState((error, result) =>
// {
// testRobot.name = "Testrobot";
// this.robots.push({device: testRobot, meta: result.meta, availableServices: result.availableServices});
// // MOCK MULTIPLE ROBOTS END
this.robots.forEach((robot, i) =>
{
this.log("Found robot #" + (i + 1) + " named \"" + robot.device.name + "\" with serial \"" + robot.device._serial.substring(0, 9) + "XXXXXXXXXXXX\"");
let mainAccessory = new NeatoVacuumRobotAccessory(this, robot);
accessories.push(mainAccessory);
robot.mainAccessory = mainAccessory;
robot.roomAccessories = [];
// Start Update Intervall
this.updateRobotTimer(robot.device._serial);
// // MOCK ZONE CLEANING START
// robot.boundary = {name: "Testroom", id: "1"};
// let roomAccessory = new NeatoVacuumRobotAccessory(this, robot);
// accessories.push(roomAccessory);
// robot.roomAccessories.push(roomAccessory);
// // MOCK ZONE CLEANING END
if (robot.device.maps)
{
robot.device.maps.forEach((map) =>
{
if (map.boundaries)
{
map.boundaries.forEach((boundary) =>
{
if (boundary.type === "polygon")
{
robot.boundary = boundary;
let roomAccessory = new NeatoVacuumRobotAccessory(this, robot);
accessories.push(roomAccessory);
robot.roomAccessories.push(roomAccessory);
}
})
}
})
}
});
callback(accessories);
// // MOCK MULTIPLE ROBOTS START
// });
// });
// });
// // MOCK MULTIPLE ROBOTS END
});
},
getRobots: function (callback)
{
debug("Loading your robots");
let client = new botvac.Client();
// Login
client.authorize(this.email, this.password, false, (error) =>
{
if (error)
{
this.log.error("Can't log on to neato cloud. Please check your internet connection and your credentials. Try again later if the neato servers have issues: " + error);
callback();
}
else
{
// Get all robots
client.getRobots((error, robots) =>
{
if (error)
{
this.log.error("Successful login but can't connect to your neato robot: " + error);
callback();
}
else if (robots.length === 0)
{
this.log.error("Successful login but no robots associated with your account.");
this.robots = [];
callback();
}
else
{
debug("Found " + robots.length + " robots");
let loadedRobots = 0;
robots.forEach((robot) =>
{
// Get additional information for the robot
robot.getState((error, state) =>
{
if (error)
{
this.log.error("Error getting robot meta information: " + error + ": " + state);
callback();
}
else
{
// Get all maps for each robot
robot.getPersistentMaps((error, maps) =>
{
if (error)
{
this.log.error("Error updating persistent maps: " + error + ": " + maps);
callback();
}
// Robot has no maps
else if (maps.length === 0)
{
robot.maps = [];
this.robots.push({device: robot, meta: state.meta, availableServices: state.availableServices});
loadedRobots++;
if (loadedRobots === robots.length)
{
callback();
}
}
// Robot has maps
else
{
robot.maps = maps;
let loadedMaps = 0;
robot.maps.forEach((map) =>
{
// Save zones in each map
robot.getMapBoundaries(map.id, (error, result) =>
{
if (error)
{
this.log.error("Error getting boundaries: " + error + ": " + result)
}
else
{
map.boundaries = result.boundaries;
}
loadedMaps++;
// Robot is completely requested if zones for all maps are loaded
if (loadedMaps === robot.maps.length)
{
this.robots.push({device: robot, meta: state.meta, availableServices: state.availableServices});
loadedRobots++;
if (loadedRobots === robots.length)
{
callback();
}
}
})
});
}
});
}
});
});
}
});
}
});
},
updateRobot: function (serial, callback)
{
let robot = this.getRobot(serial);
// Data is up to date
if (typeof (robot.lastUpdate) !== 'undefined' && new Date() - robot.lastUpdate < 2000)
{
callback();
}
else
{
debug(robot.device.name + ": ++ Updating robot state");
robot.lastUpdate = new Date();
robot.device.getState((error, result) =>
{
if (error)
{
this.log.error("Cannot update robot. Check if robot is online. " + error);
}
callback();
});
}
},
getRobot(serial)
{
let result;
this.robots.forEach(function (robot)
{
if (robot.device._serial === serial)
{
result = robot;
}
});
return result;
},
updateRobotTimer: function (serial)
{
this.updateRobot(serial, () =>
{
let robot = this.getRobot(serial);
// Clear any other overlapping timers for this robot
clearTimeout(robot.timer);
// Tell all accessories of this robot (mainAccessory and roomAccessories) that updated robot data is available
robot.mainAccessory.updated();
robot.roomAccessories.forEach(accessory =>
{
accessory.updated();
});
// Periodic refresh interval set in config
if (this.refresh !== 'auto' && this.refresh !== 0)
{
debug(robot.device.name + ": ++ Next background update in " + this.refresh + " seconds");
robot.timer = setTimeout(this.updateRobotTimer.bind(this), this.refresh * 1000, serial);
}
// Auto refresh set in config
else if (this.refresh === 'auto' && robot.device.canPause)
{
debug(robot.device.name + ": ++ Next background update in 60 seconds while cleaning (auto mode)");
robot.timer = setTimeout(this.updateRobotTimer.bind(this), 60 * 1000, serial);
}
// No refresh
else
{
debug(robot.device.name + ": ++ Stopped background updates");
}
});
},
};