-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
standalone.js
68 lines (59 loc) · 1.51 KB
/
standalone.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
/**
* standalone.js
*
* @description exports a class that shims the Signal K server, in order to make this plugin available as a library in other software
* @module @signalk/vedirect-serial-usb
* @author Fabian Tollenaar <[email protected]> (https://essense.ai)
* @license Apache_2.0
*/
const EventEmitter = require('events')
const SKPlugin = require('./index')
class VEDirect extends EventEmitter {
constructor (config = {}, _debug = false) {
super()
this._debug = _debug
this.app = {
handleMessage: (kind, data) => {
if (kind === 'pluginId') {
this.emit('delta', data)
return
}
this.emit(kind, data)
},
debug: (args) => this.debug(args),
options: {
device: 'Serial',
connection: '/dev/ttyUSB0',
port: 7878,
ignoreChecksum: true,
mainBatt: 'House',
auxBatt: 'Starter',
bmv: 'bmv',
solar: 'Main',
...(config || {}),
}
}
this.plugin = SKPlugin(this.app)
this.start()
}
start () {
if (!this.plugin || !this.plugin.hasOwnProperty('start')) {
this.debug(`Plugin not initialised, can't start`)
return
}
this.plugin.start(this.app.options)
}
stop () {
if (!this.plugin || !this.plugin.hasOwnProperty('stop')) {
this.debug(`Plugin not initialised, can't stop`)
return
}
this.plugin.stop()
}
debug (...args) {
if (this._debug) {
console.log.apply(console, args)
}
}
}
module.exports = VEDirect