Files
FA_Gateway_Java/src/config.java
2025-06-17 16:23:50 +07:00

129 lines
5.3 KiB
Java

import lombok.Getter;
import lombok.Setter;
import org.tinylog.Logger;
import java.nio.file.Files;
import java.nio.file.Path;
public class config {
@Getter @Setter int Modbus_Port ;
@Getter @Setter String Modbus_MasterIP;
@Getter @Setter String Modbus_SlaveID ;
@Getter @Setter String VX3KTargetIP;
@Getter @Setter int VX3KTargetPort;
@Getter @Setter String Email_SMTPServer;
@Getter @Setter int Email_SMTPPort;
@Getter @Setter boolean Email_SMTPSSL;
@Getter @Setter String Email_SMTPUsername;
@Getter @Setter String Email_SMTPPassword;
@Getter @Setter String Email_SMTPFrom ;
@Getter @Setter String Email_SenderName;
@Getter @Setter String Email_Subject;
@Getter @Setter String MQTT_Broker;
@Getter @Setter int MQTT_Port;
@Getter @Setter String MQTT_Topic ;
@Getter @Setter String MQTT_ClientID;
@Getter @Setter String MQTT_Username;
@Getter @Setter String MQTT_Password ;
@Getter @Setter String WebListenPort;
/**
* Load configuration from file.
* If the file does not exist, create default configuration.
*/
public void Load(){
Path configPath = Path.of(Somecodes.currentdirectory, "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);
config loadedConfig = Somecodes.gson.fromJson(configContent, config.class);
Logger.info("Loaded config from {}", loadedConfig.toString());
if (loadedConfig != null) {
// Copy values from loadedConfig to this instance
this.Modbus_MasterIP = loadedConfig.Modbus_MasterIP;
this.Modbus_Port = loadedConfig.Modbus_Port;
this.Modbus_SlaveID = loadedConfig.Modbus_SlaveID;
this.VX3KTargetIP = loadedConfig.VX3KTargetIP;
this.VX3KTargetPort = loadedConfig.VX3KTargetPort;
this.Email_SMTPServer = loadedConfig.Email_SMTPServer;
this.Email_SMTPPort = loadedConfig.Email_SMTPPort;
this.Email_SMTPSSL = loadedConfig.Email_SMTPSSL;
this.Email_SMTPUsername = loadedConfig.Email_SMTPUsername;
this.Email_SMTPPassword = loadedConfig.Email_SMTPPassword;
this.Email_SMTPFrom = loadedConfig.Email_SMTPFrom;
this.Email_SenderName = loadedConfig.Email_SenderName;
this.Email_Subject = loadedConfig.Email_Subject;
this.MQTT_Broker = loadedConfig.MQTT_Broker;
this.MQTT_Port = loadedConfig.MQTT_Port;
this.MQTT_Topic = loadedConfig.MQTT_Topic;
this.MQTT_ClientID = loadedConfig.MQTT_ClientID;
this.MQTT_Username = loadedConfig.MQTT_Username;
this.MQTT_Password = loadedConfig.MQTT_Password;
this.WebListenPort = loadedConfig.WebListenPort;
} else {
Logger.error("Loaded config is null, creating Default Config");
MakeDefaults();
}
} catch (Exception e){
Logger.error("Failed to read config file: {}, creating Default Config", e.getMessage());
MakeDefaults();
}
} else {
Logger.error("Config file not found at {}, creating Default Config", configPath.toAbsolutePath());
MakeDefaults();
}
}
/**
* Create default configuration values.
* This method sets default values for all configuration fields.
*/
private void MakeDefaults() {
// Set default values if needed
Modbus_MasterIP = "192.168.10.1";
Modbus_Port = 502;
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";
Save();
}
/**
* Save the current configuration to a file.
* This method serializes the configuration object to JSON and writes it to a file.
* @return true if the save operation was successful, false otherwise.
*/
public boolean Save(){
Path configPath = Path.of(Somecodes.currentdirectory, "config.json");
String gs = Somecodes.gson.toJson(this);
try {
Files.writeString(configPath, gs);
Logger.info("Default config written to {}", configPath.toAbsolutePath());
return true;
} catch (Exception e) {
Logger.error("Failed to write default config: {}", e.getMessage());
}
return false;
}
}