Added a print queue and rate limiting for the API

This commit is contained in:
plane000
2018-07-19 14:35:45 +01:00
parent 436e5726b1
commit 566c7fb0ad
7 changed files with 85 additions and 14 deletions

View File

@@ -8,9 +8,10 @@
<link rel="stylesheet" type="text/css" media="screen" href="style.css" />
</head>
<body>
<input class="input" id="name" type="text" placeholder="To Print">
<input class="input" id="name" type="text" placeholder="Name">
<input class="input1" id="coords" type="text" placeholder="Your coordinates (lat,long)">
<input class="update" id="update" type="submit" value="Submit" onclick="print();">
<script src="main.js"></script>
<div class="serverResponse" id="response"></div>
</body>
</html>

View File

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

View File

@@ -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%;
}

View File

@@ -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);
let inname = req.query.name;
let incoords = req.query.coords;
console.log('New print request for ', inname, ' at ', incoords);
Printer.print(name, coords);
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);
}

7
helper.js Normal file
View File

@@ -0,0 +1,7 @@
module.exports.sleep = function(ms) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, ms);
});
}

View File

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

Binary file not shown.