Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Refactored attribute model

Christian Bauer95bccbc21bf1
diff --git a/agent/build.gradle b/agent/build.gradle
index 42933faf6..35ce03d8f 100644
--- a/agent/build.gradle
+++ b/agent/build.gradle
@@ -5,8 +5,8 @@ dependencies {
compile resolveProject(":container")
- compile "org.drools:drools-compiler:6.5.0.Final"
- compile "org.drools:drools-decisiontables:6.5.0.Final"
+ compile "org.drools:drools-compiler:$droolsVersion"
+ compile "org.drools:drools-decisiontables:$droolsVersion"
compile "com.luckycatlabs:SunriseSunsetCalculator:$sunriseSunsetCalculatorVersion"
diff --git a/agent/src/main/java/org/openremote/agent3/protocol/AbstractProtocol.java b/agent/src/main/java/org/openremote/agent3/protocol/AbstractProtocol.java
index 30265c3cb..54fd60f34 100644
--- a/agent/src/main/java/org/openremote/agent3/protocol/AbstractProtocol.java
+++ b/agent/src/main/java/org/openremote/agent3/protocol/AbstractProtocol.java
@@ -25,7 +25,7 @@ import org.apache.camel.builder.RouteBuilder;
import org.openremote.container.message.MessageBrokerSetupService;
import org.openremote.model.AttributeEvent;
import org.openremote.model.AttributeState;
-import org.openremote.model.asset.ThingAttribute;
+import org.openremote.model.asset.thing.ThingAttribute;
import org.openremote.container.Container;
import org.openremote.container.message.MessageBrokerContext;
import org.openremote.model.AttributeRef;
diff --git a/agent/src/main/java/org/openremote/agent3/protocol/Protocol.java b/agent/src/main/java/org/openremote/agent3/protocol/Protocol.java
index a3d866a97..cd1839a71 100644
--- a/agent/src/main/java/org/openremote/agent3/protocol/Protocol.java
+++ b/agent/src/main/java/org/openremote/agent3/protocol/Protocol.java
@@ -22,7 +22,7 @@ package org.openremote.agent3.protocol;
import org.openremote.container.ContainerService;
import org.openremote.model.AttributeEvent;
import org.openremote.model.Constants;
-import org.openremote.model.asset.ThingAttribute;
+import org.openremote.model.asset.thing.ThingAttribute;
import java.util.Collection;
diff --git a/agent/src/main/java/org/openremote/agent3/protocol/simulator/SimulatorProtocol.java b/agent/src/main/java/org/openremote/agent3/protocol/simulator/SimulatorProtocol.java
index 7a2f83d43..848943651 100644
--- a/agent/src/main/java/org/openremote/agent3/protocol/simulator/SimulatorProtocol.java
+++ b/agent/src/main/java/org/openremote/agent3/protocol/simulator/SimulatorProtocol.java
@@ -23,8 +23,8 @@ import elemental.json.JsonValue;
import org.openremote.agent3.protocol.AbstractProtocol;
import org.openremote.agent3.protocol.simulator.element.*;
import org.openremote.model.*;
-import org.openremote.model.asset.ProtocolConfiguration;
-import org.openremote.model.asset.ThingAttribute;
+import org.openremote.model.asset.agent.ProtocolConfiguration;
+import org.openremote.model.asset.thing.ThingAttribute;
import java.util.HashMap;
import java.util.Locale;
diff --git a/container/src/main/java/org/openremote/container/Container.java b/container/src/main/java/org/openremote/container/Container.java
index 82a06199a..dd92c7662 100644
--- a/container/src/main/java/org/openremote/container/Container.java
+++ b/container/src/main/java/org/openremote/container/Container.java
@@ -25,7 +25,6 @@ import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
-import org.openremote.container.json.AttributesJsonModule;
import org.openremote.container.json.ElementalJsonModule;
import org.openremote.container.util.LogUtil;
@@ -59,8 +58,7 @@ public class Container {
.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE)
.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE)
.setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.NONE)
- .registerModule(new ElementalJsonModule())
- .registerModule(new AttributesJsonModule());
+ .registerModule(new ElementalJsonModule());
protected final Map<String, String> config = new HashMap<>();
protected final boolean devMode;
diff --git a/container/src/main/java/org/openremote/container/json/AttributesJsonModule.java b/container/src/main/java/org/openremote/container/json/AttributesJsonModule.java
deleted file mode 100644
index 7d80dac82..000000000
--- a/container/src/main/java/org/openremote/container/json/AttributesJsonModule.java
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * Copyright 2016, OpenRemote Inc.
- *
- * See the CONTRIBUTORS.txt file in the distribution for a
- * full listing of individual contributors.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.openremote.container.json;
-
-import com.fasterxml.jackson.core.JsonGenerator;
-import com.fasterxml.jackson.core.JsonParser;
-import com.fasterxml.jackson.core.Version;
-import com.fasterxml.jackson.databind.DeserializationContext;
-import com.fasterxml.jackson.databind.SerializerProvider;
-import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
-import com.fasterxml.jackson.databind.module.SimpleModule;
-import com.fasterxml.jackson.databind.ser.std.StdSerializer;
-import elemental.json.Json;
-import org.openremote.model.Attributes;
-
-import java.io.IOException;
-
-public class AttributesJsonModule extends SimpleModule {
-
- private static class AttributesDeserializer extends StdDeserializer<Attributes> {
-
- public AttributesDeserializer() {
- this(null);
- }
-
- public AttributesDeserializer(Class<Attributes> t) {
- super(t);
- }
-
- @Override
- public Attributes deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
- return new Attributes(Json.parse(p.getText()));
- }
- }
-
- private static class AttributesSerializer extends StdSerializer<Attributes> {
-
- public AttributesSerializer() {
- this(null);
- }
-
- public AttributesSerializer(Class<Attributes> t) {
- super(t);
- }
-
- @Override
- public void serialize(Attributes value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
- jgen.writeObject(value.getJsonObject());
- }
- }
-
-
- public AttributesJsonModule() {
- super("AttributesJsonModule", new Version(1, 0, 0, "latest", null, null));
- AttributesSerializer serializer = new AttributesSerializer();
- AttributesDeserializer deserializer = new AttributesDeserializer();
- this.addSerializer(Attributes.class, serializer);
- this.addDeserializer(Attributes.class, deserializer);
- }
-}
diff --git a/container/src/main/java/org/openremote/container/json/ElementalJsonModule.java b/container/src/main/java/org/openremote/container/json/ElementalJsonModule.java
index 73185631d..fec3b295a 100644
--- a/container/src/main/java/org/openremote/container/json/ElementalJsonModule.java
+++ b/container/src/main/java/org/openremote/container/json/ElementalJsonModule.java
@@ -49,6 +49,8 @@ public class ElementalJsonModule extends SimpleModule {
private static class ElementalJsonSerializer extends JsonSerializer<JsonValue> {
@Override
public void serialize(JsonValue value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
+ if (value == null)
+ return;
if (value instanceof JsonNull) {
gen.writeNull();
} else {
diff --git a/deployment/manager/logging.properties b/deployment/manager/logging.properties
index e613c9d8f..9b1818060 100644
--- a/deployment/manager/logging.properties
+++ b/deployment/manager/logging.properties
@@ -21,7 +21,7 @@ org.xnio.level=WARNING
org.apache.camel.management.level=WARNING
org.apache.camel.impl.converter.DefaultTypeConverter.level=WARNING
-# Set to ERROR to log invalid access tokens
+# Set to SEVERE to log invalid access tokens
org.keycloak.adapters.BearerTokenRequestAuthenticator.level=OFF
# Set this to FINEST to log outgoing HTTP traffic
diff --git a/gradle.properties b/gradle.properties
index d8f658ddd..32c920f33 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -30,3 +30,4 @@ tyrusVersion = 1.10
# TODO Explicit dependency can be removed with Camel 2.18.x, then AHC 2.x is the default
ahcVersion = 1.9.39
sunriseSunsetCalculatorVersion = 1.2
+droolsVersion = 6.5.0.Final
diff --git a/manager/client/src/main/java/org/openremote/manager/client/assets/asset/AssetEditActivity.java b/manager/client/src/main/java/org/openremote/manager/client/assets/asset/AssetEditActivity.java
index 32223db6c..bb5882e60 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/assets/asset/AssetEditActivity.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/assets/asset/AssetEditActivity.java
@@ -38,13 +38,12 @@ import org.openremote.manager.shared.map.MapResource;
import org.openremote.manager.shared.security.Tenant;
import org.openremote.manager.shared.security.TenantResource;
import org.openremote.manager.shared.validation.ConstraintViolation;
-import org.openremote.model.Attributes;
import org.openremote.model.Consumer;
import org.openremote.model.asset.Asset;
+import org.openremote.model.asset.AssetAttributes;
import org.openremote.model.asset.AssetType;
import javax.inject.Inject;
-import java.util.Arrays;
import java.util.Collection;
import static org.openremote.manager.client.http.RequestExceptionHandler.handleRequestException;
@@ -349,11 +348,9 @@ public class AssetEditActivity
attributesEditor = new AttributesEditor(
environment,
view.getAttributesEditorContainer(),
- new Attributes(
- assetId == null
- ? asset.getWellKnownType().getDefaultAttributes()
- : asset.getAttributes()
- ),
+ assetId == null
+ ? asset.getWellKnownType().getDefaultAttributes()
+ : new AssetAttributes(asset.getAttributes()),
assetId == null
);
view.setAttributesEditor(attributesEditor);
diff --git a/manager/client/src/main/java/org/openremote/manager/client/assets/asset/AssetViewActivity.java b/manager/client/src/main/java/org/openremote/manager/client/assets/asset/AssetViewActivity.java
index 08e14e0dd..1f4d2ec92 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/assets/asset/AssetViewActivity.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/assets/asset/AssetViewActivity.java
@@ -40,9 +40,9 @@ import org.openremote.manager.shared.http.EntityWriter;
import org.openremote.manager.shared.map.MapResource;
import org.openremote.manager.shared.security.Tenant;
import org.openremote.manager.shared.security.TenantResource;
-import org.openremote.model.Attribute;
-import org.openremote.model.Attributes;
import org.openremote.model.Runnable;
+import org.openremote.model.asset.AssetAttribute;
+import org.openremote.model.asset.AssetAttributes;
import org.openremote.model.asset.AssetType;
import javax.inject.Inject;
@@ -180,10 +180,10 @@ public class AssetViewActivity
attributesBrowser = new AttributesBrowser(
environment,
view.getAttributesBrowserContainer(),
- new Attributes(asset.getAttributes())
+ new AssetAttributes(asset.getAttributes())
) {
@Override
- protected void readAttributeValue(Attribute attribute, Runnable onSuccess) {
+ protected void readAttributeValue(AssetAttribute attribute, Runnable onSuccess) {
environment.getRequestService().execute(
value -> value,
requestParams -> assetResource.readAttributeValue(requestParams, assetId, attribute.getName()),
@@ -200,7 +200,7 @@ public class AssetViewActivity
}
@Override
- protected void writeAttributeValue(Attribute attribute) {
+ protected void writeAttributeValue(AssetAttribute attribute) {
environment.getRequestService().execute(
(EntityWriter<String>) value -> value,
requestParams -> assetResource.writeAttributeValue(
diff --git a/manager/client/src/main/java/org/openremote/manager/client/assets/attributes/AttributesBrowser.java b/manager/client/src/main/java/org/openremote/manager/client/assets/attributes/AttributesBrowser.java
index 75cf57cc1..f928ed251 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/assets/attributes/AttributesBrowser.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/assets/attributes/AttributesBrowser.java
@@ -24,11 +24,12 @@ import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import org.openremote.manager.client.Environment;
import org.openremote.manager.client.widget.*;
-import org.openremote.model.Attribute;
-import org.openremote.model.Attributes;
import org.openremote.model.Runnable;
+import org.openremote.model.asset.AssetAttribute;
+import org.openremote.model.asset.AssetAttributes;
-public abstract class AttributesBrowser extends AttributesView<AttributesBrowser.Container, AttributesBrowser.Style> {
+public abstract class AttributesBrowser
+ extends AttributesView<AttributesBrowser.Container, AttributesBrowser.Style, AssetAttributes, AssetAttribute> {
public interface Container extends AttributesView.Container<AttributesBrowser.Style> {
@@ -40,7 +41,7 @@ public abstract class AttributesBrowser extends AttributesView<AttributesBrowser
protected FormGroup liveUpdatesGroup;
- public AttributesBrowser(Environment environment, Container container, Attributes attributes) {
+ public AttributesBrowser(Environment environment, Container container, AssetAttributes attributes) {
super(environment, container, attributes);
}
@@ -58,7 +59,7 @@ public abstract class AttributesBrowser extends AttributesView<AttributesBrowser
}
@Override
- protected void addAttributeActions(Attribute attribute,
+ protected void addAttributeActions(AssetAttribute attribute,
FormGroup formGroup,
FormField formField,
FormGroupActions formGroupActions,
@@ -92,7 +93,7 @@ public abstract class AttributesBrowser extends AttributesView<AttributesBrowser
}
@Override
- protected void addAttributeExtensions(Attribute attribute,
+ protected void addAttributeExtensions(AssetAttribute attribute,
FormGroup formGroup) {
formGroup.addExtension(createDatapointBrowser(attribute));
}
@@ -126,15 +127,15 @@ public abstract class AttributesBrowser extends AttributesView<AttributesBrowser
return formGroup;
}
- protected Widget createDatapointBrowser(Attribute attribute) {
+ protected Widget createDatapointBrowser(AssetAttribute attribute) {
// TODO
return new Image("/static/img/Example%20Chart%20Big.png");
}
/* ####################################################################### */
- abstract protected void readAttributeValue(Attribute attribute, Runnable onSuccess);
+ abstract protected void readAttributeValue(AssetAttribute attribute, Runnable onSuccess);
- abstract protected void writeAttributeValue(Attribute attribute);
+ abstract protected void writeAttributeValue(AssetAttribute attribute);
}
diff --git a/manager/client/src/main/java/org/openremote/manager/client/assets/attributes/AttributesEditor.java b/manager/client/src/main/java/org/openremote/manager/client/assets/attributes/AttributesEditor.java
index 3b2b0a03a..3d4a1e024 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/assets/attributes/AttributesEditor.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/assets/attributes/AttributesEditor.java
@@ -31,12 +31,14 @@ import org.openremote.manager.client.Environment;
import org.openremote.manager.client.util.JsUtil;
import org.openremote.manager.client.widget.*;
import org.openremote.model.*;
+import org.openremote.model.asset.AssetAttribute;
+import org.openremote.model.asset.AssetAttributes;
import org.openremote.model.asset.AssetMeta;
import java.util.logging.Logger;
public class AttributesEditor
- extends AttributesView<AttributesEditor.Container, AttributesEditor.Style> {
+ extends AttributesView<AttributesEditor.Container, AttributesEditor.Style, AssetAttributes, AssetAttribute> {
private static final Logger LOG = Logger.getLogger(AttributesEditor.class.getName());
@@ -53,7 +55,7 @@ public class AttributesEditor
final protected boolean isCreate;
protected FormGroup newAttributeGroup;
- public AttributesEditor(Environment environment, Container container, Attributes attributes, boolean isCreate) {
+ public AttributesEditor(Environment environment, Container container, AssetAttributes attributes, boolean isCreate) {
super(environment, container, attributes);
this.isCreate = isCreate;
}
@@ -72,7 +74,7 @@ public class AttributesEditor
}
@Override
- protected void addAttributeActions(Attribute attribute,
+ protected void addAttributeActions(AssetAttribute attribute,
FormGroup formGroup,
FormField formField,
FormGroupActions formGroupActions,
@@ -89,14 +91,14 @@ public class AttributesEditor
}
@Override
- protected void addAttributeExtensions(Attribute attribute, FormGroup formGroup) {
+ protected void addAttributeExtensions(AssetAttribute attribute, FormGroup formGroup) {
formGroup.addExtension(new MetaEditor(attribute));
}
/* ####################################################################### */
protected FormGroup createNewAttributeEditor() {
- Attribute attribute = new Attribute();
+ AssetAttribute attribute = new AssetAttribute();
FormGroup formGroup = createAttributeNameEditor(attribute);
@@ -108,7 +110,8 @@ public class AttributesEditor
if (attribute.isValid()) {
formGroup.setError(false);
// TODO This is necessary because JSON elemental behavior is weird
- Attribute storedAttribute = new Attribute(attribute.getName(), Json.parse(attribute.getJsonObject().toJson()));
+ AssetAttribute storedAttribute =
+ new AssetAttribute(attribute.getName(), Json.parse(attribute.getJsonObject().toJson()));
attributes.put(storedAttribute);
showInfo(environment.getMessages().attributeAdded(attribute.getName()));
build();
diff --git a/manager/client/src/main/java/org/openremote/manager/client/assets/attributes/AttributesView.java b/manager/client/src/main/java/org/openremote/manager/client/assets/attributes/AttributesView.java
index 6fafdfc04..b58b9224e 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/assets/attributes/AttributesView.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/assets/attributes/AttributesView.java
@@ -30,13 +30,22 @@ import org.openremote.manager.client.i18n.ManagerMessages;
import org.openremote.manager.client.util.CollectionsUtil;
import org.openremote.manager.client.util.TextUtil;
import org.openremote.manager.client.widget.*;
-import org.openremote.model.*;
+import org.openremote.model.AttributeType;
+import org.openremote.model.Consumer;
+import org.openremote.model.MetaItem;
+import org.openremote.model.asset.AbstractAssetAttribute;
+import org.openremote.model.asset.AbstractAssetAttributes;
import org.openremote.model.asset.AssetMeta;
import java.util.Comparator;
import java.util.LinkedHashMap;
-public abstract class AttributesView<C extends AttributesView.Container<S>, S extends AttributesView.Style> {
+public abstract class AttributesView<
+ C extends AttributesView.Container<S>,
+ S extends AttributesView.Style,
+ ATTRIBUTES extends AbstractAssetAttributes<?, A>,
+ A extends AbstractAssetAttribute
+ > {
public interface Container<S extends AttributesView.Style> {
S getStyle();
@@ -59,16 +68,16 @@ public abstract class AttributesView<C extends AttributesView.Container<S>, S ex
final protected Environment environment;
final protected C container;
- final protected Attributes attributes;
- final protected LinkedHashMap<Attribute, FormGroup> attributeGroups = new LinkedHashMap<>();
+ final protected ATTRIBUTES attributes;
+ final protected LinkedHashMap<A, FormGroup> attributeGroups = new LinkedHashMap<>();
- public AttributesView(Environment environment, C container, Attributes attributes) {
+ public AttributesView(Environment environment, C container, ATTRIBUTES attributes) {
this.environment = environment;
this.container = container;
this.attributes = attributes;
}
- public Attributes getAttributes() {
+ public ATTRIBUTES getAttributes() {
return attributes;
}
@@ -78,6 +87,7 @@ public abstract class AttributesView<C extends AttributesView.Container<S>, S ex
}
attributeGroups.clear();
}
+
public void build() {
clear();
createAttributeGroups();
@@ -95,19 +105,21 @@ public abstract class AttributesView<C extends AttributesView.Container<S>, S ex
/* ####################################################################### */
- protected void addAttributeActions(Attribute attribute,
+ protected void addAttributeActions(A attribute,
FormGroup formGroup,
FormField formField,
FormGroupActions formGroupActions,
- IsWidget editor) {}
+ IsWidget editor) {
+ }
- protected void addAttributeExtensions(Attribute attribute,
- FormGroup formGroup) {}
+ protected void addAttributeExtensions(A attribute,
+ FormGroup formGroup) {
+ }
/* ####################################################################### */
protected void createAttributeGroups() {
- for (Attribute attribute : attributes.get()) {
+ for (A attribute : attributes.get()) {
FormGroup formGroup = createAttributeGroup(attribute);
if (formGroup != null) {
attributeGroups.put(attribute, formGroup);
@@ -121,14 +133,14 @@ public abstract class AttributesView<C extends AttributesView.Container<S>, S ex
}
}
- protected FormGroup createAttributeGroup(Attribute attribute) {
+ protected FormGroup createAttributeGroup(A attribute) {
FormGroup formGroup = new FormGroup();
FormLabel formLabel = createAttributeLabel(attribute);
formGroup.addFormLabel(formLabel);
- MetaItem description = AssetMeta.getFirst(attribute, AssetMeta.DESCRIPTION);
+ MetaItem description = attribute.firstMetaItem(AssetMeta.DESCRIPTION);
if (description != null) {
formGroup.addInfolabel(new Label(description.getValueAsString()));
}
@@ -153,9 +165,9 @@ public abstract class AttributesView<C extends AttributesView.Container<S>, S ex
return formGroup;
}
- protected FormLabel createAttributeLabel(Attribute attribute) {
+ protected FormLabel createAttributeLabel(A attribute) {
String label = attribute.getName();
- MetaItem labelItem = AssetMeta.getFirst(attribute, AssetMeta.LABEL);
+ MetaItem labelItem = attribute.firstMetaItem(AssetMeta.LABEL);
if (labelItem != null) {
label = labelItem.getValueAsString();
}
@@ -165,10 +177,10 @@ public abstract class AttributesView<C extends AttributesView.Container<S>, S ex
return formLabel;
}
- protected IsWidget createEditor(Attribute attribute, FormGroup formGroup) {
+ protected IsWidget createEditor(A attribute, FormGroup formGroup) {
IsWidget editor;
S style = container.getStyle();
- MetaItem defaultValueItem = AssetMeta.getFirst(attribute, AssetMeta.DEFAULT);
+ MetaItem defaultValueItem = attribute.firstMetaItem(AssetMeta.DEFAULT);
Consumer<String> errorConsumer = msg -> {
formGroup.setError(true);
showValidationError(msg);
@@ -212,7 +224,7 @@ public abstract class AttributesView<C extends AttributesView.Container<S>, S ex
return editor;
}
- protected IsWidget createUnsupportedEditor(Attribute attribute) {
+ protected IsWidget createUnsupportedEditor(A attribute) {
FormField unsupportedField = new FormField();
unsupportedField.add(new FormOutputText(
environment.getMessages().unsupportedAttributeType(attribute.getType().getValue())
@@ -234,13 +246,11 @@ public abstract class AttributesView<C extends AttributesView.Container<S>, S ex
if (updateConsumer != null) {
input.addValueChangeHandler(
- event -> {
- updateConsumer.accept(
- event.getValue() != null && event.getValue().length() > 0
- ? event.getValue()
- : null
- );
- }
+ event -> updateConsumer.accept(
+ event.getValue() != null && event.getValue().length() > 0
+ ? event.getValue()
+ : null
+ )
);
} else {
input.setReadOnly(true);
@@ -351,11 +361,11 @@ public abstract class AttributesView<C extends AttributesView.Container<S>, S ex
return input;
}
- protected boolean isDefaultReadOnly(Attribute attribute) {
+ protected boolean isDefaultReadOnly(A attribute) {
return false;
}
- protected void removeAttribute(Attribute attribute) {
+ protected void removeAttribute(A attribute) {
attributes.remove(attribute.getName());
int attributeGroupIndex = container.getPanel().getWidgetIndex(attributeGroups.get(attribute));
container.getPanel().remove(attributeGroupIndex);
diff --git a/manager/server/src/main/java/org/openremote/manager/server/agent/AgentAttributes.java b/manager/server/src/main/java/org/openremote/manager/server/agent/AgentAttributes.java
deleted file mode 100644
index 7c30334f9..000000000
--- a/manager/server/src/main/java/org/openremote/manager/server/agent/AgentAttributes.java
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * Copyright 2016, OpenRemote Inc.
- *
- * See the CONTRIBUTORS.txt file in the distribution for a
- * full listing of individual contributors.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.openremote.manager.server.agent;
-
-import elemental.json.Json;
-import org.openremote.model.Attribute;
-import org.openremote.model.AttributeType;
-import org.openremote.model.Attributes;
-import org.openremote.model.asset.Asset;
-import org.openremote.model.asset.ProtocolConfiguration;
-
-import java.util.ArrayList;
-import java.util.List;
-
-public class AgentAttributes extends Attributes {
-
- public AgentAttributes() {
- setEnabled(false);
- }
-
- public AgentAttributes(Asset agentAsset) {
- super(agentAsset.getAttributes());
- }
-
- public ProtocolConfiguration[] getProtocolConfigurations() {
- Attribute[] attributes = super.get();
- List<ProtocolConfiguration> list = new ArrayList<>();
- for (Attribute attribute : attributes) {
- if (ProtocolConfiguration.isProtocolConfiguration(attribute)) {
- list.add(new ProtocolConfiguration(attribute));
- }
- }
- return list.toArray(new ProtocolConfiguration[list.size()]);
- }
-
- public ProtocolConfiguration getProtocolConfiguration(String name) {
- Attribute attribute = super.get(name);
- if (attribute == null || !ProtocolConfiguration.isProtocolConfiguration(attribute))
- return null;
- return new ProtocolConfiguration(attribute);
- }
-
- public boolean isEnabled() {
- return hasAttribute("enabled") && get("enabled").isValueTrue();
- }
-
- public AgentAttributes setEnabled(boolean enabled) {
- if (hasAttribute("enabled")) {
- get("enabled").setValueAsBoolean(enabled);
- } else {
- put(new Attribute("enabled", AttributeType.BOOLEAN, Json.create(enabled)));
- }
- return this;
- }
-
-}
diff --git a/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java b/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java
index 96bd0ad90..d24fc25aa 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/agent/AgentService.java
@@ -34,6 +34,10 @@ import org.openremote.manager.server.datapoint.AssetDatapointService;
import org.openremote.model.AttributeRef;
import org.openremote.model.Function;
import org.openremote.model.asset.*;
+import org.openremote.model.asset.agent.AgentAttributes;
+import org.openremote.model.asset.agent.ProtocolConfiguration;
+import org.openremote.model.asset.thing.ThingAttribute;
+import org.openremote.model.asset.thing.ThingAttributes;
import java.util.Collection;
import java.util.List;
@@ -64,6 +68,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Cons
AgentAttributes agentAttributes = new AgentAttributes(agent);
return agentAttributes.getProtocolConfiguration(agentLink.getAttributeName());
}
+ LOG.info("No agent/protocol configuration found for thing attribute: " + agentLink);
return null;
};
@@ -226,7 +231,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Cons
return;
}
- AttributeRef agentLink = ThingAttribute.getAgentLink(assetUpdate.getAttribute());
+ AttributeRef agentLink = ThingAttribute.getAgentLink(new ThingAttribute(assetUpdate.getAttribute()));
if (agentLink != null) {
// Check attribute is linked to an actual agent
@@ -246,7 +251,10 @@ public class AgentService extends RouteBuilder implements ContainerService, Cons
// Its' a send to actuator - push the update to the protocol
LOG.fine("Processing asset update: " + assetUpdate);
- messageBrokerService.getProducerTemplate().sendBody(ACTUATOR_TOPIC, assetUpdate.getAttribute().getStateEvent(assetUpdate.getAssetId()));
+ messageBrokerService.getProducerTemplate().sendBody(
+ ACTUATOR_TOPIC,
+ assetUpdate.getAttribute().getStateEvent()
+ );
assetUpdate.setStatus(AssetUpdate.Status.HANDLED);
}
diff --git a/manager/server/src/main/java/org/openremote/manager/server/agent/ThingAttributes.java b/manager/server/src/main/java/org/openremote/manager/server/agent/ThingAttributes.java
deleted file mode 100644
index cb90c0117..000000000
--- a/manager/server/src/main/java/org/openremote/manager/server/agent/ThingAttributes.java
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
- * Copyright 2016, OpenRemote Inc.
- *
- * See the CONTRIBUTORS.txt file in the distribution for a
- * full listing of individual contributors.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.openremote.manager.server.agent;
-
-import org.openremote.model.Attribute;
-import org.openremote.model.AttributeRef;
-import org.openremote.model.Attributes;
-import org.openremote.model.Function;
-import org.openremote.model.asset.Asset;
-import org.openremote.model.asset.ProtocolConfiguration;
-import org.openremote.model.asset.ThingAttribute;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.logging.Logger;
-
-public class ThingAttributes extends Attributes {
-
- private static final Logger LOG = Logger.getLogger(ThingAttributes.class.getName());
-
- final protected String thingId;
-
- public ThingAttributes(Asset thing) {
- super(thing.getAttributes());
- this.thingId = thing.getId();
- }
-
- public String getThingId() {
- return thingId;
- }
-
- public Map<String, List<ThingAttribute>> getLinkedAttributes(Function<AttributeRef, ProtocolConfiguration> linkResolver) {
- Attribute[] attributes = super.get();
- List<ThingAttribute> thingAttributes = new ArrayList<>();
- for (Attribute attribute : attributes) {
- ThingAttribute thingAttribute = getLinkedAttribute(linkResolver, attribute);
- if (thingAttribute != null) {
- thingAttributes.add(thingAttribute);
- }
- }
-
- Map<String, List<ThingAttribute>> result = new HashMap<>();
- for (ThingAttribute thingAttribute : thingAttributes) {
- String protocolName = thingAttribute.getProtocolConfiguration().getProtocolName();
- if (!result.containsKey(protocolName)) {
- result.put(protocolName, new ArrayList<>());
- }
- result.get(protocolName).add(thingAttribute);
- }
- return result;
- }
-
- public ThingAttribute getLinkedAttribute(Function<AttributeRef, ProtocolConfiguration> linkResolver,
- String attributeName) {
- return getLinkedAttribute(linkResolver, get(attributeName));
- }
-
- protected ThingAttribute getLinkedAttribute(Function<AttributeRef, ProtocolConfiguration> linkResolver,
- Attribute attribute) {
- if (attribute == null || !ThingAttribute.isLinkedAttribute(attribute))
- return null;
-
- AttributeRef agentLink = ThingAttribute.getAgentLink(attribute);
- if (agentLink == null)
- return null;
-
- ProtocolConfiguration protocolConfiguration = linkResolver.apply(agentLink);
- if (protocolConfiguration == null) {
- LOG.info("Protocol configuration not found in agent '" + agentLink + "', ignoring: " + attribute);
- return null;
- }
-
- return new ThingAttribute(getThingId(), protocolConfiguration, attribute);
- }
-}
diff --git a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetProcessingService.java b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetProcessingService.java
index 5a8493fbd..ee7852c62 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetProcessingService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetProcessingService.java
@@ -25,16 +25,13 @@ import org.openremote.container.ContainerService;
import org.openremote.container.message.MessageBrokerService;
import org.openremote.container.message.MessageBrokerSetupService;
import org.openremote.manager.server.agent.AgentService;
-import org.openremote.manager.server.agent.ThingAttributes;
import org.openremote.manager.server.datapoint.AssetDatapointService;
import org.openremote.manager.server.rules.RulesService;
-import org.openremote.model.Attribute;
import org.openremote.model.AttributeEvent;
import org.openremote.model.AttributeState;
-import org.openremote.model.Attributes;
-import org.openremote.model.asset.AssetType;
-import org.openremote.model.asset.AssetUpdate;
-import org.openremote.model.asset.ThingAttribute;
+import org.openremote.model.asset.*;
+import org.openremote.model.asset.thing.ThingAttribute;
+import org.openremote.model.asset.thing.ThingAttributes;
import java.util.ArrayList;
import java.util.List;
@@ -204,8 +201,8 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
// Pass attribute event through the processing chain
LOG.fine("Processing client " + attributeEvent + " for: " + asset);
- Attributes attributes = new Attributes(asset.getAttributes());
- Attribute attribute = attributes.get(attributeEvent.getAttributeName());
+ AssetAttributes attributes = new AssetAttributes(asset);
+ AssetAttribute attribute = attributes.get(attributeEvent.getAttributeName());
if (attribute == null) {
LOG.warning("Ignoring " + attributeEvent + ", attribute doesn't exist on asset: " + asset);
@@ -257,7 +254,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
}
protected void processUpdate(ServerAsset asset,
- Attribute attribute,
+ AbstractAssetAttribute attribute,
AttributeEvent attributeEvent) {
// Ensure timestamp of event is not in the future as that would essentially block access to
// the attribute until after that time (maybe that is desirable behaviour)
@@ -269,7 +266,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
}
// Hold on to existing attribute state so we can use it during processing
- AttributeEvent lastStateEvent = attribute.getStateEvent(asset.getId());
+ AttributeEvent lastStateEvent = attribute.getStateEvent();
// Check the last update timestamp of the attribute, ignoring any event that is older than last update
// TODO: This means we drop out-of-sequence events, we might need better at-least-once handling
@@ -280,7 +277,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
// Set new value and event timestamp on attribute, thus validating any attribute constraints
try {
- attribute.applyStateEvent(attributeEvent);
+ attribute.setValue(attributeEvent.getAttributeState().getValue(), attributeEvent.getTimestamp());
} catch (IllegalArgumentException ex) {
LOG.log(Level.WARNING, "Ignoring " + attributeEvent + ", attribute constraint violation in: " + asset, ex);
return;
diff --git a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetResourceImpl.java b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetResourceImpl.java
index 43af33072..61d568333 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetResourceImpl.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetResourceImpl.java
@@ -29,8 +29,8 @@ import org.openremote.manager.shared.security.Tenant;
import org.openremote.model.Attribute;
import org.openremote.model.AttributeEvent;
import org.openremote.model.AttributeRef;
-import org.openremote.model.Attributes;
import org.openremote.model.asset.Asset;
+import org.openremote.model.asset.AssetAttributes;
import org.openremote.model.asset.AssetQuery;
import javax.ws.rs.WebApplicationException;
@@ -208,7 +208,7 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour
throw new WebApplicationException(NOT_FOUND);
// Check attribute exists
- Attributes attributes = new Attributes(asset.getAttributes());
+ AssetAttributes attributes = new AssetAttributes(asset.getAttributes());
if (!attributes.hasAttribute(attributeName))
throw new WebApplicationException(NOT_FOUND);
@@ -227,7 +227,9 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour
// Process update
try {
JsonValue value = Json.instance().parse(rawJson);
- assetProcessingService.updateAttributeValue(new AttributeEvent(new AttributeRef(assetId, attributeName), value, this.getClass()));
+ assetProcessingService.updateAttributeValue(
+ new AttributeEvent(new AttributeRef(assetId, attributeName), value, this.getClass())
+ );
} catch (RuntimeException ex) {
throw new IllegalStateException("Error updating attribute: " + attributeName, ex);
}
@@ -256,7 +258,7 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
- Attributes attributes = new Attributes(asset.getAttributes());
+ AssetAttributes attributes = new AssetAttributes(asset.getAttributes());
Attribute attribute = attributes.get(attributeName);
if (attribute == null) {
throw new WebApplicationException(NOT_FOUND);
diff --git a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetStorageService.java b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetStorageService.java
index bc27cf9c8..3ea6c3d68 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetStorageService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetStorageService.java
@@ -29,7 +29,8 @@ import org.openremote.container.persistence.PersistenceService;
import org.openremote.container.util.Pair;
import org.openremote.container.web.WebService;
import org.openremote.manager.server.security.ManagerIdentityService;
-import org.openremote.model.*;
+import org.openremote.model.Attribute;
+import org.openremote.model.AttributeEvent;
import org.openremote.model.asset.*;
import org.postgresql.util.PGobject;
@@ -139,7 +140,7 @@ public class AssetStorageService implements ContainerService, Consumer<AssetUpda
throw new IllegalStateException("Invalid parent");
}
// Validate attribute names
- Attributes attributes = new Attributes(asset.getAttributes());
+ AssetAttributes attributes = new AssetAttributes(asset);
for (Attribute attribute : attributes.get()) {
if (!attributeNamePattern.matcher(attribute.getName()).matches()) {
throw new IllegalStateException(
@@ -299,7 +300,7 @@ public class AssetStorageService implements ContainerService, Consumer<AssetUpda
}
if (query.orderBy != null && query.orderBy.property != null) {
- sb.append(" order by " ) ;
+ sb.append(" order by ");
switch (query.orderBy.property) {
case CREATED_ON:
diff --git a/manager/server/src/main/java/org/openremote/manager/server/datapoint/AssetDatapointService.java b/manager/server/src/main/java/org/openremote/manager/server/datapoint/AssetDatapointService.java
index 4404fbf00..48e3b8f0d 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/datapoint/AssetDatapointService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/datapoint/AssetDatapointService.java
@@ -37,7 +37,7 @@ public class AssetDatapointService implements ContainerService, Consumer<AssetUp
public void accept(AssetUpdate assetUpdate) {
if (assetUpdate.getAttribute().isStoreDatapoints()) {
LOG.finest("Storing asset update data point: " + assetUpdate);
- AssetDatapoint assetDatapoint = new AssetDatapoint(assetUpdate.getAttribute().getStateEvent(assetUpdate.getAssetId()));
+ AssetDatapoint assetDatapoint = new AssetDatapoint(assetUpdate.getAttribute().getStateEvent());
persistenceService.doTransaction(entityManager -> entityManager.persist(assetDatapoint));
} else {
LOG.finest("Ignoring asset update as attribute is not a data point: " + assetUpdate);
diff --git a/manager/server/src/main/java/org/openremote/manager/server/rules/RulesDeployment.java b/manager/server/src/main/java/org/openremote/manager/server/rules/RulesDeployment.java
index 6746258a6..761c8580e 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/rules/RulesDeployment.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/rules/RulesDeployment.java
@@ -50,7 +50,16 @@ import java.util.logging.Level;
import java.util.logging.Logger;
public class RulesDeployment<T extends RulesDefinition> {
+
public static final Logger LOG = Logger.getLogger(RulesDeployment.class.getName());
+
+ static {
+ /* TODO needed for RHS lambda support?
+ System.setProperty("drools.dialect.java.lngLevel", "1.8");
+ System.setPRoperty("drools.dialect.java.compiler.lnglevel", "1.8");
+ */
+ }
+
// This is here so Clock Type can be set to pseudo from tests
protected static ClockTypeOption DefaultClockType;
private static final int AUTO_START_DELAY_SECONDS = 2;
@@ -385,13 +394,15 @@ public class RulesDeployment<T extends RulesDefinition> {
if (knowledgeSession != null) {
try {
// ... retract it from working memory ...
- LOG.finest("Removed stale attribute fact: " + update);
+ LOG.finest("Removed stale fact '" + update + "' in: " + this);
knowledgeSession.delete(factHandle);
int fireCount = knowledgeSession.fireAllRules();
} catch (Exception e) {
- LOG.fine("Failed to retract fact:" + update);
+ LOG.warning("Failed to retract fact '" + update + "' in: " + this);
}
}
+ } else {
+ LOG.fine("No fact handle for '" + assetUpdate + "' in: " + this);
}
}
@@ -402,7 +413,7 @@ public class RulesDeployment<T extends RulesDefinition> {
long newFactCount;
factHandle = knowledgeSession.insert(assetUpdate);
facts.put(assetUpdate, factHandle);
- LOG.finest("Inserted fact into session");
+ LOG.finest("Inserting fact '" + assetUpdate+ "' in: " + this);
LOG.finest("On " + this + ", firing all rules");
int fireCount = knowledgeSession.fireAllRules();
diff --git a/manager/server/src/main/java/org/openremote/manager/server/rules/RulesService.java b/manager/server/src/main/java/org/openremote/manager/server/rules/RulesService.java
index d6e96cbde..fe20dd9e8 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/rules/RulesService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/rules/RulesService.java
@@ -37,12 +37,7 @@ import org.openremote.manager.shared.rules.RulesDefinition;
import org.openremote.manager.shared.rules.TenantRulesDefinition;
import org.openremote.manager.shared.security.Tenant;
import org.openremote.model.AbstractValueTimestampHolder;
-import org.openremote.model.Attribute;
-import org.openremote.model.Attributes;
-import org.openremote.model.asset.Asset;
-import org.openremote.model.asset.AssetMeta;
-import org.openremote.model.asset.AssetQuery;
-import org.openremote.model.asset.AssetUpdate;
+import org.openremote.model.asset.*;
import org.openremote.model.util.JsonUtil;
import java.util.*;
@@ -114,26 +109,31 @@ public class RulesService extends RouteBuilder implements ContainerService, Cons
// We must load the asset from database (only when required), as the
// persistence event might not contain a completely loaded asset
- Function<Attribute, AssetUpdate> buildUpdateFunction = new Function<Attribute, AssetUpdate>() {
+ Function<AssetAttribute, AssetUpdate> buildUpdateFunction = new Function<AssetAttribute, AssetUpdate>() {
final Asset loadedAsset = assetStorageService.find(eventAsset.getId(), true);
@Override
- public AssetUpdate apply(Attribute attribute) {
- return new AssetUpdate(loadedAsset, attribute);
+ public AssetUpdate apply(AssetAttribute attribute) {
+ return new AssetUpdate(loadedAsset,
+ new AssetAttribute(loadedAsset.getId(),
+ attribute.getName(),
+ attribute.getJsonObject())
+ );
}
};
switch (persistenceEvent.getCause()) {
case INSERT:
// New asset has been created so get attributes that have RULES_FACT meta
- Attributes addedAttributes = new Attributes(eventAsset.getAttributes());
+ AssetAttributes addedAttributes = new AssetAttributes(eventAsset);
- Arrays.stream(addedAttributes.get())
- .filter(Attribute::isRulesFact)
+ addedAttributes.get().stream()
+ .filter(AssetAttribute::isRulesFact)
.forEach(attribute -> {
AssetUpdate update = buildUpdateFunction.apply(attribute);
// Set the status to completed already so rules cannot interfere with this initial insert
update.setStatus(AssetUpdate.Status.COMPLETED);
+ LOG.fine("Asset was persisted (" + persistenceEvent.getCause() + "), inserting fact: " + update);
insertFact(update, true);
});
break;
@@ -143,15 +143,15 @@ public class RulesService extends RouteBuilder implements ContainerService, Cons
if (attributesIndex >= 0) {
// Attributes have possibly changed so need to compare old and new state to determine
// which facts to retract and which to insert
- Attributes oldAttributes = new Attributes((JsonObject) persistenceEvent.getPreviousState()[attributesIndex]);
- Attributes newAttributes = new Attributes((JsonObject) persistenceEvent.getCurrentState()[attributesIndex]);
+ AssetAttributes oldAttributes = new AssetAttributes((JsonObject) persistenceEvent.getPreviousState()[attributesIndex]);
+ AssetAttributes newAttributes = new AssetAttributes((JsonObject) persistenceEvent.getCurrentState()[attributesIndex]);
- List<Attribute> oldFactAttributes = Arrays.stream(oldAttributes.get())
- .filter(Attribute::isRulesFact)
+ List<AssetAttribute> oldFactAttributes = oldAttributes.get().stream()
+ .filter(AssetAttribute::isRulesFact)
.collect(Collectors.toList());
- List<Attribute> newFactAttributes = Arrays.stream(newAttributes.get())
- .filter(Attribute::isRulesFact)
+ List<AssetAttribute> newFactAttributes = newAttributes.get().stream()
+ .filter(AssetAttribute::isRulesFact)
.collect(Collectors.toList());
// Compare attributes by JSON value
@@ -169,6 +169,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Cons
)
.forEach(obsoleteFactAttribute -> {
AssetUpdate update = buildUpdateFunction.apply(obsoleteFactAttribute);
+ LOG.fine("Asset was persisted (" + persistenceEvent.getCause() + "), retracting fact: " + update);
retractFact(update);
});
@@ -189,19 +190,21 @@ public class RulesService extends RouteBuilder implements ContainerService, Cons
AssetUpdate update = buildUpdateFunction.apply(newFactAttribute);
// Set the status to completed already so rules cannot interfere with this initial insert
update.setStatus(AssetUpdate.Status.COMPLETED);
+ LOG.fine("Asset was persisted (" + persistenceEvent.getCause() + "), inserting fact: " + update);
insertFact(update, true);
});
}
break;
case DELETE:
// Retract any facts that were associated with this asset
- Attributes removedAttributes = new Attributes(eventAsset.getAttributes());
- Arrays.stream(removedAttributes.get())
- .filter(Attribute::isRulesFact)
+ AssetAttributes removedAttributes = new AssetAttributes(eventAsset);
+ removedAttributes.get().stream()
+ .filter(AssetAttribute::isRulesFact)
.forEach(attribute -> {
// We can't load the asset again (it was deleted), so don't use buildUpdateFunction() and
// hope that the path of the event asset has been loaded before deletion
AssetUpdate update = new AssetUpdate(eventAsset, attribute);
+ LOG.fine("Asset was persisted (" + persistenceEvent.getCause() + "), retracting fact: " + update);
retractFact(update);
});
break;
@@ -238,10 +241,9 @@ public class RulesService extends RouteBuilder implements ContainerService, Cons
));
assets.forEach(asset -> {
- Attributes attributes = new Attributes(asset.getAttributes());
- Arrays
- .stream(attributes.get())
- .filter(Attribute::isRulesFact)
+ AssetAttributes attributes = new AssetAttributes(asset);
+ attributes.get().stream()
+ .filter(AssetAttribute::isRulesFact)
.forEach(attribute -> {
AssetUpdate update = new AssetUpdate(asset, attribute);
// Set the status to completed already so rules cannot interfere with this initial insert
@@ -438,6 +440,10 @@ public class RulesService extends RouteBuilder implements ContainerService, Cons
// Get the chain of rule engines that we need to pass through
List<RulesDeployment> rulesDeployments = getEnginesInScope(assetUpdate.getAssetRealmId(), assetUpdate.getAssetPath());
+ if (rulesDeployments.size() == 0) {
+ LOG.fine("Ignoring asset update as there are no matching rules deployments: " + assetUpdate);
+ }
+
// Pass through each engine and try and insert the fact
for (RulesDeployment deployment : rulesDeployments) {
deployment.insertFact(assetUpdate);
@@ -460,6 +466,10 @@ public class RulesService extends RouteBuilder implements ContainerService, Cons
// Get the chain of rule engines that we need to pass through
List<RulesDeployment> rulesDeployments = getEnginesInScope(assetUpdate.getAssetRealmId(), assetUpdate.getAssetPath());
+ if (rulesDeployments.size() == 0) {
+ LOG.fine("Ignoring asset update as there are no matching rules deployments: " + assetUpdate);
+ }
+
// Pass through each engine and retract this fact
for (RulesDeployment deployment : rulesDeployments) {
deployment.retractFact(assetUpdate);
diff --git a/manager/server/src/main/java/org/openremote/manager/server/setup/builtin/ManagerDemoSetup.java b/manager/server/src/main/java/org/openremote/manager/server/setup/builtin/ManagerDemoSetup.java
index e0e41911d..2866dba5b 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/setup/builtin/ManagerDemoSetup.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/setup/builtin/ManagerDemoSetup.java
@@ -21,32 +21,25 @@ package org.openremote.manager.server.setup.builtin;
import com.vividsolutions.jts.geom.Coordinate;
import elemental.json.Json;
-import org.apache.commons.io.IOUtils;
import org.openremote.agent3.protocol.simulator.SimulatorProtocol;
import org.openremote.agent3.protocol.simulator.element.ColorSimulatorElement;
import org.openremote.agent3.protocol.simulator.element.DecimalSimulatorElement;
import org.openremote.agent3.protocol.simulator.element.IntegerSimulatorElement;
import org.openremote.agent3.protocol.simulator.element.SwitchSimulatorElement;
import org.openremote.container.Container;
-import org.openremote.manager.server.agent.AgentAttributes;
-import org.openremote.manager.server.agent.ThingAttributes;
import org.openremote.manager.server.asset.ServerAsset;
import org.openremote.manager.server.setup.AbstractManagerSetup;
-import org.openremote.manager.shared.rules.AssetRulesDefinition;
-import org.openremote.manager.shared.rules.GlobalRulesDefinition;
-import org.openremote.manager.shared.rules.RulesDefinition;
-import org.openremote.manager.shared.rules.TenantRulesDefinition;
import org.openremote.manager.shared.security.Tenant;
import org.openremote.model.*;
+import org.openremote.model.asset.AssetAttribute;
+import org.openremote.model.asset.AssetAttributes;
import org.openremote.model.asset.AssetMeta;
import org.openremote.model.asset.AssetType;
-import org.openremote.model.asset.ProtocolConfiguration;
+import org.openremote.model.asset.agent.AgentAttributes;
+import org.openremote.model.asset.agent.ProtocolConfiguration;
import org.openremote.model.units.AttributeUnits;
import org.openremote.model.units.ColorRGB;
-import java.io.InputStream;
-import java.nio.charset.Charset;
-
import static org.openremote.model.AttributeType.*;
import static org.openremote.model.asset.AssetMeta.*;
import static org.openremote.model.asset.AssetType.*;
@@ -88,28 +81,28 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
smartOffice.setName("Smart Office");
smartOffice.setLocation(geometryFactory.createPoint(new Coordinate(5.460315214821094, 51.44541688237109)));
smartOffice.setType(BUILDING);
- Attributes smartOfficeAttributes = new Attributes();
+ AssetAttributes smartOfficeAttributes = new AssetAttributes();
smartOfficeAttributes.put(
- new Attribute("geoStreet", STRING, Json.create("Torenallee 20"))
- .setMeta(new Meta()
- .add(createMetaItem(LABEL, Json.create("Street")))
- .add(createMetaItem(ABOUT, Json.create("http://project-haystack.org/tag/geoStreet")))
- ),
- new Attribute("geoPostalCode", AttributeType.INTEGER, Json.create(5617))
- .setMeta(new Meta()
- .add(createMetaItem(LABEL, Json.create("Postal Code")))
- .add(createMetaItem(ABOUT, Json.create("http://project-haystack.org/tag/geoPostalCode")))
- ),
- new Attribute("geoCity", STRING, Json.create("Eindhoven"))
- .setMeta(new Meta()
- .add(createMetaItem(LABEL, Json.create("City")))
- .add(createMetaItem(ABOUT, Json.create("http://project-haystack.org/tag/geoCity")))
- ),
- new Attribute("geoCountry", STRING, Json.create("Netherlands"))
- .setMeta(new Meta()
- .add(createMetaItem(LABEL, Json.create("Country")))
- .add(createMetaItem(ABOUT, Json.create("http://project-haystack.org/tag/geoCountry")))
- )
+ new AssetAttribute("geoStreet", STRING, Json.create("Torenallee 20"))
+ .setMeta(new Meta()
+ .add(createMetaItem(LABEL, Json.create("Street")))
+ .add(createMetaItem(ABOUT, Json.create("http://project-haystack.org/tag/geoStreet")))
+ ),
+ new AssetAttribute("geoPostalCode", AttributeType.INTEGER, Json.create(5617))
+ .setMeta(new Meta()
+ .add(createMetaItem(LABEL, Json.create("Postal Code")))
+ .add(createMetaItem(ABOUT, Json.create("http://project-haystack.org/tag/geoPostalCode")))
+ ),
+ new AssetAttribute("geoCity", STRING, Json.create("Eindhoven"))
+ .setMeta(new Meta()
+ .add(createMetaItem(LABEL, Json.create("City")))
+ .add(createMetaItem(ABOUT, Json.create("http://project-haystack.org/tag/geoCity")))
+ ),
+ new AssetAttribute("geoCountry", STRING, Json.create("Netherlands"))
+ .setMeta(new Meta()
+ .add(createMetaItem(LABEL, Json.create("Country")))
+ .add(createMetaItem(ABOUT, Json.create("http://project-haystack.org/tag/geoCountry")))
+ )
);
smartOffice.setAttributes(smartOfficeAttributes.getJsonObject());
smartOffice = assetStorageService.merge(smartOffice);
@@ -134,19 +127,18 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
agent.setLocation(geometryFactory.createPoint(new Coordinate(5.460315214821094, 51.44541688237109)));
agent.setType(AssetType.AGENT);
AgentAttributes agentAttributes = new AgentAttributes();
- agentAttributes.setEnabled(false);
ProtocolConfiguration protocolConfigSimulator123 = new ProtocolConfiguration(demoAgentProtocolConfigName, SimulatorProtocol.PROTOCOL_NAME);
protocolConfigSimulator123.setMeta(new Meta()
- .add(new MetaItem(
- SimulatorProtocol.CONFIG_MODE,
- Json.create(SimulatorProtocol.Mode.WRITE_THROUGH_DELAYED.toString())
- )
+ .add(new MetaItem(
+ SimulatorProtocol.CONFIG_MODE,
+ Json.create(SimulatorProtocol.Mode.WRITE_THROUGH_DELAYED.toString())
)
- .add(new MetaItem(
- SimulatorProtocol.CONFIG_WRITE_DELAY_MILLISECONDS,
- Json.create(500)
- )
- ));
+ )
+ .add(new MetaItem(
+ SimulatorProtocol.CONFIG_WRITE_DELAY_MILLISECONDS,
+ Json.create(500)
+ )
+ ));
agentAttributes.put(protocolConfigSimulator123);
agent.setAttributes(agentAttributes.getJsonObject());
agent = assetStorageService.merge(agent);
@@ -156,94 +148,94 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
thing.setName("Demo Thing");
thing.setLocation(geometryFactory.createPoint(new Coordinate(5.460315214821094, 51.44541688237109)));
thing.setType(AssetType.THING);
- ThingAttributes thingAttributes = new ThingAttributes(thing);
+ AssetAttributes thingAttributes = new AssetAttributes(thing);
thingAttributes.put(
- new Attribute("light1Toggle", BOOLEAN, Json.create(true))
- .setMeta(new Meta()
- .add(new MetaItem(
- AssetMeta.DESCRIPTION,
- Json.create("The switch for the light in the living room"))
- )
- .add(new MetaItem(
- AssetMeta.AGENT_LINK,
- new AttributeRef(agent.getId(), demoAgentProtocolConfigName).asJsonValue()
- ))
- .add(new MetaItem(
- SimulatorProtocol.SIMULATOR_ELEMENT, Json.create(SwitchSimulatorElement.ELEMENT_NAME)
- ))
- ),
- new Attribute("light1Dimmer", INTEGER) // No initial value!
- .setMeta(new Meta()
- .add(new MetaItem(
- AssetMeta.DESCRIPTION,
- Json.create("The dimmer for the light in the living room"))
- )
- .add(new MetaItem(
- AssetMeta.RANGE_MIN,
- Json.create(0))
- )
- .add(new MetaItem(
- AssetMeta.RANGE_MAX,
- Json.create(100))
- )
- .add(new MetaItem(
- AssetMeta.AGENT_LINK,
- new AttributeRef(agent.getId(), demoAgentProtocolConfigName).asJsonValue()
- ))
- .add(new MetaItem(
- SimulatorProtocol.SIMULATOR_ELEMENT, Json.create(IntegerSimulatorElement.ELEMENT_NAME_RANGE)
- ))
- .add(new MetaItem(
- SimulatorProtocol.CONFIG_MODE, Json.create(true)
- ))
- ),
- new Attribute("light1Color", INTEGER_ARRAY, new ColorRGB(88, 123, 88).asJsonValue())
- .setMeta(new Meta()
- .add(new MetaItem(
- AssetMeta.UNITS,
- Json.create(AttributeUnits.COLOR_RGB.toString())
- ))
- .add(new MetaItem(
- AssetMeta.DESCRIPTION,
- Json.create("The color of the living room light"))
- )
- .add(new MetaItem(
- AssetMeta.AGENT_LINK,
- new AttributeRef(agent.getId(), demoAgentProtocolConfigName).asJsonValue()
- ))
- .add(new MetaItem(
- SimulatorProtocol.SIMULATOR_ELEMENT, Json.create(ColorSimulatorElement.ELEMENT_NAME)
- ))
- ),
- new Attribute("light1PowerConsumption", DECIMAL, Json.create(12.345))
- .setMeta(new Meta()
- .add(new MetaItem(
- AssetMeta.DESCRIPTION,
- Json.create("The total power consumption of the living room light"))
- )
- .add(new MetaItem(
- AssetMeta.READ_ONLY,
- Json.create(true))
- )
- .add(new MetaItem(
- AssetMeta.FORMAT,
- Json.create("%3d kWh"))
- )
- .add(new MetaItem(
- AssetMeta.UNITS,
- Json.create(AttributeUnits.ENERGY_KWH.name()))
- )
- .add(new MetaItem(
- AssetMeta.AGENT_LINK,
- new AttributeRef(agent.getId(), demoAgentProtocolConfigName).asJsonValue()
- ))
- .add(new MetaItem(
- SimulatorProtocol.SIMULATOR_ELEMENT, Json.create(DecimalSimulatorElement.ELEMENT_NAME)
- ))
- .add(new MetaItem(
- AssetMeta.STORE_DATA_POINTS.getName(), Json.create(true)
- ))
- )
+ new AssetAttribute("light1Toggle", BOOLEAN, Json.create(true))
+ .setMeta(new Meta()
+ .add(new MetaItem(
+ AssetMeta.DESCRIPTION,
+ Json.create("The switch for the light in the living room"))
+ )
+ .add(new MetaItem(
+ AssetMeta.AGENT_LINK,
+ new AttributeRef(agent.getId(), demoAgentProtocolConfigName).asJsonValue()
+ ))
+ .add(new MetaItem(
+ SimulatorProtocol.SIMULATOR_ELEMENT, Json.create(SwitchSimulatorElement.ELEMENT_NAME)
+ ))
+ ),
+ new AssetAttribute("light1Dimmer", INTEGER) // No initial value!
+ .setMeta(new Meta()
+ .add(new MetaItem(
+ AssetMeta.DESCRIPTION,
+ Json.create("The dimmer for the light in the living room"))
+ )
+ .add(new MetaItem(
+ AssetMeta.RANGE_MIN,
+ Json.create(0))
+ )
+ .add(new MetaItem(
+ AssetMeta.RANGE_MAX,
+ Json.create(100))
+ )
+ .add(new MetaItem(
+ AssetMeta.AGENT_LINK,
+ new AttributeRef(agent.getId(), demoAgentProtocolConfigName).asJsonValue()
+ ))
+ .add(new MetaItem(
+ SimulatorProtocol.SIMULATOR_ELEMENT, Json.create(IntegerSimulatorElement.ELEMENT_NAME_RANGE)
+ ))
+ .add(new MetaItem(
+ SimulatorProtocol.CONFIG_MODE, Json.create(true)
+ ))
+ ),
+ new AssetAttribute("light1Color", INTEGER_ARRAY, new ColorRGB(88, 123, 88).asJsonValue())
+ .setMeta(new Meta()
+ .add(new MetaItem(
+ AssetMeta.UNITS,
+ Json.create(AttributeUnits.COLOR_RGB.toString())
+ ))
+ .add(new MetaItem(
+ AssetMeta.DESCRIPTION,
+ Json.create("The color of the living room light"))
+ )
+ .add(new MetaItem(
+ AssetMeta.AGENT_LINK,
+ new AttributeRef(agent.getId(), demoAgentProtocolConfigName).asJsonValue()
+ ))
+ .add(new MetaItem(
+ SimulatorProtocol.SIMULATOR_ELEMENT, Json.create(ColorSimulatorElement.ELEMENT_NAME)
+ ))
+ ),
+ new AssetAttribute("light1PowerConsumption", DECIMAL, Json.create(12.345))
+ .setMeta(new Meta()
+ .add(new MetaItem(
+ AssetMeta.DESCRIPTION,
+ Json.create("The total power consumption of the living room light"))
+ )
+ .add(new MetaItem(
+ AssetMeta.READ_ONLY,
+ Json.create(true))
+ )
+ .add(new MetaItem(
+ AssetMeta.FORMAT,
+ Json.create("%3d kWh"))
+ )
+ .add(new MetaItem(
+ AssetMeta.UNITS,
+ Json.create(AttributeUnits.ENERGY_KWH.name()))
+ )
+ .add(new MetaItem(
+ AssetMeta.AGENT_LINK,
+ new AttributeRef(agent.getId(), demoAgentProtocolConfigName).asJsonValue()
+ ))
+ .add(new MetaItem(
+ SimulatorProtocol.SIMULATOR_ELEMENT, Json.create(DecimalSimulatorElement.ELEMENT_NAME)
+ ))
+ .add(new MetaItem(
+ AssetMeta.STORE_DATA_POINTS.getName(), Json.create(true)
+ ))
+ )
);
thing.setAttributes(thingAttributes.getJsonObject());
thing = assetStorageService.merge(thing);
@@ -256,12 +248,12 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
smartHome.setName("Smart Home");
smartHome.setLocation(geometryFactory.createPoint(new Coordinate(5.469751699216005, 51.44760787406028)));
smartHome.setType(BUILDING);
- Attributes smartHomeAttributes = new Attributes();
+ AssetAttributes smartHomeAttributes = new AssetAttributes();
smartHomeAttributes.put(
- new Attribute("demoBoolean", BOOLEAN, Json.create(true)),
- new Attribute("demoString", STRING, Json.create("demo")),
- new Attribute("demoInteger", INTEGER, Json.create(0)),
- new Attribute("demoDecimal", DECIMAL, Json.create(0d))
+ new AssetAttribute("demoBoolean", BOOLEAN, Json.create(true)),
+ new AssetAttribute("demoString", STRING, Json.create("demo")),
+ new AssetAttribute("demoInteger", INTEGER, Json.create(0)),
+ new AssetAttribute("demoDecimal", DECIMAL, Json.create(0d))
);
smartHome.setAttributes(smartHomeAttributes.getJsonObject());
smartHome = assetStorageService.merge(smartHome);
@@ -271,12 +263,12 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
apartment1.setName("Apartment 1");
apartment1.setLocation(geometryFactory.createPoint(new Coordinate(5.469751699216005, 51.44760787406028)));
apartment1.setType(RESIDENCE);
- Attributes apartment1Attributes = new Attributes();
+ AssetAttributes apartment1Attributes = new AssetAttributes();
apartment1Attributes.put(
- new Attribute("demoBoolean", BOOLEAN, Json.create(true)),
- new Attribute("demoString", STRING, Json.create("demo")),
- new Attribute("demoInteger", INTEGER, Json.create(0)),
- new Attribute("demoDecimal", DECIMAL, Json.create(0d))
+ new AssetAttribute("demoBoolean", BOOLEAN, Json.create(true)),
+ new AssetAttribute("demoString", STRING, Json.create("demo")),
+ new AssetAttribute("demoInteger", INTEGER, Json.create(0)),
+ new AssetAttribute("demoDecimal", DECIMAL, Json.create(0d))
);
apartment1.setAttributes(apartment1Attributes.getJsonObject());
apartment1 = assetStorageService.merge(apartment1);
@@ -286,21 +278,21 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
apartment1Livingroom.setName("Livingroom");
apartment1Livingroom.setLocation(geometryFactory.createPoint(new Coordinate(5.469751699216005, 51.44760787406028)));
apartment1Livingroom.setType(ROOM);
- Attributes apartment1LivingroomAttributes = new Attributes();
+ AssetAttributes apartment1LivingroomAttributes = new AssetAttributes();
apartment1LivingroomAttributes.put(
- new Attribute("demoBoolean", BOOLEAN, Json.create(true))
- .setMeta(new Meta().add(
- new MetaItem(AssetMeta.RULES_FACT, Json.create(true))
- )),
- new Attribute("demoString", STRING, Json.create("demo"))
- .setMeta(new Meta().add(
- new MetaItem(AssetMeta.RULES_FACT, Json.create(false))
- )),
- new Attribute("demoInteger", INTEGER, Json.create(0)),
- new Attribute("demoDecimal", DECIMAL, Json.create(0d))
- .setMeta(new Meta().add(
- new MetaItem(AssetMeta.RULES_FACT, Json.create(true))
- ))
+ new AssetAttribute("demoBoolean", BOOLEAN, Json.create(true))
+ .setMeta(new Meta().add(
+ new MetaItem(AssetMeta.RULES_FACT, Json.create(true))
+ )),
+ new AssetAttribute("demoString", STRING, Json.create("demo"))
+ .setMeta(new Meta().add(
+ new MetaItem(AssetMeta.RULES_FACT, Json.create(false))
+ )),
+ new AssetAttribute("demoInteger", INTEGER, Json.create(0)),
+ new AssetAttribute("demoDecimal", DECIMAL, Json.create(0d))
+ .setMeta(new Meta().add(
+ new MetaItem(AssetMeta.RULES_FACT, Json.create(true))
+ ))
);
apartment1Livingroom.setAttributes(apartment1LivingroomAttributes.getJsonObject());
apartment1Livingroom = assetStorageService.merge(apartment1Livingroom);
@@ -309,73 +301,73 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
ServerAsset apartment1LivingroomThermostat = new ServerAsset(apartment1Livingroom);
apartment1LivingroomThermostat.setName("Livingroom Thermostat");
apartment1LivingroomThermostat.setType(AssetType.THING);
- ThingAttributes apartment1LivingroomThermostatAttributes = new ThingAttributes(apartment1LivingroomThermostat);
+ AssetAttributes apartment1LivingroomThermostatAttributes = new AssetAttributes(apartment1LivingroomThermostat);
apartment1LivingroomThermostatAttributes.put(
- new Attribute("demoBoolean", BOOLEAN, Json.create(true)),
- new Attribute("demoString", STRING, Json.create("demo")),
- new Attribute("demoInteger", INTEGER, Json.create(0)),
- new Attribute("demoDecimal", DECIMAL, Json.create(0d)),
- new Attribute("currentTemperature", DECIMAL, Json.createNull())
- .setMeta(new Meta()
- .add(new MetaItem(
- AssetMeta.AGENT_LINK,
- new AttributeRef(demoAgentId, demoAgentProtocolConfigName).asJsonValue()
- ))
- .add(new MetaItem(
- AssetMeta.RULES_FACT,
- Json.create(true)
- ))
- .add(new MetaItem(
- AssetMeta.LABEL,
- Json.create("Current Temp"))
- )
- .add(new MetaItem(
- AssetMeta.PROTECTED,
- Json.create(true))
- )
- .add(new MetaItem(
- AssetMeta.READ_ONLY,
- Json.create(true))
- )
- .add(new MetaItem(
- Constants.NAMESPACE + ":foo:bar", Json.create("FOO")
- ))
- .add(new MetaItem(
- "urn:thirdparty:bar", Json.create("BAR")
- ))
- .add(new MetaItem(
- SimulatorProtocol.SIMULATOR_ELEMENT, Json.create(DecimalSimulatorElement.ELEMENT_NAME)
- ))
+ new AssetAttribute("demoBoolean", BOOLEAN, Json.create(true)),
+ new AssetAttribute("demoString", STRING, Json.create("demo")),
+ new AssetAttribute("demoInteger", INTEGER, Json.create(0)),
+ new AssetAttribute("demoDecimal", DECIMAL, Json.create(0d)),
+ new AssetAttribute("currentTemperature", DECIMAL, Json.createNull())
+ .setMeta(new Meta()
+ .add(new MetaItem(
+ AssetMeta.AGENT_LINK,
+ new AttributeRef(demoAgentId, demoAgentProtocolConfigName).asJsonValue()
+ ))
+ .add(new MetaItem(
+ AssetMeta.RULES_FACT,
+ Json.create(true)
+ ))
+ .add(new MetaItem(
+ AssetMeta.LABEL,
+ Json.create("Current Temp"))
+ )
+ .add(new MetaItem(
+ AssetMeta.PROTECTED,
+ Json.create(true))
+ )
+ .add(new MetaItem(
+ AssetMeta.READ_ONLY,
+ Json.create(true))
+ )
+ .add(new MetaItem(
+ Constants.NAMESPACE + ":foo:bar", Json.create("FOO")
+ ))
+ .add(new MetaItem(
+ "urn:thirdparty:bar", Json.create("BAR")
+ ))
+ .add(new MetaItem(
+ SimulatorProtocol.SIMULATOR_ELEMENT, Json.create(DecimalSimulatorElement.ELEMENT_NAME)
+ ))
- ),
- new Attribute("targetTemperature", DECIMAL, Json.createNull())
- .setMeta(new Meta()
- .add(new MetaItem(
- AssetMeta.AGENT_LINK,
- new AttributeRef(demoAgentId, demoAgentProtocolConfigName).asJsonValue()
- ))
- .add(new MetaItem(
- AssetMeta.RULES_FACT,
- Json.create(true)
- ))
- .add(new MetaItem(
- AssetMeta.LABEL,
- Json.create("Target Temp"))
- )
- .add(new MetaItem(
- AssetMeta.PROTECTED,
- Json.create(true))
- )
- .add(new MetaItem(
- Constants.NAMESPACE + ":foo:bar", Json.create("FOO")
- ))
- .add(new MetaItem(
- "urn:thirdparty:bar", Json.create("BAR")
- ))
- .add(new MetaItem(
- SimulatorProtocol.SIMULATOR_ELEMENT, Json.create(DecimalSimulatorElement.ELEMENT_NAME)
- ))
- )
+ ),
+ new AssetAttribute("targetTemperature", DECIMAL, Json.createNull())
+ .setMeta(new Meta()
+ .add(new MetaItem(
+ AssetMeta.AGENT_LINK,
+ new AttributeRef(demoAgentId, demoAgentProtocolConfigName).asJsonValue()
+ ))
+ .add(new MetaItem(
+ AssetMeta.RULES_FACT,
+ Json.create(true)
+ ))
+ .add(new MetaItem(
+ AssetMeta.LABEL,
+ Json.create("Target Temp"))
+ )
+ .add(new MetaItem(
+ AssetMeta.PROTECTED,
+ Json.create(true))
+ )
+ .add(new MetaItem(
+ Constants.NAMESPACE + ":foo:bar", Json.create("FOO")
+ ))
+ .add(new MetaItem(
+ "urn:thirdparty:bar", Json.create("BAR")
+ ))
+ .add(new MetaItem(
+ SimulatorProtocol.SIMULATOR_ELEMENT, Json.create(DecimalSimulatorElement.ELEMENT_NAME)
+ ))
+ )
);
apartment1LivingroomThermostat.setAttributes(apartment1LivingroomThermostatAttributes.getJsonObject());
@@ -386,12 +378,12 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
apartment2.setName("Apartment 2");
apartment2.setLocation(geometryFactory.createPoint(new Coordinate(5.469751699216005, 51.44760787406028)));
apartment2.setType(RESIDENCE);
- Attributes apartment2Attributes = new Attributes();
+ AssetAttributes apartment2Attributes = new AssetAttributes();
apartment2Attributes.put(
- new Attribute("demoBoolean", BOOLEAN, Json.create(true)),
- new Attribute("demoString", STRING, Json.create("demo")),
- new Attribute("demoInteger", INTEGER, Json.create(0)),
- new Attribute("demoDecimal", DECIMAL, Json.create(0d))
+ new AssetAttribute("demoBoolean", BOOLEAN, Json.create(true)),
+ new AssetAttribute("demoString", STRING, Json.create("demo")),
+ new AssetAttribute("demoInteger", INTEGER, Json.create(0)),
+ new AssetAttribute("demoDecimal", DECIMAL, Json.create(0d))
);
apartment2.setAttributes(apartment2Attributes.getJsonObject());
apartment2 = assetStorageService.merge(apartment2);
@@ -401,12 +393,12 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
apartment2Livingroom.setName("Livingroom");
apartment2Livingroom.setLocation(geometryFactory.createPoint(new Coordinate(5.469751699216005, 51.44760787406028)));
apartment2Livingroom.setType(ROOM);
- Attributes apartment2LivingroomAttributes = new Attributes();
+ AssetAttributes apartment2LivingroomAttributes = new AssetAttributes();
apartment2LivingroomAttributes.put(
- new Attribute("demoBoolean", BOOLEAN, Json.create(true)),
- new Attribute("demoString", STRING, Json.create("demo")),
- new Attribute("demoInteger", INTEGER, Json.create(0)),
- new Attribute("demoDecimal", DECIMAL, Json.create(0d))
+ new AssetAttribute("demoBoolean", BOOLEAN, Json.create(true)),
+ new AssetAttribute("demoString", STRING, Json.create("demo")),
+ new AssetAttribute("demoInteger", INTEGER, Json.create(0)),
+ new AssetAttribute("demoDecimal", DECIMAL, Json.create(0d))
);
apartment2Livingroom.setAttributes(apartment2LivingroomAttributes.getJsonObject());
apartment2Livingroom = assetStorageService.merge(apartment2Livingroom);
@@ -416,12 +408,12 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
apartment3.setName("Apartment 3");
apartment3.setLocation(geometryFactory.createPoint(new Coordinate(5.469751699216005, 51.44760787406028)));
apartment3.setType(RESIDENCE);
- Attributes apartment3Attributes = new Attributes();
+ AssetAttributes apartment3Attributes = new AssetAttributes();
apartment3Attributes.put(
- new Attribute("demoBoolean", BOOLEAN, Json.create(true)),
- new Attribute("demoString", STRING, Json.create("demo")),
- new Attribute("demoInteger", INTEGER, Json.create(0)),
- new Attribute("demoDecimal", DECIMAL, Json.create(0d))
+ new AssetAttribute("demoBoolean", BOOLEAN, Json.create(true)),
+ new AssetAttribute("demoString", STRING, Json.create("demo")),
+ new AssetAttribute("demoInteger", INTEGER, Json.create(0)),
+ new AssetAttribute("demoDecimal", DECIMAL, Json.create(0d))
);
apartment3.setAttributes(apartment3Attributes.getJsonObject());
apartment3 = assetStorageService.merge(apartment3);
@@ -431,15 +423,15 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
apartment3Livingroom.setName("Livingroom");
apartment3Livingroom.setLocation(geometryFactory.createPoint(new Coordinate(5.469751699216005, 51.44760787406028)));
apartment3Livingroom.setType(ROOM);
- Attributes apartment3LivingroomAttributes = new Attributes();
+ AssetAttributes apartment3LivingroomAttributes = new AssetAttributes();
apartment3LivingroomAttributes.put(
- new Attribute("demoBoolean", BOOLEAN, Json.create(true)),
- new Attribute("demoString", STRING, Json.create("demo"))
- .setMeta(new Meta().add(
- new MetaItem(AssetMeta.RULES_FACT, Json.create(true))
- )),
- new Attribute("demoInteger", INTEGER, Json.create(0)),
- new Attribute("demoDecimal", DECIMAL, Json.create(0d))
+ new AssetAttribute("demoBoolean", BOOLEAN, Json.create(true)),
+ new AssetAttribute("demoString", STRING, Json.create("demo"))
+ .setMeta(new Meta().add(
+ new MetaItem(AssetMeta.RULES_FACT, Json.create(true))
+ )),
+ new AssetAttribute("demoInteger", INTEGER, Json.create(0)),
+ new AssetAttribute("demoDecimal", DECIMAL, Json.create(0d))
);
apartment3Livingroom.setAttributes(apartment3LivingroomAttributes.getJsonObject());
apartment3Livingroom = assetStorageService.merge(apartment3Livingroom);
diff --git a/manager/server/src/main/resources/logging.properties b/manager/server/src/main/resources/logging.properties
index 97cd5d751..7132c0515 100644
--- a/manager/server/src/main/resources/logging.properties
+++ b/manager/server/src/main/resources/logging.properties
@@ -21,8 +21,8 @@ org.xnio.level=WARNING
org.apache.camel.management.level=WARNING
org.apache.camel.impl.converter.DefaultTypeConverter.level=WARNING
-# Set to ERROR to log invalid access tokens
-org.keycloak.adapters.BearerTokenRequestAuthenticator.level=ERROR
+# Set to SEVERE to log invalid access tokens
+org.keycloak.adapters.BearerTokenRequestAuthenticator.level=SEVERE
# Set this to FINEST to log outgoing HTTP traffic
org.apache.http.level=WARNING
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/device/CapabilityRef.java b/manager/shared/src/main/java/org/openremote/manager/shared/device/CapabilityRef.java
index 0433bb72d..e13bb8fb5 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/device/CapabilityRef.java
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/device/CapabilityRef.java
@@ -6,7 +6,8 @@ import org.openremote.model.AttributeType;
import org.openremote.model.Attributes;
// TODO Not used
-public class CapabilityRef extends Attribute {
+public class CapabilityRef {/*
+extends Attribute {
protected Attributes resources;
@@ -37,4 +38,5 @@ public class CapabilityRef extends Attribute {
public void setResourceRefs(Attributes resourceRefs) {
jsonObject.put("resourceRefs", resourceRefs.getJsonObject());
}
+ */
}
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/device/DeviceResource.java b/manager/shared/src/main/java/org/openremote/manager/shared/device/DeviceResource.java
index 9414ea2fd..ab64f6503 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/device/DeviceResource.java
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/device/DeviceResource.java
@@ -38,7 +38,8 @@ import org.openremote.model.Constants;
* <li>if the resource value is a constant and can be cached forever</li>
* </ul>
*/
-public class DeviceResource extends Attribute {
+public class DeviceResource { /* NOT USED
+extends Attribute {
public static final String DEVICE_RESOURCE = "deviceResource";
public static final String DEVICE_RESOURCE_TYPE = Constants.NAMESPACE + ":device:resource:type";
@@ -56,13 +57,11 @@ public class DeviceResource extends Attribute {
public static boolean isDeviceResource(Attribute attribute) {
// The given attribute must satisfy the API of this class
return false;
-/*
return attribute.hasMetadataElement(DEVICE_RESOURCE)
&& attribute.getMetadata().getElement(DEVICE_RESOURCE).getName().equals(DEVICE_RESOURCE_TYPE)
&& AttributeType.isValid(attribute.getMetadata().getElement(DEVICE_RESOURCE).getValue().asString())
&& attribute.hasMetadataElement(ACCESS)
&& attribute.getMetadata().getElement(ACCESS).getName().equals(ACCESS_TYPE);
-*/
}
public DeviceResource(Attribute attribute) {
@@ -84,37 +83,30 @@ public class DeviceResource extends Attribute {
new MetadataElement(ACCESS, ACCESS_TYPE, Json.create(access.name()))
)
);
- */
}
public AttributeType getResourceType() {
return null;
-/*
+
return getMetadata().hasElement(DEVICE_RESOURCE)
&& AttributeType.isValid(getMetadata().getElement(DEVICE_RESOURCE).getValue().asString())
? AttributeType.fromValue(getMetadata().getElement(DEVICE_RESOURCE).getValue().asString())
: null;
-*/
}
public Access getAccess() {
return null;
-/*
return getMetadata().hasElement(ACCESS)
? Access.valueOf(getMetadata().getElement(ACCESS).getValue().asString())
: null;
-*/
}
public boolean isPassive() {
return false;
-/*
return getMetadata().hasElement(PASSIVE) && getMetadata().getElement(PASSIVE).getValue().asBoolean();
-*/
}
public DeviceResource setPassive(boolean passive) {
-/*
if (passive) {
getMetadata().putElement(
new MetadataElement(PASSIVE, AttributeType.BOOLEAN.getValue(), Json.create(true))
@@ -122,19 +114,15 @@ public class DeviceResource extends Attribute {
} else {
getMetadata().removeElement(PASSIVE);
}
-*/
return this;
}
public boolean isConstant() {
return false;
-/*
return getMetadata().hasElement(CONSTANT) && getMetadata().getElement(CONSTANT).getValue().asBoolean();
-*/
}
public DeviceResource setConstant(boolean constant) {
-/*
if (constant) {
getMetadata().putElement(
new MetadataElement(CONSTANT, AttributeType.BOOLEAN.getValue(), Json.create(true))
@@ -142,8 +130,7 @@ public class DeviceResource extends Attribute {
} else {
getMetadata().removeElement(CONSTANT);
}
-*/
return this;
}
-
+*/
}
diff --git a/manager/test/src/test/groovy/org/openremote/test/agent/AgentDeploymentTest.groovy b/manager/test/src/test/groovy/org/openremote/test/agent/AgentDeploymentTest.groovy
index 82c8d0ee9..722899b12 100644
--- a/manager/test/src/test/groovy/org/openremote/test/agent/AgentDeploymentTest.groovy
+++ b/manager/test/src/test/groovy/org/openremote/test/agent/AgentDeploymentTest.groovy
@@ -8,7 +8,7 @@ import org.openremote.manager.server.asset.AssetStorageService
import org.openremote.manager.server.setup.SetupService
import org.openremote.manager.server.setup.builtin.ManagerDemoSetup
import org.openremote.model.AttributeEvent
-import org.openremote.model.Attributes
+import org.openremote.model.asset.AssetAttributes
import org.openremote.model.units.ColorRGB
import org.openremote.test.ManagerContainerTrait
import spock.lang.Specification
@@ -31,7 +31,8 @@ class AgentDeploymentTest extends Specification implements ManagerContainerTrait
then: "the simulator elements should have the initial state"
conditions.eventually {
assert simulatorProtocol.getState(managerDemoSetup.thingId, "light1Toggle").asBoolean()
- assert simulatorProtocol.getState(managerDemoSetup.thingId, "light1Dimmer").getType() == JsonType.NULL // No initial value!
+ assert simulatorProtocol.getState(managerDemoSetup.thingId, "light1Dimmer").getType() == JsonType.NULL
+ // No initial value!
assert new ColorRGB(simulatorProtocol.getState(managerDemoSetup.thingId, "light1Color") as JsonArray) == new ColorRGB(88, 123, 88)
assert simulatorProtocol.getState(managerDemoSetup.thingId, "light1PowerConsumption").asNumber() == 12.345d
}
@@ -47,7 +48,7 @@ class AgentDeploymentTest extends Specification implements ManagerContainerTrait
conditions.eventually {
assert simulatorProtocol.getState(managerDemoSetup.thingId, "light1Dimmer").asNumber() == 66
def thing = assetStorageService.find(managerDemoSetup.thingId, true)
- def attributes = new Attributes(thing.getAttributes())
+ def attributes = new AssetAttributes(thing.getAttributes())
assert attributes.get("light1Dimmer").getValue().getType() == JsonType.NUMBER
assert attributes.get("light1Dimmer").getValueAsInteger() == 66
}
@@ -59,7 +60,7 @@ class AgentDeploymentTest extends Specification implements ManagerContainerTrait
then: "the thing attribute value should be updated"
conditions.eventually {
def thing = assetStorageService.find(managerDemoSetup.thingId, true)
- def attributes = new Attributes(thing.getAttributes())
+ def attributes = new AssetAttributes(thing.getAttributes())
assert attributes.get("light1Dimmer").getValue().getType() == JsonType.NUMBER
assert attributes.get("light1Dimmer").getValueAsInteger() == 77
}
diff --git a/manager/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy b/manager/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy
index 7533e3e7c..815750807 100644
--- a/manager/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy
+++ b/manager/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy
@@ -8,6 +8,7 @@ import org.openremote.manager.server.setup.SetupService
import org.openremote.manager.server.setup.builtin.ManagerDemoSetup
import org.openremote.model.AttributeRef
import org.openremote.model.Attributes
+import org.openremote.model.asset.AssetAttributes
import org.openremote.test.ManagerContainerTrait
import spock.lang.Specification
import spock.util.concurrent.PollingConditions
@@ -44,7 +45,7 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
then: "the thing attribute value should be updated and the datapoints stored"
conditions.eventually {
def thing = assetStorageService.find(managerDemoSetup.thingId, true)
- def attributes = new Attributes(thing.getAttributes())
+ def attributes = new AssetAttributes(thing.getAttributes())
assert attributes.get("light1PowerConsumption").getValueAsDecimal() == 15.5d
def datapoints = assetDatapointService.getDatapoints(new AttributeRef(managerDemoSetup.thingId, "light1PowerConsumption"))
datapoints.size() == 3
diff --git a/manager/test/src/test/groovy/org/openremote/test/assets/AssetIntegrityTest.groovy b/manager/test/src/test/groovy/org/openremote/test/assets/AssetIntegrityTest.groovy
index f902a49cd..f478abc4a 100644
--- a/manager/test/src/test/groovy/org/openremote/test/assets/AssetIntegrityTest.groovy
+++ b/manager/test/src/test/groovy/org/openremote/test/assets/AssetIntegrityTest.groovy
@@ -9,6 +9,8 @@ import org.openremote.manager.shared.asset.AssetResource
import org.openremote.model.Attribute
import org.openremote.model.Attributes
import org.openremote.model.asset.Asset
+import org.openremote.model.asset.AssetAttribute
+import org.openremote.model.asset.AssetAttributes
import org.openremote.model.asset.AssetType
import org.openremote.model.AttributeType
import org.openremote.test.ManagerContainerTrait
@@ -57,8 +59,8 @@ class AssetIntegrityTest extends Specification implements ManagerContainerTrait
testAsset.parentId == null
when: "an asset is stored with an illegal attribute name"
- def attributes = new Attributes(testAsset.getAttributes())
- attributes.put(new Attribute("illegal- Attribute:name&&&"))
+ def attributes = new AssetAttributes(testAsset.getAttributes())
+ attributes.put(new AssetAttribute(testAsset.id, "illegal- Attribute:name&&&"))
testAsset.setAttributes(attributes.getJsonObject())
assetResource.update(null, testAsset.id, testAsset)
@@ -69,20 +71,20 @@ class AssetIntegrityTest extends Specification implements ManagerContainerTrait
when: "an asset is stored with a non-empty attribute value"
testAsset = assetResource.get(null, testAsset.getId())
testAsset.setAttributes(
- new Attributes().put(new Attribute("foo", AttributeType.STRING, Json.create("bar"))).getJsonObject()
+ new AssetAttributes().put(new AssetAttribute("foo", AttributeType.STRING, Json.create("bar"))).getJsonObject()
)
assetResource.update(null, testAsset.id, testAsset)
testAsset = assetResource.get(null, testAsset.getId())
then: "the attribute should exist"
- new Attributes(testAsset.getAttributes()).get("foo").getValueAsString() == "bar"
+ new AssetAttributes(testAsset.getAttributes()).get("foo").getValueAsString() == "bar"
when: "an asset attribute value is written directly"
assetResource.writeAttributeValue(null, testAsset.getId(), "foo", "\"bar2\"")
testAsset = assetResource.get(null, testAsset.getId())
then: "the attribute value should match"
- new Attributes(testAsset.getAttributes()).get("foo").getValueAsString() == "bar2"
+ new AssetAttributes(testAsset.getAttributes()).get("foo").getValueAsString() == "bar2"
assetResource.readAttributeValue(null, testAsset.getId(), "foo") == "\"bar2\""
when: "an asset attribute value null is written directly"
@@ -90,7 +92,7 @@ class AssetIntegrityTest extends Specification implements ManagerContainerTrait
testAsset = assetResource.get(null, testAsset.getId())
then: "the attribute value should match"
- !new Attributes(testAsset.getAttributes()).get("foo").hasValue()
+ !new AssetAttributes(testAsset.getAttributes()).get("foo").hasValue()
assetResource.readAttributeValue(null, testAsset.getId(), "foo") == "null"
when: "an asset is updated with a different type"
diff --git a/manager/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy b/manager/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy
index fa0dce086..9b9c446d6 100644
--- a/manager/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy
+++ b/manager/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy
@@ -1,15 +1,14 @@
package org.openremote.test.assets
-import elemental.json.Json
import org.openremote.container.util.IdentifierUtil
import org.openremote.manager.server.setup.SetupService
-import org.openremote.manager.server.setup.builtin.ManagerDemoSetup
import org.openremote.manager.server.setup.builtin.KeycloakDemoSetup
+import org.openremote.manager.server.setup.builtin.ManagerDemoSetup
import org.openremote.manager.shared.asset.AssetResource
import org.openremote.model.AttributeType
-import org.openremote.model.Attributes
import org.openremote.model.Meta
import org.openremote.model.asset.Asset
+import org.openremote.model.asset.AssetAttributes
import org.openremote.model.asset.AssetMeta
import org.openremote.model.asset.AssetType
import org.openremote.test.ManagerContainerTrait
@@ -461,7 +460,7 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTrai
apartment1LivingroomThermostat.id == managerDemoSetup.apartment1LivingroomThermostatId
apartment1LivingroomThermostat.name == "Livingroom Thermostat"
- Attributes protectedAttributes = new Attributes(apartment1LivingroomThermostat.attributes)
+ AssetAttributes protectedAttributes = new AssetAttributes(apartment1LivingroomThermostat.attributes)
protectedAttributes.size() == 2
protectedAttributes.get("currentTemperature")
protectedAttributes.get("currentTemperature").getType() == AttributeType.DECIMAL
diff --git a/manager/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy b/manager/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy
index 11e6d080f..6dda28b9f 100644
--- a/manager/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy
+++ b/manager/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy
@@ -2,28 +2,18 @@ package org.openremote.test.assets
import elemental.json.Json
import org.openremote.agent3.protocol.AbstractProtocol
-import org.openremote.manager.server.agent.AgentAttributes
-import org.openremote.manager.server.agent.ThingAttributes
-import org.openremote.model.asset.AssetUpdate
+import org.openremote.manager.server.asset.AssetProcessingService
+import org.openremote.manager.server.asset.AssetStorageService
import org.openremote.manager.server.asset.ServerAsset
-import org.openremote.model.Attribute
-import org.openremote.model.AttributeEvent
-import org.openremote.model.AttributeRef
-import org.openremote.model.AttributeState
-import org.openremote.model.Meta
-import org.openremote.model.Constants
-import org.openremote.model.MetaItem
-import org.openremote.model.asset.ProtocolConfiguration
-import org.openremote.model.asset.ThingAttribute
-import org.openremote.model.asset.AssetMeta
-import org.openremote.model.AttributeType
-import org.openremote.test.ManagerContainerTrait
import org.openremote.manager.server.setup.SetupService
-import org.openremote.manager.server.asset.AssetStorageService
-import org.openremote.manager.server.asset.AssetProcessingService
import org.openremote.manager.server.setup.builtin.KeycloakDemoSetup
import org.openremote.manager.server.setup.builtin.ManagerDemoSetup
-import org.openremote.model.asset.AssetType
+import org.openremote.model.*
+import org.openremote.model.asset.*
+import org.openremote.model.asset.agent.AgentAttributes
+import org.openremote.model.asset.agent.ProtocolConfiguration
+import org.openremote.model.asset.thing.ThingAttribute
+import org.openremote.test.ManagerContainerTrait
import spock.lang.Specification
import spock.util.concurrent.PollingConditions
@@ -43,7 +33,7 @@ class AssetProcessingTest extends Specification implements ManagerContainerTrait
//region and: "a mock protocol"
// TODO: Update this to use Simulator Protocol
and: "a mock protocol"
- def mockProtocolName = Constants.PROTOCOL_NAMESPACE + ":mockProtocol"
+ def mockProtocolName = Constants.PROTOCOL_NAMESPACE + ":mockProtocol"
def protocolDeployed = false
List<AttributeEvent> sendToActuatorEvents = []
def mockProtocol = new AbstractProtocol() {
@@ -88,11 +78,11 @@ class AssetProcessingTest extends Specification implements ManagerContainerTrait
List<AssetUpdate> updatesPassedStartOfProcessingChain = []
List<AssetUpdate> updatesReachedEndOfProcessingChain = []
- Consumer<AssetUpdate> mockEndConsumer = {assetUpdate ->
+ Consumer<AssetUpdate> mockEndConsumer = { assetUpdate ->
updatesReachedEndOfProcessingChain.add(assetUpdate)
}
- Consumer<AssetUpdate> mockStartConsumer = {assetUpdate ->
+ Consumer<AssetUpdate> mockStartConsumer = { assetUpdate ->
updatesPassedStartOfProcessingChain.add(assetUpdate)
}
//endregion
@@ -115,7 +105,6 @@ class AssetProcessingTest extends Specification implements ManagerContainerTrait
mockAgent.setName("Mock Agent");
mockAgent.setType(AssetType.AGENT);
AgentAttributes agentAttributes = new AgentAttributes();
- agentAttributes.setEnabled(false);
ProtocolConfiguration mockProtocolConfig = new ProtocolConfiguration("mock123", mockProtocolName);
agentAttributes.put(mockProtocolConfig);
mockAgent.setAttributes(agentAttributes.getJsonObject());
@@ -128,9 +117,9 @@ class AssetProcessingTest extends Specification implements ManagerContainerTrait
def mockThing = new ServerAsset(mockAgent)
mockThing.setName("Mock Thing Asset")
mockThing.setType(AssetType.THING)
- def mockThingAttributes = new ThingAttributes(mockThing);
+ def mockThingAttributes = new AssetAttributes(mockThing);
mockThingAttributes.put(
- new Attribute("light1Toggle", AttributeType.BOOLEAN, Json.create(false))
+ new AssetAttribute("light1Toggle", AttributeType.BOOLEAN, Json.create(false))
.setMeta(new Meta()
.add(new MetaItem(
AssetMeta.DESCRIPTION,
@@ -139,11 +128,11 @@ class AssetProcessingTest extends Specification implements ManagerContainerTrait
.add(new MetaItem(
AssetMeta.AGENT_LINK,
- new AttributeRef(mockAgent.getId(),"mock123").asJsonValue()
+ new AttributeRef(mockAgent.getId(), "mock123").asJsonValue()
)
)
),
- new Attribute("light2Toggle", AttributeType.BOOLEAN, Json.create(false))
+ new AssetAttribute("light2Toggle", AttributeType.BOOLEAN, Json.create(false))
.setMeta(new Meta()
.add(new MetaItem(
AssetMeta.DESCRIPTION,
@@ -156,7 +145,7 @@ class AssetProcessingTest extends Specification implements ManagerContainerTrait
)
)
),
- new Attribute("plainAttribute", AttributeType.STRING, Json.create("demo"))
+ new AssetAttribute("plainAttribute", AttributeType.STRING, Json.create("demo"))
.setMeta(new Meta()
.add(new MetaItem(
AssetMeta.DESCRIPTION,
diff --git a/manager/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy b/manager/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy
index 9f9b6a1e9..cf02c8f16 100644
--- a/manager/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy
+++ b/manager/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy
@@ -6,7 +6,7 @@ import org.openremote.manager.server.asset.ServerAsset
import org.openremote.manager.server.setup.SetupService
import org.openremote.manager.server.setup.builtin.KeycloakDemoSetup
import org.openremote.manager.server.setup.builtin.ManagerDemoSetup
-import org.openremote.model.Attributes
+import org.openremote.model.asset.AssetAttributes
import org.openremote.model.asset.AssetMeta
import org.openremote.model.asset.AssetQuery
import org.openremote.model.asset.AssetType
@@ -101,7 +101,7 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
asset.coordinates.length == 2
asset.path.length == 1
asset.path[0] == managerDemoSetup.smartOfficeId
- new Attributes(asset.attributes).get("geoStreet").valueAsString == "Torenallee 20"
+ new AssetAttributes(asset.attributes).get("geoStreet").valueAsString == "Torenallee 20"
when: "a query is executed"
def assets = assetStorageService.findAll(
@@ -151,7 +151,7 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
assets.get(0).coordinates.length == 2
assets.get(0).path.length == 1
assets.get(0).path[0] == managerDemoSetup.smartOfficeId
- new Attributes(assets.get(0).attributes).get("geoStreet").valueAsString == "Torenallee 20"
+ new AssetAttributes(assets.get(0).attributes).get("geoStreet").valueAsString == "Torenallee 20"
when: "a query is executed"
assets = assetStorageService.findAll(
@@ -248,11 +248,11 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
assets.get(0).id == managerDemoSetup.apartment1Id
assets.get(1).id == managerDemoSetup.apartment1LivingroomId
assets.get(2).id == managerDemoSetup.apartment1LivingroomThermostatId
- new Attributes(assets.get(2).attributes).size() == 2
- new Attributes(assets.get(2).attributes).get("currentTemperature").valueAsDecimal == null
- new Attributes(assets.get(2).attributes).get("currentTemperature").meta.size() == 3
- new Attributes(assets.get(2).attributes).get("targetTemperature").valueAsDecimal == null
- new Attributes(assets.get(2).attributes).get("targetTemperature").meta.size() == 2
+ new AssetAttributes(assets.get(2).attributes).size() == 2
+ new AssetAttributes(assets.get(2).attributes).get("currentTemperature").valueAsDecimal == null
+ new AssetAttributes(assets.get(2).attributes).get("currentTemperature").meta.size() == 3
+ new AssetAttributes(assets.get(2).attributes).get("targetTemperature").valueAsDecimal == null
+ new AssetAttributes(assets.get(2).attributes).get("targetTemperature").meta.size() == 2
assets.get(3).id == managerDemoSetup.apartment2Id
when: "a query is executed"
diff --git a/manager/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy b/manager/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy
index edb978918..88fd12d46 100644
--- a/manager/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy
+++ b/manager/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy
@@ -16,6 +16,8 @@ import org.openremote.manager.shared.rules.AssetRulesDefinition
import org.openremote.manager.shared.rules.GlobalRulesDefinition
import org.openremote.manager.shared.rules.RulesDefinition.DeploymentStatus
import org.openremote.model.*
+import org.openremote.model.asset.AssetAttribute
+import org.openremote.model.asset.AssetAttributes
import org.openremote.model.asset.AssetMeta
import org.openremote.model.asset.AssetType
import org.openremote.test.ManagerContainerTrait
@@ -317,9 +319,9 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainer
asset.setRealmId(keycloakDemoSetup.customerATenant.getId())
asset.setType(AssetType.ROOM)
asset.setName("Kitchen")
- Attributes attributes = new Attributes()
+ AssetAttributes attributes = new AssetAttributes()
attributes.put(
- new Attribute("testString", AttributeType.STRING, Json.create("test"))
+ new AssetAttribute("testString", AttributeType.STRING, Json.create("test"))
.setMeta(
new Meta()
.add(new MetaItem(AssetMeta.RULES_FACT, Json.create(true)))
@@ -343,14 +345,14 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainer
when: "the Kitchen room asset is modified to add a new attribute but RULES_FACT = true meta is not changed"
resetRuleExecutionLoggers()
- attributes = new Attributes()
+ attributes = new AssetAttributes()
attributes.put(
- new Attribute("testString", AttributeType.STRING, Json.create("test"))
+ new AssetAttribute("testString", AttributeType.STRING, Json.create("test"))
.setMeta(
new Meta()
.add(new MetaItem(AssetMeta.RULES_FACT, Json.create(true)))
),
- new Attribute("testInteger", AttributeType.INTEGER, Json.create(0))
+ new AssetAttribute("testInteger", AttributeType.INTEGER, Json.create(0))
)
asset.setAttributes(attributes.getJsonObject())
def factCount = smartHomeEngine.facts.size()
@@ -362,14 +364,14 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainer
}
when: "the Kitchen room asset is modified to set the RULES_FACT to false"
- attributes = new Attributes()
+ attributes = new AssetAttributes()
attributes.put(
- new Attribute("testString", AttributeType.STRING, Json.create("test"))
+ new AssetAttribute("testString", AttributeType.STRING, Json.create("test"))
.setMeta(
new Meta()
.add(new MetaItem(AssetMeta.RULES_FACT, Json.create(false)))
),
- new Attribute("testInteger", AttributeType.INTEGER, Json.create(0))
+ new AssetAttribute("testInteger", AttributeType.INTEGER, Json.create(0))
)
asset.setAttributes(attributes.getJsonObject())
factCount = smartHomeEngine.facts.size()
@@ -382,14 +384,14 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainer
when: "the Kitchen room asset is modified to set all attributes to RULES_FACT = true"
resetRuleExecutionLoggers()
- attributes = new Attributes()
+ attributes = new AssetAttributes()
attributes.put(
- new Attribute("testString", AttributeType.STRING, Json.create("test"))
+ new AssetAttribute("testString", AttributeType.STRING, Json.create("test"))
.setMeta(
new Meta()
.add(new MetaItem(AssetMeta.RULES_FACT, Json.create(true)))
),
- new Attribute("testInteger", AttributeType.INTEGER, Json.create(0))
+ new AssetAttribute("testInteger", AttributeType.INTEGER, Json.create(0))
.setMeta(
new Meta()
.add(new MetaItem(AssetMeta.RULES_FACT, Json.create(true)))
diff --git a/model/src/main/java/org/openremote/model/AbstractValueHolder.java b/model/src/main/java/org/openremote/model/AbstractValueHolder.java
index 286cf145e..34a37023a 100644
--- a/model/src/main/java/org/openremote/model/AbstractValueHolder.java
+++ b/model/src/main/java/org/openremote/model/AbstractValueHolder.java
@@ -26,7 +26,7 @@ import elemental.json.*;
* {@link JsonObject} that has a <code>value</code> field that accepts any
* {@link JsonValue}.
*/
-public abstract class AbstractValueHolder<T extends AbstractValueHolder> {
+public abstract class AbstractValueHolder<CHILD extends AbstractValueHolder> {
protected static final String VALUE_FIELD_NAME = "value";
@@ -78,27 +78,27 @@ public abstract class AbstractValueHolder<T extends AbstractValueHolder> {
jsonObject.remove(VALUE_FIELD_NAME);
}
- public T setValueAsString(String value) {
+ public CHILD setValueAsString(String value) {
return setValue(value == null ? Json.createNull() : Json.create(value));
}
- public T setValueAsInteger(Integer value) {
+ public CHILD setValueAsInteger(Integer value) {
return setValue(value == null ? Json.createNull() : Json.create(value));
}
- public T setValueAsDecimal(Double value) {
+ public CHILD setValueAsDecimal(Double value) {
return setValue(value == null ? Json.createNull() : Json.create(value));
}
- public T setValueAsBoolean(Boolean value) {
+ public CHILD setValueAsBoolean(Boolean value) {
return setValue(value == null ? Json.createNull() : Json.create(value));
}
- public T setValueAsObject(JsonObject value) {
+ public CHILD setValueAsObject(JsonObject value) {
return setValue(value == null ? Json.createNull() : value);
}
- public T setValueAsArray(JsonArray value) {
+ public CHILD setValueAsArray(JsonArray value) {
return setValue(value == null ? Json.createNull() : value);
}
@@ -115,7 +115,7 @@ public abstract class AbstractValueHolder<T extends AbstractValueHolder> {
* @throws IllegalArgumentException if the given value is invalid and failed constraint checking.
*/
@SuppressWarnings("unchecked")
- public T setValue(JsonValue value) throws IllegalArgumentException {
+ public CHILD setValue(JsonValue value) throws IllegalArgumentException {
if (value == null) {
value = Json.createNull();
@@ -132,7 +132,7 @@ public abstract class AbstractValueHolder<T extends AbstractValueHolder> {
jsonObject.put(VALUE_FIELD_NAME, value);
- return (T) this;
+ return (CHILD) this;
}
/**
diff --git a/model/src/main/java/org/openremote/model/AbstractValueTimestampHolder.java b/model/src/main/java/org/openremote/model/AbstractValueTimestampHolder.java
index 30bc0e911..3487859cf 100644
--- a/model/src/main/java/org/openremote/model/AbstractValueTimestampHolder.java
+++ b/model/src/main/java/org/openremote/model/AbstractValueTimestampHolder.java
@@ -27,7 +27,7 @@ import elemental.json.*;
* {@link JsonValue} and a <code>valueTimestamp</code> timestamp field, in
* milliseconds since the Unix epoch, of the most recent value change.
*/
-public abstract class AbstractValueTimestampHolder<T extends AbstractValueTimestampHolder> extends AbstractValueHolder<T> {
+public abstract class AbstractValueTimestampHolder<CHILD extends AbstractValueTimestampHolder> extends AbstractValueHolder<CHILD> {
public static final String VALUE_TIMESTAMP_FIELD_NAME = "valueTimestamp";
@@ -49,7 +49,7 @@ public abstract class AbstractValueTimestampHolder<T extends AbstractValueTimest
/**
* Sets the value timestamp to current system time.
*/
- public T setValueTimestamp() {
+ public CHILD setValueTimestamp() {
return setValueTimestamp(System.currentTimeMillis());
}
@@ -57,9 +57,9 @@ public abstract class AbstractValueTimestampHolder<T extends AbstractValueTimest
* Sets the value timestamp to given time.
*/
@SuppressWarnings("unchecked")
- public T setValueTimestamp(long timestamp) {
+ public CHILD setValueTimestamp(long timestamp) {
jsonObject.put(VALUE_TIMESTAMP_FIELD_NAME, Json.create(timestamp));
- return (T) this;
+ return (CHILD) this;
}
/**
@@ -74,8 +74,8 @@ public abstract class AbstractValueTimestampHolder<T extends AbstractValueTimest
/**
* Sets the value and the timestamp to given time.
*/
- public T setValue(JsonValue value, long timestamp) throws IllegalArgumentException {
- T result = setValue(value);
+ public CHILD setValue(JsonValue value, long timestamp) throws IllegalArgumentException {
+ CHILD result = setValue(value);
setValueTimestamp(timestamp);
return result;
}
@@ -84,8 +84,8 @@ public abstract class AbstractValueTimestampHolder<T extends AbstractValueTimest
* Sets the value and the timestamp to current system time.
*/
@Override
- public T setValue(JsonValue value) throws IllegalArgumentException {
- T result = super.setValue(value);
+ public CHILD setValue(JsonValue value) throws IllegalArgumentException {
+ CHILD result = super.setValue(value);
setValueTimestamp();
return result;
}
diff --git a/model/src/main/java/org/openremote/model/Attribute.java b/model/src/main/java/org/openremote/model/Attribute.java
index a0d30aff5..ab2ff5306 100644
--- a/model/src/main/java/org/openremote/model/Attribute.java
+++ b/model/src/main/java/org/openremote/model/Attribute.java
@@ -22,18 +22,15 @@ package org.openremote.model;
import elemental.json.Json;
import elemental.json.JsonObject;
import elemental.json.JsonValue;
-import org.openremote.model.asset.AssetMeta;
import java.util.NoSuchElementException;
-import static org.openremote.model.asset.AssetMeta.*;
-
/**
* Convenience overlay API for {@link JsonObject}.
* <p>
* Modifies the given or an empty object.
*/
-public class Attribute extends AbstractValueTimestampHolder<Attribute> {
+public abstract class Attribute<CHILD extends Attribute<CHILD>> extends AbstractValueTimestampHolder<CHILD> {
/**
* Attribute names should be very simple, as we use them in SQL path expressions, etc. and must manually escape.
@@ -66,6 +63,10 @@ public class Attribute extends AbstractValueTimestampHolder<Attribute> {
setValue(value);
}
+ public Attribute(CHILD attribute) {
+ this(attribute.getName(), attribute.getJsonObject());
+ }
+
@Override
protected boolean isValidValue(JsonValue value) {
return getType() != null
@@ -90,13 +91,14 @@ public class Attribute extends AbstractValueTimestampHolder<Attribute> {
return typeName != null ? AttributeType.fromValue(typeName) : null;
}
- public Attribute setType(AttributeType type) {
+ @SuppressWarnings("unchecked")
+ public CHILD setType(AttributeType type) {
if (type != null) {
jsonObject.put("type", Json.create(type.getValue()));
} else if (jsonObject.hasKey("type")) {
jsonObject.remove("type");
}
- return this;
+ return (CHILD)this;
}
public boolean hasMeta() {
@@ -107,13 +109,14 @@ public class Attribute extends AbstractValueTimestampHolder<Attribute> {
return hasMeta() ? new Meta(jsonObject.getArray("meta")) : null;
}
- public Attribute setMeta(Meta meta) {
+ @SuppressWarnings("unchecked")
+ public CHILD setMeta(Meta meta) {
if (meta != null) {
jsonObject.put("meta", meta.getJsonArray());
} else if (jsonObject.hasKey("meta")) {
jsonObject.remove("meta");
}
- return this;
+ return (CHILD) this;
}
public boolean hasMetaItem(String name) {
@@ -130,82 +133,9 @@ public class Attribute extends AbstractValueTimestampHolder<Attribute> {
return firstMetaItem(name);
}
- public Attribute copy() {
- return new Attribute(getName(), Json.parse(getJsonObject().toJson()));
- }
-
public boolean isValid() {
return getName() != null && getName().length() > 0 && getType() != null;
}
- // TODO: This needs to go into AssetAttribute - bit weird here
- public AttributeRef getAttributeRef(String entityId) {
- return new AttributeRef(entityId, getName());
- }
-
- /**
- * @return The current value.
- */
- public AttributeState getState(String assetId) {
- return new AttributeState(
- getAttributeRef(assetId),
- getValue()
- );
- }
-
- /**
- * @return The current value and its timestamp represented as an attribute event.
- */
- public AttributeEvent getStateEvent(String assetId) {
- return new AttributeEvent(
- getAttributeRef(assetId),
- getValue(),
- null,
- getValueTimestamp()
- );
- }
-
- /**
- * Set the attributes' value and updates the value timestamp to event time.
- *
- * @throws IllegalArgumentException If a constraint is violated.
- */
- public Attribute applyStateEvent(AttributeEvent attributeEvent) throws IllegalArgumentException {
- if (!attributeEvent.getAttributeState().getAttributeRef().getAttributeName().equals(getName())) {
- throw new IllegalArgumentException("Attribute name mismatch, expected: " + getName());
- }
- return setValue(attributeEvent.getAttributeState().getValue(), attributeEvent.getTimestamp());
- }
-
- /* ############################################################################### */
-
- // The following methods are only applicable to attributes of assets, but they are very convenient here...
- // TODO Introduce AssetAttribute class
-
- public boolean hasMetaItem(AssetMeta assetMeta) {
- return hasMetaItem(assetMeta.getName());
- }
-
- public MetaItem firstMetaItem(AssetMeta assetMeta) {
- return firstMetaItem(assetMeta.getName());
- }
-
- public boolean isProtected() {
- return hasMetaItem(PROTECTED) && firstMetaItem(PROTECTED).isValueTrue();
- }
-
- public boolean isReadOnly() {
- return hasMetaItem(READ_ONLY) && firstMetaItem(READ_ONLY).isValueTrue();
- }
-
- public boolean isStoreDatapoints() {
- return hasMetaItem(STORE_DATA_POINTS) && firstMetaItem(STORE_DATA_POINTS).isValueTrue();
- }
-
- public boolean isRulesFact() {
- return hasMetaItem(RULES_FACT) && firstMetaItem(RULES_FACT).isValueTrue();
- }
-
- /* ############################################################################### */
-
+ public abstract CHILD copy();
}
diff --git a/model/src/main/java/org/openremote/model/Attributes.java b/model/src/main/java/org/openremote/model/Attributes.java
index cf56f1c3e..58fc4b697 100644
--- a/model/src/main/java/org/openremote/model/Attributes.java
+++ b/model/src/main/java/org/openremote/model/Attributes.java
@@ -19,18 +19,20 @@
*/
package org.openremote.model;
-import com.fasterxml.jackson.annotation.JsonCreator;
import elemental.json.Json;
import elemental.json.JsonObject;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
/**
* Convenience overlay API for {@link JsonObject}.
*
* Modifies the given or an empty object.
*/
-public class Attributes {
+public abstract class Attributes<CHILD extends Attributes<CHILD, A>, A extends Attribute> {
final protected JsonObject jsonObject;
@@ -38,11 +40,15 @@ public class Attributes {
this(Json.createObject());
}
- @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
+ // TODO @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
public Attributes(JsonObject jsonObject) {
this.jsonObject = jsonObject != null ? jsonObject : Json.createObject();
}
+ public Attributes(CHILD attributes) {
+ this.jsonObject = attributes != null && attributes.getJsonObject() != null ? attributes.getJsonObject() : Json.createObject();
+ }
+
public JsonObject getJsonObject() {
return jsonObject;
}
@@ -51,59 +57,59 @@ public class Attributes {
return jsonObject.hasKey(name);
}
- public String[] names() {
- return jsonObject.keys();
+ public List<String> names() {
+ return Collections.unmodifiableList(Arrays.asList(jsonObject.keys()));
}
public int size() {
- return get().length;
+ return jsonObject.keys().length;
}
- public Attribute[] get() {
- Set<Attribute> attributes = new LinkedHashSet<>();
+ public List<A> get() {
+ List<A> attributes = new ArrayList<>();
String[] keys = jsonObject.keys();
for (String key : keys) {
- if (hasAttribute(key)) {
- Attribute attribute = new Attribute(key, jsonObject.getObject(key));
- attributes.add(attribute);
- }
+ attributes.add(createAttribute(key, jsonObject.getObject(key)));
}
- return attributes.toArray(new Attribute[attributes.size()]);
+ return Collections.unmodifiableList(attributes);
}
- public Attribute get(String name) {
- return hasAttribute(name) ? new Attribute(name, jsonObject.getObject(name)) : null;
+ public A get(String name) {
+ return hasAttribute(name) ? createAttribute(name, jsonObject.getObject(name)) : null;
}
- public Attributes put(Attribute... attributes) {
+ @SuppressWarnings("unchecked")
+ public CHILD put(A... attributes) {
if (attributes != null)
- for (Attribute attribute : attributes) {
+ for (A attribute : attributes) {
jsonObject.put(attribute.getName(), attribute.getJsonObject());
}
- return this;
+ return (CHILD) this;
}
- public Attributes remove(String name) {
+ @SuppressWarnings("unchecked")
+ public CHILD remove(String name) {
if (hasAttribute(name)) {
jsonObject.remove(name);
}
- return this;
+ return (CHILD) this;
}
- public Attributes clear(String... excludeAttributes) {
+ @SuppressWarnings("unchecked")
+ public CHILD clear(String... excludeAttributes) {
List<String> excluded =
excludeAttributes != null ? Arrays.asList(excludeAttributes) : new ArrayList<>();
- for (Attribute attribute : get()) {
+ for (A attribute : get()) {
if (!excluded.contains(attribute.getName())) {
remove(attribute.getName());
}
}
- return this;
+ return (CHILD) this;
}
- public Attributes copy() {
- return new Attributes(Json.parse(getJsonObject().toJson()));
- }
+ abstract public CHILD copy();
+
+ abstract protected A createAttribute(String name, JsonObject jsonObject);
@Override
public String toString() {
diff --git a/model/src/main/java/org/openremote/model/asset/AbstractAssetAttribute.java b/model/src/main/java/org/openremote/model/asset/AbstractAssetAttribute.java
new file mode 100644
index 000000000..0a8ea829e
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/asset/AbstractAssetAttribute.java
@@ -0,0 +1,142 @@
+/*
+ * Copyright 2017, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.model.asset;
+
+import elemental.json.Json;
+import elemental.json.JsonObject;
+import elemental.json.JsonValue;
+import org.openremote.model.*;
+
+import static org.openremote.model.asset.AssetMeta.*;
+
+public class AbstractAssetAttribute<CHILD extends AbstractAssetAttribute<CHILD>> extends Attribute<CHILD> {
+
+ final public String assetId;
+
+ public AbstractAssetAttribute() {
+ this.assetId = null;
+ }
+
+ public AbstractAssetAttribute(String assetId) {
+ this.assetId = assetId;
+ }
+
+ public AbstractAssetAttribute(String name, AttributeType type) {
+ super(name, type);
+ this.assetId = null;
+ }
+
+ public AbstractAssetAttribute(String name, JsonObject jsonObject) {
+ super(name, jsonObject);
+ this.assetId = null;
+ }
+
+ public AbstractAssetAttribute(String name, AttributeType type, JsonValue value) {
+ super(name, type, value);
+ this.assetId = null;
+ }
+
+ public AbstractAssetAttribute(String assetId, String name) {
+ super(name);
+ this.assetId = assetId;
+ }
+
+ public AbstractAssetAttribute(String assetId, String name, AttributeType type) {
+ super(name, type);
+ this.assetId = assetId;
+ }
+
+ public AbstractAssetAttribute(String assetId, String name, JsonObject jsonObject) {
+ super(name, jsonObject);
+ this.assetId = assetId;
+ }
+
+ public AbstractAssetAttribute(String assetId, String name, AttributeType type, JsonValue value) {
+ super(name, type, value);
+ this.assetId = assetId;
+ }
+
+ public AbstractAssetAttribute(AbstractAssetAttribute attribute) {
+ this(attribute.assetId, attribute.getName(), attribute.getJsonObject());
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public CHILD copy() {
+ return (CHILD) new AbstractAssetAttribute<CHILD>(getName(), Json.parse(getJsonObject().toJson()));
+ }
+
+ public String getAssetId() {
+ if (assetId == null) {
+ throw new IllegalStateException("Asset identifier not set on: " + this);
+ }
+ return assetId;
+ }
+
+ public AttributeRef getAttributeRef() {
+ return new AttributeRef(getAssetId(), getName());
+ }
+
+ /**
+ * @return The current value.
+ */
+ public AttributeState getState() {
+ return new AttributeState(
+ getAttributeRef(),
+ getValue()
+ );
+ }
+
+ /**
+ * @return The current value and its timestamp represented as an attribute event.
+ */
+ public AttributeEvent getStateEvent() {
+ return new AttributeEvent(
+ getState(),
+ null,
+ getValueTimestamp()
+ );
+ }
+
+ public boolean hasMetaItem(AssetMeta assetMeta) {
+ return hasMetaItem(assetMeta.getName());
+ }
+
+ public MetaItem firstMetaItem(AssetMeta assetMeta) {
+ return firstMetaItem(assetMeta.getName());
+ }
+
+ public boolean isProtected() {
+ return hasMetaItem(PROTECTED) && firstMetaItem(PROTECTED).isValueTrue();
+ }
+
+ public boolean isReadOnly() {
+ return hasMetaItem(READ_ONLY) && firstMetaItem(READ_ONLY).isValueTrue();
+ }
+
+ public boolean isStoreDatapoints() {
+ return hasMetaItem(STORE_DATA_POINTS) && firstMetaItem(STORE_DATA_POINTS).isValueTrue();
+ }
+
+ public boolean isRulesFact() {
+ return hasMetaItem(RULES_FACT) && firstMetaItem(RULES_FACT).isValueTrue();
+ }
+
+}
diff --git a/model/src/main/java/org/openremote/model/asset/AbstractAssetAttributes.java b/model/src/main/java/org/openremote/model/asset/AbstractAssetAttributes.java
new file mode 100644
index 000000000..0e3952fb3
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/asset/AbstractAssetAttributes.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2017, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.model.asset;
+
+import elemental.json.JsonObject;
+import org.openremote.model.Attributes;
+import org.openremote.model.Meta;
+import org.openremote.model.MetaItem;
+
+import static org.openremote.model.Constants.NAMESPACE;
+
+public abstract class AbstractAssetAttributes<CHILD extends AbstractAssetAttributes<CHILD, A>, A extends AbstractAssetAttribute> extends Attributes<CHILD, A> {
+
+ final public String assetId;
+
+ public AbstractAssetAttributes() {
+ this.assetId = null;
+ }
+
+ public AbstractAssetAttributes(JsonObject jsonObject) {
+ super(jsonObject);
+ this.assetId = null;
+ }
+
+ public AbstractAssetAttributes(CHILD attributes) {
+ super(attributes);
+ this.assetId = null;
+ }
+
+ public AbstractAssetAttributes(String assetId) {
+ this.assetId = assetId;
+ }
+
+ public AbstractAssetAttributes(String assetId, JsonObject jsonObject) {
+ super(jsonObject);
+ this.assetId = assetId;
+ }
+
+ public AbstractAssetAttributes(String assetId, CHILD attributes) {
+ super(attributes);
+ this.assetId = assetId;
+ }
+
+ public AbstractAssetAttributes(Asset asset) {
+ super(asset.getAttributes());
+ this.assetId = asset.getId();
+ }
+
+ public String getAssetId() {
+ if (assetId == null) {
+ throw new IllegalStateException("Asset identifier not set on: " + this);
+ }
+ return assetId;
+ }
+
+ /* TODO Do we need an "enabled" attribute on many asset types?
+ @SuppressWarnings("unchecked")
+ public CHILD setEnabled(boolean enabled) {
+ if (hasAttribute("enabled")) {
+ get("enabled").setValueAsBoolean(enabled);
+ } else {
+ A enabledAttribute = createAttribute("enabled", Json.createObject());
+ enabledAttribute.setType(AttributeType.BOOLEAN);
+ enabledAttribute.setValue(Json.create(enabled));
+ put(enabledAttribute);
+ }
+ return (CHILD) this;
+ }
+ */
+
+ /**
+ * Remove all attributes and meta items that are private.
+ */
+ public void filterProtected() {
+ for (A attribute : get()) {
+
+ if (!attribute.isProtected()) {
+ remove(attribute.getName());
+ continue;
+ }
+
+ if (!attribute.hasMeta())
+ continue;
+
+ // Any meta item of the attribute, if it's in our namespace, must be protected READ to be included
+ Meta protectedMeta = new Meta();
+ for (MetaItem metaItem : attribute.getMeta().all()) {
+ if (!metaItem.getName().startsWith(NAMESPACE))
+ continue;
+
+ AssetMeta wellKnownMeta = AssetMeta.byName(metaItem.getName());
+ if (wellKnownMeta != null && wellKnownMeta.getAccess().protectedRead) {
+ protectedMeta.add(
+ new MetaItem(metaItem.getName(), metaItem.getValue())
+ );
+ }
+ }
+ if (protectedMeta.size() > 0)
+ attribute.setMeta(protectedMeta);
+ }
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/asset/Asset.java b/model/src/main/java/org/openremote/model/asset/Asset.java
index b9c105830..62381e37c 100644
--- a/model/src/main/java/org/openremote/model/asset/Asset.java
+++ b/model/src/main/java/org/openremote/model/asset/Asset.java
@@ -21,17 +21,16 @@ package org.openremote.model.asset;
import elemental.json.JsonObject;
import org.hibernate.annotations.Formula;
-import org.openremote.model.*;
+import org.openremote.model.IdentifiableEntity;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Arrays;
-import java.util.Collections;
import java.util.Date;
-import java.util.List;
-import static org.openremote.model.Constants.*;
+import static org.openremote.model.Constants.PERSISTENCE_JSON_OBJECT_TYPE;
+import static org.openremote.model.Constants.PERSISTENCE_UNIQUE_ID_GENERATOR;
// @formatter:off
/**
@@ -207,42 +206,6 @@ import static org.openremote.model.Constants.*;
@MappedSuperclass
public class Asset implements IdentifiableEntity {
- static JsonObject filterProtectedAttributes(JsonObject originalAttributes) {
- if (originalAttributes == null)
- return null;
- Attributes filteredAttributes = new Attributes();
- for (Attribute originalAttribute : new Attributes(originalAttributes).get()) {
-
- // An attribute must be protected to be included
- if (!originalAttribute.isProtected()) {
- continue;
- }
-
- Attribute protectedAttribute = originalAttribute.copy();
- filteredAttributes.put(protectedAttribute);
-
- if (!protectedAttribute.hasMeta())
- continue;
-
- // Any meta item of the attribute, if it's in our namespace, must be protected READ to be included
- Meta protectedMeta = new Meta();
- for (MetaItem metaItem : protectedAttribute.getMeta().all()) {
- if (!metaItem.getName().startsWith(NAMESPACE))
- continue;
-
- AssetMeta wellKnownMeta = AssetMeta.byName(metaItem.getName());
- if (wellKnownMeta != null && wellKnownMeta.getAccess().protectedRead) {
- protectedMeta.add(
- new MetaItem(metaItem.getName(), metaItem.getValue())
- );
- }
- }
- if (protectedMeta.size() > 0)
- protectedAttribute.setMeta(protectedMeta);
-
- }
- return filteredAttributes.getJsonObject();
- }
@Id
@Column(name = "ID", length = 27)
@@ -333,7 +296,14 @@ public class Asset implements IdentifiableEntity {
this.tenantRealm = tenantRealm;
this.tenantDisplayName = tenantDisplayName;
this.path = path;
- this.attributes = filterProtectedAttributes ? filterProtectedAttributes(attributes) : attributes;
+
+ if (filterProtectedAttributes) {
+ AbstractAssetAttributes assetAttributes = new AssetAttributes(this.id, attributes);
+ assetAttributes.filterProtected();
+ this.attributes = assetAttributes.size() > 0 ? assetAttributes.getJsonObject() : null;
+ } else {
+ this.attributes = attributes;
+ }
}
public Asset(Asset parent) {
@@ -487,8 +457,8 @@ public class Asset implements IdentifiableEntity {
String[] newArray = new String[path.length];
int j = 0;
- for (int i=path.length; i>0; i--) {
- newArray[j] = path[i-1];
+ for (int i = path.length; i > 0; i--) {
+ newArray[j] = path[i - 1];
j++;
}
return newArray;
diff --git a/model/src/main/java/org/openremote/model/asset/AssetAttribute.java b/model/src/main/java/org/openremote/model/asset/AssetAttribute.java
new file mode 100644
index 000000000..c71581428
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/asset/AssetAttribute.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2017, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.model.asset;
+
+import elemental.json.JsonObject;
+import elemental.json.JsonValue;
+import org.openremote.model.AttributeType;
+
+public class AssetAttribute extends AbstractAssetAttribute<AssetAttribute> {
+
+ public AssetAttribute() {
+ }
+
+ public AssetAttribute(String assetId) {
+ super(assetId);
+ }
+
+ public AssetAttribute(String name, AttributeType type) {
+ super(name, type);
+ }
+
+ public AssetAttribute(String name, JsonObject jsonObject) {
+ super(name, jsonObject);
+ }
+
+ public AssetAttribute(String name, AttributeType type, JsonValue value) {
+ super(name, type, value);
+ }
+
+ public AssetAttribute(String assetId, String name) {
+ super(assetId, name);
+ }
+
+ public AssetAttribute(String assetId, String name, AttributeType type) {
+ super(assetId, name, type);
+ }
+
+ public AssetAttribute(String assetId, String name, JsonObject jsonObject) {
+ super(assetId, name, jsonObject);
+ }
+
+ public AssetAttribute(String assetId, String name, AttributeType type, JsonValue value) {
+ super(assetId, name, type, value);
+ }
+
+ public AssetAttribute(AbstractAssetAttribute attribute) {
+ super(attribute);
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/asset/AssetAttributes.java b/model/src/main/java/org/openremote/model/asset/AssetAttributes.java
new file mode 100644
index 000000000..53354ee13
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/asset/AssetAttributes.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2017, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.model.asset;
+
+import elemental.json.Json;
+import elemental.json.JsonObject;
+
+public class AssetAttributes extends AbstractAssetAttributes<AssetAttributes, AssetAttribute> {
+
+ public AssetAttributes() {
+ }
+
+ public AssetAttributes(JsonObject jsonObject) {
+ super(jsonObject);
+ }
+
+ public AssetAttributes(AssetAttributes attributes) {
+ super(attributes);
+ }
+
+ public AssetAttributes(String assetId) {
+ super(assetId);
+ }
+
+ public AssetAttributes(String assetId, JsonObject jsonObject) {
+ super(assetId, jsonObject);
+ }
+
+ public AssetAttributes(String assetId, AssetAttributes attributes) {
+ super(assetId, attributes);
+ }
+
+ public AssetAttributes(Asset asset) {
+ super(asset);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public AssetAttributes copy() {
+ return new AssetAttributes(assetId, Json.parse(getJsonObject().toJson()));
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ protected AssetAttribute createAttribute(String name, JsonObject jsonObject) {
+ return new AssetAttribute(assetId, name, jsonObject);
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/asset/AssetMeta.java b/model/src/main/java/org/openremote/model/asset/AssetMeta.java
index 9a3613fdc..e42d3d544 100644
--- a/model/src/main/java/org/openremote/model/asset/AssetMeta.java
+++ b/model/src/main/java/org/openremote/model/asset/AssetMeta.java
@@ -21,7 +21,6 @@ package org.openremote.model.asset;
import elemental.json.JsonType;
import elemental.json.JsonValue;
-import org.openremote.model.Attribute;
import org.openremote.model.MetaItem;
import org.openremote.model.units.AttributeUnits;
@@ -169,12 +168,6 @@ public enum AssetMeta {
return new MetaItem(name.getName(), value);
}
- public static MetaItem getFirst(Attribute attribute, AssetMeta meta) {
- return attribute.hasMetaItem(meta.getName())
- ? attribute.getMeta().first(meta.getName())
- : null;
- }
-
/**
* In theory, meta item values can be of any JSON type. In practice, these are the
* types we can edit/have editor UI for. We inspect the actual JSON value and
diff --git a/model/src/main/java/org/openremote/model/asset/AssetType.java b/model/src/main/java/org/openremote/model/asset/AssetType.java
index e3d2484c9..735942bcd 100644
--- a/model/src/main/java/org/openremote/model/asset/AssetType.java
+++ b/model/src/main/java/org/openremote/model/asset/AssetType.java
@@ -20,10 +20,7 @@
package org.openremote.model.asset;
import elemental.json.Json;
-import elemental.json.JsonObject;
-import org.openremote.model.Attribute;
import org.openremote.model.AttributeType;
-import org.openremote.model.Attributes;
import org.openremote.model.Meta;
import java.util.ArrayList;
@@ -46,34 +43,34 @@ public enum AssetType {
CUSTOM(null, true, null),
- BUILDING(ASSET_NAMESPACE + ":building", true, new Attributes().put(
- new Attribute("area", INTEGER)
+ BUILDING(ASSET_NAMESPACE + ":building", true, new AssetAttributes().put(
+ new AssetAttribute("area", INTEGER)
.setMeta(new Meta()
.add(createMetaItem(LABEL, Json.create("Surface area")))
.add(createMetaItem(DESCRIPTION, Json.create("Floor area of building measured in m²")))
.add(createMetaItem(ABOUT, Json.create("http://project-haystack.org/tag/area")))
),
- new Attribute("geoStreet", STRING)
+ new AssetAttribute("geoStreet", STRING)
.setMeta(new Meta()
.add(createMetaItem(LABEL, Json.create("Street")))
.add(createMetaItem(ABOUT, Json.create("http://project-haystack.org/tag/geoStreet")))
),
- new Attribute("geoPostalCode", AttributeType.INTEGER)
+ new AssetAttribute("geoPostalCode", AttributeType.INTEGER)
.setMeta(new Meta()
.add(createMetaItem(LABEL, Json.create("Postal Code")))
.add(createMetaItem(ABOUT, Json.create("http://project-haystack.org/tag/geoPostalCode")))
),
- new Attribute("geoCity", STRING)
+ new AssetAttribute("geoCity", STRING)
.setMeta(new Meta()
.add(createMetaItem(LABEL, Json.create("City")))
.add(createMetaItem(ABOUT, Json.create("http://project-haystack.org/tag/geoCity")))
),
- new Attribute("geoCountry", STRING)
+ new AssetAttribute("geoCountry", STRING)
.setMeta(new Meta()
.add(createMetaItem(LABEL, Json.create("Country")))
.add(createMetaItem(ABOUT, Json.create("http://project-haystack.org/tag/geoCountry")))
)
- ).getJsonObject()),
+ )),
FLOOR(ASSET_NAMESPACE + ":floor", true, null),
@@ -84,24 +81,24 @@ public enum AssetType {
AGENT(ASSET_NAMESPACE + ":agent", true, null),
/**
- * When a Thing asset is modified (created, updated, deleted), its attributes are examined
- * and linked to and unlinked from the configured Protocol.
+ * When a Thing asset is modified (created, updated, deleted), its attributes are examined
+ * and linked to and unlinked from the configured Protocol.
*/
THING(ASSET_NAMESPACE + ":thing", true, null);
final protected String value;
- final protected JsonObject defaultAttributes;
+ final protected AssetAttributes defaultAttributes;
- AssetType(String value, boolean editable, JsonObject defaultAttributes) {
+ AssetType(String value, boolean editable, AssetAttributes defaultAttributes) {
this.value = value;
- this.defaultAttributes = defaultAttributes;
+ this.defaultAttributes = defaultAttributes != null ? defaultAttributes : new AssetAttributes();
}
public String getValue() {
return value;
}
- public JsonObject getDefaultAttributes() {
+ public AssetAttributes getDefaultAttributes() {
return defaultAttributes;
}
diff --git a/model/src/main/java/org/openremote/model/asset/AssetUpdate.java b/model/src/main/java/org/openremote/model/asset/AssetUpdate.java
index 19126e691..690d4a31f 100644
--- a/model/src/main/java/org/openremote/model/asset/AssetUpdate.java
+++ b/model/src/main/java/org/openremote/model/asset/AssetUpdate.java
@@ -20,7 +20,6 @@
package org.openremote.model.asset;
import elemental.json.JsonValue;
-import org.openremote.model.Attribute;
import org.openremote.model.AttributeType;
import org.openremote.model.util.JsonUtil;
@@ -62,7 +61,7 @@ public class AssetUpdate {
COMPLETED
}
- final protected Attribute attribute;
+ final protected AbstractAssetAttribute attribute;
final protected Date assetCreatedOn;
@@ -96,11 +95,11 @@ public class AssetUpdate {
final protected Class<?> sender;
- public AssetUpdate(Asset asset, Attribute attribute) {
+ public AssetUpdate(Asset asset, AbstractAssetAttribute attribute) {
this(asset, attribute, null, 0, null);
}
- public AssetUpdate(Asset asset, Attribute attribute, JsonValue oldValue, long oldValueTimestamp, Class<?> sender) {
+ public AssetUpdate(Asset asset, AbstractAssetAttribute attribute, JsonValue oldValue, long oldValueTimestamp, Class<?> sender) {
this.attribute = attribute;
this.assetId = asset.getId();
this.assetName = asset.getName();
@@ -221,7 +220,7 @@ public class AssetUpdate {
// GETTERS AND SETTERS BELOW CAN ONLY BE USED WHEN STATUS IS NOT COMPLETED
/////////////////////////////////////////////////////////////////
- public Attribute getAttribute() {
+ public AbstractAssetAttribute getAttribute() {
if (!isCompleted()) {
return attribute;
}
diff --git a/model/src/main/java/org/openremote/model/asset/ThingAttribute.java b/model/src/main/java/org/openremote/model/asset/ThingAttribute.java
deleted file mode 100644
index 446ca6957..000000000
--- a/model/src/main/java/org/openremote/model/asset/ThingAttribute.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * Copyright 2016, OpenRemote Inc.
- *
- * See the CONTRIBUTORS.txt file in the distribution for a
- * full listing of individual contributors.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.openremote.model.asset;
-
-import elemental.json.JsonArray;
-import elemental.json.JsonObject;
-import org.openremote.model.Attribute;
-import org.openremote.model.AttributeRef;
-
-public class ThingAttribute extends Attribute {
-
- final protected ProtocolConfiguration protocolConfiguration;
- final protected String thingId;
-
- public static boolean isLinkedAttribute(Attribute attribute) {
- return getAgentLink(attribute) != null;
- }
-
- static public AttributeRef getAgentLink(Attribute attribute) {
- JsonArray array = attribute.hasMetaItem(AssetMeta.AGENT_LINK)
- ? attribute.firstMetaItem(AssetMeta.AGENT_LINK).getValueAsArray()
- : null;
- if (array == null || array.length() != 2)
- return null;
- return new AttributeRef(array);
- }
-
- public ThingAttribute(String thingId, ProtocolConfiguration protocolConfiguration, Attribute attribute) {
- this(thingId, protocolConfiguration, attribute.getName(), attribute.getJsonObject());
- }
-
- public ThingAttribute(String thingId, ProtocolConfiguration protocolConfiguration, String name, JsonObject jsonObject) {
- super(name, jsonObject);
- this.thingId = thingId;
- this.protocolConfiguration = protocolConfiguration;
- }
-
- public ProtocolConfiguration getProtocolConfiguration() {
- return protocolConfiguration;
- }
-
- public AttributeRef getAttributeRef() {
- return new AttributeRef(thingId, getName());
- }
-
- public String getThingId() {
- return thingId;
- }
-
- @Override
- public String toString() {
- return getClass().getSimpleName() + "{" +
- "protocolConfiguration=" + protocolConfiguration +
- ", thingId='" + thingId + '\'' +
- "} " + super.toString();
- }
-}
diff --git a/model/src/main/java/org/openremote/model/asset/agent/AgentAttribute.java b/model/src/main/java/org/openremote/model/asset/agent/AgentAttribute.java
new file mode 100644
index 000000000..81dc00840
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/asset/agent/AgentAttribute.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2017, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.model.asset.agent;
+
+import elemental.json.Json;
+import elemental.json.JsonObject;
+import elemental.json.JsonValue;
+import org.openremote.model.AttributeType;
+import org.openremote.model.asset.AbstractAssetAttribute;
+
+public class AgentAttribute<CHILD extends AgentAttribute<CHILD>> extends AbstractAssetAttribute<CHILD> {
+
+ public AgentAttribute() {
+ }
+
+ public AgentAttribute(String assetId) {
+ super(assetId);
+ }
+
+ public AgentAttribute(String name, AttributeType type) {
+ super(name, type);
+ }
+
+ public AgentAttribute(String name, JsonObject jsonObject) {
+ super(name, jsonObject);
+ }
+
+ public AgentAttribute(String name, AttributeType type, JsonValue value) {
+ super(name, type, value);
+ }
+
+ public AgentAttribute(String assetId, String name) {
+ super(assetId, name);
+ }
+
+ public AgentAttribute(String assetId, String name, AttributeType type) {
+ super(assetId, name, type);
+ }
+
+ public AgentAttribute(String assetId, String name, JsonObject jsonObject) {
+ super(assetId, name, jsonObject);
+ }
+
+ public AgentAttribute(String assetId, String name, AttributeType type, JsonValue value) {
+ super(assetId, name, type, value);
+ }
+
+ public AgentAttribute(AbstractAssetAttribute attribute) {
+ super(attribute);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public CHILD copy() {
+ return (CHILD) new AgentAttribute<CHILD>(getName(), Json.parse(getJsonObject().toJson()));
+ }
+
+}
diff --git a/model/src/main/java/org/openremote/model/asset/agent/AgentAttributes.java b/model/src/main/java/org/openremote/model/asset/agent/AgentAttributes.java
new file mode 100644
index 000000000..c04a59ec5
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/asset/agent/AgentAttributes.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2017, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.model.asset.agent;
+
+import elemental.json.Json;
+import elemental.json.JsonObject;
+import org.openremote.model.asset.AbstractAssetAttributes;
+import org.openremote.model.asset.Asset;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class AgentAttributes extends AbstractAssetAttributes<AgentAttributes, AgentAttribute> {
+
+ public AgentAttributes() {
+ }
+
+ public AgentAttributes(String assetId) {
+ super(assetId);
+ }
+
+ public AgentAttributes(String assetId, JsonObject jsonObject) {
+ super(assetId, jsonObject);
+ }
+
+ public AgentAttributes(AgentAttributes attributes) {
+ super(attributes);
+ }
+
+ public AgentAttributes(Asset asset) {
+ super(asset);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public AgentAttributes copy() {
+ return new AgentAttributes(assetId, Json.parse(getJsonObject().toJson()));
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ protected AgentAttribute createAttribute(String name, JsonObject jsonObject) {
+ return new AgentAttribute(assetId, name, jsonObject);
+ }
+
+ public List<ProtocolConfiguration> getProtocolConfigurations() {
+ List<AgentAttribute> attributes = super.get();
+ List<ProtocolConfiguration> protocolConfigurations = new ArrayList<>();
+ for (AgentAttribute attribute : attributes) {
+ if (ProtocolConfiguration.isProtocolConfiguration(attribute)) {
+ protocolConfigurations.add(new ProtocolConfiguration(attribute));
+ }
+ }
+ return Collections.unmodifiableList(protocolConfigurations);
+ }
+
+ public ProtocolConfiguration getProtocolConfiguration(String name) {
+ AgentAttribute attribute = super.get(name);
+ if (attribute == null || !ProtocolConfiguration.isProtocolConfiguration(attribute))
+ return null;
+ return new ProtocolConfiguration(attribute);
+ }
+
+}
diff --git a/model/src/main/java/org/openremote/model/asset/ProtocolConfiguration.java b/model/src/main/java/org/openremote/model/asset/agent/ProtocolConfiguration.java
similarity index 54%
rename from model/src/main/java/org/openremote/model/asset/ProtocolConfiguration.java
rename to model/src/main/java/org/openremote/model/asset/agent/ProtocolConfiguration.java
index 82a788710..658965f63 100644
--- a/model/src/main/java/org/openremote/model/asset/ProtocolConfiguration.java
+++ b/model/src/main/java/org/openremote/model/asset/agent/ProtocolConfiguration.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016, OpenRemote Inc.
+ * Copyright 2017, OpenRemote Inc.
*
* See the CONTRIBUTORS.txt file in the distribution for a
* full listing of individual contributors.
@@ -17,13 +17,14 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-package org.openremote.model.asset;
+package org.openremote.model.asset.agent;
import elemental.json.Json;
import elemental.json.JsonObject;
-import org.openremote.model.Attribute;
+import elemental.json.JsonValue;
import org.openremote.model.AttributeType;
import org.openremote.model.Meta;
+import org.openremote.model.asset.AbstractAssetAttribute;
import java.util.Locale;
@@ -32,29 +33,66 @@ import java.util.Locale;
* <p>
* Configuration details are managed as {@link Meta} of the attribute.
*/
-public class ProtocolConfiguration extends Attribute {
+public class ProtocolConfiguration extends AgentAttribute<ProtocolConfiguration> {
/**
* A protocol configuration attribute value must be a URN string representation starting with "urn:".
*/
- public static boolean isProtocolConfiguration(Attribute attribute) {
+ public static boolean isProtocolConfiguration(AgentAttribute attribute) {
return attribute.getType() == AttributeType.STRING
&& attribute.hasValue()
&& attribute.getValueAsString().toLowerCase(Locale.ROOT).startsWith("urn:");
}
- public ProtocolConfiguration(Attribute attribute) {
- this(attribute.getName(), attribute.getJsonObject());
+ public ProtocolConfiguration() {
+ }
+
+ public ProtocolConfiguration(String assetId) {
+ super(assetId);
+ }
+
+ public ProtocolConfiguration(String name, AttributeType type) {
+ super(name, type);
}
public ProtocolConfiguration(String name, JsonObject jsonObject) {
super(name, jsonObject);
}
+ public ProtocolConfiguration(String name, AttributeType type, JsonValue value) {
+ super(name, type, value);
+ }
+
public ProtocolConfiguration(String name, String protocol) {
super(name, AttributeType.STRING, Json.create(protocol));
}
+ public ProtocolConfiguration(String assetId, String name, AttributeType type) {
+ super(assetId, name, type);
+ }
+
+ public ProtocolConfiguration(String assetId, String name, JsonObject jsonObject) {
+ super(assetId, name, jsonObject);
+ }
+
+ public ProtocolConfiguration(String assetId, String name, AttributeType type, JsonValue value) {
+ super(assetId, name, type, value);
+ }
+
+ public ProtocolConfiguration(ProtocolConfiguration attribute) {
+ super(attribute);
+ }
+
+ public ProtocolConfiguration(AbstractAssetAttribute attribute) {
+ super(attribute);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public ProtocolConfiguration copy() {
+ return new ProtocolConfiguration(getName(), Json.parse(getJsonObject().toJson()));
+ }
+
public String getProtocolName() {
return getValueAsString();
}
diff --git a/model/src/main/java/org/openremote/model/asset/thing/ThingAttribute.java b/model/src/main/java/org/openremote/model/asset/thing/ThingAttribute.java
new file mode 100644
index 000000000..95178e70d
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/asset/thing/ThingAttribute.java
@@ -0,0 +1,109 @@
+/*
+ * Copyright 2017, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.model.asset.thing;
+
+import elemental.json.Json;
+import elemental.json.JsonArray;
+import elemental.json.JsonObject;
+import elemental.json.JsonValue;
+import org.openremote.model.AttributeRef;
+import org.openremote.model.AttributeType;
+import org.openremote.model.asset.AbstractAssetAttribute;
+import org.openremote.model.asset.AssetMeta;
+import org.openremote.model.asset.agent.ProtocolConfiguration;
+
+public class ThingAttribute extends AbstractAssetAttribute<ThingAttribute> {
+
+ protected ProtocolConfiguration protocolConfiguration;
+
+ public static boolean isLinkedAttribute(ThingAttribute attribute) {
+ return getAgentLink(attribute) != null;
+ }
+
+ static public AttributeRef getAgentLink(ThingAttribute attribute) {
+ JsonArray array = attribute.hasMetaItem(AssetMeta.AGENT_LINK)
+ ? attribute.firstMetaItem(AssetMeta.AGENT_LINK).getValueAsArray()
+ : null;
+ if (array == null || array.length() != 2)
+ return null;
+ return new AttributeRef(array);
+ }
+
+ public ThingAttribute() {
+ }
+
+ public ThingAttribute(String assetId) {
+ super(assetId);
+ }
+
+ public ThingAttribute(String name, AttributeType type) {
+ super(name, type);
+ }
+
+ public ThingAttribute(String name, JsonObject jsonObject) {
+ super(name, jsonObject);
+ }
+
+ public ThingAttribute(String name, AttributeType type, JsonValue value) {
+ super(name, type, value);
+ }
+
+ public ThingAttribute(String assetId, String name) {
+ super(assetId, name);
+ }
+
+ public ThingAttribute(String assetId, String name, AttributeType type) {
+ super(assetId, name, type);
+ }
+
+ public ThingAttribute(String assetId, String name, JsonObject jsonObject) {
+ super(assetId, name, jsonObject);
+ }
+
+ public ThingAttribute(String assetId, String name, AttributeType type, JsonValue value) {
+ super(assetId, name, type, value);
+ }
+
+ public ThingAttribute(AbstractAssetAttribute attribute) {
+ this(attribute, null);
+ }
+
+ public ThingAttribute(AbstractAssetAttribute attribute, ProtocolConfiguration protocolConfiguration) {
+ this(attribute.assetId, protocolConfiguration, attribute.getName(), attribute.getJsonObject());
+ }
+
+ public ThingAttribute(String assetId, ProtocolConfiguration protocolConfiguration, String name, JsonObject jsonObject) {
+ super(assetId, name, jsonObject);
+ this.protocolConfiguration = protocolConfiguration;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public ThingAttribute copy() {
+ return new ThingAttribute(assetId, protocolConfiguration, getName(), Json.parse(getJsonObject().toJson()));
+ }
+
+ /**
+ * Linked thing attributes have a protocol configuration of their agent after they are resolved.
+ */
+ public ProtocolConfiguration getProtocolConfiguration() {
+ return protocolConfiguration;
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/asset/thing/ThingAttributes.java b/model/src/main/java/org/openremote/model/asset/thing/ThingAttributes.java
new file mode 100644
index 000000000..53f507611
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/asset/thing/ThingAttributes.java
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2017, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.model.asset.thing;
+
+import elemental.json.Json;
+import elemental.json.JsonObject;
+import org.openremote.model.AttributeRef;
+import org.openremote.model.Function;
+import org.openremote.model.asset.AbstractAssetAttributes;
+import org.openremote.model.asset.Asset;
+import org.openremote.model.asset.agent.ProtocolConfiguration;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class ThingAttributes extends AbstractAssetAttributes<ThingAttributes, ThingAttribute> {
+
+ public ThingAttributes() {
+ }
+
+ public ThingAttributes(String assetId) {
+ super(assetId);
+ }
+
+ public ThingAttributes(String assetId, JsonObject jsonObject) {
+ super(assetId, jsonObject);
+ }
+
+ public ThingAttributes(ThingAttributes attributes) {
+ super(attributes);
+ }
+
+ public ThingAttributes(Asset asset) {
+ super(asset);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public ThingAttributes copy() {
+ return new ThingAttributes(assetId, Json.parse(getJsonObject().toJson()));
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ protected ThingAttribute createAttribute(String name, JsonObject jsonObject) {
+ return new ThingAttribute(assetId, null, name, jsonObject);
+ }
+
+ /**
+ * Get all attributes which are linked to an agent, grouped by protocol name.
+ */
+ public Map<String, List<ThingAttribute>> getLinkedAttributes(Function<AttributeRef, ProtocolConfiguration> linkResolver) {
+ List<ThingAttribute> attributes = super.get();
+
+ // Resolve all which are linked to an agent
+ List<ThingAttribute> linkedAttributes = new ArrayList<>();
+ for (ThingAttribute attribute : attributes) {
+ ThingAttribute linkedAttribute = getLinkedAttribute(linkResolver, attribute.getName());
+ if (linkedAttribute != null) {
+ linkedAttributes.add(linkedAttribute);
+ }
+ }
+
+ // Group by protocol name
+ Map<String, List<ThingAttribute>> result = new HashMap<>();
+ for (ThingAttribute linkedAttribute : linkedAttributes) {
+ String protocolName = linkedAttribute.getProtocolConfiguration().getProtocolName();
+ if (!result.containsKey(protocolName)) {
+ result.put(protocolName, new ArrayList<>());
+ }
+ result.get(protocolName).add(linkedAttribute);
+ }
+ return result;
+ }
+
+ public ThingAttribute getLinkedAttribute(Function<AttributeRef, ProtocolConfiguration> linkResolver,
+ String attributeName) {
+ ThingAttribute attribute = get(attributeName);
+ if (attribute == null || !ThingAttribute.isLinkedAttribute(attribute))
+ return null;
+
+ AttributeRef agentLink = ThingAttribute.getAgentLink(attribute);
+ if (agentLink == null)
+ return null;
+
+ ProtocolConfiguration protocolConfiguration = linkResolver.apply(agentLink);
+ if (protocolConfiguration == null) {
+ return null;
+ }
+
+ return new ThingAttribute(attribute, protocolConfiguration);
+ }
+}