-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
68 lines (47 loc) · 1.53 KB
/
server.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
var http = require("http");
//var url = require("url");
var SerialPort = require("serialport").SerialPort;
var ledcube = new SerialPort("/dev/ttyACM1", {
baudrate: 9600,
dataBits: 8,
parity: 'none',
stopBits: 1,
flowControl: false
});
var ledcubeReady = false;
ledcube.on("open", function(){
console.log('ledcube connected');
setTimeout(cubeReady , 2000);
});
function cubeReady() {
ledcubeReady = true;
ledcube.on('data', function getData(data) {
receivedData = data.toString();
console.log(receivedData);
});
}
function mainLoop() {
console.log('open');
}
function onRequest(request, response) {
//var pathname = url.parse(request.url).pathname;
//console.log("url " + request.url);
var bits = request.url.replace(/[^01]/g, "");
if(bits.length==64) {
console.log("64 bits baby");
console.log("bits " + bits);
var bytes = bits.match(/.{8}/g).map(function(x) { return String.fromCharCode( parseInt(x, 2) ); }).join("");
console.log("grouped " + bits.match(/.{8}/g).map(function(x) { return parseInt(x, 2); }) );
console.log("bytes " + bytes);
ledcube.write(bytes);
if(bytes.length!=8) {console.log("no 8 bytes!!");}
response.writeHead(200);
} else {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("request should contain 64 bits, now contains "+bits.length);
}
//input.match(/.{2}/g).map(function(x) { return parseInt(x, 16); })
response.end();
}
http.createServer(onRequest).listen(8000);
console.log("server listening on port 8000");