-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
361 lines (311 loc) · 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
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
let snmp = require("net-snmp");
module.exports = (api) => {
api.registerAccessory('ups', UPS);
};
class UPS {
services = []
getServices() {
return this.services;
}
configureAccessory(accessory) {
console.log(accessory)
}
constructor(log, config, api) {
this.cachedInformations = {};
this.cachedBatteryLevel = 0;
this.cachedBatteryLevel = false;
this.cachedLowBattery = 0;
this.cachedTemperature = 0;
this.updateInterval = 20000;
this.outletGroupsSupported = false;
this.envTemp1Supported = false;
this.envTemp2Supported = false;
this.envHum1Supported = false;
this.envHum2Supported = false;
this.log = log;
this.config = config;
this.api = api;
this.session = snmp.createSession(this.config.address, this.config.community);
this.oids = {
"env_temp_1": ".1.3.6.1.4.1.318.1.1.2.1.1.0",
"env_temp_2": ".1.3.6.1.4.1.318.1.1.2.1.3.0",
"env_hum_1": ".1.3.6.1.4.1.318.1.1.2.1.2.0",
"env_hum_2": ".1.3.6.1.4.1.318.1.1.2.1.4.0",
"date_of_manufacture": "1.3.6.1.4.1.318.1.1.1.1.2.2.0",
"name": "1.3.6.1.4.1.318.1.1.1.1.1.2.0",
"model": "1.3.6.1.4.1.318.1.1.1.1.1.1.0",
"serial_number": "1.3.6.1.4.1.318.1.1.1.1.2.3.0",
"firmware_rev": "1.3.6.1.4.1.318.1.1.1.1.2.1.0",
"out_volt": "1.3.6.1.4.1.318.1.1.1.4.2.1.0",
"bat_capacity": "1.3.6.1.4.1.318.1.1.1.2.2.1.0",
"bat_status": "1.3.6.1.4.1.318.1.1.1.2.1.1.0",
"time_on_bat": "1.3.6.1.4.1.318.1.1.1.2.1.2.0",
"alarm_state": "1.3.6.1.4.1.318.1.1.1.5.2.4.0",
"self_test_state": "1.3.6.1.4.1.318.1.1.1.7.2.3.0",
"temp": "1.3.6.1.4.1.318.1.1.1.2.2.2.0",
"turn_on": {"oid": "1.3.6.1.4.1.318.1.1.1.6.2.6.0", "type": snmp.ObjectType.INTEGER, "value": 2},
"turn_off": {"oid": "1.3.6.1.4.1.318.1.1.1.6.2.1.0", "type": snmp.ObjectType.INTEGER, "value": 2},
"turn_off_graceful": {"oid": "1.3.6.1.4.1.318.1.1.1.6.2.1.0", "type": snmp.ObjectType.INTEGER, "value": 3},
"alarm_on": {"oid": "1.3.6.1.4.1.318.1.1.1.5.2.4.0", "type": snmp.ObjectType.INTEGER, "value": 1},
"alarm_off": {"oid": "1.3.6.1.4.1.318.1.1.1.5.2.4.0", "type": snmp.ObjectType.INTEGER, "value": 3},
"start_self_test": {"oid": "1.3.6.1.4.1.318.1.1.1.7.2.2.0", "type": snmp.ObjectType.INTEGER, "value": 2}
};
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
this.name = this.config.name;
this.informationService = new this.Service.AccessoryInformation()
this.informationService.setCharacteristic(this.Characteristic.Manufacturer, "APC");
this.informationService.getCharacteristic(this.Characteristic.Name).onGet(
this.getCachedAccessoryInformation.bind(this, this.oids.name, "Name")
);
this.informationService.getCharacteristic(this.Characteristic.Model).onGet(
this.getCachedAccessoryInformation.bind(this, this.oids.model, "Model")
);
this.informationService.getCharacteristic(this.Characteristic.SerialNumber).onGet(
this.getCachedAccessoryInformation.bind(this, this.oids.serial_number, "Serial Number")
);
this.informationService.getCharacteristic(this.Characteristic.FirmwareRevision).onGet(
this.getCachedAccessoryInformation.bind(this, this.oids.firmware_rev, "Firmware Rev.")
);
this.batteryService = new this.Service.BatteryService(this.name);
this.batteryService.getCharacteristic(this.Characteristic.StatusLowBattery).onGet(
this.getLowBatteryHandler.bind(this)
);
this.batteryService.getCharacteristic(this.Characteristic.BatteryLevel).onGet(
this.getBatteryLevelHandler.bind(this)
);
this.batteryService.getCharacteristic(this.Characteristic.ChargingState).onGet(
this.getBatteryChargingStateHandler.bind(this)
);
this.switchService = new this.Service.Switch(this.name);
this.switchService.getCharacteristic(this.Characteristic.On).onGet(
this.getPowerStateHandler.bind(this)
).onSet(
this.setPowerStateHandler.bind(this)
);
this.gracefulSwitchService = new this.Service.Switch(this.name + " (Graceful)", "Graceful");
this.gracefulSwitchService.getCharacteristic(this.Characteristic.On).onGet(
this.getPowerStateHandler.bind(this)
).onSet(
this.setGracefulPowerStateHandler.bind(this)
);
this.alarmSwitchService = new this.Service.Switch(this.name + " Alarm", "Alarm");
this.alarmSwitchService.getCharacteristic(this.Characteristic.On).onGet(
this.getAlarmStateHandler.bind(this)
).onSet(
this.setAlarmStateHandler.bind(this)
);
this.selfTestSwitchService = new this.Service.Switch(this.name + " Self-Test", "Self-Test");
this.selfTestSwitchService.getCharacteristic(this.Characteristic.On).onGet(
this.getSelfTestHandler.bind(this)
).onSet(
this.setSelfTestHandler.bind(this)
);
this.tempService = new this.Service.TemperatureSensor(this.name + " Battery");
this.tempService.getCharacteristic(this.Characteristic.CurrentTemperature).onGet(
this.getTempHandler.bind(this)
);
this.services.push(this.informationService);
if (this.config.enable_non_graceful || this.config.enable_non_graceful === undefined) {
this.services.push(this.switchService);
}
if (this.config.enable_graceful || this.config.enable_graceful === undefined) {
this.services.push(this.gracefulSwitchService)
}
if (this.config.enable_alarm || this.config.enable_alarm === undefined) {
this.services.push(this.alarmSwitchService)
}
if (this.config.enable_self_test || this.config.enable_self_test === undefined) {
this.services.push(this.selfTestSwitchService)
}
if (this.config.enable_temp || this.config.enable_temp === undefined) {
this.services.push(this.tempService)
}
if (this.config.enable_battery || this.config.enable_battery === undefined) {
this.services.push(this.batteryService)
}
let configUpdateInterval = this.config.update_interval;
if (configUpdateInterval !== undefined) {
if (Number.isInteger(configUpdateInterval)) {
if (configUpdateInterval < 5) {
this.log.error("Minimum update interval value is 5 seconds!");
} else {
this.updateInterval = configUpdateInterval * 1000;
}
} else {
this.log.error("Update interval is not a number!");
}
}
this.getInitialAccessoryInformation();
this.updateLoop();
}
async getInitialAccessoryInformation() {
await this.getCachedAccessoryInformation(this.oids.name, "Name");
await this.getCachedAccessoryInformation(this.oids.model, "Model");
await this.getCachedAccessoryInformation(this.oids.serial_number, "Serial Number");
await this.getCachedAccessoryInformation(this.oids.firmware_rev, "Firmware Rev.");
await this.getCachedAccessoryInformation(this.oids.date_of_manufacture, "Date Of Manufacture");
await this.getSNMP(this.oids.env_temp_1) !== null ? this.envTemp1Supported = true : null;
await this.getSNMP(this.oids.env_temp_2) !== null ? this.envTemp2Supported = true : null;
await this.getSNMP(this.oids.env_hum_1) !== null ? this.envHum1Supported = true : null;
await this.getSNMP(this.oids.env_hum_2) !== null ? this.envHum2Supported = true : null;
}
// Helper
async updateLoop() {
setInterval(function () {
this.update()
}.bind(this), this.updateInterval)
};
async update() {
this.log.debug("Updating values...")
this.tempService.getCharacteristic(this.Characteristic.CurrentTemperature).updateValue(
await this.getTempHandler()
);
this.batteryService.getCharacteristic(this.Characteristic.StatusLowBattery).updateValue(
await this.getLowBatteryHandler()
);
this.batteryService.getCharacteristic(this.Characteristic.BatteryLevel).updateValue(
await this.getBatteryLevelHandler()
);
this.batteryService.getCharacteristic(this.Characteristic.ChargingState).updateValue(
await this.getBatteryChargingStateHandler()
);
}
setSNMP(oid, type, value) {
let logging = this.log;
this.session.set([{oid, type, value}], function (error, varbinds) {
if (error) {
logging.debug(error.toString());
return null;
}
for (let i = 0; i < varbinds.length; i++) {
if (snmp.isVarbindError(varbinds[i])) {
logging.debug(snmp.varbindError(varbinds[i]));
} else {
logging.debug("Set " + varbinds[i].oid + " to value " + varbinds[i].value);
}
}
});
}
async getSNMP(oid) {
let logging = this.log;
let session = this.session;
let timeoutPromise = new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('timeout'));
}, 1800);
});
let snmpPromise = new Promise(function (resolve, reject) {
try {
session.get([oid], function (error, varbinds) {
if (error) {
logging.debug(error);
reject(error.toString());
} else if (snmp.isVarbindError(varbinds[0])) {
logging.debug(snmp.varbindError(varbinds[0]));
reject(snmp.varbindError(varbinds[0]).toString());
} else {
logging.debug("Got " + oid + " value " + varbinds[0].value);
resolve(varbinds[0].value.toString());
}
})
} catch (RequestFailedError) {
logging.warn("Device not reachable or operation not supported")
}
});
return Promise.race([snmpPromise, timeoutPromise])
.then(result => {
return result;
})
.catch(error => {
return null;
});
}
// Self-Test
async getSelfTestHandler() {
this.log.debug('Triggered GET setSelfTestHandler');
return await this.getSNMP(this.oids.self_test_state) > 3;
}
async setSelfTestHandler(value) {
this.log.debug('Triggered SET setSelfTestHandler');
this.log.info('Starting UPS self-test...')
this.log.info('After the self-test is completed, the UPS will be unavailable for a few seconds.')
value ? this.setSNMP(
this.oids.start_self_test.oid,
this.oids.start_self_test.type,
this.oids.start_self_test.value
) : null;
}
// Power
async getPowerStateHandler() {
this.log.debug('Triggered GET getPowerStateHandler');
return await this.getSNMP(this.oids.out_volt) >= 10;
}
async setPowerStateHandler(value) {
this.log.debug('Triggered SET setPowerStateHandler');
let cmd = value ? this.oids.turn_on : this.oids.turn_off;
this.setSNMP(cmd.oid, cmd.type, cmd.value);
}
async setGracefulPowerStateHandler(value) {
this.log.debug('Triggered SET setGracefulPowerStateHandler');
let cmd = value ? this.oids.turn_on : this.oids.turn_off_graceful;
this.setSNMP(cmd.oid, cmd.type, cmd.value);
}
// Alarm
async getAlarmStateHandler() {
this.log.debug('Triggered GET getAlarmStateHandler');
return await this.getSNMP(this.oids.alarm_state) <= 2;
}
async setAlarmStateHandler(value) {
this.log.debug('Triggered SET setAlarmStateHandler');
let cmd = value ? this.oids.alarm_on : this.oids.alarm_off;
this.setSNMP(cmd.oid, cmd.type, cmd.value);
}
// Temp
async getTempHandler() {
this.log.debug('Triggered GET getTempHandler')
let temp = await this.getSNMP(this.oids.temp);
temp ? this.cachedTemperature = temp : null;
return temp ? temp : this.cachedTemperature;
}
// Battery
async getBatteryLevelHandler() {
this.log.debug('Triggered GET getBatteryLevelHandler');
let level = await this.getSNMP(this.oids.bat_capacity);
level ? this.cachedBatteryLevel = level : null;
return level ? level : this.cachedBatteryLevel;
}
async getLowBatteryHandler() {
this.log.debug('Triggered GET getLowBatteryHandler');
let status = await this.getSNMP(this.oids.bat_status);
if (status === null) {
return this.cachedLowBattery;
}
status ? this.cachedLowBattery = status === 3 : null;
return status === 3 ? 1 : 0;
}
async getBatteryChargingStateHandler() {
this.log.debug('Triggered GET getBatteryChargingStateHandler');
let charging = await this.getSNMP(this.oids.time_on_bat);
if (charging === null) {
return this.cachedBatteryLevel;
}
charging ? this.cachedBatteryLevel = charging : null;
return charging === "0" ? 1 : 0;
}
async getCachedAccessoryInformation(oid, log_text) {
if (!this.cachedInformations.hasOwnProperty(oid)) {
this.cachedInformations[oid] = null;
}
let val = await this.getSNMP(oid);
if (val) {
this.cachedInformations[oid] === null ? this.log.info(log_text + ": " + val) : null;
this.cachedInformations[oid] = val;
return val;
}
if (this.cachedInformations[oid] === null) {
return "Unknown";
}
return this.cachedInformations[oid];
}
}