-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
320 lines (275 loc) · 11.1 KB
/
app.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
/*
Copyright 2017 - 2023, Robin de Gruijter ([email protected])
This file is part of com.gruijter.netgear.
com.gruijter.netgear is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
com.gruijter.netgear is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with com.gruijter.netgear. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
const Homey = require('homey');
const _test = require('netgear/test/_test');
const Logger = require('./captureLogs');
// require('inspector').open(9229, '0.0.0.0', false);
class MyApp extends Homey.App {
onInit() {
if (!this.logger) this.logger = new Logger({ name: 'netgearLog', length: 200, homey: this.homey });
this.log('Netgear App is running!');
// register some listeners
process.on('unhandledRejection', (error) => {
this.error('unhandledRejection! ', error);
});
process.on('uncaughtException', (error) => {
this.error('uncaughtException! ', error);
});
this.homey
.on('unload', () => {
this.log('app unload called');
// save logs to persistant storage
this.logger.saveLogs();
})
.on('memwarn', () => {
this.log('memwarn!');
});
this.registerFlowListeners();
// do garbage collection every 10 minutes
// this.intervalIdGc = setInterval(() => {
// global.gc();
// }, 1000 * 60 * 10);
}
// stuff for frontend API
deleteLogs() {
return this.logger.deleteLogs();
}
getLogs() {
return this.logger.logArray;
}
async emitTestResults(data) {
try {
const options = {
password: data.password,
host: data.host,
port: Number(data.port),
info: `Homey fw:${this.homey.version} app: ${this.homey.manifest.version}`,
// shorttest: true,
};
const output = await _test.test(options);
this.homey.api.realtime('test_results', output);
this.log('test ready');
} catch (error) {
this.homey.api.realtime('test_results', error);
}
}
runTest(data) {
this.log('Router compatibility test started');
this.emitTestResults(data);
return true;
}
discover() {
this.log('Router discovery started');
const discover = _test.discover();
return Promise.resolve(discover);
}
async getKnownDevices() {
this.log('Retrieving known devices list');
const driver = this.homey.drivers.getDriver('netgear');
const routers = driver.getDevices();
// const kdArray = [];
// routers.forEach((router) => { kdArray[router.getName()] = router.knownDevices; });
return Promise.resolve(routers[0].knownDevices);
}
registerFlowListeners() {
// autocomplete function for Netgear driver
const autoComplete = (query, args) => {
try {
const list = [];
Object.keys(args.device.knownDevices).forEach((key) => {
const device = args.device.knownDevices[key];
if (!device.MAC) { return; }
list.push({
name: device.MAC,
description: device.Name || 'unknown',
});
});
const results = list.filter((result) => { // filter for query on MAC and Name
const macFound = result.name.toLowerCase().indexOf(query.toLowerCase()) > -1;
const nameFound = result.description.toLowerCase().indexOf(query.toLowerCase()) > -1;
return macFound || nameFound;
});
return Promise.resolve(results);
} catch (error) {
return Promise.reject(error);
}
};
// trigger cards
this._cameOnline = this.homey.flow.getDeviceTriggerCard('came_online');
this.triggerCameOnline = (device, tokens, state) => {
this._cameOnline
.trigger(device, tokens, state)
// .then(this.log(device.getName(), tokens))
.catch(this.error);
};
this._wentOffline = this.homey.flow.getDeviceTriggerCard('went_offline');
this.triggerWentOffline = (device, tokens, state) => {
this._wentOffline
.trigger(device, tokens, state)
// .then(this.log(device.getName(), tokens))
.catch(this.error);
};
this._nameChanged = this.homey.flow.getDeviceTriggerCard('name_changed');
this.triggerNameChanged = (device, tokens, state) => {
this._nameChanged
.trigger(device, tokens, state)
// .then(this.log(device.getName(), tokens))
.catch(this.error);
};
this._IPChanged = this.homey.flow.getDeviceTriggerCard('ip_changed');
this.triggerIPChanged = (device, tokens, state) => {
this._IPChanged
.trigger(device, tokens, state)
// .then(this.log(device.getName(), tokens))
.catch(this.error);
};
// trigger cards for attachedDevice
this._metricsChanged = this.homey.flow.getDeviceTriggerCard('device_metrics_changed');
this.triggerMetricsChanged = (device, tokens, state) => {
this._metricsChanged
.trigger(device, tokens, state)
// .then(this.log(device.getName(), tokens))
.catch(this.error);
};
// trigger cards for Netgear driver
this._speedChanged = this.homey.flow.getDeviceTriggerCard('uldl_speed_changed');
this.triggerSpeedChanged = (device, tokens, state) => {
this._speedChanged
.trigger(device, tokens, state)
// .then(this.log(device.getName(), tokens))
.catch(this.error);
};
this._newAttachedDevice = this.homey.flow.getDeviceTriggerCard('new_attached_device');
this.triggerNewAttachedDevice = (device, tokens, state) => {
this._newAttachedDevice
.trigger(device, tokens, state)
// .then(this.log(device.getName(), tokens))
.catch(this.error);
};
// this._deviceOnline = this.homey.flow.getDeviceTriggerCard('device_online');
// this.triggerDeviceOnline = (device, tokens, state) => {
// this._deviceOnline
// .trigger(device, tokens, state)
// // .then(this.log(device.getName(), tokens))
// .catch(this.error);
// };
// this._deviceOffline = this.homey.flow.getDeviceTriggerCard('device_offline');
// this.triggerDeviceOffline = (device, tokens, state) => {
// this._deviceOffline
// .trigger(device, tokens, state)
// // .then(this.log(device.getName(), tokens))
// .catch(this.error);
// };
this._speedTestResult = this.homey.flow.getDeviceTriggerCard('speed_test_result');
this.triggerSpeedTestResult = (device, tokens, state) => {
this._speedTestResult
.trigger(device, tokens, state)
// .then(this.log(device.getName(), tokens))
.catch(this.error);
};
this._newRouterFirmware = this.homey.flow.getDeviceTriggerCard('new_router_firmware');
this.triggerNewRouterFirmware = (device, tokens, state) => {
this._newRouterFirmware
.trigger(device, tokens, state)
// .then(this.log(device.getName(), tokens))
.catch(this.error);
};
// condition cards for attachedDevice
const deviceIsOnline = this.homey.flow.getConditionCard('device_is_online');
deviceIsOnline.registerRunListener((args) => args.device.getCapabilityValue('device_connected'));
// condition cards for Netgear driver
const internetConnected = this.homey.flow.getConditionCard('alarm_generic');
internetConnected.registerRunListener((args) => !args.device.getCapabilityValue('alarm_generic'));
const newFirmware = this.homey.flow.getConditionCard('new_firmware_condition');
newFirmware.registerRunListener((args) => (args.device.readings.newFirmware.newVersion
&& args.device.readings.newFirmware.newVersion !== ''));
const deviceIsOnlineAutocomplete = this.homey.flow.getConditionCard('device_is_online_autocomplete');
deviceIsOnlineAutocomplete
.registerRunListener((args) => {
if (Object.prototype.hasOwnProperty.call(args, 'device')) {
let isOnline = false;
if (Object.prototype.hasOwnProperty.call(args.device.knownDevices, args.mac.name)) {
isOnline = args.device.knownDevices[args.mac.name].online; // true or false
}
return Promise.resolve(isOnline);
}
return Promise.reject(Error('The netgear device is unknown or not ready'));
})
.registerArgumentAutocompleteListener('mac', autoComplete);
const deviceIsOnlineIpRange = this.homey.flow.getConditionCard('device_is_online_ip_range');
deviceIsOnlineIpRange.registerRunListener((args) => {
const OnlineInIpRange = (total, knownDevice) => {
if (!knownDevice.online) { return total; }
if (!knownDevice.IP) { return total; }
const hostOctet = Number(knownDevice.IP.split('.').pop());
if (hostOctet >= args.ip_from && hostOctet <= args.ip_to) {
return total + 1;
}
return total;
};
const devicesOnlineInIpRange = Object.values(args.device.knownDevices).reduce(OnlineInIpRange, 0);
return Promise.resolve(devicesOnlineInIpRange > 0);
});
// action cards for Netgear driver
const blockDevice = this.homey.flow.getActionCard('block_device');
blockDevice
.registerRunListener((args) => args.device.blockOrAllow(args.mac.name, 'Block').catch(this.error))
.registerArgumentAutocompleteListener('mac', autoComplete);
const blockDeviceText = this.homey.flow.getActionCard('block_device_text');
blockDeviceText
.registerRunListener((args) => args.device.blockOrAllow(args.mac.replace(' ', ''), 'Block').catch(this.error));
const allowDevice = this.homey.flow.getActionCard('allow_device');
allowDevice
.registerRunListener((args) => args.device.blockOrAllow(args.mac.name, 'Allow').catch(this.error))
.registerArgumentAutocompleteListener('mac', autoComplete);
const allowDeviceText = this.homey.flow.getActionCard('allow_device_text');
allowDeviceText
.registerRunListener((args) => args.device.blockOrAllow(args.mac.replace(' ', ''), 'Allow').catch(this.error));
const wol = this.homey.flow.getActionCard('wol');
wol
.registerRunListener((args) => args.device.wol(args.mac.name, args.password).catch(this.error))
.registerArgumentAutocompleteListener('mac', autoComplete);
const setGuestWifi = this.homey.flow.getActionCard('set_guest_wifi');
setGuestWifi
.registerRunListener((args) => {
if (args.network === '5') {
args.device.set5GGuestWifi(args.on_off).catch(this.error);
} else if (args.network === '5-2') {
args.device.set5GGuestWifi2(args.on_off).catch(this.error);
} else if (args.network === '2.4') {
args.device.setGuestwifi(args.on_off).catch(this.error);
} else {
args.device.setGuestwifi2(args.on_off).catch(this.error);
}
});
const speedTestStart = this.homey.flow.getActionCard('speed_test_start');
speedTestStart.registerRunListener(async (args) => {
const speed = await args.device.speedTest().catch(this.error);
const tokens = {
uplink_bandwidth: speed.uplinkBandwidth,
downlink_bandwidth: speed.downlinkBandwidth,
average_ping: speed.averagePing,
};
this.log(tokens);
this.homey.app.triggerSpeedTestResult(args.device, tokens, {}).catch(this.error);
});
const updateFirmware = this.homey.flow.getActionCard('update_firmware');
updateFirmware.registerRunListener(async (args) => args.device.updateNewFirmware().catch(this.error));
const reboot = this.homey.flow.getActionCard('reboot');
reboot.registerRunListener(async (args) => args.device.reboot().catch(this.error));
}
}
module.exports = MyApp;