-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
99 lines (84 loc) · 2.5 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const express = require('express');
const bodyParser = require('body-parser');
const json = require('./piface.json');
const piface = require('piface');
const WebSocket = require("ws");
const port = 3069;
let app = express();
app.use(bodyParser.json()); // for parsing application/json
app.use('/static', express.static('public'));
app.get('/', (req, res) => {
console.log("get index");
res.sendFile(__dirname+'/index.html');
});
app.listen(port, () => console.log(`app listening on port ${port}!`))
let portnum = "8069";
const wss = new WebSocket.Server({ port: portnum });
console.log("Websocket created on port " + portnum);
wss.on("connection", onConnected.bind(wss));
// initialize piface
piface.init();
let piState = json;
let startup = true;
function onConnected(socket, request) {
socket.on("message", onMessage.bind(socket));
socket.on("close", onClosed.bind(socket));
// address of websocket server
var me = this.address();
console.log(
"Connection" +
socket._socket.remoteAddress +
" -> " +
me.address +
":" +
me.port
);
setTimeout(sendScheduled.bind(socket), 1000);
}
function onMessage(message) {
// setPiface on message
console.log("Received: '" + message + "'");
setPiface(JSON.parse(message));
piState = JSON.parse(message);
this.send(message);
}
function onClosed() {
startup = true;
console.log("Closed connection to " + this._socket.remoteAddress);
}
function sendScheduled() {
if (!this) {
return;
}
if (this.readyState != this.OPEN) {
return;
}
// get pi status and send it to browser
let newPiState = getPiface();
if(piState.inputs.state != newPiState.inputs.state || startup) {
startup = false;
piState.inputs.state = newPiState.inputs.state;
this.send(JSON.stringify(piState));
console.log("Sent to " + this._socket.remoteAddress + ": " + JSON.stringify(newPiState));
}
setTimeout(sendScheduled.bind(this), 10);
}
// set state of piface
function setPiface(piState) {
let outputs = piState.outputs.state;
piface.write_output(convertBinToDec(outputs));
}
// get state of piface
function getPiface() {
let inputs = piface.read_input();
// necessary to clone object and avoid reference to old object
let newPiState = JSON.parse(JSON.stringify(piState));
newPiState.inputs.state = convertDecToBin(inputs);
return newPiState;
}
function convertDecToBin(decimal) {
return parseInt(decimal, 10).toString(2);
}
function convertBinToDec(binAsString) {
return parseInt(binAsString, 2).toString(10);
}