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-11-13 09:45:16 +00:00
|
|
|
const axios = require('axios');
|
2018-02-02 06:46:44 +00:00
|
|
|
const app = express();
|
|
|
|
|
2018-11-13 09:45:16 +00:00
|
|
|
const AUTH_SERVER_URL = 'http://localhost:8000';
|
|
|
|
|
2018-02-06 06:37:44 +00:00
|
|
|
// Enums
|
|
|
|
const lockStates = {
|
|
|
|
LOCK_OFF: 0,
|
2018-11-13 09:45:16 +00:00
|
|
|
LOCK_PREARM: 1,
|
|
|
|
LOCK_ARMED: 2,
|
|
|
|
LOCK_ON_PRESSED: 3,
|
|
|
|
LOCK_ON: 4,
|
2018-02-03 08:48:49 +00:00
|
|
|
};
|
|
|
|
|
2018-11-13 09:45:16 +00:00
|
|
|
let toolStatus = null;
|
2018-02-04 02:59:39 +00:00
|
|
|
|
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-06 02:31:54 +00:00
|
|
|
app.post('/api/lockout/:mac', (req, res) => {
|
2018-11-13 09:45:16 +00:00
|
|
|
if (toolStatus) {
|
|
|
|
const mac = req.params.mac;
|
|
|
|
console.log('Request from MAC:', mac, ': ', JSON.stringify(req.body));
|
|
|
|
|
|
|
|
const tmp = Object.entries(toolStatus).find(x => x[1].mac == mac)
|
|
|
|
const toolSlug = tmp ? tmp[0] : null;
|
|
|
|
|
|
|
|
if (toolSlug) {
|
|
|
|
let clearAction = false;
|
|
|
|
let tool = toolStatus[toolSlug];
|
|
|
|
tool.lastState = tool.state;
|
|
|
|
|
|
|
|
switch (req.body.lockState) {
|
|
|
|
case lockStates.LOCK_OFF:
|
|
|
|
tool.state = 'off';
|
|
|
|
if (tool.action == 'disarm') clearAction = true;
|
|
|
|
break;
|
|
|
|
case lockStates.LOCK_ARMED:
|
|
|
|
tool.state = 'armed';
|
|
|
|
if (tool.action == 'arm') clearAction = true;
|
|
|
|
break;
|
|
|
|
case lockStates.LOCK_ON:
|
|
|
|
tool.state = 'on';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Track in case state is switched from elsewhere
|
|
|
|
if (tool.state != tool.lastState) clearAction = true;
|
|
|
|
|
|
|
|
if (clearAction) {
|
|
|
|
tool.action = '';
|
|
|
|
|
|
|
|
console.log(tool);
|
|
|
|
io.sockets.emit('toolStatus', toolStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.setHeader('Content-Type', 'application/json');
|
|
|
|
res.send(JSON.stringify({ action: tool.action }));
|
|
|
|
|
|
|
|
toolStatus[toolSlug] = tool;
|
|
|
|
} else {
|
|
|
|
res.sendStatus(404);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res.sendStatus(503);
|
2018-02-06 06:37:44 +00:00
|
|
|
}
|
2018-02-02 06:46:44 +00:00
|
|
|
});
|
2018-02-04 02:59:39 +00:00
|
|
|
|
2018-05-17 04:14:41 +00:00
|
|
|
app.get('*', (req, res) => {
|
2018-11-13 09:45:16 +00:00
|
|
|
res.sendStatus(404);
|
2018-05-17 04:14:41 +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-11-13 09:45:16 +00:00
|
|
|
console.log('Interlock change requested: ', JSON.stringify(data));
|
2018-02-04 02:59:39 +00:00
|
|
|
|
2018-11-13 09:45:16 +00:00
|
|
|
const token = data.token;
|
|
|
|
const toolSlug = data.change.toolSlug;
|
2018-02-04 02:59:39 +00:00
|
|
|
const action = data.change.action;
|
|
|
|
|
2018-11-13 09:45:16 +00:00
|
|
|
axios.get(AUTH_SERVER_URL + '/user/', {
|
|
|
|
headers: {'Authorization': 'Token ' + token},
|
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
const profile = res.data[0].profile || null;
|
|
|
|
if (profile && profile.authorized_tools.includes(toolSlug)) {
|
|
|
|
toolStatus[toolSlug].action = action;
|
|
|
|
|
|
|
|
console.log(profile.user, action, toolSlug);
|
|
|
|
io.sockets.emit('toolStatus', toolStatus);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err =>
|
|
|
|
console.log(err.message)
|
|
|
|
);
|
2018-02-04 02:59:39 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-13 09:45:16 +00:00
|
|
|
|
|
|
|
setInterval(() => {
|
|
|
|
axios.get(AUTH_SERVER_URL + '/tooldata/')
|
|
|
|
.then(res => {
|
|
|
|
toolStatus = res.data.categories
|
|
|
|
.reduce((a, x) => a.concat(x.tools), [])
|
|
|
|
.reduce((a, x) => ({...a, [x.slug]: a[x.slug] ? {
|
|
|
|
...a[x.slug], mac: x.mac
|
|
|
|
} : {
|
|
|
|
mac: x.mac, action: '', state: 'off', lastState: 'n/a'
|
|
|
|
}}), toolStatus || {});
|
|
|
|
io.sockets.emit('toolStatus', toolStatus);
|
|
|
|
})
|
|
|
|
.catch(err =>
|
|
|
|
console.log(err.message)
|
|
|
|
);
|
|
|
|
}, 10000);
|