Skip to content

Commit

Permalink
Refactor PrecipitationController to WeatherDataController
Browse files Browse the repository at this point in the history
Renamed PrecipitationController to WeatherDataController to accommodate broader weather data handling. Introduced ManualWeatherDataService and ManualWeatherDataEvent for managing various types of weather data. Modified BaseMappings and related DTOs to support the new weather data features.
  • Loading branch information
saschadoemer committed Aug 19, 2024
1 parent 8e7effc commit 87a20a7
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 67 deletions.
3 changes: 2 additions & 1 deletion src/main/java/de/app/fivegla/api/enums/EntityType.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public enum EntityType {
AGRI_CROP("AgriCrop"),
OPEN_WEATHER_MAP("OpenWeatherMap"),
MANUAL_PRECIPITATION_EVENT("ManualPrecipitationEvent"),
UNKNOWN("Unknown");
UNKNOWN("Unknown"),
MANUAL_WEATHER_DATA_EVENT("ManualWeatherDataEvent");

private final String key;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import de.app.fivegla.api.enums.EntityType;
import de.app.fivegla.controller.dto.request.PrecipitationRequest;
import de.app.fivegla.controller.dto.request.WeatherDataRequest;
import de.app.fivegla.integration.fiware.FiwareEntityIntegrationService;
import de.app.fivegla.integration.fiware.model.ManualPrecipitationEvent;
import de.app.fivegla.integration.fiware.model.ManualWeatherDataEvent;
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 All @@ -16,7 +18,7 @@
@Slf4j
@Service
@RequiredArgsConstructor
public class PrecipitationService {
public class ManualWeatherDataService {

private final GroupService groupService;
private final FiwareEntityIntegrationService fiwareEntityIntegrationService;
Expand All @@ -27,7 +29,7 @@ public class PrecipitationService {
* @param tenant The tenant
* @param request The request
*/
public void addManualPrecipitationEvent(Tenant tenant, PrecipitationRequest request) {
public void addPrecipitationEvent(Tenant tenant, PrecipitationRequest request) {
var group = groupService.getOrDefault(tenant, request.getGroupId());
var manualPrecipitationEvent = new ManualPrecipitationEvent(
UUID.randomUUID().toString(),
Expand All @@ -43,4 +45,32 @@ public void addManualPrecipitationEvent(Tenant tenant, PrecipitationRequest requ
fiwareEntityIntegrationService.persist(tenant, group, manualPrecipitationEvent);
}

/**
* Add a manual weather data.
*
* @param tenant The tenant
* @param request The request
*/
public void addManualWeatherData(Tenant tenant, WeatherDataRequest request) {
var group = groupService.getOrDefault(tenant, request.getGroupId());
var manualWeatherDataEvent = new ManualWeatherDataEvent(
UUID.randomUUID().toString(),
EntityType.MANUAL_WEATHER_DATA_EVENT.getKey(),
new TextAttribute(group.getOid()),
new TextAttribute(request.getDateCreated().toString()),
request.getLatitude(),
request.getLongitude(),
new NumberAttribute(request.getTemp()),
new NumberAttribute(request.getHumidity()),
new NumberAttribute(request.getPrecipitation()),
new NumberAttribute(request.getAirPressure()),
new NumberAttribute(request.getUvIndex()),
new NumberAttribute(request.getSolarRadiation()),
new NumberAttribute(request.getVisibility()),
new NumberAttribute(request.getDewPoint()),
new NumberAttribute(request.getFeelsLike()),
new NumberAttribute(request.getHeatIndex())
);
fiwareEntityIntegrationService.persist(tenant, group, manualWeatherDataEvent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ public class BaseMappings {
public static final String SUBSCRIPTION = SECURED_BY_TENANT + "/subscription";
public static final String REGISTERED_DEVICES = SECURED_BY_TENANT + "/registered-devices";
public static final String OPEN_WEATHER = SECURED_BY_TENANT + "/open-weather";
public static final String PRECIPITATION = SECURED_BY_TENANT + "/precipitation";
public static final String WEATHER_DATA = SECURED_BY_TENANT + "/weather-data";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package de.app.fivegla.controller.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;

import java.util.Date;

/**
* Represents a request to add a weather data.
*/
@Getter
@Setter
@Schema(description = "Represents a request to add a weather data.")
public class WeatherDataRequest {

@Schema(description = "The group id.")
private String groupId;

@NotNull
@Schema(description = "The date the precipitation was created.")
private Date dateCreated;

@Schema(description = "The latitude.")
private double latitude;

@Schema(description = "The longitude.")
private double longitude;

@Schema(description = "The temperature.")
private double temp;

@Schema(description = "The humidity.")
private double humidity;

@Schema(description = "The precipitation.")
private double precipitation;

@Schema(description = "The air pressure.")
private double airPressure;

@Schema(description = "The UV index.")
private double uvIndex;

@Schema(description = "The solar radiation.")
private double solarRadiation;

@Schema(description = "The visibility.")
private double visibility;

@Schema(description = "The dew point.")
private double dewPoint;

@Schema(description = "The feels like.")
private double feelsLike;

@Schema(description = "The heat index.")
private double heatIndex;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package de.app.fivegla.controller.tenant;

import de.app.fivegla.api.Response;
import de.app.fivegla.business.ManualWeatherDataService;
import de.app.fivegla.business.TenantService;
import de.app.fivegla.config.security.marker.TenantCredentialApiAccess;
import de.app.fivegla.controller.api.BaseMappings;
import de.app.fivegla.controller.dto.request.PrecipitationRequest;
import de.app.fivegla.controller.dto.request.WeatherDataRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
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;

import java.security.Principal;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(BaseMappings.WEATHER_DATA)
public class WeatherDataController implements TenantCredentialApiAccess {

private final TenantService tenantService;
private final ManualWeatherDataService manualWeatherDataService;

@Operation(
operationId = "weather-data.add-manual",
description = "Add a manual precipitation measurement.",
tags = BaseMappings.WEATHER_DATA
)
@ApiResponse(
responseCode = "200",
description = "The precipitation measurement was added.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = Response.class)
)
)
@ApiResponse(
responseCode = "400",
description = "The request is invalid.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = Response.class)
)
)
@PostMapping(value = "/precipitation/manual", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<? extends Response> addManualPrecipitation(@Parameter(description = "The request to add a precipitation measurement.") @Valid @RequestBody PrecipitationRequest request, Principal principal) {
var tenant = validateTenant(tenantService, principal);
manualWeatherDataService.addPrecipitationEvent(tenant, request);
return ResponseEntity.ok(new Response());
}

@Operation(
operationId = "weather-data.add-manual",
description = "Add a manual weather data measurement.",
tags = BaseMappings.WEATHER_DATA
)
@ApiResponse(
responseCode = "200",
description = "The weather data measurement was added.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = Response.class)
)
)
@ApiResponse(
responseCode = "400",
description = "The request is invalid.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = Response.class)
)
)
@PostMapping(value = "/weather-data/manual", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<? extends Response> addManualWeatherData(@Parameter(description = "The request to add a weather data measurement.") @Valid @RequestBody WeatherDataRequest request, Principal principal) {
var tenant = validateTenant(tenantService, principal);
manualWeatherDataService.addManualWeatherData(tenant, request);
return ResponseEntity.ok(new Response());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package de.app.fivegla.integration.fiware.model;

import de.app.fivegla.integration.fiware.model.api.FiwareEntity;
import de.app.fivegla.integration.fiware.model.api.Validatable;
import de.app.fivegla.integration.fiware.model.internal.Attribute;
import de.app.fivegla.integration.fiware.model.internal.NumberAttribute;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public record ManualWeatherDataEvent(
String id,
String type,
Attribute group,
Attribute dateCreated,
double latitude,
double longitude,
NumberAttribute temp,
NumberAttribute humidity,
NumberAttribute precipitation,
NumberAttribute airPressure,
NumberAttribute uvIndex,
NumberAttribute solarRadiation,
NumberAttribute visibility,
NumberAttribute dewPoint,
NumberAttribute feelsLike,
NumberAttribute heatIndex
) implements FiwareEntity, Validatable {

@Override
public String asJson() {
validate();
var json = "{" +
" \"id\":\"" + id.trim() + "\"," +
" \"type\":\"" + type.trim() + "\"," +
" \"customGroup\":" + group.asJson().trim() + "," +
" \"dateCreated\":" + dateCreated.asJson().trim() + "," +
" \"location\":" + locationAsJson(latitude, longitude).trim() +
" \"temperature\":" + temp.asJson().trim() + "," +
" \"humidity\":" + humidity.asJson().trim() + "," +
" \"precipitation\":" + precipitation.asJson().trim() +
" \"airPressure\":" + airPressure.asJson().trim() +
" \"uvIndex\":" + uvIndex.asJson().trim() +
" \"solarRadiation\":" + solarRadiation.asJson().trim() +
" \"visibility\":" + visibility.asJson().trim() +
" \"dewPoint\":" + dewPoint.asJson().trim() +
" \"feelsLike\":" + feelsLike.asJson().trim() +
" \"heatIndex\":" + heatIndex.asJson().trim() +
"}";
log.debug("{} as JSON: {}", this.getClass().getSimpleName(), json);
return json;
}

@Override
public void validate() {
if (StringUtils.isBlank(id)) {
throw new IllegalArgumentException("The id of the entity must not be null or blank.");
}
if (StringUtils.isBlank(type)) {
throw new IllegalArgumentException("The type of the entity must not be null or blank.");
}
}

@Override
public String getId() {
return id;
}

@Override
public String getType() {
return type;
}
}

0 comments on commit 87a20a7

Please sign in to comment.