|
|
|
@ -3,8 +3,15 @@ const bodyParser = require('body-parser'); |
|
|
|
|
const axios = require('axios'); |
|
|
|
|
const app = express(); |
|
|
|
|
|
|
|
|
|
const DEBUG_LOGGING = false; |
|
|
|
|
|
|
|
|
|
const AUTH_SERVER_URL = 'http://localhost:8000'; |
|
|
|
|
|
|
|
|
|
const log = (string) => { |
|
|
|
|
date = new Date().toISOString().replace('T', ' ').split('.')[0]; |
|
|
|
|
console.log(date, '-', string); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Enums
|
|
|
|
|
const lockStates = { |
|
|
|
|
LOCK_OFF: 0, |
|
|
|
@ -17,7 +24,7 @@ const lockStates = { |
|
|
|
|
let toolStatus = null; |
|
|
|
|
|
|
|
|
|
const server = app.listen(8080, () => { |
|
|
|
|
console.log('Example app listening on port 8080!'); |
|
|
|
|
log('Lockout socket server listening on port 8080!'); |
|
|
|
|
}); |
|
|
|
|
const io = require('socket.io')(server); |
|
|
|
|
|
|
|
|
@ -35,7 +42,7 @@ app.use(bodyParser.json()); |
|
|
|
|
app.post('/api/lockout/:mac', (req, res) => { |
|
|
|
|
if (toolStatus) { |
|
|
|
|
const mac = req.params.mac; |
|
|
|
|
console.log('Request from MAC:', mac, ': ', JSON.stringify(req.body)); |
|
|
|
|
DEBUG_LOGGING && log(`${mac} - Lock state: ${Object.keys(lockStates)[req.body.lockState]}`); |
|
|
|
|
|
|
|
|
|
const tmp = Object.entries(toolStatus).find(x => x[1].mac == mac) |
|
|
|
|
const toolSlug = tmp ? tmp[0] : null; |
|
|
|
@ -65,9 +72,9 @@ app.post('/api/lockout/:mac', (req, res) => { |
|
|
|
|
if (tool.state != tool.lastState) clearAction = true; |
|
|
|
|
|
|
|
|
|
if (clearAction) { |
|
|
|
|
tool.action = ''; |
|
|
|
|
log(`${mac} - Report | Previous state: ${tool.lastState} | New state: ${tool.state} | Website action: ${tool.action || 'N/A'}`); |
|
|
|
|
|
|
|
|
|
console.log(tool); |
|
|
|
|
tool.action = ''; |
|
|
|
|
io.sockets.emit('toolStatus', toolStatus); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -95,13 +102,18 @@ io.origins('*:*'); |
|
|
|
|
io.on('connection', socket => { |
|
|
|
|
socket.emit('toolStatus', toolStatus); |
|
|
|
|
|
|
|
|
|
socket.on('requestInterlock', data => { |
|
|
|
|
console.log('Interlock change requested: ', JSON.stringify(data)); |
|
|
|
|
socket.on('log', string => { |
|
|
|
|
log(string); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
socket.on('requestInterlock', data => { |
|
|
|
|
const username = data.username; |
|
|
|
|
const token = data.token; |
|
|
|
|
const toolSlug = data.change.toolSlug; |
|
|
|
|
const action = data.change.action; |
|
|
|
|
|
|
|
|
|
log(`${username} - Request | Tool: ${toolSlug} | Action: ${action}`); |
|
|
|
|
|
|
|
|
|
axios.get(AUTH_SERVER_URL + '/user/', { |
|
|
|
|
headers: {'Authorization': 'Token ' + token}, |
|
|
|
|
}) |
|
|
|
@ -110,12 +122,14 @@ io.on('connection', socket => { |
|
|
|
|
if (profile && profile.authorized_tools.includes(toolSlug)) { |
|
|
|
|
toolStatus[toolSlug].action = action; |
|
|
|
|
|
|
|
|
|
console.log(profile.user, action, toolSlug); |
|
|
|
|
log(`${username} - Allowed | Tool: ${toolSlug} | Action: ${action}`); |
|
|
|
|
io.sockets.emit('toolStatus', toolStatus); |
|
|
|
|
} else { |
|
|
|
|
log(`${username} - DISALLOWED | Tool: ${toolSlug} | Action: ${action}`); |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
.catch(err => |
|
|
|
|
console.log(err.message) |
|
|
|
|
log(err.message) |
|
|
|
|
); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
@ -133,6 +147,6 @@ setInterval(() => { |
|
|
|
|
io.sockets.emit('toolStatus', toolStatus); |
|
|
|
|
}) |
|
|
|
|
.catch(err => |
|
|
|
|
console.log(err.message) |
|
|
|
|
log(err.message) |
|
|
|
|
); |
|
|
|
|
}, 10000); |
|
|
|
|