From df5521d323cf5d093e6da2b433bcecfdceedc57a Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sat, 28 Oct 2017 03:09:06 +0000 Subject: [PATCH] Add support for DIN --- server.js | 35 +++++++++++++++++++++++++---------- src/app.js | 53 +++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 64 insertions(+), 24 deletions(-) diff --git a/server.js b/server.js index 7a947f5..9c5932c 100644 --- a/server.js +++ b/server.js @@ -1,8 +1,17 @@ -const express = require('express') -const app = express() +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], + dout: [false, false, false, false], + din: [false, false, false, false] } const updateState = data => { @@ -14,15 +23,21 @@ const updateState = data => { app.use('/', express.static('dist')) -app.get('/api/dout/:pin', function (req, res) { - res.send(ioState.dout[req.params.pin]) -}) +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 server = app.listen(3000, function () { - console.log('Example app listening on port 3000!') -}) + 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(); -const io = require('socket.io').listen(server); + if (ioState.din.toString() != din.toString()) { + ioState.din = din; + io.emit('ioState', ioState); + console.log(din); + } +}) io.on('connection', socket => { socket.emit('ioState', ioState); diff --git a/src/app.js b/src/app.js index c3b11b1..93468d6 100644 --- a/src/app.js +++ b/src/app.js @@ -13,6 +13,11 @@ export default class App extends React.Component { componentDidMount() { this.connect(); + + this.setState({ioState: { + dout: [false, true, false, false], + din: [false, false, true, false] + }}); } componentWillUnmount() { @@ -48,28 +53,48 @@ export default class App extends React.Component {
- LED Toggle Demo + Modem I/O Demo
{this.state.ioState &&
Digital Out
-
-
-
- DO1 + {this.state.ioState.dout.map((x, i) => ( +
+
+
+ DO{i+1} +
+
+ + +
+
+
+ {!x && } + {x && } +
-
- - + ))} +
+
} + {this.state.ioState &&
+
+
Digital In
+ {this.state.ioState.din.map((x, i) => ( +
+
+
+ DI{i+1} +
+
+
+ {!x && } + {x && } +
-
-
- {!this.state.ioState.dout[0] && } - {this.state.ioState.dout[0] && } -
-
+ ))}
}