From f7232e3a30ce0eeb84063ccd38edc88f7e984d52 Mon Sep 17 00:00:00 2001 From: Raphael Arce Date: Mon, 21 Oct 2024 18:22:03 +0200 Subject: [PATCH 1/9] feat: reduce amount of pregenerated ideas Signed-off-by: Raphael Arce --- services/rest/idea-generation/constants.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/rest/idea-generation/constants.js b/services/rest/idea-generation/constants.js index 5a24050..d10beb9 100644 --- a/services/rest/idea-generation/constants.js +++ b/services/rest/idea-generation/constants.js @@ -17,4 +17,4 @@ export const strategies = { * The amount of pregenerated ideas that need to be generated in advance. * @type {number} */ -export const MIN_AMOUNT_OF_PREGENERATED_IDEAS = 2; +export const MIN_AMOUNT_OF_PREGENERATED_IDEAS = 1; From 90c047d86a7400a75db181ca91edfca8571216c0 Mon Sep 17 00:00:00 2001 From: Raphael Arce Date: Mon, 21 Oct 2024 18:22:58 +0200 Subject: [PATCH 2/9] feat: disable console logs Signed-off-by: Raphael Arce --- services/bluetooth/bluetooth.js | 77 +++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 28 deletions(-) diff --git a/services/bluetooth/bluetooth.js b/services/bluetooth/bluetooth.js index 241bfb3..101cfa5 100644 --- a/services/bluetooth/bluetooth.js +++ b/services/bluetooth/bluetooth.js @@ -1,9 +1,5 @@ import childProcess from "child_process"; -import { - setDiceSide, - getDices, - setDiceConnectionStatus, -} from "../state/state.js"; +import { setDiceSide, setDiceConnectionStatus } from "../state/state.js"; const diceAMacAddress = process.env.DICE_A_MAC_ADDRESS; const rfcommA = "1"; @@ -13,21 +9,33 @@ const diceCMacAddress = process.env.DICE_C_MAC_ADDRESS; const rfcommC = "3"; const dices = [ - { diceId: "A", diceMacAddress: diceAMacAddress, rfcomm: rfcommA }, - { diceId: "B", diceMacAddress: diceBMacAddress, rfcomm: rfcommB }, - { diceId: "C", diceMacAddress: diceCMacAddress, rfcomm: rfcommC }, + { + diceId: "A", + diceMacAddress: diceAMacAddress, + rfcomm: rfcommA, + }, + { + diceId: "B", + diceMacAddress: diceBMacAddress, + rfcomm: rfcommB, + }, + { + diceId: "C", + diceMacAddress: diceCMacAddress, + rfcomm: rfcommC, + }, ]; -const bluetoothSerialMonitors = dices.map((dice) => +export const bluetoothSerialMonitors = dices.map((dice) => createBluetoothSerialMonitor(dice), ); function createBluetoothSerialMonitor({ diceId, diceMacAddress, rfcomm }) { let isRestarting = false; - console.log( - `createBluetoothSerialMonitor, ${diceId}, ${diceMacAddress}, ${rfcomm}`, - ); + // console.log( + // `createBluetoothSerialMonitor, ${diceId}, ${diceMacAddress}, ${rfcomm}`, + // ); bindDiceToRfcomm(diceMacAddress, rfcomm); @@ -39,10 +47,17 @@ function createBluetoothSerialMonitor({ diceId, diceMacAddress, rfcomm }) { const initial = setTimeout( () => setDiceConnectionStatus({ [diceId]: "connected" }), - 5000, + 5_000, ); picocom.stdout.on("data", (data) => { + // console.log("data", data.toString()); + + if (data.toString().length > 2) { + // console.log("data is bigger than 2, ignoring for setting dice"); + return; + } + /** * The old dices send the side in the format "A1", "B2", "C3". * The new dices send the side in the format "1", "2", "3". @@ -52,9 +67,13 @@ function createBluetoothSerialMonitor({ diceId, diceMacAddress, rfcomm }) { * dice id in the firmware anymore. */ const side = data.toString().replace(/^\D+/g, ""); + // console.log("side", side); + + clearTimeout(initial); setDiceSide(`${diceId}${side}`); - console.log(getDices()); + + // console.log(getDices()); }); picocom.stderr.on("data", (data) => { @@ -69,9 +88,9 @@ function createBluetoothSerialMonitor({ diceId, diceMacAddress, rfcomm }) { clearTimeout(initial); cleanup({ diceId, diceMacAddress, rfcomm }); - console.log( - `picocom for dice ${diceId} threw an error. Retrying in 10 seconds..`, - ); + // console.log( + // `picocom for dice ${diceId} threw an error. Retrying in 30 seconds..`, + // ); setTimeout( () => @@ -97,19 +116,21 @@ function cleanup({ diceId, diceMacAddress, rfcomm }) { function bindDiceToRfcomm(diceMacAddress, rfcomm) { const stdout = childProcess.execSync( `sudo rfcomm bind ${rfcomm} ${diceMacAddress}`, + { stdio: "ignore" }, ); - console.log( - `bound dice ${diceMacAddress} to rfcomm${rfcomm}, stdout:`, - stdout.toString(), - ); + // console.log( + // `bound dice ${diceMacAddress} to rfcomm${rfcomm}, stdout:`, + // stdout.toString(), + // ); } function releaseDiceToRfcomm(diceMacAddress, rfcomm) { - const stdout = childProcess.execSync(`sudo rfcomm release rfcomm${rfcomm}`); - console.log( - `released dice ${diceMacAddress} from rfcomm${rfcomm}, stdout:`, - stdout.toString(), - ); + const stdout = childProcess.execSync(`sudo rfcomm release rfcomm${rfcomm}`, { + stdio: "ignore", + }); + // console.log( + // `released dice ${diceMacAddress} from rfcomm${rfcomm}, stdout:`, + // stdout.toString(), + // ); } - -export default bluetoothSerialMonitors; +1; From 5b7e906dddc4a84ed2221db78f33c90f25001e40 Mon Sep 17 00:00:00 2001 From: Raphael Arce Date: Mon, 21 Oct 2024 18:23:34 +0200 Subject: [PATCH 3/9] feat: add timeout to properly auto-start on pi Signed-off-by: Raphael Arce --- index.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 4a341df..f31cdb7 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,11 @@ setTimeout(async () => { - const bluetoothSerialMonitors = import("./services/bluetooth/bluetooth.js"); - const server = import("./services/rest/rest.js"); - const { webSocketServer } = import("./services/socket/socket.js"); - const { gpioProcess } = import("./services/gpio/gpio.js"); - await import("./services/print/print.js"); + const { bluetoothSerialMonitors } = await import( + "./services/bluetooth/bluetooth.js" + ); + const { server } = await import("./services/rest/rest.js"); + const { webSocketServer } = await import("./services/socket/socket.js"); + const { gpioProcess } = await import("./services/gpio/gpio.js"); + // await import("./services/print/print.js"); await import("./services/browser/browser.js"); process.on("SIGINT", () => closeEverything(1)); @@ -11,12 +13,12 @@ setTimeout(async () => { process.on("SIGTERM", () => closeEverything(0)); function closeEverything(eventId) { - bluetoothSerialMonitors.forEach((monitor) => monitor.kill()); server.close(); webSocketServer.clients.forEach((client) => client.close()); webSocketServer.close(); gpioProcess.kill(); + bluetoothSerialMonitors.forEach((monitor) => monitor.kill()); process.exit(eventId); } -}, 30_000); +}, 10_000); From 86d8a71e2372272546f766309742d3781908ff95 Mon Sep 17 00:00:00 2001 From: Raphael Arce Date: Mon, 21 Oct 2024 18:24:32 +0200 Subject: [PATCH 4/9] test: make them work again Signed-off-by: Raphael Arce --- services/rest/rest.js | 9 +-------- services/rest/rest.test.js | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/services/rest/rest.js b/services/rest/rest.js index 25fe8db..8b9e0ff 100644 --- a/services/rest/rest.js +++ b/services/rest/rest.js @@ -1,5 +1,4 @@ import http from "node:http"; -import crypto from "node:crypto"; import { handleIsApiAlive } from "./paths/default.js"; import { handleLabels } from "./paths/labels/labels.js"; import { handleShutdown } from "./paths/shutdown/shutdown.js"; @@ -10,7 +9,7 @@ import { handleRegenerateOnlyPdfs } from "./paths/regenerate-only-pdfs/regenerat const port = process.env.API_PORT; -const server = http.createServer(); +export const server = http.createServer(); server.on("request", handleRequest); @@ -20,10 +19,6 @@ server.on("request", handleRequest); * @param {ServerResponse} response */ function handleRequest(request, response) { - const requestId = crypto.randomUUID(); - console.time(`request ${requestId} time`); - response.on("close", () => console.timeEnd(`request ${requestId} time`)); - response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "OPTIONS, PUT, GET"); @@ -73,5 +68,3 @@ function handleRequest(request, response) { server.listen(port, () => console.info("listening on port:", port)); server.on("close", () => console.log("api server closed")); - -export default server; diff --git a/services/rest/rest.test.js b/services/rest/rest.test.js index d4a4bd5..4bead03 100644 --- a/services/rest/rest.test.js +++ b/services/rest/rest.test.js @@ -14,11 +14,11 @@ describe("services/rest", () => { assert.strictEqual(response.status, 204); assert.strictEqual( - response.headers.get("access-control-allow-origin"), + response.headers.get("Access-Control-Allow-Origin"), "*", ); assert.strictEqual( - response.headers.get("access-control-request-method"), + response.headers.get("Access-Control-Allow-Methods"), "OPTIONS, PUT, GET", ); }); @@ -52,19 +52,28 @@ describe("services/rest", () => { const currentLabels = getLabels(); const givenLabels = { ...currentLabels, A1: "some other label" }; - const response = await fetch("http://localhost:8000/labels", { + const promise = fetch("http://localhost:8000/labels", { method: "PUT", body: JSON.stringify({ labels: givenLabels }), }); + t.mock.method(global, "fetch"); + fetch.mock.mockImplementationOnce(async () => ({ + json: () => Promise.resolve({ message: "success" }), + })); + + const response = await promise; + const actualResponse = await response.json(); const expectedResponse = { message: "success" }; const actualLabels = getLabels(); const expectedLabels = givenLabels; + const expectedFetchUrl = `http://localhost:${process.env.API_PORT}/pregenerate`; assert.deepStrictEqual(actualResponse, expectedResponse); assert.deepStrictEqual(actualLabels, expectedLabels); + assert.strictEqual(fetch.mock.calls[0].arguments[0], expectedFetchUrl); // reset labels setLabels(currentLabels); From 9dac455c84dd2ee47e9a4195cb25bf2f259fd929 Mon Sep 17 00:00:00 2001 From: Raphael Arce Date: Mon, 21 Oct 2024 18:25:09 +0200 Subject: [PATCH 5/9] feat: disable console logs Signed-off-by: Raphael Arce --- services/socket/socket.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/socket/socket.js b/services/socket/socket.js index 8aed838..3caf9cb 100644 --- a/services/socket/socket.js +++ b/services/socket/socket.js @@ -13,7 +13,7 @@ webSocketServer.on("connection", function connection(webSocket) { webSocket.on("error", console.error); webSocket.on("message", function message(data) { - console.log("received: %s", data); + // console.log("received: %s", data); }); webSocket.send(getDicesAsJson()); @@ -22,7 +22,7 @@ webSocketServer.on("connection", function connection(webSocket) { webSocketServer.on("close", () => console.log("websocket server closed")); export function broadcast(data) { - console.log("broadcast", data); + // console.log("broadcast", data); webSocketServer.clients.forEach((client) => { if (client.readyState !== WebSocket.OPEN) { return; From 09ff2cc18439bd99f883aa782069282fffdc9f57 Mon Sep 17 00:00:00 2001 From: Raphael Arce Date: Mon, 21 Oct 2024 18:25:55 +0200 Subject: [PATCH 6/9] feat: add pregeneration when updating labels Signed-off-by: Raphael Arce --- services/rest/paths/labels/labels.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/services/rest/paths/labels/labels.js b/services/rest/paths/labels/labels.js index f61acd7..d3a1e53 100644 --- a/services/rest/paths/labels/labels.js +++ b/services/rest/paths/labels/labels.js @@ -1,5 +1,7 @@ import { getLabels, setLabels } from "../../../state/state.js"; +const port = process.env.API_PORT; + /** * Handles the labels endpoint * @param request @@ -40,4 +42,9 @@ function handlePutLabels({ body, response }) { setLabels(newLabels); response.end(JSON.stringify({ message: "success" })); + + /** + * start pregeneration in the background + */ + fetch(`http://localhost:${port}/pregenerate`).catch(console.error); } From f2395cbc2de7b91215b9e25164b9b34224b2e759 Mon Sep 17 00:00:00 2001 From: Raphael Arce Date: Mon, 21 Oct 2024 18:29:07 +0200 Subject: [PATCH 7/9] fix: automatically connect dices when receiving a value Signed-off-by: Raphael Arce --- services/state/state.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/state/state.js b/services/state/state.js index 3bcc72a..4278c34 100644 --- a/services/state/state.js +++ b/services/state/state.js @@ -81,7 +81,7 @@ export function setDiceSide(input) { const [diceId, diceSide] = diceIdAndSide; - setDices(diceId, { ...dices[diceId], side: diceSide }); + setDices(diceId, { status: "connected", side: diceSide }); } export function getDices() { From 5eff763c1f05d279422b5ece2a5f0913e76817b4 Mon Sep 17 00:00:00 2001 From: Raphael Arce Date: Mon, 21 Oct 2024 18:33:15 +0200 Subject: [PATCH 8/9] feat: add warning to save Signed-off-by: Raphael Arce --- index.css | 11 ++++++++++- index.html | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/index.css b/index.css index a7e9762..52fa0c1 100644 --- a/index.css +++ b/index.css @@ -14,7 +14,7 @@ main { } button { - margin-top: 2rem; + margin-top: 1rem; width: 200px; background-color: #121212; color: white; @@ -99,3 +99,12 @@ form { .group-c { width: 280px; } + +.warning { + border: 2px solid; + padding: 0.5rem; + background-color: #f4a203; + margin-top: 1rem; + margin-left: auto; + margin-right: auto; +} diff --git a/index.html b/index.html index 8fb4bca..2cda0b9 100644 --- a/index.html +++ b/index.html @@ -149,6 +149,8 @@

Würfel C: Medium

+ +
⚠️Warnung: Speichern verursacht Kosten ⚠️
@@ -216,6 +218,13 @@

Würfel C: Medium