diff --git a/controllers/modbus.controller.js b/controllers/modbus.controller.js index 2fb1df8..c0bfb87 100644 --- a/controllers/modbus.controller.js +++ b/controllers/modbus.controller.js @@ -1,6 +1,5 @@ import db from "#root/config/database.config.js"; import modbusClient from "#root/config/modbus-client.config.js"; -import * as utils from '#root/utils/index.js' import { appService, modbusService } from "#root/services/index.js"; export const modbusController = { @@ -12,30 +11,10 @@ export const modbusController = { */ async connect(req, res, next) { try { - const config = await appService.getConfig(db); - if (!config) return res.status(400).json({ message: "Application is not configured" }); - - if (modbusClient.isOpen) { - modbusClient.close(undefined); - modbusClient.destroy(undefined); - } - - if (config.mb_connection_type === "TCP") { - await modbusClient.connectTCP(config.mb_tcp_ip, { port: config.mb_tcp_port }); - return res.status(200).json({ message: "Connected." }); - } - - if (config.mb_connection_type === "RTU") { - await modbusClient.connectRTU(config.mb_rtu_path, { - baudRate: config.mb_rtu_baud, - dataBits: config.mb_rtu_data_bits, - parity: config.mb_rtu_parity, - stopBits: config.mb_rtu_stop_bits, - }); - return res.status(200).json({ message: "Connected." }); - } - - return res.status(400).json({ message: "Connection type is invalid." }); + const appConfig = await appService.getConfig(); + if (!appConfig) return res.status(400).json({ message: "Application is not configured" }); + await modbusService.connect(appConfig) + return res.status(200).json({ message: "Connected." }); } catch (error) { next(error); } @@ -66,25 +45,8 @@ 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_display_reg_type, g_y_label, is_logging, - CASE - WHEN COUNT(dv.id) = 0 THEN '[]' - ELSE '[' || GROUP_CONCAT( - JSON_OBJECT( - 'id', dv.id, - 'name', dv.name, - 'reg_addr', dv.reg_addr, - 'reg_format', dv.reg_format, - 'reg_type', dv.reg_type - ), ', ' - ) || ']' END AS display_values - FROM modbus_slaves AS ms - LEFT JOIN display_values AS dv ON dv.slave_id = ms.id - GROUP BY ms.id, ms.name; - `); - slaves.forEach((s) => (s.display_values = JSON.parse(s.display_values))); - return res.status(200).json(slaves); + const devices = await modbusService.getDevices(); + return res.status(200).json(devices); } catch (error) { next(error); } @@ -218,6 +180,8 @@ export const modbusController = { if (!device) return res.status(404).json({ error: { message: "Device not found." } }); + // modbusService.createPollInterval(req, res, device).poll(1000); + res.setHeader("Cache-Control", "no-cache"); res.setHeader("Content-Type", "text/event-stream"); res.setHeader("Connection", "keep-alive"); diff --git a/data.db b/data.db index 73f3c09..2b1489b 100644 Binary files a/data.db and b/data.db differ diff --git a/public/css/dist.css b/public/css/dist.css index d8092ab..d242996 100644 --- a/public/css/dist.css +++ b/public/css/dist.css @@ -774,6 +774,14 @@ video { max-width: 64rem; } +.max-w-xl { + max-width: 36rem; +} + +.max-w-md { + max-width: 28rem; +} + .flex-1 { flex: 1 1 0%; } @@ -956,6 +964,10 @@ video { padding: 20rem; } +.p-5 { + padding: 1.25rem; +} + .px-2 { padding-left: 0.5rem; padding-right: 0.5rem; @@ -971,6 +983,21 @@ 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 8bae9e4..d9b2150 100644 --- a/public/index.html +++ b/public/index.html @@ -264,7 +264,7 @@