-
Notifications
You must be signed in to change notification settings - Fork 1
/
device.js
116 lines (92 loc) · 2.52 KB
/
device.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
const { EventEmitter } = require('events');
const { State } = require('./state');
const camelCase = ([first, ...rest] = []) => [
first,
rest.map((s) => s[0].toUpperCase() + s.substring(1)),
].join('');
const status = {
OL: 'online',
OB: 'battery',
LB: 'low battery',
'OL CAL': 'calibrating',
};
const parse = (data, parsers) => {
const result = { ...data };
parsers.forEach(({ parse, fields }) => {
fields.forEach((field) => {
if (undefined === result[field]) return;
result[field] = parse(result[field]);
});
});
return result;
};
module.exports.Device = class Device extends EventEmitter {
topics() {
return ['info', 'battery', 'input', 'output', 'status'];
}
constructor(platform, name, description) {
super();
this.state = {};
this.topics().forEach((topic) => {
this.state[topic] = new State();
this.state[topic].on('change', (data) => this.emit('update', topic, data));
});
this.platform = platform;
this.name = name;
this.description = description;
setInterval(() => this.fetch(), this.platform.config.interval);
}
async fetch() {
const data = await this.platform.call('GetUPSVars', this.name);
this.update(this.parse(data));
}
parse(raw) {
const data = parse(raw, [
{
parse: (a) => parseInt(a, 10),
fields: [
'battery.charge',
'ups.temperature',
'ups.load',
],
},
{
parse: (a) => parseFloat(a),
fields: [
'battery.voltage',
'battery.voltage.high',
'battery.voltage.low',
'battery.voltage.nominal',
'input.current.nominal',
'input.frequency',
'input.frequency.nominal',
'input.voltage',
'input.voltage.fault',
'input.voltage.nominal',
'output.voltage',
],
},
]);
return {
...data,
'status.mode': status[data['ups.status']],
'status.beeper': data['ups.beeper.status'] === 'enabled',
'status.temperature': data['ups.temperature'],
'status.load': data['ups.load'],
'info.type': data['ups.type'] || undefined,
'info.manufacturer': data['ups.mfr'] || data['device.mfr'] || undefined,
'info.model': data['ups.model'] || data['device.model'] || undefined,
'info.firmware': data['ups.firmware'] || undefined,
};
}
update(data) {
const update = { info: {} };
Object.keys(data).forEach((key) => {
const [topic, ...parts] = key.split('.');
if (!this.state[topic]) return;
const name = camelCase(parts);
update[topic] = { ...update[topic], [name]: data[key] };
});
Object.keys(update).forEach((key) => this.state[key].set(update[key]));
}
};