-
Notifications
You must be signed in to change notification settings - Fork 0
/
talker.js
72 lines (58 loc) · 1.93 KB
/
talker.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
71
// Begin Server Implementation
// ---------------------------
// Dependencies
var spawn = require('child_process').spawn;
var express = require('express');
var bodyParser = require('body-parser');
var instance
var IP_ADDRESS = '127.0.0.1';
var sys = require('sys')
var exec = require('child_process').exec;
var rpio = require('rpio');
rpio.open(12, rpio.OUTPUT, rpio.LOW);
// Log server output to stdout
function log(data) {
process.stdout.write(data.toString());
}
// Create an express web app
var app = express();
//app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.post('/restart', function(request, response) {
var command = request.body.command;
var yourscript = exec('sh /opt/talker/talker.sh ' + command,
(error, stdout, stderr) => {
//console.log(request.body.command);
console.log(`${stdout}`);
console.log(`${stderr}`);
if (error !== null) {
console.log(`exec error: ${error}`);
}
});
for (var i = 0; i < 5; i++) {
/* On for 1 second */
rpio.write(12, rpio.HIGH);
rpio.sleep(1);
/* Off for half a second (500ms) */
rpio.write(12, rpio.LOW);
rpio.msleep(500);
}
// buffer output for a quarter of a second, then reply to HTTP request
var buffer = [];
var collector = function(data) {
data = data.toString();
buffer.push(data.split(']: ')[1]);
};
// Delay for a bit, then send a response with the latest server output
requestAsJson = JSON.stringify(request.body);
//set the appropriate HTTP header
response.setHeader('Content-Type', 'application/json');
//log the output
console.log('The POST data received was: ' + requestAsJson);
//send the POST data back as JSON
response.end(requestAsJson);
});
// Listen for incoming HTTP requests on port 3000
app.listen(3000);
process.on('exit', function() {
});