This commit is contained in:
Sergey Yarkov 2023-11-01 16:08:20 +03:00
parent 53056a3362
commit 82d8976ee3
8 changed files with 32 additions and 27 deletions

View File

@ -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) {

BIN
data.db

Binary file not shown.

1
index.d.ts vendored
View File

@ -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[]

View File

@ -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"

View File

@ -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;
}

View File

@ -85,9 +85,16 @@
<label :for="$id('g_display_reg_addr')"><span class="text-red-500">*</span>Register address:</label>
<input x-model="data.g_display_reg_addr" :id="$id('g_display_reg_addr')" name="g_display_reg_addr" placeholder="1" type="number" min="0" max="65534" :required="graphView === 'new-value'" />
</div>
<div>
<label :for="$id('g_display_reg_type')"><span class="text-red-500">*</span>Registers type:</label>
<select x-model="data.g_display_reg_type" :id="$id('g_display_reg_type')" name="g_display_reg_type" :required="graphView === 'new-value'">
<option value="IR">Input Registers</option>
<option value="HR">Holding Registers</option>
</select>
</div>
<div>
<label :for="$id('g_display_reg_format')"><span class="text-red-500">*</span>Format (type):</label>
<select x-model="data.g_display_reg_format" :id="$id('g_display_reg_format')" name="g_display_reg_format">
<select x-model="data.g_display_reg_format" :id="$id('g_display_reg_format')" name="g_display_reg_format" :required="graphView === 'new-value'">
<option value="UI16">Unsigned Int 16</option>
<option value="I16">Signed Int 16</option>
<option value="UI32">Unsigned Int 32</option>
@ -138,6 +145,7 @@
<div class="pt-5 border-b flex flex-wrap justify-between items-center pr-6">
<div class="mb-2">
<h2 x-text="selectedDevice?.name" class="font-bold text-3xl my-2 mr-2"></h2>
<span>ID: <span x-text="selectedDevice?.id"></span></span>
</div>
<!-- Device create, delete buttons -->
<div>
@ -162,8 +170,9 @@
<div x-show="!isLoading && selectedDevice?.g_display_reg_addr !== null" class="mt-4">
<div id="div_g" style="width: 720px; height: 300px"></div>
<div class="flex space-x-2 mt-2">
<span>Register: <span x-text="selectedDevice?.g_display_reg_addr"></span></span>
<span>Format: <span x-text="selectedDevice?.g_display_reg_format"></span></span>
<span class="italic text-sm">Register: <span x-text="selectedDevice?.g_display_reg_addr"></span>&comma;</span>
<span class="italic text-sm">Format: <span x-text="selectedDevice?.g_display_reg_format"></span>&comma;</span>
<span class="italic text-sm">Register type: <span x-text="selectedDevice?.g_display_reg_type"></span></span>
</div>
<hr class="my-3" />
</div>

View File

@ -68,6 +68,7 @@ document.addEventListener("alpine:init", async () => {
name: "",
g_display_reg_addr: null,
g_display_reg_format: "UI16",
g_display_reg_type: 'IR',
g_y_label: "",
is_logging: false,
},
@ -76,6 +77,7 @@ document.addEventListener("alpine:init", async () => {
this.data.name = "";
this.data.g_display_reg_addr = null;
this.data.g_display_reg_format = "UI16";
this.g_display_reg_type = 'IR';
this.data.g_y_label = "";
this.data.is_logging = false;
},

View File

@ -5,6 +5,7 @@ export const modbusDeviceSchema = object({
name: string().required(),
g_display_reg_addr: number().min(0).max(65534).nullable(),
g_display_reg_format: string().oneOf(["UI16", "I16", "UI32", "I32", "FP32"]).nullable(),
g_display_reg_type: string().oneOf(["HR", "IR"]).nullable(),
g_y_label: string().max(40).nullable(),
is_logging: boolean().required()
});