You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.1 KiB
JavaScript

const express = require('express');
const app = express();
const UDP = require('dgram')
const client = UDP.createSocket('udp4')
const port = 52381
const hostname = '192.168.0.152'
app.listen(8080, () => {
console.log('Server running on port 8080');
require('child_process').exec("start http://localhost:8080");
});
app.use(express.static('public'));
app.use(express.json());
const fs = require('fs');
let playlist = JSON.parse(fs.readFileSync('playlist.json', 'utf8'));
app.post("/playlist", (req, res) => {
playlist = req.body;
fs.writeFileSync('playlist.json', JSON.stringify(playlist));
res.sendStatus(200);
});
app.get("/playlist", (req, res) => {
res.json(playlist);
});
app.post("/setpos", (req, res) => {
setPos(req.body.x, req.body.y, req.body.zoom, req.body.vs, req.body.hs);
res.sendStatus(200);
});
app.post("/run", (req, res) => {
run();
res.sendStatus(200);
});
function setPos(x, y, zoom, vs = 18, hs = 14) {
const packet = Buffer.from("01 00 00 09 00 00 00 00 81 01 06 02 12 0e 00 00 00 00 00 00 00 00 ff".replaceAll(" ", ""), "hex");
x1 = x & 0xf
x2 = (x >> 4) & 0xf
x3 = (x >> 8) & 0xf
x4 = (x >> 12) & 0xf
y1 = y & 0xf
y2 = (y >> 4) & 0xf
y3 = (y >> 8) & 0xf
y4 = (y >> 12) & 0xf
packet[14] = x4
packet[15] = x3
packet[16] = x2
packet[17] = x1
packet[18] = y4
packet[19] = y3
packet[20] = y2
packet[21] = y1
packet[12] = vs
packet[13] = hs
client.send(packet, port, hostname, (err) => {
if (err) {
console.error('Failed to send packet !!')
}
})
const zoomPacket = Buffer.from("01 00 00 09 00 00 00 00 81 01 04 47 00 00 00 00 FF".replaceAll(" ", ""), "hex");
z1 = zoom & 0xf
z2 = (zoom >> 4) & 0xf
z3 = (zoom >> 8) & 0xf
z4 = (zoom >> 12) & 0xf
zoomPacket[12] = z4
zoomPacket[13] = z3
zoomPacket[14] = z2
zoomPacket[15] = z1
client.send(zoomPacket, port, hostname, (err) => {
if (err) {
console.error('Failed to send packet !!')
}
})
}
function run() {
let delaySum = 0;
for (let i = 0; i < playlist.length; i++) {
setTimeout(() => {
setPos(playlist[i].x, playlist[i].y, playlist[i].zoom, playlist[i].vs, playlist[i].hs)
}, delaySum);
delaySum += playlist[i].delay;
}
}