From 80ba7b0887d79c67c25c667d7cf9c3d4256d9466 Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Thu, 9 Nov 2023 15:00:23 +0300 Subject: [PATCH] refactor --- controllers/modbus.controller.js | 26 +++++++------------------- data.db | Bin 20480 -> 20480 bytes public/index.html | 5 ++--- public/js/app.js | 23 ++++++++++++----------- validators/modbus-device.schema.js | 4 ++-- 5 files changed, 23 insertions(+), 35 deletions(-) diff --git a/controllers/modbus.controller.js b/controllers/modbus.controller.js index 712741b..8dbe8b8 100644 --- a/controllers/modbus.controller.js +++ b/controllers/modbus.controller.js @@ -102,26 +102,13 @@ export const modbusController = { await db.run(`UPDATE "modbus_slaves" SET id = ?, name = ?, - g_display_reg_addr = NULL, - g_display_reg_type = NULL, - g_display_reg_format = NULL, - g_y_label = NULL + g_display_reg_addr = ?, + g_display_reg_type = ?, + g_display_reg_format = ?, + g_y_label = ? WHERE id = ? `, - [newId, name, id]); - - if (g_display_reg_addr !== null && - g_display_reg_type !== null && - g_display_reg_format !== null && - g_y_label !== null - ) { - await db.run(`UPDATE "modbus_slaves" SET - g_display_reg_addr = ?, - g_display_reg_type = ?, - g_display_reg_format = ?, - g_y_label = ? WHERE id = ?`, - [g_display_reg_addr, g_display_reg_type, g_display_reg_format, g_y_label, id]); - } - + [newId, name, g_display_reg_addr, g_display_reg_type, g_display_reg_format, g_y_label, id]); + return res.status(200).json({ message: 'Updated.' }) } catch (error) { next(error); @@ -145,6 +132,7 @@ export const modbusController = { } }, + /** * Create display value for modbus slave device * @param {import('express').Request} req diff --git a/data.db b/data.db index c0f7498781fd668a576e19a9f6386cff74904a7c..e7d0fa113d7c535def9c3a949049a8802f82d474 100644 GIT binary patch delta 278 zcmZozz}T>Wae@>RtHneaCm^{oVUav*0fP{q>tuEXInIrIbNO8PqWEGr8!CwN)eALp zvonY?atMlsIr0meprGZ{n}grQ2IDuo(3*nwg| zmBlH=to$yXPX7Kr{9z0%f{k1d31N@~muHZtv5`g_0PAF5M~e*pQ{# delta 232 zcmZozz}T>Wae@>R%f5*+PC#;F!XkOrnGAe>ie}(@F&~khJ$)W!F zHtNC*f}*@YYkCaDqq%AV^LTV`XQM1v-)6CABOwIaR?JqBaBo Diy1gn diff --git a/public/index.html b/public/index.html index 357d296..32f59cf 100644 --- a/public/index.html +++ b/public/index.html @@ -219,10 +219,9 @@ actionType: 'EDIT' })" href="#">Edit - await removeDevice(selectedDevice.id) });" diff --git a/public/js/app.js b/public/js/app.js index fc106d4..0fa8e70 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -67,7 +67,7 @@ document.addEventListener("alpine:init", async () => { oldId: null, id: null, name: "", - g_display_reg_addr: 0, + g_display_reg_addr: null, g_display_reg_format: "UI16", g_display_reg_type: "IR", g_y_label: "", @@ -91,8 +91,7 @@ document.addEventListener("alpine:init", async () => { this.isLoading = true; this.error = null; await api.post("/modbus/create_device", utils.dellNullableKeys({ ...this.data })); - this.$dispatch("add-device", { ...this.data }); // add device to state - this.resetDataFields(); + this.$dispatch("add-device", { ...this.data, display_values: [] }); // add device to state this.close(); cb && cb(); } catch (error) { @@ -106,11 +105,11 @@ document.addEventListener("alpine:init", async () => { try { this.isLoading = true; this.error = null; - if (this.graphView === 'none') { + if (this.graphView !== 'new-value') { this.data.g_display_reg_addr = null; this.data.g_display_reg_format = null; - this.data.g_display_reg_type = null; this.data.g_y_label = null; + this.data.g_display_reg_type = null; } await api.post('/modbus/update_device', utils.dellNullableKeys({ ...this.data, id: this.data.oldId, newId: this.data.id })); window.location.reload(); @@ -147,11 +146,13 @@ document.addEventListener("alpine:init", async () => { this.data.oldId = detail.device.id; this.data.id = detail.device.id; this.data.name = detail.device.name; - this.data.g_display_reg_addr = detail.device.g_display_reg_addr || 0; - this.data.g_display_reg_format = detail.device.g_display_reg_format || 'UI16'; - this.data.g_display_reg_type = detail.device.g_display_reg_type || 'IR'; - this.data.g_y_label = detail.device.g_y_label || ''; - if (detail.device.g_display_reg_addr !== null) this.graphView = 'new-value' + if (detail.device.g_display_reg_addr !== null) { + this.graphView = 'new-value' + this.data.g_display_reg_addr = detail.device.g_display_reg_addr; + this.data.g_display_reg_format = detail.device.g_display_reg_format; + this.data.g_display_reg_type = detail.device.g_display_reg_type; + this.data.g_y_label = detail.device.g_y_label; + } } this.isOpen = true; }, @@ -380,6 +381,6 @@ document.addEventListener("alpine:init", async () => { this.isOpen = detail.isOpen; this.onConfirm = detail.onConfirm; this.labels = { title: detail.title, body: detail.body }; - }, + } })); }); diff --git a/validators/modbus-device.schema.js b/validators/modbus-device.schema.js index 0b4ce2e..c128937 100644 --- a/validators/modbus-device.schema.js +++ b/validators/modbus-device.schema.js @@ -1,4 +1,4 @@ -import { object, number, boolean, string } from 'yup'; +import { object, number, string } from 'yup'; export const modbusDeviceSchema = object({ id: number().required(), @@ -12,4 +12,4 @@ export const modbusDeviceSchema = object({ export const removeModbusDeviceSchema = modbusDeviceSchema.pick(['id']); export const editModbusDeviceSchema = modbusDeviceSchema.shape({ newId: number().required() -}) \ No newline at end of file +}); \ No newline at end of file