This commit is contained in:
Sergey Yarkov 2023-10-31 16:27:41 +03:00
parent 6b9b15970d
commit ac2e27cf6a
6 changed files with 135 additions and 68 deletions

View File

@ -208,6 +208,7 @@ export const modbusController = {
if (!req.query.slave_id) return res.status(400).json({ error: { message: "'slave_id' parameter is required." } }); 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 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." } }); if (!device) return res.status(404).json({ error: { message: "Device not found." } });
@ -216,9 +217,13 @@ export const modbusController = {
res.setHeader("Connection", "keep-alive"); res.setHeader("Connection", "keep-alive");
let interval = setInterval(async () => { let interval = setInterval(async () => {
let value = 0; let graphValue = 0;
let len = 1; // 16 bits default 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; if (device.g_display_reg_format === 32) len = 2;
// ...64, 128, 256... // ...64, 128, 256...
@ -226,17 +231,20 @@ export const modbusController = {
switch (device.g_display_reg_format) { switch (device.g_display_reg_format) {
case 32: case 32:
value = Buffer.from(result.buffer).readUInt32BE(); graphValue = Buffer.from(result.buffer).readUInt32BE();
break; break;
case 16: case 16:
value = Buffer.from(result.buffer).readUint16BE(); graphValue = Buffer.from(result.buffer).readUint16BE();
break; break;
default: default:
break; break;
} }
res.write("event: message\n"); 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); }, 1000);
req.on("close", () => clearInterval(interval)); req.on("close", () => clearInterval(interval));
} catch (error) { } catch (error) {

BIN
data.db

Binary file not shown.

View File

@ -58,22 +58,22 @@ CREATE TABLE IF NOT EXISTS "display_values" (
INSERT INTO "modbus_slaves" ( 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_format", "g_y_label", "is_logging"
) VALUES ) VALUES
(1, "Device #1", 1, 16, "Label 1", 1), (1, "Device #1", 0, 16, "Label 1", 1),
(2, "Device #2", 2, 32, "Label 2", 1), (2, "Device #2", 1, 32, "Label 2", 1),
(3, "Device #3", 8, 16, "Label 3", 0), (3, "Device #3", 3, 16, "Label 3", 0),
(4, "Device #4", NULL, NULL, "Label 4", 0); (4, "Device #4", NULL, NULL, "Label 4", 0);
INSERT INTO "display_values" ( INSERT INTO "display_values" (
"id", "name", "slave_id", "reg_addr", "reg_format" "id", "name", "slave_id", "reg_addr", "reg_format"
) VALUES ) VALUES
(1, "CV1", 1, 0, 16), (1, "CV1", 1, 10, 16),
(2, "CV2", 1, 1, 16), (2, "CV2", 1, 11, 16),
(3, "SP", 1, 2, 16), (3, "SP", 1, 12, 16),
(4, "CV1", 2, 3, 16), (4, "CV1", 2, 13, 16),
(5, "CV2", 2, 4, 16), (5, "CV2", 2, 14, 16),
(6, "SP", 2, 5, 16), (6, "SP", 2, 15, 16),
(7, "CV1", 3, 6, 16), (7, "CV1", 3, 16, 16),
(8, "SP", 3, 7, 16); (8, "SP", 3, 17, 16);
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Down -- Down
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@ -698,6 +698,10 @@ video {
margin-top: 1.5rem; margin-top: 1.5rem;
} }
.mt-auto {
margin-top: auto;
}
.block { .block {
display: block; display: block;
} }
@ -726,6 +730,10 @@ video {
height: 5rem; height: 5rem;
} }
.h-\[720px\] {
height: 720px;
}
.h-full { .h-full {
height: 100%; height: 100%;
} }
@ -734,6 +742,10 @@ video {
height: 100vh; height: 100vh;
} }
.h-\[300px\] {
height: 300px;
}
.max-h-\[400px\] { .max-h-\[400px\] {
max-height: 400px; max-height: 400px;
} }

View File

@ -135,7 +135,6 @@
<div class="pt-5 border-b flex flex-wrap justify-between items-center pr-6"> <div class="pt-5 border-b flex flex-wrap justify-between items-center pr-6">
<div class="mb-2"> <div class="mb-2">
<h2 x-text="selectedDevice?.name" class="font-bold text-3xl my-2 mr-2"></h2> <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> </div>
<!-- Device create, delete buttons --> <!-- Device create, delete buttons -->
<div> <div>
@ -152,12 +151,44 @@
</a> </a>
</div> </div>
</div> </div>
<!-- Error message -->
<!-- <div>
<span class="text-red-500" x-text="error?.message"></span>
</div> -->
<!-- Display Graph --> <!-- 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 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" /> <hr class="my-3" />
</div> </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 --> <!-- Display values -->
<div x-show="!isLoading">
<h2 class="text-xl font-bold mb-3 mt-5">Display values</h2> <h2 class="text-xl font-bold mb-3 mt-5">Display values</h2>
<div class="mb-2"> <div class="mb-2">
<div class="grid grid-cols-1 md:sm:grid-cols-2 lg:sm:grid-cols-3 gap-2"> <div class="grid grid-cols-1 md:sm:grid-cols-2 lg:sm:grid-cols-3 gap-2">
@ -193,7 +224,7 @@
</div> </div>
<hr /> <hr />
<div class="flex flex-col"> <div class="flex flex-col">
<span>Value: ---</span> <span>Value: <span x-text="value?.data"></span></span>
<span>Reg Addr: <span x-text="value.reg_addr"></span></span> <span>Reg Addr: <span x-text="value.reg_addr"></span></span>
<span>Format: <span x-text="value.reg_format"></span></span> <span>Format: <span x-text="value.reg_format"></span></span>
</div> </div>
@ -207,6 +238,7 @@
</div> </div>
</div> </div>
</div> </div>
</div>
<div class="select-none" x-show="selectedDevice === null"> <div class="select-none" x-show="selectedDevice === null">
<span class="block text-center italic text-gray-500 mt-10">No device selected.</span> <span class="block text-center italic text-gray-500 mt-10">No device selected.</span>
</div> </div>

View File

@ -5,7 +5,7 @@ import * as utils from "./utils/index.js";
var pages = ["loading", "monitoring", "configuration", "404"]; var pages = ["loading", "monitoring", "configuration", "404"];
var dataStreamSource; var dataStreamSource;
var graphData = [[0, 0]]; var graphData = [[0,0]];
var g; var g;
document.addEventListener("alpine:init", async () => { document.addEventListener("alpine:init", async () => {
@ -106,14 +106,17 @@ document.addEventListener("alpine:init", async () => {
Alpine.data("monitoringPage", () => ({ Alpine.data("monitoringPage", () => ({
selectedDevice: null, selectedDevice: null,
devices: [], devices: [],
isLoading: false,
error: null,
resetGraph() {
if (g instanceof Dygraph) {
g.destroy();
}
},
async init() { async init() {
try { try {
this.$store.app.currentPage = "loading"; this.$store.app.currentPage = "loading";
this.devices = await api.get("/modbus/devices"); this.devices = await api.get("/modbus/devices");
// window.intervalId = setInterval(() => {
// graphData.push([new Date(), Math.random()]);
// g.updateOptions({ file: graphData });
// }, 500);
} catch (error) { } catch (error) {
this.$store.app.error = error; this.$store.app.error = error;
} finally { } finally {
@ -121,6 +124,14 @@ document.addEventListener("alpine:init", async () => {
} }
}, },
selectDevice(device) { 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?.close();
dataStreamSource = new EventSource(`/api/modbus/data_stream?slave_id=${device.id}`); dataStreamSource = new EventSource(`/api/modbus/data_stream?slave_id=${device.id}`);
g = new Dygraph(document.getElementById("div_g"), graphData, { g = new Dygraph(document.getElementById("div_g"), graphData, {
@ -130,15 +141,19 @@ document.addEventListener("alpine:init", async () => {
// valueRange: [0, 1000], // valueRange: [0, 1000],
// labels: ["Time", "Random"], // labels: ["Time", "Random"],
}); });
g.resize(720, 300);
dataStreamSource.onerror = (e) => {
dataStreamSource.close();
}
dataStreamSource.onmessage = (e) => { dataStreamSource.onmessage = (e) => {
const data = JSON.parse(e.data); const data = JSON.parse(e.data);
console.log(data); console.log(data);
graphData.push([new Date(), data.graph.value]); graphData.push([new Date(), data.graph.value]);
g.updateOptions({ file: graphData }); 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 = [];
graphData = []; // todo: reset graph
this.selectedDevice = device;
}, },
async removeDevice(id) { async removeDevice(id) {
try { try {