-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (82 loc) · 2.34 KB
/
index.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var raspi = require('raspi-io');
var five = require('johnny-five');
var board = new five.Board({
io: new raspi()
});
var http = require('http');
var interval = 6000;
var speed = 120;
var offset = 16;
board.on('ready', function() {
var internet_gekkies = new five.Motor({pins:{pwm:26,dir:21}});
var unexpectables = new five.Motor({pins:{pwm:23,dir:22}});
internet_gekkies.stop();
unexpectables.stop();
console.log('Ready to rumble!!');
http.createServer(function(request, response) {
var headers = request.headers;
var method = request.method;
var url = request.url;
var body = [];
request.on('error', function(err) {
console.error(err);
}).on('data', function(chunk) {
body.push(chunk);
}).on('end', function() {
// At this point, we have the headers, method, url and body, and
// do whatever we need to in order to respond to this request.
body = Buffer.concat(body).toString();
if (method == 'POST') {
var msg = null;
response.writeHead(200, {
'Content-Type': 'text/plain'
});
// parse the body!
try {
msg = JSON.parse(body);
} catch (e) {
response.write('NOOB')
}
// valid json
if (msg !== null) {
if (msg.kill == true) {
internet_gekkies.stop();
unexpectables.stop();
response.write('OK')
} else if (msg.hacktrack == true) {
internet_gekkies.forward(speed + offset);
// setTimeout(function () {
// unexpectables.forward(speed + offset)
// }, 300)
unexpectables.forward(speed + offset)
interval = (msg.interval !== undefined) ? parseInt(msg.interval) : 30000;
board.wait(interval, function() {
internet_gekkies.stop();
unexpectables.stop();
});
response.write('OK')
} else if (msg.highlight == 'accepted') {
var project_id = parseInt(msg.project.id);
if (project_id == 1435890) {
console.log('InternetGekkies!!');
internet_gekkies.forward(speed);
board.wait(interval, function() {
internet_gekkies.stop();
});
} else if (project_id == 1547353) {
console.log('Unexpectables');
unexpectables.forward(speed);
board.wait(interval, function() {
unexpectables.stop();
});
}
response.write('OK')
} else {
response.write('EUH?')
}
}
response.end();
}
});
}).listen(1337);
});