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="digitalpetri.modbus.tcp" level="project" />
<orderEntry type="library" name="slf4j.simple" level="project" />
<orderEntry type="library" name="fasterxml.jackson.core.databind" level="project" />
</component>
</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.Setter;
import org.tinylog.Logger;
@@ -33,15 +34,15 @@ public class config {
* If the file does not exist, create default configuration.
*/
public void Load(){
Path configPath = Path.of(Somecodes.currentdirectory, "config.json");
Path configPath = Path.of(Somecodes.currentdirectory, "additional.config.json");
if (Files.exists(configPath)){
// Read the configuration from the file
// and if not complete, create defaults
try{
String configContent = Files.readString(configPath);
Logger.info("config content: {}", configContent);
Logger.info("additional.config content: {}", configContent);
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) {
// Copy values from loadedConfig to this instance
this.Modbus_MasterIP = loadedConfig.Modbus_MasterIP;
@@ -66,11 +67,11 @@ public class config {
this.WebListenPort = loadedConfig.WebListenPort;
} else {
Logger.error("Loaded config is null, creating Default Config");
Logger.error("Loaded additional.config is null, creating Default Config");
MakeDefaults();
}
} 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();
}
} else {
@@ -115,14 +116,14 @@ public class config {
* @return true if the save operation was successful, false otherwise.
*/
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);
try {
Files.writeString(configPath, gs);
Logger.info("Default config written to {}", configPath.toAbsolutePath());
Logger.info("Default additional.config written to {}", configPath.toAbsolutePath());
return true;
} catch (Exception e) {
Logger.error("Failed to write default config: {}", e.getMessage());
Logger.error("Failed to write default additional.config: {}", e.getMessage());
}
return false;
}

View File

@@ -1,19 +1,13 @@
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);

View File

@@ -2,4 +2,9 @@ document.addEventListener("DOMContentLoaded", function () {
console.log("Index page loaded");
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 () {
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.Database;
import gpio.NanopiGpio;
@@ -12,7 +15,7 @@ import web.WebServer;
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
private static config config;
public static config config;
private static MqttClient mqttClient;
private static NanopiGpio gpio;
private static Database db;
@@ -24,7 +27,7 @@ public class Main {
public static void main(String[] args) {
Logger.info("Application started");
// initialize config
// initialize additional.config
config = new config();
config.Load();

View File

@@ -1,7 +1,9 @@
package web;
import additional.config;
import io.javalin.Javalin;
import lombok.Getter;
import main.Main;
import org.tinylog.Logger;
import static additional.Somecodes.ValidString;
@@ -47,15 +49,32 @@ public class WebServer {
ws.onMessage(ctx -> {
Logger.info("WebSocket message received: {}", ctx.message());
webRequest req = gson.fromJson(ctx.message(), webRequest.class);
try{
webRequest req = ctx.messageAsClass(webRequest.class);
if (ValidString(req.request())){
String cmd = req.request().trim().toUpperCase();
switch(cmd){
case "GET_SETTING" :
webSettingArgs args1 = new webSettingArgs();
args1.mqttBrokerIP = Main.config.getMQTT_Broker();
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);
}
});

View File

@@ -1,4 +1,4 @@
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;
}