diff --git a/server.js b/server.js index 604726d..7a947f5 100644 --- a/server.js +++ b/server.js @@ -1,10 +1,21 @@ const express = require('express') const app = express() +var ioState = { + dout: [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('/hello', function (req, res) { - res.send('Hello World!') +app.get('/api/dout/:pin', function (req, res) { + res.send(ioState.dout[req.params.pin]) }) const server = app.listen(3000, function () { @@ -13,6 +24,11 @@ const server = app.listen(3000, function () { const io = require('socket.io').listen(server); -io.on('connection', (socket) => { - socket.emit('message', 'hello socket'); +io.on('connection', socket => { + socket.emit('ioState', ioState); + socket.on('update', data => { + console.log(data); + updateState(data); + io.sockets.emit('ioState', ioState); + }); }); diff --git a/src/app.js b/src/app.js index 43d7264..f5ebf48 100644 --- a/src/app.js +++ b/src/app.js @@ -6,9 +6,7 @@ export default class App extends React.Component { super(props); this.state = { - ioState: { - dout: [false], - }, + ioState: null, socket: io.connect() } } @@ -28,8 +26,9 @@ export default class App extends React.Component { console.log('connected'); }); - socket.on('message', (data) => { + socket.on('ioState', (data) => { console.log(data); + this.setState({ioState: data}); }); } @@ -42,6 +41,14 @@ export default class App extends React.Component { }); } + sendUpdate(type, pin, value) { + this.state.socket.emit('update', { + type: type, + pin: pin, + value: value, + }); + } + render() { return (
@@ -54,7 +61,7 @@ export default class App extends React.Component {
-
+ {this.state.ioState &&
Digital Out
@@ -63,8 +70,8 @@ export default class App extends React.Component { DO1
- - + +
@@ -73,7 +80,7 @@ export default class App extends React.Component {
- + } );