diff --git a/client/index.html b/client/index.html index 1d50074..216db99 100644 --- a/client/index.html +++ b/client/index.html @@ -8,9 +8,10 @@ - + +
\ No newline at end of file diff --git a/client/main.js b/client/main.js index e46118e..12b04b3 100644 --- a/client/main.js +++ b/client/main.js @@ -8,6 +8,10 @@ async function print() { } let http = new XMLHttpRequest(); + http.onreadystatechange = function() { + let response = this.responseText; + document.getElementById('response').innerHTML = 'Server responded with: ' + response; + } let string = '/print?name=' + name + '&coords=' + coords; await http.open('GET', string, true); diff --git a/client/style.css b/client/style.css index 1125c7f..22f6629 100644 --- a/client/style.css +++ b/client/style.css @@ -11,7 +11,7 @@ input { input[type=text] { -webkit-transition: width 0.4s ease-in-out; transition: width 0.4s ease-in-out; - width: 20%; + width: 300px; padding: 12px 20px; margin: 8px 0; border-radius: 4px; @@ -28,7 +28,7 @@ input[type=text]:focus { input[type=submit] { top: 56%; - width: 20%; + width: 300px; background-color: rgb(66, 183, 238); border-radius: 4px; color: white; @@ -40,3 +40,8 @@ input[type=submit] { input[type=submit]:hover { background-color: rgb(59, 164, 212); } + +.serverResponse { + position: absolute; + top: 63%; +} diff --git a/endpoints.js b/endpoints.js index 4985e31..2d5efe6 100644 --- a/endpoints.js +++ b/endpoints.js @@ -1,5 +1,6 @@ const bodyParser = require('body-parser'); const express = require('express'); +const Helper = require('./helper'); const fs = require('fs'); const Printer = require('./print'); @@ -19,11 +20,34 @@ app.get('/', function (req, res) { res.sendFile( __dirname + '/client'); }); -app.get('/print', function (req, res) { - let name = req.query.name; - let coords = req.query.coords; +app.get('/print', async function (req, res) { + let rate = await rateLimit(); + if (rate == -1) return; - console.log(name, coords); - - Printer.print(name, coords); + let inname = req.query.name; + let incoords = req.query.coords; + console.log('New print request for ', inname, ' at ', incoords); + + let set = Printer.getprintqueue(); + set.push({ + name: inname, + coords: incoords + }); + Printer.setprintqueue(set); + if (!Printer.printing()) Printer.print(); + res.end('200 OK'); }); + +let requests = []; +async function rateLimit() { + if (requests.indexOf(req.connection.remoteAddress) != -1) { + res.end('400 BAD REQUEST: TO MANY REQUESTS'); + await Helper.sleep('3000'); + + while (requests.indexOf(req.connection.remoteAddress) != -1) { + requests.splice(requests.indexOf(req.connection.remoteAddress), 1); + } + return -1; + } + requests.push(req.connection.remoteAddress); +} diff --git a/helper.js b/helper.js new file mode 100644 index 0000000..1930ff2 --- /dev/null +++ b/helper.js @@ -0,0 +1,7 @@ +module.exports.sleep = function(ms) { + return new Promise((resolve, reject) => { + setTimeout(() => { + resolve(); + }, ms); + }); + } \ No newline at end of file diff --git a/print.js b/print.js index c585400..b771c43 100644 --- a/print.js +++ b/print.js @@ -1,8 +1,34 @@ const PythonShell = require('python-shell'); -const Result = require('./resultbuilder'); const WeatherAPI = require('./weatherAPI'); +const Result = require('./resultbuilder'); +const Helper = require('./helper'); -module.exports.print = async function(name, coords) { +let printQueue = []; +let printing = false; + +module.exports.getprintqueue = function() { + return printQueue; +} + +module.exports.setprintqueue = function(tmp) { + printQueue = tmp; +} + +module.exports.printing = function() { + return printing; +} + +module.exports.print = async function() { + printing = true; + while (printQueue.length > 0) { + await currentprint(printQueue[0].name, printQueue[0].coords); + await Helper.sleep(200); + printQueue.splice(0, 1); + } + printing = false; +} + +async function currentprint(name, coords) { let text = Result.genCompilerSettings({ padding: 2, lineBreaks: 1, @@ -29,7 +55,11 @@ module.exports.print = async function(name, coords) { } ] }); - PythonShell.run('printer/write.py', text, function (err, results) { - if (err) throw err; - }); + 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) {} } \ No newline at end of file diff --git a/printer/__pycache__/Adafruit_Thermal.cpython-37.pyc b/printer/__pycache__/Adafruit_Thermal.cpython-37.pyc new file mode 100644 index 0000000..888b052 Binary files /dev/null and b/printer/__pycache__/Adafruit_Thermal.cpython-37.pyc differ