Improve logging on the web server
This commit is contained in:
parent
35c4ae4925
commit
95d6ac34a7
|
@ -3,8 +3,15 @@ const bodyParser = require('body-parser');
|
||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
|
const DEBUG_LOGGING = false;
|
||||||
|
|
||||||
const AUTH_SERVER_URL = 'http://localhost:8000';
|
const AUTH_SERVER_URL = 'http://localhost:8000';
|
||||||
|
|
||||||
|
const log = (string) => {
|
||||||
|
date = new Date().toISOString().replace('T', ' ').split('.')[0];
|
||||||
|
console.log(date, '-', string);
|
||||||
|
}
|
||||||
|
|
||||||
// Enums
|
// Enums
|
||||||
const lockStates = {
|
const lockStates = {
|
||||||
LOCK_OFF: 0,
|
LOCK_OFF: 0,
|
||||||
|
@ -17,7 +24,7 @@ const lockStates = {
|
||||||
let toolStatus = null;
|
let toolStatus = null;
|
||||||
|
|
||||||
const server = app.listen(8080, () => {
|
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);
|
const io = require('socket.io')(server);
|
||||||
|
|
||||||
|
@ -35,7 +42,7 @@ app.use(bodyParser.json());
|
||||||
app.post('/api/lockout/:mac', (req, res) => {
|
app.post('/api/lockout/:mac', (req, res) => {
|
||||||
if (toolStatus) {
|
if (toolStatus) {
|
||||||
const mac = req.params.mac;
|
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 tmp = Object.entries(toolStatus).find(x => x[1].mac == mac)
|
||||||
const toolSlug = tmp ? tmp[0] : null;
|
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 (tool.state != tool.lastState) clearAction = true;
|
||||||
|
|
||||||
if (clearAction) {
|
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);
|
io.sockets.emit('toolStatus', toolStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,13 +102,18 @@ io.origins('*:*');
|
||||||
io.on('connection', socket => {
|
io.on('connection', socket => {
|
||||||
socket.emit('toolStatus', toolStatus);
|
socket.emit('toolStatus', toolStatus);
|
||||||
|
|
||||||
socket.on('requestInterlock', data => {
|
socket.on('log', string => {
|
||||||
console.log('Interlock change requested: ', JSON.stringify(data));
|
log(string);
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on('requestInterlock', data => {
|
||||||
|
const username = data.username;
|
||||||
const token = data.token;
|
const token = data.token;
|
||||||
const toolSlug = data.change.toolSlug;
|
const toolSlug = data.change.toolSlug;
|
||||||
const action = data.change.action;
|
const action = data.change.action;
|
||||||
|
|
||||||
|
log(`${username} - Request | Tool: ${toolSlug} | Action: ${action}`);
|
||||||
|
|
||||||
axios.get(AUTH_SERVER_URL + '/user/', {
|
axios.get(AUTH_SERVER_URL + '/user/', {
|
||||||
headers: {'Authorization': 'Token ' + token},
|
headers: {'Authorization': 'Token ' + token},
|
||||||
})
|
})
|
||||||
|
@ -110,12 +122,14 @@ io.on('connection', socket => {
|
||||||
if (profile && profile.authorized_tools.includes(toolSlug)) {
|
if (profile && profile.authorized_tools.includes(toolSlug)) {
|
||||||
toolStatus[toolSlug].action = action;
|
toolStatus[toolSlug].action = action;
|
||||||
|
|
||||||
console.log(profile.user, action, toolSlug);
|
log(`${username} - Allowed | Tool: ${toolSlug} | Action: ${action}`);
|
||||||
io.sockets.emit('toolStatus', toolStatus);
|
io.sockets.emit('toolStatus', toolStatus);
|
||||||
|
} else {
|
||||||
|
log(`${username} - DISALLOWED | Tool: ${toolSlug} | Action: ${action}`);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(err =>
|
.catch(err =>
|
||||||
console.log(err.message)
|
log(err.message)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -133,6 +147,6 @@ setInterval(() => {
|
||||||
io.sockets.emit('toolStatus', toolStatus);
|
io.sockets.emit('toolStatus', toolStatus);
|
||||||
})
|
})
|
||||||
.catch(err =>
|
.catch(err =>
|
||||||
console.log(err.message)
|
log(err.message)
|
||||||
);
|
);
|
||||||
}, 10000);
|
}, 10000);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user