-
-
-
-
-
-
-
-
+
+
-
-
-
+
-
-
+
+
+
+
+
-
-
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
diff --git a/public/js/api.js b/public/js/api.js
new file mode 100644
index 0000000..82caf60
--- /dev/null
+++ b/public/js/api.js
@@ -0,0 +1,25 @@
+const urlPrefix = "/api";
+
+export default {
+ async patch(path, data) {
+ const response = await fetch(`${urlPrefix}${path}`, {
+ method: "PATCH",
+ body: new URLSearchParams(data),
+ headers: {
+ "Content-Type": "application/x-www-form-urlencoded",
+ },
+ });
+ return response;
+ },
+
+ async post(path, data) {
+ const response = await fetch(`${urlPrefix}${path}`, {
+ method: "POST",
+ body: new URLSearchParams(data),
+ headers: {
+ "Content-Type": "application/x-www-form-urlencoded",
+ },
+ });
+ return response;
+ },
+};
diff --git a/public/js/app.js b/public/js/app.js
index f9998b2..3f6b371 100644
--- a/public/js/app.js
+++ b/public/js/app.js
@@ -1,18 +1,35 @@
// @ts-nocheck
import "./alpine.js";
+import api from "./api.js";
document.addEventListener("alpine:init", async () => {
Alpine.store("app", {
is_configured: false,
- config: null,
+ /**
+ * Default configuration
+ */
+ config: {
+ mb_connection_type: "TCP",
+ mb_tcp_ip: "127.0.0.1",
+ mb_tcp_port: 502,
+ mb_rtu_path: "",
+ mb_rtu_baud: 9600,
+ mb_rtu_data_bits: 8,
+ mb_rtu_stop_bits: 1,
+ mb_rtu_parity: "even",
+ log_interval_ms: 1000,
+ },
currentPage: "loading",
async init() {
- const { config } = await (await fetch("/api/app/config")).json();
- const { is_configured } = await (await fetch("/api/app/is_configured")).json();
- this.is_configured = is_configured;
- this.config = config;
+ const config = await fetch("/api/app/config");
+ const is_configured = await fetch("/api/app/is_configured");
- if (is_configured) {
+ if (config.ok && is_configured.ok) {
+ this.config = (await config.json()).config;
+ this.is_configured = (await is_configured.json()).is_configured;
+ }
+
+ if (this.is_configured) {
this.currentPage = "monitoring";
return;
}
@@ -21,9 +38,52 @@ document.addEventListener("alpine:init", async () => {
},
});
- Alpine.data("configAppModal", () => ({
+ Alpine.data("configApp", () => ({
+ selectedConnectionType: "TCP",
+ isLoading: false,
async configurateApp() {
- console.log("Configuration app...");
+ try {
+ const {
+ mb_connection_type,
+ mb_tcp_ip,
+ mb_tcp_port,
+ mb_rtu_path,
+ mb_rtu_baud,
+ mb_rtu_parity,
+ mb_rtu_data_bits,
+ mb_rtu_stop_bits,
+ log_interval_ms,
+ } = this.$store.app.config;
+
+ if (mb_connection_type === "TCP") {
+ await api.patch("/app/config", {
+ mb_connection_type,
+ mb_tcp_ip,
+ mb_tcp_port,
+ log_interval_ms,
+ });
+
+ await api.post("/modbus/connect");
+
+ this.$store.app.currentPage = "monitoring";
+ return;
+ }
+
+ if (mb_connection_type === "RTU") {
+ await api.patch("/app/config", {
+ mb_rtu_path,
+ mb_rtu_parity,
+ mb_rtu_data_bits,
+ mb_rtu_stop_bits,
+ mb_rtu_baud,
+ });
+ await api.post("/modbus_connect");
+ this.$store.app.currentPage = "monitoring";
+ return;
+ }
+ } catch (error) {
+ console.error("Error while configuring app.");
+ }
},
}));
});
diff --git a/public/js/services/app.service.js b/public/js/services/app.service.js
new file mode 100644
index 0000000..912f6f2
--- /dev/null
+++ b/public/js/services/app.service.js
@@ -0,0 +1,27 @@
+import api from "../api";
+
+export async function configurateApp(config) {
+ const {
+ mb_connection_type,
+ mb_tcp_ip,
+ mb_tcp_port,
+ mb_rtu_path,
+ mb_rtu_parity,
+ mb_rtu_data_bits,
+ mb_rtu_stop_bits,
+ mb_rtu_baud,
+ log_interval_ms,
+ } = config;
+ let data = {};
+
+ if (config.mb_connection_type === "TCP") {
+ data = { mb_connection_type, mb_tcp_ip, mb_tcp_port, log_interval_ms };
+ }
+
+ if (config.mb_connection_type === "RTU") {
+ data = { mb_rtu_path, mb_rtu_parity, mb_rtu_data_bits, mb_rtu_stop_bits, mb_rtu_baud };
+ }
+
+ const configRes = await api.patch("/app/config", data);
+ if (configRes.ok) await api.post("/modbus/connect");
+}
diff --git a/routes/api.router.js b/routes/api.router.js
index a13df36..6d8f088 100644
--- a/routes/api.router.js
+++ b/routes/api.router.js
@@ -52,7 +52,7 @@ apiRouter.patch("/app/config", validateBodyMiddleware(appConfigSchema), async (r
[mb_tcp_ip, mb_tcp_port, payload.log_interval_ms],
);
- return res.status(200).send("Updated");
+ return res.status(200).send("Config Updated.");
}
/* Update RTU configuration */
@@ -71,7 +71,7 @@ apiRouter.patch("/app/config", validateBodyMiddleware(appConfigSchema), async (r
`,
[mb_rtu_path, mb_rtu_baud, mb_rtu_parity, mb_rtu_data_bits, mb_rtu_stop_bits, payload.log_interval_ms],
);
- return res.status(200).send("Updated.");
+ return res.status(200).send("Config updated.");
}
return res.status(400).send("Undefined Modbus connection type.");