I may be stupid
This commit is contained in:
47
api/src/main/java/be/naaturel/boardmateapi/MqttStarter.java
Normal file
47
api/src/main/java/be/naaturel/boardmateapi/MqttStarter.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package be.naaturel.boardmateapi;
|
||||
|
||||
import be.naaturel.boardmateapi.common.helpers.Logger;
|
||||
import be.naaturel.boardmateapi.services.MqttService;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@Component
|
||||
public class MqttStarter {
|
||||
|
||||
private final MqttService service;
|
||||
|
||||
public MqttStarter(MqttService service){
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void start(){
|
||||
try {
|
||||
Logger.displayInfo("Broker initialized");
|
||||
setCallback();
|
||||
service.subscribe("/board-mate/+/telemetry");
|
||||
} catch (Exception e){
|
||||
System.err.println(Arrays.toString(e.getStackTrace()));
|
||||
}
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void stop() {
|
||||
service.disconnect();
|
||||
}
|
||||
|
||||
private void setCallback(){
|
||||
service.onConnectionLost((cause) -> {
|
||||
Logger.displayError("Connection lost: " + cause.getMessage());
|
||||
});
|
||||
|
||||
service.onMessageReceived((msg) -> {
|
||||
Logger.displayInfo("Received message on topic " + msg.getTopic() + ": " + msg.getContent());
|
||||
});
|
||||
|
||||
service.registerCallback();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user