Sync tool status across all clients
This commit is contained in:
+78
-11
@@ -1,6 +1,7 @@
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
|
||||
// Hardcoded data - can only be changed by admin
|
||||
const toolData = {
|
||||
categories: [
|
||||
{
|
||||
@@ -50,30 +51,60 @@ const toolData = {
|
||||
],
|
||||
};
|
||||
|
||||
const user = {
|
||||
username: "protospace",
|
||||
name: "Protospace User",
|
||||
authorizedTools: [1, 2],
|
||||
}
|
||||
// Hardcoded data - can only be changed by admin
|
||||
const users = [
|
||||
{
|
||||
username: "protospace",
|
||||
name: "Protospace User",
|
||||
authorizedTools: [1, 2],
|
||||
},
|
||||
];
|
||||
|
||||
// Hardcoded data - can only be changed by admin
|
||||
const lockoutData = {
|
||||
lockouts: [
|
||||
{
|
||||
id: 0,
|
||||
mac: 'ABCDEF000000',
|
||||
},
|
||||
{
|
||||
id: 1,
|
||||
mac: '2C3AE843A15F',
|
||||
relayOn: false,
|
||||
ledOn: true,
|
||||
date: '2018-02-01',
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
mac: 'ABCDEF000002',
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
mac: 'ABCDEF000003',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
// Derived data - changes through use of system
|
||||
let toolStatus = lockoutData.lockouts.map(x => (
|
||||
{
|
||||
id: x.id,
|
||||
on: false,
|
||||
armed: false,
|
||||
user: null,
|
||||
}
|
||||
));
|
||||
|
||||
console.log(toolStatus);
|
||||
|
||||
const server = app.listen(8080, function () {
|
||||
console.log('Example app listening on port 8080!');
|
||||
});
|
||||
const io = require('socket.io')(server);
|
||||
|
||||
// Express http server stuff:
|
||||
|
||||
// TODO : remove on prod
|
||||
app.use(function(req, res, next) {
|
||||
res.header("Access-Control-Allow-Origin", "*");
|
||||
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
||||
res.header('Access-Control-Allow-Origin', '*');
|
||||
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
|
||||
next();
|
||||
});
|
||||
|
||||
@@ -90,7 +121,7 @@ app.get('/api/user', function (req, res) {
|
||||
console.log('Request for user data');
|
||||
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.send(user);
|
||||
res.send(users[0]);
|
||||
});
|
||||
|
||||
app.get('/api/lockout/:mac', function (req, res) {
|
||||
@@ -106,3 +137,39 @@ app.get('/api/lockout/:mac', function (req, res) {
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.send(JSON.stringify(data));
|
||||
});
|
||||
|
||||
// Socket.io websocket stuff:
|
||||
|
||||
// TODO : remove on prod
|
||||
io.origins('*:*');
|
||||
|
||||
io.on('connection', socket => {
|
||||
socket.emit('toolStatus', toolStatus);
|
||||
|
||||
socket.on('requestInterlock', data => {
|
||||
console.log('Interlock change requested: ' + data.toString());
|
||||
|
||||
const user = users.find(x => x.username === data.username);
|
||||
const toolId = data.change.toolId;
|
||||
const action = data.change.action;
|
||||
|
||||
// TODO ; Make this part prettier
|
||||
if (user) {
|
||||
if (user.authorizedTools.includes(data.change.toolId)) {
|
||||
const toolIndex = toolStatus.findIndex(x => x.id === toolId);
|
||||
let tool = toolStatus[toolIndex];
|
||||
|
||||
if (action === 'arm' && !tool.armed && !tool.on) {
|
||||
tool.armed = true;
|
||||
} else if (action === 'disarm' && tool.armed) {
|
||||
tool.armed = false;
|
||||
tool.on = false;
|
||||
}
|
||||
|
||||
toolStatus[toolIndex] = tool;
|
||||
console.log(toolStatus);
|
||||
io.sockets.emit('toolStatus', toolStatus);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user