Add telemetry registration
This commit is contained in:
@@ -2,20 +2,29 @@ package be.naaturel.boardmateapi;
|
||||
|
||||
import be.naaturel.boardmateapi.common.exceptions.ServiceException;
|
||||
import be.naaturel.boardmateapi.common.helpers.Logger;
|
||||
import be.naaturel.boardmateapi.common.models.TelemetryData;
|
||||
import be.naaturel.boardmateapi.services.MqttService;
|
||||
import be.naaturel.boardmateapi.services.TelemetryService;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class MqttStarter {
|
||||
|
||||
private final MqttService service;
|
||||
private final MqttService mqttService;
|
||||
private final TelemetryService telemetryService;
|
||||
|
||||
public MqttStarter(MqttService service){
|
||||
this.service = service;
|
||||
public MqttStarter(MqttService mqttService, TelemetryService telemetryService){
|
||||
this.mqttService = mqttService;
|
||||
this.telemetryService = telemetryService;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
@@ -23,7 +32,7 @@ public class MqttStarter {
|
||||
try {
|
||||
Logger.displayInfo("Broker initialized");
|
||||
setCallback();
|
||||
service.subscribe("/board-mate/+/telemetry");
|
||||
mqttService.subscribe("/board-mate/+/telemetry");
|
||||
} catch (Exception e){
|
||||
System.err.println(Arrays.toString(e.getStackTrace()));
|
||||
}
|
||||
@@ -32,21 +41,33 @@ public class MqttStarter {
|
||||
@PreDestroy
|
||||
public void stop() {
|
||||
try {
|
||||
service.disconnect();
|
||||
mqttService.disconnect();
|
||||
} catch (ServiceException se){
|
||||
Logger.displayError("Failed to disconnect : " + se.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void setCallback(){
|
||||
service.onConnectionLost((cause) -> {
|
||||
mqttService.onConnectionLost((cause) -> {
|
||||
Logger.displayError("Connection lost: " + cause.getMessage());
|
||||
});
|
||||
|
||||
service.onMessageReceived((msg) -> {
|
||||
mqttService.onMessageReceived((msg) -> {
|
||||
Logger.displayInfo("Received message on topic " + msg.getTopic() + ": " + msg.getContent());
|
||||
try{
|
||||
String[] topicParts = msg.getTopic().split("/");
|
||||
String clientId = topicParts[2];
|
||||
Map<String, Object> map = new ObjectMapper().readValue(msg.getContent(), new TypeReference<>() {});
|
||||
|
||||
TelemetryData tlm = new TelemetryData(clientId, map);
|
||||
telemetryService.insert(tlm);
|
||||
} catch (ServiceException se){
|
||||
Logger.displayError("Failed register data : " + se.getMessage());
|
||||
} catch (JsonProcessingException jpe) {
|
||||
Logger.displayError("Failed to parse received data : " + jpe.getMessage());
|
||||
}
|
||||
});
|
||||
|
||||
service.registerCallback();
|
||||
mqttService.registerCallback();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package be.naaturel.boardmateapi.common.models;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class TelemetryData {
|
||||
|
||||
private String clientId;
|
||||
|
||||
private Map<String, Object> data;
|
||||
|
||||
public TelemetryData(String clientId, Map<String, Object> data){
|
||||
this.clientId = clientId;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void setClientId(String clientId) {
|
||||
this.clientId = clientId;
|
||||
}
|
||||
|
||||
public String getClientId() {
|
||||
return clientId;
|
||||
}
|
||||
|
||||
public Map<String, Object> getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(Map<String, Object> data) {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package be.naaturel.boardmateapi.repository;
|
||||
|
||||
import be.naaturel.boardmateapi.repository.dtos.TelemetryDataDto;
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
public interface TelemetryRepo extends MongoRepository<TelemetryDataDto, String> {
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package be.naaturel.boardmateapi.repository.dtos;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
import org.springframework.data.mongodb.core.mapping.Field;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Document(collection = "telemetry")
|
||||
public class TelemetryDataDto {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
@Field("clientId")
|
||||
private String clientId;
|
||||
|
||||
@Field("data")
|
||||
private Map<String, Object> data;
|
||||
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getClientId() {
|
||||
return clientId;
|
||||
}
|
||||
|
||||
public void setClientId(String clientId) {
|
||||
this.clientId = clientId;
|
||||
}
|
||||
|
||||
public Map<String, Object> getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(Map<String, Object> data) {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package be.naaturel.boardmateapi.services;
|
||||
|
||||
import be.naaturel.boardmateapi.common.exceptions.ServiceException;
|
||||
import be.naaturel.boardmateapi.common.models.TelemetryData;
|
||||
import be.naaturel.boardmateapi.repository.TelemetryRepo;
|
||||
import be.naaturel.boardmateapi.repository.dtos.TelemetryDataDto;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class TelemetryService {
|
||||
|
||||
private final TelemetryRepo repo;
|
||||
|
||||
@Autowired
|
||||
public TelemetryService(TelemetryRepo repo){
|
||||
this.repo = repo;
|
||||
}
|
||||
|
||||
public void insert(TelemetryData telemetry) throws ServiceException {
|
||||
try{
|
||||
TelemetryDataDto dto = new TelemetryDataDto();
|
||||
dto.setClientId(telemetry.getClientId());
|
||||
dto.setData(telemetry.getData());
|
||||
repo.insert(dto);
|
||||
} catch (Exception e){
|
||||
throw new ServiceException("Failed to add telemetry data : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user