Skip to content

armgit / core

Public Imported from GitHub
0

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

Make connector2 host and port configurable

Christian Bauer74a6400aa425
diff --git a/connector/controller2/src/main/java/org/openremote/agent/controller2/Controller2Component.java b/connector/controller2/src/main/java/org/openremote/agent/controller2/Controller2Component.java
index b31b397ab..fdab685cb 100644
--- a/connector/controller2/src/main/java/org/openremote/agent/controller2/Controller2Component.java
+++ b/connector/controller2/src/main/java/org/openremote/agent/controller2/Controller2Component.java
@@ -23,7 +23,6 @@ import org.apache.camel.Endpoint;
import org.apache.camel.impl.UriEndpointComponent;
import java.net.URI;
-import java.net.URL;
import java.util.Map;
public class Controller2Component extends UriEndpointComponent {
@@ -48,15 +47,14 @@ public class Controller2Component extends UriEndpointComponent {
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
try {
- URI endpointUri = URI.create(remaining);
- if (endpointUri.getPort() <= 0) {
- throw new Exception("Invalid port number");
- }
+ URI parsedUri = URI.create(uri);
- URL controllerUrl = new URL(endpointUri.getScheme(), endpointUri.getHost(), endpointUri.getPort(), "/controller");
- Endpoint ep = new Controller2Endpoint(uri, this, adapterManager, controllerUrl, endpointUri.getPath());
- setProperties(ep, parameters);
- return ep;
+ parameters.put("host", parsedUri.getHost());
+ parameters.put("port", parsedUri.getPort());
+
+ String path = parsedUri.getPath();
+
+ return new Controller2Endpoint(uri, this, adapterManager, path);
} catch (Exception ex) {
throw new IllegalArgumentException("Required URL in format of " + URI_SYNTAX, ex);
}
diff --git a/connector/controller2/src/main/java/org/openremote/agent/controller2/Controller2Endpoint.java b/connector/controller2/src/main/java/org/openremote/agent/controller2/Controller2Endpoint.java
index 532a9cf58..6a8fdca91 100644
--- a/connector/controller2/src/main/java/org/openremote/agent/controller2/Controller2Endpoint.java
+++ b/connector/controller2/src/main/java/org/openremote/agent/controller2/Controller2Endpoint.java
@@ -24,7 +24,9 @@ import org.apache.camel.Processor;
import org.apache.camel.Producer;
import org.apache.camel.impl.DefaultEndpoint;
import org.apache.camel.spi.UriEndpoint;
+import org.apache.camel.spi.UriParam;
+import java.net.MalformedURLException;
import java.net.URL;
@UriEndpoint(
@@ -38,15 +40,26 @@ public class Controller2Endpoint extends DefaultEndpoint {
final protected Controller2Adapter.Manager adapterManager;
final protected boolean isDiscovery;
final protected boolean isInventory;
- final protected URL controllerUrl;
- @org.apache.camel.spi.UriParam(
+ @UriParam(
+ label = "Host/IP Address",
+ description = "The OR Controller 2 network hostname or IP address"
+ )
+ protected String host;
+
+ @UriParam(
+ label = "Port",
+ description = "The OR Controller 2 network port"
+ )
+ protected Integer port;
+
+ @UriParam(
label = "Username",
description = "The OR Controller 2 Username"
)
protected String username;
- @org.apache.camel.spi.UriParam(
+ @UriParam(
label = "Password",
description = "The OR Controller 2 Password"
)
@@ -57,18 +70,14 @@ public class Controller2Endpoint extends DefaultEndpoint {
protected Controller2Adapter adapter;
- public Controller2Endpoint(String endpointUri, Controller2Component component, Controller2Adapter.Manager adapterManager,
- URL controllerUrl, String path) {
+ public Controller2Endpoint(String endpointUri, Controller2Component component, Controller2Adapter.Manager adapterManager, String path) {
super(endpointUri, component);
+
this.adapterManager = adapterManager;
- this.controllerUrl = controllerUrl;
this.isDiscovery = "/discovery".equals(path);
this.isInventory = "/inventory".equals(path);
if (!isDiscovery && !isInventory && path != null) {
- // See if we have device and resource URIs
- // There must be a nicer way of dealing with this??
- // maybe we should use params and let setProperties on component resolve the values
String[] deviceResourceArr = path.split("//");
if (deviceResourceArr.length == 2) {
deviceUri = deviceResourceArr[0];
@@ -77,6 +86,22 @@ public class Controller2Endpoint extends DefaultEndpoint {
}
}
+ public String getHost() {
+ return host;
+ }
+
+ public void setHost(String host) {
+ this.host = host;
+ }
+
+ public Integer getPort() {
+ return port;
+ }
+
+ public void setPort(Integer port) {
+ this.port = port;
+ }
+
public String getUsername() {
return username;
}
@@ -110,7 +135,6 @@ public class Controller2Endpoint extends DefaultEndpoint {
if (isDiscovery) {
return new Controller2DiscoveryProducer(this);
}
-
return new Controller2WriteProducer(this, deviceUri, resourceUri);
}
@@ -127,7 +151,7 @@ public class Controller2Endpoint extends DefaultEndpoint {
if (deviceUri == null || resourceUri == null) {
return new Controller2ReadConsumer(this, processor, deviceUri, resourceUri);
} else {
- throw new UnsupportedOperationException("Read consumer requires deviceURI and resource URI");
+ throw new UnsupportedOperationException("Read consumer requires deviceURI and resource URI");
}
}
@@ -138,9 +162,15 @@ public class Controller2Endpoint extends DefaultEndpoint {
public Controller2Adapter getAdapter() {
if (adapter == null) {
- adapter = adapterManager.openAdapter(controllerUrl, username, password);
- if (adapter == null)
- throw new IllegalStateException("Manager did not open adapter: " + controllerUrl);
+ try {
+ // TODO: HTTPS support should be implemented with a "?secure=true|false" Endpoint URL query param
+ URL controllerUrl = new URL("http", getHost(), getPort(), "/controller");
+ adapter = adapterManager.openAdapter(controllerUrl, username, password);
+ if (adapter == null)
+ throw new IllegalStateException("Manager did not open adapter: " + controllerUrl);
+ } catch (MalformedURLException ex) {
+ throw new RuntimeException("Invalid controller URL: " + ex);
+ }
}
return adapter;
}
diff --git a/connector/controller2/src/main/resources/META-INF/services/org/apache/camel/component.properties b/connector/controller2/src/main/resources/META-INF/services/org/apache/camel/component.properties
index 7a3593501..be281173f 100644
--- a/connector/controller2/src/main/resources/META-INF/services/org/apache/camel/component.properties
+++ b/connector/controller2/src/main/resources/META-INF/services/org/apache/camel/component.properties
@@ -1,2 +1,4 @@
components=controller2
openremote-connector-type=urn:openremote:connector:controller2
+openremote-connector-supports-discovery=true
+openremote-connector-supports-inventory=true
\ No newline at end of file
diff --git a/manager/client/src/main/java/org/openremote/manager/client/i18n/ManagerMessages.java b/manager/client/src/main/java/org/openremote/manager/client/i18n/ManagerMessages.java
index 4468e5498..bdae669b1 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/i18n/ManagerMessages.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/i18n/ManagerMessages.java
@@ -172,4 +172,6 @@ public interface ManagerMessages extends Messages {
String agentCreated(String name);
String agentDeleted(String name);
+
+ String unsupportedAttributeType(String name);
}
diff --git a/manager/client/src/main/java/org/openremote/manager/client/widget/EntityFormView.java b/manager/client/src/main/java/org/openremote/manager/client/widget/EntityFormView.java
index b2d37056f..2dfb811bb 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/widget/EntityFormView.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/widget/EntityFormView.java
@@ -19,6 +19,7 @@
*/
package org.openremote.manager.client.widget;
+import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import elemental.json.Json;
import org.openremote.manager.shared.connector.Connector;
@@ -37,13 +38,17 @@ public class EntityFormView extends FormView {
if (Connector.IMMUTABLE_ATTRIBUTES.contains(attribute.getName()))
continue;
- // TODO: Support editing non-string attributes
- if (!attribute.getType().equals(AttributeType.STRING)) {
- continue;
- }
-
FormGroup formGroup = createAttributeFormGroup(attribute);
- formGroup.getFormField().add(createAttributeTextBox(formFieldStyleName, attribute));
+
+ if (attribute.getType().equals(AttributeType.STRING)) {
+ formGroup.getFormField().add(createStringEditor(formFieldStyleName, attribute));
+ } else if (attribute.getType().equals(AttributeType.INTEGER)) {
+ formGroup.getFormField().add(createIntegerEditor(formFieldStyleName, attribute));
+ } else {
+ formGroup.getFormField().add(
+ new Label(managerMessages.unsupportedAttributeType(attribute.getType().getName()))
+ );
+ }
list.add(formGroup);
}
@@ -70,7 +75,7 @@ public class EntityFormView extends FormView {
return formGroup;
}
- public TextBox createAttributeTextBox(String formFieldStyleName, Attribute attribute) {
+ protected TextBox createTextBoxEditor(String formFieldStyleName, Attribute attribute) {
TextBox textBox = new TextBox();
textBox.addStyleName(formFieldStyleName);
@@ -89,12 +94,33 @@ public class EntityFormView extends FormView {
if (attribute.getValue() != null) {
textBox.setValue(attribute.getValue().asString());
}
+ return textBox;
+ }
+
+ protected TextBox createStringEditor(String formFieldStyleName, Attribute attribute) {
+ TextBox textBox = createTextBoxEditor(formFieldStyleName, attribute);
+
+ textBox.addValueChangeHandler(
+ event -> attribute.setValue(
+ Json.create(event.getValue())
+ )
+ );
+
+ return textBox;
+ }
+
+ protected TextBox createIntegerEditor(String formFieldStyleName, Attribute attribute) {
+ TextBox textBox = createTextBoxEditor(formFieldStyleName, attribute);
textBox.addValueChangeHandler(event -> {
- attribute.setValue(Json.create(event.getValue()));
+ try {
+ Integer value = Integer.valueOf(event.getValue());
+ attribute.setValue(Json.create(value));
+ } catch (NumberFormatException ex) {
+ // TODO Do nothing? can we hook this up into validation?
+ }
});
return textBox;
}
-
}
diff --git a/manager/client/src/main/resources/org/openremote/manager/client/i18n/ManagerMessages.properties b/manager/client/src/main/resources/org/openremote/manager/client/i18n/ManagerMessages.properties
index b6fa63abf..a19722ad0 100644
--- a/manager/client/src/main/resources/org/openremote/manager/client/i18n/ManagerMessages.properties
+++ b/manager/client/src/main/resources/org/openremote/manager/client/i18n/ManagerMessages.properties
@@ -77,4 +77,5 @@ connectorNotInstalled= CONNECTOR NOT INSTALLED
agentUpdated= Agent updated: {0}
agentCreated= Agent created: {0}
agentDeleted= Agent deleted: {0}
+unsupportedAttributeType= Unsupported attribute type: {0}
diff --git a/manager/server/src/main/java/org/openremote/manager/server/agent/ConnectorService.java b/manager/server/src/main/java/org/openremote/manager/server/agent/ConnectorService.java
index 3e7d8eaa7..7f783ccae 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/agent/ConnectorService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/agent/ConnectorService.java
@@ -99,7 +99,10 @@ public class ConnectorService implements ContainerService {
protected void findConnectors(CamelContext context) throws Exception {
for (Map.Entry<String, Properties> entry : CamelContextHelper.findComponents(context).entrySet()) {
String connectorComponentName = entry.getKey();
- Object connectorType = entry.getValue().get(Connector.PROPERTY_TYPE);
+
+ Properties properties = entry.getValue();
+ Object connectorType = properties.get(Connector.PROPERTY_TYPE);
+
if (connectorType != null) {
Component component = context.getComponent(connectorComponentName);
if (component == null) {
@@ -112,7 +115,7 @@ public class ConnectorService implements ContainerService {
Connector connector = createConnector(
connectorComponentName,
- connectorType.toString(),
+ properties,
uriEndpointComponent
);
@@ -141,11 +144,13 @@ public class ConnectorService implements ContainerService {
}
protected Connector createConnector(String connectorComponentName,
- String connectorType,
+ Properties componentProperties,
UriEndpointComponent component) {
LOG.fine("Creating connector: " + connectorComponentName);
- Connector connector = new Connector(connectorType);
+ Connector connector = new Connector(
+ componentProperties.get(Connector.PROPERTY_TYPE).toString()
+ );
UriEndpoint uriEndpoint = component.getEndpointClass().getAnnotation(UriEndpoint.class);
if (uriEndpoint == null) {
@@ -164,6 +169,16 @@ public class ConnectorService implements ContainerService {
connector.setSyntax(uriEndpoint.syntax());
}
+ Object supportsDiscovery = componentProperties.get(Connector.PROPERTY_SUPPORTS_DISCOVERY);
+ if (supportsDiscovery != null) {
+ connector.setSupportsDiscovery(Boolean.valueOf(supportsDiscovery.toString()));
+ }
+
+ Object supportsInventory = componentProperties.get(Connector.PROPERTY_SUPPORTS_INVENTORY);
+ if (supportsInventory != null) {
+ connector.setSupportsInventory(Boolean.valueOf(supportsInventory.toString()));
+ }
+
addConnectorAttributes(
connector,
connectorComponentName,
@@ -175,9 +190,9 @@ public class ConnectorService implements ContainerService {
}
protected void addConnectorAttributes(Connector connector,
- String connectorComponentName,
- ComponentConfiguration componentConfiguration,
- Class<? extends Endpoint> endpointClass) {
+ String connectorComponentName,
+ ComponentConfiguration componentConfiguration,
+ Class<? extends Endpoint> endpointClass) {
for (Map.Entry<String, ParameterConfiguration> configEntry : componentConfiguration.getParameterConfigurationMap().entrySet()) {
try {
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/connector/Connector.java b/manager/shared/src/main/java/org/openremote/manager/shared/connector/Connector.java
index 014f9f23c..3179f1079 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/connector/Connector.java
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/connector/Connector.java
@@ -40,11 +40,17 @@ public class Connector extends Entity {
// Camel components which are OR connectors must have this in component.properties
public static final String PROPERTY_TYPE = "openremote-connector-type";
+ public static final String PROPERTY_SUPPORTS_DISCOVERY = "openremote-connector-supports-discovery";
+ public static final String PROPERTY_SUPPORTS_INVENTORY = "openremote-connector-supports-inventory";
public static final String ATTRIBUTE_NAME = "name";
public static final String ATTRIBUTE_SYNTAX = "syntax";
+ public static final String ATTRIBUTE_SUPPORTS_DISCOVERY = "supportsDiscovery";
+ public static final String ATTRIBUTE_SUPPORTS_INVENTORY = "supportsInventory";
- public static final List<String> IMMUTABLE_ATTRIBUTES = Arrays.asList(ATTRIBUTE_NAME, ATTRIBUTE_SYNTAX);
+ public static final List<String> IMMUTABLE_ATTRIBUTES = Arrays.asList(
+ ATTRIBUTE_NAME, ATTRIBUTE_SYNTAX, ATTRIBUTE_SUPPORTS_DISCOVERY, ATTRIBUTE_SUPPORTS_INVENTORY
+ );
public Connector() {
}
@@ -76,6 +82,24 @@ public class Connector extends Entity {
super.addAttribute(attr);
}
+ public boolean isSupportsDiscovery() {
+ return getAttributeValueAsBoolean(ATTRIBUTE_SUPPORTS_DISCOVERY);
+ }
+
+ public void setSupportsDiscovery(boolean supportsDiscovery) {
+ Attribute attr = new Attribute(ATTRIBUTE_SUPPORTS_DISCOVERY, AttributeType.BOOLEAN, Json.create(supportsDiscovery));
+ super.addAttribute(attr);
+ }
+
+ public boolean isSupportsInventory() {
+ return getAttributeValueAsBoolean(ATTRIBUTE_SUPPORTS_INVENTORY);
+ }
+
+ public void setSupportsInventory(boolean supportsInventory) {
+ Attribute attr = new Attribute(ATTRIBUTE_SUPPORTS_INVENTORY, AttributeType.BOOLEAN, Json.create(supportsInventory));
+ super.addAttribute(attr);
+ }
+
public void writeSettings(Agent agent) {
JsonObject settings = Json.createObject();
for (Attribute attribute : getAttributes()) {
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 0d93e432d..fca2ef7a4 100644
--- a/test/src/test/groovy/org/openremote/test/controller2/Controller2Service.java
+++ b/test/src/test/groovy/org/openremote/test/controller2/Controller2Service.java
@@ -43,7 +43,7 @@ public class Controller2Service implements ContainerService {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
- from("mockController2:http://10.0.0.123:8080/inventory")
+ from("mockController2:10.0.0.123:8080/inventory")
.routeId("Device Inventory")
.choice()
.when(header(HEADER_DEVICE_ACTION).isEqualTo(ADD))
@@ -64,16 +64,16 @@ public class Controller2Service implements ContainerService {
from("direct:triggerDiscovery")
.routeId("Trigger discovery")
- .to("mockController2:http://10.0.0.123:8080/discovery");
+ .to("mockController2:10.0.0.123:8080/discovery");
- from("mockController2:http://10.0.0.123:8080/discovery")
+ from("mockController2:10.0.0.123:8080/discovery")
.routeId("Discovered devices")
.process(exchange -> {
List<String> discovered = exchange.getIn().getBody(List.class);
discoveredItems.addAll(discovered);
});
- from("mockController2:http://10.0.0.123:8080")
+ from("mockController2:10.0.0.123:8080")
.routeId("Received sensor values")
.process(exchange -> {
receivedSensorData.add(exchange.getIn().getBody(String.class));
@@ -81,11 +81,11 @@ public class Controller2Service implements ContainerService {
from("direct:write")
.routeId("Write Resource")
- .to("mockController2:http://10.0.0.123:8080");
+ .to("mockController2:10.0.0.123:8080");
from("direct:read")
.routeId("Read Resource")
- .to("mockController2:http://10.0.0.123:8080");
+ .to("mockController2:10.0.0.123:8080");
}
});
}