commit 18/06/2025

This commit is contained in:
2025-06-23 12:26:08 +07:00
parent 3ed1123d1a
commit 0d572514db
15 changed files with 95 additions and 27 deletions

View File

@@ -0,0 +1,12 @@
<component name="libraryTable">
<library name="fasterxml.jackson.core.databind" type="repository">
<properties maven-id="com.fasterxml.jackson.core:jackson-databind:2.17.2" />
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/jackson-databind-2.17.2.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jackson-annotations-2.17.2.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jackson-core-2.17.2.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@@ -17,5 +17,6 @@
<orderEntry type="library" name="sun.mail.jakarta" level="project" /> <orderEntry type="library" name="sun.mail.jakarta" level="project" />
<orderEntry type="library" name="digitalpetri.modbus.tcp" level="project" /> <orderEntry type="library" name="digitalpetri.modbus.tcp" level="project" />
<orderEntry type="library" name="slf4j.simple" level="project" /> <orderEntry type="library" name="slf4j.simple" level="project" />
<orderEntry type="library" name="fasterxml.jackson.core.databind" level="project" />
</component> </component>
</module> </module>

1
additional.config.json Normal file
View File

@@ -0,0 +1 @@
{"Modbus_Port":502,"Modbus_MasterIP":"192.168.10.1","Modbus_SlaveID":"1","VX3KTargetIP":"192.168.14.1","VX3KTargetPort":5000,"Email_SMTPServer":"mail.galva.co.id","Email_SMTPPort":587,"Email_SMTPSSL":true,"Email_SMTPUsername":"admin","Email_SMTPPassword":"admin","Email_SMTPFrom":"fa@galva.co.id","Email_SenderName":"Fire Alarm Gateway","Email_Subject":"Fire Alarm Gateway Notification","MQTT_Broker":"34.101.202.96","MQTT_Port":1883,"MQTT_Topic":"FA_Gateway/status","MQTT_ClientID":"Pekojan","MQTT_Username":"gtcdev","MQTT_Password":"gtcdev2025","WebListenPort":"80"}

Binary file not shown.

BIN
lib/jackson-core-2.17.2.jar Normal file

Binary file not shown.

Binary file not shown.

View File

@@ -1,4 +1,5 @@
import additional.Somecodes; package additional;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.tinylog.Logger; import org.tinylog.Logger;
@@ -33,15 +34,15 @@ public class config {
* If the file does not exist, create default configuration. * If the file does not exist, create default configuration.
*/ */
public void Load(){ public void Load(){
Path configPath = Path.of(Somecodes.currentdirectory, "config.json"); Path configPath = Path.of(Somecodes.currentdirectory, "additional.config.json");
if (Files.exists(configPath)){ if (Files.exists(configPath)){
// Read the configuration from the file // Read the configuration from the file
// and if not complete, create defaults // and if not complete, create defaults
try{ try{
String configContent = Files.readString(configPath); String configContent = Files.readString(configPath);
Logger.info("config content: {}", configContent); Logger.info("additional.config content: {}", configContent);
config loadedConfig = Somecodes.gson.fromJson(configContent, config.class); config loadedConfig = Somecodes.gson.fromJson(configContent, config.class);
Logger.info("Loaded config from {}", loadedConfig.toString()); Logger.info("Loaded additional.config from {}", loadedConfig.toString());
if (loadedConfig != null) { if (loadedConfig != null) {
// Copy values from loadedConfig to this instance // Copy values from loadedConfig to this instance
this.Modbus_MasterIP = loadedConfig.Modbus_MasterIP; this.Modbus_MasterIP = loadedConfig.Modbus_MasterIP;
@@ -66,11 +67,11 @@ public class config {
this.WebListenPort = loadedConfig.WebListenPort; this.WebListenPort = loadedConfig.WebListenPort;
} else { } else {
Logger.error("Loaded config is null, creating Default Config"); Logger.error("Loaded additional.config is null, creating Default Config");
MakeDefaults(); MakeDefaults();
} }
} catch (Exception e){ } catch (Exception e){
Logger.error("Failed to read config file: {}, creating Default Config", e.getMessage()); Logger.error("Failed to read additional.config file: {}, creating Default Config", e.getMessage());
MakeDefaults(); MakeDefaults();
} }
} else { } else {
@@ -115,14 +116,14 @@ public class config {
* @return true if the save operation was successful, false otherwise. * @return true if the save operation was successful, false otherwise.
*/ */
public boolean Save(){ public boolean Save(){
Path configPath = Path.of(Somecodes.currentdirectory, "config.json"); Path configPath = Path.of(Somecodes.currentdirectory, "additional.config.json");
String gs = Somecodes.gson.toJson(this); String gs = Somecodes.gson.toJson(this);
try { try {
Files.writeString(configPath, gs); Files.writeString(configPath, gs);
Logger.info("Default config written to {}", configPath.toAbsolutePath()); Logger.info("Default additional.config written to {}", configPath.toAbsolutePath());
return true; return true;
} catch (Exception e) { } catch (Exception e) {
Logger.error("Failed to write default config: {}", e.getMessage()); Logger.error("Failed to write default additional.config: {}", e.getMessage());
} }
return false; return false;
} }

View File

@@ -1,19 +1,13 @@
console.log("Initializing websocket"); console.log("Initializing websocket");
if (window.socket==null) { if (window.socket==null) {
window.socket = new WebSocket("ws://" + location.host + "/ws"); window.socket = new WebSocket("ws://" + location.host + "/ws");
console.log("creating window.socket"); }
} else {
console.log("window.socket already exists");
}
socket.onopen = () => { socket.onopen = () => {
console.log("WebSocket connected"); console.log("WebSocket connected");
}; };
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log("Received from server:", data);
};
socket.onerror = (err) => { socket.onerror = (err) => {
console.error("WebSocket error:", err); console.error("WebSocket error:", err);

View File

@@ -2,4 +2,9 @@ document.addEventListener("DOMContentLoaded", function () {
console.log("Index page loaded"); console.log("Index page loaded");
send_data("Hello from Index.html") send_data("Hello from Index.html")
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log("Received from server:", data);
};
}); });

View File

@@ -1,5 +1,10 @@
document.addEventListener("DOMContentLoaded", function () { document.addEventListener("DOMContentLoaded", function () {
console.log("Setting page loaded"); console.log("Setting page loaded");
send_data("Hello from setting.js");
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log("Received from server:", data);
};
send_data(JSON.stringify({request:"GET_SETTING"}));
}); });

View File

@@ -1,3 +1,6 @@
package main;
import additional.config;
import database.ContactInputData; import database.ContactInputData;
import database.Database; import database.Database;
import gpio.NanopiGpio; import gpio.NanopiGpio;
@@ -12,7 +15,7 @@ import web.WebServer;
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or //TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter. // click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main { public class Main {
private static config config; public static config config;
private static MqttClient mqttClient; private static MqttClient mqttClient;
private static NanopiGpio gpio; private static NanopiGpio gpio;
private static Database db; private static Database db;
@@ -24,7 +27,7 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Logger.info("Application started"); Logger.info("Application started");
// initialize config // initialize additional.config
config = new config(); config = new config();
config.Load(); config.Load();

View File

@@ -1,7 +1,9 @@
package web; package web;
import additional.config;
import io.javalin.Javalin; import io.javalin.Javalin;
import lombok.Getter; import lombok.Getter;
import main.Main;
import org.tinylog.Logger; import org.tinylog.Logger;
import static additional.Somecodes.ValidString; import static additional.Somecodes.ValidString;
@@ -47,16 +49,33 @@ public class WebServer {
ws.onMessage(ctx -> { ws.onMessage(ctx -> {
Logger.info("WebSocket message received: {}", ctx.message()); Logger.info("WebSocket message received: {}", ctx.message());
webRequest req = gson.fromJson(ctx.message(), webRequest.class); try{
if (ValidString(req.request())){ webRequest req = ctx.messageAsClass(webRequest.class);
String cmd = req.request().trim().toUpperCase(); if (ValidString(req.request())){
switch(cmd){ String cmd = req.request().trim().toUpperCase();
case "GET_SETTING" : switch(cmd){
case "GET_SETTING" :
webSettingArgs args1 = new webSettingArgs();
args1.mqttBrokerIP = Main.config.getMQTT_Broker();
break; webResponse response = new webResponse("GET_SETTING", gson.toJson(args1));
ctx.send(response);
break;
case "RESET_DEFAULT":
break;
case "SET_SETTING":
webSettingArgs args2 = gson.fromJson(req.args(), webSettingArgs.class);
break;
}
} }
} catch (Exception e){
Logger.error("Not processing web request: {} because {}", ctx.message(), e.getMessage());
webResponse response = new webResponse("ERROR", e.getMessage());
ctx.send(response);
} }
}); });
ws.onClose(ctx -> { ws.onClose(ctx -> {

View File

@@ -1,4 +1,4 @@
package web; package web;
public record webRequest(String request, String[] args) { public record webRequest(String request, String args) {
} }

4
src/web/webResponse.java Normal file
View File

@@ -0,0 +1,4 @@
package web;
public record webResponse(String response, String args) {
}

View File

@@ -0,0 +1,23 @@
package web;
import lombok.Data;
@Data
public class webSettingArgs {
String smtpServer;
int smtpPort;
String smtpUsername;
String smtpPassword;
boolean smtpSSL;
String smtpSenderAddress;
String smtpSenderName;
String smtpSubject;
String vx3kIP;
int vx3kPort;
String mqttBrokerIP;
int mqttPort;
String mqttUsername;
String mqttPassword;
String mqttClientID;
String mqttTopic;
}