Skip to content

armgit / core

Public Imported from GitHub
0

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

Updated device object model to use EAV model

Rich Turner6f8d1d291385
diff --git a/connector/controller2/src/main/java/org/openremote/agent/controller2/Controller2Adapter.java b/connector/controller2/src/main/java/org/openremote/agent/controller2/Controller2Adapter.java
index f6a505196..8f0ef2a29 100644
--- a/connector/controller2/src/main/java/org/openremote/agent/controller2/Controller2Adapter.java
+++ b/connector/controller2/src/main/java/org/openremote/agent/controller2/Controller2Adapter.java
@@ -25,9 +25,9 @@ import org.openremote.console.controller.ControllerConnectionStatus;
import org.openremote.console.controller.DeviceRegistrationHandle;
import org.openremote.console.controller.auth.UserPasswordCredentials;
import org.openremote.entities.controller.*;
+import org.openremote.manager.shared.attribute.AttributeType;
import org.openremote.manager.shared.device.Device;
import org.openremote.manager.shared.device.DeviceResource;
-import org.openremote.manager.shared.device.Resource;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
@@ -525,8 +525,7 @@ public class Controller2Adapter {
.findFirst()
.orElseGet(() -> null);
- DeviceResource dr = new DeviceResource();
- dr.setName(sensor.getName());
+ DeviceResource dr = new DeviceResource(sensor.getName());
dr.setUri(sensor.getName().toLowerCase());
DeviceResourceMapping resourceMapping = new DeviceResourceMapping();
resourceMapping.resource = dr;
@@ -553,17 +552,17 @@ public class Controller2Adapter {
switch(sensor.getType()) {
case SWITCH:
- dr.setType(Resource.Type.BOOLEAN);
- dr.setAccess(matchingWidgetInfo != null ? Resource.Access.RW : Resource.Access.R);
+ dr.setType(AttributeType.BOOLEAN);
+ dr.setAccess(matchingWidgetInfo != null ? DeviceResource.Access.RW : DeviceResource.Access.R);
break;
case RANGE:
case LEVEL:
- dr.setType(Resource.Type.INTEGER);
- dr.setAccess(matchingWidgetInfo != null ? Resource.Access.RW : Resource.Access.R);
+ dr.setType(AttributeType.INTEGER);
+ dr.setAccess(matchingWidgetInfo != null ? DeviceResource.Access.RW : DeviceResource.Access.R);
break;
default:
- dr.setType(Resource.Type.STRING);
- dr.setAccess(Resource.Access.R);
+ dr.setType(AttributeType.STRING);
+ dr.setAccess(DeviceResource.Access.R);
break;
}
}
@@ -573,11 +572,10 @@ public class Controller2Adapter {
Stream<Command> unassignedCommands = gatewayDevice.getCommands().stream().filter(command -> !assignedCommands.contains(command.getId()));
unassignedCommands.forEach(command -> {
- DeviceResource dr = new DeviceResource();
- dr.setName(command.getName());
+ DeviceResource dr = new DeviceResource(command.getName());
dr.setUri(command.getName().toLowerCase());
dr.setType(null);
- dr.setAccess(Resource.Access.W);
+ dr.setAccess(DeviceResource.Access.W);
DeviceResourceMapping resourceMapping = new DeviceResourceMapping();
resourceMapping.resource = dr;
@@ -586,7 +584,7 @@ public class Controller2Adapter {
deviceMapping.resourceMap.put(dr.getUri(), resourceMapping);
});
- device.setResources(resources);
+ device.setResources(resources.toArray(new DeviceResource[resources.size()]));
}
}
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/asset/AssetType.java b/manager/shared/src/main/java/org/openremote/manager/shared/asset/AssetType.java
index fc4313c31..18e51f7c1 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/asset/AssetType.java
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/asset/AssetType.java
@@ -8,7 +8,9 @@ public enum AssetType {
GENERIC("urn:openremote:asset:generic"),
BUILDING("urn:openremote:asset:building"),
- ROOM("urn:openremote:asset:room");
+ ROOM("urn:openremote:asset:room"),
+ AGENT("urn:openremote:asset:agent"),
+ DEVICE("urn:openremote:asset:device");
final protected String value;
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/device/Capability.java b/manager/shared/src/main/java/org/openremote/manager/shared/device/Capability.java
index 454a9f163..8748807c0 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/device/Capability.java
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/device/Capability.java
@@ -1,5 +1,8 @@
package org.openremote.manager.shared.device;
+import org.openremote.manager.shared.attribute.Attribute;
+import org.openremote.manager.shared.attribute.Attributes;
+
import java.util.Map;
/**
@@ -15,13 +18,18 @@ import java.util.Map;
* 3311 = Light Control
*/
public class Capability implements CapabilityDefinition {
- protected int id;
+ protected String type;
protected String name;
protected String description;
- protected Map<Integer, Resource> resources;
+ protected Attributes resources;
+
+ @Override
+ public String getType() {
+ return type;
+ }
- public int getId() {
- return id;
+ public void setType(String type) {
+ this.type = type;
}
public String getName() {
@@ -32,14 +40,10 @@ public class Capability implements CapabilityDefinition {
return description;
}
- public Map<Integer, Resource> getResources() {
+ public Attributes getResources() {
return resources;
}
- public void setId(int id) {
- this.id = id;
- }
-
public void setName(String name) {
this.name = name;
}
@@ -48,19 +52,7 @@ public class Capability implements CapabilityDefinition {
this.description = description;
}
- public void setResources(Map<Integer, Resource> resources) {
+ public void setResources(Attributes resources) {
this.resources = resources;
}
-
- public void addResource(int id, Resource resource) {
- resources.put(id, resource);
- }
-
- public void removeResource(int id) {
- resources.remove(id);
- }
-
- public void removeResource(Resource resource) {
- resources.entrySet().stream().filter(entry -> entry.getValue().equals(resource)).map(Map.Entry::getKey).forEach(resources::remove);
- }
}
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/device/CapabilityDefinition.java b/manager/shared/src/main/java/org/openremote/manager/shared/device/CapabilityDefinition.java
index 4f3f04a26..504a6d5c7 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/device/CapabilityDefinition.java
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/device/CapabilityDefinition.java
@@ -1,13 +1,11 @@
package org.openremote.manager.shared.device;
-import java.util.Map;
+import org.openremote.manager.shared.attribute.Attributes;
public interface CapabilityDefinition {
- int getId();
-
- String getName();
+ String getType();
String getDescription();
- Map<Integer, Resource> getResources();
+ Attributes getResources();
}
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 5376f3d64..ae097b82f 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
@@ -1,45 +1,38 @@
package org.openremote.manager.shared.device;
-import java.util.Map;
+import elemental.json.JsonObject;
+import org.openremote.manager.shared.attribute.Attribute;
+import org.openremote.manager.shared.attribute.AttributeType;
+import org.openremote.manager.shared.attribute.Attributes;
-public class CapabilityRef {
- protected int typeId;
- protected String name;
- protected Map<Integer, ResourceRef> resources;
+public class CapabilityRef extends Attribute {
+ protected Attributes resources;
- public int getTypeId() {
- return typeId;
+ public CapabilityRef(JsonObject jsonObject) {
+ super(jsonObject.get("name"), jsonObject);
}
- public void setTypeId(int typeId) {
- this.typeId = typeId;
+ public CapabilityRef(String name, String capabilityType, Attributes resourceRefs) {
+ super(name, AttributeType.OBJECT);
+ jsonObject.put("name", name);
+ setCapabilityType(capabilityType);
+ setResourceRefs(resourceRefs);
}
- public String getName() {
- return name;
+ public String getCapabilityType() {
+ return jsonObject.getString("capabilityType");
}
- public void setName(String name) {
- this.name = name;
+ public void setCapabilityType(String capabilityType) {
+ jsonObject.put("capabilityType", capabilityType);
}
- public Map<Integer, ResourceRef> getResources() {
- return resources;
+ public Attributes getResourceRefs() {
+ JsonObject refsObj = jsonObject.getObject("resourceRefs");
+ return refsObj == null ? null : new Attributes(refsObj);
}
- public void setResources(Map<Integer, ResourceRef> resources) {
- this.resources = resources;
- }
-
- public void addResource(int id, ResourceRef resource) {
- resources.put(id, resource);
- }
-
- public void removeResource(int id) {
- resources.remove(id);
- }
-
- public void removeResource(ResourceRef resource) {
- resources.entrySet().stream().filter(entry -> entry.getValue().equals(resource)).map(Map.Entry::getKey).forEach(resources::remove);
+ public void setResourceRefs(Attributes resourceRefs) {
+ jsonObject.put("resourceRefs", resourceRefs.getJsonObject());
}
}
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/device/Device.java b/manager/shared/src/main/java/org/openremote/manager/shared/device/Device.java
index 545b22e67..bb034f51b 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/device/Device.java
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/device/Device.java
@@ -1,9 +1,13 @@
package org.openremote.manager.shared.device;
-import java.util.List;
+import elemental.json.Json;
+import elemental.json.JsonArray;
+import elemental.json.JsonObject;
+import org.openremote.manager.shared.asset.Asset;
+import org.openremote.manager.shared.asset.AssetType;
/**
- * Device model for representing devices that come from IOT connectors.
+ * Device asset for representing devices that come from Agents.
* A Device requires a URI (unique within the scope of the connector) and
* one or more DeviceResource objects.
*
@@ -12,76 +16,106 @@ import java.util.List;
*
* The capabilities allow a device to expose defined 'capabilities' such as
* LightControl and these capabilities provide contextual information for UI
- * purposes and also NGSI entity mapping.
+ * and mapping purposes.
*
*/
-public class Device {
- protected String uri;
- protected String name;
- protected String description;
- protected String type;
- protected List<DeviceResource> resources;
- protected List<CapabilityRef> capabilities;
-
- public String getUri() {
- return uri;
+public class Device extends Asset {
+ protected static final String URI_KEY = "uri";
+ protected static final String RESOURCES_KEY = "resources";
+ protected static final String CAPABILITIES_KEY = "capabilities";
+
+ public Device() {
+ setType(AssetType.DEVICE);
}
- public void setUri(String uri) {
- this.uri = uri;
+ @Override
+ public void setType(String type) {
}
- public String getName() {
- return name;
- }
+ public String getUri() {
+ if (attributes == null) {
+ return null;
+ }
- public void setName(String name) {
- this.name = name;
+ return attributes.getString(URI_KEY);
}
- public String getDescription() {
- return description;
- }
+ public void setUri(String uri) {
+ if (attributes == null) {
+ attributes = Json.createObject();
+ }
- public void setDescription(String description) {
- this.description = description;
+ attributes.put(URI_KEY, uri);
}
- public String getType() {
- return type;
- }
+ public DeviceResource[] getResources()
+ {
+ if (attributes == null || !attributes.hasKey(RESOURCES_KEY)) {
+ return null;
+ }
- public void setType(String type) {
- this.type = type;
- }
+ JsonArray resourcesArr = attributes.getArray(RESOURCES_KEY);
+ DeviceResource[] resources = new DeviceResource[resourcesArr.length()];
+
+ for (int i = 0; i < resourcesArr.length(); i++) {
+ JsonObject resourceObj = resourcesArr.getObject(i);
+ resources[i] = new DeviceResource(resourceObj);
+ }
- public List<DeviceResource> getResources() {
return resources;
}
- public void setResources(List<DeviceResource> resources) {
- this.resources = resources;
+ public void setResources(DeviceResource[] resources){
+ if (attributes == null) {
+ attributes = Json.createObject();
+ }
+
+ if (resources == null || resources.length == 0) {
+ attributes.remove(RESOURCES_KEY);
+ return;
+ }
+
+ JsonArray resourcesArr = Json.createArray();
+
+ for (int i = 0; i < resources.length; i++) {
+ resourcesArr.set(i, Json.parse(resources[i].toString()));
+ }
+
+ attributes.put(RESOURCES_KEY, resourcesArr);
}
- public List<CapabilityRef> getCapabilities() {
+ public CapabilityRef[] getCapabilities() {
+ if (attributes == null || !attributes.hasKey(CAPABILITIES_KEY)) {
+ return null;
+ }
+
+ JsonArray capabilitiesArr = attributes.getArray(CAPABILITIES_KEY);
+ CapabilityRef[] capabilities = new CapabilityRef[capabilitiesArr.length()];
+
+ for (int i = 0; i < capabilitiesArr.length(); i++) {
+ JsonObject capabilityObj = capabilitiesArr.getObject(i);
+ capabilities[i] = new CapabilityRef(capabilityObj);
+ }
+
return capabilities;
}
- public void setCapabilities(List<CapabilityRef> capabilities) {
- this.capabilities = capabilities;
- }
+ public void setCapabilities(CapabilityRef[] capabilities){
+ if (attributes == null) {
+ attributes = Json.createObject();
+ }
- public void addCapability(CapabilityRef capability) {
- capabilities.add(capability);
- }
+ if (capabilities == null || capabilities.length == 0) {
+ attributes.remove(CAPABILITIES_KEY);
+ return;
+ }
- public void removeCapability(CapabilityRef capabilityRef) {
- capabilities.remove(capabilityRef);
- }
+ JsonArray capabilitiesArr = Json.createArray();
- public void removeCapability(int index) {
- if(index < capabilities.size()) {
- capabilities.remove(index);
+ for (int i = 0; i < capabilities.length; i++) {
+ capabilitiesArr.set(i, Json.parse(capabilities[i].toString()));
}
+
+ attributes.put(CAPABILITIES_KEY, capabilitiesArr);
}
}
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 2171e58f7..c62284931 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
@@ -1,38 +1,85 @@
package org.openremote.manager.shared.device;
-public class DeviceResource extends Resource {
- protected String uri;
- protected boolean passive;
- protected boolean constant;
+import elemental.json.JsonObject;
+import org.openremote.manager.shared.attribute.Attribute;
+import org.openremote.manager.shared.attribute.AttributeType;
- public DeviceResource() {
+public class DeviceResource extends Attribute {
+
+ public enum Access {
+ R,
+ W,
+ RW
+ }
+
+ public DeviceResource(String name) {
+ super(name);
+ }
+
+ public DeviceResource(JsonObject jsonObject) {
+ super(jsonObject.get("name"), jsonObject);
+ }
+
+ public DeviceResource(String name, String description, AttributeType type, Access access, String units) {
+ super(name, type);
+ jsonObject.put("name", name);
+ setAccess(access);
+ setUnits(units);
+ setDescription(description);
+ }
+
+ public String getDescription() {
+ return jsonObject.getString("description");
+ }
+
+ public DeviceResource setDescription(String description) {
+ jsonObject.put("description", description);
+ return this;
}
- public DeviceResource(String name, String description, Type type, Access access, boolean collection, String units) {
- super(name, description, type, access, collection, units);
+ public Access getAccess() {
+ String accessStr = jsonObject.get("access");
+ return accessStr != null ? Access.valueOf(accessStr) : null;
+ }
+
+ public DeviceResource setAccess(Access access) {
+ jsonObject.put("access", access.toString());
+ return this;
+ }
+
+ public String getUnits() {
+ return jsonObject.get("units");
+ }
+
+ public DeviceResource setUnits(String units) {
+ jsonObject.put("units", units);
+ return this;
}
public String getUri() {
- return uri;
+ return jsonObject.get("uri");
}
- public boolean isPassive() {
- return passive;
+ public DeviceResource setUri(String uri) {
+ jsonObject.put("uri", uri);
+ return this;
}
- public boolean isConstant() {
- return constant;
+ public boolean isPassive() {
+ return jsonObject.getBoolean("passive");
}
- public void setUri(String uri) {
- this.uri = uri;
+ public DeviceResource setPassive(boolean passive) {
+ jsonObject.put("passive", passive);
+ return this;
}
- public void setPassive(boolean passive) {
- this.passive = passive;
+ public boolean isConstant() {
+ return jsonObject.getBoolean("constant");
}
- public void setConstant(boolean constant) {
- this.constant = constant;
+ public DeviceResource setConstant(boolean constant) {
+ jsonObject.put("constant", constant);
+ return this;
}
}
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/device/Resource.java b/manager/shared/src/main/java/org/openremote/manager/shared/device/Resource.java
deleted file mode 100644
index db242716f..000000000
--- a/manager/shared/src/main/java/org/openremote/manager/shared/device/Resource.java
+++ /dev/null
@@ -1,85 +0,0 @@
-package org.openremote.manager.shared.device;
-
-public class Resource {
- public enum Access {
- R,
- W,
- RW
- }
-
- public enum Type {
- NONE,
- BOOLEAN,
- STRING,
- INTEGER,
- DOUBLE,
- DATETIME
- }
-
- protected String name;
- protected String description;
- protected Access access;
- protected Type type;
- protected String units;
- protected boolean isCollection;
-
- public Resource() {
- }
-
- public Resource(String name, String description, Type type, Access access, boolean isCollection, String units) {
- this.type = type;
- this.name = name;
- this.access = access;
- this.units = units;
- this.description = description;
- this.isCollection = isCollection;
- }
-
- public String getName() {
- return name;
- }
-
- public String getDescription() {
- return description;
- }
-
- public Type getType() {
- return type;
- }
-
- public Access getAccess() {
- return access;
- }
-
- public String getUnits() {
- return units;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public void setDescription(String description) {
- this.description = description;
- }
-
- public void setAccess(Access access) {
- this.access = access;
- }
-
- public void setType(Type type) {
- this.type = type;
- }
-
- public void setUnits(String units) {
- this.units = units;
- }
-
- public boolean isCollection() {
- return isCollection;
- }
-
- public void setCollection(boolean collection) {
- this.isCollection = collection;
- }
-}
diff --git a/test/src/test/groovy/org/openremote/test/controller2/Controller2Service.java b/test/src/test/groovy/org/openremote/test/controller2/Controller2Service.java
index 1584c48c9..3c116edb5 100644
--- a/test/src/test/groovy/org/openremote/test/controller2/Controller2Service.java
+++ b/test/src/test/groovy/org/openremote/test/controller2/Controller2Service.java
@@ -35,9 +35,6 @@ public class Controller2Service implements ContainerService {
public void configure(Container container) throws Exception {
MessageBrokerService messageBrokerService = container.getService(MessageBrokerService.class);
MessageBrokerContext context = messageBrokerService.getContext();
-
- configure(context);
-
messageProducerTemplate = context.createProducerTemplate();
context.addRoutes(new RouteBuilder() {
@@ -90,10 +87,6 @@ public class Controller2Service implements ContainerService {
});
}
- protected void configure(MessageBrokerContext context) {
- context.addComponent("mockController2", new Controller2Component());
- }
-
@Override
public void start(Container container) {
}