-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·212 lines (178 loc) · 7.14 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
var Service, Characteristic;
const fetch = require('node-fetch');
const url = require('url');
let setupOK = false;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-robonect", "HomebridgeRobonect", myRobo);
};
function myRobo(log, config) {
this.config = config;
this.log = log;
/* URLS */
if(typeof config.getUrl === 'string' && isValidHttpUrl(config.getUrl)){
this.getUrl = url.parse(config.getUrl);
setupOK = true;
}else{
this.log("URL not properly configured, plugin will not work.");
}
function isValidHttpUrl(string) {
let url;
try {
url = new URL(string);
} catch (_) {
return false;
}
return url.protocol === "http:" || url.protocol === "https:";
}
this.statusUrl = url.parse(config.getUrl + '/json?cmd=status');
this.healthUrl = url.parse(config.getUrl + '/json?cmd=health');
this.versionUrl = url.parse(config.getUrl + '/json?cmd=version');
this.setAutoModeUrl = url.parse(config.getUrl + '/json?cmd=mode&mode=auto');
this.setHomeModeUrl = url.parse(config.getUrl + '/json?cmd=mode&mode=home');
this.setEodModeUrl = url.parse(config.getUrl + '/json?cmd=mode&mode=eod');
this.stopUrl = url.parse(config.getUrl + '/json?cmd=stop');
this.startUrl = url.parse(config.getUrl + '/json?cmd=start');
/* Static config values */
this.manufactInfo = config.mower || "Generic Mower";
this.modelInfo = config.model || "Generic Model";
this.serialNumberInfo = config['serial-number'] || "12345";
this.pollingInterval = config.pollingInterval || 60;
this.fanMode = config.fanMode || 0;
this.showHealth = config.showHealth || 1;
if(this.pollingInterval < 30 || isNaN(this.pollingInterval)){
this.pollingInterval = 60000;
} else {
this.pollingInterval = this.pollingInterval * 1000;
}
}
myRobo.prototype = {
getServices: function () {
async function populateJson(me) {
try{
const responses = await Promise.all([fetch(me.statusUrl), fetch(me.healthUrl)])
const [status, health] = await Promise.all(responses.map(res => res.json()))
await updateDevices(me, status, health);
}catch(err){
me.log("Could not fetch status values :( ");
}
}
function updateDevices(me, statusJson, healthJson){
/* Is mower in auto or home mode */
me.switchService.getCharacteristic(Characteristic.On).updateValue((statusJson.status.mode === 0 || statusJson.status.mode === 1) ? true : false);
/* Is mower mowing */
me.fanService.getCharacteristic(Characteristic.On).updateValue((statusJson.status.status === 2 || statusJson.status.status === 5) ? 1 : 0);
/* Update battery level */
me.batteryService.getCharacteristic(Characteristic.BatteryLevel).updateValue(statusJson.status.battery);
/* Update charging status */
me.batteryService.getCharacteristic(Characteristic.ChargingState).updateValue((statusJson.status.status == 4) ? 1 : 0);
/* Update low battery warning */
me.batteryService.getCharacteristic(Characteristic.StatusLowBattery).updateValue((statusJson.status.battery < 20) ? 1 : 0);
if(me.showHealth === 1){
/* Update humidity level */
me.humidityService.getCharacteristic(Characteristic.CurrentRelativeHumidity).updateValue(healthJson.health.climate.humidity);
/* Update temperature */
me.tempService.getCharacteristic(Characteristic.CurrentTemperature).updateValue(healthJson.health.climate.temperature);
}
/* Check if mower has an error */
me.motionService.getCharacteristic(Characteristic.MotionDetected).updateValue((statusJson.status.status === 7 || statusJson.status.status === 8) ? true : false);
/* Chatty log */
//me.log("Updating status values every " + me.pollingInterval/1000 + "s");
}
if(setupOK){
populateJson(this);
setInterval(() => { populateJson(this) }, this.pollingInterval);
}
this.services = [];
/* Information Service */
let informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, this.manufactInfo)
.setCharacteristic(Characteristic.Model, this.modelInfo)
.setCharacteristic(Characteristic.SerialNumber, this.serialNumberInfo);
this.services.push(informationService);
/* Switch Service */
let switchService = new Service.Switch("Auto");
switchService
.getCharacteristic(Characteristic.On).on('set', this.setSwitchOnCharacteristic.bind(this));
this.services.push(switchService);
/* Fan Service */
let fanService = new Service.Fan("Mowing");
fanService
.getCharacteristic(Characteristic.On)
.on('set', this.setMowerOnCharacteristic.bind(this));
this.services.push(fanService);
/* Battery Service */
let batteryService = new Service.BatteryService();
this.services.push(batteryService);
if(this.showHealth === 1){
/* HumidityService */
let humidityService = new Service.HumiditySensor("Humidity");
this.services.push(humidityService);
this.humidityService = humidityService;
/* Temperature Service */
let tempService = new Service.TemperatureSensor("Temperature");
this.services.push(tempService);
this.tempService = tempService;
}
/* Motion sensor Service */
let motionService = new Service.MotionSensor("Mower Error");
this.services.push(motionService);
this.switchService = switchService;
this.fanService = fanService;
this.informationService = informationService;
this.batteryService = batteryService;
this.motionService = motionService;
switchService.setPrimaryService(true);
return this.services;
},
setSwitchOnCharacteristic: function (on, next) {
const me = this;
if(on){
me.setModeUrl = me.setAutoModeUrl;
me.log("Setting auto mode");
}else{
me.setModeUrl = me.setHomeModeUrl;
me.fanService.getCharacteristic(Characteristic.On).updateValue(0);
me.log("Setting home mode");
}
fetch(me.setModeUrl).catch(error => {
me.log("Set Switch on error.");
//return next(error);
});
return next();
},
setMowerOnCharacteristic: function (on, next) {
const me = this;
if(on){
if(me.switchService.getCharacteristic(Characteristic.On).value === true){
if(me.fanMode === 1){
me.setModeUrl = me.startUrl;
me.log("Starting mower ");
}else{
me.setModeUrl = me.setAutoModeUrl;
me.log("Setting auto mode ");
}
}else{
me.fanService.getCharacteristic(Characteristic.On).updateValue(0);
me.log("Mower not in auto mode");
me.setModeUrl = me.setHomeModeUrl;
}
}else{
if(me.fanMode === 1){
me.setModeUrl = me.stopUrl;
me.log("Stopping mower ");
}else{
me.setModeUrl = me.setEodModeUrl;
me.log("Setting EOD mode ");
}
me.fanService.getCharacteristic(Characteristic.On).updateValue(0);
}
fetch(me.setModeUrl).catch(error => {
me.log("Set Mower on error.");
//return next(error);
});
return next();
}
};