first commit

This commit is contained in:
2025-06-14 11:58:57 +07:00
parent d918f88283
commit d9b005f784
22 changed files with 138 additions and 3 deletions

1
.gitignore vendored
View File

@@ -2,7 +2,6 @@
out/ out/
!**/src/main/**/out/ !**/src/main/**/out/
!**/src/test/**/out/ !**/src/test/**/out/
config.json
### Eclipse ### ### Eclipse ###
.apt_generated .apt_generated

View File

@@ -0,0 +1,10 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false">
<option name="processCode" value="true" />
<option name="processLiterals" value="true" />
<option name="processComments" value="true" />
</inspection_tool>
</profile>
</component>

23
.idea/libraries/hivemq_mqtt_client.xml generated Normal file
View File

@@ -0,0 +1,23 @@
<component name="libraryTable">
<library name="hivemq.mqtt.client" type="repository">
<properties maven-id="com.hivemq:hivemq-mqtt-client:1.3.6" />
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/hivemq-mqtt-client-1.3.6.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/rxjava-2.2.21.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/reactive-streams-1.0.4.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/netty-buffer-4.1.122.Final.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/netty-codec-4.1.122.Final.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/netty-common-4.1.122.Final.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/netty-handler-4.1.122.Final.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/netty-resolver-4.1.122.Final.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/netty-transport-native-unix-common-4.1.122.Final.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/netty-transport-4.1.122.Final.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/jctools-core-2.1.2.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/annotations-26.0.2.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/dagger-2.27.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/javax.inject-1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@@ -13,5 +13,6 @@
<orderEntry type="library" name="google.code.gson" level="project" /> <orderEntry type="library" name="google.code.gson" level="project" />
<orderEntry type="library" name="tinylog.impl" level="project" /> <orderEntry type="library" name="tinylog.impl" level="project" />
<orderEntry type="library" name="tinylog.api" level="project" /> <orderEntry type="library" name="tinylog.api" level="project" />
<orderEntry type="library" name="hivemq.mqtt.client" level="project" />
</component> </component>
</module> </module>

1
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"}

BIN
lib/annotations-26.0.2.jar Normal file

Binary file not shown.

BIN
lib/dagger-2.27.jar Normal file

Binary file not shown.

Binary file not shown.

BIN
lib/javax.inject-1.jar Normal file

Binary file not shown.

BIN
lib/jctools-core-2.1.2.jar Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
lib/rxjava-2.2.21.jar Normal file

Binary file not shown.

View File

@@ -1,8 +1,27 @@
import mqtt.MqttClient;
import org.tinylog.Logger;
//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; private static config config;
private static MqttClient mqttClient;
public static void main(String[] args) { public static void main(String[] args) {
Logger.info("Application started");
config = new config(); config = new config();
mqttClient = new MqttClient(config.getMQTT_Broker(), config.getMQTT_Port(), config.getMQTT_Username(), config.getMQTT_Password(), connected -> {
if (connected) {
Logger.info("MQTT client connected successfully.");
mqttClient.Publish(config.getMQTT_Topic(), config.getMQTT_ClientID(), "Fire Alarm Gateway Online", published -> {
if (published) {
Logger.info("Online Message published successfully.");
} else {
Logger.error("Failed to publish Online Message.");
}
});
} else {
Logger.error("Failed to connect MQTT client.");
}
});
} }
} }

View File

@@ -6,6 +6,9 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
public class config { public class config {
private @Getter @Setter int Modbus_Port = 502;
private @Getter @Setter String Modbus_MasterIP = "192.168.10.1";
private @Getter @Setter String Modbus_SlaveID = "1";
private @Getter @Setter String VX3KTargetIP; private @Getter @Setter String VX3KTargetIP;
private @Getter @Setter int VX3KTargetPort; private @Getter @Setter int VX3KTargetPort;
private @Getter @Setter String Email_SMTPServer; private @Getter @Setter String Email_SMTPServer;
@@ -42,6 +45,9 @@ public class config {
config loadedConfig = Somecodes.gson.fromJson(configContent, config.class); config loadedConfig = Somecodes.gson.fromJson(configContent, config.class);
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_Port = loadedConfig.Modbus_Port;
this.Modbus_SlaveID = loadedConfig.Modbus_SlaveID;
this.VX3KTargetIP = loadedConfig.VX3KTargetIP; this.VX3KTargetIP = loadedConfig.VX3KTargetIP;
this.VX3KTargetPort = loadedConfig.VX3KTargetPort; this.VX3KTargetPort = loadedConfig.VX3KTargetPort;
this.Email_SMTPServer = loadedConfig.Email_SMTPServer; this.Email_SMTPServer = loadedConfig.Email_SMTPServer;
@@ -59,7 +65,6 @@ public class config {
this.MQTT_Username = loadedConfig.MQTT_Username; this.MQTT_Username = loadedConfig.MQTT_Username;
this.MQTT_Password = loadedConfig.MQTT_Password; this.MQTT_Password = loadedConfig.MQTT_Password;
Logger.info("Loaded config from {}", configPath.toAbsolutePath());
} else { } else {
Logger.error("Loaded config is null, creating Default Config"); Logger.error("Loaded config is null, creating Default Config");
MakeDefaults(); MakeDefaults();
@@ -80,7 +85,9 @@ public class config {
*/ */
private void MakeDefaults() { private void MakeDefaults() {
// Set default values if needed // Set default values if needed
Modbus_MasterIP = "192.168.10.1";
Modbus_Port = 502;
Modbus_SlaveID = "1";
VX3KTargetIP = "192.168.14.1"; VX3KTargetIP = "192.168.14.1";
VX3KTargetPort = 5000; VX3KTargetPort = 5000;
Email_SMTPServer = "mail.galva.co.id"; Email_SMTPServer = "mail.galva.co.id";

75
src/mqtt/MqttClient.java Normal file
View File

@@ -0,0 +1,75 @@
package mqtt;
import com.hivemq.client.mqtt.mqtt3.Mqtt3AsyncClient;
import com.hivemq.client.mqtt.mqtt3.Mqtt3Client;
import com.hivemq.client.mqtt.mqtt3.message.auth.Mqtt3SimpleAuth;
import org.tinylog.Logger;
import java.nio.charset.StandardCharsets;
import java.util.function.Consumer;
public class MqttClient {
Mqtt3AsyncClient client;
/**
* Constructor to create an MQTT client and connect to the broker.
*
* @param host The hostname of the MQTT broker.
* @param port The port of the MQTT broker.
* @param username The username for authentication.
* @param password The password for authentication.
* @param connectedCallback A callback that is called when the connection is established or fails.
*/
public MqttClient(String host, int port, String username, String password, Consumer<Boolean> connectedCallback) {
Mqtt3AsyncClient clientx = Mqtt3Client.builder()
.simpleAuth(Mqtt3SimpleAuth.builder().username(username).password(password.getBytes(StandardCharsets.UTF_8)).build())
.serverHost(host)
.serverPort(port)
.buildAsync();
clientx.connect()
.whenComplete((conn, throwable) -> {
if (throwable != null) {
client = null;
Logger.error("Failed to connect to MQTT broker at {}:{}, Message : {}", host, port, throwable.getMessage());
if (connectedCallback != null) {
connectedCallback.accept(false);
}
} else {
client = clientx;
Logger.info("Connected to MQTT broker at {}:{}", host, port);
if (connectedCallback != null) {
connectedCallback.accept(true);
}
}
});
}
public void Publish(String topic, String clientID, String message, Consumer<Boolean> publishCallback) {
if (client == null) {
Logger.error("Cannot publish message, MQTT client is not connected.");
if (publishCallback != null) {
publishCallback.accept(false);
}
return;
}
client.publishWith()
.topic(topic+"/" + clientID)
.payload(message.getBytes(StandardCharsets.UTF_8))
.send()
.whenComplete((pub, throwable) -> {
if (throwable != null) {
Logger.error("Failed to publish message to topic '{}', Message : {}", topic, throwable.getMessage());
if (publishCallback != null) {
publishCallback.accept(false);
}
} else {
Logger.info("Message published to topic '{}'", topic);
if (publishCallback != null) {
publishCallback.accept(true);
}
}
});
}
}