diff --git a/controllers/modbus.controller.js b/controllers/modbus.controller.js index c083bcb..9f34f9b 100644 --- a/controllers/modbus.controller.js +++ b/controllers/modbus.controller.js @@ -67,7 +67,7 @@ export const modbusController = { async list(req, res, next) { try { const slaves = await db.all(` - SELECT ms.id AS id, ms.name AS name, g_display_reg_addr, g_display_reg_format, g_y_label, is_logging, + SELECT ms.id AS id, ms.name AS name, g_display_reg_addr, g_display_reg_format, g_display_reg_type, g_y_label, is_logging, CASE WHEN COUNT(dv.id) = 0 THEN '[]' ELSE '[' || GROUP_CONCAT( @@ -97,17 +97,18 @@ export const modbusController = { */ async createDevice(req, res, next) { try { - const { id, name, g_display_reg_addr, g_display_reg_format, g_y_label, is_logging } = req.body; + const { id, name, g_display_reg_addr, g_display_reg_format, g_display_reg_type, g_y_label, is_logging } = req.body; await db.run( `INSERT INTO "modbus_slaves" ( "id", "name", "g_display_reg_addr", - "g_display_reg_format", + "g_display_reg_format", + "g_display_reg_type", "g_y_label", "is_logging") - VALUES (?, ?, ?, ?, ?, ?)`, - [id, name, g_display_reg_addr, g_display_reg_format, g_y_label, is_logging], + VALUES (?, ?, ?, ?, ?, ?, ?)`, + [id, name, g_display_reg_addr, g_display_reg_format, g_display_reg_type, g_y_label, is_logging], ); return res.status(200).json({ message: "Device created." }); } catch (error) { @@ -216,15 +217,14 @@ export const modbusController = { if (!device) return res.status(404).json({ error: { message: "Device not found." } }); - device.display_values = await db.all(`SELECT * FROM "display_values" WHERE slave_id = ?`, [slave_id]) - res.setHeader("Cache-Control", "no-cache"); res.setHeader("Content-Type", "text/event-stream"); res.setHeader("Connection", "keep-alive"); const streamInterval = setInterval(async () => { try { - const data = await modbusService.readDataFromDevice(device, 'HR'); + device.display_values = await db.all(`SELECT * FROM "display_values" WHERE slave_id = ?`, [slave_id]) + const data = await modbusService.readDataFromDevice(device, device.g_display_reg_type); res.write("event: message\n"); res.write(`data: ${JSON.stringify(data)}\n\n`); } catch (error) { diff --git a/data.db b/data.db index 4549151..436c841 100644 Binary files a/data.db and b/data.db differ diff --git a/index.d.ts b/index.d.ts index 93322e8..5e81d19 100644 --- a/index.d.ts +++ b/index.d.ts @@ -26,6 +26,7 @@ export type ModbusDevice = { name: string; g_display_reg_addr: number | null; g_display_reg_format: NumberType | null; + g_display_reg_type: "HR" | "IR"; g_y_label: string | null; is_logging: boolean; display_values?: DisplayValue[] diff --git a/migrations/001-init.sql b/migrations/001-init.sql index 6c843b6..1cc312b 100644 --- a/migrations/001-init.sql +++ b/migrations/001-init.sql @@ -22,7 +22,7 @@ CREATE TABLE IF NOT EXISTS "modbus_slaves" ( "id" INTEGER CHECK (id >= 1 AND id <= 255), "name" VARCHAR(256) NOT NULL, "g_display_reg_addr" INTEGER CHECK(g_display_reg_addr >= 0 AND g_display_reg_addr <= 65534) DEFAULT NULL, - "g_display_reg_type" VARCHAR(3) CHECK(g_display_reg_type, IN("HR", "IR")) DEFAULT "HR", + "g_display_reg_type" VARCHAR(3) CHECK(g_display_reg_type IN("HR", "IR")) DEFAULT "HR", "g_display_reg_format" VARCHAR(5) CHECK(g_display_reg_format IN("UI16", "I16", "UI32", "I32", "FP32")) DEFAULT "UI16", "g_y_label" VARCHAR(40) DEFAULT "Label", "is_logging" BOOLEAN NOT NULL DEFAULT FALSE, @@ -55,12 +55,12 @@ CREATE TABLE IF NOT EXISTS "display_values" ( -- ) INSERT INTO "modbus_slaves" ( - "id", "name", "g_display_reg_addr", "g_display_reg_format", "g_y_label", "is_logging" + "id", "name", "g_display_reg_addr", "g_display_reg_type", "g_display_reg_format", "g_y_label", "is_logging" ) VALUES - (1, "Device #1", 0, "UI16", "Label 1", 1), - (2, "Device #2", 1, "UI32", "Label 2", 1), - (3, "Device #3", 3, "UI16", "Label 3", 0), - (4, "Device #4", NULL, NULL, "Label 4", 0); + (1, "Device #1", 0, "IR", "UI16", "Label 1", 1), + (2, "Device #2", 1, "IR", "UI32", "Label 2", 1), + (3, "Device #3", 3, "IR", "UI16", "Label 3", 0), + (4, "Device #4", NULL, NULL, NULL, "Label 4", 0); INSERT INTO "display_values" ( "id", "name", "slave_id", "reg_addr", "reg_format" diff --git a/public/css/dist.css b/public/css/dist.css index ac86915..d8092ab 100644 --- a/public/css/dist.css +++ b/public/css/dist.css @@ -698,10 +698,6 @@ video { margin-top: 1.5rem; } -.mt-auto { - margin-top: auto; -} - .block { display: block; } @@ -730,8 +726,8 @@ video { height: 5rem; } -.h-\[720px\] { - height: 720px; +.h-\[300px\] { + height: 300px; } .h-full { @@ -742,10 +738,6 @@ video { height: 100vh; } -.h-\[300px\] { - height: 300px; -} - .max-h-\[400px\] { max-height: 400px; } diff --git a/public/index.html b/public/index.html index de8fab7..20e279d 100644 --- a/public/index.html +++ b/public/index.html @@ -85,9 +85,16 @@ +