Fixed breaking bug and server now responds with print status
This commit is contained in:
@@ -19,4 +19,5 @@ async function print() {
|
|||||||
|
|
||||||
document.getElementById('coords').value = '';
|
document.getElementById('coords').value = '';
|
||||||
document.getElementById('name').value = '';
|
document.getElementById('name').value = '';
|
||||||
|
document.getElementById('response').innerHTML = 'Printing...'
|
||||||
}
|
}
|
||||||
11
endpoints.js
11
endpoints.js
@@ -21,7 +21,7 @@ app.get('/', function (req, res) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.get('/print', async function (req, res) {
|
app.get('/print', async function (req, res) {
|
||||||
let rate = await rateLimit();
|
let rate = await rateLimit(req, res);
|
||||||
if (rate == -1) return;
|
if (rate == -1) return;
|
||||||
|
|
||||||
let inname = req.query.name;
|
let inname = req.query.name;
|
||||||
@@ -31,17 +31,18 @@ app.get('/print', async function (req, res) {
|
|||||||
let set = Printer.getprintqueue();
|
let set = Printer.getprintqueue();
|
||||||
set.push({
|
set.push({
|
||||||
name: inname,
|
name: inname,
|
||||||
coords: incoords
|
coords: incoords,
|
||||||
|
req: req,
|
||||||
|
res: res
|
||||||
});
|
});
|
||||||
Printer.setprintqueue(set);
|
Printer.setprintqueue(set);
|
||||||
if (!Printer.printing()) Printer.print();
|
if (!Printer.printing()) Printer.print();
|
||||||
res.end('200 OK');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let requests = [];
|
let requests = [];
|
||||||
async function rateLimit() {
|
async function rateLimit(req, res) {
|
||||||
if (requests.indexOf(req.connection.remoteAddress) != -1) {
|
if (requests.indexOf(req.connection.remoteAddress) != -1) {
|
||||||
res.end('400 BAD REQUEST: TO MANY REQUESTS');
|
res.end('429 TO MANY REQUESTS');
|
||||||
await Helper.sleep('3000');
|
await Helper.sleep('3000');
|
||||||
|
|
||||||
while (requests.indexOf(req.connection.remoteAddress) != -1) {
|
while (requests.indexOf(req.connection.remoteAddress) != -1) {
|
||||||
|
|||||||
19
print.js
19
print.js
@@ -21,14 +21,18 @@ module.exports.printing = function() {
|
|||||||
module.exports.print = async function() {
|
module.exports.print = async function() {
|
||||||
printing = true;
|
printing = true;
|
||||||
while (printQueue.length > 0) {
|
while (printQueue.length > 0) {
|
||||||
await currentprint(printQueue[0].name, printQueue[0].coords);
|
await currentprint(printQueue[0].name, printQueue[0].coords, printQueue[0].req, printQueue[0].res);
|
||||||
await Helper.sleep(200);
|
await Helper.sleep(200);
|
||||||
printQueue.splice(0, 1);
|
printQueue.splice(0, 1);
|
||||||
}
|
}
|
||||||
printing = false;
|
printing = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function currentprint(name, coords) {
|
async function currentprint(name, coords, req, res) {
|
||||||
|
let currentWeather = await WeatherAPI.getWeather(1, coords);
|
||||||
|
let todaysForcast = await WeatherAPI.getWeather(2, coords);
|
||||||
|
if (currentWeather == -1 || todaysForcast == -1) res.end('400 BAD REQUEST: INVALID COORDINATES');
|
||||||
|
|
||||||
let text = Result.genCompilerSettings({
|
let text = Result.genCompilerSettings({
|
||||||
padding: 2,
|
padding: 2,
|
||||||
lineBreaks: 1,
|
lineBreaks: 1,
|
||||||
@@ -44,22 +48,23 @@ async function currentprint(name, coords) {
|
|||||||
size: `small`
|
size: `small`
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
content: `${await WeatherAPI.getWeather(1, coords)}`,
|
content: `${currentWeather}`,
|
||||||
align: `left`,
|
align: `left`,
|
||||||
size: `small`
|
size: `small`
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
content: `The forcast for today is ${await WeatherAPI.getWeather(2, coords)}`,
|
content: `The forcast for today is ${todaysForcast}`,
|
||||||
align: `left`,
|
align: `left`,
|
||||||
size: `small`
|
size: `small`
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
try {
|
await PythonShell.run('printer/write.py', text, async function (err, results) {
|
||||||
await PythonShell.run('printer/write.py', text, function (err, results) {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log('Could not open serial connection for ' + name + '\'s print');
|
console.log('Could not open serial connection for ' + name + '\'s print');
|
||||||
|
res.end('500 INTERNAL SERVER ERROR: PRINTER NOT RESPONDING');
|
||||||
|
} else {
|
||||||
|
res.end('200 OK');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (err) {}
|
|
||||||
}
|
}
|
||||||
@@ -11,9 +11,13 @@ module.exports.getWeather = async function(mode, coords) {
|
|||||||
res = await req.send();
|
res = await req.send();
|
||||||
} catch (e) { }
|
} catch (e) { }
|
||||||
|
|
||||||
|
try {
|
||||||
if (mode == 1) {
|
if (mode == 1) {
|
||||||
return res.body.currently.summary;
|
return res.body.currently.summary;
|
||||||
} else if (mode == 2) {
|
} else if (mode == 2) {
|
||||||
return res.body.hourly.summary;
|
return res.body.hourly.summary;
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user