-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/download openweather data during sensor data fetching (#226)
- Loading branch information
1 parent
3fdf7d6
commit 101b5f1
Showing
27 changed files
with
639 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,6 @@ public enum Manufacturer { | |
AGVOLUTION, | ||
SENSOTERRA, | ||
SENTEK, | ||
WEENAT | ||
WEENAT, | ||
OPEN_WEATHER | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/de/app/fivegla/business/RegisteredDevicesService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package de.app.fivegla.business; | ||
|
||
import de.app.fivegla.persistence.RegisteredDeviceRepository; | ||
import de.app.fivegla.persistence.entity.RegisteredDevice; | ||
import de.app.fivegla.persistence.entity.Tenant; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class RegisteredDevicesService { | ||
|
||
private final GroupService groupService; | ||
private final RegisteredDeviceRepository registeredDeviceRepository; | ||
|
||
/** | ||
* Registers a device. | ||
* | ||
* @param prefilledEntity The prefilled device entity to register. | ||
* @return The registered device entity. | ||
*/ | ||
public RegisteredDevice registerDevice(RegisteredDevice prefilledEntity, String groupId) { | ||
log.info("Registering device: {}", prefilledEntity); | ||
prefilledEntity.setOid(UUID.randomUUID().toString()); | ||
groupService.getOrDefault(prefilledEntity.getTenant(), groupId); | ||
return registeredDeviceRepository.save(prefilledEntity); | ||
} | ||
|
||
/** | ||
* Finds all registered devices for a tenant. | ||
* | ||
* @param tenant The tenant to find the registered devices for. | ||
* @return The list of registered devices. | ||
*/ | ||
public List<RegisteredDevice> findAll(Tenant tenant) { | ||
log.info("Finding all registered devices for tenant: {}", tenant); | ||
return registeredDeviceRepository.findAllByTenant(tenant); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/de/app/fivegla/controller/dto/request/RegisterDeviceRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package de.app.fivegla.controller.dto.request; | ||
|
||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Setter | ||
@Getter | ||
@Schema(description = "Request to register a device.") | ||
public class RegisterDeviceRequest { | ||
|
||
@NotBlank | ||
@Schema(description = "The name.", requiredMode = Schema.RequiredMode.REQUIRED) | ||
private String name; | ||
|
||
@Schema(description = "The description.") | ||
private String description; | ||
|
||
@Schema(description = "The longitude.", requiredMode = Schema.RequiredMode.REQUIRED) | ||
private double longitude; | ||
|
||
@Schema(description = "The latitude.", requiredMode = Schema.RequiredMode.REQUIRED) | ||
private double latitude; | ||
|
||
@Schema(description = "The id of the group, if not set, the default group will be used.") | ||
private String groupId; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/de/app/fivegla/controller/dto/response/RegisterDeviceResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package de.app.fivegla.controller.dto.response; | ||
|
||
import de.app.fivegla.api.Response; | ||
import de.app.fivegla.controller.dto.response.inner.RegisteredDevice; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
@Schema(description = "Response to register a device.") | ||
public class RegisterDeviceResponse extends Response { | ||
|
||
@Schema(description = "The registered device.") | ||
private RegisteredDevice registeredDevice; | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/de/app/fivegla/controller/dto/response/inner/RegisteredDevice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package de.app.fivegla.controller.dto.response.inner; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
/** | ||
* Image. | ||
*/ | ||
@Getter | ||
@Setter | ||
@Builder | ||
@Schema(description = "A registered device.") | ||
public class RegisteredDevice { | ||
|
||
/** | ||
* The oid. | ||
*/ | ||
@Schema(description = "The oid.") | ||
private String oid; | ||
|
||
/** | ||
* The name. | ||
*/ | ||
@Schema(description = "The name.") | ||
private String name; | ||
|
||
/** | ||
* The description. | ||
*/ | ||
@Schema(description = "The description.") | ||
private String description; | ||
|
||
/** | ||
* The location of the image. | ||
*/ | ||
@Schema(description = "The longitude.") | ||
private double longitude; | ||
|
||
/** | ||
* The location. | ||
*/ | ||
@Schema(description = "The latitude.") | ||
private double latitude; | ||
|
||
/** | ||
* The group. | ||
*/ | ||
@Schema(description = "The ID of the group.") | ||
private String groupId; | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/de/app/fivegla/controller/tenant/RegisteredDevicesController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package de.app.fivegla.controller.tenant; | ||
|
||
import de.app.fivegla.api.Response; | ||
import de.app.fivegla.business.RegisteredDevicesService; | ||
import de.app.fivegla.config.security.marker.TenantCredentialApiAccess; | ||
import de.app.fivegla.controller.api.BaseMappings; | ||
import de.app.fivegla.controller.dto.request.RegisterDeviceRequest; | ||
import de.app.fivegla.controller.dto.response.RegisterDeviceResponse; | ||
import de.app.fivegla.persistence.entity.RegisteredDevice; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping(BaseMappings.REGISTERED_DEVICES) | ||
public class RegisteredDevicesController implements TenantCredentialApiAccess { | ||
|
||
private final RegisteredDevicesService registeredDevicesService; | ||
|
||
@Operation( | ||
operationId = "registered-devices.register", | ||
description = "Registers a device.", | ||
tags = BaseMappings.REGISTERED_DEVICES | ||
) | ||
@ApiResponse( | ||
responseCode = "201", | ||
description = "The device was registered.", | ||
content = @Content( | ||
mediaType = MediaType.APPLICATION_JSON_VALUE, | ||
schema = @Schema(implementation = RegisterDeviceResponse.class) | ||
) | ||
) | ||
@ApiResponse( | ||
responseCode = "400", | ||
description = "The request is invalid.", | ||
content = @Content( | ||
mediaType = MediaType.APPLICATION_JSON_VALUE, | ||
schema = @Schema(implementation = Response.class) | ||
) | ||
) | ||
@PostMapping(value = "/register", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) | ||
public ResponseEntity<? extends Response> register(@Valid @RequestBody RegisterDeviceRequest request) { | ||
var registeredDevice = new RegisteredDevice(); | ||
registeredDevice.setName(request.getName()); | ||
registeredDevice.setDescription(request.getDescription()); | ||
registeredDevice.setLongitude(request.getLongitude()); | ||
registeredDevice.setLatitude(request.getLatitude()); | ||
var registeredAndSavedDevice = registeredDevicesService.registerDevice(registeredDevice, request.getGroupId()); | ||
return ResponseEntity.ok(RegisterDeviceResponse.builder() | ||
.registeredDevice(de.app.fivegla.controller.dto.response.inner.RegisteredDevice.builder() | ||
.oid(registeredAndSavedDevice.getOid()) | ||
.name(registeredAndSavedDevice.getName()) | ||
.description(registeredAndSavedDevice.getDescription()) | ||
.longitude(registeredAndSavedDevice.getLongitude()) | ||
.latitude(registeredAndSavedDevice.getLatitude()) | ||
.groupId(registeredAndSavedDevice.getGroup().getOid()) | ||
.build()) | ||
.build()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/de/app/fivegla/event/OpenWeatherImportEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package de.app.fivegla.event; | ||
|
||
import de.app.fivegla.event.events.OpenWeatherImportEvent; | ||
import de.app.fivegla.integration.openweather.OpenWeatherIntegrationService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class OpenWeatherImportEventHandler { | ||
|
||
public final OpenWeatherIntegrationService openWeatherIntegrationService; | ||
|
||
@EventListener(OpenWeatherImportEvent.class) | ||
public void handleOpenWeatherImportEvent(OpenWeatherImportEvent openWeatherImportEvent) { | ||
log.info("Handling OpenWeather import event for longitude {} and latitude {}.", openWeatherImportEvent.getLongitude(), openWeatherImportEvent.getLatitude()); | ||
var openWeatherData = openWeatherIntegrationService.fetchWeatherData(openWeatherImportEvent.getThirdPartyApiConfiguration().getApiToken(), openWeatherImportEvent.getLatitude(), openWeatherImportEvent.getLongitude()); | ||
log.info("Successfully imported weather data from OpenWeather for longitude {} and latitude {}.", openWeatherImportEvent.getLongitude(), openWeatherImportEvent.getLatitude()); | ||
log.debug("OpenWeather data: {}", openWeatherData); | ||
} | ||
|
||
} |
15 changes: 12 additions & 3 deletions
15
src/main/java/de/app/fivegla/event/events/DataImportEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,18 @@ | ||
package de.app.fivegla.event.events; | ||
|
||
import lombok.Getter; | ||
import org.springframework.context.ApplicationEvent; | ||
|
||
/** | ||
* Event for data import. | ||
* | ||
* @param thirdPartyApiConfigurationId The ID of the third party API configuration. | ||
*/ | ||
public record DataImportEvent(Long thirdPartyApiConfigurationId) { | ||
@Getter | ||
public class DataImportEvent extends ApplicationEvent { | ||
|
||
private final Long thirdPartyApiConfigurationId; | ||
|
||
public DataImportEvent(Object source, Long thirdPartyApiConfigurationId) { | ||
super(source); | ||
this.thirdPartyApiConfigurationId = thirdPartyApiConfigurationId; | ||
} | ||
} |
Oops, something went wrong.