This repository has been archived by the owner on Jan 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
375 lines (283 loc) · 11.9 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
var Service, Characteristic;
var dorita980 = require('dorita980');
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-roomba690", "Roomba690", Roomba690Accessory);
}
/**
* Roomba690Accessory
*
* @method Roomba690Accessory
* @param {Object} log
* @param {Object} config config.json
* @param {String} config.name Name of Roomba to show in Home app
* @param {String} config.blid Roomba's BLID
* @param {String} config.password Roomba's password
* @param {String} config.hostname Roomba's IP address
* @param {String} config.model Model of Roomba to show in Home app
*
*/
function Roomba690Accessory(log, config) {
this.log = log;
this.blid = config["blid"];
this.password = config["password"];
this.name = config["name"];
this.hostname = config["hostname"];
this.model = config["model"];
log("Initialised Roomba with Name: [%s] Hostname: [%s] BLID: [%s] Model: [%s]", this.name, this.hostname, this.blid, this.model);
}
/**
* Roomba690Accessory
*
* Provides the following functions:
* setPowerState()
* getPowerState()
* getIsCharging()
* getBatteryLevel()
* identify()
* getServices()
*/
Roomba690Accessory.prototype = {
/**
* setPowerState
*
* @method setPowerState
* @param {int} state 0 or 1
* @param {function} callback
*
* Setting state to 0 will pause the Roomba and request that it docks.
*
*/
setPowerState: function(state, callback) {
var log = this.log;
log("Request to set power state to [%s]", state);
var myRobotViaLocal = new dorita980.Local(this.blid, this.password, this.hostname);
if (state) {
log("Starting Roomba");
myRobotViaLocal.on('connect', function() {
log("Connected to Roomba");
myRobotViaLocal.start().then((response) => {
myRobotViaLocal.end();
log("Roomba started.");
log(response);
callback();
}).catch((err) => {
myRobotViaLocal.end();
log("Failed to start Roomba. Error was [%s]", err.message);
log(response);
callback(err);
});
});
} else {
// The below code block is essentially the same as homebridge-roomba980
// with the exception of getMission, which is changed to getRobotState.
// Logging has been tweaked a little, but the core code is the same.
// TODO - Do we need to accommodate the "pause" phase in our dock request?
log("Pausing Roomba");
myRobotViaLocal.on('connect', function () {
myRobotViaLocal.pause().then((response) => {
log('Roomba is paused');
// We call back so Siri can show success.
callback();
// We still have to dock!
log("Requesting that Roomba return to dock")
var checkStatus = function (time) {
setTimeout(
function () {
log('Checking Roomba status');
myRobotViaLocal.getRobotState(['cleanMissionStatus']).then((function(state) {
log ("Status is [%s]", state.cleanMissionStatus.phase);
switch (state.cleanMissionStatus.phase) {
case "stop":
log("Roomba has stopped, issuing dock request");
myRobotViaLocal.dock().then(((response) => {
myRobotViaLocal.end();
log('Roomba docking');
//callback();
})).catch((err) => {
log('Roomba failed: %s', err.message);
log(response);
});
break;
case "run":
log('Roomba is still running. Will check again in 3 seconds');
checkStatus(3000);
break;
default:
myRobotViaLocal.end();
log('Roomba is not running');
break;
}
})).catch(function (err) {
log(err);
});
}, time
);
};
checkStatus(3000);
}).catch((err) => {
log('Stopping/docking Roomba failed: %s', err.message);
log(response);
callback(err);
});
});
}
},
/* End setPowerState */
/**
* getPowerState
*
* @method getPowerState
* @param {function} callback
* @return {Number} status
*
* Returns 1 if cleanMissionStatus.phase = run
*
*/
getPowerState: function(callback) {
var log = this.log;
log("Power state requested for Roomba");
var myRobotViaLocal = new dorita980.Local(this.blid, this.password, this.hostname);
myRobotViaLocal.on('connect', function() {
log("Connected to Roomba");
myRobotViaLocal.getRobotState(['cleanMissionStatus']).then((function(state) {
myRobotViaLocal.end();
log ("Status is [%s]", state.cleanMissionStatus.phase);
switch (state.cleanMissionStatus.phase) {
case "run":
log("Roomba is running");
callback(null, 1);
break;
default:
log("Roomba is not running");
callback(null, 0);
break;
}
})).catch(function(err) {
myRobotViaLocal.end();
log("Unable to determine power state of Roomba");
log(err);
callback(err);
});
});
},
/* End getPowerState */
/**
* getPowerState
*
* @method getPowerState
* @param {function} callback
* @return {Characteristic.ChargingState}
*
*/
getIsCharging: function(callback) {
var log = this.log;
log("Charging status requested for Roomba");
var myRobotViaLocal = new dorita980.Local(this.blid, this.password, this.hostname);
myRobotViaLocal.on('connect', function() {
log("Connected to Roomba");
myRobotViaLocal.getRobotState(['cleanMissionStatus']).then((function(state) {
myRobotViaLocal.end();
log ("Status is [%s]", state.cleanMissionStatus.phase);
switch (state.cleanMissionStatus.phase) {
case "charge":
log("Roomba is charging");
callback(null, Characteristic.ChargingState.CHARGING);
break;
default:
log(state.cleanMissionStatus.phase);
log("Roomba is not charging");
callback(null, Characteristic.ChargingState.NOT_CHARGING);
break;
}
})).catch(function(err) {
myRobotViaLocal.end();
log("Unable to determine charging status for Roomba");
log(err);
callback(err);
});
});
},
/* End getIsCharging */
/**
* getBatteryLevel
*
* @method getBatteryLevel
* @param {function} callback
* @return {Number} batteryLevel
*
* Returns a float representation of the battery level
*
*/
getBatteryLevel: function(callback) {
var log = this.log;
log("Battery level requested for Roomba");
var myRobotViaLocal = new dorita980.Local(this.blid, this.password, this.hostname);
myRobotViaLocal.on('connect', function() {
log("Connected");
myRobotViaLocal.getRobotState(['batPct']).then((function(state) {
myRobotViaLocal.end();
log("Roomba battery level [%s]", state.batPct);
callback(null, state.batPct);
})).catch(function(err) {
myRobotViaLocal.end();
log("Unable to determine battery level");
log(err);
callback(err);
});
});
},
/* End getBatteryLevel */
/**
* identify
* If it is ever supported by dorita980, it'd be cool to have the Roomba play
* a sound when identify() is called.
*
* As we can't yet do that, this method will always return successful to keep Siri happy.
*
* @method identify
* @param {function} callback
*
*/
identify: function(callback) {
this.log("Identify requested. Not supported yet.");
callback();
}, /* End identify */
/**
* getServices
*
* Roomba690 supports the following Services:
* AccessoryInformation
* BatteryService
* Switch
*
* @method identify
* @param {function} callback
* @return {Array} services
*
*/
getServices: function() {
var log = this.log;
log("Services requested");
/* Roomba Information */
// Populates the info box for the accessory in the Home app
var accessoryInfo = new Service.AccessoryInformation();
accessoryInfo.setCharacteristic(Characteristic.Manufacturer, "iRobot"); // No need to dynamically return this.
accessoryInfo.setCharacteristic(Characteristic.SerialNumber, "See iRobot App"); // dorita980 doesn't yet support a query for serial number
accessoryInfo.setCharacteristic(Characteristic.Identify, false); // Disable idenitify capability
accessoryInfo.setCharacteristic(Characteristic.Name, this.name); // Pull from conifguration
accessoryInfo.setCharacteristic(Characteristic.Model, this.model); // Pull from configuration
/* Battery Service */
// Allows for Siri commands such as "Is the Roomba charging?" or "What is the battery percentage of the Roomba?"
var batteryService = new Service.BatteryService();
batteryService.getCharacteristic(Characteristic.BatteryLevel).on('get', this.getBatteryLevel.bind(this));
batteryService.getCharacteristic(Characteristic.ChargingState).on('get', this.getIsCharging.bind(this));
/* Switch Service */
// Supports our on/off for the Roomba
var switchService = new Service.Switch(this.name);
switchService.getCharacteristic(Characteristic.On).on('get', this.getPowerState.bind(this)).on('set', this.setPowerState.bind(this));
log("Reporting that we support AccessoryInformation, SwitchService and BatteryService");
return [accessoryInfo, switchService, batteryService];
} /* End getServices */
}