This commit is contained in:
Sergey Yarkov 2023-11-09 15:00:23 +03:00
parent da4cd16ade
commit 80ba7b0887
5 changed files with 23 additions and 35 deletions

View File

@ -102,25 +102,12 @@ export const modbusController = {
await db.run(`UPDATE "modbus_slaves" SET await db.run(`UPDATE "modbus_slaves" SET
id = ?, id = ?,
name = ?, name = ?,
g_display_reg_addr = NULL,
g_display_reg_type = NULL,
g_display_reg_format = NULL,
g_y_label = NULL
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_addr = ?,
g_display_reg_type = ?, g_display_reg_type = ?,
g_display_reg_format = ?, g_display_reg_format = ?,
g_y_label = ? WHERE id = ?`, g_y_label = ?
[g_display_reg_addr, g_display_reg_type, g_display_reg_format, g_y_label, id]); WHERE 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.' }) return res.status(200).json({ message: 'Updated.' })
} catch (error) { } catch (error) {
@ -145,6 +132,7 @@ export const modbusController = {
} }
}, },
/** /**
* Create display value for modbus slave device * Create display value for modbus slave device
* @param {import('express').Request} req * @param {import('express').Request} req

BIN
data.db

Binary file not shown.

View File

@ -219,10 +219,9 @@
actionType: 'EDIT' actionType: 'EDIT'
})" })"
href="#">Edit</a> href="#">Edit</a>
<a <a @click="$dispatch('dialog', {
@click="$dispatch('dialog', {
title: 'Remove device', title: 'Remove device',
body: `Remove ${selectedDevice.name}? All logs will be cleared.`, body: `Remove ${selectedDevice.name}?`,
isOpen: true, isOpen: true,
onConfirm: async () => await removeDevice(selectedDevice.id) onConfirm: async () => await removeDevice(selectedDevice.id)
});" });"

View File

@ -67,7 +67,7 @@ document.addEventListener("alpine:init", async () => {
oldId: null, oldId: null,
id: null, id: null,
name: "", name: "",
g_display_reg_addr: 0, g_display_reg_addr: null,
g_display_reg_format: "UI16", g_display_reg_format: "UI16",
g_display_reg_type: "IR", g_display_reg_type: "IR",
g_y_label: "", g_y_label: "",
@ -91,8 +91,7 @@ document.addEventListener("alpine:init", async () => {
this.isLoading = true; this.isLoading = true;
this.error = null; this.error = null;
await api.post("/modbus/create_device", utils.dellNullableKeys({ ...this.data })); await api.post("/modbus/create_device", utils.dellNullableKeys({ ...this.data }));
this.$dispatch("add-device", { ...this.data }); // add device to state this.$dispatch("add-device", { ...this.data, display_values: [] }); // add device to state
this.resetDataFields();
this.close(); this.close();
cb && cb(); cb && cb();
} catch (error) { } catch (error) {
@ -106,11 +105,11 @@ document.addEventListener("alpine:init", async () => {
try { try {
this.isLoading = true; this.isLoading = true;
this.error = null; this.error = null;
if (this.graphView === 'none') { if (this.graphView !== 'new-value') {
this.data.g_display_reg_addr = null; this.data.g_display_reg_addr = null;
this.data.g_display_reg_format = null; this.data.g_display_reg_format = null;
this.data.g_display_reg_type = null;
this.data.g_y_label = 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 })); await api.post('/modbus/update_device', utils.dellNullableKeys({ ...this.data, id: this.data.oldId, newId: this.data.id }));
window.location.reload(); window.location.reload();
@ -147,11 +146,13 @@ document.addEventListener("alpine:init", async () => {
this.data.oldId = detail.device.id; this.data.oldId = detail.device.id;
this.data.id = detail.device.id; this.data.id = detail.device.id;
this.data.name = detail.device.name; this.data.name = detail.device.name;
this.data.g_display_reg_addr = detail.device.g_display_reg_addr || 0; if (detail.device.g_display_reg_addr !== null) {
this.data.g_display_reg_format = detail.device.g_display_reg_format || 'UI16'; this.graphView = 'new-value'
this.data.g_display_reg_type = detail.device.g_display_reg_type || 'IR'; this.data.g_display_reg_addr = detail.device.g_display_reg_addr;
this.data.g_y_label = detail.device.g_y_label || ''; this.data.g_display_reg_format = detail.device.g_display_reg_format;
if (detail.device.g_display_reg_addr !== null) this.graphView = 'new-value' 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; this.isOpen = true;
}, },
@ -380,6 +381,6 @@ document.addEventListener("alpine:init", async () => {
this.isOpen = detail.isOpen; this.isOpen = detail.isOpen;
this.onConfirm = detail.onConfirm; this.onConfirm = detail.onConfirm;
this.labels = { title: detail.title, body: detail.body }; this.labels = { title: detail.title, body: detail.body };
}, }
})); }));
}); });

View File

@ -1,4 +1,4 @@
import { object, number, boolean, string } from 'yup'; import { object, number, string } from 'yup';
export const modbusDeviceSchema = object({ export const modbusDeviceSchema = object({
id: number().required(), id: number().required(),
@ -12,4 +12,4 @@ export const modbusDeviceSchema = object({
export const removeModbusDeviceSchema = modbusDeviceSchema.pick(['id']); export const removeModbusDeviceSchema = modbusDeviceSchema.pick(['id']);
export const editModbusDeviceSchema = modbusDeviceSchema.shape({ export const editModbusDeviceSchema = modbusDeviceSchema.shape({
newId: number().required() newId: number().required()
}) });