-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainCap-brim.js
71 lines (67 loc) · 1.95 KB
/
trainCap-brim.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
var io = require('socket.io-client')
var socket = io.connect('http://localhost:8080')
socket.emit('targetConnected', {})
socket.on('setSpeed', function(data) {
console.log("Got speed from server "+data.speed)
global_speed = data.speed
sendPy("l1.speed = "+global_speed.toString())
})
socket.on('setF', function(data) {
console.log("HAA")
if(data.f[0] == "f") {
console.log("Got function lamp from server "+data.f)
sendPy("l1."+data.f.toString()+" = True")
}
else if(data.f == "reverse") {
sendPy("l1.direction = 0")
setTimeout(function() {
sendPy("l1.speed = "+global_speed.toString())
}, 100);
}
else {
console.log("Got unknown function from server "+data.f)
}
})
socket.on('unsetF', function(data) {
console.log("unset HAA")
if(data.f[0] == "f") {
console.log("Got function lamp from server "+data.f)
sendPy("l1."+data.f.toString()+" = False")
}
else if(data.f == "reverse") {
sendPy("l1.direction = 1")
setTimeout(function() {
sendPy("l1.speed = "+global_speed.toString())
}, 100);
}
else {
console.log("Got unknown function from server "+data.f)
}
})
var spawn = require('child_process').spawn
var python_process = spawn('/usr/bin/python',['./brimWorker.py'])
function sendPy(cmd) {
console.log("#########"+cmd)
python_process.stdin.write(cmd+"\n")
}
python_process.stdout.on('data', function(data) {
console.log('stdout: '+data)
});
python_process.stderr.on('data', function(data) {
console.log('stderr: '+data)
});
python_process.stderr.on('close', function(code) {
console.log('close: '+code)
});
var global_speed = 0
/*
sendPy("from dccpi import *")
sendPy("e = DCCRPiEncoder(pin_a=8,pin_b=9,pin_break=7)")
sendPy("c = DCCController(e)")
sendPy("l1 = DCCLocomotive('DCC', 3)")
sendPy("c.register(l1)")
sendPy("l1.speed = 4")
*/
setInterval(function() {
// sendPy("l1.speed = "+global_speed.toString())
},1000)