From d6f7444266661aa565cfab5fdeaa2f08841a7607 Mon Sep 17 00:00:00 2001 From: sergeyyarkov Date: Thu, 2 Nov 2023 21:46:50 +0300 Subject: [PATCH] refactor --- controllers/modbus.controller.js | 56 +++++++------ data.db | Bin 20480 -> 20480 bytes public/css/dist.css | 35 ++------ public/index.html | 137 +++++++++++++++++++++++++------ public/js/app.js | 29 +++---- services/modbus.service.js | 135 ++++++++++++++---------------- 6 files changed, 231 insertions(+), 161 deletions(-) diff --git a/controllers/modbus.controller.js b/controllers/modbus.controller.js index c0bfb87..7d69ae0 100644 --- a/controllers/modbus.controller.js +++ b/controllers/modbus.controller.js @@ -13,7 +13,7 @@ export const modbusController = { try { const appConfig = await appService.getConfig(); if (!appConfig) return res.status(400).json({ message: "Application is not configured" }); - await modbusService.connect(appConfig) + await modbusService.connect(appConfig); return res.status(200).json({ message: "Connected." }); } catch (error) { next(error); @@ -60,7 +60,8 @@ export const modbusController = { */ async createDevice(req, res, next) { try { - const { id, name, g_display_reg_addr, g_display_reg_format, g_display_reg_type, 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", @@ -168,37 +169,46 @@ export const modbusController = { */ async streamData(req, res, next) { try { - if (!modbusClient.isOpen) return res.status(503).json({ error: { message: "Modbus connection closed." } }); - const { slave_id } = req.query; - - if (!slave_id) - return res.status(400).json({ error: { message: "'slave_id' parameter is required." } }); - - const device = await db.get(`SELECT * FROM "modbus_slaves" WHERE id = ?`, [slave_id]); - - if (!device) - return res.status(404).json({ error: { message: "Device not found." } }); - // modbusService.createPollInterval(req, res, device).poll(1000); + if (!modbusClient.isOpen) return res.status(503).json({ error: { message: "Modbus connection closed." } }); + if (!slave_id) return res.status(400).json({ error: { message: "'slave_id' parameter is required." } }); + + const device = await db.get(`SELECT * FROM "modbus_slaves" WHERE id = ?`, [slave_id]); + if (!device) return res.status(404).json({ error: { message: "Device not found." } }); res.setHeader("Cache-Control", "no-cache"); res.setHeader("Content-Type", "text/event-stream"); res.setHeader("Connection", "keep-alive"); - const streamInterval = setInterval(async () => { - try { - device.display_values = await db.all(`SELECT * FROM "display_values" WHERE slave_id = ?`, [slave_id]) - const data = await modbusService.readDataFromDevice(device); + const intervalId = modbusService.startDevicePollInterval( + device, + (data, error) => { + if (error) { + next(error); + return; + } res.write("event: message\n"); res.write(`data: ${JSON.stringify(data)}\n\n`); - } catch (error) { - clearInterval(streamInterval); - next(error); - } - }, 1000); + }, + 1000, + ); - req.on("close", () => clearInterval(streamInterval)); + req.on("close", () => clearInterval(intervalId)); + + // const streamInterval = setInterval(async () => { + // try { + // device.display_values = await db.all(`SELECT * FROM "display_values" WHERE slave_id = ?`, [slave_id]) + // const data = await modbusService.readDataFromDevice(device); + // res.write("event: message\n"); + // res.write(`data: ${JSON.stringify(data)}\n\n`); + // } catch (error) { + // clearInterval(streamInterval); + // next(error); + // } + // }, 1000); + + // req.on("close", () => clearInterval(streamInterval)); } catch (error) { next(error); } diff --git a/data.db b/data.db index 2b1489b6dca84ce72b984f63209d2c44a7dbd800..4b0af30dc8b13cb45d3c805575068659ca8c77c1 100644 GIT binary patch delta 114 zcmZozz}T>Wae}nqRt5$JRv?A~MyZK9#*AAxCfsLdWZCS%;mN|tyt#{~N|=q2{{{p9 zjm?4vC-^4@uxY3Wae}nq0tN;KRv?A~Mv;j+#*7O#CfsLdWZvw+;mN|tvbl?=N_a7gfdCsL z|4jz|o0|m{j`L3p;80g(;ARk(XA))-W#nWEaSkvvGS@QzLPN%H#VN(A4SD%_sT|BN I7C8t205lsJp#T5? diff --git a/public/css/dist.css b/public/css/dist.css index d242996..cd4f58e 100644 --- a/public/css/dist.css +++ b/public/css/dist.css @@ -766,18 +766,10 @@ video { min-width: 256px; } -.max-w-2xl { - max-width: 42rem; -} - .max-w-5xl { max-width: 64rem; } -.max-w-xl { - max-width: 36rem; -} - .max-w-md { max-width: 28rem; } @@ -854,6 +846,10 @@ video { justify-content: space-between; } +.justify-evenly { + justify-content: space-evenly; +} + .gap-2 { gap: 0.5rem; } @@ -956,6 +952,10 @@ video { padding: 10rem; } +.p-5 { + padding: 1.25rem; +} + .p-60 { padding: 15rem; } @@ -964,10 +964,6 @@ video { padding: 20rem; } -.p-5 { - padding: 1.25rem; -} - .px-2 { padding-left: 0.5rem; padding-right: 0.5rem; @@ -983,21 +979,6 @@ video { padding-bottom: 0.75rem; } -.px-6 { - padding-left: 1.5rem; - padding-right: 1.5rem; -} - -.py-4 { - padding-top: 1rem; - padding-bottom: 1rem; -} - -.px-5 { - padding-left: 1.25rem; - padding-right: 1.25rem; -} - .pl-0 { padding-left: 0px; } diff --git a/public/index.html b/public/index.html index d9b2150..cd81eac 100644 --- a/public/index.html +++ b/public/index.html @@ -28,12 +28,16 @@ No devices added. -
+
@@ -45,7 +49,8 @@ @open-create-device-modal.window="open()" @keyup.escape.window="close()" class="backdrop z-50" - x-show="isOpen"> + x-show="isOpen" + >