diff --git a/src/Main.java b/src/Main.java index fd49dde..1e643a6 100644 --- a/src/Main.java +++ b/src/Main.java @@ -39,8 +39,24 @@ public class Main { mailsender = new SMTPSender(config.getEmail_SMTPServer(), config.getEmail_SMTPPort(), true, config.getEmail_SMTPUsername(), config.getEmail_SMTPPassword()); // initialize Modbus TCP Server - modbusServer = new ModbusTCPServer(config.getModbus_MasterIP(), config.getModbus_Port(), 1000); + modbusServer = new ModbusTCPServer("0.0.0.0", config.getModbus_Port(), 1000); modbusServer.Start(); + // try changing modbus register several times + /*Thread xx = new Thread(() -> { + try { + for (int i = 0; i < 100; i++) { + modbusServer.SetRegister(0, i); + modbusServer.SetRegister(1, i); + modbusServer.SetRegister(2, i); + modbusServer.SetRegister(3, i); + Thread.sleep(1000); // sleep for 1 second + } + } catch (InterruptedException e) { + Logger.error("Thread interrupted: {}", e.getMessage()); + } + }); + xx.setName("Modbus tester"); + xx.start();*/ // Initialize the web server webServer = new WebServer(); @@ -106,14 +122,14 @@ public class Main { }, - new gpio.PinInfo(11, "Relay 1"), - new gpio.PinInfo(12, "Relay 2"), - new gpio.PinInfo(13, "Relay 3"), - new gpio.PinInfo(14, "Relay 4"), - new gpio.PinInfo(16, "Relay 5"), - new gpio.PinInfo(15, "Relay 6"), - new gpio.PinInfo(199, "Relay 7"), - new gpio.PinInfo(198, "Relay 8") + new gpio.PinInfo(11, db.GetContactInputDataDescription(1,"Relay 1")), + new gpio.PinInfo(12, db.GetContactInputDataDescription(2,"Relay 2")), + new gpio.PinInfo(13, db.GetContactInputDataDescription(3,"Relay 3")), + new gpio.PinInfo(14, db.GetContactInputDataDescription(4,"Relay 4")), + new gpio.PinInfo(16, db.GetContactInputDataDescription(5,"Relay 5")), + new gpio.PinInfo(15, db.GetContactInputDataDescription(6,"Relay 6")), + new gpio.PinInfo(199, db.GetContactInputDataDescription(7,"Relay 7")), + new gpio.PinInfo(198, db.GetContactInputDataDescription(8,"Relay 8")) ); // initialize the MQTT client diff --git a/src/Somecodes.java b/src/additional/Somecodes.java similarity index 58% rename from src/Somecodes.java rename to src/additional/Somecodes.java index 786e12c..4e5296c 100644 --- a/src/Somecodes.java +++ b/src/additional/Somecodes.java @@ -1,3 +1,5 @@ +package additional; + import com.google.gson.Gson; @@ -6,7 +8,9 @@ public class Somecodes { public static String currentdirectory = System.getProperty("user.dir"); public static Gson gson = new Gson(); - + public static boolean ValidString(String string) { + return string != null && !string.isBlank(); + } diff --git a/src/config.java b/src/config.java index 79c62e1..2fda8a0 100644 --- a/src/config.java +++ b/src/config.java @@ -1,3 +1,4 @@ +import additional.Somecodes; import lombok.Getter; import lombok.Setter; import org.tinylog.Logger; diff --git a/src/database/Database.java b/src/database/Database.java index 5ee3ce8..2e8cf7d 100644 --- a/src/database/Database.java +++ b/src/database/Database.java @@ -1,5 +1,6 @@ package database; +import lombok.NonNull; import org.tinylog.Logger; import java.sql.Connection; @@ -163,6 +164,20 @@ public class Database { return contactInputDataList.stream().filter(x -> x.getContactID() == contactID).findFirst().orElse(null); } + /** + * Get ContactInputData description by ID + * @param contactID ID to get + * @param defaultname Default name to return if ContactInputData not found + * @return Description of ContactInputData if exists, or defaultname if not exists + */ + public String GetContactInputDataDescription(int contactID, @NonNull String defaultname){ + ContactInputData contactInputData = GetContactInputData(contactID); + if (contactInputData != null) { + return contactInputData.getDescription(); + } + return defaultname; + } + /** * Get ContactInputData by description * @param description Description to get diff --git a/src/html/assets/js/connection.js b/src/html/assets/js/connection.js new file mode 100644 index 0000000..4fd9c4a --- /dev/null +++ b/src/html/assets/js/connection.js @@ -0,0 +1,35 @@ +console.log("Initializing websocket"); + if (window.socket==null) { + window.socket = new WebSocket("ws://" + location.host + "/ws"); + console.log("creating window.socket"); + } else { + console.log("window.socket already exists"); + } + + socket.onopen = () => { + console.log("WebSocket connected"); + }; + + socket.onmessage = (event) => { + const data = JSON.parse(event.data); + console.log("Received from server:", data); + }; + + socket.onerror = (err) => { + console.error("WebSocket error:", err); + }; + + socket.onclose = () => { + console.log("WebSocket closed"); + }; + +window.send_data = function(value){ + if (socket.readyState === WebSocket.OPEN){ + socket.send(value); + console.log("Sent data : "+value); + } else { + setTimeout(() => send_data(value),50) + console.log("Failed to send_data, socket readyState = "+socket.readyState); + } +} + diff --git a/src/html/assets/js/index.js b/src/html/assets/js/index.js new file mode 100644 index 0000000..76c994d --- /dev/null +++ b/src/html/assets/js/index.js @@ -0,0 +1,5 @@ +document.addEventListener("DOMContentLoaded", function () { + console.log("Index page loaded"); + + send_data("Hello from Index.html") +}); \ No newline at end of file diff --git a/src/html/assets/js/setting.js b/src/html/assets/js/setting.js index a09735e..0eac1f8 100644 --- a/src/html/assets/js/setting.js +++ b/src/html/assets/js/setting.js @@ -1,22 +1,5 @@ document.addEventListener("DOMContentLoaded", function () { - const socket = new WebSocket("ws://" + location.host + "/ws"); - - socket.onopen = () => { - console.log("WebSocket connected"); - const message = { type: "hello", value: 123 }; - socket.send(JSON.stringify(message)); - }; - - socket.onmessage = (event) => { - const data = JSON.parse(event.data); - console.log("Received from server:", data); - }; - - socket.onerror = (err) => { - console.error("WebSocket error:", err); - }; - - socket.onclose = () => { - console.log("WebSocket closed"); - }; + console.log("Setting page loaded"); + send_data("Hello from setting.js"); + }); \ No newline at end of file diff --git a/src/html/index.html b/src/html/index.html index a47a29d..840ca4d 100644 --- a/src/html/index.html +++ b/src/html/index.html @@ -26,7 +26,8 @@ - + + \ No newline at end of file diff --git a/src/html/setting.html b/src/html/setting.html index 0da344a..60f5dc3 100644 --- a/src/html/setting.html +++ b/src/html/setting.html @@ -26,22 +26,89 @@
-
-
-
-
+
+
+

SMTP Setting

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+

VX-3000 Setting

+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+

MQTT Setting

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -52,6 +119,7 @@
+ diff --git a/src/modbus/ModbusTCPServer.java b/src/modbus/ModbusTCPServer.java index 3b61500..4436076 100644 --- a/src/modbus/ModbusTCPServer.java +++ b/src/modbus/ModbusTCPServer.java @@ -33,6 +33,7 @@ public class ModbusTCPServer { */ public ModbusTCPServer(String bindAddress, int port, int holdingRegisterSize) { this.holdingregister = new AtomicIntegerArray(holdingRegisterSize); + Logger.info("Creating ModbusTCPServer at {}:{} with holding register size {}", bindAddress, port, holdingRegisterSize); transport = NettyTcpServerTransport.create(cfg ->{ cfg.bindAddress = bindAddress; cfg.port = port; diff --git a/src/web/WebServer.java b/src/web/WebServer.java index 4baf119..47875b9 100644 --- a/src/web/WebServer.java +++ b/src/web/WebServer.java @@ -4,6 +4,9 @@ import io.javalin.Javalin; import lombok.Getter; import org.tinylog.Logger; +import static additional.Somecodes.ValidString; +import static additional.Somecodes.gson; + public class WebServer { private Javalin app; private @Getter boolean isRunning = false; @@ -44,7 +47,16 @@ public class WebServer { ws.onMessage(ctx -> { Logger.info("WebSocket message received: {}", ctx.message()); - // Handle incoming messages here + webRequest req = gson.fromJson(ctx.message(), webRequest.class); + if (ValidString(req.request())){ + String cmd = req.request().trim().toUpperCase(); + switch(cmd){ + case "GET_SETTING" : + + break; + } + } + }); ws.onClose(ctx -> { diff --git a/src/web/webRequest.java b/src/web/webRequest.java new file mode 100644 index 0000000..a6c05fd --- /dev/null +++ b/src/web/webRequest.java @@ -0,0 +1,4 @@ +package web; + +public record webRequest(String request, String[] args) { +}