Skip to content

armgit / core

Public Imported from GitHub
0

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

Minor Attribute update

Rich Turnere77f1869cbaa
diff --git a/model/src/main/java/org/openremote/model/Attribute.java b/model/src/main/java/org/openremote/model/Attribute.java
index 1ca1636a2..2e6bd7eca 100644
--- a/model/src/main/java/org/openremote/model/Attribute.java
+++ b/model/src/main/java/org/openremote/model/Attribute.java
@@ -234,20 +234,20 @@ public abstract class Attribute extends AbstractValueTimestampHolder {
return null;
}
- public static Consumer<Attribute> setAttributeLink(String metaName, AttributeRef attributeRef) {
+ public static Function<Attribute, Attribute> setAttributeLink(String metaName, AttributeRef attributeRef) {
return attribute -> {
if (attributeRef == null) {
throw new IllegalArgumentException(
"Attribute reference cannot be null for '" + metaName + "' on: " + attribute
);
}
- attribute.setMeta(
+ return attribute.setMeta(
attribute.getMeta().replace(metaName, new MetaItem(metaName, attributeRef.asJsonValue()))
);
};
}
- public static Consumer<Attribute> removeAttributeLink(String metaName) {
+ public static Function<Attribute, Attribute> removeAttributeLink(String metaName) {
return attribute -> attribute.setMeta(attribute.getMeta().removeAll(metaName));
}
}
diff --git a/model/src/main/java/org/openremote/model/asset/AssetAttribute.java b/model/src/main/java/org/openremote/model/asset/AssetAttribute.java
index 46a87b99c..1661bde97 100644
--- a/model/src/main/java/org/openremote/model/asset/AssetAttribute.java
+++ b/model/src/main/java/org/openremote/model/asset/AssetAttribute.java
@@ -195,10 +195,7 @@ public class AssetAttribute extends Attribute {
}
public static Function<Asset, AssetAttribute> findAssetAttribute(String name) {
- return asset -> asset.getAttributeStream()
- .filter(attribute -> attribute.getName().equals(name))
- .findFirst()
- .orElse(null);
+ return asset -> getAssetAttributeFromJson(asset.getId(), name).apply(asset.getAttributes());
}
public static Predicate<Asset> containsAssetAttributeNamed(String name) {
@@ -261,6 +258,15 @@ public class AssetAttribute extends Attribute {
};
}
+ public static Function<JsonObject, AssetAttribute> getAssetAttributeFromJson(String assetId, String attributeName) {
+ return jsonObject -> {
+ if (jsonObject == null || jsonObject.keys().length == 0 || !jsonObject.hasKey(attributeName)) {
+ return null;
+ }
+ return new AssetAttribute(assetId, attributeName, jsonObject.getObject(attributeName));
+ };
+ }
+
/**
* Maps the attribute stream to a {@link JsonObject}, duplicate attribute names are not
* allowed (internally this is a hash map in Elemental, thus not allowing duplicate
diff --git a/model/src/main/java/org/openremote/model/asset/agent/AgentLink.java b/model/src/main/java/org/openremote/model/asset/agent/AgentLink.java
index daebd49f6..76dd55edc 100644
--- a/model/src/main/java/org/openremote/model/asset/agent/AgentLink.java
+++ b/model/src/main/java/org/openremote/model/asset/agent/AgentLink.java
@@ -56,11 +56,11 @@ final public class AgentLink {
return getAttributeLink(AGENT_LINK.getUrn());
}
- public static Consumer<Attribute> setAgentLink(AttributeRef protocolConfigurationRef) {
+ public static Function<Attribute, Attribute> setAgentLink(AttributeRef protocolConfigurationRef) {
return setAttributeLink(AGENT_LINK.getUrn(), protocolConfigurationRef);
}
- public static Consumer<Attribute> removeAgentLink(Attribute attribute) {
+ public static Function<Attribute, Attribute> removeAgentLink() {
return removeAttributeLink(AGENT_LINK.getUrn());
}
}
diff --git a/model/src/main/java/org/openremote/model/asset/macro/MacroConfiguration.java b/model/src/main/java/org/openremote/model/asset/macro/MacroConfiguration.java
index 4a2f9c03f..3dacca3d8 100644
--- a/model/src/main/java/org/openremote/model/asset/macro/MacroConfiguration.java
+++ b/model/src/main/java/org/openremote/model/asset/macro/MacroConfiguration.java
@@ -28,7 +28,6 @@ import org.openremote.model.asset.AssetMeta;
import org.openremote.model.util.JsonUtil;
import java.util.List;
-import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.IntStream;
@@ -85,22 +84,24 @@ final public class MacroConfiguration {
.map(getMacroActionFromMetaItem());
}
- public static Consumer<AssetAttribute> addMacroAction(MacroAction action) {
+ public static Function<AssetAttribute, AssetAttribute> addMacroAction(MacroAction action) {
return attribute -> attribute.setMeta(
attribute.getMeta().add(getMetaItemFromMacroAction().apply(action))
);
}
- public static Consumer<AssetAttribute> removeMacroAction(MacroAction action) {
+ public static Function<AssetAttribute, AssetAttribute> removeMacroAction(MacroAction action) {
return attribute -> {
if (!attribute.hasMeta()) {
- return;
+ return attribute;
}
List<MetaItem> metaItems = attribute.getMeta().all();
IntStream.range(0, metaItems.size())
.filter(i -> isAssetMetaItem(MACRO_ACTION).test(metaItems.get(i)))
.filter(i -> JsonUtil.equals(metaItems.get(i).getValueAsObject(), action.asJsonValue()))
.forEach(i -> attribute.getMeta().remove(i));
+
+ return attribute;
};
}
}