-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ls
executable file
·64 lines (53 loc) · 1.59 KB
/
main.ls
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
ntp = require "./ntp.js"
mmt = require \moment
con = require \connect
dgr = require \dgram
prt = require \serialport
fig = require \figlet
srv = require \ws .Server
ws-server = new srv { port: 8000 }
args = process.argv
length = (.length)
fig "PiSTON", {font: 'Delta Corps Priest 1'}, (_, data) -> console.log data
if (length args) <= 2 then console.log "using defaults"
set-decoder = (dn) ->
js = "./decoders/#dn.js"
console.log js
require js
clients = []
disconnect = (c) ->
i = clients.index-of c
clients.splice(i, 1) if i != -1
codec = set-decoder switch args.3
| \nmea => \nmea
| \tss => \tss
| \kongsberg-ea => \kongsberg-ea
| otherwise => "nmea"
receive-chunk = (chunk) ->
for msg in codec.receive chunk
obj = piston-time: mmt.utc!, raw: msg
obj <<< codec.decode msg
json = JSON.stringify obj
for c in clients
c.send json
start-com = ->
prt.list (err, [port]) ->
console.log "com:#{port.com-name}"
ser = new prt.SerialPort port.com-name, { baudrate : 19200 }, true
ser.on \open -> ser.on \data, receive-chunk
start-udp = (port) ->
console.log "udp:#port"
udp = new dgr.create-socket \udp4
udp.on \message, receive-chunk
udp.on \error (msg) -> console.log "error: #msg"
udp.bind port
switch args.4
| \com => start-com!
| \udp => start-udp args.5 or 40001
| otherwise => start-com!
ws-server.on \connection (ws) ->
clients.push ws
ws.on \message (m) -> console.log m
ws.on \close -> disconnect ws
ws.on \error -> disconnect ws
con!use (con.static __dirname) .listen args.2 or 8080