forked from homespun/homebridge-accessory-apcupsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
648 lines (543 loc) · 23 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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/* jshint asi: true, esversion: 6, node: true, laxbreak: true, laxcomma: true, undef: true, unused: true */
const APCaccess = require('apcaccess')
, NodeCache = require('node-cache')
, debug = require('debug')('apcupsd')
, inherits = require('util').inherits
, moment = require('moment')
, underscore = require('underscore')
module.exports = function (homebridge) {
const Characteristic = homebridge.hap.Characteristic
, Service = homebridge.hap.Service
, CommunityTypes = require('hap-nodejs-community-types')(homebridge)
homebridge.registerAccessory("homebridge-accessory-apcupsd", "apcupsd", APC)
const cache = new NodeCache({ stdTTL: 1 })
function APC(log, config) {
if (!(this instanceof APC)) return new APC(log, config)
this.log = log
this.config = config || { accessory: 'apcupsd' }
this.name = this.config.name
if (!this.config.subtype) this.config.subtype = 'battery'
if ([ 'battery', 'power' ].indexOf(this.config.subtype) === -1) throw new Error('invalid subtype: ' + this.config.subtype)
this.options = underscore.defaults(this.config.options || {}, { ttl: 600, verboseP: false })
if (this.options.ttl < 10) this.options.ttl = 600
debug('options', this.options)
this.location = require('url').parse('http://' + this.config.location + '/')
if (!this.location.port) {
this.location.port = 3551
this.location.host += ':' + this.location.port
}
this.whoami = { location: this.location.host, subtype: this.config.subtype }
const now = moment().unix()
this.historyExtra = { lastReset: now - moment('2001-01-01T00:00:00Z').unix(), lastChange: now }
if (this.config.subtype === 'power') {
underscore.extend(this.historyExtra, { wattSeconds: 0, totalSamples: 1, lastSample: now })
} else {
underscore.extend(this.historyExtra,
{ timesOpened : 0
, openDuration : 0
, closedDuration : 0
, lastActivation : 0
, lastStatus : -1
})
}
this.statusFault = Characteristic.StatusFault.NO_FAULT
this.callbacks = []
}
/* getStatusJson().then(status)
{ "UPSNAME": "alarmpi" // UPS name from EEPROM or configuration
, "MODEL": "Back-UPS ES 700G" // UPS model derived from UPS information
, "LINEV": "232.0 Volts" // Current input line voltage
, "LOADPCT": "5.0 Percent" // Percentage of UPS load capacity used
, "BCHARGE": "100.0 Percent" // Current battery capacity charge percentage
, "OUTPUTV": "219.7 Volts" // Current UPS output voltage
, "OUTCURNT": "239.50 Amps" // Current UPS output amperage
, "ITEMP": "31.5 C" //
, "BATTV": "13.5 Volts" // Current battery voltage
, "STATFLAG": "0x05000008" // UPS status flag in hex
// 0x08: on line
// 0x10: on battery
// 0x20: overloaded output
// 0x40: battery low
// 0x80: replace battery
, "SERIALNO": "5B1325T16968" // UPS serial number
, "FIRMWARE": "871.O2 .I USB FW:O2" // UPS firmware version
, "NOMPOWER": "1500 Watts" // Nominal power
, ...
}
*/
APC.prototype =
{ fetchStatus:
function (callback) {
const self = this
if (!callback) callback = () => {}
cache.get(self.location.host + '_status', (err, status) => {
if (err || status) return callback(err, status)
self._fetchStatus((err, result) => {
const i = (value) => {
const integer = value && parseInt(value)
if (!isNaN(integer)) return integer
}
const p = (value) => {
const percentage = value && parseFloat(value.split(' ')[0])
if ((!isNaN(percentage)) && (0 <= percentage) && (percentage <= 100)) return percentage
}
const r = (value) => {
const real = value && parseFloat(value.split(' ')[0])
if (!isNaN(real)) return real
}
const s = (value) => {
return value.split('/').join(' ')
}
const t = (value) => {
return s(value.split('.')[0])
}
const z =
{ BATTV : r
, BCHARGE : p
, FIRMWARE : t
, ITEMP : r
, LINEV : r
, LOADPCT : p
, MODEL : s
, NOMPOWER : r
, OUTPUTV : r
, OUTCURNT : r
, SERIALNO : s
, STATFLAG : i
, UPSNAME : s
}
self.statusFault = (err || (!result)) ? Characteristic.StatusFault.GENERAL_FAULT : Characteristic.StatusFault.NO_FAULT
err = null
const status = {}
for (let key in z) {
let value = result && result[key] && result[key].trim()
if ((!z.hasOwnProperty(key)) || (value === undefined)) continue
value = z[key](value)
if (value !== undefined) status[key] = value
}
// debug('fetchStatus', underscore.extend({}, self.whoami, { status }))
if (!self.initP) {
// debug('fetchStatus', underscore.extend({}, self.whoami, { status: 'initialized' }))
self.initP = true
if (self.config.subtype === 'power') {
if (status.NOMPOWER) {
self.myPowerService
.getCharacteristic(CommunityTypes.Watts).on('get', self.getWatts.bind(self))
self.myPowerService
.getCharacteristic(CommunityTypes.KilowattHours).on('get', self.getKilowattHours.bind(self))
}
if (status.OUTPUTV) {
self.myPowerService
.getCharacteristic(CommunityTypes.Volts).on('get', self.getOutputVoltageAC.bind(self))
}
if (status.OUTCURNT) {
self.myPowerService
.getCharacteristic(CommunityTypes.VoltAmperes).on('get', self.getOutputVoltAmperes.bind(self))
}
if (status.ITEMP) {
self.myPowerService
.getCharacteristic(Characteristic.CurrentTemperature).on('get', self.getCurrentTemperature.bind(self))
}
}
}
const now = moment().unix()
const delta = now - self.historyExtra.lastChange
if ((self.config.subtype === 'power') && (status.NOMPOWER !== undefined) && (status.LOADPCT !== undefined)) {
const gap = now - self.historyExtra.lastSample
const power = Math.round((status.NOMPOWER * status.LOADPCT) / 100.0)
if (delta >= this.options.ttl) {
self.history.addEntry({ time: now, power })
self.historyExtra.lastChange = now
}
if (gap > 0) {
self.historyExtra.wattSeconds += power * gap
self.historyExtra.totalSamples += gap
self.historyExtra.lastSample = now
/*
self.historyExtra.average = ((power * gap) / self.historyExtra.totalSamples)
self.historyExtra.power = power
self.historyExtra.gap = gap
*/
// debug('fetchStatus', underscore.extend({}, self.whoami, { historyExtra: self.historyExtra }))
}
}
if ((self.config.subtype === 'battery') && (status.STATFLAG !== undefined)) {
const contact = Characteristic.ContactSensorState[(status.STATFLAG & 0x08) ? 'CONTACT_DETECTED'
: 'CONTACT_NOT_DETECTED']
self.history.addEntry({ time: now, status: contact })
if (self.historyExtra.lastStatus !== contact) {
self.historyExtra.lastStatus = contact
if (contact == Characteristic.ContactSensorState.CONTACT_NOT_DETECTED) {
self.historyExtra.timesOpened++
self.historyExtra.closedDuration += delta
self.historyExtra.lastActivation = now - self.history.getInitialTime()
} else {
self.historyExtra.openDuration += delta
}
self.historyExtra.lastChange = now
self.history.setExtraPersistedData(self.historyExtra)
// debug('fetchStatus', underscore.extend({}, self.whoami, { historyExtra: self.historyExtra }))
}
}
debug('set ' + self.location.host + '_status cache: ' + JSON.stringify(status, null, 2))
cache.set(self.location.host + '_status', status)
callback(err, status)
})
})
}
, _fetchStatus:
function (callback) {
const self = this
const flush = (err, result) => {
const callbacks = self.callbacks
self.callbacks = []
self.client = null
for (let cb of callbacks) {
try {
cb(null, result)
} catch (ex) {
self.log.error('callback error: ' + ex.stack)
}
}
}
if (!self.client) {
self.client = new APCaccess()
self.client.connect(self.location.hostname, self.location.port).then(() => {
// debug('fetchStatus', underscore.extend({}, self.whoami, { status: 'connected' }))
return self.client.getStatusJson()
}).then((result) => {
const client = self.client
flush(null, result)
return client.disconnect()
}).then(() => {
// debug('fetchStatus', underscore.extend({}, self.whoami, { status: 'disconnected' }))
}).catch((err) => {
self.log.error('getStatusJSON error: ' + err.toString())
flush(err)
})
}
self.callbacks.push(callback)
}
, loadExtra:
function () {
let extra
if (!this.history.isHistoryLoaded()) return setTimeout(this.loadExtra.bind(this), 100)
extra = this.history.getExtraPersistedData()
if (extra) this.historyExtra = extra
else this.history.setExtraPersistedData(this.historyExtra)
// debug('fetchStatus', underscore.extend({}, this.whoami, { historyExtra: this.historyExtra }))
}
, getName:
function (callback) {
const self = this
self.fetchStatus(function (err, status) {
callback(err, (status && status.UPSNAME) || self.name)
})
}
, getModel:
function (callback) {
const self = this
self.fetchStatus(function (err, status) {
callback(err, (status && status.MODEL) || self.config.model)
})
}
, getSerialNumber:
function (callback) {
const self = this
self.fetchStatus(function (err, status) {
callback(err, (status && status.SERIALNO) || self.config.serialNo)
})
}
, getFirmwareRevision:
function (callback) {
const self = this
self.fetchStatus(function (err, status) {
callback(err, ((status && status.FIRMWARE) || self.config.firmware))
})
}
, getInputVoltageAC:
function (callback) {
this.fetchStatus(function (err, status) {
callback(err, status && status.LINEV)
})
}
, getBatteryVoltageDC:
function (callback) {
this.fetchStatus(function (err, status) {
callback(err, status && status.BATTV)
})
}
, getUPSLoadPercent:
function (callback) {
this.fetchStatus(function (err, status) {
callback(err, status && status.LOADPCT)
})
}
, getVolts:
function (callback) {
this.fetchStatus(function (err, status) {
callback(err, status && (status.OUTPUTV || status.LINEV))
})
}
, getVoltAmperes:
function (callback) {
this.fetchStatus(function (err, status) {
// TBD:
callback(err, 0)
})
}
, getWatts:
function (callback) {
this.fetchStatus(function (err, status) {
const power = status && status.NOMPOWER
const percentage = status && status.LOADPCT
if ((err) || (power === undefined) || (percentage === undefined)) return callback(err)
callback(err, (power * percentage) / 100.0)
})
}
, getKilowattHours:
function (callback) {
callback(null, (this.historyExtra.wattSeconds * 3600) / (this.historyExtra.totalSamples * 1000))
}
, getOutputVoltageAC:
function (callback) {
this.fetchStatus(function (err, status) {
callback(err, status && status.OUTPUTV)
})
}
, getOutputVoltAmperes:
function (callback) {
this.fetchStatus(function (err, status) {
callback(err, status && status.OUTCURNT)
})
}
, getCurrentTemperature:
function (callback) {
this.fetchStatus(function (err, status) {
callback(err, status && status.ITEMP)
})
}
, getContactSensorState:
function (callback) {
this.fetchStatus(function (err, status) {
let flags = status && status.STATFLAG
if ((err) || (flags === undefined)) flags = 0x00
callback(err, Characteristic.ContactSensorState[(flags & 0x08) ? 'CONTACT_DETECTED' : 'CONTACT_NOT_DETECTED'])
})
}
, getStatusActive:
function (callback) {
this.fetchStatus(function (err, status) {
let percentage = status && status.LOADPCT
if ((err) || (percentage === undefined)) percentage = 0
callback(err, Characteristic.Active[percentage ? 'ACTIVE' : 'INACTIVE'])
})
}
, getStatusFault:
function (callback) {
callback(null, this.statusFault)
}
, getEveTimesOpened:
function (callback) {
callback(null, this.historyExtra.timesOpened)
}
, getEveOpenDuration:
function (callback) {
callback(null, this.historyExtra.openDuration)
}
, getEveClosedDuration:
function (callback) {
callback(null, this.historyExtra.closedDuration)
}
, getEveLastActivation:
function (callback) {
callback(null, this.historyExtra.lastActivation)
}
, getEveResetTotal:
function (callback) {
this.history.getCharacteristic(CommunityTypes.EveResetTotal).updateValue(this.historyExtra.lastReset)
callback(null, this.historyExtra.lastReset)
}
, setEveResetTotal:
function (value, callback) {
if (this.config.subtype === 'battery') this.historyExtra.timesOpened = 0
this.historyExtra.lastReset = value
this.history.setExtraPersistedData(this.historyExtra)
this.history.getCharacteristic(CommunityTypes.EveResetTotal).updateValue(this.historyExtra.lastReset)
callback(null)
}
, getBatteryFail:
function (callback) {
this.fetchStatus(function (err, status) {
let flags = status && status.STATFLAG
if ((err) || (flags === undefined)) flags = 0x80
callback(err, Characteristic.SecuritySystemCurrentState[(flags & 0x80) ? 'ALARM_TRIGGERED' : 'STAY_ARM'])
})
}
, getBatteryLevel:
function (callback) {
this.fetchStatus(function (err, status) {
let percentage = status && status.BCHARGE
if ((err) || (percentage === undefined)) percentage = 100
callback(err, percentage)
})
}
, getChargingState:
function (callback) {
this.fetchStatus(function (err, status) {
const percentage = status && status.BCHARGE
let flags = status && status.STATFLAG
if ((err) || (flags === undefined) || (percentage === undefined)) flags = 0x80
callback(err, Characteristic.ChargingState[(flags & 0x80) ? 'NOT_CHARGEABLE'
: ((flags & 0x10) || (percentage === 100)) ? 'NOT_CHARGING'
: 'CHARGING'])
})
}
, getStatusLowBattery:
function (callback) {
this.fetchStatus(function (err, status) {
let flags = status && status.STATFLAG
if ((err) || (flags === undefined)) flags = 0x00
callback(err, Characteristic.StatusLowBattery[(flags & 0x40) ? 'BATTERY_LEVEL_LOW' : 'BATTERY_LEVEL_NORMAL'])
})
}
, getServices: function () {
const FakeGatoHistoryService = require('fakegato-history')(homebridge)
, myAccessoryInformation = new Service.AccessoryInformation()
const services = [ myAccessoryInformation ]
let interval, myPowerService, myContactService, myBatteryService, myAlarmService
myAccessoryInformation
.setCharacteristic(Characteristic.Name, this.name)
.setCharacteristic(Characteristic.Manufacturer, "American Power Conversion (APC)")
myAccessoryInformation
.getCharacteristic(Characteristic.Name)
.on('get', this.getName.bind(this))
if (this.config.model) myAccessoryInformation.setCharacteristic(Characteristic.Model, this.config.model)
myAccessoryInformation
.getCharacteristic(Characteristic.Model)
.on('get', this.getModel.bind(this))
if (this.config.serialNo) myAccessoryInformation.setCharacteristic(Characteristic.SerialNumber, this.config.serialNo)
myAccessoryInformation
.getCharacteristic(Characteristic.SerialNumber)
.on('get', this.getSerialNumber.bind(this))
if (this.config.firmware) myAccessoryInformation.setCharacteristic(Characteristic.FirmwareRevision, this.config.firmware)
myAccessoryInformation
.getCharacteristic(Characteristic.FirmwareRevision)
.on('get', this.getFirmwareRevision.bind(this))
if (this.config.subtype === 'power') {
const PowerService = function (displayName, subtype) {
Service.call(this, displayName, '00000001-0000-1000-8000-135D67EC4377', subtype)
this.addCharacteristic(CommunityTypes.InputVoltageAC)
this.addCharacteristic(CommunityTypes.BatteryVoltageDC)
this.addCharacteristic(CommunityTypes.UPSLoadPercent)
this.addCharacteristic(CommunityTypes.Volts)
this.addCharacteristic(CommunityTypes.VoltAmperes)
this.addOptionalCharacteristic(CommunityTypes.Watts)
this.addOptionalCharacteristic(CommunityTypes.KilowattHours)
this.addOptionalCharacteristic(CommunityTypes.OutputVoltageAC)
this.addOptionalCharacteristic(CommunityTypes.OutputVoltAmperes)
this.addOptionalCharacteristic(Characteristic.CurrentTemperature)
this.addCharacteristic(CommunityTypes.EveResetTotal)
}
inherits(PowerService, Service)
myPowerService = new PowerService()
this.myPowerService = myPowerService
myPowerService
.getCharacteristic(CommunityTypes.InputVoltageAC)
.on('get', this.getInputVoltageAC.bind(this))
myPowerService
.getCharacteristic(CommunityTypes.BatteryVoltageDC)
.on('get', this.getBatteryVoltageDC.bind(this))
myPowerService
.getCharacteristic(CommunityTypes.UPSLoadPercent)
.on('get', this.getUPSLoadPercent.bind(this))
myPowerService
.getCharacteristic(CommunityTypes.Volts)
.on('get', this.getVolts.bind(this))
myPowerService
.getCharacteristic(CommunityTypes.VoltAmperes)
.on('get', this.getVoltAmperes.bind(this))
myPowerService
.getCharacteristic(CommunityTypes.EveResetTotal)
.on('get', this.getEveResetTotal.bind(this))
.on('set', this.setEveResetTotal.bind(this))
services.push(myPowerService)
this.displayName = this.name + ' Power'
this.history = new FakeGatoHistoryService('energy', this, {
storage : 'fs',
disableTimer : true,
length : Math.pow(2, 14),
path : homebridge.user.cachedAccessoryPath(),
filename : this.location.host + '-apcupsd-power_persist.json'
})
interval = 1
} else {
myContactService = new Service.ContactSensor()
myContactService.addOptionalCharacteristic(CommunityTypes.EveTimesOpened)
myContactService.addOptionalCharacteristic(CommunityTypes.EveOpenDuration)
myContactService.addOptionalCharacteristic(CommunityTypes.EveClosedDuration)
myContactService.addOptionalCharacteristic(CommunityTypes.EveLastActivation)
myContactService.addOptionalCharacteristic(CommunityTypes.EveResetTotal)
myContactService
.getCharacteristic(Characteristic.ContactSensorState)
.on('get', this.getContactSensorState.bind(this))
myContactService
.getCharacteristic(Characteristic.StatusActive)
.on('get', this.getStatusActive.bind(this))
myContactService
.getCharacteristic(Characteristic.StatusFault)
.on('get', this.getStatusFault.bind(this))
myContactService
.getCharacteristic(CommunityTypes.EveTimesOpened)
.on('get', this.getEveTimesOpened.bind(this))
myContactService
.getCharacteristic(CommunityTypes.EveOpenDuration)
.on('get', this.getEveOpenDuration.bind(this))
myContactService
.getCharacteristic(CommunityTypes.EveClosedDuration)
.on('get', this.getEveClosedDuration.bind(this))
myContactService
.getCharacteristic(CommunityTypes.EveLastActivation)
.on('get', this.getEveLastActivation.bind(this))
myContactService
.getCharacteristic(CommunityTypes.EveResetTotal)
.on('get', this.getEveResetTotal.bind(this))
.on('set', this.setEveResetTotal.bind(this))
services.push(myContactService)
myAlarmService = new Service.SecuritySystem()
myAlarmService
.setCharacteristic(Characteristic.Name, this.name + ' Fail')
myAlarmService
.getCharacteristic(Characteristic.SecuritySystemCurrentState)
.on('get', this.getBatteryFail.bind(this))
services.push(myAlarmService)
myBatteryService = new Service.BatteryService()
myBatteryService
.getCharacteristic(Characteristic.BatteryLevel)
.on('get', this.getBatteryLevel.bind(this))
myBatteryService
.getCharacteristic(Characteristic.ChargingState)
.on('get', this.getChargingState.bind(this))
myBatteryService
.getCharacteristic(Characteristic.StatusLowBattery)
.on('get', this.getStatusLowBattery.bind(this))
services.push(myBatteryService)
this.displayName = this.name + ' Battery'
this.history = new FakeGatoHistoryService('door', this, {
storage : 'fs',
disableTimer : true,
length : Math.pow(2, 14),
path : homebridge.user.cachedAccessoryPath(),
filename : this.location.host + '-apcupsd-battery_persist.json'
})
interval = this.options.ttl
this.loadExtra() // not used for 'power' subtype
}
this.history.subtype = this.config.subtype
services.push(this.history)
setTimeout(this.fetchStatus.bind(this), 1 * 1000)
setInterval(this.fetchStatus.bind(this), interval * 1000)
return services
}
}
}