first commit

This commit is contained in:
2025-06-14 11:22:44 +07:00
parent 6010ce956c
commit 0c748cbac1
29 changed files with 242 additions and 1 deletions

122
src/config.java Normal file
View File

@@ -0,0 +1,122 @@
import lombok.Getter;
import lombok.Setter;
import org.tinylog.Logger;
import java.nio.file.Files;
import java.nio.file.Path;
public class config {
private @Getter @Setter String VX3KTargetIP;
private @Getter @Setter int VX3KTargetPort;
private @Getter @Setter String Email_SMTPServer;
private @Getter @Setter int Email_SMTPPort;
private @Getter @Setter boolean Email_SMTPSSL;
private @Getter @Setter String Email_SMTPUsername;
private @Getter @Setter String Email_SMTPPassword;
private @Getter @Setter String Email_SMTPFrom ;
private @Getter @Setter String Email_SenderName;
private @Getter @Setter String Email_Subject;
private @Getter @Setter String MQTT_Broker;
private @Getter @Setter int MQTT_Port;
private @Getter @Setter String MQTT_Topic ;
private @Getter @Setter String MQTT_ClientID;
private @Getter @Setter String MQTT_Username;
private @Getter @Setter String MQTT_Password ;
public config(){
Load();
}
/**
* Load configuration from file.
* If the file does not exist, create default configuration.
*/
private 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);
config loadedConfig = Somecodes.gson.fromJson(configContent, config.class);
if (loadedConfig != null) {
// Copy values from loadedConfig to this instance
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;
Logger.info("Loaded config from {}", configPath.toAbsolutePath());
} 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
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";
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;
}
}