-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
234 lines (190 loc) · 5.87 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
// Simple zwavejs2mqtt plugin for prometheus metrics
const promCli = require('prom-client')
const PromCliRegistry = promCli.Registry
const register = promCli.register;
const labelNames = [
'nodeId',
'location',
'name',
'commandClass',
'property',
'propertyKey',
'label',
'endpoint',
'id',
'state'
]
function shallowEquals(a, b) {
for (let key in a) {
if (!(key in b) || a[key] !== b[key]) {
return false;
}
}
for (let key in b) {
if (!(key in a)) {
return false;
}
}
return true;
}
function isDefined(a) {
return a !== undefined && a !== null
}
function getOrDefault(dict, id, func) {
let v = dict[id]
if (!isDefined(v)) {
v = func()
dict[id] = v
}
return v
}
function zwaveLabel(label) {
return label.toString()
.toLowerCase()
.replaceAll(' ', '_')
.replaceAll('₂', '2') // special case for co2
.replaceAll(/[^a-zA-Z0-9_]/ig, '') // Remove all non-allowed letters (see https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels)
}
class ZwavejsProm {
constructor(ctx) {
this.zwave = ctx.zwave
this.mqtt = ctx.mqtt
this.logger = ctx.logger
this.app = ctx.app
this.logger.info('Starting ZwaveJS prom plugin')
this.registry = new PromCliRegistry()
this.gauges = {}
this.nodes = {}
this.zwave.on('valueChanged', this.onValueChanged.bind(this))
this.zwave.on('nodeRemoved', this.onNodeRemoved.bind(this))
this.zwave.on('nodeStatus', this.onNodeStatus.bind(this))
this.app.get("/metrics", this.sendMetrics.bind(this))
}
async destroy() {
this.logger.info('Stopping ZwaveJS prom plugin')
}
async sendMetrics(req, res) {
res.set('Content-Type', register.contentType);
this.registry.metrics().then((m) => res.send(m))
}
onNodeRemoved(node) {
//this.logger.info(`Node removed: ${JSON.stringify(node)}`)
let id = node.id.toString();
let n = this.nodes[id]
if (isDefined(n)) {
for (const v in n.values) {
v.gauge.remove(v.labels)
}
delete this.nodes[id]
}
}
onNodeStatus(node) {
//this.logger.info(`Node status updated: ${JSON.stringify(node)}`)
this.updateNode(node)
}
updateNode(node) {
let n = getOrDefault(this.nodes, node.id.toString(), () => ({
values: {},
name: node.name,
location: node.loc
}))
if (n.name !== node.name || n.loc !== node.loc) {
this.updateNameAndLocation(n, node.name, node.loc)
}
return n
}
onValueChanged(value) {
//this.logger.info(`Value changed: ${JSON.stringify(value)}`)
// skip command classes making no sense to monitor
switch (value.commandClass) {
case 0x70: // Configuration
case 0x72: // Manufacturer specific
case 0x86: // Version
case 0x60: // Multi Channel
return
}
// skip non-readable values
if (!value.readable) {
return
}
let states = {}
if (value.list) {
for (const s of value.states) {
states[s.value] = s.text
}
}
let v = value.value
if (v === undefined && states[0] === 'idle') {
v = 0
}
let metricValue = 0
switch (typeof v) {
case 'number':
metricValue = v
break
case 'boolean':
if (v) {
metricValue = 1
}
break
default:
return
}
let z2mNode = this.zwave.nodes.get(value.nodeId)
let gaugeName = `zwave_${zwaveLabel(value.commandClassName)}_${zwaveLabel(value.property)}`
let gaugeHelp = `Zwave, ${value.commandClassName}, ${value.propertyName}`
let labels = {
nodeId: value.nodeId,
name: z2mNode.name,
location: z2mNode.loc,
commandClass: value.commandClassName,
property: value.property,
label: value.label,
endpoint: value.endpoint,
id: value.id
}
if (isDefined(value.propertyKey)) {
labels.propertyKey = value.propertyKey
gaugeName = `${gaugeName}_${zwaveLabel(value.propertyKey)}`
gaugeHelp = `${gaugeHelp}, ${value.propertyKeyName}`
}
const state = states[v]
if (isDefined(state)) {
labels.state = state
}
let node = this.updateNode(z2mNode)
//this.logger.info(`value: ${JSON.stringify(value)}`)
//this.logger.info(`gaugeName: ${gaugeName}, gaugeHelp: ${gaugeHelp}`)
let gauge = getOrDefault(this.gauges, gaugeName, () =>
new promCli.Gauge({
registers: [this.registry],
name: gaugeName,
help: gaugeHelp,
labelNames: labelNames
}))
let nodeValue = getOrDefault(node.values, value.id, () => ({
labels: labels,
value: metricValue,
gauge: gauge
}))
if (!shallowEquals(labels, nodeValue.labels)) {
gauge.remove(labels)
}
gauge.set(labels, metricValue)
nodeValue.labels = labels
nodeValue.value = metricValue
}
updateNameAndLocation(node, name, loc) {
for (const v of Object.values(node.values)) {
v.gauge.remove(v.labels)
v.labels.name = name
v.labels.location = loc
v.gauge.set(v.labels, v.value)
}
node.name = name
node.location = loc
}
}
module.exports = function (ctx) {
return new ZwavejsProm(ctx)
}