-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
36 lines (30 loc) · 922 Bytes
/
Copy pathserver.js
File metadata and controls
36 lines (30 loc) · 922 Bytes
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
var exec = require('child_process').exec;
var bodyParser = require("body-parser");
var express = require('express')
var sys = require('sys')
var app = express()
app.use(bodyParser.json());
function puts(error, stdout, stderr) {
if (stdout) {console.log(stdout)};
if (stderr) {console.log(stderr)};
}
function changeSwitch(id, unit, state) {
var stateString = state ? "1" : "0"
var params = id + " " + unit + " " + stateString
exec("sudo rcswitch-pi/send " + params, puts)
}
app.get('/', function (req, res) {
res.send('Server up and running!')
})
app.post('/switch', function (req, res) {
var id = req.body.id
var unit = req.body.unit
var state = req.body.state
changeSwitch(id, unit, state)
res.send(req.body)
})
var server = app.listen(3000, function() {
var host = server.address().address
var port = server.address().port
console.log('Server listening at http://%s:%s', host, port)
})