From d633cbe2f9e4e5094721987215c0a170bca687d2 Mon Sep 17 00:00:00 2001 From: plane000 Date: Thu, 19 Jul 2018 15:18:49 +0100 Subject: [PATCH] Fixed breaking bug and server now responds with print status --- client/main.js | 1 + endpoints.js | 11 ++++++----- print.js | 27 ++++++++++++++++----------- weatherAPI.js | 12 ++++++++---- 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/client/main.js b/client/main.js index 12b04b3..6900bcb 100644 --- a/client/main.js +++ b/client/main.js @@ -19,4 +19,5 @@ async function print() { document.getElementById('coords').value = ''; document.getElementById('name').value = ''; + document.getElementById('response').innerHTML = 'Printing...' } \ No newline at end of file diff --git a/endpoints.js b/endpoints.js index 2d5efe6..1636488 100644 --- a/endpoints.js +++ b/endpoints.js @@ -21,7 +21,7 @@ app.get('/', function (req, res) { }); app.get('/print', async function (req, res) { - let rate = await rateLimit(); + let rate = await rateLimit(req, res); if (rate == -1) return; let inname = req.query.name; @@ -31,17 +31,18 @@ app.get('/print', async function (req, res) { let set = Printer.getprintqueue(); set.push({ name: inname, - coords: incoords + coords: incoords, + req: req, + res: res }); Printer.setprintqueue(set); if (!Printer.printing()) Printer.print(); - res.end('200 OK'); }); let requests = []; -async function rateLimit() { +async function rateLimit(req, res) { 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'); while (requests.indexOf(req.connection.remoteAddress) != -1) { diff --git a/print.js b/print.js index b771c43..f6febf3 100644 --- a/print.js +++ b/print.js @@ -21,14 +21,18 @@ module.exports.printing = function() { module.exports.print = async function() { printing = true; 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); printQueue.splice(0, 1); } 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({ padding: 2, lineBreaks: 1, @@ -44,22 +48,23 @@ async function currentprint(name, coords) { size: `small` }, { - content: `${await WeatherAPI.getWeather(1, coords)}`, + content: `${currentWeather}`, align: `left`, size: `small` }, { - content: `The forcast for today is ${await WeatherAPI.getWeather(2, coords)}`, + content: `The forcast for today is ${todaysForcast}`, align: `left`, size: `small` } ] }); - try { - await PythonShell.run('printer/write.py', text, function (err, results) { - if (err) { - console.log('Could not open serial connection for ' + name + '\'s print'); - } - }); - } catch (err) {} + await PythonShell.run('printer/write.py', text, async function (err, results) { + if (err) { + 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'); + } + }); } \ No newline at end of file diff --git a/weatherAPI.js b/weatherAPI.js index 5c2c41e..bc9af0e 100644 --- a/weatherAPI.js +++ b/weatherAPI.js @@ -11,9 +11,13 @@ module.exports.getWeather = async function(mode, coords) { res = await req.send(); } catch (e) { } - if (mode == 1) { - return res.body.currently.summary; - } else if (mode == 2) { - return res.body.hourly.summary; + try { + if (mode == 1) { + return res.body.currently.summary; + } else if (mode == 2) { + return res.body.hourly.summary; + } + } catch (e) { + return -1; } }