Skip to content

Commit

Permalink
Feature/adapt device measurement creation to avoid multiple tables (#122
Browse files Browse the repository at this point in the history
)
  • Loading branch information
saschadoemer authored Feb 20, 2024
1 parent 692006a commit c5b1b9e
Show file tree
Hide file tree
Showing 16 changed files with 29 additions and 442 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
<dependency>
<groupId>de.app.5gla</groupId>
<artifactId>fiware-integration-layer</artifactId>
<version>6.4.0</version>
<version>7.1.0</version>
<exclusions>
<!-- SLF4J NOP -->
<exclusion>
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/de/app/fivegla/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,6 @@ public ModelMapper modelMapper() {
return new ModelMapper();
}

/**
* Dependency injection for the device integration service.
*
* @return -
*/
@Bean
public DeviceIntegrationService deviceIntegrationService() {
return new DeviceIntegrationService(contextBrokerUrl, tenant);
}

/**
* Dependency injection for the device measurement integration service.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ default String getKey() {
*/
String fiwareDeviceIdPrefix();

/**
* Get the FIWARE device measurement ID prefix.
*
* @return the FIWARE device measurement ID prefix as a String
*/
String fiwareDeviceMeasurementIdPrefix();


/**
* Get the manufacturer of the device.
*
Expand Down
83 changes: 0 additions & 83 deletions src/main/java/de/app/fivegla/controller/DeviceController.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import de.app.fivegla.controller.dto.request.SentekDataLoggingRequest;
import de.app.fivegla.controller.dto.request.WeenatDataLoggingRequest;
import de.app.fivegla.controller.security.SecuredApiAccess;
import de.app.fivegla.fiware.DeviceIntegrationService;
import de.app.fivegla.integration.agvolution.AgvolutionFiwareIntegrationServiceWrapper;
import de.app.fivegla.integration.agvolution.AgvolutionSensorIntegrationService;
import de.app.fivegla.integration.sentek.SentekFiwareIntegrationServiceWrapper;
Expand All @@ -19,6 +18,7 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -33,6 +33,7 @@
@Slf4j
@RestController
@RequestMapping(BaseMappings.DEVICE_MEASUREMENT)
@RequiredArgsConstructor
public class DeviceMeasurementController implements SecuredApiAccess {

private final SentekSensorIntegrationService sentekSensorIntegrationService;
Expand All @@ -42,23 +43,6 @@ public class DeviceMeasurementController implements SecuredApiAccess {
private final AgvolutionSensorIntegrationService agvolutionSensorIntegrationService;
private final AgvolutionFiwareIntegrationServiceWrapper agvolutionFiwareIntegrationServiceWrapper;

private final DeviceIntegrationService deviceIntegrationService;

public DeviceMeasurementController(SentekSensorIntegrationService sentekSensorIntegrationService,
SentekFiwareIntegrationServiceWrapper sentekFiwareIntegrationServiceWrapper,
WeenatPlotIntegrationService weenatPlotIntegrationService,
WeenatFiwareIntegrationServiceWrapper weenatFiwareIntegrationServiceWrapper,
AgvolutionSensorIntegrationService agvolutionSensorIntegrationService,
AgvolutionFiwareIntegrationServiceWrapper agvolutionFiwareIntegrationServiceWrapper, DeviceIntegrationService deviceIntegrationService) {
this.sentekSensorIntegrationService = sentekSensorIntegrationService;
this.sentekFiwareIntegrationServiceWrapper = sentekFiwareIntegrationServiceWrapper;
this.weenatPlotIntegrationService = weenatPlotIntegrationService;
this.weenatFiwareIntegrationServiceWrapper = weenatFiwareIntegrationServiceWrapper;
this.agvolutionSensorIntegrationService = agvolutionSensorIntegrationService;
this.agvolutionFiwareIntegrationServiceWrapper = agvolutionFiwareIntegrationServiceWrapper;
this.deviceIntegrationService = deviceIntegrationService;
}

/**
* This method logs the Sentek data for a specific sensor.
*
Expand Down Expand Up @@ -88,13 +72,8 @@ public ResponseEntity<String> sentek(@PathVariable @Parameter(description = "The
sentekFiwareIntegrationServiceWrapper.persist(sensor, request.getReadings());
dataHasBeenLogged.set(true);
}, () -> {
log.warn("No sensor found for id {}.", sensorId);
log.info("Now looking for a generic sensor for id {}.", sensorId);
deviceIntegrationService.read(sentekFiwareIntegrationServiceWrapper.deviceIdOf(sensorId)).ifPresentOrElse(device -> {
log.info("Persisting {} measurements for sensor {}.", request.getReadings().size(), sensorId);
sentekFiwareIntegrationServiceWrapper.persist(device, request.getReadings());
dataHasBeenLogged.set(true);
}, () -> log.error("No sensor found for id {}.", sensorId));
log.warn("No SENTEK sensor found for id {}.", sensorId);
dataHasBeenLogged.set(false);
});

if (dataHasBeenLogged.get()) {
Expand Down Expand Up @@ -139,18 +118,10 @@ public ResponseEntity<String> weenat(@PathVariable @Parameter(description = "The
weenatFiwareIntegrationServiceWrapper.persist(plot, Measurements.builder().measurements(request.getMeasurements()).plot(plot).build());
dataHasBeenLogged.set(true);
}, () -> {
log.warn("No sensor found for id {}.", plotId);
log.info("Now looking for a generic sensor for id {}.", plotId);
deviceIntegrationService.read(weenatFiwareIntegrationServiceWrapper.deviceIdOf(plotId)).ifPresentOrElse(device -> {
log.info("Persisting {} measurements for plot {}.", request.getMeasurements().size(), plotId);
var plot = weenatPlotIntegrationService.plotFromDevice(device);
weenatFiwareIntegrationServiceWrapper.persist(plot, Measurements.builder()
.measurements(request.getMeasurements())
.plot(plot).build());
dataHasBeenLogged.set(true);
dataHasBeenLogged.set(true);
}, () -> log.error("No sensor found for id {}.", plotId));
log.warn("No WEENAT sensor found for id {}.", plotId);
dataHasBeenLogged.set(false);
});

if (dataHasBeenLogged.get()) {
return ResponseEntity.status(HttpStatus.CREATED).build();
} else {
Expand Down Expand Up @@ -193,13 +164,8 @@ public ResponseEntity<String> agvolution(@PathVariable @Parameter(description =
agvolutionFiwareIntegrationServiceWrapper.persist(request.getSeriesEntry());
dataHasBeenLogged.set(true);
}, () -> {
log.warn("No sensor found for id {}.", deviceId);
log.info("Now looking for a generic sensor for id {}.", deviceId);
deviceIntegrationService.read(agvolutionFiwareIntegrationServiceWrapper.deviceIdOf(deviceId)).ifPresentOrElse(device -> {
log.info("Persisting {} series entries for device {}.", request.getSeriesEntry().getTimeSeriesEntries().size(), deviceId);
agvolutionFiwareIntegrationServiceWrapper.persist(request.getSeriesEntry());
dataHasBeenLogged.set(true);
}, () -> log.error("No sensor found for id {}.", deviceId));
log.warn("No AGVOLUTION sensor found for id {}.", deviceId);
dataHasBeenLogged.set(false);
});

if (dataHasBeenLogged.get()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package de.app.fivegla.integration.agranimo;


import de.app.fivegla.api.FiwareDevicMeasurementeId;
import de.app.fivegla.api.FiwareDeviceId;
import de.app.fivegla.api.Format;
import de.app.fivegla.config.ApplicationConfiguration;
import de.app.fivegla.config.manufacturer.CommonManufacturerConfiguration;
import de.app.fivegla.fiware.DeviceIntegrationService;
import de.app.fivegla.fiware.DeviceMeasurementIntegrationService;
import de.app.fivegla.fiware.model.Device;
import de.app.fivegla.fiware.model.DeviceCategory;
import de.app.fivegla.fiware.model.DeviceMeasurement;
import de.app.fivegla.fiware.model.Location;
import de.app.fivegla.integration.agranimo.model.SoilMoisture;
Expand All @@ -29,41 +25,24 @@
@SuppressWarnings({"FieldCanBeLocal", "unused"})
public class AgranimoFiwareIntegrationServiceWrapper {
private final ApplicationConfiguration applicationConfiguration;
private final DeviceIntegrationService deviceIntegrationService;
private final DeviceMeasurementIntegrationService deviceMeasurementIntegrationService;

/**
* Persists the soil moisture measurement for a given zone.
*
* @param zone the zone associated with the soil moisture measurement
* @param zone the zone associated with the soil moisture measurement
* @param soilMoisture the soil moisture measurement to persist
*/
public void persist(Zone zone, SoilMoisture soilMoisture) {
persist(soilMoisture.getDeviceId(), zone.getData().getPoint().getCoordinates()[0], zone.getData().getPoint().getCoordinates()[1]);
var deviceMeasurement = createDeviceMeasurements(zone, soilMoisture);
log.info("Persisting measurement for device: {}", soilMoisture.getDeviceId());
deviceMeasurementIntegrationService.persist(deviceMeasurement);
}

private void persist(String deviceId, double latitude, double longitude) {
var device = Device.builder()
.id(FiwareDeviceId.create(getManufacturerConfiguration(), deviceId))
.manufacturerSpecificId(deviceId)
.deviceCategory(DeviceCategory.builder()
.value(List.of(getManufacturerConfiguration().getKey()))
.build())
.location(Location.builder()
.coordinates(List.of(latitude, longitude))
.build())
.build();
deviceIntegrationService.persist(device);
}

private DeviceMeasurement createDeviceMeasurements(Zone zone, SoilMoisture soilMoisture) {
log.debug("Persisting data for zone: {}", zone.getId());
return DeviceMeasurement.builder()
.id(FiwareDevicMeasurementeId.create(getManufacturerConfiguration()))
.refDevice(FiwareDeviceId.create(getManufacturerConfiguration(), String.valueOf(soilMoisture.getDeviceId())))
.id(deviceIdOf(soilMoisture.getDeviceId()))
.dateObserved(Format.format(soilMoisture.getTms()))
.location(Location.builder()
.coordinates(List.of(zone.getData().getPoint().getCoordinates()[0], zone.getData().getPoint().getCoordinates()[1]))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package de.app.fivegla.integration.agvolution;


import de.app.fivegla.api.FiwareDevicMeasurementeId;
import de.app.fivegla.api.FiwareDeviceId;
import de.app.fivegla.api.Format;
import de.app.fivegla.config.ApplicationConfiguration;
import de.app.fivegla.config.manufacturer.CommonManufacturerConfiguration;
import de.app.fivegla.fiware.DeviceIntegrationService;
import de.app.fivegla.fiware.DeviceMeasurementIntegrationService;
import de.app.fivegla.fiware.model.Device;
import de.app.fivegla.fiware.model.DeviceCategory;
import de.app.fivegla.fiware.model.DeviceMeasurement;
import de.app.fivegla.fiware.model.Location;
import de.app.fivegla.integration.agvolution.model.SeriesEntry;
Expand All @@ -29,12 +25,10 @@
@RequiredArgsConstructor
@SuppressWarnings({"FieldCanBeLocal", "unused"})
public class AgvolutionFiwareIntegrationServiceWrapper {
private final DeviceIntegrationService deviceIntegrationService;
private final DeviceMeasurementIntegrationService deviceMeasurementIntegrationService;
private final ApplicationConfiguration applicationConfiguration;

public void persist(SeriesEntry seriesEntry) {
persist(seriesEntry.getDeviceId(), seriesEntry.getLatitude(), seriesEntry.getLongitude());
seriesEntry.getTimeSeriesEntries().forEach(timeSeriesEntry -> {
var deviceMeasurements = createDeviceMeasurements(seriesEntry, timeSeriesEntry);
log.info("Persisting measurement for device: {}", seriesEntry.getDeviceId());
Expand All @@ -45,28 +39,13 @@ public void persist(SeriesEntry seriesEntry) {
});
}

private void persist(String deviceId, double latitude, double longitude) {
var device = Device.builder()
.id(FiwareDeviceId.create(getManufacturerConfiguration(), deviceId))
.manufacturerSpecificId(deviceId)
.deviceCategory(DeviceCategory.builder()
.value(List.of(getManufacturerConfiguration().getKey()))
.build())
.location(Location.builder()
.coordinates(List.of(latitude, longitude))
.build())
.build();
deviceIntegrationService.persist(device);
}

private List<DeviceMeasurement> createDeviceMeasurements(SeriesEntry seriesEntry, TimeSeriesEntry timeSeriesEntry) {
log.debug("Persisting data for device: {}", seriesEntry.getDeviceId());
log.debug("Persisting data: {}", timeSeriesEntry);
var deviceMeasurements = new ArrayList<DeviceMeasurement>();
timeSeriesEntry.getValues().forEach(timeSeriesValue -> {
var deviceMeasurement = DeviceMeasurement.builder()
.id(FiwareDevicMeasurementeId.create(getManufacturerConfiguration()))
.refDevice(FiwareDeviceId.create(getManufacturerConfiguration(), String.valueOf(seriesEntry.getDeviceId())))
.id(deviceIdOf(seriesEntry.getDeviceId()))
.dateObserved(Format.format(timeSeriesValue.getTime()))
.location(Location.builder()
.coordinates(List.of(seriesEntry.getLatitude(), seriesEntry.getLongitude()))
Expand Down
Loading

0 comments on commit c5b1b9e

Please sign in to comment.