100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Demo agent with real protocols can be created on startup with env variables
Christian Bauera8849025bd9e
diff --git a/README.md b/README.mdindex 275452dc5..29dc65f7b 100644--- a/README.md+++ b/README.md@@ -46,7 +46,7 @@ An instance of the application in developer mode can be started with:```./gradlew clean prepareImage-docker-compose -p ordev \+docker-compose -p openremote \-f profile/postgresql_dev.yml \-f profile/keycloak_dev.yml \-f profile/manager_dev.yml \@@ -57,7 +57,7 @@ docker-compose -p ordev \Stop the containers with:```-docker-compose -p ordev \+docker-compose -p openremote \-f profile/postgresql_dev.yml \-f profile/keycloak_dev.yml \-f profile/manager_dev.yml \diff --git a/agent/src/main/java/org/openremote/agent/protocol/upnp/UpnpProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/upnp/UpnpProtocol.javaindex bcda9a470..fc7b61ed7 100644--- a/agent/src/main/java/org/openremote/agent/protocol/upnp/UpnpProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/upnp/UpnpProtocol.java@@ -9,15 +9,19 @@ import org.fourthline.cling.registry.DefaultRegistryListener;import org.fourthline.cling.registry.Registry;import org.fourthline.cling.registry.RegistryListener;import org.openremote.agent.protocol.AbstractProtocol;+import org.openremote.model.AbstractValueHolder;import org.openremote.model.asset.Asset;import org.openremote.model.asset.AssetAttribute;import org.openremote.model.asset.AssetType;+import org.openremote.model.asset.agent.ConnectionStatus;import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.attribute.AttributeRef;import org.openremote.model.attribute.MetaItem;import org.openremote.model.value.Values;import java.util.Collection;+import java.util.HashMap;+import java.util.Map;import java.util.Optional;import java.util.logging.Logger;@@ -34,17 +38,24 @@ public class UpnpProtocol extends AbstractProtocol {public static final String PROTOCOL_NAME = PROTOCOL_NAMESPACE + ":upnp";public static final String PROTOCOL_DISPLAY_NAME = "UPnP";+ /**+ * The asset under which all discovered devices are merged.+ */+ public static final String GROUP_ASSET_ID = PROTOCOL_NAME + ":groupAssetId";+protected static final String VERSION = "1.0";+ protected Map<AttributeRef, String> protocolConfigsGroupAssetId = new HashMap<>();+static protected UpnpService INSTANCE = null;final protected RegistryListener registryListener = new DefaultRegistryListener() {@Overridepublic void deviceAdded(Registry registry, Device device) {LOG.fine("UPnP device discovered: " + device.getDisplayString());- for (AttributeRef attributeRef : linkedProtocolConfigurations.keySet()) {- storeAssets(attributeRef.getEntityId(), device);- }+ linkedProtocolConfigurations.values().forEach(linkedProtocolInfo -> {+ storeAssets(linkedProtocolInfo.getProtocolConfiguration(), device);+ });}@Override@@ -74,12 +85,26 @@ public class UpnpProtocol extends AbstractProtocol {@Overrideprotected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {+ boolean isEnabled = protocolConfiguration.isEnabled();+ if (!isEnabled) {+ updateStatus(protocolConfiguration.getReferenceOrThrow(), ConnectionStatus.DISABLED);+ return;+ }+protocolConfiguration.getAssetId().ifPresent(agentId -> {++ // The parent is either the configured asset to group all discovered devices or the agent+ protocolConfigsGroupAssetId.put(+ protocolConfiguration.getReferenceOrThrow(),+ protocolConfiguration.getMetaItem(GROUP_ASSET_ID).flatMap(AbstractValueHolder::getValueAsString).orElse(agentId)+ );+Collection<Device> devices = getService().getRegistry().getDevices();+ updateStatus(protocolConfiguration.getReferenceOrThrow(), ConnectionStatus.CONNECTED);+if (devices.size() > 0) {- LOG.fine("Storing UPnP devices as child assets of agent: " + agentId);for (Device device : getService().getRegistry().getDevices()) {- storeAssets(agentId, device);+ storeAssets(protocolConfiguration, device);}}});@@ -87,6 +112,10 @@ public class UpnpProtocol extends AbstractProtocol {@Overrideprotected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {+ protocolConfigsGroupAssetId.remove(protocolConfiguration.getReferenceOrThrow());++ updateStatus(protocolConfiguration.getReferenceOrThrow(), ConnectionStatus.DISABLED);+if (linkedProtocolConfigurations.size() == 1) {closeService();}@@ -123,8 +152,10 @@ public class UpnpProtocol extends AbstractProtocol {}}- protected void storeAssets(String agentId, Device device) {- Asset asset = createAsset(agentId, device);+ protected void storeAssets(AssetAttribute protocolConfiguration, Device device) {+ String parentId = protocolConfigsGroupAssetId.get(protocolConfiguration.getReferenceOrThrow());+ LOG.fine("Storing UPnP device as child assets of " + parentId + ": " + device);+ Asset asset = createAsset(parentId, device);asset = assetService.mergeAsset(asset);}diff --git a/manager/server/src/main/java/org/openremote/manager/server/setup/builtin/ManagerDemoAgentSetup.java b/manager/server/src/main/java/org/openremote/manager/server/setup/builtin/ManagerDemoAgentSetup.javaindex dc2484cff..de69bd223 100644--- a/manager/server/src/main/java/org/openremote/manager/server/setup/builtin/ManagerDemoAgentSetup.java+++ b/manager/server/src/main/java/org/openremote/manager/server/setup/builtin/ManagerDemoAgentSetup.java@@ -20,6 +20,7 @@package org.openremote.manager.server.setup.builtin;import org.openremote.agent.protocol.knx.KNXProtocol;+import org.openremote.agent.protocol.upnp.UpnpProtocol;import org.openremote.agent.protocol.velbus.VelbusSerialProtocol;import org.openremote.container.Container;import org.openremote.manager.server.asset.ServerAsset;@@ -29,16 +30,50 @@ import org.openremote.model.asset.AssetAttribute;import org.openremote.model.attribute.MetaItem;import org.openremote.model.value.Values;+import java.util.logging.Logger;++import static org.openremote.container.util.MapAccess.getBoolean;+import static org.openremote.container.util.MapAccess.getString;import static org.openremote.model.asset.AssetType.AGENT;import static org.openremote.model.asset.AssetType.THING;import static org.openremote.model.asset.agent.ProtocolConfiguration.initProtocolConfiguration;public class ManagerDemoAgentSetup extends AbstractManagerSetup {+ private static final Logger LOG = Logger.getLogger(ManagerDemoAgentSetup.class.getName());++ public static final String SETUP_IMPORT_DEMO_AGENT_KNX = "SETUP_IMPORT_DEMO_AGENT_KNX";+ public static final String SETUP_IMPORT_DEMO_AGENT_KNX_GATEWAY_IP = "SETUP_IMPORT_DEMO_AGENT_KNX_GATEWAY_IP";+ public static final String SETUP_IMPORT_DEMO_AGENT_KNX_LOCAL_IP = "SETUP_IMPORT_DEMO_AGENT_KNX_LOCAL_IP";++ public static final String SETUP_IMPORT_DEMO_AGENT_VELBUS = "SETUP_IMPORT_DEMO_AGENT_VELBUS";+ public static final String SETUP_IMPORT_DEMO_AGENT_VELBUS_COM_PORT = "SETUP_IMPORT_DEMO_AGENT_VELBUS_COM_PORT";++ public static final String SETUP_IMPORT_DEMO_AGENT_UPNP = "SETUP_IMPORT_DEMO_AGENT_UPNP";+public String masterRealmId;++ final protected boolean knx;+ final protected String knxGatewayIp;+ final protected String knxLocalIp;++ final protected boolean velbus;+ final protected String velbusComPort;++ final protected boolean upnp;+public ManagerDemoAgentSetup(Container container) {super(container);++ this.knx = getBoolean(container.getConfig(), SETUP_IMPORT_DEMO_AGENT_KNX, false);+ this.knxGatewayIp = getString(container.getConfig(), SETUP_IMPORT_DEMO_AGENT_KNX_GATEWAY_IP, "192.168.0.64");+ this.knxLocalIp = getString(container.getConfig(), SETUP_IMPORT_DEMO_AGENT_KNX_LOCAL_IP, "192.168.0.65");++ this.velbus = getBoolean(container.getConfig(), SETUP_IMPORT_DEMO_AGENT_VELBUS, false);+ this.velbusComPort = getString(container.getConfig(), SETUP_IMPORT_DEMO_AGENT_VELBUS_COM_PORT, "COM3");++ this.upnp = getBoolean(container.getConfig(), SETUP_IMPORT_DEMO_AGENT_UPNP, false);}@Override@@ -48,30 +83,48 @@ public class ManagerDemoAgentSetup extends AbstractManagerSetup {Tenant masterTenant = keycloakDemoSetup.masterTenant;masterRealmId = masterTenant.getId();- // ########### Assets in 'master' realm ####################- ServerAsset agent = new ServerAsset("Agent", AGENT);+ ServerAsset agent = new ServerAsset("Demo Agent", AGENT);agent.setRealmId(masterRealmId);- agent.setAttributes(- initProtocolConfiguration(new AssetAttribute("knxConfig"), KNXProtocol.PROTOCOL_NAME)- .addMeta(- new MetaItem(KNXProtocol.META_KNX_GATEWAY_IP, Values.create("192.168.0.64")),- new MetaItem(KNXProtocol.META_KNX_LOCAL_IP, Values.create("192.168.0.65"))- ),- initProtocolConfiguration(new AssetAttribute("velbusConfig"), VelbusSerialProtocol.PROTOCOL_NAME)- .addMeta(- new MetaItem(VelbusSerialProtocol.META_VELBUS_SERIAL_PORT, Values.create("COM3"))- )- );agent = assetStorageService.merge(agent);- ServerAsset velbusDevices = new ServerAsset("VELBUS Devices", THING, agent);- velbusDevices.setRealmId(masterRealmId);- velbusDevices = assetStorageService.merge(velbusDevices);+ if (knx) {+ LOG.info("Enable KNX demo protocol configuration, gateway/local IP: " + knxGatewayIp + "/" + knxLocalIp);+ agent.addAttributes(+ initProtocolConfiguration(new AssetAttribute("knxConfig"), KNXProtocol.PROTOCOL_NAME)+ .addMeta(+ new MetaItem(KNXProtocol.META_KNX_GATEWAY_IP, Values.create(knxGatewayIp)),+ new MetaItem(KNXProtocol.META_KNX_LOCAL_IP, Values.create(knxLocalIp))+ )+ );+ ServerAsset knxDevices = new ServerAsset("KNX Devices", THING, agent, masterRealmId);+ knxDevices = assetStorageService.merge(knxDevices);+ }- ServerAsset knxDevices = new ServerAsset("KNX Devices", THING, agent);- knxDevices.setRealmId(masterRealmId);- knxDevices = assetStorageService.merge(knxDevices);+ if (velbus) {+ LOG.info("Enable Velbus demo protocol configuration, COM port: " + velbusComPort);+ agent.addAttributes(+ initProtocolConfiguration(new AssetAttribute("velbusConfig"), VelbusSerialProtocol.PROTOCOL_NAME)+ .addMeta(+ new MetaItem(VelbusSerialProtocol.META_VELBUS_SERIAL_PORT, Values.create(velbusComPort))+ )+ );+ ServerAsset velbusDevices = new ServerAsset("VELBUS Devices", THING, agent, masterRealmId);+ velbusDevices = assetStorageService.merge(velbusDevices);+ }+ if (upnp) {+ LOG.info("Enable UPnP demo protocol configuration");+ ServerAsset upnpDevices = new ServerAsset("UPnP Devices", THING, agent, masterRealmId);+ upnpDevices = assetStorageService.merge(upnpDevices);+ agent.addAttributes(+ initProtocolConfiguration(new AssetAttribute("upnpConfig"), UpnpProtocol.PROTOCOL_NAME)+ .addMeta(+ // TODO Protocols should create these grouping assets automatically and import assets underneath for each protocol configuration+ new MetaItem(UpnpProtocol.GROUP_ASSET_ID, Values.create(upnpDevices.getId()))+ )+ );+ }+ agent = assetStorageService.merge(agent);}}\ No newline at end of filediff --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.javaindex 6560c2ea2..377907e03 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@@ -415,14 +415,6 @@ public class ManagerDemoSetup extends AbstractManagerSetup {apartment2 = assetStorageService.merge(apartment2);apartment2Id = apartment2.getId();- /* TODO Experimental- ServerAsset apartment2UpnpAgent= new ServerAsset("UPnP Agent", AGENT, apartment2);- apartment2UpnpAgent.setAttributes(- initProtocolConfiguration(new AssetAttribute("upnpConfig1"), UpnpProtocol.PROTOCOL_NAME)- );- apartment2UpnpAgent = assetStorageService.merge(apartment2UpnpAgent);- */-ServerAsset apartment2Livingroom = new ServerAsset("Living Room", ROOM, apartment2);apartment2Livingroom.setLocation(apartment2.getLocation());apartment2Livingroom.setAttributes(diff --git a/profile/demo.yml b/profile/demo.ymlindex a217d47b4..1934329ea 100644--- a/profile/demo.yml+++ b/profile/demo.yml@@ -60,6 +60,21 @@ services:SETUP_IMPORT_DEMO_ASSETS: 'true'SETUP_IMPORT_DEMO_RULES: 'true'+ # Create a demo agent with example configurations for all built-in protocols:+ #+ # - KNX IP gateway+ # - Velbus IP gateway or serial bridge+ # - UPnP (asset discovery only)+ #+ SETUP_IMPORT_DEMO_AGENT: 'true'+ SETUP_IMPORT_DEMO_AGENT_KNX: 'false'+ #SETUP_IMPORT_DEMO_AGENT_KNX_GATEWAY_IP: '10.0.0.11'+ #SETUP_IMPORT_DEMO_AGENT_KNX_LOCAL_IP: '10.0.0.12'+ SETUP_IMPORT_DEMO_AGENT_VELBUS: 'false'+ #SETUP_IMPORT_DEMO_AGENT_VELBUS_COM_PORT: 'COM3'+ SETUP_IMPORT_DEMO_AGENT_UPNP: 'false'+ # TODO: Additional docker settings are necessary to use com ports and multicast from within containers+# Optional: Configure SMTP smart host for Keycloak, to send password reset emails etc.#SETUP_KEYCLOAK_EMAIL_HOST: smtp-host.demo.tld#SETUP_KEYCLOAK_EMAIL_USER: 'smtp-user'