forked from sbender9/signalk-venus-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
219 lines (198 loc) · 6.15 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
const PLUGIN_ID = 'venus'
const PLUGIN_NAME = 'Victron Venus Plugin'
const debug = require('debug')('signalk-venus-plugin')
const promiseRetry = require('promise-retry')
const _ = require('lodash')
const createDbusListener = require('./dbus-listener')
const venusToDeltas = require('./venusToDeltas')
const gpsDestination = 'com.victronenergy.gps'
module.exports = function (app) {
const plugin = {}
let onStop = []
let dbusSetValue
let pluginStarted = false
plugin.id = PLUGIN_ID
plugin.name = PLUGIN_NAME
plugin.description =
'Plugin taking Battery, and other, from the D-Bus in Venus'
plugin.schema = {
title: PLUGIN_NAME,
type: 'object',
properties: {
installType: {
type: 'string',
title: 'How to connect to Venus D-Bus',
enum: ['local', 'remote'],
enumNames: [
'Connect to localhost (signalk-server is running on a Venus device)',
'Connect to remote Venus installation'
],
default: 'local'
},
dbusAddress: {
type: 'string',
title: 'Address for remote Venus device (D-Bus address notation)',
default: 'tcp:host=192.168.1.57,port=78'
},
pollInterval: {
type: 'number',
title: 'Interval (in seconds) to poll venus for current values',
default: 20
},
relayPath0: {
type: 'string',
title: 'The Signal K path for relay 1',
default: 'electrical.switches.venus-0'
},
relayDisplayName0: {
type: 'string',
title: 'The Display Name for relay 1',
},
relayPath1: {
type: 'string',
title: 'The Signal K path for relay 2',
default: 'electrical.switches.venus-1'
},
relayDisplayName1: {
type: 'string',
title: 'The Display Name for relay 2',
},
/*,
sendPosistion: {
type: 'boolean',
title: 'Send Signal K position, course and speed to venus',
default: false
} */
}
}
function handleMessage (delta) {
app.handleMessage(PLUGIN_ID, delta)
}
function setValueCallback (msg) {
dbusSetValue(msg.destination, msg.path, msg.value)
}
function actionHandler(context, path, value, relay, cb) {
debug(`setting relay ${relay} to ${value}`)
dbusSetValue('com.victronenergy.system',
`/Relay/${relay}/State`,
value ? 1 : 0)
setTimeout(() => {
var val = app.getSelfPath(path)
if ( val && val.value == value ) {
cb({ state: 'SUCCESS' })
} else {
cb({
state: 'FAILURE',
message: 'Did not receive change confirmation'
})
}
}, 1000)
return { state: 'PENDING' }
}
function getActionHandler(relay) {
return (context, path, value, cb) => {
return actionHandler(context, path, value, relay, cb)
}
}
/*
Called when the plugin is started (server is started with plugin enabled
or the plugin is enabled from ui on a running server).
*/
plugin.start = function (options) {
var { toDelta } = venusToDeltas(app, options, handleMessage)
pluginStarted = true
plugin.options = options
plugin.onError = () => {}
this.connect(options, toDelta)
if ( app.registerActionHandler ) {
[0, 1].forEach(relay => {
let path = (options['relayPath' + relay] || `electrical.switches.venus-${relay}`) + '.state'
app.registerActionHandler('vessels.self',
path,
getActionHandler(relay))
})
}
}
plugin.connect = function(options, toDelta) {
promiseRetry(
(retry, number) => {
if (!pluginStarted) {
return null
}
debug(`Dbus connection attempt ${number}`)
return createDbusListener(
app,
venusMessages => {
toDelta(venusMessages).forEach(delta => {
app.handleMessage(PLUGIN_ID, delta)
})
},
options.installType == 'remote' ? options.dbusAddress : null,
plugin,
_.isUndefined(options.pollInterval) ? 20 : options.pollInterval
).catch(retry)
},
{
maxTimeout: 30 * 1000,
forever: true
}
)
.then(dbus => {
if (dbus && pluginStarted) {
plugin.onError = () => {
app.removeListener('venusSetValue', setValueCallback)
dbus.onStop()
onStop = []
plugin.onError = () => {}
plugin.connect(options, toDelta)
}
dbusSetValue = dbus.setValue
onStop.push(dbus.onStop)
app.on('venusSetValue', setValueCallback)
onStop.push(() =>
app.removeListener('venusSetValue', setValueCallback)
)
}
})
.catch(error => {
console.error(`error creating dbus listener: ${error}`)
})
}
/*
function handleDelta(delta) {
if ( delta.updates ) {
delta.updates.forEach(update => {
if ( typeof update.source !== 'undefined'
&& typeof update.source.label !== 'undefined'
&& update.source.label === "venus") {
//don't reset data we got from venus
return
}
update.values.forEach((valuePath) => {
if ( valuePath.path === 'navigation.speedOverGround' ) {
dbusSetValue(gpsDestination, '/Speed', Math.round(valuePath.value * 100))
} else if ( valuePath.path === 'navigation.courseOverGroundTrue' ) {
dbusSetValue(gpsDestination, '/Course', Math.round(radsToDeg(valuePath.value)))
} else if ( valuePath.path === 'navigation.position' ) {
dbusSetValue(gpsDestination, '/Location/Latitude', Math.round(valuePath.value.latitude*100000))
dbusSetValue(gpsDestination, '/Location/Longitude', Math.round(valuePath.value.longitude*10000))
}
})
})
}
}
*/
/*
Called when the plugin is disabled on a running server with the plugin enabled.
*/
plugin.stop = function () {
debug('stop')
pluginStarted = false
onStop.forEach(f => f())
onStop = []
}
return plugin
}
function radsToDeg (radians) {
return radians * 180 / Math.PI
}