Skip to content

armgit / core

Public Imported from GitHub
0

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

Protocol discovery/import enhancements to allow asset hierarchy

Rich Turner42af78da922c
diff --git a/agent/src/main/java/org/openremote/agent/protocol/ProtocolLinkedAttributeDiscovery.java b/agent/src/main/java/org/openremote/agent/protocol/ProtocolLinkedAttributeDiscovery.java
index b4b3cfc92..98ce22568 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/ProtocolLinkedAttributeDiscovery.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/ProtocolLinkedAttributeDiscovery.java
@@ -21,6 +21,7 @@ package org.openremote.agent.protocol;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
+import org.openremote.model.asset.AssetTreeNode;
/**
* To be used by protocols that support linked {@link AssetAttribute} discovery.
@@ -28,12 +29,13 @@ import org.openremote.model.asset.AssetAttribute;
* The discovery process should return {@link AssetAttribute}s that contain the necessary {@link org.openremote.model.attribute.MetaItem}s
* to establish a link to the specified {@link org.openremote.model.asset.agent.ProtocolConfiguration}.
* <p>
- * The returned {@link AssetAttribute}s must be logically grouped into {@link Asset}s.
+ * The returned {@link AssetAttribute}s must be logically grouped into {@link Asset}s and then ordered into the desired
+ * hierarchy using {@link AssetTreeNode}s.
*/
public interface ProtocolLinkedAttributeDiscovery {
/**
* Discover all linked {@link AssetAttribute}s for the specified {@link org.openremote.model.asset.agent.ProtocolConfiguration}.
*/
- Asset[] discoverLinkedAssetAttributes(AssetAttribute protocolConfiguration);
+ AssetTreeNode[] discoverLinkedAssetAttributes(AssetAttribute protocolConfiguration);
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/ProtocolLinkedAttributeImport.java b/agent/src/main/java/org/openremote/agent/protocol/ProtocolLinkedAttributeImport.java
index 53eb11ca1..8d8efa358 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/ProtocolLinkedAttributeImport.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/ProtocolLinkedAttributeImport.java
@@ -21,6 +21,7 @@ package org.openremote.agent.protocol;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
+import org.openremote.model.asset.AssetTreeNode;
import org.openremote.model.file.FileInfo;
/**
@@ -45,5 +46,5 @@ public interface ProtocolLinkedAttributeImport {
* using the supplied {@link FileInfo}.
* @throws IllegalStateException when there is an issue processing the provided {@link FileInfo}
*/
- Asset[] discoverLinkedAssetAttributes(AssetAttribute protocolConfiguration, FileInfo fileInfo) throws IllegalStateException;
+ AssetTreeNode[] discoverLinkedAssetAttributes(AssetAttribute protocolConfiguration, FileInfo fileInfo) throws IllegalStateException;
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/knx/KNXProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/knx/KNXProtocol.java
index a7fc20e08..c42d1dc3a 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/knx/KNXProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/knx/KNXProtocol.java
@@ -1,13 +1,13 @@
package org.openremote.agent.protocol.knx;
import org.openremote.agent.protocol.AbstractProtocol;
+import org.openremote.model.asset.AssetTreeNode;
import org.openremote.agent.protocol.ProtocolLinkedAttributeImport;
import org.openremote.container.util.CodecUtil;
import org.openremote.model.AbstractValueHolder;
import org.openremote.model.ValidationFailure;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
-import org.openremote.model.attribute.MetaItemType;
import org.openremote.model.asset.AssetType;
import org.openremote.model.asset.agent.AgentLink;
import org.openremote.model.asset.agent.ConnectionStatus;
@@ -428,7 +428,7 @@ public class KNXProtocol extends AbstractProtocol implements ProtocolLinkedAttri
}
@Override
- public Asset[] discoverLinkedAssetAttributes(AssetAttribute protocolConfiguration, FileInfo fileInfo) throws IllegalStateException {
+ public AssetTreeNode[] discoverLinkedAssetAttributes(AssetAttribute protocolConfiguration, FileInfo fileInfo) throws IllegalStateException {
ZipInputStream zin = null;
try {
@@ -489,9 +489,9 @@ public class KNXProtocol extends AbstractProtocol implements ProtocolLinkedAttri
LOG.info("Only group addresses ending on #A, #S, #AS or #SA will be imported. Ignoring: " + dp.getName());
}
}
-
- return createdAssets.values().toArray(new Asset[createdAssets.values().size()]);
-
+
+ return createdAssets.values().stream().map(AssetTreeNode::new).toArray(AssetTreeNode[]::new);
+
} catch (Exception e) {
throw new IllegalStateException("ETS import error", e);
} finally {
diff --git a/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.java
index a271abb79..35e82b51b 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.java
@@ -26,8 +26,8 @@ import org.openremote.agent.protocol.velbus.device.VelbusDeviceType;
import org.openremote.container.util.CodecUtil;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
-import org.openremote.model.attribute.MetaItemType;
import org.openremote.model.asset.AssetType;
+import org.openremote.model.asset.AssetTreeNode;
import org.openremote.model.asset.agent.AgentLink;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.attribute.*;
@@ -253,13 +253,13 @@ public abstract class AbstractVelbusProtocol extends AbstractProtocol implements
}
@Override
- public Asset[] discoverLinkedAssetAttributes(AssetAttribute protocolConfiguration) {
+ public AssetTreeNode[] discoverLinkedAssetAttributes(AssetAttribute protocolConfiguration) {
// TODO: Implement asset attribute discovery using the bus
- return new Asset[0];
+ return new AssetTreeNode[0];
}
@Override
- public Asset[] discoverLinkedAssetAttributes(AssetAttribute protocolConfiguration, FileInfo fileInfo) throws IllegalStateException {
+ public AssetTreeNode[] discoverLinkedAssetAttributes(AssetAttribute protocolConfiguration, FileInfo fileInfo) throws IllegalStateException {
Document xmlDoc;
try {
String xmlStr = fileInfo.isBinary() ? new String(CodecUtil.decodeBase64(fileInfo.getContents()), "UTF8") : fileInfo.getContents();
@@ -333,7 +333,7 @@ public abstract class AbstractVelbusProtocol extends AbstractProtocol implements
devices.add(device);
}
- return devices.toArray(new Asset[devices.size()]);
+ return devices.stream().map(AssetTreeNode::new).toArray(AssetTreeNode[]::new);
}
ProtocolExecutorService getExecutorService() {
diff --git a/client/src/main/java/org/openremote/app/client/assets/AssetTreeNodeArrayMapper.java b/client/src/main/java/org/openremote/app/client/assets/AssetTreeNodeArrayMapper.java
new file mode 100644
index 000000000..922727eb1
--- /dev/null
+++ b/client/src/main/java/org/openremote/app/client/assets/AssetTreeNodeArrayMapper.java
@@ -0,0 +1,32 @@
+/*
+ * 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.app.client.assets;
+
+import com.github.nmorel.gwtjackson.client.ObjectMapper;
+import com.github.nmorel.gwtjackson.client.annotation.JsonMixIns;
+import org.openremote.app.client.interop.jackson.DefaultJsonMixin;
+import org.openremote.app.client.rest.EntityReader;
+import org.openremote.model.asset.AssetTreeNode;
+
+@JsonMixIns({@JsonMixIns.JsonMixIn(target = AssetTreeNode.class, mixIn = DefaultJsonMixin.class)})
+public interface AssetTreeNodeArrayMapper
+ extends ObjectMapper<AssetTreeNode[]>,
+ EntityReader<AssetTreeNode[]> {
+}
diff --git a/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditActivity.java b/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditActivity.java
index 54767a4d3..d17c76658 100644
--- a/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditActivity.java
+++ b/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditActivity.java
@@ -76,6 +76,7 @@ public class AssetEditActivity
protected final AgentResource agentResource;
protected final AssetMapper assetMapper;
protected final AssetArrayMapper assetArrayMapper;
+ protected final AssetTreeNodeArrayMapper assetTreeNodeArrayMapper;
protected final AssetQueryMapper assetQueryMapper;
protected final ProtocolDescriptorArrayMapper protocolDescriptorArrayMapper;
protected final ProtocolDescriptorMapMapper protocolDescriptorMapMapper;
@@ -96,6 +97,7 @@ public class AssetEditActivity
AssetResource assetResource,
AgentResource agentResource,
AssetMapper assetMapper,
+ AssetTreeNodeArrayMapper assetTreeNodeArrayMapper,
AssetArrayMapper assetArrayMapper,
AssetQueryMapper assetQueryMapper,
ProtocolDescriptorArrayMapper protocolDescriptorArrayMapper,
@@ -113,6 +115,7 @@ public class AssetEditActivity
this.agentResource = agentResource;
this.assetMapper = assetMapper;
this.assetArrayMapper = assetArrayMapper;
+ this.assetTreeNodeArrayMapper = assetTreeNodeArrayMapper;
this.assetQueryMapper = assetQueryMapper;
this.protocolDescriptorArrayMapper = protocolDescriptorArrayMapper;
this.protocolDescriptorMapMapper = protocolDescriptorMapMapper;
@@ -694,7 +697,7 @@ public class AssetEditActivity
}
environment.getApp().getRequests().sendWithAndReturn(
- assetArrayMapper,
+ assetTreeNodeArrayMapper,
fileInfoMapper,
requestParams -> {
if (request.getFileInfo() != null) {
@@ -720,12 +723,27 @@ public class AssetEditActivity
view.setFormBusy(false);
view.setAvailableAttributeTypes(attributeTypesToList());
if (isImport) {
- showSuccess(environment.getMessages().protocolLinkImportSuccess(discoveredAssets.length));
+ showSuccess(environment.getMessages().protocolLinkImportSuccess(getAssetTreeNodeCount(discoveredAssets)));
} else {
- showSuccess(environment.getMessages().protocolLinkDiscoverySuccess(discoveredAssets.length));
+ showSuccess(environment.getMessages().protocolLinkDiscoverySuccess(getAssetTreeNodeCount(discoveredAssets)));
}
callback.run();
}
);
}
+
+ protected int getAssetTreeNodeCount(org.openremote.model.asset.AssetTreeNode assetTreeNodes[]) {
+ int count = 0;
+
+ if (assetTreeNodes != null) {
+ for (org.openremote.model.asset.AssetTreeNode node : assetTreeNodes) {
+ if (node.getAsset() != null) {
+ count++;
+ }
+ count += getAssetTreeNodeCount(node.getChildAssets());
+ }
+ }
+
+ return count;
+ }
}
diff --git a/manager/src/main/java/org/openremote/manager/agent/AgentConnector.java b/manager/src/main/java/org/openremote/manager/agent/AgentConnector.java
index 957bb0f41..57d9708e1 100644
--- a/manager/src/main/java/org/openremote/manager/agent/AgentConnector.java
+++ b/manager/src/main/java/org/openremote/manager/agent/AgentConnector.java
@@ -19,6 +19,7 @@
*/
package org.openremote.manager.agent;
+import org.openremote.model.asset.AssetTreeNode;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.agent.AgentStatusEvent;
@@ -49,7 +50,7 @@ public interface AgentConnector {
* @throws IllegalArgumentException when the protocol cannot be found for this {@link org.openremote.model.asset.agent.ProtocolConfiguration} reference
* @throws UnsupportedOperationException when the protocol doesn't implement {@link org.openremote.agent.protocol.ProtocolLinkedAttributeDiscovery}
*/
- Asset[] getDiscoveredLinkedAttributes(AttributeRef protocolConfigurationRef) throws IllegalArgumentException, UnsupportedOperationException;
+ AssetTreeNode[] getDiscoveredLinkedAttributes(AttributeRef protocolConfigurationRef) throws IllegalArgumentException, UnsupportedOperationException;
/**
* Ask the protocol for discovered linked {@link AssetAttribute}s using the supplied file (protocol specific file).
@@ -60,6 +61,6 @@ public interface AgentConnector {
* @throws UnsupportedOperationException when the protocol doesn't implement {@link org.openremote.agent.protocol.ProtocolLinkedAttributeDiscovery}
* @throws IllegalStateException thrown by the protocol if an error occurs processing the supplied {@link FileInfo}
*/
- Asset[] getDiscoveredLinkedAttributes(AttributeRef protocolConfigurationRef, FileInfo fileInfo) throws IllegalArgumentException, UnsupportedOperationException, IllegalStateException;
+ AssetTreeNode[] getDiscoveredLinkedAttributes(AttributeRef protocolConfigurationRef, FileInfo fileInfo) throws IllegalArgumentException, UnsupportedOperationException, IllegalStateException;
}
diff --git a/manager/src/main/java/org/openremote/manager/agent/AgentResourceImpl.java b/manager/src/main/java/org/openremote/manager/agent/AgentResourceImpl.java
index 6d1333b03..e8da3d2ae 100644
--- a/manager/src/main/java/org/openremote/manager/agent/AgentResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/agent/AgentResourceImpl.java
@@ -19,6 +19,7 @@
*/
package org.openremote.manager.agent;
+import org.openremote.model.asset.AssetTreeNode;
import org.openremote.container.timer.TimerService;
import org.openremote.manager.asset.AssetStorageService;
import org.openremote.manager.security.ManagerIdentityService;
@@ -40,10 +41,7 @@ import org.openremote.model.util.TextUtil;
import javax.ws.rs.*;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
+import java.util.*;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -129,7 +127,7 @@ public class AgentResourceImpl extends ManagerWebResource implements AgentResour
}
@Override
- public Asset[] searchForLinkedAttributes(RequestParams requestParams, String agentId, String protocolConfigurationName, String parentId, String realm) {
+ public AssetTreeNode[] searchForLinkedAttributes(RequestParams requestParams, String agentId, String protocolConfigurationName, String parentId, String realm) {
AttributeRef protocolConfigRef = new AttributeRef(agentId, protocolConfigurationName);
if (!isSuperUser() && TextUtil.isNullOrEmpty(realm)) {
@@ -139,7 +137,7 @@ public class AgentResourceImpl extends ManagerWebResource implements AgentResour
Asset parentAsset = getParent(parentId, realm);
// TODO: Allow user to select which assets/attributes are actually added to the DB
- Asset[] assets = withAgentConnector(
+ AssetTreeNode[] assets = withAgentConnector(
agentId,
agentConnector -> {
LOG.finer(
@@ -149,20 +147,12 @@ public class AgentResourceImpl extends ManagerWebResource implements AgentResour
}
);
- try {
- persistAssets(assets, parentAsset, realm);
- return assets;
- } catch (IllegalArgumentException e) {
- LOG.log(Level.WARNING, e.getMessage(), e);
- throw new NotFoundException(e.getMessage());
- } catch (UnsupportedOperationException e) {
- LOG.log(Level.WARNING, e.getMessage(), e);
- throw new NotSupportedException(e.getMessage());
- }
+ persistAssets(assets, parentAsset, realm);
+ return assets;
}
@Override
- public Asset[] importLinkedAttributes(RequestParams requestParams, String agentId, String protocolConfigurationName, String parentId, String realm, FileInfo fileInfo) {
+ public AssetTreeNode[] importLinkedAttributes(RequestParams requestParams, String agentId, String protocolConfigurationName, String parentId, String realm, FileInfo fileInfo) {
AttributeRef protocolConfigRef = new AttributeRef(agentId, protocolConfigurationName);
if (TextUtil.isNullOrEmpty(parentId) && TextUtil.isNullOrEmpty(realm)) {
@@ -175,7 +165,7 @@ public class AgentResourceImpl extends ManagerWebResource implements AgentResour
throw new BadRequestException("A file must be provided for import");
}
- Asset[] assets = withAgentConnector(
+ AssetTreeNode[] assets = withAgentConnector(
agentId,
agentConnector -> {
LOG.finer(
@@ -186,20 +176,8 @@ public class AgentResourceImpl extends ManagerWebResource implements AgentResour
}
);
- try {
- // TODO: Allow user to select which assets/attributes are actually added to the DB
- persistAssets(assets, parentAsset, realm);
- return assets;
- } catch (IllegalArgumentException e) {
- LOG.log(Level.WARNING, e.getMessage(), e);
- throw new NotFoundException(e.getMessage());
- } catch (UnsupportedOperationException e) {
- LOG.log(Level.WARNING, e.getMessage(), e);
- throw new NotSupportedException(e.getMessage());
- } catch (IllegalStateException e) {
- LOG.log(Level.SEVERE, e.getMessage(), e);
- throw new InternalServerErrorException(e.getMessage());
- }
+ persistAssets(assets, parentAsset, realm);
+ return assets;
}
protected <T> T withAgentConnector(String agentId, Function<Pair<Asset, AgentConnector>, T> function) {
@@ -216,19 +194,44 @@ public class AgentResourceImpl extends ManagerWebResource implements AgentResour
});
}
- protected void persistAssets(Asset[] assets, Asset parentAsset, String realm) {
- if (assets == null || assets.length == 0) {
- LOG.info("No assets to import");
- return;
- }
+ // TODO: Allow user to select which assets/attributes are actually added to the DB
+ protected void persistAssets(AssetTreeNode[] assets, Asset parentAsset, String realm) {
+ try {
+
+ if (assets == null || assets.length == 0) {
+ LOG.info("No assets to import");
+ return;
+ }
+
+ for (int i = 0; i < assets.length; i++) {
+ AssetTreeNode assetNode = assets[i];
+ Asset asset = assetNode.asset;
+
+ if (asset == null) {
+ LOG.info("Skipping node as asset not set");
+ continue;
+ }
+
+ asset.setId(null);
+ asset.setRealm(realm);
+ asset.setParent(parentAsset);
+ assetNode.asset = assetStorageService.merge(asset);
+
+ if (assetNode.children != null) {
+ persistAssets(assetNode.children, asset, realm);
+ }
+ }
- for (int i = 0; i < assets.length; i++) {
- Asset asset = assets[i];
- asset.setId(null);
- asset.setRealm(realm);
- asset.setParent(parentAsset);
- Asset newAsset = Asset.map(asset, new Asset());
- assets[i] = assetStorageService.merge(newAsset);
+
+ } catch (IllegalArgumentException e) {
+ LOG.log(Level.WARNING, e.getMessage(), e);
+ throw new NotFoundException(e.getMessage());
+ } catch (UnsupportedOperationException e) {
+ LOG.log(Level.WARNING, e.getMessage(), e);
+ throw new NotSupportedException(e.getMessage());
+ } catch (IllegalStateException e) {
+ LOG.log(Level.SEVERE, e.getMessage(), e);
+ throw new InternalServerErrorException(e.getMessage());
}
}
diff --git a/manager/src/main/java/org/openremote/manager/agent/LocalAgentConnector.java b/manager/src/main/java/org/openremote/manager/agent/LocalAgentConnector.java
index 52898085b..12391ca3e 100644
--- a/manager/src/main/java/org/openremote/manager/agent/LocalAgentConnector.java
+++ b/manager/src/main/java/org/openremote/manager/agent/LocalAgentConnector.java
@@ -20,6 +20,7 @@
package org.openremote.manager.agent;
import org.openremote.agent.protocol.Protocol;
+import org.openremote.model.asset.AssetTreeNode;
import org.openremote.agent.protocol.ProtocolLinkedAttributeDiscovery;
import org.openremote.agent.protocol.ProtocolLinkedAttributeImport;
import org.openremote.model.ValidationFailure;
@@ -88,7 +89,7 @@ public class LocalAgentConnector implements AgentConnector {
}
@Override
- public Asset[] getDiscoveredLinkedAttributes(AttributeRef protocolConfigurationRef) throws IllegalArgumentException, UnsupportedOperationException {
+ public AssetTreeNode[] getDiscoveredLinkedAttributes(AttributeRef protocolConfigurationRef) throws IllegalArgumentException, UnsupportedOperationException {
Optional<Pair<Protocol, AssetAttribute>> protocolAndConfigOptional = getProtocolAndConfig(protocolConfigurationRef);
if (!protocolAndConfigOptional.isPresent()) {
@@ -106,11 +107,11 @@ public class LocalAgentConnector implements AgentConnector {
ProtocolLinkedAttributeDiscovery discoveryProtocol = (ProtocolLinkedAttributeDiscovery)protocolAndConfig.key;
return discoveryProtocol.discoverLinkedAssetAttributes(protocolAndConfig.value);
})
- .orElse(new Asset[0]);
+ .orElse(new AssetTreeNode[0]);
}
@Override
- public Asset[] getDiscoveredLinkedAttributes(AttributeRef protocolConfigurationRef, FileInfo fileInfo) throws IllegalArgumentException, UnsupportedOperationException, IllegalStateException {
+ public AssetTreeNode[] getDiscoveredLinkedAttributes(AttributeRef protocolConfigurationRef, FileInfo fileInfo) throws IllegalArgumentException, UnsupportedOperationException, IllegalStateException {
Optional<Pair<Protocol, AssetAttribute>> protocolAndConfigOptional = getProtocolAndConfig(protocolConfigurationRef);
if (!protocolAndConfigOptional.isPresent()) {
diff --git a/model/src/main/java/org/openremote/model/asset/AssetTreeNode.java b/model/src/main/java/org/openremote/model/asset/AssetTreeNode.java
new file mode 100644
index 000000000..e29ab0c14
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/asset/AssetTreeNode.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2019, 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 com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+import java.util.Arrays;
+
+/**
+ * This is used to store {@link Asset} hierarchy information before {@link Asset}s have been assigned IDs
+ */
+public class AssetTreeNode {
+
+ public Asset asset;
+ public AssetTreeNode[] children;
+
+ public AssetTreeNode(Asset asset) {
+ this.asset = asset;
+ }
+
+ @JsonCreator
+ public AssetTreeNode(@JsonProperty("asset") Asset asset,
+ @JsonProperty("children") AssetTreeNode... children) {
+ this.asset = asset;
+ this.children = children;
+ }
+
+ public Asset getAsset() {
+ return asset;
+ }
+
+ public AssetTreeNode[] getChildAssets() {
+ return children;
+ }
+
+ public AssetTreeNode addChild(AssetTreeNode asset) {
+ if (this == asset) {
+ throw new IllegalArgumentException("Cannot add parent asset as a child");
+ }
+
+ if (children != null) {
+ children = Arrays.copyOf(children, children.length+1);
+ children[children.length-1] = asset;
+ } else {
+ children = new AssetTreeNode[] {asset};
+ }
+
+ return this;
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/asset/agent/AgentResource.java b/model/src/main/java/org/openremote/model/asset/agent/AgentResource.java
index e381256d9..2502f7a5b 100644
--- a/model/src/main/java/org/openremote/model/asset/agent/AgentResource.java
+++ b/model/src/main/java/org/openremote/model/asset/agent/AgentResource.java
@@ -23,6 +23,7 @@ import jsinterop.annotations.JsType;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.AssetResource;
+import org.openremote.model.asset.AssetTreeNode;
import org.openremote.model.attribute.AttributeValidationResult;
import org.openremote.model.file.FileInfo;
import org.openremote.model.http.RequestParams;
@@ -131,7 +132,7 @@ public interface AgentResource {
@Produces(APPLICATION_JSON)
@SuccessStatusCode(200)
@SuppressWarnings("unusable-by-js")
- Asset[] searchForLinkedAttributes(
+ AssetTreeNode[] searchForLinkedAttributes(
@BeanParam RequestParams requestParams,
@PathParam("agentId") String agentId,
@PathParam("protocolConfigurationName") String protocolConfigurationName,
@@ -155,7 +156,7 @@ public interface AgentResource {
@SuccessStatusCode(200)
@SuppressWarnings("unusable-by-js")
// TODO: File upload should use standard multipart mechanism
- Asset[] importLinkedAttributes(
+ AssetTreeNode[] importLinkedAttributes(
@BeanParam RequestParams requestParams,
@PathParam("agentId") String agentId,
@PathParam("protocolConfigurationName") String protocolConfigurationName,
diff --git a/test/src/test/groovy/org/openremote/test/protocol/KNXImportTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/KNXImportTest.groovy
index dc5c76d1e..9f80aa8f9 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/KNXImportTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/KNXImportTest.groovy
@@ -111,10 +111,10 @@ class KNXImportTest extends Specification implements ManagerContainerTrait {
assert assets != null
assert assets.length == 12
assert assets.each {
- !TextUtil.isNullOrEmpty(it.id) &&
- !TextUtil.isNullOrEmpty(it.getName()) &&
- !it.getAttributesList().isEmpty() &&
- it.getAttributesStream().allMatch({attr ->
+ !TextUtil.isNullOrEmpty(it.asset.id) &&
+ !TextUtil.isNullOrEmpty(it.asset.getName()) &&
+ !it.asset.getAttributesList().isEmpty() &&
+ it.asset.getAttributesStream().allMatch({attr ->
AgentLink.getAgentLink(attr)
.map({agentLink -> agentLink.entityId == knxAgent.id && agentLink.attributeName == "knxConfigError1"})
.orElse(false)
@@ -124,10 +124,10 @@ class KNXImportTest extends Specification implements ManagerContainerTrait {
}
and: "a given asset should have the correct attributes (Target Temperature)"
- def asset = assets.find {it.name == "Target Temperature"}
+ def asset = assets.find {it.asset.name == "Target Temperature"}
assert asset != null
- assert asset.getAttributesList().size() == 1
- def attribute = asset.getAttribute("TargetTemperature").get()
+ assert asset.asset.getAttributesList().size() == 1
+ def attribute = asset.asset.getAttribute("TargetTemperature").get()
assert attribute != null
def metaItem = attribute.getMetaItem(KNXProtocol.META_KNX_STATUS_GA).get()
assert metaItem != null
diff --git a/test/src/test/groovy/org/openremote/test/protocol/velbus/VelbusProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/velbus/VelbusProtocolTest.groovy
index 8ac7398e4..510d461a2 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/velbus/VelbusProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/velbus/VelbusProtocolTest.groovy
@@ -200,10 +200,10 @@ class VelbusProtocolTest extends Specification implements ManagerContainerTrait
assert assets != null
assert assets.length == 13
assert assets.each {
- !TextUtil.isNullOrEmpty(it.id) &&
- !TextUtil.isNullOrEmpty(it.getName()) &&
- !it.getAttributesList().isEmpty() &&
- it.getAttributesStream().allMatch({attr ->
+ !TextUtil.isNullOrEmpty(it.asset.id) &&
+ !TextUtil.isNullOrEmpty(it.asset.getName()) &&
+ !it.asset.getAttributesList().isEmpty() &&
+ it.asset.getAttributesStream().allMatch({attr ->
AgentLink.getAgentLink(attr)
.map({agentLink -> agentLink.entityId == agent.id && agentLink.attributeName == "protocolConfig"})
.orElse(false)
@@ -212,10 +212,10 @@ class VelbusProtocolTest extends Specification implements ManagerContainerTrait
}
and: "a given asset should have the correct attributes (VMBGPOD)"
- def asset = assets.find {it.name == "VMBGPOD"}
+ def asset = assets.find {it.asset.name == "VMBGPOD"}
assert asset != null
- assert asset.getAttributesList().size() == 303
- def memoTextAttribute = asset.getAttributesList().find {VelbusConfiguration.getVelbusDevicePropertyLink(it) == "MEMO_TEXT"}
+ assert asset.asset.getAttributesList().size() == 303
+ def memoTextAttribute = asset.asset.getAttributesList().find {VelbusConfiguration.getVelbusDevicePropertyLink(it) == "MEMO_TEXT"}
assert memoTextAttribute != null
assert VelbusConfiguration.getVelbusDeviceAddress(memoTextAttribute) == 24