-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
69 lines (61 loc) · 1.57 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
const { spawn } = require('child_process')
const schema = require('./schema')
const pkgData = require('./package.json')
module.exports = function (app) {
let child
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
function run_python_plugin(options) {
let args = ['plugin.py']
child = spawn('ve/bin/python', args, { cwd: __dirname })
child.stdout.on('data', data => {
app.debug(data.toString())
try {
data.toString().split(/\r?\n/).forEach(line => {
// console.log(JSON.stringify(line))
if (line.length > 0) {
app.handleMessage(undefined, JSON.parse(line))
}
})
} catch (e) {
console.error(e.message)
}
})
child.stderr.on('data', fromChild => {
console.error(fromChild.toString())
})
child.on('error', err => {
console.error(err)
})
child.on('close', code => {
if (code !== 0) {
console.warn(`Plugin exited ${code}, restarting...`)
}
child = undefined
})
child.stdin.write(JSON.stringify(options))
child.stdin.write('\n')
};
return {
start: async (options) => {
while (true) {
if (child === undefined) {
run_python_plugin(options);
}
await sleep(1000);
}
},
stop: () => {
if (child) {
process.kill(child.pid)
child = undefined
}
},
schema,
id: pkgData.name,
name: "Victron Instant Data over BLE"
}
}