pslockout/webserver/server.js

207 lines
4.7 KiB
JavaScript
Raw Normal View History

2018-02-02 06:46:44 +00:00
const express = require('express');
2018-02-06 06:37:44 +00:00
const bodyParser = require('body-parser');
2018-02-02 06:46:44 +00:00
const app = express();
2018-02-06 06:37:44 +00:00
// Enums
const lockStates = {
LOCK_OFF: 0,
LOCK_ARMED: 1,
LOCK_ON_PRESSED: 2,
LOCK_ON: 3,
LOCK_OFF_PRESSED: 4,
};
2018-02-04 02:59:39 +00:00
// Hardcoded data - can only be changed by admin
2018-02-04 00:20:58 +00:00
const toolData = {
2018-02-03 08:48:49 +00:00
categories: [
{
name: 'Woodshop',
slug: 'woodshop',
info: 'Some info about the woodshop.',
photo: 'https://i.imgur.com/RIm8aoG.jpg',
tools: [
{
id: 0,
name: 'White Bandsaw',
photo: 'http://wiki.protospace.ca/images/thumb/f/f8/105.jpg/146px-105.jpg',
notes: 'Requires training. Tighten tension lever prior to use / Loosen tension lever after use. Tension lever is on rear.',
wikiId: 105,
},
{
id: 1,
name: 'Jointer',
photo: 'http://wiki.protospace.ca/images/thumb/6/6f/73.jpg/302px-73.jpg',
notes: 'Boards must be ABSOLUTELY FREE of nails, staples, screws, or other metal. Check boards thoroughly each and every time, make no presumptions. One mistake will chip the blade and render it useless (drum blades are very expensive).',
wikiId: 73,
},
],
},
{
name: 'Metalshop',
slug: 'metalshop',
info: 'Some info about the metalshop.',
photo: 'https://i.imgur.com/A2L2ACY.jpg',
tools: [
{
id: 2,
name: 'Tormach CNC',
photo: 'http://wiki.protospace.ca/images/thumb/c/cf/49.jpg/320px-49.jpg',
notes: 'Must complete Shop Safety, Lathe Training, 3D CAD Training, CAM Training, 1 on 1 Project.',
wikiId: 49,
},
{
id: 3,
name: 'Powerfist Bench Grinder',
photo: 'http://wiki.protospace.ca/images/thumb/c/cd/39.jpg/298px-39.jpg',
notes: 'A bench grinder is for grinding steels or deburring steel or aluminum (if fitted with a wire wheel).',
wikiId: 39,
},
],
},
],
};
2018-02-04 02:59:39 +00:00
// Hardcoded data - can only be changed by admin
const users = [
{
username: "protospace",
name: "Protospace User",
authorizedTools: [1, 2],
},
];
2018-02-04 00:20:58 +00:00
2018-02-04 02:59:39 +00:00
// Hardcoded data - can only be changed by admin
2018-02-04 00:20:58 +00:00
const lockoutData = {
lockouts: [
{
2018-02-04 02:59:39 +00:00
id: 0,
mac: 'ABCDEF000000',
},
{
id: 1,
2018-02-04 00:20:58 +00:00
mac: '2C3AE843A15F',
2018-02-04 02:59:39 +00:00
},
{
id: 2,
2018-02-06 06:37:44 +00:00
mac: '2C3AE8439EAD',
2018-02-04 02:59:39 +00:00
},
{
id: 3,
mac: 'ABCDEF000003',
2018-02-04 00:20:58 +00:00
},
],
2018-02-02 06:46:44 +00:00
};
2018-02-04 02:59:39 +00:00
// Derived data - changes through use of system
let toolStatus = lockoutData.lockouts.map(x => (
{
id: x.id,
2018-02-06 06:37:44 +00:00
action: '',
state: '',
lastState: 'n/a',
2018-02-04 02:59:39 +00:00
}
));
console.log(toolStatus);
2018-02-06 02:31:54 +00:00
const server = app.listen(8080, () => {
2018-02-03 08:48:49 +00:00
console.log('Example app listening on port 8080!');
});
2018-02-04 02:59:39 +00:00
const io = require('socket.io')(server);
// Express http server stuff:
2018-02-03 08:48:49 +00:00
2018-02-04 02:59:39 +00:00
// TODO : remove on prod
2018-02-06 02:31:54 +00:00
app.use((req, res, next) => {
2018-02-04 02:59:39 +00:00
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
2018-02-03 08:48:49 +00:00
next();
2018-02-02 06:46:44 +00:00
});
2018-02-06 06:37:44 +00:00
app.use(bodyParser.json());
2018-02-02 06:46:44 +00:00
app.use('/', express.static('dist'));
2018-02-06 02:31:54 +00:00
app.get('/api/tooldata', (req, res) => {
2018-02-04 00:20:58 +00:00
console.log('Request for tool data');
res.setHeader('Content-Type', 'application/json');
res.send(toolData);
});
2018-02-06 02:31:54 +00:00
app.get('/api/user', (req, res) => {
2018-02-04 00:20:58 +00:00
console.log('Request for user data');
2018-02-03 08:48:49 +00:00
res.setHeader('Content-Type', 'application/json');
2018-02-04 02:59:39 +00:00
res.send(users[0]);
2018-02-03 08:48:49 +00:00
});
2018-02-06 02:31:54 +00:00
app.post('/api/lockout/:mac', (req, res) => {
2018-02-02 06:46:44 +00:00
const mac = req.params.mac;
2018-02-06 06:37:44 +00:00
console.log('Request from MAC: ' + mac + ': ' + JSON.stringify(req.body));
2018-02-02 06:46:44 +00:00
2018-02-06 02:31:54 +00:00
const lockout = lockoutData.lockouts.find(x => x.mac === mac);
2018-02-06 06:37:44 +00:00
if (!lockout) {
res.sendStatus(404);
2018-02-02 06:46:44 +00:00
}
2018-02-06 06:37:44 +00:00
const toolIndex = toolStatus.findIndex(x => x.id === lockout.id);
let tool = toolStatus[toolIndex];
tool.lastState = tool.state;
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;
}
2018-02-02 06:46:44 +00:00
res.setHeader('Content-Type', 'application/json');
2018-02-06 06:37:44 +00:00
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;
2018-02-02 06:46:44 +00:00
});
2018-02-04 02:59:39 +00:00
// Socket.io websocket stuff:
// TODO : remove on prod
io.origins('*:*');
io.on('connection', socket => {
socket.emit('toolStatus', toolStatus);
socket.on('requestInterlock', data => {
2018-02-06 06:37:44 +00:00
console.log('Interlock change requested: ' + JSON.stringify(data));
2018-02-04 02:59:39 +00:00
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);
2018-02-06 06:37:44 +00:00
toolStatus[toolIndex].action = action;
2018-02-04 02:59:39 +00:00
console.log(toolStatus);
io.sockets.emit('toolStatus', toolStatus);
}
}
});
});