Skip to content

armgit / core

Public Imported from GitHub
0

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

Protocol linked attribute discovery

Rich Turner737c7ed0dc3d
diff --git a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
index 6417b23d0..b26b572d9 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
@@ -296,8 +296,8 @@ public abstract class AbstractProtocol implements Protocol {
getVersion(),
this instanceof ProtocolConfigurationDiscovery,
this instanceof ProtocolConfigurationImport,
- this instanceof ProtocolDeviceDiscovery,
- this instanceof ProtocolDeviceImport,
+ this instanceof ProtocolLinkedAttributeDiscovery,
+ this instanceof ProtocolLinkedAttributeImport,
getProtocolConfigurationTemplate(),
getProtocolConfigurationMetaItemDescriptors(),
getLinkedAttributeMetaItemDescriptors()
diff --git a/agent/src/main/java/org/openremote/agent/protocol/ProtocolConfigurationDiscovery.java b/agent/src/main/java/org/openremote/agent/protocol/ProtocolConfigurationDiscovery.java
index 170a88621..5b38e8681 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/ProtocolConfigurationDiscovery.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/ProtocolConfigurationDiscovery.java
@@ -29,5 +29,5 @@ public interface ProtocolConfigurationDiscovery {
/**
* Get discovered {@link org.openremote.model.asset.agent.ProtocolConfiguration}s.
*/
- AssetAttribute[] getProtocolConfigurationsByDiscovery();
+ AssetAttribute[] discoverProtocolConfigurations();
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/ProtocolDeviceDiscovery.java b/agent/src/main/java/org/openremote/agent/protocol/ProtocolLinkedAttributeDiscovery.java
similarity index 50%
rename from agent/src/main/java/org/openremote/agent/protocol/ProtocolDeviceDiscovery.java
rename to agent/src/main/java/org/openremote/agent/protocol/ProtocolLinkedAttributeDiscovery.java
index 3eaef5a52..b4b3cfc92 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/ProtocolDeviceDiscovery.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/ProtocolLinkedAttributeDiscovery.java
@@ -23,24 +23,17 @@ import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
/**
- * To be used by protocols that support device discovery.
+ * To be used by protocols that support linked {@link AssetAttribute} discovery.
* <p>
- * The discovery process should return an array of {@link Asset}s that represent the physical devices
- * connected to a particular {@link org.openremote.model.asset.agent.ProtocolConfiguration}. The device
- * assets should contain all the appropriate {@link AssetAttribute}s needed for interacting with the
- * device. Each {@link AssetAttribute} should be of the correct type and have any required
- * {@link org.openremote.model.attribute.MetaItem}s already set.
+ * 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>
- * <b>
- * NOTE: It is not necessary to set an {@link org.openremote.model.asset.agent.AgentLink}
- * {@link org.openremote.model.attribute.MetaItem} for each attribute as the system will insert these
- * automatically if omitted.
- * </b>
+ * The returned {@link AssetAttribute}s must be logically grouped into {@link Asset}s.
*/
-public interface ProtocolDeviceDiscovery {
+public interface ProtocolLinkedAttributeDiscovery {
/**
- * Get discovered devices for the specified {@link org.openremote.model.asset.agent.ProtocolConfiguration}.
+ * Discover all linked {@link AssetAttribute}s for the specified {@link org.openremote.model.asset.agent.ProtocolConfiguration}.
*/
- Asset[] getDevicesByDiscovery(AssetAttribute protocolConfiguration);
+ Asset[] discoverLinkedAssetAttributes(AssetAttribute protocolConfiguration);
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/ProtocolDeviceImport.java b/agent/src/main/java/org/openremote/agent/protocol/ProtocolLinkedAttributeImport.java
similarity index 83%
rename from agent/src/main/java/org/openremote/agent/protocol/ProtocolDeviceImport.java
rename to agent/src/main/java/org/openremote/agent/protocol/ProtocolLinkedAttributeImport.java
index f63a04f2c..53eb11ca1 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/ProtocolDeviceImport.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.file.FileInfo;
/**
* To be used by protocols that support device import from one or more protocol specific files.
@@ -37,11 +38,12 @@ import org.openremote.model.asset.AssetAttribute;
* automatically if omitted.
* </b>
*/
-public interface ProtocolDeviceImport {
+public interface ProtocolLinkedAttributeImport {
/**
* Import devices for the specified {@link org.openremote.model.asset.agent.ProtocolConfiguration}
- * using the supplied file(s).
+ * using the supplied {@link FileInfo}.
+ * @throws IllegalStateException when there is an issue processing the provided {@link FileInfo}
*/
- Asset[] getDevicesByDiscovery(AssetAttribute protocolConfiguration);
+ Asset[] discoverLinkedAssetAttributes(AssetAttribute protocolConfiguration, FileInfo fileInfo) throws IllegalStateException;
}
diff --git a/manager/server/src/main/java/org/openremote/manager/server/agent/AgentConnector.java b/manager/server/src/main/java/org/openremote/manager/server/agent/AgentConnector.java
index e28d08983..a0c51fa8b 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/agent/AgentConnector.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/agent/AgentConnector.java
@@ -22,14 +22,38 @@ package org.openremote.manager.server.agent;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.agent.ProtocolDescriptor;
+import org.openremote.model.attribute.AttributeRef;
import org.openremote.model.attribute.AttributeValidationResult;
+import org.openremote.model.file.FileInfo;
/**
* This is an interface for communicating with an agent. It
* should be stateless and re-usable.
*/
public interface AgentConnector {
+
ProtocolDescriptor[] getProtocolDescriptors(Asset agent);
AttributeValidationResult validateProtocolConfiguration(AssetAttribute protocolConfiguration);
+
+ /**
+ * Ask the protocol for discovered linked {@link AssetAttribute}s.
+ * <p>
+ * <b>NOTE:</b>The protocol must implement {@link org.openremote.agent.protocol.ProtocolLinkedAttributeDiscovery}
+ *
+ * @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;
+
+ /**
+ * Ask the protocol for discovered linked {@link AssetAttribute}s using the supplied file (protocol specific file).
+ * <p>
+ * <b>NOTE:</b>The protocol must implement {@link org.openremote.agent.protocol.ProtocolLinkedAttributeImport}
+ *
+ * @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}
+ * @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;
}
diff --git a/manager/server/src/main/java/org/openremote/manager/server/agent/AgentResourceImpl.java b/manager/server/src/main/java/org/openremote/manager/server/agent/AgentResourceImpl.java
index 1cadee6fd..065592fc8 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/agent/AgentResourceImpl.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/agent/AgentResourceImpl.java
@@ -19,31 +19,45 @@
*/
package org.openremote.manager.server.agent;
+import org.openremote.container.timer.TimerService;
import org.openremote.manager.server.asset.AssetResourceImpl;
+import org.openremote.manager.server.asset.AssetStorageService;
import org.openremote.manager.server.asset.ServerAsset;
-import org.openremote.manager.server.web.ManagerWebService;
+import org.openremote.manager.server.security.ManagerIdentityService;
+import org.openremote.manager.server.web.ManagerWebResource;
import org.openremote.manager.shared.agent.AgentResource;
import org.openremote.manager.shared.http.RequestParams;
+import org.openremote.manager.shared.security.Tenant;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.AssetType;
import org.openremote.model.asset.agent.ProtocolDescriptor;
+import org.openremote.model.attribute.AttributeRef;
import org.openremote.model.attribute.AttributeValidationResult;
-import org.openremote.model.util.Pair;
+import org.openremote.model.file.FileInfo;
+import javax.ws.rs.*;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
import java.util.Optional;
+import java.util.logging.Level;
import java.util.logging.Logger;
-public class AgentResourceImpl extends ManagerWebService implements AgentResource {
+import static org.openremote.model.util.TextUtil.isNullOrEmpty;
+
+public class AgentResourceImpl extends ManagerWebResource implements AgentResource {
private static final Logger LOG = Logger.getLogger(AssetResourceImpl.class.getName());
- protected AgentService agentService;
+ protected final AgentService agentService;
+ protected final AssetStorageService assetStorageService;
- public AgentResourceImpl(AgentService agentService) {
+ public AgentResourceImpl(TimerService timerService,
+ ManagerIdentityService identityService,
+ AssetStorageService assetStorageService,
+ AgentService agentService) {
+ super(timerService, identityService);
this.agentService = agentService;
+ this.assetStorageService = assetStorageService;
}
@Override
@@ -68,8 +82,7 @@ public class AgentResourceImpl extends ManagerWebService implements AgentResourc
@Override
public Map<String, ProtocolDescriptor[]> getAllSupportedProtocols(RequestParams requestParams) {
Map<String, ProtocolDescriptor[]> agentDescriptorMap = new HashMap<>(agentService.getAgents().size());
- agentService.getAgents().forEach((id, agent) -> {
-
+ agentService.getAgents().forEach((id, agent) ->
agentDescriptorMap.put(
id,
agentService.getAgentConnector(agent)
@@ -81,8 +94,7 @@ public class AgentResourceImpl extends ManagerWebService implements AgentResourc
LOG.warning("Agent connector not found for agent ID: " + id);
return new IllegalStateException("Agent connector not found or returned invalid response");
})
- );
- });
+ ));
return agentDescriptorMap;
}
@@ -95,7 +107,7 @@ public class AgentResourceImpl extends ManagerWebService implements AgentResourc
@Override
public AttributeValidationResult validateProtocolConfiguration(RequestParams requestParams, String agentId, AssetAttribute protocolConfiguration) {
return findAgent(agentId)
- .flatMap(agent -> agentService.getAgentConnector(agent))
+ .flatMap(agentService::getAgentConnector)
.map(agentConnector -> {
LOG.finer("Asking connector '" + agentConnector.getClass().getSimpleName() + "' to validate protocol configuration: " + protocolConfiguration);
return agentConnector.validateProtocolConfiguration(protocolConfiguration);
@@ -106,6 +118,58 @@ public class AgentResourceImpl extends ManagerWebService implements AgentResourc
});
}
+ @Override
+ public Asset[] getDiscoveredLinkedAttributes(RequestParams requestParams, String agentId, String protocolConfigurationName, String parentId) {
+ AttributeRef protocolConfigRef = new AttributeRef(agentId, protocolConfigurationName);
+ AgentConnector agentConnector = findAgent(agentId)
+ .flatMap(agentService::getAgentConnector)
+ .orElseThrow(() -> new NotFoundException("Agent connector not found for: " + agentId));
+
+ LOG.finer("Asking connector '" + agentConnector.getClass().getSimpleName() + "' to do linked attribute discovery for protocol configuration: " + protocolConfigRef);
+ try {
+ // TODO: Allow user to select which assets/attributes are actually added to the DB
+ Asset[] assets = agentConnector.getDiscoveredLinkedAttributes(protocolConfigRef);
+ persistAssets(assets, parentId);
+ 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());
+ }
+ }
+
+ @Override
+ public Asset[] getDiscoveredLinkedAttributes(RequestParams requestParams, String agentId, String protocolConfigurationName, String parentId, FileInfo fileInfo) {
+ AttributeRef protocolConfigRef = new AttributeRef(agentId, protocolConfigurationName);
+ AgentConnector agentConnector = findAgent(agentId)
+ .flatMap(agentService::getAgentConnector)
+ .orElseThrow(() -> new NotFoundException("Agent connector not found for: " + agentId));
+
+ if (fileInfo == null || fileInfo.getContents() == null) {
+ throw new BadRequestException("A file must be provided for import");
+ }
+
+ LOG.finer("Asking connector '" + agentConnector.getClass().getSimpleName() + "' to do linked attribute discovery using uploaded file for protocol configuration: " + protocolConfigRef);
+
+ try {
+ // TODO: Allow user to select which assets/attributes are actually added to the DB
+ Asset[] assets = agentConnector.getDiscoveredLinkedAttributes(protocolConfigRef, fileInfo);
+ persistAssets(assets, parentId);
+ 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());
+ }
+ }
+
protected Optional<Asset> findAgent(String agentId) {
// Find the agent
LOG.finer("Find agent: " + agentId);
@@ -116,4 +180,41 @@ public class AgentResourceImpl extends ManagerWebService implements AgentResourc
}
return Optional.of(agent);
}
+
+ protected void persistAssets(Asset[] assets, String parentId) {
+ if (assets == null || assets.length == 0) {
+ LOG.info("No assets to import");
+ return;
+ }
+
+ if (isRestrictedUser()) {
+ throw new ForbiddenException("User is restricted");
+ }
+
+ // Assets must be added in the same realm as the user (unless super user)
+ Asset parentAsset = isNullOrEmpty(parentId) ? null : assetStorageService.find(parentId);
+
+ if (parentAsset == null && !isNullOrEmpty(parentId)) {
+ // Either invalid asset or user doesn't have access to it
+ LOG.info("User is trying to import with an invalid or inaccessible parent");
+ throw new BadRequestException("Parent either doesn't exist or is not accessible");
+ }
+
+ Tenant tenant = parentAsset != null ? identityService.getIdentityProvider().getTenantForRealmId(parentAsset.getRealmId()) : getAuthenticatedTenant();
+
+ if (!isTenantActiveAndAccessible(tenant)) {
+ String msg = "The requested parent asset is in an inaccessible realm: " + tenant.getRealm();
+ LOG.fine(msg);
+ throw new ForbiddenException(msg);
+ }
+
+ for (int i=0; i< assets.length; i++) {
+ Asset asset = assets[i];
+ asset.setId(null);
+ asset.setParent(parentAsset);
+ asset.setRealmId(tenant.getId());
+ ServerAsset serverAsset = ServerAsset.map(asset, new ServerAsset());
+ assets[i] = assetStorageService.merge(serverAsset);
+ }
+ }
}
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 bb9eb6957..b77a36ec3 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
@@ -28,10 +28,12 @@ import org.openremote.container.ContainerService;
import org.openremote.container.message.MessageBrokerService;
import org.openremote.container.message.MessageBrokerSetupService;
import org.openremote.container.persistence.PersistenceEvent;
+import org.openremote.container.timer.TimerService;
import org.openremote.container.web.WebService;
import org.openremote.manager.server.asset.AssetProcessingService;
import org.openremote.manager.server.asset.AssetStorageService;
import org.openremote.manager.server.asset.ServerAsset;
+import org.openremote.manager.server.security.ManagerIdentityService;
import org.openremote.model.AbstractValueTimestampHolder;
import org.openremote.model.asset.*;
import org.openremote.model.asset.agent.Agent;
@@ -92,7 +94,11 @@ public class AgentService extends RouteBuilder implements ContainerService, Cons
localAgentConnector = new LocalAgentConnector(this);
container.getService(WebService.class).getApiSingletons().add(
- new AgentResourceImpl(this)
+ new AgentResourceImpl(
+ container.getService(TimerService.class),
+ container.getService(ManagerIdentityService.class),
+ assetStorageService,
+ this)
);
}
diff --git a/manager/server/src/main/java/org/openremote/manager/server/agent/LocalAgentConnector.java b/manager/server/src/main/java/org/openremote/manager/server/agent/LocalAgentConnector.java
index 57283fd00..a1b3cd95a 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/agent/LocalAgentConnector.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/agent/LocalAgentConnector.java
@@ -20,17 +20,26 @@
package org.openremote.manager.server.agent;
import org.openremote.agent.protocol.Protocol;
+import org.openremote.agent.protocol.ProtocolLinkedAttributeDiscovery;
+import org.openremote.agent.protocol.ProtocolLinkedAttributeImport;
+import org.openremote.manager.server.asset.AssetResourceImpl;
import org.openremote.model.ValidationFailure;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.agent.ProtocolConfiguration;
import org.openremote.model.asset.agent.ProtocolDescriptor;
+import org.openremote.model.attribute.AttributeRef;
import org.openremote.model.attribute.AttributeValidationResult;
+import org.openremote.model.file.FileInfo;
+import org.openremote.model.util.Pair;
import java.util.Collections;
+import java.util.Optional;
+import java.util.logging.Logger;
public class LocalAgentConnector implements AgentConnector {
+ private static final Logger LOG = Logger.getLogger(LocalAgentConnector.class.getName());
protected AgentService agentService;
public LocalAgentConnector(AgentService agentService) {
@@ -61,4 +70,62 @@ public class LocalAgentConnector implements AgentConnector {
)),
null));
}
+
+ @Override
+ public Asset[] getDiscoveredLinkedAttributes(AttributeRef protocolConfigurationRef) throws IllegalArgumentException, UnsupportedOperationException {
+ Optional<Pair<Protocol, AssetAttribute>> protocolAndConfigOptional = getProtocolAndConfig(protocolConfigurationRef);
+
+ if (!protocolAndConfigOptional.isPresent()) {
+ throw new IllegalArgumentException("Protocol not found for: " + protocolConfigurationRef);
+ }
+
+ // Check protocol is of correct type
+ if (!(protocolAndConfigOptional.get().key instanceof ProtocolLinkedAttributeDiscovery)) {
+ LOG.info("Protocol not of type '" + ProtocolLinkedAttributeDiscovery.class.getSimpleName() + "'");
+ throw new UnsupportedOperationException("Protocol doesn't support linked attribute discovery:" + protocolAndConfigOptional.get().key.getProtocolDisplayName());
+ }
+
+ return protocolAndConfigOptional
+ .map(protocolAndConfig -> {
+ ProtocolLinkedAttributeDiscovery discoveryProtocol = (ProtocolLinkedAttributeDiscovery)protocolAndConfig.key;
+ return discoveryProtocol.discoverLinkedAssetAttributes(protocolAndConfig.value);
+ })
+ .orElse(new Asset[0]);
+ }
+
+ @Override
+ public Asset[] getDiscoveredLinkedAttributes(AttributeRef protocolConfigurationRef, FileInfo fileInfo) throws IllegalArgumentException, UnsupportedOperationException, IllegalStateException {
+ Optional<Pair<Protocol, AssetAttribute>> protocolAndConfigOptional = getProtocolAndConfig(protocolConfigurationRef);
+
+ if (!protocolAndConfigOptional.isPresent()) {
+ throw new IllegalArgumentException("Protocol not found for: " + protocolConfigurationRef);
+ }
+
+ Pair<Protocol, AssetAttribute> protocolAndConfig = protocolAndConfigOptional.get();
+
+ // Check protocol is of correct type
+ if (!(protocolAndConfigOptional.get().key instanceof ProtocolLinkedAttributeImport)) {
+ LOG.info("Protocol not of type '" + ProtocolLinkedAttributeImport.class.getSimpleName() + "'");
+ throw new UnsupportedOperationException("Protocol doesn't support linked attribute import:" + protocolAndConfigOptional.get().key.getProtocolDisplayName());
+ }
+
+ ProtocolLinkedAttributeImport discoveryProtocol = (ProtocolLinkedAttributeImport)protocolAndConfig.key;
+ return discoveryProtocol.discoverLinkedAssetAttributes(protocolAndConfig.value, fileInfo);
+ }
+
+ protected Optional<Pair<Protocol,AssetAttribute>> getProtocolAndConfig(AttributeRef protocolConfigurationRef) {
+ return agentService.getProtocolConfiguration(protocolConfigurationRef)
+ .map(protocolConfig ->
+ // Find protocol
+ agentService.protocols
+ .values()
+ .stream()
+ .filter(protocol -> protocol.getProtocolName().equals(protocolConfig.getValueAsString().orElse(null)))
+ .findFirst()
+ .map(protocol -> new Pair<>(protocol, protocolConfig))
+ .orElseGet(() -> {
+ LOG.info("Failed to find protocol configuration and protocol for: " + protocolConfigurationRef);
+ return null;
+ }));
+ }
}
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/agent/AgentResource.java b/manager/shared/src/main/java/org/openremote/manager/shared/agent/AgentResource.java
index aeccaa9fd..541d2e6c2 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/agent/AgentResource.java
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/agent/AgentResource.java
@@ -22,13 +22,14 @@ package org.openremote.manager.shared.agent;
import jsinterop.annotations.JsType;
import org.openremote.manager.shared.http.RequestParams;
import org.openremote.manager.shared.http.SuccessStatusCode;
+import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.agent.ProtocolDescriptor;
import org.openremote.model.attribute.AttributeValidationResult;
+import org.openremote.model.file.FileInfo;
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.*;
-
import java.util.Map;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
@@ -96,4 +97,47 @@ public interface AgentResource {
@PathParam("agentId") String agentId,
AssetAttribute protocolConfiguration
);
+
+ /**
+ * Get discovered linked attributes for the specified {@link org.openremote.model.asset.agent.Agent}
+ * and {@link org.openremote.model.asset.agent.ProtocolConfiguration}.
+ * <p>
+ * Currently this request will automatically add the found {@link Asset}s to the DB as well as returning
+ * them in the response. The {@param parentId} is used to set where in the {@link Asset} tree the new assets are
+ * inserted.
+ */
+ @GET
+ @Path("discover/{agentId}/{protocolConfigurationName}")
+ @Consumes(APPLICATION_JSON)
+ @Produces(APPLICATION_JSON)
+ @SuccessStatusCode(200)
+ Asset[] getDiscoveredLinkedAttributes(
+ @BeanParam RequestParams requestParams,
+ @PathParam("agentId") String agentId,
+ @PathParam("protocolConfigurationName") String protocolConfigurationName,
+ @QueryParam("parentId") String parentId
+ );
+
+ /**
+ * Get discovered linked attributes for the specified {@link org.openremote.model.asset.agent.Agent}
+ * and {@link org.openremote.model.asset.agent.ProtocolConfiguration} using the supplied {@link FileInfo}.
+ * <p>
+ * Currently this request will automatically add the found {@link Asset}s to the DB as well as returning
+ * them in the response. The {@param parentId} is used to set where in the {@link Asset} tree the new assets are
+ * inserted.
+ * <b>NOTE:</b> The {@link FileInfo} should be a file that the protocol understands.
+ */
+ @POST
+ @Path("discover/{agentId}/{protocolConfigurationName}")
+ @Consumes(APPLICATION_JSON)
+ @Produces(APPLICATION_JSON)
+ @SuccessStatusCode(200)
+ // TODO: File upload should use standard multipart mechanism
+ Asset[] getDiscoveredLinkedAttributes(
+ @BeanParam RequestParams requestParams,
+ @PathParam("agentId") String agentId,
+ @PathParam("protocolConfigurationName") String protocolConfigurationName,
+ @QueryParam("parentId") String parentId,
+ FileInfo fileInfo
+ );
}
diff --git a/model/src/main/java/org/openremote/model/attribute/LinkedAttributeDescriptor.java b/model/src/main/java/org/openremote/model/attribute/LinkedAttributeDescriptor.java
new file mode 100644
index 000000000..e4b953416
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/attribute/LinkedAttributeDescriptor.java
@@ -0,0 +1,69 @@
+/*
+ * 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.attribute;
+
+public class LinkedAttributeDescriptor {
+
+ protected final String name;
+ protected final String displayName;
+ protected final AttributeType attributeType;
+ protected final boolean readOnly;
+ protected final boolean executable;
+ protected final MetaItem[] metaItems;
+
+ public LinkedAttributeDescriptor(
+ String name,
+ String displayName,
+ AttributeType attributeType,
+ boolean readOnly,
+ boolean executable,
+ MetaItem[] metaItems) {
+ this.name = name;
+ this.displayName = displayName;
+ this.attributeType = attributeType;
+ this.readOnly = readOnly;
+ this.executable = executable;
+ this.metaItems = metaItems;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getDisplayName() {
+ return displayName;
+ }
+
+ public AttributeType getAttributeType() {
+ return attributeType;
+ }
+
+ public boolean isReadOnly() {
+ return readOnly;
+ }
+
+ public boolean isExecutable() {
+ return executable;
+ }
+
+ public MetaItem[] getMetaItems() {
+ return metaItems;
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/file/FileInfo.java b/model/src/main/java/org/openremote/model/file/FileInfo.java
index 384a06450..3e0e427d4 100644
--- a/model/src/main/java/org/openremote/model/file/FileInfo.java
+++ b/model/src/main/java/org/openremote/model/file/FileInfo.java
@@ -19,20 +19,42 @@
*/
package org.openremote.model.file;
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.gwt.user.server.Base64Utils;
+
+import java.io.UnsupportedEncodingException;
+
public class FileInfo {
- protected String name;
- protected byte[] contents;
+ protected final String name;
+ protected final String contents;
+ protected final boolean binary;
- public FileInfo(String name, byte[] contents) {
+ @JsonCreator
+ public FileInfo(@JsonProperty("name") String name, @JsonProperty("contents") String contents, @JsonProperty("binary") boolean binary) {
this.name = name;
this.contents = contents;
+ this.binary = binary;
}
public String getName() {
return name;
}
- public byte[] getContents() {
+ public byte[] getBytes() {
+ try {
+ return binary ? Base64Utils.fromBase64(contents) : contents.getBytes("UTF8");
+ } catch (UnsupportedEncodingException e) {
+ e.printStackTrace();
+ return new byte[0];
+ }
+ }
+
+ public String getContents() {
return contents;
}
+
+ public boolean isBinary() {
+ return binary;
+ }
}