Integrate login and auth server API

This commit is contained in:
2018-11-13 02:45:16 -07:00
parent bd44438277
commit 75d4395964
8 changed files with 319 additions and 238 deletions

View File

@@ -10,6 +10,7 @@
"author": "",
"license": "MIT",
"dependencies": {
"axios": "^0.18.0",
"body-parser": "^1.18.2",
"express": "^4.16.2",
"socket.io": "^2.0.4"

View File

@@ -1,106 +1,20 @@
const express = require('express');
const bodyParser = require('body-parser');
const axios = require('axios');
const app = express();
const AUTH_SERVER_URL = 'http://localhost:8000';
// Enums
const lockStates = {
LOCK_OFF: 0,
LOCK_ARMED: 1,
LOCK_ON_PRESSED: 2,
LOCK_ON: 3,
LOCK_OFF_PRESSED: 4,
LOCK_PREARM: 1,
LOCK_ARMED: 2,
LOCK_ON_PRESSED: 3,
LOCK_ON: 4,
};
// Hardcoded data - can only be changed by admin
const toolData = {
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,
},
],
},
],
};
// 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 = [
{
id: 0,
mac: 'ABCDEF000000',
},
{
id: 1,
mac: '2C3AE843A15F',
},
{
id: 2,
mac: '2C3AE8439EAD',
},
{
id: 3,
mac: 'ABCDEF000003',
},
];
// Derived data - changes through use of system
let toolStatus = lockoutData.map(x => (
{
id: x.id,
action: '',
state: 'off',
lastState: 'n/a',
}
));
console.log(toolStatus);
let toolStatus = null;
const server = app.listen(8080, () => {
console.log('Example app listening on port 8080!');
@@ -118,64 +32,59 @@ app.use((req, res, next) => {
app.use(bodyParser.json());
app.use('/', express.static('dist'));
app.get('/api/tooldata', (req, res) => {
console.log('Request for tool data');
res.setHeader('Content-Type', 'application/json');
res.send(toolData);
});
app.get('/api/user', (req, res) => {
console.log('Request for user data');
res.setHeader('Content-Type', 'application/json');
res.send(users[0]);
});
app.post('/api/lockout/:mac', (req, res) => {
const mac = req.params.mac;
console.log('Request from MAC: ' + mac + ': ' + JSON.stringify(req.body));
if (toolStatus) {
const mac = req.params.mac;
console.log('Request from MAC:', mac, ': ', JSON.stringify(req.body));
const lockout = lockoutData.find(x => x.mac === mac);
if (!lockout) {
res.sendStatus(404);
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);
}
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;
}
res.setHeader('Content-Type', 'application/json');
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;
});
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'dist', 'index.html'));
res.sendStatus(404);
});
// Socket.io websocket stuff:
@@ -187,19 +96,43 @@ io.on('connection', socket => {
socket.emit('toolStatus', toolStatus);
socket.on('requestInterlock', data => {
console.log('Interlock change requested: ' + JSON.stringify(data));
console.log('Interlock change requested: ', JSON.stringify(data));
const user = users.find(x => x.username === data.username);
const toolId = data.change.toolId;
const token = data.token;
const toolSlug = data.change.toolSlug;
const action = data.change.action;
if (user && user.authorizedTools.includes(data.change.toolId)) {
const toolIndex = toolStatus.findIndex(x => x.id === toolId);
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;
toolStatus[toolIndex].action = action;
console.log(toolStatus);
io.sockets.emit('toolStatus', toolStatus);
}
console.log(profile.user, action, toolSlug);
io.sockets.emit('toolStatus', toolStatus);
}
})
.catch(err =>
console.log(err.message)
);
});
});
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);

View File

@@ -25,6 +25,14 @@ async-limiter@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.0.tgz#78faed8c3d074ab81f22b4e985d79e8738f720f8"
axios@^0.18.0:
version "0.18.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.18.0.tgz#32d53e4851efdc0a11993b6cd000789d70c05102"
integrity sha1-MtU+SFHv3AoRmTts0AB4nXDAUQI=
dependencies:
follow-redirects "^1.3.0"
is-buffer "^1.1.5"
backo2@1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/backo2/-/backo2-1.0.2.tgz#31ab1ac8b129363463e35b3ebb69f4dfcfba7947"
@@ -119,7 +127,7 @@ debug@2.6.9:
dependencies:
ms "2.0.0"
debug@~3.1.0:
debug@=3.1.0, debug@~3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
dependencies:
@@ -237,6 +245,13 @@ finalhandler@1.1.1:
statuses "~1.4.0"
unpipe "~1.0.0"
follow-redirects@^1.3.0:
version "1.5.9"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.9.tgz#c9ed9d748b814a39535716e531b9196a845d89c6"
integrity sha512-Bh65EZI/RU8nx0wbYF9shkFZlqLP+6WT/5FnA3cE/djNSuKNHJEinGGZgu/cQEkeeb2GdFOgenAmn8qaqYke2w==
dependencies:
debug "=3.1.0"
forwarded@~0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.1.2.tgz#98c23dab1175657b8c0573e8ceccd91b0ff18c84"
@@ -295,6 +310,11 @@ ipaddr.js@1.6.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/ipaddr.js/-/ipaddr.js-1.6.0.tgz#e3fa357b773da619f26e95f049d055c72796f86b"
is-buffer@^1.1.5:
version "1.1.6"
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be"
integrity sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==
isarray@2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-2.0.1.tgz#a37d94ed9cda2d59865c9f76fe596ee1f338741e"