This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
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/add device positions to data model (#53)
* Add DevicePosition model and integration service This commit introduces a new DevicePosition model. It is used to store the position of a device within the FIWARE system. A DevicePositionIntegrationService has also been added for persisting the DevicePosition entities. Additionally, this commit refactors the DeviceMeasurement model to leverage the FiwareEntity interface for creating a JSON representation. * Update type for 'entities' in UpdateOrCreateDeviceMeasurementRequest The type of 'entities' in UpdateOrCreateDeviceMeasurementRequest has been changed from DeviceMeasurement to FiwareEntity. This change involved adjusting related methods to match the new type. It was also necessary to adjust imports as a consequence of this type change. * Update type for 'entities' in UpdateOrCreateDeviceMeasurementRequest The type of 'entities' in UpdateOrCreateDeviceMeasurementRequest has been changed from DeviceMeasurement to FiwareEntity. This change involved adjusting related methods to match the new type. It was also necessary to adjust imports as a consequence of this type change. * Update project version in pom.xml The project version in the pom.xml file has been updated from 13.0.0 to 14.0.0. This version increment indicates the completion of new features or significant changes to the existing codebase.
- Loading branch information
1 parent
bd7854f
commit bc92b98
Showing
6 changed files
with
136 additions
and
22 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
55 changes: 55 additions & 0 deletions
55
src/main/java/de/app/fivegla/fiware/DevicePositiontIntegrationService.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,55 @@ | ||
package de.app.fivegla.fiware; | ||
|
||
import de.app.fivegla.fiware.api.CustomHeader; | ||
import de.app.fivegla.fiware.api.FiwareIntegrationLayerException; | ||
import de.app.fivegla.fiware.model.DevicePosition; | ||
import de.app.fivegla.fiware.request.UpdateOrCreateDeviceMeasurementRequest; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.util.List; | ||
|
||
/** | ||
* Integration service for FIWARE to send requests to the context broker. | ||
*/ | ||
@Slf4j | ||
public class DevicePositiontIntegrationService extends AbstractIntegrationService { | ||
|
||
public DevicePositiontIntegrationService(String contextBrokerUrl, String tenant) { | ||
super(contextBrokerUrl, tenant); | ||
} | ||
|
||
/** | ||
* Creates a new device in the context broker. | ||
* | ||
* @param entity the device to create | ||
*/ | ||
public void persist(DevicePosition entity) { | ||
var updateOrCreateEntityRequest = UpdateOrCreateDeviceMeasurementRequest.builder() | ||
.entities(List.of(entity)) | ||
.build(); | ||
var httpClient = HttpClient.newHttpClient(); | ||
var httpRequest = HttpRequest.newBuilder() | ||
.uri(URI.create(contextBrokerUrlForCommands() + "/op/update")) | ||
.header("Content-Type", "application/json") | ||
.header(CustomHeader.FIWARE_SERVICE, getTenant()) | ||
.POST(HttpRequest.BodyPublishers.ofString(updateOrCreateEntityRequest.asJson())).build(); | ||
try { | ||
var response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); | ||
if (response.statusCode() != 204) { | ||
log.error("Could not create entity. Response: " + response.body()); | ||
log.debug("Request: " + updateOrCreateEntityRequest.asJson()); | ||
log.debug("Response: " + response.body()); | ||
throw new FiwareIntegrationLayerException("Could not create entity, there was an error from FIWARE."); | ||
} else { | ||
log.info("Device created/updated successfully."); | ||
} | ||
} catch (Exception e) { | ||
throw new FiwareIntegrationLayerException("Could not create/update entity", e); | ||
} | ||
} | ||
|
||
} |
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
43 changes: 43 additions & 0 deletions
43
src/main/java/de/app/fivegla/fiware/model/DevicePosition.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,43 @@ | ||
package de.app.fivegla.fiware.model; | ||
|
||
import de.app.fivegla.fiware.model.api.Validatable; | ||
import de.app.fivegla.fiware.model.internal.Attribute; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
/** | ||
* Device position. | ||
*/ | ||
public record DevicePosition( | ||
String id, | ||
String type, | ||
Attribute transactionId, | ||
Attribute deviceId, | ||
Attribute dateCreated, | ||
double latitude, | ||
double longitude | ||
) implements Validatable, FiwareEntity { | ||
|
||
@Override | ||
public String asJson() { | ||
validate(); | ||
return "{" + | ||
" \"id\":\"" + id + "\"," + | ||
" \"type\":\"" + type + "\"," + | ||
" \"transactionId\":" + transactionId.asJson() + "," + | ||
" \"deviceId\":" + deviceId.asJson() + "," + | ||
" \"dateCreated\":" + dateCreated.asJson() + "," + | ||
" \"location\":" + locationAsJson(latitude, longitude) + | ||
"}"; | ||
} | ||
|
||
@Override | ||
public void validate() { | ||
if (StringUtils.isBlank(id)) { | ||
throw new IllegalArgumentException("The id of the device position must not be null or blank."); | ||
} | ||
if (StringUtils.isBlank(type)) { | ||
throw new IllegalArgumentException("The type of the device position must not be null or blank."); | ||
} | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/de/app/fivegla/fiware/model/FiwareEntity.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,31 @@ | ||
package de.app.fivegla.fiware.model; | ||
|
||
import de.app.fivegla.fiware.api.FiwareType; | ||
|
||
/** | ||
* The FiwareEntity interface represents an entity in the Fiware system. | ||
* Implementing classes should provide a JSON representation of the entity. | ||
*/ | ||
public interface FiwareEntity { | ||
|
||
/** | ||
* Converts the FiwareEntity object to JSON format. | ||
* | ||
* @return The FiwareEntity object in JSON format. | ||
*/ | ||
String asJson(); | ||
|
||
default String locationAsJson(double latitude, double longitude) { | ||
if (latitude == 0.0 && longitude == 0.0) { | ||
return "{}"; | ||
} else { | ||
return "{" + | ||
" \"type\":\"" + FiwareType.GEO_JSON.getKey() + "\"," + | ||
" \"value\": {" + | ||
" \"type\":\"Point\"," + | ||
" \"coordinates\": [" + longitude + "," + latitude + "]" + | ||
" }" + | ||
"}"; | ||
} | ||
} | ||
} |
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