100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Added code for checking well known attributes. Predicate code for location is improved
Michael Rademaker53cd55c56d3f
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingException.java b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingException.javaindex 6c1bf9ec2..72d8eac36 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingException.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingException.java@@ -112,7 +112,12 @@ public class AssetProcessingException extends RuntimeException {/*** Writing the asset attribute state to database failed.*/- STATE_STORAGE_FAILED+ STATE_STORAGE_FAILED,++ /**+ * The event value is not the excepted value for the attribute+ */+ INVALID_VALUE_FOR_WELL_KNOWN_ATTRIBUTE}final protected Reason reason;diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.javaindex 8189eba9e..7149690ad 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java@@ -55,7 +55,6 @@ import java.util.List;import java.util.Optional;import java.util.logging.Level;import java.util.logging.Logger;-import java.util.stream.Collectors;import static org.openremote.container.concurrent.GlobalLock.withLock;import static org.openremote.manager.asset.AssetProcessingException.Reason.*;@@ -290,6 +289,18 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer}}+ //Check if attribute is well known and the value is valid+ AssetModel.getAttributeDescriptor(oldAttribute.name).ifPresent(wellKnownAttribute -> {+ //Check if the value is valid+ wellKnownAttribute.getType()+ .isValidValue(event.getValue().orElseThrow(() -> new AssetProcessingException(INVALID_VALUE_FOR_WELL_KNOWN_ATTRIBUTE)))+ .ifPresent(validationFailure -> {+ throw new AssetProcessingException(+ INVALID_VALUE_FOR_WELL_KNOWN_ATTRIBUTE+ );+ });+ });+switch (source) {case CLIENT:AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.javaindex 214dfddca..12f90335d 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java@@ -253,6 +253,9 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour// Proper validation happens on merge(), here we only need the name to continueString updatedAttributeName = updatedAttribute.getNameOrThrow();+ //Check if a well known attribute is added+ checkForWellKnownAttributes(asset);+// Check if attribute is present on the asset in storageOptional<AssetAttribute> serverAttribute = resultAsset.getAttribute(updatedAttributeName);if (serverAttribute.isPresent()) {@@ -331,6 +334,28 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour}}+ private void checkForWellKnownAttributes(Asset asset) {+ asset.getAttributesStream().forEach(assetAttribute -> {+ AssetModel.getAttributeDescriptor(assetAttribute.name).ifPresent(wellKnownAttribute -> {+ //Check if the type matches+ if (!wellKnownAttribute.getType().equals(assetAttribute.getTypeOrThrow())) {+ throw new IllegalStateException(+ String.format("Well known attribute isn't of the correct type. Attribute name: %s. Expected type: %s",+ assetAttribute.name, wellKnownAttribute.getType().name()));+ }++ //Check if the value is valid+ wellKnownAttribute.getType()+ .isValidValue(assetAttribute.getValue().orElseThrow(() -> new IllegalStateException("Value is empty for " + assetAttribute.name)))+ .ifPresent(validationFailure -> {+ throw new IllegalStateException(+ String.format("Validation failed for %s with reason %s", assetAttribute.name, validationFailure.getReason().name())+ );+ });+ });+ });+ }+@Overridepublic void writeAttributeValue(RequestParams requestParams, String assetId, String attributeName, String rawJson) {try {@@ -364,6 +389,7 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResourcase INVALID_AGENT_LINK:case ILLEGAL_AGENT_UPDATE:case INVALID_ATTRIBUTE_EXECUTE_STATUS:+ case INVALID_VALUE_FOR_WELL_KNOWN_ATTRIBUTE:throw new IllegalStateException(processingException);default:throw processingException;@@ -440,23 +466,8 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour);});- asset.getAttributesStream().forEach(assetAttribute -> {- AssetModel.getAttributeDescriptor(assetAttribute.name).ifPresent(wellKnownAttribute -> {- if (!wellKnownAttribute.getType().equals(assetAttribute.getTypeOrThrow())) {- throw new IllegalStateException(- String.format("Well known attribute isn't of the correct type. Attribute name: %s. Expected type: %s",- assetAttribute.name, wellKnownAttribute.getType().name()));- }-- wellKnownAttribute.getType()- .isValidValue(assetAttribute.getValue().orElseThrow(() -> new IllegalStateException("Value is empty for " + assetAttribute.name)))- .ifPresent(validationFailure -> {- throw new IllegalStateException(- String.format("Validation failed for %s with reason %s", assetAttribute.name, validationFailure.getReason().name())- );- });- });- });+ //Check if a well known attribute is added+ checkForWellKnownAttributes(asset);return assetStorageService.merge(newAsset);diff --git a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.javaindex fb75c1fee..b1a710e78 100644--- a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java+++ b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java@@ -90,15 +90,12 @@ public class AssetQueryPredicate implements Predicate<AssetState> {}if (query.location != null) {- // Cannot use JTS libraries here without moving this class into manager module- // TODO: decide whether to use JTS lib or do simple approx calcs (e.g. (x - center_x)^2 + (y - center_y)^2 < radius^2)- if (query.location instanceof RadialLocationPredicate) {- RadialLocationPredicate radialLocationPredicate = (RadialLocationPredicate) query.location;- return false;- } else if (query.location instanceof RectangularLocationPredicate) {- return false;+ ObjectValue objectValue = assetState.getValueAsObject().orElse(null);+ if (objectValue != null) {+ Coordinate coordinate = new Coordinate(objectValue.getNumber("latitude").orElse(0d), objectValue.getNumber("longitude").orElse(0d));+ return asPredicate(query.location).test(coordinate);}- throw new UnsupportedOperationException("Location predicate '" + query.location.getClass().getSimpleName() + "' not supported in rules matching");+ return false;}return true;@@ -234,8 +231,9 @@ public class AssetQueryPredicate implements Predicate<AssetState> {rectangularLocationPredicate.latMax,rectangularLocationPredicate.lngMax);return envelope.contains(coordinate);+ } else {+ throw new UnsupportedOperationException("Location predicate '" + query.location.getClass().getSimpleName() + "' not supported in rules matching");}- return false;};}@@ -264,15 +262,6 @@ public class AssetQueryPredicate implements Predicate<AssetState> {NumberPredicate p = (NumberPredicate) predicate.value;return asPredicate(p).test(assetState.getValueAsNumber().orElse(null));-- } else if (predicate.value instanceof BaseAssetQuery.LocationPredicate) {- LocationPredicate p = (LocationPredicate) predicate.value;- ObjectValue objectValue = assetState.getValueAsObject().orElse(null);- if (objectValue != null) {- Coordinate coordinate = new Coordinate(objectValue.getNumber("latitude").orElse(0d), objectValue.getNumber("longitude").orElse(0d));- return asPredicate(p).test(coordinate);- }- return false;} else {// TODO Implement morethrow new UnsupportedOperationException(