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
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>de.ph87</groupId>
|
<groupId>de.ph87</groupId>
|
||||||
<artifactId>DataMulti</artifactId>
|
<artifactId>Data2025</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
@ -61,4 +61,14 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>Data2025</finalName>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
</project>
|
</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 GraphRepository graphRepository;
|
||||||
|
|
||||||
|
private final DemoConfig demoConfig;
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
@EventListener(ApplicationReadyEvent.class)
|
@EventListener(ApplicationReadyEvent.class)
|
||||||
public void init() {
|
public void init() {
|
||||||
|
if (!demoConfig.isEnabled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
topics();
|
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.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
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.MqttConnectOptions;
|
||||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||||
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
||||||
|
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@ -20,6 +24,8 @@ public class MqttService {
|
|||||||
|
|
||||||
private final Object lock = new Object();
|
private final Object lock = new Object();
|
||||||
|
|
||||||
|
private final MqttConfig config;
|
||||||
|
|
||||||
private boolean stop = false;
|
private boolean stop = false;
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
@ -50,15 +56,18 @@ public class MqttService {
|
|||||||
private void connectOnce() throws InterruptedException {
|
private void connectOnce() throws InterruptedException {
|
||||||
MqttClient client = null;
|
MqttClient client = null;
|
||||||
try {
|
try {
|
||||||
log.info("MQTT connecting...");
|
final boolean cleanSession = config.getClientId() == null || config.getClientId().isEmpty();
|
||||||
client = new MqttClient("tcp://10.0.0.50:1883", "DataDynamic", new MemoryPersistence());
|
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();
|
final MqttConnectOptions options = new MqttConnectOptions();
|
||||||
options.setAutomaticReconnect(false);
|
options.setAutomaticReconnect(false);
|
||||||
options.setCleanSession(true);
|
options.setCleanSession(cleanSession);
|
||||||
options.setConnectionTimeout(5);
|
options.setConnectionTimeout(5);
|
||||||
options.setKeepAliveInterval(2);
|
options.setKeepAliveInterval(2);
|
||||||
client.connect(options);
|
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.");
|
log.info("MQTT connected.");
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
while (!stop && client.isConnected()) {
|
while (!stop && client.isConnected()) {
|
||||||
@ -69,10 +78,13 @@ public class MqttService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (MqttException e) {
|
} catch (MqttException e) {
|
||||||
log.error("MQTT connection error: {}", e.getMessage());
|
log.error("MQTT error.", e);
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
lock.wait(3000);
|
lock.wait(3000);
|
||||||
}
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Unexpected error connecting MQTT.", e);
|
||||||
|
System.exit(1);
|
||||||
} finally {
|
} finally {
|
||||||
if (client != null && client.isConnected()) {
|
if (client != null && client.isConnected()) {
|
||||||
log.info("MQTT disconnecting...");
|
log.info("MQTT disconnecting...");
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user