Fixed breaking bug and server now responds with print status

This commit is contained in:
plane000
2018-07-19 15:18:49 +01:00
parent 566c7fb0ad
commit d633cbe2f9
4 changed files with 31 additions and 20 deletions

View File

@@ -19,4 +19,5 @@ async function print() {
document.getElementById('coords').value = '';
document.getElementById('name').value = '';
document.getElementById('response').innerHTML = 'Printing...'
}

View File

@@ -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) {

View File

@@ -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) {
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');
}
});
} catch (err) {}
}

View File

@@ -11,9 +11,13 @@ module.exports.getWeather = async function(mode, coords) {
res = await req.send();
} catch (e) { }
try {
if (mode == 1) {
return res.body.currently.summary;
} else if (mode == 2) {
return res.body.hourly.summary;
}
} catch (e) {
return -1;
}
}