init server

master
vitrinekast 11 months ago
commit f6f69d6f62

1
.gitignore vendored

@ -0,0 +1 @@
/node_modules

@ -0,0 +1,95 @@
const { SerialPort } = require('serialport')
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const movement = 1000;
// Create a port
const port = new SerialPort({
path: '/dev/ttyUSB1',
baudRate: 9600,
})
// app.use(bodyParser.json());
const toPlotter = (message) => {
console.log("i will write to plotter ", message);
port.write(message, function (err) {
if (err) {
return console.log('Error on write: ', err.message)
}
console.log('message written: ', message)
})
}
// Open errors will be emitted as an error event
port.on('error', function (err) {
console.log('Error: ', err.message)
})
port.on("open", function () {
console.log('open');
port.on('data', function (data) {
console.log(data);
});
});
app.post('/init', (req, res) => {
toPlotter(`IN;SC0,0,100,0,100;SP1;PA60,60;PU`)
res.send('Initialise the plotter')
})
app.post('/pen-up', (req, res) => {
toPlotter(`PU;`);
res.send("Sending a message to the plotter about PU;",)
});
app.post('/pen-down', (req, res) => {
toPlotter(`PD;`);
res.send("Sending a message to the plotter about PD;",)
});
app.post('/move-left', (req, res) => {
toPlotter(`PR-${movement},0;`);
res.send("Sending a message to the plotter about PR-10,0;",)
});
app.post('/move-right', (req, res) => {
toPlotter(`PR${movement},0;`);
res.send("Sending a message to the plotter about PR10,0;",)
});
app.post('/move-up', (req, res) => {
toPlotter(`PR0,${movement},0;`);
res.send("Sending a message to the plotter about PR0,10;",)
});
app.post('/move-down', (req, res) => {
toPlotter(`PR0,-${movement};`);
res.send("Sending a message to the plotter about PR0,-10;",)
});
// GET /text?message={message}
app.post('/text', (req, res) => {
toPlotter(`SI0.5,0.8;LB${req.query.message};`);
res.send(`SI0.5,0.8;LB${req.query.message};`)
});
// GET /direct?message={message}
app.post('/direct', (req, res) => {
toPlotter(`${req.query.message}`);
res.send(`${req.query.message}`)
});
app.use(express.static('public'))
app.listen(3000, () => {
console.log(`Example app listening on port`)
})

@ -0,0 +1,16 @@
{
"name": "plotterparty",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.2",
"express": "^4.18.2",
"serialport": "^12.0.0"
}
}

@ -0,0 +1,58 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>TwitchPlaysPlotter</h1>
<ul>
<li><button class="fn-action" data-action="init">init</button></li>
<li><button class="fn-action" data-action="pen-up">pen-up</button></li>
<li><button class="fn-action" data-action="pen-down">pen-down</button></li>
<li><button class="fn-action" data-action="move-up">move-up</button></li>
<li><button class="fn-action" data-action="move-left">move-left</button></li>
<li><button class="fn-action" data-action="move-right">move-right</button></li>
<li><button class="fn-action" data-action="move-down">move-down</button></li>
<li>
<form class="fn-form">
<input type="text" placeholder="send a direct command">
<input type="submit" />
</form>
</li>
</ul>
<script type="text/javascript">
async function sendCommand(path, data) {
try {
console.log(JSON.stringify(data));
const response = await fetch("/" + path, {
method: "POST", // or 'PUT'
});
const result = await response.json();
console.log("Success:", result);
} catch (error) {
console.error("Error:", error);
}
}
document.querySelector(".fn-form").addEventListener("submit", (e) => {
e.preventDefault();
sendCommand("direct?message=" + e.currentTarget.querySelector("input[type='text']").value);
})
document.querySelectorAll(".fn-action").forEach((button) => {
button.addEventListener("click", (e) => {
sendCommand(e.currentTarget.getAttribute("data-action"));
})
})
</script>
</body>
</html>
Loading…
Cancel
Save