Skip to content

Commit

Permalink
Add support for Smart Model JSON conversion (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
saschadoemer authored Nov 27, 2024
1 parent 62335a4 commit 4015dde
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/main/java/de/app/fivegla/api/enums/SmartModelEntityType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.app.fivegla.api.enums;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum SmartModelEntityType {
DEVICE_MEASUREMENT("DeviceMeasurement");

private final String key;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import de.app.fivegla.integration.fiware.model.api.Validatable;
import de.app.fivegla.integration.fiware.model.internal.Attribute;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
Expand Down Expand Up @@ -35,6 +36,11 @@ public String asJson() {
return json;
}

@Override
public String asSmartModelJson() {
throw new NotImplementedException("Smart model JSON is not supported for AgriCrop.");
}

@Override
public void validate() {
if (StringUtils.isBlank(id)) {
Expand All @@ -54,4 +60,9 @@ public String getId() {
public String getType() {
return type;
}

@Override
public boolean shouldCreateSmartModelEntity() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.app.fivegla.integration.fiware.model.api.Validatable;
import de.app.fivegla.integration.fiware.model.internal.Attribute;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.StringUtils;

/**
Expand Down Expand Up @@ -43,6 +44,11 @@ public String asJson() {
return json;
}

@Override
public String asSmartModelJson() {
throw new NotImplementedException("Smart model JSON is not supported for CameraImage.");
}

@Override
public void validate() {
if (StringUtils.isBlank(id)) {
Expand All @@ -62,4 +68,9 @@ public String getId() {
public String getType() {
return type;
}

@Override
public boolean shouldCreateSmartModelEntity() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.app.fivegla.integration.fiware.model;

import de.app.fivegla.api.enums.SmartModelEntityType;
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;
Expand Down Expand Up @@ -39,6 +40,23 @@ public String asJson() {
return json;
}

@Override
public String asSmartModelJson() {
validate();
var json = "{" +
" \"id\":\"" + id.trim() + "\"," +
" \"type\":\"" + SmartModelEntityType.DEVICE_MEASUREMENT.getKey() + "\"," +
" \"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;
}

@Override
public void validate() {
if (StringUtils.isBlank(id)) {
Expand All @@ -58,4 +76,9 @@ public String getId() {
public String getType() {
return type;
}

@Override
public boolean shouldCreateSmartModelEntity() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.app.fivegla.integration.fiware.model.api.Validatable;
import de.app.fivegla.integration.fiware.model.internal.Attribute;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.StringUtils;

/**
Expand Down Expand Up @@ -37,6 +38,11 @@ public String asJson() {
return json;
}

@Override
public String asSmartModelJson() {
throw new NotImplementedException("Smart model JSON is not supported for DevicePosition.");
}

@Override
public void validate() {
if (StringUtils.isBlank(id)) {
Expand All @@ -56,4 +62,9 @@ public String getId() {
public String getType() {
return type;
}

@Override
public boolean shouldCreateSmartModelEntity() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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.NotImplementedException;
import org.apache.commons.lang3.StringUtils;

@Slf4j
Expand Down Expand Up @@ -37,6 +38,11 @@ public String asJson() {
return json;
}

@Override
public String asSmartModelJson() {
throw new NotImplementedException("Smart model JSON is not supported for ManualPrecipitationEvent.");
}

@Override
public void validate() {
if (StringUtils.isBlank(id)) {
Expand All @@ -56,4 +62,9 @@ public String getId() {
public String getType() {
return type;
}

@Override
public boolean shouldCreateSmartModelEntity() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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.NotImplementedException;
import org.apache.commons.lang3.StringUtils;

@Slf4j
Expand Down Expand Up @@ -51,6 +52,11 @@ public String asJson() {
return json;
}

@Override
public String asSmartModelJson() {
throw new NotImplementedException("Smart model JSON is not supported for ManualWeatherDataEvent.");
}

@Override
public void validate() {
if (StringUtils.isBlank(id)) {
Expand All @@ -70,4 +76,9 @@ public String getId() {
public String getType() {
return type;
}

@Override
public boolean shouldCreateSmartModelEntity() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public String asJson() {
return json;
}

@Override
public String asSmartModelJson() {
throw new UnsupportedOperationException("Smart model JSON is not supported for StationaryCameraImage.");
}

@Override
public void validate() {
if (StringUtils.isBlank(id)) {
Expand All @@ -60,4 +65,9 @@ public String getId() {
public String getType() {
return type;
}

@Override
public boolean shouldCreateSmartModelEntity() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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.NotImplementedException;
import org.apache.commons.lang3.StringUtils;

@Slf4j
Expand Down Expand Up @@ -55,6 +56,11 @@ public String asJson() {
return json;
}

@Override
public String asSmartModelJson() {
throw new NotImplementedException("Smart model JSON is not supported for WeatherData.");
}

@Override
public void validate() {
if (StringUtils.isBlank(id)) {
Expand All @@ -74,4 +80,9 @@ public String getId() {
public String getType() {
return type;
}

@Override
public boolean shouldCreateSmartModelEntity() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ public interface FiwareEntity {
*/
String asJson();

/**
* Converts the FiwareEntity object to JSON format for a smart model.
*
* @return The FiwareEntity object in JSON format for a smart model.
*/
String asSmartModelJson();

/**
* Returns the JSON representation of the location coordinates.
*
Expand Down Expand Up @@ -72,4 +79,11 @@ default String coordinatesAsJson(List<GpsCoordinate> coordinates) {
* @return The type of the FiwareEntity object.
*/
String getType();

/**
* Returns whether a smart model entity should be created.
*
* @return true if a smart model entity should be created, false otherwise
*/
boolean shouldCreateSmartModelEntity();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public class UpdateOrCreateFiwareEntitiesRequest {
private List<FiwareEntity> entities;

public String asJson() {
return "{\"actionType\":\"" + actionType + "\",\"entities\":[" + entitiesAsJson() + "]}";
return "{\"actionType\":\"" + actionType + "\",\"entities\":[" + entitiesAsJson() + "," + entitiesAsSmartModelJson() + "]}";
}

private String entitiesAsSmartModelJson() {
return entities.stream().filter(FiwareEntity::shouldCreateSmartModelEntity).map(FiwareEntity::asSmartModelJson).collect(Collectors.joining(","));
}

private String entitiesAsJson() {
Expand Down

0 comments on commit 4015dde

Please sign in to comment.