You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.4 KiB
49 lines
1.4 KiB
const express = require('express'); |
|
const app = express(); |
|
|
|
const DOUT_LUT = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']; |
|
|
|
const server = app.listen(3000, function () { |
|
console.log('Example app listening on port 3000!'); |
|
}) |
|
|
|
const io = require('socket.io').listen(server); |
|
|
|
var ioState = { |
|
dout: [false, false, false, false], |
|
din: [false, false, false, false] |
|
} |
|
|
|
const updateState = data => { |
|
ioState[data.type] = ioState[data.type].map((x, i) => |
|
i == data.pin ? data.value : x |
|
); |
|
console.log(ioState); |
|
} |
|
|
|
app.use('/', express.static('dist')) |
|
|
|
app.get('/api/dio/:data', function (req, res) { |
|
const doutNum = ioState.dout.reduce((total, x, i) => x ? total + Math.pow(2, i) : total, 0); |
|
const doutChar = DOUT_LUT[doutNum]; |
|
res.send(doutChar); |
|
|
|
const dinChar = req.params.data || '0'; |
|
const dinArray = parseInt(dinChar, 16).toString(2).split('').reverse(); |
|
const din = new Array(4).fill(false).map((x, i) => dinArray[i] != '1').reverse(); |
|
|
|
if (ioState.din.toString() != din.toString()) { |
|
ioState.din = din; |
|
io.emit('ioState', ioState); |
|
console.log(din); |
|
} |
|
}) |
|
|
|
io.on('connection', socket => { |
|
socket.emit('ioState', ioState); |
|
socket.on('update', data => { |
|
console.log(data); |
|
updateState(data); |
|
io.sockets.emit('ioState', ioState); |
|
}); |
|
});
|
|
|