Prepare web side for Pi hosting
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"body-parser": "^1.18.2",
|
||||
"express": "^4.16.2",
|
||||
"socket.io": "^2.0.4"
|
||||
}
|
||||
|
@@ -1,6 +1,16 @@
|
||||
const express = require('express');
|
||||
const bodyParser = require('body-parser');
|
||||
const app = express();
|
||||
|
||||
// Enums
|
||||
const lockStates = {
|
||||
LOCK_OFF: 0,
|
||||
LOCK_ARMED: 1,
|
||||
LOCK_ON_PRESSED: 2,
|
||||
LOCK_ON: 3,
|
||||
LOCK_OFF_PRESSED: 4,
|
||||
};
|
||||
|
||||
// Hardcoded data - can only be changed by admin
|
||||
const toolData = {
|
||||
categories: [
|
||||
@@ -73,7 +83,7 @@ const lockoutData = {
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
mac: 'ABCDEF000002',
|
||||
mac: '2C3AE8439EAD',
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
@@ -86,9 +96,9 @@ const lockoutData = {
|
||||
let toolStatus = lockoutData.lockouts.map(x => (
|
||||
{
|
||||
id: x.id,
|
||||
on: false,
|
||||
armable: false,
|
||||
armed: false,
|
||||
action: '',
|
||||
state: '',
|
||||
lastState: 'n/a',
|
||||
}
|
||||
));
|
||||
|
||||
@@ -108,6 +118,8 @@ app.use((req, res, next) => {
|
||||
next();
|
||||
});
|
||||
|
||||
app.use(bodyParser.json());
|
||||
|
||||
app.use('/', express.static('dist'));
|
||||
|
||||
app.get('/api/tooldata', (req, res) => {
|
||||
@@ -126,18 +138,42 @@ app.get('/api/user', (req, res) => {
|
||||
|
||||
app.post('/api/lockout/:mac', (req, res) => {
|
||||
const mac = req.params.mac;
|
||||
console.log('Request from MAC: ' + mac + ': ' + JSON.stringify(req.body));
|
||||
|
||||
const lockout = lockoutData.lockouts.find(x => x.mac === mac);
|
||||
if (!data) {
|
||||
res.send(404);
|
||||
if (!lockout) {
|
||||
res.sendStatus(404);
|
||||
}
|
||||
|
||||
const tool = toolStatus.find(x => x.id === lockout.id);
|
||||
const toolIndex = toolStatus.findIndex(x => x.id === lockout.id);
|
||||
let tool = toolStatus[toolIndex];
|
||||
tool.lastState = tool.state;
|
||||
|
||||
console.log('Request from MAC: ' + mac);
|
||||
switch (req.body.lockState) {
|
||||
case lockStates.LOCK_OFF:
|
||||
tool.state = 'off';
|
||||
break;
|
||||
case lockStates.LOCK_ARMED:
|
||||
tool.state = 'armed';
|
||||
break;
|
||||
case lockStates.LOCK_ON:
|
||||
tool.state = 'on';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.send(JSON.stringify({ armable: tool.armable }));
|
||||
res.send(JSON.stringify({ action: tool.action }));
|
||||
|
||||
if (tool.state != tool.lastState) {
|
||||
tool.action = '';
|
||||
|
||||
console.log(toolStatus);
|
||||
io.sockets.emit('toolStatus', toolStatus);
|
||||
}
|
||||
|
||||
toolStatus[toolIndex] = tool;
|
||||
});
|
||||
|
||||
// Socket.io websocket stuff:
|
||||
@@ -149,7 +185,7 @@ io.on('connection', socket => {
|
||||
socket.emit('toolStatus', toolStatus);
|
||||
|
||||
socket.on('requestInterlock', data => {
|
||||
console.log('Interlock change requested: ' + data.toString());
|
||||
console.log('Interlock change requested: ' + JSON.stringify(data));
|
||||
|
||||
const user = users.find(x => x.username === data.username);
|
||||
const toolId = data.change.toolId;
|
||||
@@ -159,15 +195,9 @@ io.on('connection', socket => {
|
||||
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.armable = true;
|
||||
} else if (action === 'disarm') {
|
||||
tool.armable = false;
|
||||
}
|
||||
toolStatus[toolIndex].action = action;
|
||||
|
||||
toolStatus[toolIndex] = tool;
|
||||
console.log(toolStatus);
|
||||
io.sockets.emit('toolStatus', toolStatus);
|
||||
}
|
||||
|
Reference in New Issue
Block a user