...
 
Commits (2)
{"status":false,"origin":"Eindhoven","destination":"Helsinki","width":233,"height":300,"depth":300,"weight":300,"new":false}
\ No newline at end of file
{"status":false,"origin":"Eindhoven","destination":"Helsinki","width":233,"height":300,"depth":300,"weight":5000,"new":false,"opened":true,"added_time":123,"current_weight":5000}
\ No newline at end of file
......@@ -73,21 +73,41 @@ const setDbItem = (key, value, overwrite=true) => {
return false;
}
const scale = (num, in_min, in_max, out_min, out_max) => {
return (num - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
const twoDigitString = (num) => {
return num < 10 ? '0' + num : num;
}
app.get('/api/notification', (req, res) => {
const newItem = getDbItem('opened');
if (!newItem) {
setDbItem('opened', true);
const weightNum = getDbItem('weight') || 0;
const currentWeightNum = getDbItem('current_weight') || 1;
const weight = `${weightNum} kg`;
const currentWeight = scale(currentWeightNum, 0, 10000, 0, 1);
const newWeight = scale(currentWeightNum + weightNum, 0, 10000, 0, 1);
const increase = `${Math.round((newWeight / currentWeight) * 100)}%`;
const size = `${getDbItem('width')} x ${getDbItem('height')} x ${getDbItem('depth')} cm`;
const time = getDbItem('added_time') | 0;
const addedTime = time > 60 ? `+ ${twoDigitString(Math.round(time / 60))}:${twoDigitString(time % 60)}` : `+ ${time} min`;
const response = {
origin: getDbItem('origin'),
destination: getDbItem('destination'),
width: getDbItem('width'),
height: getDbItem('height'),
depth: getDbItem('depth'),
weight: getDbItem('weight'),
weight_string: weight,
weight_bar_primary: currentWeight,
weight_bar_secondary: newWeight,
weight_increase: increase,
size: size,
time_increase: addedTime,
}
console.log(colors.yellow(`New notification shown to user`));
setDbItem('opened', true);
console.log(colors.yellow(`${new Date().toLocaleString()} New notification shown to user`));
return res.json({result: response});
}
......@@ -114,13 +134,13 @@ app.post('/api/shipment', (req, res) => {
setDbItem(val, req.body[val]);
}
console.log(colors.green(`New shipment from ${req.body.origin} to ${req.body.destination}`));
console.log(colors.green(`${new Date().toLocaleString()} New shipment from ${req.body.origin} to ${req.body.destination}`));
setTimeout(() => {
if (getDbItem('status') == 0) {
setDbItem('status', false);
setDbItem('opened', true);
console.log(colors.red(`Timed out, declining...`));
console.log(colors.red(`${new Date().toLocaleString()} Timed out, declining...`));
}
}, 20000)
res.json({result: 'ok'});
......@@ -136,10 +156,10 @@ app.post('/api/accept', (req, res) => {
if (getDbItem('status') === 0) {
setDbItem('status', state);
console.log(colors.green(`Updated status: ${state}`));
console.log(colors.green(`${new Date().toLocaleString()} Updated status: ${state}`));
}
else {
console.log(colors.yellow(`Status was already set.`));
console.log(colors.yellow(`${new Date().toLocaleString()} Status was already set.`));
return res.json({result: 'Status was already set.'});
}
......