mirror of
https://github.com/sergeyyarkov/modbus-logger.git
synced 2026-10-11 02:18:51 +03:00
progress
This commit is contained in:
parent
6b9b15970d
commit
ac2e27cf6a
@ -208,6 +208,7 @@ export const modbusController = {
|
||||
if (!req.query.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 = ?`, [req.query.slave_id]);
|
||||
const displayValues = await db.all(`SELECT * FROM "display_values" WHERE slave_id = ?`, [req.query.slave_id])
|
||||
|
||||
if (!device) return res.status(404).json({ error: { message: "Device not found." } });
|
||||
|
||||
@ -216,8 +217,12 @@ export const modbusController = {
|
||||
res.setHeader("Connection", "keep-alive");
|
||||
|
||||
let interval = setInterval(async () => {
|
||||
let value = 0;
|
||||
let len = 1; // 16 bits default
|
||||
let graphValue = 0;
|
||||
let len = 1; // 16 bits default
|
||||
|
||||
for (const value of displayValues) {
|
||||
value.data = Buffer.from((await modbusClient.readHoldingRegisters(value.reg_addr, 1)).buffer).readUInt16BE();
|
||||
}
|
||||
|
||||
if (device.g_display_reg_format === 32) len = 2;
|
||||
// ...64, 128, 256...
|
||||
@ -226,17 +231,20 @@ export const modbusController = {
|
||||
|
||||
switch (device.g_display_reg_format) {
|
||||
case 32:
|
||||
value = Buffer.from(result.buffer).readUInt32BE();
|
||||
graphValue = Buffer.from(result.buffer).readUInt32BE();
|
||||
break;
|
||||
case 16:
|
||||
value = Buffer.from(result.buffer).readUint16BE();
|
||||
graphValue = Buffer.from(result.buffer).readUint16BE();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
res.write("event: message\n");
|
||||
res.write(`data: ${JSON.stringify({ graph: { value, format: device.g_display_reg_format } })}\n\n`);
|
||||
res.write(`data: ${JSON.stringify({
|
||||
graph: { value: graphValue, format: device.g_display_reg_format },
|
||||
displayValues
|
||||
})}\n\n`);
|
||||
}, 1000);
|
||||
req.on("close", () => clearInterval(interval));
|
||||
} catch (error) {
|
||||
|
||||
@ -58,22 +58,22 @@ 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"
|
||||
) VALUES
|
||||
(1, "Device #1", 1, 16, "Label 1", 1),
|
||||
(2, "Device #2", 2, 32, "Label 2", 1),
|
||||
(3, "Device #3", 8, 16, "Label 3", 0),
|
||||
(1, "Device #1", 0, 16, "Label 1", 1),
|
||||
(2, "Device #2", 1, 32, "Label 2", 1),
|
||||
(3, "Device #3", 3, 16, "Label 3", 0),
|
||||
(4, "Device #4", NULL, NULL, "Label 4", 0);
|
||||
|
||||
INSERT INTO "display_values" (
|
||||
"id", "name", "slave_id", "reg_addr", "reg_format"
|
||||
) VALUES
|
||||
(1, "CV1", 1, 0, 16),
|
||||
(2, "CV2", 1, 1, 16),
|
||||
(3, "SP", 1, 2, 16),
|
||||
(4, "CV1", 2, 3, 16),
|
||||
(5, "CV2", 2, 4, 16),
|
||||
(6, "SP", 2, 5, 16),
|
||||
(7, "CV1", 3, 6, 16),
|
||||
(8, "SP", 3, 7, 16);
|
||||
(1, "CV1", 1, 10, 16),
|
||||
(2, "CV2", 1, 11, 16),
|
||||
(3, "SP", 1, 12, 16),
|
||||
(4, "CV1", 2, 13, 16),
|
||||
(5, "CV2", 2, 14, 16),
|
||||
(6, "SP", 2, 15, 16),
|
||||
(7, "CV1", 3, 16, 16),
|
||||
(8, "SP", 3, 17, 16);
|
||||
--------------------------------------------------------------------------------
|
||||
-- Down
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
@ -698,6 +698,10 @@ video {
|
||||
margin-top: 1.5rem;
|
||||
}
|
||||
|
||||
.mt-auto {
|
||||
margin-top: auto;
|
||||
}
|
||||
|
||||
.block {
|
||||
display: block;
|
||||
}
|
||||
@ -726,6 +730,10 @@ video {
|
||||
height: 5rem;
|
||||
}
|
||||
|
||||
.h-\[720px\] {
|
||||
height: 720px;
|
||||
}
|
||||
|
||||
.h-full {
|
||||
height: 100%;
|
||||
}
|
||||
@ -734,6 +742,10 @@ video {
|
||||
height: 100vh;
|
||||
}
|
||||
|
||||
.h-\[300px\] {
|
||||
height: 300px;
|
||||
}
|
||||
|
||||
.max-h-\[400px\] {
|
||||
max-height: 400px;
|
||||
}
|
||||
|
||||
@ -135,7 +135,6 @@
|
||||
<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><span x-text="selectedDevice?.id"></span>
|
||||
</div>
|
||||
<!-- Device create, delete buttons -->
|
||||
<div>
|
||||
@ -152,57 +151,90 @@
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
<!-- Error message -->
|
||||
<!-- <div>
|
||||
<span class="text-red-500" x-text="error?.message"></span>
|
||||
</div> -->
|
||||
<!-- Display Graph -->
|
||||
<div x-show="selectedDevice?.g_display_reg_addr != null" class="mt-4">
|
||||
<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>
|
||||
</div>
|
||||
<hr class="my-3" />
|
||||
</div>
|
||||
<div x-show="isLoading" class="text-center flex justify-center items-center h-[300px]">
|
||||
<div>
|
||||
<div role="status">
|
||||
<svg
|
||||
aria-hidden="true"
|
||||
class="inline w-20 h-20 mr-2 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600"
|
||||
viewBox="0 0 100 101"
|
||||
fill="none"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
>
|
||||
<path
|
||||
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
|
||||
fill="currentColor"
|
||||
/>
|
||||
<path
|
||||
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
|
||||
fill="currentFill"
|
||||
/>
|
||||
</svg>
|
||||
</div>
|
||||
<p class="mt-4">Loading...</p>
|
||||
</div>
|
||||
</div>
|
||||
<!-- Display values -->
|
||||
<h2 class="text-xl font-bold mb-3 mt-5">Display values</h2>
|
||||
<div class="mb-2">
|
||||
<div class="grid grid-cols-1 md:sm:grid-cols-2 lg:sm:grid-cols-3 gap-2">
|
||||
<template x-for="value in selectedDevice?.display_values" :key="value.id">
|
||||
<div class="border-2 p-2.5 rounded-sm">
|
||||
<div class="flex justify-between items-start">
|
||||
<h3 x-text="value.name" class="text-xl font-bold"></h3>
|
||||
<div class="flex">
|
||||
<!-- Edit display value -->
|
||||
<button>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
|
||||
<path
|
||||
fill="#eab308"
|
||||
d="M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25zM20.71 7.04a.996.996 0 0 0 0-1.41l-2.34-2.34a.996.996 0 0 0-1.41 0l-1.83 1.83l3.75 3.75l1.83-1.83z"
|
||||
/>
|
||||
</svg>
|
||||
</button>
|
||||
<!-- Remove display value -->
|
||||
<button @click="$dispatch('dialog', {
|
||||
title: 'Remove display value',
|
||||
body: `Remove display value ${value.name}?`,
|
||||
isOpen: true,
|
||||
onConfirm: async () => await removeDisplayValue(value.id)
|
||||
});">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
|
||||
<path
|
||||
fill="#ef4444"
|
||||
d="m9.4 16.5l2.6-2.6l2.6 2.6l1.4-1.4l-2.6-2.6L16 9.9l-1.4-1.4l-2.6 2.6l-2.6-2.6L8 9.9l2.6 2.6L8 15.1l1.4 1.4ZM5 21V6H4V4h5V3h6v1h5v2h-1v15H5Zm2-2h10V6H7v13ZM7 6v13V6Z"
|
||||
/>
|
||||
</svg>
|
||||
</button>
|
||||
<div x-show="!isLoading">
|
||||
<h2 class="text-xl font-bold mb-3 mt-5">Display values</h2>
|
||||
<div class="mb-2">
|
||||
<div class="grid grid-cols-1 md:sm:grid-cols-2 lg:sm:grid-cols-3 gap-2">
|
||||
<template x-for="value in selectedDevice?.display_values" :key="value.id">
|
||||
<div class="border-2 p-2.5 rounded-sm">
|
||||
<div class="flex justify-between items-start">
|
||||
<h3 x-text="value.name" class="text-xl font-bold"></h3>
|
||||
<div class="flex">
|
||||
<!-- Edit display value -->
|
||||
<button>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
|
||||
<path
|
||||
fill="#eab308"
|
||||
d="M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25zM20.71 7.04a.996.996 0 0 0 0-1.41l-2.34-2.34a.996.996 0 0 0-1.41 0l-1.83 1.83l3.75 3.75l1.83-1.83z"
|
||||
/>
|
||||
</svg>
|
||||
</button>
|
||||
<!-- Remove display value -->
|
||||
<button @click="$dispatch('dialog', {
|
||||
title: 'Remove display value',
|
||||
body: `Remove display value ${value.name}?`,
|
||||
isOpen: true,
|
||||
onConfirm: async () => await removeDisplayValue(value.id)
|
||||
});">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
|
||||
<path
|
||||
fill="#ef4444"
|
||||
d="m9.4 16.5l2.6-2.6l2.6 2.6l1.4-1.4l-2.6-2.6L16 9.9l-1.4-1.4l-2.6 2.6l-2.6-2.6L8 9.9l2.6 2.6L8 15.1l1.4 1.4ZM5 21V6H4V4h5V3h6v1h5v2h-1v15H5Zm2-2h10V6H7v13ZM7 6v13V6Z"
|
||||
/>
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<hr />
|
||||
<div class="flex flex-col">
|
||||
<span>Value: <span x-text="value?.data"></span></span>
|
||||
<span>Reg Addr: <span x-text="value.reg_addr"></span></span>
|
||||
<span>Format: <span x-text="value.reg_format"></span></span>
|
||||
</div>
|
||||
</div>
|
||||
<hr />
|
||||
<div class="flex flex-col">
|
||||
<span>Value: ---</span>
|
||||
<span>Reg Addr: <span x-text="value.reg_addr"></span></span>
|
||||
<span>Format: <span x-text="value.reg_format"></span></span>
|
||||
</div>
|
||||
</template>
|
||||
<div
|
||||
class="flex justify-center min-h-[140px] items-center border-4 text-center border-dashed p-2.5 rounded-sm cursor-pointer hover:bg-gray-50"
|
||||
>
|
||||
<span class="text-5xl text-gray-400 font-bold select-none">+</span>
|
||||
</div>
|
||||
</template>
|
||||
<div
|
||||
class="flex justify-center min-h-[140px] items-center border-4 text-center border-dashed p-2.5 rounded-sm cursor-pointer hover:bg-gray-50"
|
||||
>
|
||||
<span class="text-5xl text-gray-400 font-bold select-none">+</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -5,7 +5,7 @@ import * as utils from "./utils/index.js";
|
||||
|
||||
var pages = ["loading", "monitoring", "configuration", "404"];
|
||||
var dataStreamSource;
|
||||
var graphData = [[0, 0]];
|
||||
var graphData = [[0,0]];
|
||||
var g;
|
||||
|
||||
document.addEventListener("alpine:init", async () => {
|
||||
@ -106,14 +106,17 @@ document.addEventListener("alpine:init", async () => {
|
||||
Alpine.data("monitoringPage", () => ({
|
||||
selectedDevice: null,
|
||||
devices: [],
|
||||
isLoading: false,
|
||||
error: null,
|
||||
resetGraph() {
|
||||
if (g instanceof Dygraph) {
|
||||
g.destroy();
|
||||
}
|
||||
},
|
||||
async init() {
|
||||
try {
|
||||
this.$store.app.currentPage = "loading";
|
||||
this.devices = await api.get("/modbus/devices");
|
||||
// window.intervalId = setInterval(() => {
|
||||
// graphData.push([new Date(), Math.random()]);
|
||||
// g.updateOptions({ file: graphData });
|
||||
// }, 500);
|
||||
} catch (error) {
|
||||
this.$store.app.error = error;
|
||||
} finally {
|
||||
@ -121,6 +124,14 @@ document.addEventListener("alpine:init", async () => {
|
||||
}
|
||||
},
|
||||
selectDevice(device) {
|
||||
if (device.id === this.selectedDevice?.id) return;
|
||||
|
||||
this.selectedDevice = device;
|
||||
this.resetGraph();
|
||||
|
||||
// if (device.g_display_reg_addr !== null)
|
||||
this.isLoading = true;
|
||||
|
||||
dataStreamSource?.close();
|
||||
dataStreamSource = new EventSource(`/api/modbus/data_stream?slave_id=${device.id}`);
|
||||
g = new Dygraph(document.getElementById("div_g"), graphData, {
|
||||
@ -130,15 +141,19 @@ document.addEventListener("alpine:init", async () => {
|
||||
// valueRange: [0, 1000],
|
||||
// labels: ["Time", "Random"],
|
||||
});
|
||||
g.resize(720, 300);
|
||||
dataStreamSource.onerror = (e) => {
|
||||
dataStreamSource.close();
|
||||
}
|
||||
dataStreamSource.onmessage = (e) => {
|
||||
const data = JSON.parse(e.data);
|
||||
console.log(data);
|
||||
graphData.push([new Date(), data.graph.value]);
|
||||
g.updateOptions({ file: graphData });
|
||||
data.displayValues.forEach((v, i) => this.selectedDevice.display_values[i] = v);
|
||||
this.isLoading = false;
|
||||
};
|
||||
if (device.id === this.selectedDevice?.id) return;
|
||||
graphData = []; // todo: reset graph
|
||||
this.selectedDevice = device;
|
||||
graphData = [];
|
||||
},
|
||||
async removeDevice(id) {
|
||||
try {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user