-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
159 lines (149 loc) · 4.9 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
const serial = require('./lib/serial')
const udp = require('./lib/udp')
const tcp = require('./lib/tcp')
const Parser = require('./lib/Parser')
const VEDirect = require('./standalone')
module.exports = function (app) {
let parser = []
let shaddow = null
const plugin = {}
plugin.id = 'vedirect-signalk'
plugin.name = 'VE.Direct to Signal K'
plugin.description = plugin.name
plugin.start = function (options) {
shaddow = options;
if (typeof options.vedirect !== 'undefined') {
Object.keys(options.vedirect).forEach(items => {
parser[items] = new Parser(options)
parser[items].on('delta', delta => {
app.handleMessage('pluginId', delta)
})
let type = options.vedirect[items].device;
let connection = options.vedirect[items].connection;
let port = options.vedirect[items].port;
if (type == 'Serial') {
serial.open(connection, parser, app.debug, items)
} else if (type == 'UDP') {
udp.listen(port, parser, app.debug, items)
} else if (type == 'TCP') {
tcp.connect(connection, port, parser, app.debug, items)
}
});
} else {
if (options.device) {
oldConfig = {vedirect: [{device: 'Serial', connection: options.device, ignoreChecksum: options.ignoreChecksum,
mainBatt: options.mainBatt, auxBatt: options.auxBatt, bmv: options.bmv, solar: options.solar}]}
} else if (options.udpPort) {
oldConfig = {vedirect: [{device: 'UDP', port: options.udpPort, ignoreChecksum: options.ignoreChecksum,
mainBatt: options.mainBatt, auxBatt: options.auxBatt, bmv: options.bmv, solar: options.solar}]}
} else if (options.host) {
oldConfig = {vedirect: [{device: 'TCP', connection: options.host, port: options.tcpPort, ignoreChecksum: options.ignoreChecksum,
mainBatt: options.mainBatt, auxBatt: options.auxBatt, bmv: options.bmv, solar: options.solar}]}
}
parser[0] = new Parser(oldConfig)
parser[0].on('delta', delta => {
app.handleMessage('pluginId', delta)
})
if (options.device) {
serial.open(options.device, parser, app.debug, 0)
} else if (options.udpPort) {
udp.listen(options.udpPort, parser, app.debug, 0)
} else if (options.host) {
tcp.connect(options.host, options.tcpPort, parser, app.debug, 0)
}
}
}
plugin.stop = function () {
if (shaddow) {
if (typeof shaddow.vedirect !== 'undefined') {
Object.keys(shaddow.vedirect).forEach(items => {
parser[items].removeAllListeners()
parser[items] = null
let type = shaddow.vedirect[items].device;
if (type == 'Serial') {
serial.close(app.debug, items)
} else if (type == 'UDP') {
udp.close(app.debug, items)
} else {
tcp.close(app.debug, items)
}
});
} else {
parser[0].removeAllListeners()
parser[0] = null
if (shaddow.device) {
serial.close(app.debug, 0)
} else if (shaddow.udpPort) {
udp.close(app.debug, 0)
} else if (shaddow.host) {
tcp.close(app.debug, 0)
}
}
shaddow = null
}
}
plugin.schema = {
type: 'object',
properties: {
vedirect: {
type: 'array',
title: 'Connections',
description: 'Connections to VE.Direct devices',
items: {
type: 'object',
required: [],
properties: {
device: {
type: 'string',
default: 'Serial',
title: 'Select device',
enum: [
'Serial',
'UDP',
'TCP',
],
},
connection: {
type: 'string',
title: 'Connection details',
description: 'Serial: e.g. /dev/ttyUSB0, UDP: ignored or TCP: IP address',
default: '/dev/ttyUSB0'
},
port: {
type: 'number',
title: 'Port',
description: 'Serial: ignored, UDP/TCP: port',
default: 7878
},
ignoreChecksum: {
type: 'boolean',
title: 'Ignore Checksum',
default: true
},
mainBatt: {
type: 'string',
title: 'Main Battery name in SK path',
default: 'House'
},
auxBatt: {
type: 'string',
title: 'Aux Battery name in SK path',
default: 'Starter'
},
bmv: {
type: 'string',
title: 'BMV name in SK path',
default: 'bmv'
},
solar: {
type: 'string',
title: 'Solar name in SK path',
default: 'Main'
}
},
},
},
},
}
return plugin
}