This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.coffee
76 lines (53 loc) · 1.68 KB
/
server.coffee
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
path = require('path')
exec = require('child_process').exec
express = require('express')
glob = require('glob')
root = path.normalize(__dirname)
commandsPath = "#{root}/commands"
app = express()
getTemps = (mode) ->
glob.sync("on_*_#{mode}.txt", cwd: commandsPath).map((f) -> f.match(/_(\d+)_/)[1])
commands =
heat: getTemps('heat')
cool: getTemps('cool')
start = ->
#host = '127.0.0.1'
host = '0.0.0.0'
port = 2990
server = app.listen port, host
console.log "App listening at http://#{host}:#{port}"
server.on 'error', (err) ->
switch err.code
when 'EADDRINUSE'
console.error "Port #{port} already taken"
throw err
sendCommand = (mode, temp, cb) ->
file = if mode is 'off'
"off.txt"
else
"on_#{temp}_#{mode}.txt"
command = "igclient --send=#{commandsPath}/#{file}"
console.log "executing: #{command}"
exec command, (err, stdout, stderr) ->
return cb(err) if err
if !stdout?.length or stdout.indexOf('send: success') is -1
return cb("Send didn't complete successfully:\n#{stdout}")
cb()
setupRoutes = ->
app.post "/", (req, res, next) ->
mode = req.param('mode')
temp = req.param('temp')
if mode is 'off'
sendCommand 'off', null, (err) ->
return next(err) if err
res.send 'ok'
else
return res.status(400).send("Invalid mode") unless commands[mode]
return res.status(400).send("Invalid temp") unless temp
if temp not in commands[mode]
return res.status(400).send("temp must be in #{commands[mode]}")
sendCommand mode, temp, (err) ->
return next(err) if err
res.send 'ok'
setupRoutes()
start()