first test deployment on server
This commit is contained in:
parent
d65628a7d6
commit
4a47c890f7
@ -8,4 +8,4 @@ spring.jpa.hibernate.ddl-auto=update
|
||||
#-
|
||||
spring.jackson.serialization.indent_output=true
|
||||
#-
|
||||
de.ph87.knx.mqtt.uri=tcp://10.0.0.50:1883
|
||||
de.ph87.knx.mqtt.uri=tcp://10.255.0.1:1883
|
||||
|
||||
12
pom.xml
12
pom.xml
@ -5,7 +5,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>de.ph87</groupId>
|
||||
<artifactId>DataMulti</artifactId>
|
||||
<artifactId>Data2025</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
@ -61,4 +61,14 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>Data2025</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
14
src/main/java/de/ph87/data/DemoConfig.java
Normal file
14
src/main/java/de/ph87/data/DemoConfig.java
Normal file
@ -0,0 +1,14 @@
|
||||
package de.ph87.data;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "de.ph87.data.demo")
|
||||
public class DemoConfig {
|
||||
|
||||
private boolean enabled = false;
|
||||
|
||||
}
|
||||
@ -39,9 +39,14 @@ public class DemoService {
|
||||
|
||||
private final GraphRepository graphRepository;
|
||||
|
||||
private final DemoConfig demoConfig;
|
||||
|
||||
@Transactional
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void init() {
|
||||
if (!demoConfig.isEnabled()) {
|
||||
return;
|
||||
}
|
||||
topics();
|
||||
}
|
||||
|
||||
|
||||
18
src/main/java/de/ph87/data/mqtt/MqttConfig.java
Normal file
18
src/main/java/de/ph87/data/mqtt/MqttConfig.java
Normal file
@ -0,0 +1,18 @@
|
||||
package de.ph87.data.mqtt;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "de.ph87.data.mqtt")
|
||||
public class MqttConfig {
|
||||
|
||||
private String clientId;
|
||||
|
||||
private String uri;
|
||||
|
||||
private String topic = "#";
|
||||
|
||||
}
|
||||
@ -6,11 +6,15 @@ import jakarta.annotation.PreDestroy;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttClientPersistence;
|
||||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
||||
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@ -20,6 +24,8 @@ public class MqttService {
|
||||
|
||||
private final Object lock = new Object();
|
||||
|
||||
private final MqttConfig config;
|
||||
|
||||
private boolean stop = false;
|
||||
|
||||
@PostConstruct
|
||||
@ -50,15 +56,18 @@ public class MqttService {
|
||||
private void connectOnce() throws InterruptedException {
|
||||
MqttClient client = null;
|
||||
try {
|
||||
log.info("MQTT connecting...");
|
||||
client = new MqttClient("tcp://10.0.0.50:1883", "DataDynamic", new MemoryPersistence());
|
||||
final boolean cleanSession = config.getClientId() == null || config.getClientId().isEmpty();
|
||||
final String clientId = cleanSession ? "Data2025-TMP-" + UUID.randomUUID() : config.getClientId();
|
||||
final MqttClientPersistence persistence = cleanSession ? new MemoryPersistence() : new MqttDefaultFilePersistence();
|
||||
log.info("MQTT connecting {} as {}{}...", config.getUri(), clientId, cleanSession ? " (CLEAN SESSION)" : "");
|
||||
client = new MqttClient(config.getUri(), clientId, persistence);
|
||||
final MqttConnectOptions options = new MqttConnectOptions();
|
||||
options.setAutomaticReconnect(false);
|
||||
options.setCleanSession(true);
|
||||
options.setCleanSession(cleanSession);
|
||||
options.setConnectionTimeout(5);
|
||||
options.setKeepAliveInterval(2);
|
||||
client.connect(options);
|
||||
client.subscribe("#", (topic, message) -> topicReceiver.receive(new MqttInbound(topic, new String(message.getPayload()))));
|
||||
client.subscribe(config.getTopic(), 2, (topic, message) -> topicReceiver.receive(new MqttInbound(topic, new String(message.getPayload()))));
|
||||
log.info("MQTT connected.");
|
||||
synchronized (lock) {
|
||||
while (!stop && client.isConnected()) {
|
||||
@ -69,10 +78,13 @@ public class MqttService {
|
||||
}
|
||||
}
|
||||
} catch (MqttException e) {
|
||||
log.error("MQTT connection error: {}", e.getMessage());
|
||||
log.error("MQTT error.", e);
|
||||
synchronized (lock) {
|
||||
lock.wait(3000);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Unexpected error connecting MQTT.", e);
|
||||
System.exit(1);
|
||||
} finally {
|
||||
if (client != null && client.isConnected()) {
|
||||
log.info("MQTT disconnecting...");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user