-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
executable file
·448 lines (407 loc) · 18.7 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
"use strict";
/*
* Landroid Worx plugin for HomeBridge
*
* CloudConnector and all files in the lib folder are copied from
* https://github.com/iobroker-community-adapters/ioBroker.worx
*
*/
const fs = require("fs");
const path_ad = require("path");
var Accessory, Service, Characteristic, UUIDGen, STORAGE_PATH;
var CloudConnector = require('./CloudConnector');
var LandroidDataset = require('./LandroidDataset');
function LandroidPlatform(log, config, api) {
this.config = config;
this.log = log;
this.partymode = config.partymode || false;
this.debug = config.debug || false;
this.mowdata = config.mowdata || false;
this.cloud = config.cloud || "worx";
this.accessories = [];
this.cloudMowers = [];
if(!config.email || !config.pwd){
this.log("WARNING: No account configured, please set email and password of your Worx account in config.json!");
return;
}
const self = this;
this.api = api;
// Listen to event "didFinishLaunching", this means homebridge already finished loading cached accessories.
// Platform Plugin should only register new accessory that doesn't exist in homebridge after this event.
// Or start discover new accessories.
this.api.on('didFinishLaunching', async function () {
self.log('DidFinishLaunching');
if(self.config.reload){
self.log('**** WARNING: Landroid plugin is in reload mode, mowers will be recreated each boot ****');
self.accessories.forEach(accessory => {
self.log('Removing Landroid ' + accessory.accessory.displayName + ' from HomeKit');
self.api.unregisterPlatformAccessories('homebridge-landroid', 'Landroid', [accessory.accessory]);
});
self.accessories = [];
}
self.landroidCloud = CloudConnector();
let persisted_states = self.landroidCloud.loadLandroidObjectData(STORAGE_PATH);
for(var myname in persisted_states) {
self.createUpdate(myname, persisted_states[myname]);
}
self.accessories.forEach(accessory=>{
accessory.landroidCloud = self.landroidCloud;
});
self.landroidCloud.config = {
"mail": config.email,
"password": config.pwd,
"server": config.cloud || "worx"
}
self.landroidCloud.log = new LandroidLogger(log);
self.landroidCloud.log.isDebug = self.debug;
self.landroidCloud.setState = function(objectname,object) {
self.landroidCloud.states[objectname] = object;
self.landroidCloud.saveLandroidObjectData();
if(objectname == "info.connection" && object == true){
self.removeTimeout = setTimeout(self.clearOldMowers.bind(self), 60000);
} else if(objectname.includes(".mower.")){
let serial = objectname.substring(0, objectname.indexOf("."));
let item = objectname.split('.').pop();
if(object && (object.val !== null || object.val !== undefined)) {
self.landroidUpdate(serial, item, object.val);
}
}
};
self.landroidCloud.setObjectNotExists = async function(objectname, object) {
if(!objectname instanceof String) return;
if(!object.common.name instanceof String) return;
if(!self.landroidCloud.objects[objectname]) {
self.landroidCloud.objects[objectname] = {};
}
if(!objectname.includes(".") && object.common.name != "[object Object]"){
self.landroidFound(object.common.name, objectname);
}
};
await self.landroidCloud.onReady();
});
}
LandroidPlatform.prototype.clearOldMowers = function() {
const self = this;
this.accessories.forEach((accessory, idx, obj) => {
if(!self.cloudMowers.includes(accessory.serial)){
self.api.unregisterPlatformAccessories('homebridge-landroid', 'Landroid', [accessory.accessory]);
obj.splice(idx, 1);
}
});
}
// Function invoked when homebridge tries to restore cached accessory.
LandroidPlatform.prototype.configureAccessory = function(accessory) {
if (!this.config) { // happens if plugin is disabled and still active accessories
return;
}
this.log('Restoring Landroid ' + accessory.displayName + ' from HomeKit');
accessory.reachable = false;
this.accessories.push(new LandroidAccessory(this, null, null, accessory));
}
// Handler will be invoked when user try to config your plugin.
// Callback can be cached and invoke when necessary.
LandroidPlatform.prototype.configurationRequestHandler = function(context, request, callback) {
callback(null);
}
LandroidPlatform.prototype.landroidFound = function(name, serial) {
if(this.debug) {
this.log("[DEBUG] MOWER: " + name + " (" + serial + ")");
}else {
this.log("Found Landroid in Worx Cloud with name: " + name);
}
if(this.mowdata) {
this.log("Mowing data logging enabled for Landroid " + name);
}
if(!this.cloudMowers.includes(serial)){
this.cloudMowers.push(serial);
}
for(var i = 0; i<this.accessories.length; i++){
const accessory = this.accessories[i];
if(accessory.serial == serial){
//already have this one
accessory.accessory.reachable = true;
//this.landroidUpdate(mower, data);
return;
}
}
// don't have this one, add it
const newMower = new LandroidAccessory(this, name, serial);
this.accessories.push(newMower);
this.log("Adding Landroid " + name + " to HomeKit");
this.api.registerPlatformAccessories('homebridge-landroid', 'Landroid', [newMower.accessory]);
//this.landroidUpdate(mower,data);
}
LandroidPlatform.prototype.createUpdate = function(objectname, object) {
if(objectname.includes(".mower.")){
let serial = objectname.substring(0, objectname.indexOf("."));
let item = objectname.split('.').pop();
if(object && (object.val !== null || object.val !== undefined)) {
this.landroidUpdate(serial, item, object.val);
}
}
}
LandroidPlatform.prototype.landroidUpdate = function(serial, item, data) {
if(this.debug) {
this.log("[DEBUG] DATA: " + item + ": " + JSON.stringify(data));
}
this.accessories.forEach(accessory=>{
accessory.landroidUpdate(serial, item, data, this.mowdata);
});
}
//TODO: add other constructor
function LandroidAccessory(platform, name, serial, accessory) {
this.landroidCloud = platform.landroidCloud;
this.log = platform.log;
this.config = platform.config;
if (accessory) {
this.accessory = accessory;
} else {
// new accessory object
var uuid = UUIDGen.generate(serial);
//this.log('Creating new accessory for ' + name + ' (' + serial + ')');
this.accessory = new Accessory("Landroid " + name, uuid);
this.accessory.context.name = name;
this.accessory.context.serial = serial;
this.accessory.addService(new Service.Switch("Landroid " + name));
this.accessory.addService(new Service.BatteryService());
this.accessory.addService(new Service.ContactSensor("Landroid " + name + " Problem", "ErrorSensor"));
if(this.config.rainsensor) this.accessory.addService(new Service.LeakSensor("Landroid " + name + " Rain"));
if(this.config.homesensor) this.accessory.addService(new Service.ContactSensor("Landroid " + name + " Home", "HomeSensor"));
if(this.config.partymode) this.accessory.addService(new Service.Switch("Landroid " + name + " PartyMode", "PartySwitch"));
}
this.name = this.accessory.context.name;
this.serial = this.accessory.context.serial;
this.dataset = {};
this.dataset.batteryState = 0;
this.dataset.batteryCharging = false;
this.dataset.statusCode = 0;
this.dataset.errorCode = 0;
if(this.accessory.getService("Landroid " + name)){
this.accessory.getService("Landroid " + name).getCharacteristic(Characteristic.On).on('get', this.getOn.bind(this));
this.accessory.getService("Landroid " + name).getCharacteristic(Characteristic.On).on('set', this.setOn.bind(this));
} else{
this.log("Fallback for On/Off switch");
this.accessory.getService(Service.Switch).getCharacteristic(Characteristic.On).on('get', this.getOn.bind(this));
this.accessory.getService(Service.Switch).getCharacteristic(Characteristic.On).on('set', this.setOn.bind(this));
}
this.accessory.getService(Service.BatteryService).getCharacteristic(Characteristic.BatteryLevel).on('get', this.getBatteryLevel.bind(this));
this.accessory.getService(Service.BatteryService).getCharacteristic(Characteristic.StatusLowBattery).on('get', this.getStatusLowBattery.bind(this));
this.accessory.getService(Service.BatteryService).getCharacteristic(Characteristic.ChargingState).on('get', this.getChargingState.bind(this));
if(this.accessory.getService("ErrorSensor")){
this.accessory.getService("ErrorSensor").getCharacteristic(Characteristic.ContactSensorState).on('get', this.getContactSensorStateError.bind(this));
} else{
this.accessory.getService(Service.ContactSensor).getCharacteristic(Characteristic.ContactSensorState).on('get', this.getContactSensorStateError.bind(this));
}
this.accessory.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Name, this.name)
.setCharacteristic(Characteristic.Manufacturer, 'Worx')
.setCharacteristic(Characteristic.Model, 'Landroid')
.setCharacteristic(Characteristic.SerialNumber, this.serial);
if(this.config.rainsensor && this.accessory.getService(Service.LeakSensor)) this.accessory.getService(Service.LeakSensor).on('get', this.getLeak.bind(this));
if(this.config.homesensor && this.accessory.getService("HomeSensor")) this.accessory.getService("HomeSensor").getCharacteristic(Characteristic.ContactSensorState).on('get', this.getContactSensorStateHome.bind(this));
if(this.config.partymode && this.accessory.getService("PartySwitch")) {
this.accessory.getService("PartySwitch").getCharacteristic(Characteristic.On).on('get', this.getPartyMode.bind(this));
this.accessory.getService("PartySwitch").getCharacteristic(Characteristic.On).on('set', this.setPartyMode.bind(this));
} else if(this.config.partymode) {
this.log("Party switch not found");
}
}
LandroidAccessory.prototype.landroidUpdate = function(serial, item, data, mowdata) {
var totalTime, totalBladeTime, totalDistance;
if(serial !== this.serial) return;
if(data != null && data != undefined){
let oldDataset = this.dataset;
if(item == "status") {
item = "statusCode";
} else if(item == "error") {
item = "errorCode";
}
this.dataset[item] = data;
// this.log("landroidUpdate ran with RSSI " + this.dataset.wifiQuality + ", battery temperature " + this.dataset.batteryTemperature);
if(mowdata){
if(oldDataset.totalTime == null || oldDataset.totalTime == undefined){
// Initialise mowing data
this.saveTime = Number(this.dataset.totalTime);
totalTime = this.saveTime / 60;
totalTime = totalTime.toFixed(2);
this.saveBladeTime = Number(this.dataset.totalBladeTime);
totalBladeTime = this.saveBladeTime / 60;
totalBladeTime = totalBladeTime.toFixed(2);
this.saveDistance = Number(this.dataset.totalDistance);
this.log("Landroid " + this.name + " hours worked so far: " + totalTime
+ ", hours mowed so far: " + totalBladeTime
+ ", distance moved so far: " + String(this.saveDistance / 1000) + "km");
}
}
if(this.dataset.batteryState != oldDataset.batteryState){
// this.log("Landroid " + this.name + " battery level changed to " + this.dataset.batteryState);
this.accessory.getService(Service.BatteryService).getCharacteristic(Characteristic.BatteryLevel).updateValue(this.dataset.batteryState);
}
if(this.dataset.partyModus != oldDataset.partyModus){
if(this.accessory.getService("PartySwitch")){
this.accessory.getService("PartySwitch").getCharacteristic(Characteristic.On).updateValue(this.dataset.partyModus == true);
}
}
if(this.dataset.batteryCharging != oldDataset.batteryCharging){
this.log("Landroid " + this.name + " charging status changed to " + this.dataset.batteryCharging
+ ", battery level " + this.dataset.batteryState);
this.accessory.getService(Service.BatteryService).getCharacteristic(Characteristic.ChargingState).updateValue(this.dataset.batteryCharging?
Characteristic.ChargingState.CHARGING:Characteristic.ChargingState.NOT_CHARGING);
}
if(this.dataset.statusCode != oldDataset.statusCode){
this.log("Landroid " + this.name + " status changed to " + this.dataset.statusCode + " (" + this.dataset.statusDescription + ")"
+ ", battery level " + this.dataset.batteryState);
if(isOn(this.dataset.statusCode)){
this.accessory.getService(Service.Switch).getCharacteristic(Characteristic.On).updateValue(true);
if(this.config.homesensor && this.accessory.getService("HomeSensor")) this.accessory.getService("HomeSensor").getCharacteristic(Characteristic.ContactSensorState).updateValue(Characteristic.ContactSensorState.CONTACT_NOT_DETECTED);
}else{
this.accessory.getService(Service.Switch).getCharacteristic(Characteristic.On).updateValue(false);
}
if(this.dataset.statusCode == 1 && oldDataset.totalTime != null && oldDataset.totalTime != undefined && mowdata) {
// Landroid has just arrived home so show how much it's worked since last leaving (or last restarting Homebridge)
totalTime = Number(this.dataset.totalTime);
totalBladeTime = Number(this.dataset.totalBladeTime);
totalDistance = Number(this.dataset.totalDistance);
this.log("Landroid " + this.name + " new minutes worked: " + String(totalTime - this.saveTime)
+ ", new minutes mowed: " + String(totalBladeTime - this.saveBladeTime)
+ ", new distance moved: " + String(totalDistance - this.saveDistance) + "m");
this.saveTime = totalTime;
this.saveBladeTime = totalBladeTime;
this.saveDistance = totalDistance;
}
if(this.dataset.statusCode == 1 && this.config.homesensor && this.accessory.getService("HomeSensor")) this.accessory.getService("HomeSensor").getCharacteristic(Characteristic.ContactSensorState).updateValue(Characteristic.ContactSensorState.CONTACT_DETECTED);
}
if(this.dataset.errorCode != oldDataset.errorCode){
this.log("Landroid " + this.name + " error code changed to " + this.dataset.errorCode + " (" + this.dataset.errorDescription + ")"
+ ", battery level " + this.dataset.batteryState);
if(this.accessory.getService("ErrorSensor")){
this.accessory.getService("ErrorSensor").getCharacteristic(Characteristic.ContactSensorState).updateValue(isError(this.dataset.errorCode)?
Characteristic.ContactSensorState.CONTACT_NOT_DETECTED:Characteristic.ContactSensorState.CONTACT_DETECTED);
} else{
this.accessory.getService(Service.ContactSensor).getCharacteristic(Characteristic.ContactSensorState).updateValue(isError(this.dataset.errorCode)?
Characteristic.ContactSensorState.CONTACT_NOT_DETECTED:Characteristic.ContactSensorState.CONTACT_DETECTED);
}
if(this.config.rainsensor && this.accessory.getService(Service.LeakSensor)) this.accessory.getService(Service.LeakSensor).getCharacteristic(Characteristic.LeakDetected).updateValue(this.dataset.errorCode == 5);
}
}
}
LandroidAccessory.prototype.getContactSensorStateError = function(callback) {
callback(null, isError(this.dataset.errorCode)?Characteristic.ContactSensorState.CONTACT_NOT_DETECTED:Characteristic.ContactSensorState.CONTACT_DETECTED);
}
LandroidAccessory.prototype.getContactSensorStateHome = function(callback) {
callback(null, this.dataset.statusCode == 1?Characteristic.ContactSensorState.CONTACT_DETECTED:Characteristic.ContactSensorState.CONTACT_NOT_DETECTED);
}
LandroidAccessory.prototype.getBatteryLevel = function(callback) {
callback(null, this.dataset.batteryState);
}
LandroidAccessory.prototype.getChargingState = function(callback) {
callback(null, this.dataset.batteryCharging?Characteristic.ChargingState.CHARGING:Characteristic.ChargingState.NOT_CHARGING);
}
LandroidAccessory.prototype.getStatusLowBattery = function(callback) {
callback(null, this.dataset.errorCode == 12?Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW:Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL);
}
LandroidAccessory.prototype.getOn = function(callback) {
if(isOn(this.dataset.statusCode)){
callback(null, true);
}else{
callback(null, false);
}
}
LandroidAccessory.prototype.getLeak = function(callback) {
if(this.dataset.statusCode == 5){
callback(null, true);
}else{
callback(null, false);
}
}
LandroidAccessory.prototype.setOn = function(state, callback) {
if(state){
this.sendMessage(1);
}else{
this.sendMessage(3);
}
callback(null);
}
LandroidAccessory.prototype.getPartyMode = function(callback) {
if(this.dataset.partyModus) {
callback(null, true);
}else{
callback(null, false);
}
}
LandroidAccessory.prototype.setPartyMode = function(state, callback) {
if(!this.serial){
this.log("Error: Mower has not been configured yet.");
}
let outMsg = "";
if (state) {
outMsg = '{"sc":{ "m":2, "distm": 0}}';
} else{
outMsg = '{"sc":{ "m":1, "distm": 0}}';
}
this.log("Sending to Landroid " + this.name + ": [" + outMsg + "] ("+this.serial+")");
this.landroidCloud.sendMessage(outMsg, this.serial);
}
LandroidAccessory.prototype.sendMessage = function(cmd, params) {
if(!this.serial){
this.log("Error: Mower has not been configured yet.");
}
let message = {};
if (cmd) {
message["cmd"] = cmd;
}
if (params) {
message = Object.assign(message, params);
}
let outMsg = JSON.stringify(message);
this.log("Sending to Landroid " + this.name + ": [" + outMsg + "] ("+this.serial+")");
this.landroidCloud.sendMessage(outMsg, this.serial);
}
function isOn(c){
if(c == 2 || c == 3 || c == 4 || c == 6 || c == 7 || c == 32 || c == 33){
return true;
}else{
return false;
}
}
function isError(c){
//no error and rain delay is "not an error"
if(c == 0 || c == 5){
return false;
}else{
return true;
}
}
function LandroidLogger(log){
let that = this;
this.log = log;
this.logMsg = function(msg){
that.log(msg);
}
this.noLogMsg = function(msg){
if(this.isDebug){
that.log(msg);
}
}
this.trace = this.noLogMsg;
this.debug = this.noLogMsg;
this.info = this.logMsg;
this.warn = this.logMsg;
this.error = this.logMsg;
this.fatal = this.logMsg;
}
function updateStorage(newPath){
var confPath = newPath + "/plugin-persist/homebridge-landroid";
if(!fs.existsSync(confPath)){
fs.mkdirSync(confPath, {recursive: true});
}
return confPath;
}
module.exports = function(homebridge) {
Accessory = homebridge.platformAccessory;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
STORAGE_PATH = updateStorage(homebridge.user.storagePath());
homebridge.registerPlatform("homebridge-landroid", "Landroid", LandroidPlatform, true);
}