-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathdbus-listener.js
330 lines (298 loc) · 10.1 KB
/
dbus-listener.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
/**
* Original version forked and modified from
* https://github.com/sbender9/signalk-venus-plugin/blob/master/dbus-listener.js
*/
const dbus = require('dbus-native-victron')
const debug = require('debug')('node-red-contrib-victron:dbus')
const _ = require('lodash')
/**
* VictronDbusListener encapsulates the dbus communications
* between Node-RED and Venus system. It establishes a dbus
* connection and exposes relevant callbacks to its owner.
*
* The class frequently polls relevant dbus rootPaths to
* keep track of available dbus services.
*
* Usage:
new VictronDbusListener(
tcpAddress,
{eventHandler, messageHandler}
).connect()
.then(callbacks => {
setValueFunc = callbacks.setValue
getValueFunc = callbacks.getValue
})
.catch(err => console.error)
*/
function searchHaystack (stack, needle, fallback) {
for (const key in stack) {
if (stack[key].deviceInstance === Number(needle) &&
stack[key].name.startsWith(fallback)) {
return stack[key].name
}
}
return fallback
}
function searchDeviceInstanceByName (stack, needle, fallback) {
for (const key in stack) {
if (stack[key].name === needle) {
return stack[key].deviceInstance
}
}
return fallback
}
class VictronDbusListener {
constructor (address, callbacks) {
this.address = address
this.pollInterval = 5
this.eventHandler = callbacks.eventHandler
this.messageHandler = callbacks.messageHandler
this.bus = null
this.rootPoller = null
this.services = {}
this.connected = false
// We need to bind the scope to getValue and setvalue,
// since they can be invoked elsewhere
this.getValue = this.getValue.bind(this)
this.setValue = this.setValue.bind(this)
this._signalRecieve = this._signalRecieve.bind(this)
}
connect () {
return new Promise((resolve, reject) => {
if (this.address) { // Connect via TCP
debug(`Connecting to TCP address ${this.address}.`)
this.bus = dbus.createClient({
busAddress: this.address,
authMethods: ['ANONYMOUS']
})
} else { // Connect via socket
debug('Connecting to system socket.')
this.bus = process.env.DBUS_SESSION_BUS_ADDRESS
? dbus.sessionBus()
: dbus.systemBus()
}
if (!this.bus) { throw new Error('Could not connect to the D-Bus') }
// Event callbacks
this.bus.connection.on('connect', () => {
debug('Connected to D-Bus.')
this.bus.listNames((props, args) => {
args.forEach(name => {
if (name.startsWith('com.victronenergy')) { this.bus.getNameOwner(name, (props, args) => this._initService(args, name)) }
})
})
this.rootPoller = setInterval(
() => _.values(this.services).forEach(service => this._requestRoot(service)),
this.pollInterval * 1000
)
// The following callbacks should be initialized
// only after dbus connection
this.bus.connection.on('message', this._signalRecieve)
this.bus.connection.on('end', () => {
clearInterval(this.rootPoller)
this.connected = false
console.error('Lost connection to D-Bus.')
reject(new Error('dbus connection end'))
})
this.connected = true
})
this.bus.connection.on('error', (err) => {
this.connected = false
console.error(`Error connecting to dbus: ${err}`)
reject(new Error('dbus connection error'))
})
this.bus.addMatch("type='signal',interface='com.victronenergy.BusItem',member='ItemsChanged'", () => { })
this.bus.addMatch("type='signal',interface='com.victronenergy.BusItem',member='PropertiesChanged'", () => { })
this.bus.addMatch("type='signal',member='NameOwnerChanged'", () => { })
})
}
_initService (owner, name) {
const service = { name }
this.bus.invoke({
path: '/DeviceInstance',
destination: name,
interface: 'com.victronenergy.BusItem',
member: 'GetValue'
},
(err, res) => {
this.services[owner] = service
if (res) {
this.services[owner].deviceInstance = res[1]?.[0]
if (!this.services[owner].deviceInstance) {
console.error(`deviceInstance could not be assigned because res[1][0] is undefined ${owner}/${this.services[owner]} (${this.services[owner].name})`)
// Handle the case where res[1][0] is undefined
}
}
if (err && err.length > 0) {
debug(`initService ${name} : ${err}`)
}
})
this._requestRoot(service)
}
_requestRoot (service) {
this.bus.invoke({
path: '/',
destination: service.name,
interface: 'com.victronenergy.BusItem',
member: 'GetItems'
},
(err, res) => {
if (!err) {
const data = {}
const getTargetValue = (arr) => arr[arr.findIndex(innerArr => innerArr[0] === 'Value')]?.[1]?.[1]?.[0]
res.forEach(([path, values]) => {
data[path] = getTargetValue(values)
})
if (!_.isUndefined(data.FluidType)) {
service.fluidType = data.FluidType
}
if (!service.deviceInstance && data['/DeviceInstance'] != null) {
service.deviceInstance = data['/DeviceInstance']
}
const deviceInstance = data['/DeviceInstance'] != null ? data['/DeviceInstance'] : service.deviceInstance
const messages = _.keys(data).map(path => {
return {
path: '/' + path.replace(/^\/+/, ''),
senderName: service.name.split('.').splice(0, 3).join('.'),
value: data[path],
deviceInstance,
fluidType: service.fluidType
}
})
this.messageHandler(messages)
}
})
}
_signalRecieve (msg) {
if (msg.interface !== 'com.victronenergy.BusItem') {
if (msg.interface === 'org.freedesktop.DBus' &&
msg.member === 'NameOwnerChanged') {
const name = msg.body[0]
if (name.startsWith('com.victronenergy')) {
const newOwner = msg.body[2]
if (newOwner) {
this._initService(newOwner, name)
this.eventHandler('INITIALIZE', name)
}
} else {
const oldOwner = msg.body[1]
if (oldOwner && this.services[oldOwner]) {
const trail = ('/' + (this.services[oldOwner].deviceInstance != null ? this.services[oldOwner].deviceInstance : '')).replace(/\.$/, '')
const svcName = this.services[oldOwner].name.split('.').splice(0, 3).join('.') + trail
this.eventHandler('DELETE', this.services[oldOwner].name)
delete this.services[oldOwner]
this.eventHandler('DELETE', svcName)
}
}
}
return
}
const messages = []
switch (msg.member) {
case 'ItemsChanged': {
msg.body[0].forEach(entry => {
const m = { changed: true }
m.path = entry[0]
if (entry[1] && entry[1].length === 2) {
entry[1].forEach(v => {
switch (v[0]) {
case 'Value': m.value = v[1][1][0]; break
case 'Text': m.text = v[1][1][0]; break
}
})
}
if (!m.path || m.value === null || !m.text) {
return
}
const service = this.services[msg.sender]
if (!service || !service.name) {
return
}
if (m.path === '/DeviceInstance') {
service.deviceInstance = m.value
}
m.senderName = service.name.split('.').splice(0, 3).join('.')
if (service.deviceInstance === null) {
service.deviceInstance = searchDeviceInstanceByName(this.services, m.senderName, '')
}
m.deviceInstance = service.deviceInstance
messages.push(m)
})
break
}
case 'PropertiesChanged': {
if (msg.body[0] && msg.body[0].length === 2) {
const m = msg
msg.body[0].forEach(v => {
switch (v[0]) {
case 'Value': m.value = v[1][1][0]; break
case 'Text': m.text = v[1][1][0]; break
}
})
if (!m.path || m.value === null || !m.text) {
return
}
const service = this.services[msg.sender]
if (!service || !service.name) {
return
}
m.senderName = service.name.split('.').splice(0, 3).join('.')
m.deviceInstance = service.deviceInstance
if (service.deviceInstance === null) {
service.deviceInstance = searchDeviceInstanceByName(this.services, m.senderName, '')
}
m.changed = true
messages.push(m)
}
break
}
default: {
debug(`Unexpected message: ${msg}`)
}
}
this.messageHandler(messages)
}
getValue (destination, path) {
this.bus.invoke({
path,
destination,
interface: 'com.victronenergy.BusItem',
member: 'GetValue'
},
(err, res) => {
if (!err) {
destination = destination.split('.').splice(0, 3).join('.')
if (path === '/DeviceInstance') {
destination += '/' + res[1][0]
}
this.messageHandler([{
path,
senderName: destination,
value: res[1][0]
}])
}
})
}
setValue (destination, path, value) {
let numType = 'd'
// Check if we need to find the full path
if (destination.split('/').length === 2) {
const deviceInstance = destination.split('/')[1]
destination = searchHaystack(this.services, deviceInstance, destination.split('/')[0])
}
if (Number.isInteger(value)) { numType = 'i' }
if (typeof value === 'string') { numType = 's' }
this.bus.invoke({
path,
destination,
interface: 'com.victronenergy.BusItem',
member: 'SetValue',
body: [[numType, value]],
signature: 'v'
},
err => {
if (err) console.error(`Error setting value for ${destination}, ${path}, ${value}: ${err}`)
}
)
}
}
module.exports = VictronDbusListener