Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/fix wrong tx handling after creating thirdpartyconfig #210

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/de/app/fivegla/api/Error.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public enum Error {
TRYING_TO_UPDATE_GROUP_FROM_ANOTHER_TENANT(errorOf(37)),
TRYING_TO_ACCESS_GROUP_FROM_ANOTHER_TENANT(errorOf(38)),
TRYING_TO_DELETE_GROUP_FROM_ANOTHER_TENANT(errorOf(39)),
DEFAULT_GROUP_FOR_TENANT_NOT_FOUND(errorOf(40));
DEFAULT_GROUP_FOR_TENANT_NOT_FOUND(errorOf(40)),
THIRD_PARTY_API_CONFIGURATION_NOT_FOUND(errorOf(41));

private static String errorOf(int i) {
return ERR_ + String.format("%05d", i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
Expand Down Expand Up @@ -74,4 +75,14 @@ public void updateLastRun(ThirdPartyApiConfiguration thirdPartyApiConfiguration)
thirdPartyApiConfiguration.setLastRun(Date.from(Instant.now()));
thirdPartyApiConfigurationRepository.save(thirdPartyApiConfiguration);
}

/**
* Finds a ThirdPartyApiConfiguration by its ID.
*
* @param id The ID of the ThirdPartyApiConfiguration.
* @return An Optional containing the ThirdPartyApiConfiguration with the given ID, or an empty Optional if not found.
*/
public Optional<ThirdPartyApiConfiguration> findById(Long id) {
return thirdPartyApiConfigurationRepository.findById(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public ResponseEntity<? extends Response> createThirdPartyApiConfiguration(@Vali
var thirdPartyApiConfiguration = request.toEntity();
thirdPartyApiConfiguration.setTenant(tenant);
var thirdPartyApiConfigurationCreated = thirdPartyApiConfigurationService.createThirdPartyApiConfiguration(thirdPartyApiConfiguration);
applicationEventPublisher.publishEvent(new DataImportEvent(thirdPartyApiConfigurationCreated));
applicationEventPublisher.publishEvent(new DataImportEvent(thirdPartyApiConfigurationCreated.getId()));
var response = CreateThirdPartyApiConfigurationResponse.builder()
.thirdPartyApiConfiguration(ThirdPartyApiConfiguration.builder()
.tenantId(thirdPartyApiConfigurationCreated.getTenant().getTenantId())
Expand Down
47 changes: 20 additions & 27 deletions src/main/java/de/app/fivegla/event/DataImportEventHandler.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package de.app.fivegla.event;

import de.app.fivegla.api.SubscriptionStatus;
import de.app.fivegla.api.enums.EntityType;
import de.app.fivegla.api.Error;
import de.app.fivegla.api.ErrorMessage;
import de.app.fivegla.api.exceptions.BusinessException;
import de.app.fivegla.business.TenantService;
import de.app.fivegla.business.ThirdPartyApiConfigurationService;
import de.app.fivegla.event.events.DataImportEvent;
import de.app.fivegla.integration.agranimo.AgranimoMeasurementImport;
import de.app.fivegla.integration.agvolution.AgvolutionMeasurementImport;
import de.app.fivegla.integration.farm21.Farm21MeasurementImport;
import de.app.fivegla.integration.fiware.SubscriptionIntegrationService;
import de.app.fivegla.integration.sensoterra.SensoterraMeasurementImport;
import de.app.fivegla.integration.sentek.SentekMeasurementImport;
import de.app.fivegla.integration.soilscout.SoilScoutMeasurementImport;
Expand All @@ -33,43 +33,36 @@ public class DataImportEventHandler {
private final SensoterraMeasurementImport sensoterraMeasurementImport;
private final SentekMeasurementImport sentekMeasurementImport;
private final WeenatMeasurementImport weenatMeasurementImport;
private final SubscriptionStatus subscriptionStatus;
private final TenantService tenantService;
private final SubscriptionIntegrationService subscriptionService;
private final ThirdPartyApiConfigurationService thirdPartyApiConfigurationService;

@EventListener(DataImportEvent.class)
public void handleDataImportEvent(DataImportEvent dataImportEvent) {
log.info("Handling data import event for tenant {} and manufacturer {}.", dataImportEvent.thirdPartyApiConfiguration().getTenant().getTenantId(), dataImportEvent.thirdPartyApiConfiguration().getManufacturer());
var manufacturer = dataImportEvent.thirdPartyApiConfiguration().getManufacturer();
var tenantId = dataImportEvent.thirdPartyApiConfiguration().getTenant().getTenantId();
var thirdPartyApiConfiguration = thirdPartyApiConfigurationService.findById(dataImportEvent.thirdPartyApiConfigurationId())
.orElseThrow(() -> new BusinessException(ErrorMessage.builder()
.error(Error.THIRD_PARTY_API_CONFIGURATION_NOT_FOUND)
.message("Third party API configuration not found.")
.build()));
log.info("Handling data import event for tenant {} and manufacturer {}.", thirdPartyApiConfiguration.getTenant().getTenantId(), thirdPartyApiConfiguration.getManufacturer());
var manufacturer = thirdPartyApiConfiguration.getManufacturer();
var tenantId = thirdPartyApiConfiguration.getTenant().getTenantId();
var optionalTenant = tenantService.findByTenantId(tenantId);
if (optionalTenant.isEmpty()) {
log.error("Tenant with id {} not found, not able to handle data import event", tenantId);
} else {
var tenant = optionalTenant.get();
var config = dataImportEvent.thirdPartyApiConfiguration();
if (subscriptionStatus.sendOutSubscriptions(tenantId)) {
try {
subscriptionService.subscribe(tenant, EntityType.values());
log.info("Subscribed to device measurement notifications.");
subscriptionStatus.subscriptionSent(tenantId);
} catch (BusinessException e) {
log.error("Could not subscribe to device measurement notifications.", e);
}
} else {
log.info("Subscriptions are disabled. Not subscribing to device measurement notifications.");
}
switch (manufacturer) {
case SOILSCOUT -> soilScoutScheduledMeasurementImport.run(tenant, config);
case AGVOLUTION -> agvolutionMeasurementImport.run(tenant, config);
case AGRANIMO -> agranimoMeasurementImport.run(tenant, config);
case FARM21 -> farm21MeasurementImport.run(tenant, config);
case SENSOTERRA -> sensoterraMeasurementImport.run(tenant, config);
case SENTEK -> sentekMeasurementImport.run(tenant, config);
case WEENAT -> weenatMeasurementImport.run(tenant, config);
case SOILSCOUT -> soilScoutScheduledMeasurementImport.run(tenant, thirdPartyApiConfiguration);
case AGVOLUTION -> agvolutionMeasurementImport.run(tenant, thirdPartyApiConfiguration);
case AGRANIMO -> agranimoMeasurementImport.run(tenant, thirdPartyApiConfiguration);
case FARM21 -> farm21MeasurementImport.run(tenant, thirdPartyApiConfiguration);
case SENSOTERRA -> sensoterraMeasurementImport.run(tenant, thirdPartyApiConfiguration);
case SENTEK -> sentekMeasurementImport.run(tenant, thirdPartyApiConfiguration);
case WEENAT -> weenatMeasurementImport.run(tenant, thirdPartyApiConfiguration);
default -> throw new IllegalArgumentException("Unknown manufacturer: " + manufacturer);
}
}
thirdPartyApiConfigurationService.updateLastRun(thirdPartyApiConfiguration);
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package de.app.fivegla.event.events;

import de.app.fivegla.persistence.entity.ThirdPartyApiConfiguration;

/**
* Event for data import.
*
* @param thirdPartyApiConfiguration The third-party API configuration.
* @param thirdPartyApiConfigurationId The ID of the third party API configuration.
*/
public record DataImportEvent(ThirdPartyApiConfiguration thirdPartyApiConfiguration) {
public record DataImportEvent(Long thirdPartyApiConfigurationId) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import de.app.fivegla.integration.agranimo.model.Zone;
import de.app.fivegla.integration.fiware.FiwareEntityIntegrationService;
import de.app.fivegla.integration.fiware.model.DeviceMeasurement;
import de.app.fivegla.integration.fiware.model.internal.InstantAttribute;
import de.app.fivegla.integration.fiware.model.internal.EmptyAttribute;
import de.app.fivegla.integration.fiware.model.internal.InstantAttribute;
import de.app.fivegla.integration.fiware.model.internal.NumberAttribute;
import de.app.fivegla.integration.fiware.model.internal.TextAttribute;
import de.app.fivegla.persistence.entity.Tenant;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.app.fivegla.integration.agranimo;

import de.app.fivegla.api.Manufacturer;
import de.app.fivegla.business.ThirdPartyApiConfigurationService;
import de.app.fivegla.integration.agranimo.model.SoilMoisture;
import de.app.fivegla.integration.agranimo.model.Zone;
import de.app.fivegla.monitoring.JobMonitor;
Expand All @@ -24,7 +23,6 @@
@RequiredArgsConstructor
public class AgranimoMeasurementImport {

private final ThirdPartyApiConfigurationService thirdPartyApiConfigurationService;
private final AgranimoFiwareIntegrationServiceWrapper fiwareIntegrationServiceWrapper;
private final AgranimoSoilMoistureIntegrationService agranimoSoilMoistureIntegrationService;
private final AgranimoZoneService agranimoZoneService;
Expand Down Expand Up @@ -61,7 +59,6 @@ public void run(Tenant tenant, ThirdPartyApiConfiguration thirdPartyApiConfigura
);
});
}
thirdPartyApiConfigurationService.updateLastRun(thirdPartyApiConfiguration);
} catch (Exception e) {
log.error("Error while running scheduled data import from Agranimo API", e);
jobMonitor.logErrorDuringExecution(Manufacturer.AGRANIMO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import de.app.fivegla.integration.agvolution.model.TimeSeriesEntry;
import de.app.fivegla.integration.fiware.FiwareEntityIntegrationService;
import de.app.fivegla.integration.fiware.model.DeviceMeasurement;
import de.app.fivegla.integration.fiware.model.internal.InstantAttribute;
import de.app.fivegla.integration.fiware.model.internal.EmptyAttribute;
import de.app.fivegla.integration.fiware.model.internal.InstantAttribute;
import de.app.fivegla.integration.fiware.model.internal.NumberAttribute;
import de.app.fivegla.integration.fiware.model.internal.TextAttribute;
import de.app.fivegla.persistence.entity.Group;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.app.fivegla.integration.agvolution;

import de.app.fivegla.api.Manufacturer;
import de.app.fivegla.business.ThirdPartyApiConfigurationService;
import de.app.fivegla.integration.agvolution.model.SeriesEntry;
import de.app.fivegla.monitoring.JobMonitor;
import de.app.fivegla.persistence.entity.Tenant;
Expand All @@ -24,7 +23,6 @@
public class AgvolutionMeasurementImport {

private final AgvolutionSensorDataIntegrationService agvolutionSensorDataIntegrationService;
private final ThirdPartyApiConfigurationService thirdPartyApiConfigurationService;
private final AgvolutionFiwareIntegrationServiceWrapper agvolutionFiwareIntegrationServiceWrapper;
private final JobMonitor jobMonitor;

Expand Down Expand Up @@ -53,7 +51,6 @@ public void run(Tenant tenant, ThirdPartyApiConfiguration thirdPartyApiConfigura
jobMonitor.logNrOfEntitiesFetched(Manufacturer.AGVOLUTION, seriesEntries.size());
seriesEntries.forEach(seriesEntry -> persistDataWithinFiware(tenant, seriesEntry));
}
thirdPartyApiConfigurationService.updateLastRun(thirdPartyApiConfiguration);
} catch (Exception e) {
log.error("Error while running scheduled data import from Agvolution API", e);
jobMonitor.logErrorDuringExecution(Manufacturer.AGVOLUTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import de.app.fivegla.integration.farm21.model.SensorData;
import de.app.fivegla.integration.fiware.FiwareEntityIntegrationService;
import de.app.fivegla.integration.fiware.model.DeviceMeasurement;
import de.app.fivegla.integration.fiware.model.internal.InstantAttribute;
import de.app.fivegla.integration.fiware.model.internal.EmptyAttribute;
import de.app.fivegla.integration.fiware.model.internal.InstantAttribute;
import de.app.fivegla.integration.fiware.model.internal.NumberAttribute;
import de.app.fivegla.integration.fiware.model.internal.TextAttribute;
import de.app.fivegla.persistence.entity.Tenant;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.app.fivegla.integration.farm21;

import de.app.fivegla.api.Manufacturer;
import de.app.fivegla.business.ThirdPartyApiConfigurationService;
import de.app.fivegla.integration.farm21.model.Sensor;
import de.app.fivegla.integration.farm21.model.SensorData;
import de.app.fivegla.monitoring.JobMonitor;
Expand All @@ -27,7 +26,6 @@
public class Farm21MeasurementImport {

private final Farm21SensorDataIntegrationService farm21SensorDataIntegrationService;
private final ThirdPartyApiConfigurationService thirdPartyApiConfigurationService;
private final Farm21FiwareIntegrationServiceWrapper farm21FiwareIntegrationServiceWrapper;
private final JobMonitor jobMonitor;

Expand Down Expand Up @@ -56,7 +54,6 @@ public void run(Tenant tenant, ThirdPartyApiConfiguration thirdPartyApiConfigura
jobMonitor.logNrOfEntitiesFetched(Manufacturer.FARM21, measurements.size());
measurements.entrySet().forEach(sensorListEntry -> persistDataWithinFiware(tenant, sensorListEntry));
}
thirdPartyApiConfigurationService.updateLastRun(thirdPartyApiConfiguration);
} catch (Exception e) {
log.error("Error while running scheduled data import from Farm21 API", e);
jobMonitor.logErrorDuringExecution(Manufacturer.FARM21);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public record AgriCrop(
public String asJson() {
validate();
var json = "{" +
" \"id\":\"" + id + "\"," +
" \"type\":\"" + type + "\"," +
" \"customGroup\":" + group.asJson() + "," +
" \"dateCreated\":" + dateCreated.asJson() + "," +
" \"coordinates\":" + coordinatesAsJson(coordinates) +
" \"id\":\"" + id.trim() + "\"," +
" \"type\":\"" + type.trim() + "\"," +
" \"customGroup\":" + group.asJson().trim() + "," +
" \"dateCreated\":" + dateCreated.asJson().trim() + "," +
" \"coordinates\":" + coordinatesAsJson(coordinates).trim() +
"}";
log.debug("{} as JSON: {}", this.getClass().getSimpleName(), json);
return json;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public record DeviceMeasurement(
public String asJson() {
validate();
var json = "{" +
" \"id\":\"" + id + "\"," +
" \"type\":\"" + type + "\"," +
" \"customGroup\":" + group.asJson() + "," +
" \"name\":" + name.asJson() + "," +
" \"controlledProperty\":" + controlledProperty.asJson() + "," +
" \"externalDataReference\":" + externalDataReference.asJson() + "," +
" \"dateCreated\":" + dateCreated.asJson() + "," +
" \"location\":" + locationAsJson(latitude, longitude) +
" \"id\":\"" + id.trim() + "\"," +
" \"type\":\"" + type.trim() + "\"," +
" \"customGroup\":" + group.asJson().trim() + "," +
" \"name\":" + name.asJson().trim() + "," +
" \"controlledProperty\":" + controlledProperty.asJson().trim() + "," +
" \"externalDataReference\":" + externalDataReference.asJson().trim() + "," +
" \"dateCreated\":" + dateCreated.asJson().trim() + "," +
" \"location\":" + locationAsJson(latitude, longitude).trim() +
"}";
log.debug("{} as JSON: {}", this.getClass().getSimpleName(), json);
return json;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public record DevicePosition(
public String asJson() {
validate();
var json = "{" +
" \"id\":\"" + id + "\"," +
" \"type\":\"" + type + "\"," +
" \"customGroup\":" + group.asJson() + "," +
" \"transactionId\":" + transactionId.asJson() + "," +
" \"deviceId\":" + deviceId.asJson() + "," +
" \"dateCreated\":" + dateCreated.asJson() + "," +
" \"location\":" + locationAsJson(latitude, longitude) +
" \"id\":\"" + id.trim() + "\"," +
" \"type\":\"" + type.trim() + "\"," +
" \"customGroup\":" + group.asJson().trim() + "," +
" \"transactionId\":" + transactionId.asJson().trim() + "," +
" \"deviceId\":" + deviceId.asJson().trim() + "," +
" \"dateCreated\":" + dateCreated.asJson().trim() + "," +
" \"location\":" + locationAsJson(latitude, longitude).trim() +
"}";
log.debug("{} as JSON: {}", this.getClass().getSimpleName(), json);
return json;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import de.app.fivegla.business.GroupService;
import de.app.fivegla.integration.fiware.FiwareEntityIntegrationService;
import de.app.fivegla.integration.fiware.model.DeviceMeasurement;
import de.app.fivegla.integration.fiware.model.internal.InstantAttribute;
import de.app.fivegla.integration.fiware.model.internal.EmptyAttribute;
import de.app.fivegla.integration.fiware.model.internal.InstantAttribute;
import de.app.fivegla.integration.fiware.model.internal.NumberAttribute;
import de.app.fivegla.integration.fiware.model.internal.TextAttribute;
import de.app.fivegla.integration.sensoterra.model.Probe;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.app.fivegla.integration.sensoterra;

import de.app.fivegla.api.Manufacturer;
import de.app.fivegla.business.ThirdPartyApiConfigurationService;
import de.app.fivegla.integration.sensoterra.model.Probe;
import de.app.fivegla.integration.sensoterra.model.ProbeData;
import de.app.fivegla.monitoring.JobMonitor;
Expand All @@ -27,7 +26,6 @@
public class SensoterraMeasurementImport {

private final ProbeDataIntegrationService probeDataIntegrationService;
private final ThirdPartyApiConfigurationService thirdPartyApiConfigurationService;
private final SensoterraFiwareIntegrationServiceWrapper sensoterraFiwareIntegrationServiceWrapper;
private final JobMonitor jobMonitor;

Expand Down Expand Up @@ -56,7 +54,6 @@ public void run(Tenant tenant, ThirdPartyApiConfiguration thirdPartyApiConfigura
jobMonitor.logNrOfEntitiesFetched(Manufacturer.SENSOTERRA, seriesEntries.size());
seriesEntries.entrySet().forEach(probeListEntry -> persistDataWithinFiware(tenant, probeListEntry));
}
thirdPartyApiConfigurationService.updateLastRun(thirdPartyApiConfiguration);
} catch (Exception e) {
log.error("Error while running scheduled data import from Sensoterra API", e);
jobMonitor.logErrorDuringExecution(Manufacturer.SENSOTERRA);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import de.app.fivegla.business.GroupService;
import de.app.fivegla.integration.fiware.FiwareEntityIntegrationService;
import de.app.fivegla.integration.fiware.model.DeviceMeasurement;
import de.app.fivegla.integration.fiware.model.internal.InstantAttribute;
import de.app.fivegla.integration.fiware.model.internal.EmptyAttribute;
import de.app.fivegla.integration.fiware.model.internal.InstantAttribute;
import de.app.fivegla.integration.fiware.model.internal.NumberAttribute;
import de.app.fivegla.integration.fiware.model.internal.TextAttribute;
import de.app.fivegla.integration.sentek.model.csv.Reading;
Expand Down
Loading