Skip to content

armgit / core

Public Imported from GitHub
0

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

Cleanup of IO client protocols and added generic TCP and Serial client protocols (netty encoders/decoders now exposed to implementers)

Rich Turner67015b4abb95
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 07e1f1c03..1b9ba4d7a 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
@@ -21,7 +21,6 @@ package org.openremote.agent.protocol;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
-import org.openremote.model.value.*;
import org.openremote.container.Container;
import org.openremote.container.ContainerService;
import org.openremote.container.concurrent.GlobalLock;
@@ -32,12 +31,16 @@ import org.openremote.container.timer.TimerService;
import org.openremote.model.ValidationFailure;
import org.openremote.model.ValueHolder;
import org.openremote.model.asset.AssetAttribute;
-import org.openremote.model.attribute.MetaItemType;
import org.openremote.model.asset.agent.AgentLink;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.asset.agent.ProtocolConfiguration;
import org.openremote.model.asset.agent.ProtocolDescriptor;
import org.openremote.model.attribute.*;
+import org.openremote.model.syslog.SyslogCategory;
+import org.openremote.model.util.Pair;
+import org.openremote.model.util.TextUtil;
+import org.openremote.model.value.Value;
+import org.openremote.model.value.Values;
import java.util.*;
import java.util.concurrent.TimeUnit;
@@ -47,6 +50,7 @@ import java.util.logging.Logger;
import static org.openremote.container.concurrent.GlobalLock.withLock;
import static org.openremote.container.concurrent.GlobalLock.withLockReturning;
+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
/**
* Thread-safe base implementation for protocols.
@@ -104,9 +108,10 @@ public abstract class AbstractProtocol implements Protocol {
}
}
- private static final Logger LOG = Logger.getLogger(AbstractProtocol.class.getName());
+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, AbstractProtocol.class);
public static final int PRIORITY = ContainerService.DEFAULT_PRIORITY - 100;
protected final Map<AttributeRef, AssetAttribute> linkedAttributes = new HashMap<>();
+ protected final Set<AttributeRef> dynamicAttributes = new HashSet<>();
protected final Map<AttributeRef, LinkedProtocolInfo> linkedProtocolConfigurations = new HashMap<>();
protected MessageBrokerContext messageBrokerContext;
protected ProducerTemplate producerTemplate;
@@ -177,21 +182,32 @@ public abstract class AbstractProtocol implements Protocol {
final public void linkProtocolConfiguration(AssetAttribute protocolConfiguration, Consumer<ConnectionStatus> statusConsumer) {
withLock(getProtocolName() + "::linkProtocolConfiguration", () -> {
LOG.finer("Linking protocol configuration to protocol '" + getProtocolName() + "': " + protocolConfiguration);
+
+ ConnectionStatus currentStatus = protocolConfiguration.isEnabled() ? ConnectionStatus.CONNECTING : ConnectionStatus.DISABLED;
+
linkedProtocolConfigurations.put(
protocolConfiguration.getReferenceOrThrow(),
- new LinkedProtocolInfo(protocolConfiguration, statusConsumer, ConnectionStatus.CONNECTING)
+ new LinkedProtocolInfo(protocolConfiguration, statusConsumer, currentStatus)
);
- statusConsumer.accept(ConnectionStatus.CONNECTING);
- doLinkProtocolConfiguration(protocolConfiguration);
+ statusConsumer.accept(currentStatus);
+ if (currentStatus == ConnectionStatus.CONNECTING) {
+ doLinkProtocolConfiguration(protocolConfiguration);
+ } else {
+ LOG.info("Protocol configuration is disabled so not linking '" + getProtocolName() + "': " + protocolConfiguration);
+ }
});
}
@Override
final public void unlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
withLock(getProtocolName() + "::unlinkProtocolConfiguration", () -> {
- LOG.finer("Unlinking protocol configuration from protocol '" + getProtocolName() + "': " + protocolConfiguration);
- doUnlinkProtocolConfiguration(protocolConfiguration);
- updateStatus(protocolConfiguration.getReferenceOrThrow(), ConnectionStatus.DISCONNECTED);
+
+ LinkedProtocolInfo protocolInfo = linkedProtocolConfigurations.get(protocolConfiguration.getReferenceOrThrow());
+ if (protocolInfo != null && protocolInfo.currentConnectionStatus != ConnectionStatus.DISABLED) {
+ LOG.finer("Unlinking protocol configuration from protocol '" + getProtocolName() + "': " + protocolConfiguration);
+ doUnlinkProtocolConfiguration(protocolConfiguration);
+ updateStatus(protocolConfiguration.getReferenceOrThrow(), ConnectionStatus.DISCONNECTED);
+ }
linkedProtocolConfigurations.remove(protocolConfiguration.getReferenceOrThrow());
});
}
@@ -199,6 +215,12 @@ public abstract class AbstractProtocol implements Protocol {
@Override
final public void linkAttributes(Collection<AssetAttribute> attributes, AssetAttribute protocolConfiguration) {
withLock(getProtocolName() + "::linkAttributes", () -> {
+
+ if (!protocolConfiguration.isEnabled()) {
+ LOG.info("Protocol configuration is disabled so not linking attributes '" + getProtocolName() + "': " + protocolConfiguration);
+ return;
+ }
+
attributes.forEach(attribute -> {
LOG.fine("Linking attribute to '" + getProtocolName() + "': " + attribute);
AttributeRef attributeRef = attribute.getReferenceOrThrow();
@@ -206,6 +228,14 @@ public abstract class AbstractProtocol implements Protocol {
// linking process and without entry in the map any update would be blocked
linkedAttributes.put(attributeRef, attribute);
+ // Check for dynamic value placeholder
+ final String writeValue = Values.getMetaItemValueOrThrow(attribute, META_ATTRIBUTE_WRITE_VALUE, false, true)
+ .map(Object::toString).orElse(null);
+
+ if (!TextUtil.isNullOrEmpty(writeValue) && writeValue.contains(DYNAMIC_VALUE_PLACEHOLDER)) {
+ dynamicAttributes.add(attributeRef);
+ }
+
try {
doLinkAttribute(attribute, protocolConfiguration);
} catch (Exception e) {
@@ -218,14 +248,22 @@ public abstract class AbstractProtocol implements Protocol {
@Override
final public void unlinkAttributes(Collection<AssetAttribute> attributes, AssetAttribute protocolConfiguration) throws Exception {
- withLock(getProtocolName() + "::unlinkAttributes", () ->
+ withLock(getProtocolName() + "::unlinkAttributes", () -> {
+
+ LinkedProtocolInfo protocolInfo = linkedProtocolConfigurations.get(protocolConfiguration.getReferenceOrThrow());
+ if (protocolInfo.currentConnectionStatus == ConnectionStatus.DISABLED) {
+ LOG.info("Protocol configuration is disabled so not unlinking attributes '" + getProtocolName() + "': " + protocolConfiguration);
+ return;
+ }
+
attributes.forEach(attribute -> {
LOG.fine("Unlinking attribute on '" + getProtocolName() + "': " + attribute);
AttributeRef attributeRef = attribute.getReferenceOrThrow();
linkedAttributes.remove(attributeRef);
+ dynamicAttributes.remove(attributeRef);
doUnlinkAttribute(attribute, protocolConfiguration);
- })
- );
+ });
+ });
}
/**
@@ -255,34 +293,24 @@ public abstract class AbstractProtocol implements Protocol {
final protected void processLinkedAttributeWrite(AttributeEvent event) {
LOG.finest("Processing linked attribute write on " + getProtocolName() + ": " + event);
withLock(getProtocolName() + "::processLinkedAttributeWrite", () -> {
- AssetAttribute attribute = linkedAttributes.get(event.getAttributeRef());
+ AttributeRef attributeRef = event.getAttributeRef();
+ AssetAttribute attribute = linkedAttributes.get(attributeRef);
if (attribute == null) {
- LOG.warning("Attribute doesn't exist on this protocol: " + event.getAttributeRef());
+ LOG.warning("Attribute doesn't exist on this protocol: " + attributeRef);
} else {
- // Do outbound value conversion
- ObjectValue converter = Values.getMetaItemValueOrThrow(
+ Pair<Boolean, Value> ignoreAndConverted = Protocol.doOutboundValueProcessing(
attribute,
- META_ATTRIBUTE_WRITE_VALUE_CONVERTER,
- false,
- false)
- .flatMap(Values::getObject)
- .orElse(null);
-
- if (converter != null) {
- LOG.fine("Applying attribute value converter to attribute write: " + event.getAttributeRef());
-
- ConvertedValue convertedValue = assetService.applyValueConverter(event.getValue().orElse(null), converter);
+ event.getValue().orElse(null),
+ dynamicAttributes.contains(attributeRef));
- if (convertedValue.isIgnore()) {
- LOG.info("Value converter returned '" + ConvertedValue.IGNORE + "' so ignoring attribute value");
- return;
- }
- event.getAttributeState().setValue(convertedValue.getValue());
+ if (ignoreAndConverted.key) {
+ LOG.info("Value conversion returned ignore so attribute will not write to protocol: " + attribute.getReferenceOrThrow());
+ return;
}
AssetAttribute protocolConfiguration = getLinkedProtocolConfiguration(attribute);
- processLinkedAttributeWrite(event, protocolConfiguration);
+ processLinkedAttributeWrite(event, ignoreAndConverted.value, protocolConfiguration);
}
});
}
@@ -312,14 +340,11 @@ public abstract class AbstractProtocol implements Protocol {
}
/**
- * Update the value of a linked attribute. Call this to publish new sensor values. This will apply any
- * {@link ValueFilter}s that have been set for the {@link Attribute} against the {@link AttributeState#getValue}
- * before sending on the sensor queue.
+ * Update the value of a linked attribute. Call this to publish new sensor values. This will call
+ * {@link #doInboundValueProcessing} before sending on the sensor queue.
*/
- @SuppressWarnings("unchecked")
- final protected void updateLinkedAttribute(final AttributeState finalState, long timestamp) {
+ final protected void updateLinkedAttribute(final AttributeState state, long timestamp) {
withLock(getProtocolName() + "::updateLinkedAttribute", () -> {
- AttributeState state = finalState;
AssetAttribute attribute = linkedAttributes.get(state.getAttributeRef());
if (attribute == null) {
@@ -327,54 +352,14 @@ public abstract class AbstractProtocol implements Protocol {
return;
}
- if (state.getValue().isPresent()) {
- Value value = state.getValue().get();
- ValueFilter[] filters = Protocol.getLinkedAttributeValueFilters(attribute).orElse(null);
-
- if (filters != null) {
- value = assetService.applyValueFilters(value, filters);
- }
-
- ObjectValue converter = Values.getMetaItemValueOrThrow(
- attribute,
- META_ATTRIBUTE_VALUE_CONVERTER,
- false,
- false)
- .flatMap(Values::getObject)
- .orElse(null);
-
- if (converter != null) {
- LOG.fine("Applying attribute value converter to attribute: " + state.getAttributeRef());
-
- ConvertedValue convertedValue = assetService.applyValueConverter(value, converter);
+ Pair<Boolean, Value> ignoreAndConverted = Protocol.doInboundValueProcessing(attribute, state.getValue().orElse(null), assetService);
- if (convertedValue.isIgnore()) {
- LOG.info("Value converter returned '" + ConvertedValue.IGNORE + "' so ignoring attribute value");
- return;
- }
- value = convertedValue.getValue();
- }
-
- // Do built in value conversion
- Optional<ValueType> attributeValueType = attribute.getType().map(AttributeValueDescriptor::getValueType);
-
- if (value != null && attributeValueType.isPresent()) {
- if (attributeValueType.get() != value.getType()) {
- LOG.fine("Trying to convert value: " + value.getType() + " -> " + attributeValueType.get());
- Optional<Value> convertedValue = Values.convert(value, attributeValueType.get());
- if (!convertedValue.isPresent()) {
- LOG.warning("Failed to convert value: " + value.getType() + " -> " + attributeValueType.get());
- LOG.warning("Cannot send linked attribute update");
- return;
- } else {
- value = convertedValue.get();
- }
- }
- }
-
- state = new AttributeState(state.getAttributeRef(), value);
+ if (ignoreAndConverted.key) {
+ LOG.info("Value conversion returned ignore so attribute will not be updated: " + attribute.getReferenceOrThrow());
+ return;
}
- AttributeEvent attributeEvent = new AttributeEvent(state, timestamp);
+
+ AttributeEvent attributeEvent = new AttributeEvent(new AttributeState(attribute.getReferenceOrThrow(), ignoreAndConverted.value), timestamp);
LOG.fine("Sending on sensor queue: " + attributeEvent);
producerTemplate.sendBodyAndHeader(SENSOR_QUEUE, attributeEvent, Protocol.SENSOR_QUEUE_SOURCE_PROTOCOL, getProtocolName());
});
@@ -453,6 +438,9 @@ public abstract class AbstractProtocol implements Protocol {
if (descriptors.stream().noneMatch(d -> d.getUrn().equalsIgnoreCase(META_ATTRIBUTE_VALUE_CONVERTER.getUrn()))) {
descriptors.add(META_ATTRIBUTE_VALUE_CONVERTER);
}
+ if (descriptors.stream().noneMatch(d -> d.getUrn().equalsIgnoreCase(META_ATTRIBUTE_WRITE_VALUE.getUrn()))) {
+ descriptors.add(META_ATTRIBUTE_WRITE_VALUE);
+ }
if (descriptors.stream().noneMatch(d -> d.getUrn().equalsIgnoreCase(META_ATTRIBUTE_WRITE_VALUE_CONVERTER.getUrn()))) {
descriptors.add(META_ATTRIBUTE_WRITE_VALUE_CONVERTER);
}
@@ -529,7 +517,12 @@ public abstract class AbstractProtocol implements Protocol {
abstract protected void doUnlinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration);
/**
- * Attribute event (write) has been requested for an attribute linked to the specified protocol configuration.
+ * Attribute event (write) has been requested for an attribute linked to the specified protocol configuration. The
+ * processedValue is the resulting {@link Value} after applying any {@link #META_ATTRIBUTE_WRITE_VALUE} and/or
+ * {@link #META_ATTRIBUTE_WRITE_VALUE_CONVERTER} {@link MetaItem}s that are defined on the {@link Attribute}; if
+ * neither are defined then the processedValue will be the same as {@link AttributeEvent#getValue}. Protocol
+ * implementations should generally use the processedValue but may also choose to use the original value for some
+ * purpose if required (e.g. {@link org.openremote.agent.protocol.http.HttpClientProtocol#META_QUERY_PARAMETERS}).
*/
- abstract protected void processLinkedAttributeWrite(AttributeEvent event, AssetAttribute protocolConfiguration);
+ abstract protected void processLinkedAttributeWrite(AttributeEvent event, Value processedValue, AssetAttribute protocolConfiguration);
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/Protocol.java b/agent/src/main/java/org/openremote/agent/protocol/Protocol.java
index 3ee4b5e1e..e06db50cf 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/Protocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/Protocol.java
@@ -32,12 +32,17 @@ import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.asset.agent.ProtocolConfiguration;
import org.openremote.model.asset.agent.ProtocolDescriptor;
import org.openremote.model.attribute.*;
+import org.openremote.model.query.AssetQuery;
+import org.openremote.model.query.filter.StringPredicate;
+import org.openremote.model.syslog.SyslogCategory;
+import org.openremote.model.util.Pair;
import org.openremote.model.util.TextUtil;
import org.openremote.model.value.*;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Collection;
+import java.util.Locale;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.logging.Level;
@@ -46,7 +51,9 @@ import java.util.logging.Logger;
import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
import static org.openremote.model.attribute.MetaItemDescriptor.Access.ACCESS_PRIVATE;
import static org.openremote.model.attribute.MetaItemDescriptorImpl.*;
+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_STRING_NON_EMPTY;
+import static org.openremote.model.value.Values.NULL_LITERAL;
/**
* A protocol is a thread-safe singleton {@link ContainerService} that connects devices and
@@ -88,18 +95,34 @@ import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_STRING_NON_EMPTY
* body will be an {@link AttributeEvent}. Each message also contains the target protocol name in
* header {@link #ACTUATOR_TOPIC_TARGET_PROTOCOL}.
* <p>
- * Data type conversion is also delegated to the protocol implementation: If an attribute has a particular
- * {@link AttributeValueType} and therefore a base {@link ValueType}, the protocol implementation must
- * receive and send value change messages with values of that type.
+ * To simplify protocol development some common protocol behaviour is recommended:
+ * <h1>Inbound value conversion (Protocol -> Linked Attribute)</h1>
* <p>
- * Generic protocols should implement support for filtering state messages from devices (or services) before the
- * protocol updates the linked attribute, to implement this protocols should use the {@link #META_ATTRIBUTE_VALUE_FILTERS}
- * {@link MetaItem}.
- * <p>
- * <h1>Dynamic value injection</h1>
- * This allows values to be dynamically injected into strings when a protocol linked attribute write occurs; the value
- * contained within the {@link AttributeEvent} can be dynamically injected into the string by using the
- * {@value Protocol#DYNAMIC_VALUE_PLACEHOLDER} as a placeholder and this will be dynamically replaced at request time.
+ * Standard value filtering and/or conversion should be performed in the following order, this is encapsulated in
+ * {@link #doInboundValueProcessing}:
+ * <ol>
+ * <li>Configurable value filtering which allows the value produced by the protocol to be filtered through any
+ * number of {@link ValueFilter}s before being written to the linked attribute
+ * (see {@link #META_ATTRIBUTE_VALUE_FILTERS})</li>
+ * <li>Configurable value conversion which allows the value produced by the protocol to be converted in a configurable
+ * way before being written to the linked attribute (see {@link #META_ATTRIBUTE_VALUE_CONVERTER})</li>
+ * <li>Automatic basic value conversion should be performed when the {@link ValueType} of the value produced by the
+ * protocol and any configured value conversion does not match the linked attributes underlying {@link ValueType}; this
+ * basic conversion should use the {@link Values#convert} method</li>
+ * </ol>
+ * <h1>Outbound value conversion (Linked Attribute -> Protocol)</h1>
+ * Standard value conversion should be performed in the following order, this is encapsulated in
+ * {@link #doOutboundValueProcessing}:
+ * <ol>
+ * <li>Configurable value conversion which allows the value sent from the linked attribute to be converted in a
+ * configurable way before being sent to the protocol for processing (see {@link #META_ATTRIBUTE_WRITE_VALUE_CONVERTER})
+ * <li>Configurable dynamic value insertion (replacement of {@link #DYNAMIC_VALUE_PLACEHOLDER} strings within a
+ * pre-defined JSON string with the value sent from the linked attribute (this allows for attribute values to be inserted
+ * into a larger payload before processing by the protocol; it also allows the written value to be fixed or statically
+ * converted.
+ * </ol>
+ * When sending the converted value onto the actual protocol implementation for processing the original
+ * {@link AttributeEvent} as well as the converted value should be made available.
* <p>
* NOTE: That {@link #linkProtocolConfiguration} will always be called
* before {@link #linkAttributes} and {@link #unlinkAttributes} will always be called before
@@ -145,7 +168,7 @@ import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_STRING_NON_EMPTY
*/
public interface Protocol extends ContainerService {
- Logger LOG = Logger.getLogger(Protocol.class.getName());
+ Logger LOG = SyslogCategory.getLogger(PROTOCOL, Protocol.class);
String ACTUATOR_TOPIC_TARGET_PROTOCOL = "Protocol";
String SENSOR_QUEUE_SOURCE_PROTOCOL = "Protocol";
@@ -172,6 +195,51 @@ public interface Protocol extends ContainerService {
Charset.availableCharsets().keySet().toArray(new String[0])
);
+ /**
+ * Max length of messages received by a {@link Protocol}; what this actually means will be protocol specific
+ * i.e. for {@link String} protocols it could be the number of characters but for {@link Byte} protocols it could be
+ * the number of bytes. This is typically used for {@link org.openremote.agent.protocol.io.IoClient} based
+ * {@link Protocol}s.
+ */
+ MetaItemDescriptor META_PROTOCOL_MAX_LENGTH = metaItemInteger(
+ PROTOCOL_NAMESPACE + ":maxLength",
+ ACCESS_PRIVATE,
+ false,
+ 0,
+ Integer.MAX_VALUE
+ );
+
+ /**
+ * Defines a delimiter for messages received by a {@link Protocol}. Multiples of this {@link MetaItem} can be used
+ * to add multiple delimiters (the first matched delimiter should be used to generate the shortest possible match(.
+ * This is typically used for {@link org.openremote.agent.protocol.io.IoClient} based {@link Protocol}s.
+ */
+ MetaItemDescriptor META_PROTOCOL_DELIMITER = new MetaItemDescriptorImpl(
+ PROTOCOL_NAMESPACE + ":delimiter",
+ ValueType.STRING,
+ ACCESS_PRIVATE,
+ false,
+ null,
+ null,
+ null,
+ null,
+ false,
+ null,
+ null,
+ null,
+ false
+ );
+
+ /**
+ * For protocols that use {@link #META_PROTOCOL_DELIMITER}, this indicates whether or not the matched delimiter
+ * should be stripped from the message.
+ */
+ MetaItemDescriptor META_PROTOCOL_STRIP_DELIMITER = metaItemFixedBoolean(
+ PROTOCOL_NAMESPACE + ":stripDelimiter",
+ ACCESS_PRIVATE,
+ false
+ );
+
/**
* OAuth grant ({@link OAuthGrant} stored as {@link ObjectValue})
*/
@@ -203,12 +271,49 @@ public interface Protocol extends ContainerService {
REGEXP_PATTERN_STRING_NON_EMPTY,
PatternFailure.STRING_EMPTY);
+
/**
- * Defines {@link ValueFilter}s to apply to values before they are sent through the system (e.g. before it is used
- * to update a protocol linked attribute); this is particularly useful for generic protocols.
- * The {@link MetaItem} value should be an {@link ArrayValue} of {@link ObjectValue}s
- * where each {@link ObjectValue} represents a serialised {@link ValueFilter}. The message should pass through the
- * filters in array order.
+ * TCP/IP network host name/IP address
+ */
+ MetaItemDescriptor META_PROTOCOL_HOST = metaItemString(
+ PROTOCOL_NAMESPACE + ":host",
+ ACCESS_PRIVATE,
+ true,
+ TextUtil.REGEXP_PATTERN_STRING_NON_EMPTY_NO_WHITESPACE,
+ PatternFailure.STRING_EMPTY_OR_CONTAINS_WHITESPACE);
+
+ /**
+ * TCP/IP network port number
+ */
+ MetaItemDescriptor META_PROTOCOL_PORT = metaItemInteger(
+ PROTOCOL_NAMESPACE + ":port",
+ ACCESS_PRIVATE,
+ true,
+ 1,
+ 65536);
+
+ /**
+ * Serial port name/address
+ */
+ MetaItemDescriptor META_PROTOCOL_SERIAL_PORT = metaItemString(
+ PROTOCOL_NAMESPACE + ":serialPort",
+ ACCESS_PRIVATE,
+ true,
+ REGEXP_PATTERN_STRING_NON_EMPTY,
+ PatternFailure.STRING_EMPTY);
+
+ MetaItemDescriptor META_PROTOCOL_SERIAL_BAUDRATE = metaItemInteger(
+ PROTOCOL_NAMESPACE + ":baudrate",
+ ACCESS_PRIVATE,
+ true,
+ 1,
+ Integer.MAX_VALUE);
+
+ /**
+ * Defines {@link ValueFilter}s to apply to an incoming value before it is written to a protocol linked attribute;
+ * this is particularly useful for generic protocols. The {@link MetaItem} value should be an {@link ArrayValue} of
+ * {@link ObjectValue}s where each {@link ObjectValue} represents a serialised {@link ValueFilter}. The message
+ * should pass through the filters in array order.
*/
MetaItemDescriptor META_ATTRIBUTE_VALUE_FILTERS = metaItemArray(
PROTOCOL_NAMESPACE + ":valueFilters",
@@ -239,9 +344,14 @@ public interface Protocol extends ContainerService {
null);
/**
- * Value to be used for attribute writes, protocols that support this should also support dynamic value insertion,
- * see interface javadoc for more details; use the {@link #createDynamicAttributeWriteConsumer} helper method where
- * possible.
+ * JSON string to be used for attribute writes and can contain {@link #DYNAMIC_VALUE_PLACEHOLDER}s; this allows the
+ * written value to be injected into a bigger JSON payload or to even hardcode the value sent to the protocol
+ * (i.e. ignore the written value). If this {@link MetaItem} is not defined then the written value is passed through
+ * to the protocol as is. The resulting JSON string (after any dynamic value insertion) is then parsed using
+ * {@link Values#parse} so it is important the value of this {@link MetaItem} is a valid JSON string so literal
+ * strings must be quoted (e.g. '"string value"' not 'string value' otherwise parsing will fail).
+ * <p>
+ * A value of 'null' will produce a literal null.
*/
MetaItemDescriptor META_ATTRIBUTE_WRITE_VALUE = metaItemAny(
PROTOCOL_NAMESPACE + ":writeValue",
@@ -255,13 +365,39 @@ public interface Protocol extends ContainerService {
* Polling frequency in milliseconds for {@link Attribute}s whose value should be polled; can be set on the
* {@link ProtocolConfiguration} or the {@link Attribute} (the latter takes precedence).
*/
- MetaItemDescriptor META_POLLING_MILLIS = metaItemInteger(
+ MetaItemDescriptor META_ATTRIBUTE_POLLING_MILLIS = metaItemInteger(
PROTOCOL_NAMESPACE + ":pollingMillis",
ACCESS_PRIVATE,
false,
1000,
null);
+ /**
+ * The predicate to use on incoming messages to determine if the message is intended for the {@link Attribute} that
+ * has this {@link MetaItem}; it is particularly useful for pub-sub based {@link Protocol}s.
+ */
+ MetaItemDescriptor META_ATTRIBUTE_MATCH_PREDICATE = metaItemObject(
+ PROTOCOL_NAMESPACE + ":matchPredicate",
+ ACCESS_PRIVATE,
+ false,
+ new StringPredicate(AssetQuery.Match.EXACT, false, "").toModelValue());
+
+ /**
+ * {@link ValueFilter}s to apply to incoming messages prior to comparison with the
+ * {@link Protocol#META_ATTRIBUTE_MATCH_PREDICATE}, if the predicate matches then the original un-filtered
+ * message is intended for this linked {@link Attribute} and the message should be written to the {@link Attribute}
+ * where the actual {@link Value} written can be filtered using the {@link Protocol#META_ATTRIBUTE_VALUE_FILTERS}.
+ * <p>
+ * The {@link Value} of this {@link MetaItem} must be an {@link ArrayValue} of {@link ObjectValue}s where each
+ * {@link ObjectValue} represents a serialised {@link ValueFilter}. The message will pass through the filters in
+ * array order and the resulting final {@link Value} should be written to the {@link Attribute}
+ */
+ MetaItemDescriptor META_ATTRIBUTE_MATCH_FILTERS = metaItemArray(
+ PROTOCOL_NAMESPACE + ":matchFilters",
+ ACCESS_PRIVATE,
+ false,
+ null);
+
// TODO: Some of these options should be configurable depending on expected load etc.
// Message topic for communicating from asset/thing to protocol layer (asset attribute changed, trigger actuator)
String ACTUATOR_TOPIC = "seda://ActuatorTopic?multipleConsumers=true&concurrentConsumers=1&waitForTaskToComplete=NEVER&purgeWhenStopping=true&discardIfNoConsumers=true&limitConcurrentConsumers=false&size=1000";
@@ -405,24 +541,201 @@ public interface Protocol extends ContainerService {
}
}
- static Consumer<Value> createDynamicAttributeWriteConsumer(AssetAttribute attribute, Consumer<String> writeConsumer) {
+ /**
+ * Will perform recommended value processing for outbound values (Linked Attribute -> Protocol); the
+ * containsDynamicPlaceholder flag is required so that the entire {@link #META_ATTRIBUTE_WRITE_VALUE} payload is
+ * not searched on every single write request (for performance reasons), instead this should be recorded when the
+ * attribute is first linked.
+ */
+ static Pair<Boolean, Value> doOutboundValueProcessing(AssetAttribute attribute, Value value, boolean containsDynamicPlaceholder) {
- final String writeValue = Values.getMetaItemValueOrThrow(attribute, META_ATTRIBUTE_WRITE_VALUE, false, true)
+ String writeValue = Values.getMetaItemValueOrThrow(attribute, META_ATTRIBUTE_WRITE_VALUE, false, true)
.map(Object::toString).orElse(null);
- return value -> {
+ if (attribute.isExecutable()) {
+ AttributeExecuteStatus status = Values.getString(value)
+ .flatMap(AttributeExecuteStatus::fromString)
+ .orElse(null);
- String str = value != null ? value.toString() : "";
+ if (status == AttributeExecuteStatus.REQUEST_START && writeValue != null) {
+ try {
+ value = Values.parse(writeValue).orElse(null);
+ } catch (Exception e) {
+ value = null;
+ LOG.log(Level.INFO, "Failed to pass attribute write payload generated by META_ATTRIBUTE_WRITE_VALUE", e);
+ }
+ return new Pair<>(false, value);
+ }
+ }
+
+ // value conversion
+ ObjectValue converter = Values.getMetaItemValueOrThrow(
+ attribute,
+ META_ATTRIBUTE_WRITE_VALUE_CONVERTER,
+ false,
+ false)
+ .flatMap(Values::getObject)
+ .orElse(null);
+
+ if (converter != null) {
+ LOG.fine("Applying attribute value converter to attribute write: " + attribute.getReferenceOrThrow());
+
+ Pair<Boolean, Value> converterResult = applyValueConverter(value, converter);
+
+ if (converterResult.key) {
+ return converterResult;
+ }
+ }
+
+ // dynamic value insertion
+ boolean hasWriteValue = attribute.hasMetaItem(META_ATTRIBUTE_WRITE_VALUE);
+
+ if (hasWriteValue) {
+ if (writeValue == null) {
+ LOG.fine("META_ATTRIBUTE_WRITE_VALUE contains null so sending null to protocol for attribute write on: " + attribute.getReferenceOrThrow());
+ return new Pair<>(false, null);
+ }
+
+ if (containsDynamicPlaceholder) {
+ String valueStr = value == null ? NULL_LITERAL : value.toString();
+ writeValue = writeValue.replaceAll(DYNAMIC_VALUE_PLACEHOLDER_REGEXP, valueStr);
+ }
+
+ try {
+ value = Values.parse(writeValue).orElse(null);
+ } catch (Exception e) {
+ LOG.log(Level.INFO, "Failed to pass attribute write payload generated by META_ATTRIBUTE_WRITE_VALUE", e);
+ }
+ }
+
+ return new Pair<>(false, value);
+ }
+
+ static Pair<Boolean, Value> doInboundValueProcessing(AssetAttribute attribute, Value value, ProtocolAssetService assetService) {
+
+ // filtering
+ ValueFilter[] filters = Protocol.getLinkedAttributeValueFilters(attribute).orElse(null);
+ if (filters != null) {
+ value = assetService.applyValueFilters(value, filters);
+ }
+
+ // value conversion
+ ObjectValue converter = Values.getMetaItemValueOrThrow(
+ attribute,
+ META_ATTRIBUTE_VALUE_CONVERTER,
+ false,
+ false)
+ .flatMap(Values::getObject)
+ .orElse(null);
+
+ if (converter != null) {
+ LOG.fine("Applying attribute value converter to attribute: " + attribute.getReferenceOrThrow());
+
+ Pair<Boolean, Value> convertedValue = applyValueConverter(value, converter);
+
+ if (convertedValue.key) {
+ return convertedValue;
+ }
+
+ value = convertedValue.value;
+ }
- if (!TextUtil.isNullOrEmpty(writeValue)) {
- if (str.isEmpty()) {
- str = writeValue;
- } else if (writeValue.contains(DYNAMIC_VALUE_PLACEHOLDER)) {
- str = writeValue.replaceAll(DYNAMIC_VALUE_PLACEHOLDER_REGEXP, str);
+ // built in value conversion
+ Optional<ValueType> attributeValueType = attribute.getType().map(AttributeValueDescriptor::getValueType);
+
+ if (value != null && attributeValueType.isPresent()) {
+ if (attributeValueType.get() != value.getType()) {
+ LOG.fine("Trying to convert value: " + value.getType() + " -> " + attributeValueType.get());
+ Optional<Value> convertedValue = Values.convert(value, attributeValueType.get());
+
+ if (!convertedValue.isPresent()) {
+ LOG.warning("Failed to convert value: " + value.getType() + " -> " + attributeValueType.get());
+ LOG.warning("Cannot send linked attribute update");
+ return new Pair<>(true, null);
}
+
+ value = convertedValue.get();
}
+ }
+
+ return new Pair<>(false, value);
+ }
+
+ static Pair<Boolean, Value> applyValueConverter(Value value, ObjectValue converter) {
+
+ if (converter == null) {
+ return new Pair<>(false, value);
+ }
+
+ String converterKey = value == null ? NULL_LITERAL.toUpperCase() : value.toString().toUpperCase(Locale.ROOT);
+ return converter.get(converterKey)
+ .map(v -> {
+ if (v.getType() == ValueType.STRING) {
+ String valStr = v.toString();
+ if ("@IGNORE".equalsIgnoreCase(valStr)) {
+ return new Pair<>(true, (Value)null);
+ }
+
+ if ("@NULL".equalsIgnoreCase(valStr)) {
+ return new Pair<>(false, (Value)null);
+ }
+ }
+
+ return new Pair<>(false, v);
+ })
+ .orElse(new Pair<>(true, value));
+ }
+
+ static Consumer<String> createGenericAttributeMessageConsumer(AssetAttribute attribute, ProtocolAssetService assetService, Consumer<AttributeState> stateConsumer) {
- writeConsumer.accept(str);
+ ValueFilter[] matchFilters = Values.getMetaItemValueOrThrow(
+ attribute,
+ META_ATTRIBUTE_MATCH_FILTERS,
+ false,
+ true)
+ .map(Value::toJson)
+ .map(json -> {
+ try {
+ return Container.JSON.readValue(json, ValueFilter[].class);
+ } catch (IOException e) {
+ LOG.log(Level.WARNING, "Failed to deserialize ValueFilter[]", e);
+ return null;
+ }
+ }).orElse(null);
+
+ StringPredicate matchPredicate = Values.getMetaItemValueOrThrow(
+ attribute,
+ META_ATTRIBUTE_MATCH_PREDICATE,
+ false,
+ true)
+ .map(Value::toJson)
+ .map(s -> {
+ try {
+ return Container.JSON.readValue(s, StringPredicate.class);
+ } catch (IOException e) {
+ LOG.log(Level.WARNING, "Failed to deserialise StringPredicate", e);
+ return null;
+ }
+ })
+ .orElse(null);
+
+ if (matchPredicate == null) {
+ return null;
+ }
+
+ AttributeRef attributeRef = attribute.getReferenceOrThrow();
+
+ return message -> {
+ if (!TextUtil.isNullOrEmpty(message)) {
+ StringValue stringValue = Values.create(message);
+ Value val = assetService.applyValueFilters(stringValue, matchFilters);
+ if (val != null) {
+ if (StringPredicate.asPredicate(matchPredicate).test(message)) {
+ LOG.finest("Message matches attribute so writing state to state consumer for attribute: " + attributeRef);
+ stateConsumer.accept(new AttributeState(attributeRef, stringValue));
+ }
+ }
+ }
};
}
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/ProtocolAssetService.java b/agent/src/main/java/org/openremote/agent/protocol/ProtocolAssetService.java
index 0de55ccbd..d4be2c5d1 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/ProtocolAssetService.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/ProtocolAssetService.java
@@ -23,12 +23,9 @@ import org.openremote.container.ContainerService;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.attribute.AttributeEvent;
-import org.openremote.model.value.ConvertedValue;
-import org.openremote.model.value.ObjectValue;
import org.openremote.model.value.Value;
import org.openremote.model.value.ValueFilter;
-import java.util.Optional;
import java.util.function.Predicate;
/**
@@ -150,9 +147,4 @@ public interface ProtocolAssetService extends ContainerService {
* Apply the specified set of {@link ValueFilter}s to the specified {@link Value}
*/
Value applyValueFilters(Value value, ValueFilter... filters);
-
- /**
- * Apply the specified value converter map to the specified value
- */
- ConvertedValue applyValueConverter(Value value, ObjectValue converter);
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/controller/ControllerProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/controller/ControllerProtocol.java
index 32fa5943d..88d81fe14 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/controller/ControllerProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/controller/ControllerProtocol.java
@@ -33,12 +33,12 @@ import org.openremote.model.AbstractValueHolder;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.attribute.*;
+import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.value.*;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
-import javax.ws.rs.core.UriBuilder;
import java.net.ConnectException;
import java.net.URI;
import java.net.URISyntaxException;
@@ -55,6 +55,7 @@ import static org.openremote.agent.protocol.http.WebTargetBuilder.createClient;
import static org.openremote.container.concurrent.GlobalLock.withLock;
import static org.openremote.container.concurrent.GlobalLock.withLockReturning;
import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
import static org.openremote.model.util.TextUtil.*;
/**
@@ -89,7 +90,7 @@ import static org.openremote.model.util.TextUtil.*;
@SuppressWarnings("JavaDoc")
public class ControllerProtocol extends AbstractProtocol {
- private static final Logger LOG = Logger.getLogger(ControllerProtocol.class.getName());
+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, ControllerProtocol.class);
public static final String PROTOCOL_NAME = PROTOCOL_NAMESPACE + ":controllerClient";
@@ -320,18 +321,28 @@ public class ControllerProtocol extends AbstractProtocol {
/**
* Write action on a linked attribute mean we execute a command on the Controller. It induce a HTTP request and manage it's return code. (No value
* is returned from the execution of a command)
- *
- * @param event
+ * @param event
+ * @param processedValue
* @param protocolConfiguration
*/
@Override
- protected void processLinkedAttributeWrite(AttributeEvent event, AssetAttribute protocolConfiguration) {
+ protected void processLinkedAttributeWrite(AttributeEvent event, Value processedValue, AssetAttribute protocolConfiguration) {
LOG.fine("### Process Linked Attribute Write");
AttributeRef attributeRef = event.getAttributeRef();
ControllerCommand controllerCommand = controllersMap.get(protocolConfiguration.getReferenceOrThrow()).getCommand(attributeRef);
HttpClientProtocol.HttpClientRequest request = RequestBuilder.buildCommandRequest(controllerCommand, event, this.controllersTargetMap.get(protocolConfiguration.getReferenceOrThrow()));
- executeAttributeWriteRequest(request, protocolConfiguration.getReferenceOrThrow(), this::onAttributeWriteResponse);
+
+ String body = null;
+
+ if (controllerCommand instanceof ControllerCommandBasic) {
+ body = event.getValue().map(v -> {
+ ObjectValue objectValue = Values.createObject();
+ objectValue.put("parameter", v);
+ return objectValue.toString();
+ }).orElse(null);
+ }
+ executeAttributeWriteRequest(request, body, protocolConfiguration.getReferenceOrThrow(), this::onAttributeWriteResponse);
}
/**
@@ -541,11 +552,11 @@ public class ControllerProtocol extends AbstractProtocol {
}
}
- private void executeAttributeWriteRequest(HttpClientProtocol.HttpClientRequest request, AttributeRef protocolRef, Consumer<Response> responseConsumer) {
+ private void executeAttributeWriteRequest(HttpClientProtocol.HttpClientRequest request, String body, AttributeRef protocolRef, Consumer<Response> responseConsumer) {
Response response = null;
try {
- response = request.invoke(null);
+ response = request.invoke(body);
this.updateConnectionStatus(protocolRef, ConnectionStatus.CONNECTED);
} catch (Exception e) {
diff --git a/agent/src/main/java/org/openremote/agent/protocol/controller/RequestBuilder.java b/agent/src/main/java/org/openremote/agent/protocol/controller/RequestBuilder.java
index 8d8721862..9218fe4ee 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/controller/RequestBuilder.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/controller/RequestBuilder.java
@@ -24,15 +24,12 @@ import org.openremote.agent.protocol.controller.command.ControllerCommandBasic;
import org.openremote.agent.protocol.controller.command.ControllerCommandMapped;
import org.openremote.agent.protocol.http.HttpClientProtocol;
import org.openremote.model.attribute.AttributeEvent;
-import org.openremote.model.value.ObjectValue;
-import org.openremote.model.value.Values;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import java.util.List;
import java.util.Map;
-import java.util.Optional;
import java.util.logging.Logger;
/**
@@ -62,18 +59,13 @@ public class RequestBuilder {
public static HttpClientProtocol.HttpClientRequest buildCommandRequest(ControllerCommand controllerCommand, AttributeEvent event, ResteasyWebTarget webTarget) {
MultivaluedMap<String, String> queryParam = new MultivaluedHashMap<>();
- Optional<ObjectValue> body = Optional.empty();
//This part could be put in a Wrapper
if(controllerCommand instanceof ControllerCommandBasic) {
String commandName = ((ControllerCommandBasic) controllerCommand).getCommandName();
queryParam.add("name", commandName);
- body = event.getValue().map(v -> {
- ObjectValue objectValue = Values.createObject();
- objectValue.put("parameter", v);
- return objectValue;
- });
+
} else {
Map<String, String> actionCommandLink = ((ControllerCommandMapped) controllerCommand).getActionCommandLink();
String attributeValue = event.getValue().map(Object::toString).orElse(null);
@@ -90,7 +82,6 @@ public class RequestBuilder {
null,
false,
false,
- body.orElse(null),
MediaType.APPLICATION_JSON
);
}
@@ -109,7 +100,6 @@ public class RequestBuilder {
null,
false,
false,
- null,
MediaType.APPLICATION_JSON
);
}
@@ -128,7 +118,6 @@ public class RequestBuilder {
null,
false,
false,
- null,
MediaType.APPLICATION_JSON
);
}
@@ -143,7 +132,6 @@ public class RequestBuilder {
null,
false,
false,
- null,
null
);
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/http/AbstractHttpServerProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/http/AbstractHttpServerProtocol.java
index 7c7855153..aa29eedcc 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/http/AbstractHttpServerProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/http/AbstractHttpServerProtocol.java
@@ -43,6 +43,7 @@ import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.agent.ProtocolConfiguration;
import org.openremote.model.attribute.AttributeRef;
import org.openremote.model.attribute.MetaItem;
+import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.TextUtil;
import org.openremote.model.value.ArrayValue;
import org.openremote.model.value.StringValue;
@@ -59,6 +60,7 @@ import java.util.regex.Pattern;
import static org.openremote.container.web.WebService.pathStartsWithHandler;
import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
import static org.openremote.model.Constants.REQUEST_HEADER_REALM;
+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
/**
* This is an abstract protocol for creating JAX-RS deployments; a concrete implementation should be created for each
@@ -124,7 +126,7 @@ public abstract class AbstractHttpServerProtocol extends AbstractProtocol {
public static final String DEFAULT_ALLOWED_METHODS = "OPTIONS, GET, POST, DELETE, PUT, PATCH";
public static final String DEFAULT_DEPLOYMENT_NAME_FORMAT = "HttpServer %1$s Deployment %2$d";
protected static final Map<AttributeRef, DeploymentInstance> deployments = new HashMap<>();
- private static final Logger LOG = Logger.getLogger(AbstractHttpServerProtocol.class.getName());
+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, AbstractHttpServerProtocol.class);
protected static WebServiceExceptions.DefaultResteasyExceptionMapper defaultResteasyExceptionMapper;
protected static WebServiceExceptions.ForbiddenResteasyExceptionMapper forbiddenResteasyExceptionMapper;
protected static JacksonConfig jacksonConfig;
diff --git a/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java
index d42cddf54..a862e7a56 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java
@@ -85,7 +85,7 @@ import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_STRING_NON_EMPTY
* {@link Attribute}s that are linked to this protocol using an {@link MetaItemType#AGENT_LINK} {@link MetaItem} support
* the following meta items: <ul> <li>{@link #META_ATTRIBUTE_PATH} (<b>if not supplied then base URI is used</b>)</li>
* <li>{@link #META_ATTRIBUTE_METHOD}</li> <li>{@link Protocol#META_ATTRIBUTE_WRITE_VALUE} (used as the body of the
- * request for writes)</li> <li>{@link #META_POLLING_MILLIS} (<b>required if attribute value should be set by
+ * request for writes)</li> <li>{@link #META_ATTRIBUTE_POLLING_MILLIS} (<b>required if attribute value should be set by
* the response received from this endpoint</b>)</li> <li>{@link #META_QUERY_PARAMETERS}</li>
* <li>{@link #META_FAILURE_CODES}</li>
* <li>{@link #META_HEADERS}</li>
@@ -95,7 +95,7 @@ import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_STRING_NON_EMPTY
* <h1>Response filtering</h1>
* <p>
* Any {@link Attribute} whose value is to be set by the HTTP server response (i.e. it has an {@link
- * #META_POLLING_MILLIS} {@link MetaItem}) can use the standard {@link Protocol#META_ATTRIBUTE_VALUE_FILTERS} in
+ * #META_ATTRIBUTE_POLLING_MILLIS} {@link MetaItem}) can use the standard {@link Protocol#META_ATTRIBUTE_VALUE_FILTERS} in
* order to filter the received HTTP response.
* <p>
* <h1>Connection Status</h1>
@@ -164,14 +164,12 @@ public class HttpClientProtocol extends AbstractProtocol {
public String method;
public MultivaluedMap<String, String> headers;
public MultivaluedMap<String, String> queryParameters;
- public String body;
protected String path;
protected List<Integer> failureCodes;
protected String contentType;
protected WebTarget client;
protected WebTarget requestTarget;
protected boolean dynamicQueryParameters;
- protected boolean dynamicBody;
protected boolean updateConnectionStatus;
protected boolean pagingEnabled;
@@ -183,7 +181,6 @@ public class HttpClientProtocol extends AbstractProtocol {
List<Integer> failureCodes,
boolean updateConnectionStatus,
boolean pagingEnabled,
- Value bodyValue,
String contentType) {
if (!TextUtil.isNullOrEmpty(path)) {
@@ -200,7 +197,7 @@ public class HttpClientProtocol extends AbstractProtocol {
this.failureCodes = failureCodes;
this.updateConnectionStatus = updateConnectionStatus;
this.pagingEnabled = pagingEnabled;
- this.contentType = contentType;
+ this.contentType = contentType != null ? contentType : DEFAULT_CONTENT_TYPE;
dynamicQueryParameters = queryParameters != null
&& queryParameters
.entrySet()
@@ -211,18 +208,6 @@ public class HttpClientProtocol extends AbstractProtocol {
.stream()
.anyMatch(val -> val.contains(DYNAMIC_VALUE_PLACEHOLDER)));
- if (bodyValue != null) {
- if (contentType == null) {
- this.contentType = bodyValue.getType() == ValueType.OBJECT || bodyValue.getType() == ValueType.ARRAY
- ? MediaType.APPLICATION_JSON
- : DEFAULT_CONTENT_TYPE;
- }
- body = bodyValue.toString();
- }
-
- dynamicBody = !TextUtil.isNullOrEmpty(body)
- && body.contains(DYNAMIC_VALUE_PLACEHOLDER);
-
boolean dynamicPath = !TextUtil.isNullOrEmpty(path) && path.contains(DYNAMIC_VALUE_PLACEHOLDER);
if (!dynamicPath) {
requestTarget = createRequestTarget(path);
@@ -243,47 +228,43 @@ public class HttpClientProtocol extends AbstractProtocol {
return requestTarget;
}
- protected Invocation.Builder getRequestBuilder(String dynamicRequestValue) {
+ protected Invocation.Builder getRequestBuilder(String value) {
Invocation.Builder requestBuilder;
if (requestTarget != null) {
requestBuilder = requestTarget.request();
} else {
// This means that the path is dynamic
- String path = this.path.replaceAll(DYNAMIC_VALUE_PLACEHOLDER_REGEXP, dynamicRequestValue);
+ String path = this.path.replaceAll(DYNAMIC_VALUE_PLACEHOLDER_REGEXP, value);
requestBuilder = createRequestTarget(path).request();
}
if (dynamicQueryParameters) {
- requestBuilder.property(QueryParameterInjectorFilter.DYNAMIC_VALUE, dynamicRequestValue);
+ requestBuilder.property(QueryParameterInjectorFilter.DYNAMIC_VALUE, value);
}
return requestBuilder;
}
- protected Invocation buildInvocation(Invocation.Builder requestBuilder, String dynamicRequestValue) {
+ protected Invocation buildInvocation(Invocation.Builder requestBuilder, String value) {
Invocation invocation;
- if (body == null) {
+ if (dynamicQueryParameters) {
+ requestBuilder.property(QueryParameterInjectorFilter.DYNAMIC_VALUE, value);
+ }
+
+ if (value == null) {
invocation = requestBuilder.build(method);
} else {
- String body = this.body;
-
- if (dynamicBody) {
- body = body.replaceAll(DYNAMIC_VALUE_PLACEHOLDER_REGEXP, dynamicRequestValue);
- }
- if (dynamicQueryParameters) {
- requestBuilder.property(QueryParameterInjectorFilter.DYNAMIC_VALUE, dynamicRequestValue);
- }
- invocation = requestBuilder.build(method, Entity.entity(body, contentType));
+ invocation = requestBuilder.build(method, Entity.entity(value, contentType));
}
return invocation;
}
- public Response invoke(String dynamicRequestValue) {
- Invocation.Builder requestBuilder = getRequestBuilder(dynamicRequestValue);
- Invocation invocation = buildInvocation(requestBuilder, dynamicRequestValue);
+ public Response invoke(String value) {
+ Invocation.Builder requestBuilder = getRequestBuilder(value);
+ Invocation invocation = buildInvocation(requestBuilder, value);
return invocation.invoke();
}
@@ -346,7 +327,7 @@ public class HttpClientProtocol extends AbstractProtocol {
public static final String DEFAULT_HTTP_METHOD = HttpMethod.GET;
public static final String DEFAULT_CONTENT_TYPE = MediaType.TEXT_PLAIN;
protected static final String HEADER_LINK = "Link";
- protected static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, HttpClientProtocol.class.getName());
+ protected static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, HttpClientProtocol.class);
protected static int MIN_POLLING_MILLIS = 1000;
protected static int MIN_PING_MILLIS = 10000;
@@ -459,12 +440,8 @@ public class HttpClientProtocol extends AbstractProtocol {
HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE, HttpMethod.OPTIONS);
/**
- * Used to indicate the type of data sent in the body, a default will be used if not supplied; default is determined
- * as follows:
- * <ul>
- * <li>{@link Protocol#META_ATTRIBUTE_WRITE_VALUE} value type = {@link ObjectValue} or {@link ArrayValue}: {@link MediaType#APPLICATION_JSON}</li>
- * <li>{@link Protocol#META_ATTRIBUTE_WRITE_VALUE} value type = any other value type: {@value #DEFAULT_CONTENT_TYPE}</li>
- * </ul>
+ * Used to indicate the type of data sent in the body, a default of {@link #DEFAULT_CONTENT_TYPE} will be used if
+ * not supplied
*/
public static final MetaItemDescriptor META_ATTRIBUTE_CONTENT_TYPE = metaItemString(
PROTOCOL_NAME + ":contentType",
@@ -553,7 +530,7 @@ public class HttpClientProtocol extends AbstractProtocol {
META_ATTRIBUTE_CONTENT_TYPE,
META_ATTRIBUTE_POLLING_ATTRIBUTE,
META_HEADERS,
- META_POLLING_MILLIS,
+ META_ATTRIBUTE_POLLING_MILLIS,
META_QUERY_PARAMETERS,
META_FAILURE_CODES,
META_PAGING_ENABLED,
@@ -679,11 +656,6 @@ public class HttpClientProtocol extends AbstractProtocol {
protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
final AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
- if (!protocolConfiguration.isEnabled()) {
- updateStatus(protocolRef, ConnectionStatus.DISABLED);
- return;
- }
-
String baseUri = protocolConfiguration.getMetaItem(META_PROTOCOL_BASE_URI)
.flatMap(AbstractValueHolder::getValueAsString).orElseThrow(() ->
new IllegalArgumentException("Missing or invalid require meta item: " + META_PROTOCOL_BASE_URI));
@@ -836,7 +808,6 @@ public class HttpClientProtocol extends AbstractProtocol {
null,
true,
false,
- pingBody,
contentType);
LOG.info("Creating ping polling request '" + pingRequest + "'");
@@ -846,6 +817,7 @@ public class HttpClientProtocol extends AbstractProtocol {
null,
protocolRef,
pingRequest,
+ pingBody != null ? pingBody.toString() : null,
pingPollingMillis));
}
@@ -885,11 +857,6 @@ public class HttpClientProtocol extends AbstractProtocol {
.flatMap(Values::getString)
.orElse(null);
- Value body = attribute
- .getMetaItem(META_ATTRIBUTE_WRITE_VALUE)
- .flatMap(AbstractValueHolder::getValue)
- .orElse(null);
-
List<Integer> failureCodes = attribute.getMetaItem(META_FAILURE_CODES)
.flatMap(AbstractValueHolder::getValueAsArray)
.flatMap(arrayValue ->
@@ -921,7 +888,7 @@ public class HttpClientProtocol extends AbstractProtocol {
Optional<Integer> pollingMillis = Values.getMetaItemValueOrThrow(
attribute,
- META_POLLING_MILLIS,
+ META_ATTRIBUTE_POLLING_MILLIS,
false,
true)
.map(polling ->
@@ -962,30 +929,29 @@ public class HttpClientProtocol extends AbstractProtocol {
}
addHttpClientRequest(protocolConfiguration,
- attributeRef,
+ attribute,
path,
method,
headers,
queryParams,
failureCodes,
pagingEnabled,
- body,
contentType,
pollingMillis.orElse(null));
}
protected void addHttpClientRequest(AssetAttribute protocolConfiguration,
- AttributeRef attributeRef,
+ AssetAttribute attribute,
String path,
String method,
MultivaluedMap<String, String> headers,
MultivaluedMap<String, String> queryParams,
List<Integer> failureCodes,
boolean pagingEnabled,
- Value body,
String contentType,
Integer pollingMillis) {
+ AttributeRef attributeRef = attribute.getReferenceOrThrow();
AttributeRef protocolConfigurationRef = protocolConfiguration.getReferenceOrThrow();
Pair<ResteasyWebTarget, List<Integer>> clientAndFailureCodes = clientMap.get(protocolConfigurationRef);
ResteasyWebTarget client = clientAndFailureCodes != null ? clientAndFailureCodes.key : null;
@@ -1020,18 +986,23 @@ public class HttpClientProtocol extends AbstractProtocol {
failureCodes,
updateConnectionStatus,
pagingEnabled,
- body,
contentType);
LOG.fine("Creating HTTP request for attributeRef '" + clientRequest + "': " + attributeRef);
requestMap.put(attributeRef, clientRequest);
- Optional.ofNullable(pollingMillis).ifPresent(seconds -> pollingMap.put(attributeRef, schedulePollingRequest(
+ Optional.ofNullable(pollingMillis).ifPresent(seconds -> {
+ String body = Values.getMetaItemValueOrThrow(attribute, META_ATTRIBUTE_WRITE_VALUE, false, true)
+ .map(Object::toString).orElse(null);
+
+ pollingMap.put(attributeRef, schedulePollingRequest(
attributeRef,
protocolConfigurationRef,
clientRequest,
- seconds)));
+ body,
+ seconds));
+ });
}
@Override
@@ -1057,7 +1028,7 @@ public class HttpClientProtocol extends AbstractProtocol {
}
@Override
- protected void processLinkedAttributeWrite(AttributeEvent event, AssetAttribute protocolConfiguration) {
+ protected void processLinkedAttributeWrite(AttributeEvent event, Value processedValue, AssetAttribute protocolConfiguration) {
AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
HttpClientRequest request = requestMap.get(event.getAttributeRef());
Pair<ResteasyWebTarget, List<Integer>> clientAndFailureCodes = clientMap.get(protocolRef);
@@ -1067,7 +1038,7 @@ public class HttpClientProtocol extends AbstractProtocol {
if (request != null && clientAndFailureCodes != null) {
executeAttributeWriteRequest(request,
- event.getValue().orElse(null),
+ processedValue,
response ->
onAttributeWriteResponse(
request,
@@ -1129,7 +1100,7 @@ public class HttpClientProtocol extends AbstractProtocol {
return result;
}
- protected HttpClientRequest buildClientRequest(WebTarget client, String path, String method, MultivaluedMap<String, String> headers, MultivaluedMap<String, String> queryParams, List<Integer> failureCodes, boolean updateConnectionStatus, boolean pagingEnabled, Value body, String contentType) {
+ protected HttpClientRequest buildClientRequest(WebTarget client, String path, String method, MultivaluedMap<String, String> headers, MultivaluedMap<String, String> queryParams, List<Integer> failureCodes, boolean updateConnectionStatus, boolean pagingEnabled, String contentType) {
return new HttpClientRequest(
client,
path,
@@ -1139,19 +1110,19 @@ public class HttpClientProtocol extends AbstractProtocol {
failureCodes,
updateConnectionStatus,
pagingEnabled,
- body,
contentType);
}
protected ScheduledFuture schedulePollingRequest(AttributeRef attributeRef,
AttributeRef protocolConfigurationRef,
HttpClientRequest clientRequest,
+ String body,
int pollingMillis) {
LOG.fine("Scheduling polling request '" + clientRequest + "' to execute every " + pollingMillis + " ms for attribute: " + attributeRef);
return executorService.scheduleWithFixedDelay(() ->
- executePollingRequest(clientRequest, response ->
+ executePollingRequest(clientRequest, body, response ->
onPollingResponse(
clientRequest,
response,
@@ -1160,12 +1131,12 @@ public class HttpClientProtocol extends AbstractProtocol {
), 0, pollingMillis, TimeUnit.MILLISECONDS);
}
- protected void executePollingRequest(HttpClientRequest clientRequest, Consumer<Response> responseConsumer) {
+ protected void executePollingRequest(HttpClientRequest clientRequest, String body, Consumer<Response> responseConsumer) {
Response originalResponse = null, lastResponse;
List<String> entities = new ArrayList<>();
try {
- originalResponse = clientRequest.invoke(null);
+ originalResponse = clientRequest.invoke(body);
if (clientRequest.pagingEnabled) {
lastResponse = originalResponse;
entities.add(lastResponse.readEntity(String.class));
diff --git a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIoClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIoClientProtocol.java
new file mode 100644
index 000000000..74e412075
--- /dev/null
+++ b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIoClientProtocol.java
@@ -0,0 +1,321 @@
+/*
+ * 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.agent.protocol.io;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.ChannelHandler;
+import io.netty.channel.ChannelInboundHandler;
+import io.netty.channel.ChannelOutboundHandler;
+import io.netty.handler.codec.DelimiterBasedFrameDecoder;
+import io.netty.handler.codec.FixedLengthFrameDecoder;
+import io.netty.handler.codec.string.StringDecoder;
+import io.netty.handler.codec.string.StringEncoder;
+import io.netty.util.CharsetUtil;
+import org.openremote.agent.protocol.AbstractProtocol;
+import org.openremote.agent.protocol.Protocol;
+import org.openremote.container.Container;
+import org.openremote.model.asset.AssetAttribute;
+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.attribute.MetaItemDescriptor;
+import org.openremote.model.syslog.SyslogCategory;
+import org.openremote.model.util.Pair;
+import org.openremote.model.value.Value;
+import org.openremote.model.value.Values;
+
+import java.net.SocketAddress;
+import java.nio.charset.Charset;
+import java.util.*;
+import java.util.function.BiConsumer;
+import java.util.function.Supplier;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+
+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
+
+/**
+ * This is an abstract {@link org.openremote.agent.protocol.Protocol} for protocols that require an {@link IoClient}.
+ */
+public abstract class AbstractIoClientProtocol<T, U extends IoClient<T>> extends AbstractProtocol {
+
+ /**
+ * List of protocol {@link MetaItem}s that are used by generic (string based) IO client protocols
+ */
+ public static final List<MetaItemDescriptor> PROTOCOL_GENERIC_META_ITEM_DESCRIPTORS = Arrays.asList(
+ META_PROTOCOL_CHARSET,
+ META_PROTOCOL_MAX_LENGTH,
+ META_PROTOCOL_DELIMITER,
+ META_PROTOCOL_STRIP_DELIMITER,
+ META_PROTOCOL_CONVERT_BINARY,
+ META_PROTOCOL_CONVERT_HEX
+ );
+
+ /**
+ * Supplies decoders that convert from ByteBuf to
+ */
+ public static Supplier<ChannelHandler[]> getGenericStringEncodersAndDecoders(AbstractNettyIoClient<String, ?> client, AssetAttribute protocolConfiguration) {
+
+ boolean hexMode = Values.getMetaItemValueOrThrow(
+ protocolConfiguration,
+ META_PROTOCOL_CONVERT_HEX,
+ false,
+ false
+ ).flatMap(Values::getBoolean).orElse(false);
+
+ boolean binaryMode = Values.getMetaItemValueOrThrow(
+ protocolConfiguration,
+ META_PROTOCOL_CONVERT_BINARY,
+ false,
+ false
+ ).flatMap(Values::getBoolean).orElse(false);
+
+ Charset charset = Values.getMetaItemValueOrThrow(
+ protocolConfiguration,
+ META_PROTOCOL_CHARSET,
+ false,
+ true
+ ).flatMap(Values::getString).map(Charset::forName).orElse(CharsetUtil.UTF_8);
+
+ int maxLength = Values.getMetaItemValueOrThrow(
+ protocolConfiguration,
+ META_PROTOCOL_MAX_LENGTH,
+ false,
+ false
+ ).flatMap(Values::getIntegerCoerced).orElse(Integer.MAX_VALUE);
+
+ List<String> delimiters = Arrays.stream(protocolConfiguration.getMetaItems(META_PROTOCOL_DELIMITER.getUrn()))
+ .map(metaItem -> metaItem.getValueAsString().orElse(null))
+ .filter(str -> !Objects.isNull(str))
+ .collect(Collectors.toList());
+
+ boolean stripDelimiter = Values.getMetaItemValueOrThrow(
+ protocolConfiguration,
+ META_PROTOCOL_STRIP_DELIMITER,
+ false,
+ false
+ ).flatMap(Values::getBoolean).orElse(false);
+
+ Supplier<ChannelHandler[]> encoderDecoderProvider = () -> {
+ List<ChannelHandler> encodersDecoders = new ArrayList<>();
+
+ if (hexMode || binaryMode) {
+ encodersDecoders.add(
+ new AbstractNettyIoClient.MessageToByteEncoder<>(
+ String.class,
+ client,
+ (msg, out) -> {
+ byte[] bytes = hexMode ? Protocol.bytesFromHexString(msg) : Protocol.bytesFromBinaryString(msg);
+ out.writeBytes(bytes);
+ }
+ ));
+
+ if (!delimiters.isEmpty()) {
+ ByteBuf[] byteDelimiters = delimiters
+ .stream()
+ .map(delim -> Unpooled.wrappedBuffer(hexMode ? Protocol.bytesFromHexString(delim) : Protocol.bytesFromBinaryString(delim)))
+ .toArray(ByteBuf[]::new);
+ encodersDecoders.add(new DelimiterBasedFrameDecoder(maxLength, stripDelimiter, byteDelimiters));
+ } else {
+ encodersDecoders.add(new FixedLengthFrameDecoder(maxLength));
+ }
+
+ // Incoming messages will be bytes
+ encodersDecoders.add(
+ new AbstractNettyIoClient.ByteToMessageDecoder<>(
+ client,
+ (byteBuf, messages) -> {
+ byte[] bytes = new byte[byteBuf.readableBytes()];
+ byteBuf.readBytes(bytes);
+ String msg = hexMode ? Protocol.bytesToHexString(bytes) : Protocol.bytesToBinaryString(bytes);
+ messages.add(msg);
+ }
+ )
+ );
+ } else {
+ encodersDecoders.add(new StringEncoder(charset));
+ if (!delimiters.isEmpty()) {
+ ByteBuf[] byteDelimiters = delimiters
+ .stream()
+ .map(delim -> Unpooled.wrappedBuffer(delim.getBytes(charset)))
+ .toArray(ByteBuf[]::new);
+ encodersDecoders.add(new DelimiterBasedFrameDecoder(maxLength, stripDelimiter, byteDelimiters));
+ } else {
+ encodersDecoders.add(new FixedLengthFrameDecoder(maxLength));
+ }
+ encodersDecoders.add(new StringDecoder(charset));
+ encodersDecoders.add(new AbstractNettyIoClient.MessageToMessageDecoder<>(String.class, client));
+ }
+
+ return encodersDecoders.toArray(new ChannelHandler[0]);
+ };
+
+ return encoderDecoderProvider;
+ }
+
+ public static <T, U extends SocketAddress> Pair<ChannelOutboundHandler[], ChannelInboundHandler[]> createBasicEncoderAndDecoder(Class<T> typeClazz, AbstractNettyIoClient<T, U> client, BiConsumer<T, ByteBuf> encoder, BiConsumer<ByteBuf, List<T>> decoder) {
+ return new Pair<>(
+ new ChannelOutboundHandler[] {
+ new AbstractNettyIoClient.MessageToByteEncoder<>(typeClazz, client, encoder)
+ },
+ new ChannelInboundHandler[] {
+ new AbstractNettyIoClient.ByteToMessageDecoder<>(client, decoder)
+ }
+ );
+ }
+
+ public static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, AbstractIoClientProtocol.class);
+ protected final Map<AttributeRef, ProtocolIoClient<T, U>> protocolIoClientMap = new HashMap<>();
+
+ public class ProtocolIoClient<T, U extends IoClient<T>> {
+ public AttributeRef protocolRef;
+ public U client;
+ public BiConsumer<AttributeRef, ConnectionStatus> connectionStatusConsumer;
+ public BiConsumer<AttributeRef, T> messageConsumer;
+
+ public ProtocolIoClient(AttributeRef protocolRef, U client, BiConsumer<AttributeRef, ConnectionStatus> connectionStatusConsumer, BiConsumer<AttributeRef, T> messageConsumer) {
+ this.protocolRef = protocolRef;
+ this.client = client;
+ this.connectionStatusConsumer = connectionStatusConsumer;
+ this.messageConsumer = messageConsumer;
+ }
+
+ public void connect() {
+ client.addConnectionStatusConsumer(status -> {
+ if (connectionStatusConsumer != null) {
+ connectionStatusConsumer.accept(protocolRef, status);
+ }
+ });
+
+ client.addMessageConsumer(msg -> {
+ if (messageConsumer != null) {
+ messageConsumer.accept(protocolRef, msg);
+ }
+ });
+
+ LOG.info("Connecting IO client");
+ client.connect();
+ }
+
+ protected void disconnect() {
+ client.removeAllMessageConsumers();
+ client.removeAllConnectionStatusConsumers();
+ LOG.info("Disconnecting IO client");
+ client.disconnect();
+ }
+
+ protected synchronized void send(T message) {
+ LOG.fine("Sending message to IO client: " + client.getClientUri());
+ client.sendMessage(message);
+ }
+ }
+
+ @Override
+ protected void doStop(Container container) throws Exception {
+ super.doStop(container);
+ protocolIoClientMap.forEach((ref, client) -> client.disconnect());
+ }
+
+ @Override
+ protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ final AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
+
+ ProtocolIoClient<T, U> protocolIoClient = null;
+
+ try {
+ protocolIoClient = createProtocolClient(protocolConfiguration);
+ LOG.fine("Created IO client '" + protocolIoClient.client.getClientUri() + "' for protocol configuration: " + protocolRef);
+ protocolIoClient.connect();
+ } catch (Exception e) {
+ LOG.log(Level.SEVERE, "Failed to create IO client for protocol Configuration: " + protocolConfiguration, e);
+ updateStatus(protocolConfiguration.getReferenceOrThrow(), ConnectionStatus.ERROR);
+ } finally {
+ if (protocolIoClient != null) {
+ protocolIoClientMap.put(protocolRef, protocolIoClient);
+ }
+ }
+ }
+
+ @Override
+ protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
+ ProtocolIoClient<T, U> protocolIoClient = protocolIoClientMap.remove(protocolRef);
+
+ if (protocolIoClient != null) {
+ LOG.fine("Removed IO client '" + protocolIoClient.client.getClientUri() + "' for protocol configuration: " + protocolRef);
+ protocolIoClient.disconnect();
+ }
+ }
+
+ @Override
+ protected void processLinkedAttributeWrite(AttributeEvent event, Value processedValue, AssetAttribute protocolConfiguration) {
+ ProtocolIoClient<T, U> protocolIoClient = protocolIoClientMap.get(protocolConfiguration.getReferenceOrThrow());
+ AssetAttribute attribute = getLinkedAttribute(event.getAttributeRef());
+
+ if (protocolIoClient == null || attribute == null) {
+ return;
+ }
+
+ T message = createWriteMessage(protocolConfiguration, attribute, event, processedValue);
+
+ if (message == null) {
+ LOG.fine("No message produced for attribute event so not sending to IO client '" + protocolIoClient.client.getClientUri() + "': " + event);
+ return;
+ }
+
+ protocolIoClient.send(message);
+ }
+
+ protected ProtocolIoClient<T, U> createProtocolClient(AssetAttribute protocolConfiguration) throws Exception {
+ final AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
+ U client = createIoClient(protocolConfiguration);
+ Supplier<ChannelHandler[]> encoderDecoderProvider = getEncoderDecoderProvider(client, protocolConfiguration);
+ client.setEncoderDecoderProvider(encoderDecoderProvider);
+ return new ProtocolIoClient<>(protocolRef, client, this::onConnectionStatusChanged, this::onMessageReceived);
+ }
+
+ /**
+ * Called when the {@link IoClient} {@link ConnectionStatus} changes
+ */
+ protected void onConnectionStatusChanged(AttributeRef protocolRef, ConnectionStatus connectionStatus) {
+ updateStatus(protocolRef, connectionStatus);
+ }
+
+ /**
+ * Should return an instance of {@link IoClient} for the supplied protocolConfiguration; the configuration of
+ * encoders/decoders is handled by the separate call to {@link #getEncoderDecoderProvider}
+ */
+ protected abstract U createIoClient(AssetAttribute protocolConfiguration) throws Exception;
+
+ protected abstract Supplier<ChannelHandler[]> getEncoderDecoderProvider(U client, AssetAttribute protocolConfiguration);
+
+ /**
+ * Called when the {@link IoClient} receives a message from the server
+ */
+ protected abstract void onMessageReceived(AttributeRef protocolRef, T message);
+
+ /**
+ * Generate the actual message to send to the {@link IoClient} for this {@link AttributeEvent}
+ */
+ protected abstract T createWriteMessage(AssetAttribute protocolConfiguration, AssetAttribute attribute, AttributeEvent event, Value processedValue);
+}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIoClient.java b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIoClient.java
index eea46c338..fe45a27ad 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIoClient.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIoClient.java
@@ -22,17 +22,19 @@ package org.openremote.agent.protocol.io;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
-import io.netty.handler.codec.ByteToMessageDecoder;
-import io.netty.handler.codec.MessageToByteEncoder;
import org.openremote.agent.protocol.ProtocolExecutorService;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.syslog.SyslogCategory;
+import javax.validation.constraints.NotNull;
import java.net.SocketAddress;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ScheduledFuture;
+import java.util.function.BiConsumer;
import java.util.function.Consumer;
+import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -43,32 +45,54 @@ import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
* <p>
* It uses the netty component for managing the connection.
* <p>
- * By default it uses a single {@link ByteToMessageDecoder} to intercept incoming data and to delegate the construction
- * of messages to the abstract {@link #decode} method.
+ * Concrete implementations are responsible for providing the {@link ChannelOutboundHandler}s and
+ * {@link ChannelInboundHandler}s required to encode/decode the specific type of messages sent/received to/from this
+ * client. For {@link IoClient}s that require some specific encoders/decoders irrespective of the message type (e.g.
+ * {@link org.openremote.agent.protocol.udp.UdpIoClient} and {@link org.openremote.agent.protocol.websocket.WebsocketIoClient})
+ * the {@link #addEncodersDecoders} method can be overridden so the {@link IoClient} can exactly control
+ * the order and types of encoders/decoders in the pipeline.
* <p>
- * For outgoing messages it uses a single
- * {@link MessageToByteEncoder} to delegate the filling of the {@link ByteBuf} to the abstract {@link #encode} method.
+ * Users of the {@link IoClient} can add encoders/decoders for their specific message type using the
+ * {@link #setEncoderDecoderProvider}, each {@link IoClient} should make it clear to users what the required output
+ * type(s) are for the last encoder and decoder that a user may wish to add, if adding encoders/decoders is not
+ * supported then {@link IoClient}s should override this setter and throw an {@link UnsupportedOperationException}.
* <p>
- * Consumers wanting to add and/or replace the default encoder/decoder should override {@link #initChannel} and insert
- * the desired {@link ChannelHandler}s into the pipeline.
+ * Typically for outgoing messages a single {@link ChannelOutboundHandler} is sufficient and the
+ * {@link MessageToByteEncoder} can be used as a base.
+ * <p>
+ * For inbound messages; the decoders required are very much dependent on the message type and {@link IoClient} type,
+ * any number of standard netty {@link ChannelInboundHandler}s can be used but the last handler should build messages
+ * of type &lt;T&gt; and pass them to the {@link #onMessageReceived} method of the client; the {@link ByteToMessageDecoder}
+ * or {@link MessageToByteEncoder} can be used for this purpose, which one to use will depend on the previous
+ * {@link ChannelInboundHandler}s in the pipeline.
* <p>
* <b>NOTE: Care must be taken when working with Netty {@link ByteBuf} as Netty uses reference counting to manage their
* lifecycle. Refer to the Netty documentation for more information.</b>
*/
public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implements IoClient<T> {
- public class MessageDecoder extends ByteToMessageDecoder {
+ /**
+ * This is intended to be used at the end of a decoder chain where the previous decoder outputs a {@link ByteBuf};
+ * the provided {@link #decoder} should extract the messages of type &lt;T&gt; from the {@link ByteBuf} and add them
+ * to the {@link List} and they will then be passed to the {@link IoClient}.
+ */
+ public static class ByteToMessageDecoder<T> extends io.netty.handler.codec.ByteToMessageDecoder {
protected List<T> messages = new ArrayList<>(1);
+ protected AbstractNettyIoClient<T, ?> client;
+ protected BiConsumer<ByteBuf, List<T>> decoder;
+
+ public ByteToMessageDecoder(AbstractNettyIoClient<T, ?> client, @NotNull BiConsumer<ByteBuf, List<T>> decoder) {
+ this.client = client;
+ this.decoder = decoder;
+ }
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
- AbstractNettyIoClient.this.decode(in, messages);
+ decoder.accept(in, messages);
if (!messages.isEmpty()) {
- @SuppressWarnings("unchecked")
- U address = (U)ctx.channel().remoteAddress();
// Don't pass them along the channel pipeline just consume them
- messages.forEach(m -> AbstractNettyIoClient.this.onMessageReceived(address, m));
+ messages.forEach(m -> client.onMessageReceived(m));
messages.clear();
}
}
@@ -76,26 +100,56 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
- AbstractNettyIoClient.this.onDecodeException(ctx, cause);
+ client.onDecodeException(ctx, cause);
}
}
- public class MessageEncoder extends MessageToByteEncoder<T> {
+ /**
+ * This is intended to be used at the end of a decoder chain where the previous decoder outputs messages of type &lt;T&gt;.
+ */
+ public static class MessageToMessageDecoder<T> extends SimpleChannelInboundHandler<T> {
+ protected AbstractNettyIoClient<T,?> client;
+
+ public MessageToMessageDecoder(Class<? extends T> typeClazz, AbstractNettyIoClient<T, ?> client) {
+ super(typeClazz);
+ this.client = client;
+ }
+
@Override
- public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception {
- super.connect(ctx, remoteAddress, localAddress, promise);
+ public void channelRead0(ChannelHandlerContext ctx, T msg) throws Exception {
+ client.onMessageReceived(msg);
+ }
+
+ @Override
+ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
+ super.exceptionCaught(ctx, cause);
+ client.onDecodeException(ctx, cause);
+ }
+ }
+
+ /**
+ * Concrete implementations must provide an encoder to fill the {@link ByteBuf} ready to be sent `over the wire`.
+ */
+ public static class MessageToByteEncoder<T> extends io.netty.handler.codec.MessageToByteEncoder<T> {
+ protected AbstractNettyIoClient<T, ?> client;
+ protected BiConsumer<T, ByteBuf> encoder;
+
+ public MessageToByteEncoder(Class<? extends T> typeClazz, AbstractNettyIoClient<T, ?> client, BiConsumer<T, ByteBuf> encoder) {
+ super(typeClazz);
+ this.client = client;
+ this.encoder = encoder;
}
@Override
protected void encode(ChannelHandlerContext ctx, T msg, ByteBuf out) {
- AbstractNettyIoClient.this.encode(msg, out);
+ encoder.accept(msg, out);
}
@SuppressWarnings("deprecation")
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
- AbstractNettyIoClient.this.onEncodeException(ctx, cause);
+ client.onEncodeException(ctx, cause);
}
}
@@ -114,14 +168,18 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen
protected ScheduledFuture reconnectTask;
protected int reconnectDelayMilliseconds = INITIAL_RECONNECT_DELAY_MILLIS;
protected boolean permanentError;
+ protected Supplier<ChannelHandler[]> encoderDecoderProvider;
- public AbstractNettyIoClient(ProtocolExecutorService executorService) {
+ protected AbstractNettyIoClient(ProtocolExecutorService executorService) {
this.executorService = executorService;
}
- protected abstract Class<? extends Channel> getChannelClass();
+ @Override
+ public void setEncoderDecoderProvider(Supplier<ChannelHandler[]> encodersProvider) throws UnsupportedOperationException {
+ this.encoderDecoderProvider = encodersProvider;
+ }
- protected abstract String getSocketAddressString();
+ protected abstract Class<? extends Channel> getChannelClass();
protected abstract EventLoopGroup getWorkerGroup();
@@ -139,11 +197,11 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen
}
if (connectionStatus != ConnectionStatus.DISCONNECTED && connectionStatus != ConnectionStatus.WAITING) {
- LOG.finest("Must be disconnected before calling connect: " + getSocketAddressString());
+ LOG.finest("Must be disconnected before calling connect: " + getClientUri());
return;
}
- LOG.fine("Connecting IO Client: " + getSocketAddressString());
+ LOG.fine("Connecting IO Client: " + getClientUri());
onConnectionStatusChanged(ConnectionStatus.CONNECTING);
if (workerGroup == null) {
@@ -179,11 +237,11 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen
}
if (future.isSuccess()) {
- LOG.log(Level.INFO, "Connection initialising: " + getSocketAddressString());
+ LOG.log(Level.INFO, "Connection initialising: " + getClientUri());
reconnectTask = null;
reconnectDelayMilliseconds = INITIAL_RECONNECT_DELAY_MILLIS;
} else if (future.cause() != null) {
- LOG.log(Level.WARNING, "Connection error: " + getSocketAddressString(), future.cause());
+ LOG.log(Level.WARNING, "Connection error: " + getClientUri(), future.cause());
// Failed to connect so schedule reconnection attempt
scheduleReconnect();
}
@@ -202,11 +260,11 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen
@Override
public synchronized void disconnect() {
if ((permanentError && connectionStatus == ConnectionStatus.ERROR) || connectionStatus == ConnectionStatus.DISCONNECTING || connectionStatus == ConnectionStatus.DISCONNECTED) {
- LOG.finest("Already disconnecting or disconnected: " + getSocketAddressString());
+ LOG.finest("Already disconnecting or disconnected: " + getClientUri());
return;
}
- LOG.finest("Disconnecting IO client: " + getSocketAddressString());
+ LOG.finest("Disconnecting IO client: " + getClientUri());
onConnectionStatusChanged(permanentError ? ConnectionStatus.ERROR : ConnectionStatus.DISCONNECTING);
try {
@@ -238,16 +296,16 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen
@Override
public void sendMessage(T message) {
if (connectionStatus != ConnectionStatus.CONNECTED) {
- LOG.warning("Cannot send message: Status = " + connectionStatus + ": " + getSocketAddressString());
+ LOG.warning("Cannot send message: Status = " + connectionStatus + ": " + getClientUri());
return;
}
try {
// Don't block here as it can cause deadlock
channel.writeAndFlush(message);
- LOG.finest("Message sent to server: " + getSocketAddressString());
+ LOG.finest("Message sent to server: " + getClientUri());
} catch (Exception e) {
- LOG.log(Level.WARNING, "Message send failed: " + getSocketAddressString(), e);
+ LOG.log(Level.WARNING, "Message send failed: " + getClientUri(), e);
}
}
@@ -304,7 +362,7 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
synchronized (AbstractNettyIoClient.this) {
- LOG.fine("Connected: " + getSocketAddressString());
+ LOG.fine("Connected: " + getClientUri());
onConnectionStatusChanged(ConnectionStatus.CONNECTED);
}
super.channelActive(ctx);
@@ -318,40 +376,42 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen
return;
}
- LOG.fine("Disconnected: " + getSocketAddressString());
+ LOG.fine("Disconnected: " + getClientUri());
onConnectionStatusChanged(ConnectionStatus.DISCONNECTED);
}
super.channelInactive(ctx);
}
});
- addDecoders(channel);
- addEncoders(channel);
- }
-
- protected void addDecoders(Channel channel) {
- channel.pipeline().addLast(new MessageDecoder());
+ addEncodersDecoders(channel);
}
- protected void addEncoders(Channel channel) {
- channel.pipeline().addLast(new MessageEncoder());
+ protected void addEncodersDecoders(Channel channel) {
+ if (encoderDecoderProvider != null) {
+ ChannelHandler[] handlers = encoderDecoderProvider.get();
+ if (handlers != null) {
+ Arrays.stream(handlers).forEach(
+ handler -> channel.pipeline().addLast(handler)
+ );
+ }
+ }
}
- protected void onMessageReceived(U address, T message) {
+ protected void onMessageReceived(T message) {
if (connectionStatus != ConnectionStatus.CONNECTED) {
return;
}
- LOG.finest("Message received notifying consumers: " + getSocketAddressString());
+ LOG.finest("Message received notifying consumers: " + getClientUri());
messageConsumers.forEach(consumer -> consumer.accept(message));
}
protected void onDecodeException(ChannelHandlerContext ctx, Throwable cause) {
- LOG.log(Level.SEVERE, "Exception occurred on in-bound message: " + getSocketAddressString(), cause);
+ LOG.log(Level.SEVERE, "Exception occurred on in-bound message: " + getClientUri(), cause);
onConnectionStatusChanged(ConnectionStatus.ERROR);
}
protected void onEncodeException(ChannelHandlerContext ctx, Throwable cause) {
- LOG.log(Level.SEVERE, "Exception occurred on out-bound message: " + getSocketAddressString(), cause);
+ LOG.log(Level.SEVERE, "Exception occurred on out-bound message: " + getClientUri(), cause);
onConnectionStatusChanged(ConnectionStatus.ERROR);
}
@@ -377,7 +437,7 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen
reconnectDelayMilliseconds = Math.min(MAX_RECONNECT_DELAY_MILLIS, reconnectDelayMilliseconds);
}
- LOG.finest("Scheduling reconnection in '" + reconnectDelayMilliseconds + "' milliseconds: " + getSocketAddressString());
+ LOG.finest("Scheduling reconnection in '" + reconnectDelayMilliseconds + "' milliseconds: " + getClientUri());
reconnectTask = executorService.schedule(() -> {
synchronized (AbstractNettyIoClient.this) {
@@ -391,30 +451,18 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen
}, reconnectDelayMilliseconds);
}
-
- /**
- * Implementations of this message processor need to implement this method in order
- * to decode incoming data.
- * <p>
- * When one or more messages are available in the {@link ByteBuf} then the messages
- * should be constructed and added to the messages list
- */
- protected abstract void decode(ByteBuf buf, List<T> messages);
-
- protected abstract void encode(T message, ByteBuf buf);
-
protected synchronized void setPermanentError(String message) {
if (permanentError) {
return;
}
- LOG.info("An unrecoverable error has occurred with client '" + getSocketAddressString() + "' is no longer usable: " + message);
+ LOG.info("An unrecoverable error has occurred with client '" + getClientUri() + "' is no longer usable: " + message);
this.permanentError = true;
disconnect();
}
@Override
public String toString() {
- return getSocketAddressString();
+ return getClientUri();
}
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/io/IoClient.java b/agent/src/main/java/org/openremote/agent/protocol/io/IoClient.java
index cf7a319ff..2795ca373 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/io/IoClient.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/io/IoClient.java
@@ -19,15 +19,17 @@
*/
package org.openremote.agent.protocol.io;
+import io.netty.channel.ChannelHandler;
+import io.netty.channel.ChannelInboundHandler;
+import io.netty.channel.ChannelOutboundHandler;
import org.openremote.model.asset.agent.ConnectionStatus;
import java.util.function.Consumer;
+import java.util.function.Supplier;
/**
- * Represents an IO client that communicates with a server.
- * <p>
- * Implementors are responsible for handling the low level communication and for providing consumers with a mechanism
- * for encoding/decoding messages of type &lt;T&gt.
+ * Represents an IO client that communicates with a server; this is heavily tied to netty and uses the concept of
+ * {@link io.netty.channel.ChannelHandler}s for encoding/decoding messages of type &lt;T&gt.
*
* @param <T> Defines the message type that the instance will encode/decode
*/
@@ -82,4 +84,15 @@ public interface IoClient<T> {
* Disconnect from the device
*/
void disconnect();
+
+ /**
+ * Should return a URI that uniquely identifies this client instance
+ */
+ String getClientUri();
+
+ /**
+ * Allows appropriate encoders and decoders to be added to the message pipeline; if an {@link IoClient} doesn't
+ * support this then an {@link UnsupportedOperationException} will be thrown, consult the {@link IoClient}'s documentation.
+ */
+ void setEncoderDecoderProvider(Supplier<ChannelHandler[]> encoderDecoderProvider) throws UnsupportedOperationException;
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/knx/EtsFileUriResolver.java b/agent/src/main/java/org/openremote/agent/protocol/knx/EtsFileUriResolver.java
index 6f393232c..745663ab8 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/knx/EtsFileUriResolver.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/knx/EtsFileUriResolver.java
@@ -1,5 +1,7 @@
package org.openremote.agent.protocol.knx;
+import org.openremote.model.syslog.SyslogCategory;
+
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@@ -18,9 +20,11 @@ import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.stream.StreamSource;
+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
+
public class EtsFileUriResolver implements URIResolver {
- private static final Logger LOG = Logger.getLogger(KNXProtocol.class.getName());
+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, KNXProtocol.class);
private byte[] data;
diff --git a/agent/src/main/java/org/openremote/agent/protocol/knx/KNXConnection.java b/agent/src/main/java/org/openremote/agent/protocol/knx/KNXConnection.java
index 9ac15b34c..67789b491 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/knx/KNXConnection.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/knx/KNXConnection.java
@@ -3,6 +3,7 @@ package org.openremote.agent.protocol.knx;
import org.apache.commons.lang3.StringUtils;
import org.openremote.agent.protocol.ProtocolExecutorService;
import org.openremote.model.asset.agent.ConnectionStatus;
+import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.Pair;
import org.openremote.model.value.Value;
import tuwien.auto.calimero.*;
@@ -27,6 +28,8 @@ import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
+
public class KNXConnection implements NetworkLinkListener, ProcessListener {
protected ConnectionStatus connectionStatus = ConnectionStatus.DISCONNECTED;
@@ -57,7 +60,7 @@ public class KNXConnection implements NetworkLinkListener, ProcessListener {
private String localIp;
- private static final Logger LOG = Logger.getLogger(KNXConnection.class.getName());
+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, KNXConnection.class);
public KNXConnection(String gatewayIp, String connectionType, ProtocolExecutorService executorService, String localIp, Integer remotePort, Boolean useNat, String localKNXAddress) {
this.gatewayIp = gatewayIp;
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 75f521f3f..27833d9b3 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
@@ -13,6 +13,7 @@ import org.openremote.model.asset.agent.AgentLink;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.attribute.*;
import org.openremote.model.file.FileInfo;
+import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.Pair;
import org.openremote.model.value.Value;
import org.openremote.model.value.ValueType;
@@ -43,6 +44,7 @@ import java.util.zip.ZipInputStream;
import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
import static org.openremote.model.attribute.MetaItem.isMetaNameEqualTo;
+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_INTEGER_POSITIVE_NON_ZERO;
import static org.openremote.model.util.TextUtil.isNullOrEmpty;
@@ -51,7 +53,7 @@ import static org.openremote.model.util.TextUtil.isNullOrEmpty;
*/
public class KNXProtocol extends AbstractProtocol implements ProtocolLinkedAttributeImport {
- private static final Logger LOG = Logger.getLogger(KNXProtocol.class.getName());
+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, KNXProtocol.class);
public static final String PROTOCOL_NAME = PROTOCOL_NAMESPACE + ":knx";
public static final String PROTOCOL_DISPLAY_NAME = "KNX";
@@ -219,11 +221,6 @@ public class KNXProtocol extends AbstractProtocol implements ProtocolLinkedAttri
return;
}
- if (!protocolConfiguration.isEnabled()) {
- updateStatus(protocolConfiguration.getReferenceOrThrow(), ConnectionStatus.DISABLED);
- return;
- }
-
String localIp = protocolConfiguration.getMetaItem(META_KNX_LOCAL_HOST).flatMap(AbstractValueHolder::getValueAsString).orElse(null);
Integer remotePort = protocolConfiguration.getMetaItem(META_KNX_GATEWAY_PORT).flatMap(AbstractValueHolder::getValueAsInteger).orElse(3671);
String localKNXAddress = protocolConfiguration.getMetaItem(META_KNX_LOCAL_BUS_ADDRESS).flatMap(AbstractValueHolder::getValueAsString).orElse("0.0.0");
@@ -327,7 +324,7 @@ public class KNXProtocol extends AbstractProtocol implements ProtocolLinkedAttri
}
@Override
- protected void processLinkedAttributeWrite(AttributeEvent event, AssetAttribute protocolConfiguration) {
+ protected void processLinkedAttributeWrite(AttributeEvent event, Value processedValue, AssetAttribute protocolConfiguration) {
if (!protocolConfiguration.isEnabled()) {
LOG.fine("Protocol configuration is disabled so ignoring write request");
return;
diff --git a/agent/src/main/java/org/openremote/agent/protocol/knx/TypeMapper.java b/agent/src/main/java/org/openremote/agent/protocol/knx/TypeMapper.java
index 6edee0819..50454c9f1 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/knx/TypeMapper.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/knx/TypeMapper.java
@@ -1,6 +1,7 @@
package org.openremote.agent.protocol.knx;
import org.openremote.model.attribute.AttributeValueType;
+import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.value.ArrayValue;
import org.openremote.model.value.Value;
import org.openremote.model.value.ValueType;
@@ -14,9 +15,11 @@ import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
+
public class TypeMapper {
- private static final Logger LOG = Logger.getLogger(TypeMapper.class.getName());
+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, TypeMapper.class);
/** map dpt to OpenRemote type */
static private Map<String, AttributeValueType> dptToTypeMap;
diff --git a/agent/src/main/java/org/openremote/agent/protocol/macro/MacroProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/macro/MacroProtocol.java
index 5d58acd85..09fcb6b71 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/macro/MacroProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/macro/MacroProtocol.java
@@ -23,6 +23,7 @@ import org.openremote.agent.protocol.AbstractProtocol;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.attribute.*;
+import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.Pair;
import org.openremote.model.value.Value;
import org.openremote.model.value.ValueType;
@@ -36,6 +37,7 @@ import java.util.logging.Logger;
import static org.openremote.agent.protocol.macro.MacroConfiguration.getMacroActionIndex;
import static org.openremote.agent.protocol.macro.MacroConfiguration.isValidMacroConfiguration;
import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_INTEGER_POSITIVE;
/**
@@ -49,7 +51,7 @@ import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_INTEGER_POSITIVE
*/
public class MacroProtocol extends AbstractProtocol {
- private static final Logger LOG = Logger.getLogger(MacroProtocol.class.getName());
+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, MacroProtocol.class);
public static final String PROTOCOL_NAME = PROTOCOL_NAMESPACE + ":macro";
public static final String PROTOCOL_DISPLAY_NAME = "Macro";
@@ -133,7 +135,7 @@ public class MacroProtocol extends AbstractProtocol {
}
}
- protected final Map<AttributeRef, Pair<Boolean, List<MacroAction>>> macroMap = new ConcurrentHashMap<>();
+ protected final Map<AttributeRef, List<MacroAction>> macroMap = new ConcurrentHashMap<>();
protected final Map<AttributeRef, MacroExecutionTask> executions = new ConcurrentHashMap<>();
@Override
@@ -180,18 +182,17 @@ public class MacroProtocol extends AbstractProtocol {
protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
// Protocol configuration is actually a Macro Configuration
AttributeRef macroRef = protocolConfiguration.getReferenceOrThrow();
- boolean isEnabled = protocolConfiguration.isEnabled();
// Check macro configuration is valid
if (!isValidMacroConfiguration(protocolConfiguration)) {
LOG.fine("Macro configuration is not valid: " + protocolConfiguration);
updateStatus(macroRef, ConnectionStatus.ERROR);
// Put an empty list of actions against this macro
- macroMap.put(macroRef, new Pair<>(isEnabled, Collections.emptyList()));
+ macroMap.put(macroRef, Collections.emptyList());
} else {
// Store the macro actions for later execution requests
- macroMap.put(macroRef, new Pair<>(isEnabled, MacroConfiguration.getMacroActions(protocolConfiguration)));
- updateStatus(macroRef, isEnabled ? ConnectionStatus.CONNECTED : ConnectionStatus.DISABLED);
+ macroMap.put(macroRef, MacroConfiguration.getMacroActions(protocolConfiguration));
+ updateStatus(macroRef, ConnectionStatus.CONNECTED);
}
}
@@ -257,7 +258,7 @@ public class MacroProtocol extends AbstractProtocol {
}
@Override
- protected void processLinkedAttributeWrite(AttributeEvent event, AssetAttribute protocolConfiguration) {
+ protected void processLinkedAttributeWrite(AttributeEvent event, Value processedValue, AssetAttribute protocolConfiguration) {
AssetAttribute attribute = getLinkedAttribute(event.getAttributeRef());
@@ -324,26 +325,18 @@ public class MacroProtocol extends AbstractProtocol {
}
protected List<MacroAction> getMacroActions(AttributeRef protocolConfigurationRef) {
- Pair<Boolean, List<MacroAction>> actionsEnabledInfo;
- actionsEnabledInfo = macroMap.get(protocolConfigurationRef);
+ List<MacroAction> macroActions = macroMap.get(protocolConfigurationRef);
- if (actionsEnabledInfo == null || actionsEnabledInfo.value.size() == 0) {
+ if (macroActions == null || macroActions.isEmpty()) {
LOG.fine("No macro actions found for macro configuration: " + protocolConfigurationRef);
return Collections.emptyList();
}
- if (!actionsEnabledInfo.key) {
- LOG.fine("Macro configuration is disabled");
- return Collections.emptyList();
- }
-
- return actionsEnabledInfo.value;
+ return macroActions;
}
protected void executeMacro(AttributeRef attributeRef, List<MacroAction> actions, boolean repeat) {
MacroExecutionTask task = new MacroExecutionTask(attributeRef, actions, repeat);
task.start();
}
-
-
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/serial/AbstractSerialClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/serial/AbstractSerialClientProtocol.java
new file mode 100644
index 000000000..3fb73adce
--- /dev/null
+++ b/agent/src/main/java/org/openremote/agent/protocol/serial/AbstractSerialClientProtocol.java
@@ -0,0 +1,87 @@
+/*
+ * 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.agent.protocol.serial;
+
+import org.openremote.agent.protocol.io.AbstractIoClientProtocol;
+import org.openremote.agent.protocol.io.IoClient;
+import org.openremote.model.asset.AssetAttribute;
+import org.openremote.model.attribute.MetaItem;
+import org.openremote.model.attribute.MetaItemDescriptor;
+import org.openremote.model.syslog.SyslogCategory;
+import org.openremote.model.util.TextUtil;
+import org.openremote.model.value.Values;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.logging.Logger;
+
+import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
+
+/**
+ * This is an abstract TCP client protocol for communicating with TCP servers; concrete implementations must provide
+ * an {@link IoClient<T> for handling over the wire communication}.
+ */
+public abstract class AbstractSerialClientProtocol<T> extends AbstractIoClientProtocol<T, SerialIoClient<T>> {
+
+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, AbstractSerialClientProtocol.class);
+ public static final String PROTOCOL_NAME = PROTOCOL_NAMESPACE + ":serialClient";
+
+ public static final List<MetaItemDescriptor> PROTOCOL_META_ITEM_DESCRIPTORS = Arrays.asList(
+ META_PROTOCOL_SERIAL_PORT,
+ META_PROTOCOL_SERIAL_BAUDRATE
+ );
+
+ @Override
+ public AssetAttribute getProtocolConfigurationTemplate() {
+ return super.getProtocolConfigurationTemplate()
+ .addMeta(
+ new MetaItem(META_PROTOCOL_SERIAL_PORT, null)
+ );
+ }
+
+ @Override
+ protected SerialIoClient<T> createIoClient(AssetAttribute protocolConfiguration) throws Exception {
+
+ String port = Values.getMetaItemValueOrThrow(
+ protocolConfiguration,
+ META_PROTOCOL_SERIAL_PORT,
+ true,
+ true
+ ).flatMap(Values::getString).orElse(null);
+
+ Integer baudrate = Values.getMetaItemValueOrThrow(
+ protocolConfiguration,
+ META_PROTOCOL_SERIAL_BAUDRATE,
+ false,
+ true
+ ).flatMap(Values::getIntegerCoerced).orElse(null);
+
+ if (baudrate != null && baudrate < 1) {
+ throw new IllegalArgumentException("Baudrate must be in greater than 0");
+ }
+
+ if (TextUtil.isNullOrEmpty(port)) {
+ throw new IllegalArgumentException("Serial port cannot be empty");
+ }
+
+ return new SerialIoClient<>(port, baudrate, executorService);
+ }
+}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/serial/SerialClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/serial/SerialClientProtocol.java
new file mode 100644
index 000000000..175d27bb0
--- /dev/null
+++ b/agent/src/main/java/org/openremote/agent/protocol/serial/SerialClientProtocol.java
@@ -0,0 +1,203 @@
+/*
+ * 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.agent.protocol.serial;
+
+import io.netty.channel.ChannelHandler;
+import org.openremote.agent.protocol.Protocol;
+import org.openremote.agent.protocol.io.AbstractIoClientProtocol;
+import org.openremote.agent.protocol.tcp.TcpIoClient;
+import org.openremote.model.asset.AssetAttribute;
+import org.openremote.model.asset.agent.ProtocolConfiguration;
+import org.openremote.model.attribute.*;
+import org.openremote.model.syslog.SyslogCategory;
+import org.openremote.model.util.Pair;
+import org.openremote.model.value.Value;
+import org.openremote.model.value.ValueFilter;
+import org.openremote.model.value.Values;
+
+import java.util.*;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+import java.util.logging.Logger;
+
+import static org.openremote.container.util.Util.joinCollections;
+import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
+
+/**
+ * This is a generic TCP client protocol for communicating with TCP servers; it uses the {@link TcpIoClient} to
+ * handle the communication and all messages are processed as strings; if you require custom message type handling or
+ * more specific {@link #getProtocolConfigurationMetaItemDescriptors()} or {@link #getLinkedAttributeMetaItemDescriptors()}
+ * then please sub class the {@link AbstractSerialClientProtocol}).
+ * <h1>Protocol Configurations</h1>
+ * <p>
+ * {@link Attribute}s that are configured as {@link ProtocolConfiguration}s for this protocol support the meta
+ * items defined in {@link #PROTOCOL_META_ITEM_DESCRIPTORS}.
+ * <h1>Linked Attributes</h1>
+ * <p>
+ * {@link Attribute}s that are linked to this protocol using an {@link MetaItemType#AGENT_LINK} {@link MetaItem} support
+ * the meta items defined in {@link #ATTRIBUTE_META_ITEM_DESCRIPTORS}.
+ * <h1>Protocol -> Attribute</h1>
+ * <p>
+ * When a new value comes from the protocol destined for a linked {@link Attribute} the actual value written to the
+ * attribute can be filtered in the standard way using {@link ValueFilter}s via the
+ * {@link Protocol#META_ATTRIBUTE_VALUE_FILTERS} {@link MetaItem}.
+ * <h1>Attribute -> Protocol</h1>
+ * <p>
+ * When a linked {@link Attribute} is written to, the actual value written to the protocol can either be the exact value
+ * written to the linked {@link Attribute} or the {@link Protocol#META_ATTRIBUTE_WRITE_VALUE} {@link MetaItem} can be
+ * used to inject the written value into a bigger payload using the {@link Protocol#DYNAMIC_VALUE_PLACEHOLDER} and then
+ * this bigger payload will be written to the protocol.
+ * <h1>Executable Attributes</h1>
+ * When a linked {@link Attribute} that has an {@link MetaItemType#EXECUTABLE} {@link MetaItem} is executed the
+ * {@link Value} stored in the {@link Protocol#META_ATTRIBUTE_WRITE_VALUE} {@link MetaItem} is actually written to the
+ * protocol (note dynamic value injection doesn't work in this scenario as there is no dynamic value to inject).
+ * <p>
+ * <h1>Protocol Specifics</h1>
+ * <p>
+ * This is a generic protocol that supports:
+ * {@link Protocol#META_PROTOCOL_CONVERT_HEX} or {@link Protocol#META_PROTOCOL_CONVERT_BINARY} to facilitate working
+ * with UDP servers that handle binary data.
+ */
+public class SerialClientProtocol extends AbstractSerialClientProtocol<String> {
+
+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, SerialClientProtocol.class);
+ public static final String PROTOCOL_NAME = PROTOCOL_NAMESPACE + ":serialClient";
+ public static final String PROTOCOL_DISPLAY_NAME = "Serial Client";
+ public static final String PROTOCOL_VERSION = "1.0";
+ public static final List<MetaItemDescriptor> PROTOCOL_META_ITEM_DESCRIPTORS = joinCollections(AbstractSerialClientProtocol.PROTOCOL_META_ITEM_DESCRIPTORS, AbstractIoClientProtocol.PROTOCOL_GENERIC_META_ITEM_DESCRIPTORS);
+
+ public static final List<MetaItemDescriptor> ATTRIBUTE_META_ITEM_DESCRIPTORS = Arrays.asList(
+ META_ATTRIBUTE_MATCH_FILTERS,
+ META_ATTRIBUTE_MATCH_PREDICATE);
+
+ protected final Map<AttributeRef, List<Pair<AttributeRef, Consumer<String>>>> protocolMessageConsumers = new HashMap<>();
+
+ @Override
+ public String getProtocolName() {
+ return PROTOCOL_NAME;
+ }
+
+ @Override
+ public String getProtocolDisplayName() {
+ return PROTOCOL_DISPLAY_NAME;
+ }
+
+ @Override
+ public String getVersion() {
+ return PROTOCOL_VERSION;
+ }
+
+ @Override
+ protected List<MetaItemDescriptor> getProtocolConfigurationMetaItemDescriptors() {
+ return PROTOCOL_META_ITEM_DESCRIPTORS;
+ }
+
+ @Override
+ protected List<MetaItemDescriptor> getLinkedAttributeMetaItemDescriptors() {
+ return ATTRIBUTE_META_ITEM_DESCRIPTORS;
+ }
+
+ @Override
+ protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ synchronized (protocolMessageConsumers) {
+ protocolMessageConsumers.remove(protocolConfiguration.getReferenceOrThrow());
+ }
+ super.doUnlinkProtocolConfiguration(protocolConfiguration);
+ }
+
+ @Override
+ protected void doLinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
+ AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
+ Consumer<String> messageConsumer = Protocol.createGenericAttributeMessageConsumer(attribute, assetService, this::updateLinkedAttribute);
+
+ if (messageConsumer != null) {
+ synchronized (protocolMessageConsumers) {
+ protocolMessageConsumers.compute(protocolRef, (ref, consumers) -> {
+ if (consumers == null) {
+ consumers = new ArrayList<>();
+ }
+ consumers.add(new Pair<>(
+ attribute.getReferenceOrThrow(),
+ messageConsumer
+ ));
+ return consumers;
+ });
+ }
+ }
+ }
+
+ @Override
+ protected void doUnlinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
+ AttributeRef attributeRef = attribute.getReferenceOrThrow();
+ synchronized (protocolMessageConsumers) {
+ protocolMessageConsumers.compute(protocolConfiguration.getReferenceOrThrow(), (ref, consumers) -> {
+ if (consumers != null) {
+ consumers.removeIf((attrRefConsumer) -> attrRefConsumer.key.equals(attributeRef));
+ }
+ return consumers;
+ });
+ }
+ }
+
+ @Override
+ protected Supplier<ChannelHandler[]> getEncoderDecoderProvider(SerialIoClient<String> client, AssetAttribute protocolConfiguration) {
+ return getGenericStringEncodersAndDecoders(client, protocolConfiguration);
+ }
+
+ @Override
+ protected void onMessageReceived(AttributeRef protocolRef, String message) {
+ List<Pair<AttributeRef, Consumer<String>>> consumers;
+
+ synchronized (protocolMessageConsumers) {
+ consumers = protocolMessageConsumers.get(protocolRef);
+
+ if (consumers != null) {
+ consumers.forEach(c -> {
+ if (c.value != null) {
+ c.value.accept(message);
+ }
+ });
+ }
+ }
+ }
+
+ @Override
+ protected String createWriteMessage(AssetAttribute protocolConfiguration, AssetAttribute attribute, AttributeEvent event, Value processedValue) {
+ if (attribute.isReadOnly()) {
+ LOG.fine("Attempt to write to an attribute that doesn't support writes: " + event.getAttributeRef());
+ return null;
+ }
+
+ if (attribute.isExecutable()) {
+ AttributeExecuteStatus status = event.getValue()
+ .flatMap(Values::getString)
+ .flatMap(AttributeExecuteStatus::fromString)
+ .orElse(null);
+
+ if (status != null && status != AttributeExecuteStatus.REQUEST_START) {
+ LOG.fine("Unsupported execution status: " + status);
+ return null;
+ }
+ }
+
+ return processedValue != null ? processedValue.toString() : null;
+ }
+}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/serial/AbstractSerialClient.java b/agent/src/main/java/org/openremote/agent/protocol/serial/SerialIoClient.java
similarity index 73%
rename from agent/src/main/java/org/openremote/agent/protocol/serial/AbstractSerialClient.java
rename to agent/src/main/java/org/openremote/agent/protocol/serial/SerialIoClient.java
index 3261686dc..21385b799 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/serial/AbstractSerialClient.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/serial/SerialIoClient.java
@@ -29,14 +29,20 @@ import org.openremote.model.util.TextUtil;
/**
* This is a {@link IoClient} implementation for serial ports.
+ * <p>
+ * Users of this {@link IoClient} are responsible for adding encoders for converting messages of type &lt;T&gt; to
+ * {@link io.netty.buffer.ByteBuf} (see {@link MessageToByteEncoder}) and adding decoders to convert from
+ * {@link io.netty.buffer.ByteBuf} to messages of type &lt;T&gt; and ensuring these decoded messages are passed back
+ * to this client via {@link AbstractNettyIoClient#onMessageReceived} (see {@link ByteToMessageDecoder and
+ * {@link MessageToMessageDecoder}).
*/
-public abstract class AbstractSerialClient<T> extends AbstractNettyIoClient<T, NrJavaSerialAddress> {
+public class SerialIoClient<T> extends AbstractNettyIoClient<T, NrJavaSerialAddress> {
protected String port;
protected int baudRate;
public static int DEFAULT_BAUD_RATE = 38400;
- public AbstractSerialClient(String port, Integer baudRate, ProtocolExecutorService executorService) {
+ public SerialIoClient(String port, Integer baudRate, ProtocolExecutorService executorService) {
super(executorService);
TextUtil.requireNonNullAndNonEmpty(port);
this.port = port;
@@ -54,7 +60,7 @@ public abstract class AbstractSerialClient<T> extends AbstractNettyIoClient<T, N
}
@Override
- protected String getSocketAddressString() {
+ public String getClientUri() {
return "serial://" + port;
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.java
index ebfc5d35c..a0c6e218b 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.java
@@ -30,6 +30,7 @@ import org.openremote.model.simulator.SimulatorState;
import org.openremote.model.simulator.element.ColorSimulatorElement;
import org.openremote.model.simulator.element.NumberSimulatorElement;
import org.openremote.model.simulator.element.SwitchSimulatorElement;
+import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.value.Value;
import org.openremote.model.value.ValueType;
@@ -43,6 +44,7 @@ import static org.openremote.container.concurrent.GlobalLock.withLockReturning;
import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
import static org.openremote.model.attribute.MetaItemType.RANGE_MAX;
import static org.openremote.model.attribute.MetaItemType.RANGE_MIN;
+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_INTEGER_POSITIVE;
public class SimulatorProtocol extends AbstractProtocol {
@@ -95,7 +97,7 @@ public class SimulatorProtocol extends AbstractProtocol {
}
}
- private static final Logger LOG = Logger.getLogger(SimulatorProtocol.class.getName());
+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, SimulatorProtocol.class);
public static final int DEFAULT_WRITE_DELAY = 1000;
@@ -220,7 +222,7 @@ public class SimulatorProtocol extends AbstractProtocol {
.flatMap(AbstractValueHolder::getValueAsInteger)
.orElse(DEFAULT_WRITE_DELAY);
- updateStatus(protocolRef, protocolConfiguration.isEnabled() ? ConnectionStatus.CONNECTED : ConnectionStatus.DISABLED);
+ updateStatus(protocolRef, ConnectionStatus.CONNECTED);
return new Instance(mode, writeDelay, protocolConfiguration.isEnabled());
}
);
@@ -273,7 +275,7 @@ public class SimulatorProtocol extends AbstractProtocol {
}
@Override
- protected void processLinkedAttributeWrite(AttributeEvent event, AssetAttribute protocolConfiguration) {
+ protected void processLinkedAttributeWrite(AttributeEvent event, Value processedValue, AssetAttribute protocolConfiguration) {
if (putValue(event.getAttributeState())) {
// Notify listener when write was successful
if (protocolConfigurationValuesChangedHandler != null)
diff --git a/agent/src/main/java/org/openremote/agent/protocol/tcp/AbstractTcpClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/tcp/AbstractTcpClientProtocol.java
new file mode 100644
index 000000000..21ffcac31
--- /dev/null
+++ b/agent/src/main/java/org/openremote/agent/protocol/tcp/AbstractTcpClientProtocol.java
@@ -0,0 +1,90 @@
+/*
+ * 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.agent.protocol.tcp;
+
+import org.openremote.agent.protocol.io.AbstractIoClientProtocol;
+import org.openremote.agent.protocol.io.IoClient;
+import org.openremote.model.asset.AssetAttribute;
+import org.openremote.model.attribute.MetaItem;
+import org.openremote.model.attribute.MetaItemDescriptor;
+import org.openremote.model.syslog.SyslogCategory;
+import org.openremote.model.util.TextUtil;
+import org.openremote.model.value.Values;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.logging.Logger;
+
+import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
+import static org.openremote.model.attribute.MetaItemDescriptor.Access.ACCESS_PRIVATE;
+import static org.openremote.model.attribute.MetaItemDescriptorImpl.*;
+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
+
+/**
+ * This is an abstract TCP client protocol for communicating with TCP servers; concrete implementations must provide
+ * an {@link IoClient<T> for handling over the wire communication}.
+ */
+public abstract class AbstractTcpClientProtocol<T> extends AbstractIoClientProtocol<T, TcpIoClient<T>> {
+
+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, AbstractTcpClientProtocol.class);
+ public static final String PROTOCOL_NAME = PROTOCOL_NAMESPACE + ":tcpClient";
+
+ public static final List<MetaItemDescriptor> PROTOCOL_META_ITEM_DESCRIPTORS = Arrays.asList(
+ META_PROTOCOL_HOST,
+ META_PROTOCOL_PORT
+ );
+
+ @Override
+ public AssetAttribute getProtocolConfigurationTemplate() {
+ return super.getProtocolConfigurationTemplate()
+ .addMeta(
+ new MetaItem(META_PROTOCOL_HOST, null),
+ new MetaItem(META_PROTOCOL_PORT, null)
+ );
+ }
+
+ @Override
+ protected TcpIoClient<T> createIoClient(AssetAttribute protocolConfiguration) throws Exception {
+
+ String host = Values.getMetaItemValueOrThrow(
+ protocolConfiguration,
+ META_PROTOCOL_HOST,
+ true,
+ true
+ ).flatMap(Values::getString).orElse(null);
+
+ int port = Values.getMetaItemValueOrThrow(
+ protocolConfiguration,
+ META_PROTOCOL_PORT,
+ true,
+ true
+ ).flatMap(Values::getIntegerCoerced).orElse(0);
+
+ if (port < 1 || port > 65536) {
+ throw new IllegalArgumentException("Port must be in the range 1-65536");
+ }
+
+ if (TextUtil.isNullOrEmpty(host)) {
+ throw new IllegalArgumentException("Host cannot be empty");
+ }
+
+ return new TcpIoClient<>(host, port, executorService);
+ }
+}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/tcp/AbstractTcpServerProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/tcp/AbstractTcpServerProtocol.java
index d7696cd4e..20fe251aa 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/tcp/AbstractTcpServerProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/tcp/AbstractTcpServerProtocol.java
@@ -71,11 +71,6 @@ public abstract class AbstractTcpServerProtocol<T extends AbstractTcpServer<U>,
protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
final AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
- if (!protocolConfiguration.isEnabled()) {
- LOG.info("Protocol configuration is disabled so ignoring: " + protocolConfiguration.getReferenceOrThrow());
- return;
- }
-
int port = protocolConfiguration.getMetaItem(META_PROTOCOL_BIND_PORT)
.flatMap(AbstractValueHolder::getValueAsInteger)
.orElseThrow(() ->
diff --git a/agent/src/main/java/org/openremote/agent/protocol/tcp/TcpClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/tcp/TcpClientProtocol.java
new file mode 100644
index 000000000..eb421a8eb
--- /dev/null
+++ b/agent/src/main/java/org/openremote/agent/protocol/tcp/TcpClientProtocol.java
@@ -0,0 +1,211 @@
+/*
+ * 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.agent.protocol.tcp;
+
+import io.netty.channel.ChannelHandler;
+import org.openremote.agent.protocol.Protocol;
+import org.openremote.agent.protocol.io.AbstractIoClientProtocol;
+import org.openremote.model.asset.AssetAttribute;
+import org.openremote.model.asset.agent.ProtocolConfiguration;
+import org.openremote.model.attribute.*;
+import org.openremote.model.syslog.SyslogCategory;
+import org.openremote.model.util.Pair;
+import org.openremote.model.value.Value;
+import org.openremote.model.value.ValueFilter;
+import org.openremote.model.value.Values;
+
+import java.util.*;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+import java.util.logging.Logger;
+
+import static org.openremote.container.util.Util.joinCollections;
+import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
+
+/**
+ * This is a generic TCP client protocol for communicating with TCP servers; it uses the {@link TcpIoClient} to
+ * handle the communication and all messages are processed as strings; if you require custom message type handling or
+ * more specific {@link #getProtocolConfigurationMetaItemDescriptors()} or {@link #getLinkedAttributeMetaItemDescriptors()}
+ * then please sub class the {@link AbstractTcpClientProtocol}).
+ * <h1>Protocol Configurations</h1>
+ * <p>
+ * {@link Attribute}s that are configured as {@link ProtocolConfiguration}s for this protocol support the meta
+ * items defined in {@link #PROTOCOL_META_ITEM_DESCRIPTORS}.
+ * <h1>Linked Attributes</h1>
+ * <p>
+ * {@link Attribute}s that are linked to this protocol using an {@link MetaItemType#AGENT_LINK} {@link MetaItem} support
+ * the meta items defined in {@link #ATTRIBUTE_META_ITEM_DESCRIPTORS}.
+ * <h1>Protocol -> Attribute</h1>
+ * <p>
+ * When a new value comes from the protocol destined for a linked {@link Attribute} the actual value written to the
+ * attribute can be filtered in the standard way using {@link ValueFilter}s via the
+ * {@link Protocol#META_ATTRIBUTE_VALUE_FILTERS} {@link MetaItem}.
+ * <h1>Attribute -> Protocol</h1>
+ * <p>
+ * When a linked {@link Attribute} is written to, the actual value written to the protocol can either be the exact value
+ * written to the linked {@link Attribute} or the {@link Protocol#META_ATTRIBUTE_WRITE_VALUE} {@link MetaItem} can be
+ * used to inject the written value into a bigger payload using the {@link Protocol#DYNAMIC_VALUE_PLACEHOLDER} and then
+ * this bigger payload will be written to the protocol.
+ * <h1>Executable Attributes</h1>
+ * When a linked {@link Attribute} that has an {@link MetaItemType#EXECUTABLE} {@link MetaItem} is executed the
+ * {@link Value} stored in the {@link Protocol#META_ATTRIBUTE_WRITE_VALUE} {@link MetaItem} is actually written to the
+ * protocol (note dynamic value injection doesn't work in this scenario as there is no dynamic value to inject).
+ * <p>
+ * <h1>Protocol Specifics</h1>
+ * <p>
+ * This is a generic protocol that supports:
+ * {@link Protocol#META_PROTOCOL_CONVERT_HEX} or {@link Protocol#META_PROTOCOL_CONVERT_BINARY} to facilitate working
+ * with UDP servers that handle binary data.
+ */
+public class TcpClientProtocol extends AbstractTcpClientProtocol<String> {
+
+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, TcpClientProtocol.class);
+ public static final String PROTOCOL_NAME = PROTOCOL_NAMESPACE + ":tcpClient";
+ public static final String PROTOCOL_DISPLAY_NAME = "TCP Client";
+ public static final String PROTOCOL_VERSION = "1.0";
+ public static final List<MetaItemDescriptor> PROTOCOL_META_ITEM_DESCRIPTORS = joinCollections(AbstractTcpClientProtocol.PROTOCOL_META_ITEM_DESCRIPTORS, AbstractIoClientProtocol.PROTOCOL_GENERIC_META_ITEM_DESCRIPTORS);
+
+ public static final List<MetaItemDescriptor> ATTRIBUTE_META_ITEM_DESCRIPTORS = Arrays.asList(
+ META_ATTRIBUTE_MATCH_FILTERS,
+ META_ATTRIBUTE_MATCH_PREDICATE);
+
+ protected final Map<AttributeRef, List<Pair<AttributeRef, Consumer<String>>>> protocolMessageConsumers = new HashMap<>();
+
+ @Override
+ public String getProtocolName() {
+ return PROTOCOL_NAME;
+ }
+
+ @Override
+ public String getProtocolDisplayName() {
+ return PROTOCOL_DISPLAY_NAME;
+ }
+
+ @Override
+ public String getVersion() {
+ return PROTOCOL_VERSION;
+ }
+
+ @Override
+ protected List<MetaItemDescriptor> getProtocolConfigurationMetaItemDescriptors() {
+ return PROTOCOL_META_ITEM_DESCRIPTORS;
+ }
+
+ @Override
+ protected List<MetaItemDescriptor> getLinkedAttributeMetaItemDescriptors() {
+ return ATTRIBUTE_META_ITEM_DESCRIPTORS;
+ }
+
+ @Override
+ public AssetAttribute getProtocolConfigurationTemplate() {
+ return super.getProtocolConfigurationTemplate()
+ .addMeta(
+ new MetaItem(META_PROTOCOL_HOST, null),
+ new MetaItem(META_PROTOCOL_PORT, null)
+ );
+ }
+
+ @Override
+ protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ synchronized (protocolMessageConsumers) {
+ protocolMessageConsumers.remove(protocolConfiguration.getReferenceOrThrow());
+ }
+ super.doUnlinkProtocolConfiguration(protocolConfiguration);
+ }
+
+ @Override
+ protected void doLinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
+ AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
+ Consumer<String> messageConsumer = Protocol.createGenericAttributeMessageConsumer(attribute, assetService, this::updateLinkedAttribute);
+
+ if (messageConsumer != null) {
+ synchronized (protocolMessageConsumers) {
+ protocolMessageConsumers.compute(protocolRef, (ref, consumers) -> {
+ if (consumers == null) {
+ consumers = new ArrayList<>();
+ }
+ consumers.add(new Pair<>(
+ attribute.getReferenceOrThrow(),
+ messageConsumer
+ ));
+ return consumers;
+ });
+ }
+ }
+ }
+
+ @Override
+ protected void doUnlinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
+ AttributeRef attributeRef = attribute.getReferenceOrThrow();
+ synchronized (protocolMessageConsumers) {
+ protocolMessageConsumers.compute(protocolConfiguration.getReferenceOrThrow(), (ref, consumers) -> {
+ if (consumers != null) {
+ consumers.removeIf((attrRefConsumer) -> attrRefConsumer.key.equals(attributeRef));
+ }
+ return consumers;
+ });
+ }
+ }
+
+ @Override
+ protected Supplier<ChannelHandler[]> getEncoderDecoderProvider(TcpIoClient<String> client, AssetAttribute protocolConfiguration) {
+ return getGenericStringEncodersAndDecoders(client, protocolConfiguration);
+ }
+
+ @Override
+ protected void onMessageReceived(AttributeRef protocolRef, String message) {
+ List<Pair<AttributeRef, Consumer<String>>> consumers;
+
+ synchronized (protocolMessageConsumers) {
+ consumers = protocolMessageConsumers.get(protocolRef);
+
+ if (consumers != null) {
+ consumers.forEach(c -> {
+ if (c.value != null) {
+ c.value.accept(message);
+ }
+ });
+ }
+ }
+ }
+
+ @Override
+ protected String createWriteMessage(AssetAttribute protocolConfiguration, AssetAttribute attribute, AttributeEvent event, Value processedValue) {
+ if (attribute.isReadOnly()) {
+ LOG.fine("Attempt to write to an attribute that doesn't support writes: " + event.getAttributeRef());
+ return null;
+ }
+
+ if (attribute.isExecutable()) {
+ AttributeExecuteStatus status = event.getValue()
+ .flatMap(Values::getString)
+ .flatMap(AttributeExecuteStatus::fromString)
+ .orElse(null);
+
+ if (status != null && status != AttributeExecuteStatus.REQUEST_START) {
+ LOG.fine("Unsupported execution status: " + status);
+ return null;
+ }
+ }
+
+ return processedValue != null ? processedValue.toString() : null;
+ }
+}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/tcp/AbstractTcpClient.java b/agent/src/main/java/org/openremote/agent/protocol/tcp/TcpIoClient.java
similarity index 74%
rename from agent/src/main/java/org/openremote/agent/protocol/tcp/AbstractTcpClient.java
rename to agent/src/main/java/org/openremote/agent/protocol/tcp/TcpIoClient.java
index ce0a27321..a7b6d643d 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/tcp/AbstractTcpClient.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/tcp/TcpIoClient.java
@@ -19,33 +19,31 @@
*/
package org.openremote.agent.protocol.tcp;
-import io.netty.channel.Channel;
-import io.netty.channel.ChannelFuture;
-import io.netty.channel.ChannelOption;
-import io.netty.channel.EventLoopGroup;
+import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.openremote.agent.protocol.ProtocolExecutorService;
import org.openremote.agent.protocol.io.AbstractNettyIoClient;
import org.openremote.agent.protocol.io.IoClient;
-import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.TextUtil;
import java.net.InetSocketAddress;
-import java.util.logging.Logger;
-
-import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
/**
* This is a {@link IoClient} implementation for TCP.
+ * <p>
+ * Users of this {@link IoClient} are responsible for adding encoders for converting messages of type &lt;T&gt; to
+ * {@link io.netty.buffer.ByteBuf} (see {@link MessageToByteEncoder}) and adding decoders to convert from
+ * {@link io.netty.buffer.ByteBuf} to messages of type &lt;T&gt; and ensuring these decoded messages are passed back
+ * to this client via {@link AbstractNettyIoClient#onMessageReceived} (see {@link ByteToMessageDecoder and
+ * {@link MessageToMessageDecoder}).
*/
-public abstract class AbstractTcpClient<T> extends AbstractNettyIoClient<T, InetSocketAddress> {
+public class TcpIoClient<T> extends AbstractNettyIoClient<T, InetSocketAddress> {
- private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, AbstractTcpClient.class);
protected String host;
protected int port;
- public AbstractTcpClient(String host, int port, ProtocolExecutorService executorService) {
+ public TcpIoClient(String host, int port, ProtocolExecutorService executorService) {
super(executorService);
TextUtil.requireNonNullAndNonEmpty(host);
this.host = host;
@@ -58,7 +56,7 @@ public abstract class AbstractTcpClient<T> extends AbstractNettyIoClient<T, Inet
}
@Override
- protected String getSocketAddressString() {
+ public String getClientUri() {
return "tcp://" + host + ":" + port;
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/timer/TimerProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/timer/TimerProtocol.java
index 5b71e71fb..f419b7c17 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/timer/TimerProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/timer/TimerProtocol.java
@@ -24,6 +24,7 @@ import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.attribute.MetaItemType;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.attribute.*;
+import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.TextUtil;
import org.openremote.model.value.Value;
import org.openremote.model.value.ValueType;
@@ -38,6 +39,9 @@ import java.util.logging.Logger;
import static java.util.logging.Level.FINER;
import static org.openremote.agent.protocol.timer.TimerConfiguration.*;
import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
+import static org.openremote.model.attribute.MetaItemDescriptor.Access.ACCESS_PRIVATE;
+import static org.openremote.model.attribute.MetaItemDescriptorImpl.metaItemFixedBoolean;
+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
/**
* This protocol can be used to trigger an {@link AttributeEvent} using a cron based timer.
@@ -55,52 +59,65 @@ import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
*/
public class TimerProtocol extends AbstractProtocol {
- private static final Logger LOG = Logger.getLogger(TimerProtocol.class.getName());
+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, TimerProtocol.class);
public static final String PROTOCOL_NAME = PROTOCOL_NAMESPACE + ":timer";
public static final String PROTOCOL_DISPLAY_NAME = "Timer";
public static final String META_TIMER_CRON_EXPRESSION = PROTOCOL_NAME + ":cronExpression";
public static final String META_TIMER_ACTION = PROTOCOL_NAME + ":action";
public static final String META_TIMER_VALUE_LINK = PROTOCOL_NAME + ":link";
+ public static final String META_TIMER_DISABLED = PROTOCOL_NAME + ":disabled";
protected static final String VERSION = "1.0";
- protected static final List<MetaItemDescriptor> PROTOCOL_META_ITEM_DESCRIPTORS = Arrays.asList(
- new MetaItemDescriptorImpl(
- META_TIMER_CRON_EXPRESSION,
- ValueType.STRING,
+ public static final MetaItemDescriptor META_PROTOCOL_TIMER_ACTION = new MetaItemDescriptorImpl(
+ META_TIMER_ACTION,
+ ValueType.OBJECT,
true,
- null, // TODO Should use TextUtil.REGEXP_PATTERN_CRON_EXPRESSION
- MetaItemDescriptor.PatternFailure.CRON_EXPRESSION.name(),
- null,
null,
- false, null, null, null),
- new MetaItemDescriptorImpl(
- META_TIMER_ACTION,
- ValueType.OBJECT,
- true,
null,
null,
null,
+ false, null, null, null, false);
+
+ public static final MetaItemDescriptor META_PROTOCOL_TIMER_CRON_EXPRESSION = new MetaItemDescriptorImpl(
+ META_TIMER_CRON_EXPRESSION,
+ ValueType.STRING,
+ true,
+ null, // TODO Should use TextUtil.REGEXP_PATTERN_CRON_EXPRESSION
+ MetaItemDescriptor.PatternFailure.CRON_EXPRESSION.name(),
null,
- false, null, null, null, false)
+ null,
+ false, null, null, null);
+
+ public static final MetaItemDescriptor META_PROTOCOL_TIMER_DISABLED = metaItemFixedBoolean(
+ META_TIMER_DISABLED,
+ ACCESS_PRIVATE,
+ false);
+
+ public static final MetaItemDescriptor META_ATTRIBUTE_TIMER_VALUE_LINK = new MetaItemDescriptorImpl(
+ META_TIMER_VALUE_LINK,
+ ValueType.STRING,
+ true,
+ "^(" +
+ TimerValue.ENABLED + "|" +
+ TimerValue.CRON_EXPRESSION + "|" +
+ TimerValue.TIME +
+ ")$",
+ TimerValue.ENABLED + "|" +
+ TimerValue.CRON_EXPRESSION + "|" +
+ TimerValue.TIME,
+ null,
+ null,
+ false, null, null, null);
+
+ protected static final List<MetaItemDescriptor> PROTOCOL_META_ITEM_DESCRIPTORS = Arrays.asList(
+ META_PROTOCOL_TIMER_ACTION,
+ META_PROTOCOL_TIMER_CRON_EXPRESSION,
+ META_PROTOCOL_TIMER_DISABLED
);
protected static final List<MetaItemDescriptor> ATTRIBUTE_META_ITEM_DESCRIPTORS = Collections.singletonList(
- new MetaItemDescriptorImpl(
- META_TIMER_VALUE_LINK,
- ValueType.STRING,
- true,
- "^(" +
- TimerValue.ENABLED + "|" +
- TimerValue.CRON_EXPRESSION + "|" +
- TimerValue.TIME +
- ")$",
- TimerValue.ENABLED + "|" +
- TimerValue.CRON_EXPRESSION + "|" +
- TimerValue.TIME,
- null,
- null,
- false, null, null, null)
+ META_ATTRIBUTE_TIMER_VALUE_LINK
);
protected final Map<AttributeRef, CronExpressionParser> cronExpressionMap = new HashMap<>();
@@ -151,7 +168,7 @@ public class TimerProtocol extends AbstractProtocol {
// Verify that this is a valid Timer Configuration
if (!isValidTimerConfiguration(protocolConfiguration)) {
LOG.warning("Timer Configuration is not valid so it will be ignored: " + protocolRef);
- updateStatus(protocolRef, ConnectionStatus.ERROR);
+ updateStatus(protocolRef, ConnectionStatus.ERROR_CONFIGURATION);
return;
}
@@ -162,25 +179,30 @@ public class TimerProtocol extends AbstractProtocol {
if (expressionParser == null || !expressionParser.isValid()) {
LOG.warning("Timer cron expression is missing or invalid");
- updateStatus(protocolRef, ConnectionStatus.ERROR);
+ updateStatus(protocolRef, ConnectionStatus.ERROR_CONFIGURATION);
+ return;
+ }
+
+ CronExpression cronExpression = createCronExpression(expressionParser.buildCronExpression());
+
+ if (cronExpression == null) {
+ LOG.warning("Timer cron expression is missing or invalid");
+ updateStatus(protocolRef, ConnectionStatus.ERROR_CONFIGURATION);
return;
}
cronExpressionMap.put(protocolRef, expressionParser);
- if (protocolConfiguration.isEnabled()) {
+ if (!isTimerDisabled(protocolConfiguration)) {
+ getCronScheduler()
+ .addOrReplaceJob(
+ getTimerId(protocolRef),
+ cronExpression,
+ () -> doTriggerAction(protocolConfiguration)
+ );
updateStatus(protocolRef, ConnectionStatus.CONNECTED);
- CronExpression cronExpression = createCronExpression(expressionParser.buildCronExpression());
- if (cronExpression != null) {
- getCronScheduler()
- .addOrReplaceJob(
- getTimerId(protocolRef),
- cronExpression,
- () -> doTriggerAction(protocolConfiguration)
- );
- } else {
- updateStatus(protocolRef, ConnectionStatus.DISABLED);
- }
+ } else {
+ updateStatus(protocolRef, ConnectionStatus.DISABLED);
}
}
@@ -194,7 +216,7 @@ public class TimerProtocol extends AbstractProtocol {
}
@Override
- protected void processLinkedAttributeWrite(AttributeEvent event, AssetAttribute protocolConfiguration) {
+ protected void processLinkedAttributeWrite(AttributeEvent event, Value processedValue, AssetAttribute protocolConfiguration) {
AssetAttribute attribute = getLinkedAttribute(event.getAttributeRef());
TimerValue timerValue = TimerConfiguration.getValue(attribute).orElse(null);
@@ -218,13 +240,19 @@ public class TimerProtocol extends AbstractProtocol {
.orElse(null))
.orElseThrow(() -> new IllegalStateException("Writing to protocol configuration CONNECTED property requires a boolean value"));
- if (enabled == protocolConfiguration.isEnabled()) {
- LOG.finer("Protocol configuration enabled status is already: " + enabled);
+ if (enabled != isTimerDisabled(protocolConfiguration)) {
+ LOG.finer("Timer enabled status is already: " + enabled);
} else {
- LOG.fine("Updating protocol configuration enabled status: " + enabled);
+ LOG.fine("Updating timer enabled status: " + enabled);
updateLinkedProtocolConfiguration(
protocolConfiguration,
- protocolConfig -> protocolConfig.setDisabled(!enabled)
+ protocolConfig -> {
+ if (!enabled) {
+ protocolConfig.addMeta(META_PROTOCOL_TIMER_DISABLED);
+ } else {
+ protocolConfig.removeMeta(META_PROTOCOL_TIMER_DISABLED);
+ }
+ }
);
}
break;
@@ -282,7 +310,7 @@ public class TimerProtocol extends AbstractProtocol {
switch (timerValue) {
case ENABLED:
- updateLinkedAttribute(new AttributeState(attribute.getReferenceOrThrow(), Values.create(protocolConfiguration.isEnabled())));
+ updateLinkedAttribute(new AttributeState(attribute.getReferenceOrThrow(), Values.create(!isTimerDisabled(protocolConfiguration))));
break;
case CRON_EXPRESSION:
case TIME:
@@ -366,4 +394,8 @@ public class TimerProtocol extends AbstractProtocol {
return cronExpression;
}
+
+ protected static boolean isTimerDisabled(Attribute protocolConfiguration) {
+ return protocolConfiguration.getMetaItem(META_TIMER_DISABLED).isPresent();
+ }
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/udp/AbstractUdpClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/udp/AbstractUdpClientProtocol.java
index 4c3b8c260..65fdeb582 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/udp/AbstractUdpClientProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/udp/AbstractUdpClientProtocol.java
@@ -19,174 +19,32 @@
*/
package org.openremote.agent.protocol.udp;
-import io.netty.util.CharsetUtil;
-import org.openremote.agent.protocol.AbstractProtocol;
+import org.openremote.agent.protocol.io.AbstractIoClientProtocol;
import org.openremote.agent.protocol.io.IoClient;
-import org.openremote.container.Container;
import org.openremote.model.asset.AssetAttribute;
-import org.openremote.model.asset.agent.ConnectionStatus;
-import org.openremote.model.attribute.AttributeRef;
-import org.openremote.model.attribute.MetaItem;
import org.openremote.model.attribute.MetaItemDescriptor;
import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.TextUtil;
-import org.openremote.model.value.Value;
import org.openremote.model.value.Values;
-import java.nio.charset.Charset;
-import java.util.*;
-import java.util.concurrent.ScheduledFuture;
-import java.util.function.Consumer;
+import java.util.Arrays;
+import java.util.List;
import java.util.logging.Logger;
import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
import static org.openremote.model.attribute.MetaItemDescriptor.Access.ACCESS_PRIVATE;
-import static org.openremote.model.attribute.MetaItemDescriptorImpl.*;
+import static org.openremote.model.attribute.MetaItemDescriptorImpl.metaItemFixedBoolean;
+import static org.openremote.model.attribute.MetaItemDescriptorImpl.metaItemInteger;
import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
/**
* This is an abstract UDP client protocol for communicating with UDP servers; concrete implementations must provide
* an {@link IoClient<T> for handling over the wire communication}.
*/
-public abstract class AbstractUdpClientProtocol<T> extends AbstractProtocol {
+public abstract class AbstractUdpClientProtocol<T> extends AbstractIoClientProtocol<T, UdpIoClient<T>> {
- protected class ClientAndQueue {
- IoClient<T> client;
- AttributeRef protocolRef;
- Deque<Runnable> actionQueue = new ArrayDeque<>();
- int retries;
- int timeoutMillis;
- Consumer<T> currentResponseConsumer;
- ScheduledFuture responseMonitor;
-
- protected ClientAndQueue(IoClient<T> client, AttributeRef protocolRef) {
- this.client = client;
- this.protocolRef = protocolRef;
- }
-
- protected void connect() {
- LOG.info("Connecting UDP client");
- client.addConnectionStatusConsumer(status -> AbstractUdpClientProtocol.this.onConnectionStatusChanged(protocolRef, status));
- client.addMessageConsumer(this::onMessageReceived);
- client.connect();
- }
-
- protected void disconnect() {
- LOG.info("Disconnecting UDP client");
- client.removeAllMessageConsumers();
- client.removeAllConnectionStatusConsumers();
- client.disconnect();
- }
-
- protected synchronized void send(T str, Consumer<T> responseConsumer, AttributeInfo attributeInfo) {
- if (currentResponseConsumer != null) {
- actionQueue.add(() -> processNextCommand(str, responseConsumer, attributeInfo));
- return;
- }
-
- executorService.schedule(() -> processNextCommand(str, responseConsumer, attributeInfo), 0);
- }
-
- protected void onMessageReceived(T msg) {
- if (currentResponseConsumer != null) {
- currentResponseConsumer.accept(msg);
- }
- }
-
- protected synchronized void processNextCommand(T msg, Consumer<T> responseConsumer, AttributeInfo attributeInfo) {
- currentResponseConsumer = null;
-
- LOG.fine("Executing send for attribute: " + attributeInfo.attributeRef);
-
- if (responseConsumer == null) {
- LOG.finer("No response consumer so fire and forget");
- client.sendMessage(msg);
-
- // Move straight onto next command
- if (!actionQueue.isEmpty()) {
- executorService.schedule(actionQueue.pop(), 0);
- }
- return;
- }
-
- LOG.finer("Send expects a response [retries=" + attributeInfo.retries + ", timeout=" + attributeInfo.responseTimeoutMillis + "]");
- this.retries = attributeInfo.retries;
- this.timeoutMillis = attributeInfo.responseTimeoutMillis;
- this.currentResponseConsumer = responseStr -> {
- responseConsumer.accept(responseStr);
- synchronized (ClientAndQueue.this) {
- if (this.responseMonitor != null) {
- this.responseMonitor.cancel(true);
- currentResponseConsumer = null;
- }
-
- if (!actionQueue.isEmpty()) {
- executorService.schedule(actionQueue.pop(), 0);
- }
- }
- };
-
- Runnable failureAction = () -> {
- synchronized (ClientAndQueue.this) {
- LOG.fine("Send failed: response timeout reached");
-
- if (retries > 0) {
- LOG.fine("Sending message again");
- retries--;
- client.sendMessage(msg);
- } else {
- LOG.fine("No more retries left so abandoning send");
- responseMonitor.cancel(false);
- currentResponseConsumer = null;
- if (!actionQueue.isEmpty()) {
- executorService.schedule(actionQueue.pop(), 0);
- }
- }
- }
- };
-
- client.sendMessage(msg);
- responseMonitor = executorService.scheduleWithFixedDelay(failureAction, timeoutMillis, timeoutMillis);
- }
- }
-
- protected static class AttributeInfo {
- AttributeRef attributeRef;
- Consumer<Value> sendConsumer;
- ScheduledFuture pollingTask;
- int retries;
- int responseTimeoutMillis;
-
- protected AttributeInfo(AttributeRef attributeRef, int retries, int responseTimeoutMillis) {
- this.attributeRef = attributeRef;
- this.retries = retries;
- this.responseTimeoutMillis = responseTimeoutMillis;
- }
- }
-
- private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, AbstractUdpClientProtocol.class.getName());
+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, AbstractUdpClientProtocol.class);
public static final String PROTOCOL_NAME = PROTOCOL_NAMESPACE + ":abstractUdpClient";
- protected final Map<AttributeRef, ClientAndQueue> clientMap = new HashMap<>();
-
- /**
- * The UDP server host name/IP address
- */
- public static final MetaItemDescriptor META_PROTOCOL_HOST = metaItemString(
- PROTOCOL_NAME + ":host",
- ACCESS_PRIVATE,
- true,
- TextUtil.REGEXP_PATTERN_STRING_NON_EMPTY_NO_WHITESPACE,
- PatternFailure.STRING_EMPTY_OR_CONTAINS_WHITESPACE);
-
- /**
- * The UDP server port
- */
- public static final MetaItemDescriptor META_PROTOCOL_PORT = metaItemInteger(
- PROTOCOL_NAME + ":port",
- ACCESS_PRIVATE,
- true,
- 1,
- 65536);
/**
* Optionally sets the port that this UDP client will bind to (if not set then a random ephemeral port will be used)
@@ -198,77 +56,14 @@ public abstract class AbstractUdpClientProtocol<T> extends AbstractProtocol {
1,
65536);
- /**
- * Indicates whether the server replies to each packet not just polling requests (need this to successfully match
- * request and response packets)
- */
- public static final MetaItemDescriptor META_SERVER_ALWAYS_RESPONDS = metaItemFixedBoolean(
- PROTOCOL_NAME + ":serverAlwaysResponds",
- ACCESS_PRIVATE,
- false);
-
- /**
- * Indicates how long to wait for a response from the server before re-attempting {@link #META_SEND_RETRIES} times
- */
- public static final MetaItemDescriptor META_RESPONSE_TIMEOUT_MILLIS = metaItemInteger(
- PROTOCOL_NAME + ":responseTimeoutMillis",
- ACCESS_PRIVATE,
- true,
- 1,
- 10000);
-
- /**
- * How many times to retry sending a failed command
- */
- public static final MetaItemDescriptor META_SEND_RETRIES = metaItemInteger(
- PROTOCOL_NAME + ":sendRetries",
- ACCESS_PRIVATE,
- true,
- 1,
- 10);
-
-
public static final List<MetaItemDescriptor> PROTOCOL_META_ITEM_DESCRIPTORS = Arrays.asList(
META_PROTOCOL_HOST,
META_PROTOCOL_PORT,
- META_PROTOCOL_BIND_PORT,
- META_PROTOCOL_CHARSET,
- META_PROTOCOL_CONVERT_BINARY,
- META_PROTOCOL_CONVERT_HEX,
- META_RESPONSE_TIMEOUT_MILLIS,
- META_SEND_RETRIES,
- META_SERVER_ALWAYS_RESPONDS
+ META_PROTOCOL_BIND_PORT
);
@Override
- protected void doStop(Container container) {
- clientMap.forEach((ref, clientAndQueue) -> clientAndQueue.disconnect());
- }
-
- @Override
- protected List<MetaItemDescriptor> getProtocolConfigurationMetaItemDescriptors() {
- return PROTOCOL_META_ITEM_DESCRIPTORS;
- }
-
- @Override
- public AssetAttribute getProtocolConfigurationTemplate() {
- return super.getProtocolConfigurationTemplate()
- .addMeta(
- new MetaItem(META_PROTOCOL_HOST, null),
- new MetaItem(META_PROTOCOL_PORT, null)
- );
- }
-
- @Override
- protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
-
- if (!protocolConfiguration.isEnabled()) {
- LOG.info("Protocol configuration is disabled so ignoring: " + protocolConfiguration.getReferenceOrThrow());
- return;
- }
-
- final AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
-
+ protected UdpIoClient<T> createIoClient(AssetAttribute protocolConfiguration) throws Exception {
String host = Values.getMetaItemValueOrThrow(
protocolConfiguration,
META_PROTOCOL_HOST,
@@ -277,61 +72,31 @@ public abstract class AbstractUdpClientProtocol<T> extends AbstractProtocol {
).flatMap(Values::getString).orElse(null);
int port = Values.getMetaItemValueOrThrow(
- protocolConfiguration,
- META_PROTOCOL_PORT,
- true,
- true
+ protocolConfiguration,
+ META_PROTOCOL_PORT,
+ true,
+ true
).flatMap(Values::getIntegerCoerced).orElse(0);
Integer bindPort = Values.getMetaItemValueOrThrow(
- protocolConfiguration,
- META_PROTOCOL_BIND_PORT,
- false,
- false
+ protocolConfiguration,
+ META_PROTOCOL_BIND_PORT,
+ false,
+ false
).flatMap(Values::getIntegerCoerced).orElse(null);
- boolean hexMode = Values.getMetaItemValueOrThrow(
- protocolConfiguration,
- META_PROTOCOL_CONVERT_HEX,
- false,
- false
- ).flatMap(Values::getBoolean).orElse(false);
-
- boolean binaryMode = Values.getMetaItemValueOrThrow(
- protocolConfiguration,
- META_PROTOCOL_CONVERT_BINARY,
- false,
- false
- ).flatMap(Values::getBoolean).orElse(false);
-
- Charset charset = Values.getMetaItemValueOrThrow(
- protocolConfiguration,
- META_PROTOCOL_CHARSET,
- false,
- true
- ).flatMap(Values::getString).map(Charset::forName).orElse(CharsetUtil.UTF_8);
-
- IoClient<T> client = createIoClient(host, port, bindPort, charset, binaryMode, hexMode);
- ClientAndQueue clientAndQueue = new ClientAndQueue(client, protocolRef);
- clientMap.put(protocolRef, clientAndQueue);
- clientAndQueue.connect();
- }
+ if (port < 1 || port > 65536) {
+ throw new IllegalArgumentException("Port must be in the range 1-65536");
+ }
- @Override
- protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
- final AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
+ if (bindPort != null && (bindPort < 1 || bindPort > 65536)) {
+ throw new IllegalArgumentException("Bind port must be in the range 1-65536 if specified");
+ }
- ClientAndQueue clientAndQueue = clientMap.remove(protocolRef);
- if (clientAndQueue != null) {
- clientAndQueue.disconnect();
- updateStatus(protocolRef, ConnectionStatus.DISCONNECTED);
+ if (TextUtil.isNullOrEmpty(host)) {
+ throw new IllegalArgumentException("Host cannot be empty");
}
- }
- protected void onConnectionStatusChanged(AttributeRef protocolRef, ConnectionStatus connectionStatus) {
- updateStatus(protocolRef, connectionStatus);
+ return new UdpIoClient<>(host, port, bindPort, executorService);
}
-
-
- protected abstract IoClient<T> createIoClient(String host, int port, Integer bindPort, Charset charset, boolean binaryMode, boolean hexMode);
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/udp/UdpClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/udp/UdpClientProtocol.java
index f6896917e..24c617dad 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/udp/UdpClientProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/udp/UdpClientProtocol.java
@@ -19,39 +19,33 @@
*/
package org.openremote.agent.protocol.udp;
-import io.netty.buffer.ByteBuf;
-import io.netty.util.CharsetUtil;
-import org.openremote.agent.protocol.AbstractProtocol;
+import io.netty.channel.ChannelHandler;
import org.openremote.agent.protocol.Protocol;
-import org.openremote.agent.protocol.io.IoClient;
-import org.openremote.container.Container;
-import org.openremote.model.asset.Asset;
+import org.openremote.agent.protocol.io.AbstractIoClientProtocol;
import org.openremote.model.asset.AssetAttribute;
-import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.asset.agent.ProtocolConfiguration;
import org.openremote.model.attribute.*;
import org.openremote.model.syslog.SyslogCategory;
-import org.openremote.model.util.TextUtil;
+import org.openremote.model.util.Pair;
import org.openremote.model.value.Value;
import org.openremote.model.value.ValueFilter;
import org.openremote.model.value.Values;
-import java.nio.charset.Charset;
import java.util.*;
-import java.util.concurrent.ScheduledFuture;
-import java.util.concurrent.TimeUnit;
-import java.util.function.BiConsumer;
import java.util.function.Consumer;
+import java.util.function.Supplier;
import java.util.logging.Logger;
+import static org.openremote.container.util.Util.joinCollections;
import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
-import static org.openremote.model.attribute.MetaItemDescriptor.Access.ACCESS_PRIVATE;
-import static org.openremote.model.attribute.MetaItemDescriptorImpl.*;
import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
/**
- * This is a UDP client protocol for communicating with UDP servers; it uses the {@link AbstractUdpClient} to handle the
- * communication, it is important that all data (sent and received) fits in a single datagram packet.
+ * This is a UDP client protocol for communicating with UDP servers; it uses the {@link UdpIoClient} to handle the
+ * communication and all messages are processed as strings; if you require custom message type handling or more specific
+ * {@link #getProtocolConfigurationMetaItemDescriptors()} or {@link #getLinkedAttributeMetaItemDescriptors()} then please
+ * sub class the {@link AbstractUdpClientProtocol}).
+ * <b>It is important that all data (sent and received) fits in a single datagram packet.</b>
* <h1>Protocol Configurations</h1>
* <p>
* {@link Attribute}s that are configured as {@link ProtocolConfiguration}s for this protocol support the meta
@@ -84,23 +78,18 @@ import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
*/
public class UdpClientProtocol extends AbstractUdpClientProtocol<String> {
- private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, UdpClientProtocol.class.getName());
+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, UdpClientProtocol.class);
public static final String PROTOCOL_NAME = PROTOCOL_NAMESPACE + ":udpClient";
public static final String PROTOCOL_DISPLAY_NAME = "UDP Client";
public static final String PROTOCOL_VERSION = "1.0";
- private static int DEFAULT_RESPONSE_TIMEOUT_MILLIS = 3000;
- private static int DEFAULT_SEND_RETRIES = 1;
- private static boolean DEFAULT_SERVER_ALWAYS_RESPONDS = false;
- private static int MIN_POLLING_MILLIS = 1000;
- protected final Map<AttributeRef, AttributeInfo> attributeInfoMap = new HashMap<>();
+
+ public static final List<MetaItemDescriptor> PROTOCOL_META_ITEM_DESCRIPTORS = joinCollections(AbstractUdpClientProtocol.PROTOCOL_META_ITEM_DESCRIPTORS, AbstractIoClientProtocol.PROTOCOL_GENERIC_META_ITEM_DESCRIPTORS);
public static final List<MetaItemDescriptor> ATTRIBUTE_META_ITEM_DESCRIPTORS = Arrays.asList(
- META_ATTRIBUTE_WRITE_VALUE,
- META_POLLING_MILLIS,
- META_RESPONSE_TIMEOUT_MILLIS,
- META_SEND_RETRIES,
- META_SERVER_ALWAYS_RESPONDS
- );
+ META_ATTRIBUTE_MATCH_FILTERS,
+ META_ATTRIBUTE_MATCH_PREDICATE);
+
+ protected final Map<AttributeRef, List<Pair<AttributeRef, Consumer<String>>>> protocolMessageConsumers = new HashMap<>();
@Override
public String getProtocolName() {
@@ -117,210 +106,96 @@ public class UdpClientProtocol extends AbstractUdpClientProtocol<String> {
return PROTOCOL_VERSION;
}
+ @Override
+ protected List<MetaItemDescriptor> getProtocolConfigurationMetaItemDescriptors() {
+ return PROTOCOL_META_ITEM_DESCRIPTORS;
+ }
+
@Override
protected List<MetaItemDescriptor> getLinkedAttributeMetaItemDescriptors() {
return ATTRIBUTE_META_ITEM_DESCRIPTORS;
}
@Override
- protected void doLinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
-
- if (!protocolConfiguration.isEnabled()) {
- LOG.info("Protocol configuration is disabled so ignoring: " + protocolConfiguration.getReferenceOrThrow());
- return;
- }
-
- ClientAndQueue clientAndQueue = clientMap.get(protocolConfiguration.getReferenceOrThrow());
-
- if (clientAndQueue == null) {
- return;
- }
-
- final Integer pollingMillis = Values.getMetaItemValueOrThrow(attribute, META_POLLING_MILLIS, false, true)
- .flatMap(Values::getIntegerCoerced)
- .orElse(null);
-
- final int responseTimeoutMillis = Values.getMetaItemValueOrThrow(attribute, META_RESPONSE_TIMEOUT_MILLIS, false, true)
- .flatMap(Values::getIntegerCoerced)
- .orElseGet(() ->
- Values.getMetaItemValueOrThrow(protocolConfiguration, META_RESPONSE_TIMEOUT_MILLIS, false, true)
- .flatMap(Values::getIntegerCoerced)
- .orElse(DEFAULT_RESPONSE_TIMEOUT_MILLIS)
- );
-
- final int sendRetries = Values.getMetaItemValueOrThrow(attribute, META_SEND_RETRIES, false, true)
- .flatMap(Values::getIntegerCoerced)
- .orElseGet(() ->
- Values.getMetaItemValueOrThrow(protocolConfiguration, META_SEND_RETRIES, false, true)
- .flatMap(Values::getIntegerCoerced)
- .orElse(DEFAULT_SEND_RETRIES)
- );
-
- final boolean serverAlwaysResponds = Values.getMetaItemValueOrThrow(
- attribute,
- META_SERVER_ALWAYS_RESPONDS,
- false,
- false
- ).flatMap(Values::getBoolean).orElseGet(() ->
- Values.getMetaItemValueOrThrow(protocolConfiguration, META_SERVER_ALWAYS_RESPONDS, false, false)
- .flatMap(Values::getBoolean)
- .orElse(DEFAULT_SERVER_ALWAYS_RESPONDS)
- );
-
- Consumer<Value> sendConsumer = null;
- ScheduledFuture pollingTask = null;
- AttributeInfo info = new AttributeInfo(attribute.getReferenceOrThrow(), sendRetries, responseTimeoutMillis);
-
- if (!attribute.isReadOnly()) {
- sendConsumer = Protocol.createDynamicAttributeWriteConsumer(attribute, str ->
- clientAndQueue.send(
- str,
- serverAlwaysResponds ? responseStr -> {
- // Just drop the response; something in the future could be used to verify send was successful
- } : null,
- info));
- }
-
- if (pollingMillis != null && pollingMillis < MIN_POLLING_MILLIS) {
- LOG.warning("Polling ms must be >= " + MIN_POLLING_MILLIS);
- return;
- }
-
- final String writeValue = Values.getMetaItemValueOrThrow(attribute, META_ATTRIBUTE_WRITE_VALUE, false, true)
- .map(Object::toString).orElse(null);
+ public AssetAttribute getProtocolConfigurationTemplate() {
+ return super.getProtocolConfigurationTemplate()
+ .addMeta(
+ new MetaItem(META_PROTOCOL_HOST, null),
+ new MetaItem(META_PROTOCOL_PORT, null)
+ );
+ }
- if (pollingMillis != null && TextUtil.isNullOrEmpty(writeValue)) {
- LOG.warning("Polling requires the META_ATTRIBUTE_WRITE_VALUE meta item to be set");
- return;
+ @Override
+ protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ synchronized (protocolMessageConsumers) {
+ protocolMessageConsumers.remove(protocolConfiguration.getReferenceOrThrow());
}
+ super.doUnlinkProtocolConfiguration(protocolConfiguration);
+ }
- if (pollingMillis != null) {
- Consumer<String> responseConsumer = str -> {
- LOG.fine("Polling response received updating attribute: " + attribute.getReferenceOrThrow());
- updateLinkedAttribute(
- new AttributeState(
+ @Override
+ protected void doLinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
+ AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
+ Consumer<String> messageConsumer = Protocol.createGenericAttributeMessageConsumer(attribute, assetService, this::updateLinkedAttribute);
+
+ if (messageConsumer != null) {
+ synchronized (protocolMessageConsumers) {
+ protocolMessageConsumers.compute(protocolRef, (ref, consumers) -> {
+ if (consumers == null) {
+ consumers = new ArrayList<>();
+ }
+ consumers.add(new Pair<>(
attribute.getReferenceOrThrow(),
- str != null ? Values.create(str) : null));
- };
-
- Runnable pollingRunnable = () -> clientAndQueue.send(writeValue, responseConsumer, info);
- pollingTask = schedulePollingRequest(clientAndQueue, attribute.getReferenceOrThrow(), pollingRunnable, pollingMillis);
+ messageConsumer
+ ));
+ return consumers;
+ });
+ }
}
-
- attributeInfoMap.put(attribute.getReferenceOrThrow(), info);
- info.pollingTask = pollingTask;
- info.sendConsumer = sendConsumer;
}
@Override
protected void doUnlinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
- AttributeInfo info = attributeInfoMap.remove(attribute.getReferenceOrThrow());
-
- if (info != null && info.pollingTask != null) {
- info.pollingTask.cancel(false);
+ AttributeRef attributeRef = attribute.getReferenceOrThrow();
+ synchronized (protocolMessageConsumers) {
+ protocolMessageConsumers.compute(protocolConfiguration.getReferenceOrThrow(), (ref, consumers) -> {
+ if (consumers != null) {
+ consumers.removeIf((attrRefConsumer) -> attrRefConsumer.key.equals(attributeRef));
+ }
+ return consumers;
+ });
}
}
@Override
- protected void processLinkedAttributeWrite(AttributeEvent event, AssetAttribute protocolConfiguration) {
- AttributeInfo info = attributeInfoMap.get(event.getAttributeRef());
-
- if (info == null || info.sendConsumer == null) {
- LOG.info("Request to write unlinked attribute or attribute that doesn't support writes so ignoring: " + event);
- return;
- }
-
- AssetAttribute attribute = getLinkedAttribute(event.getAttributeRef());
- AttributeExecuteStatus status = null;
-
- if (attribute.isExecutable()) {
- status = event.getValue()
- .flatMap(Values::getString)
- .flatMap(AttributeExecuteStatus::fromString)
- .orElse(null);
+ protected Supplier<ChannelHandler[]> getEncoderDecoderProvider(UdpIoClient<String> client, AssetAttribute protocolConfiguration) {
+ return getGenericStringEncodersAndDecoders(client, protocolConfiguration);
+ }
- if (status != null && status != AttributeExecuteStatus.REQUEST_START) {
- LOG.fine("Unsupported execution status: " + status);
- return;
+ @Override
+ protected void onMessageReceived(AttributeRef protocolRef, String message) {
+ List<Pair<AttributeRef, Consumer<String>>> consumers;
+
+ synchronized (protocolMessageConsumers) {
+ consumers = protocolMessageConsumers.get(protocolRef);
+
+ if (consumers != null) {
+ consumers.forEach(c -> {
+ if (c.value != null) {
+ c.value.accept(message);
+ }
+ });
}
}
-
- Value value = status != null ? null : event.getValue().orElse(null);
- info.sendConsumer.accept(value);
-
- if (status != null) {
- updateLinkedAttribute(new AttributeState(event.getAttributeRef(), AttributeExecuteStatus.COMPLETED.asValue()));
- }
- }
-
- protected void onConnectionStatusChanged(AttributeRef protocolRef, ConnectionStatus connectionStatus) {
- updateStatus(protocolRef, connectionStatus);
- }
-
- protected ScheduledFuture schedulePollingRequest(ClientAndQueue clientAndQueue,
- AttributeRef attributeRef,
- Runnable pollingTask,
- int pollingMillis) {
-
- LOG.fine("Scheduling polling request on client '" + clientAndQueue.client + "' to execute every " + pollingMillis + " ms for attribute: " + attributeRef);
-
- return executorService.scheduleWithFixedDelay(pollingTask, 0, pollingMillis, TimeUnit.MILLISECONDS);
}
@Override
- protected IoClient<String> createIoClient(String host, int port, Integer bindPort, Charset charset, boolean binaryMode, boolean hexMode) {
-
- LOG.info("Adding UDP client");
-
- BiConsumer<ByteBuf, List<String>> decoder;
- BiConsumer<String, ByteBuf> encoder;
-
- if (hexMode) {
- decoder = (buf, messages) -> {
- byte[] bytes = new byte[buf.readableBytes()];
- buf.readBytes(bytes);
- String msg = Protocol.bytesToHexString(bytes);
- messages.add(msg);
- };
- encoder = (message, buf) -> {
- byte[] bytes = Protocol.bytesFromHexString(message);
- buf.writeBytes(bytes);
- };
- } else if (binaryMode) {
- decoder = (buf, messages) -> {
- byte[] bytes = new byte[buf.readableBytes()];
- buf.readBytes(bytes);
- String msg = Protocol.bytesToBinaryString(bytes);
- messages.add(msg);
- };
- encoder = (message, buf) -> {
- byte[] bytes = Protocol.bytesFromBinaryString(message);
- buf.writeBytes(bytes);
- };
- } else {
- final Charset finalCharset = charset != null ? charset : CharsetUtil.UTF_8;
- decoder = (buf, messages) -> {
- String msg = buf.toString(finalCharset);
- messages.add(msg);
- buf.readerIndex(buf.readerIndex() + buf.readableBytes());
- };
- encoder = (message, buf) -> buf.writeBytes(message.getBytes(finalCharset));
+ protected String createWriteMessage(AssetAttribute protocolConfiguration, AssetAttribute attribute, AttributeEvent event, Value processedValue) {
+ if (attribute.isReadOnly()) {
+ LOG.fine("Attempt to write to an attribute that doesn't support writes: " + event.getAttributeRef());
+ return null;
}
- BiConsumer<ByteBuf, List<String>> finalDecoder = decoder;
- BiConsumer<String, ByteBuf> finalEncoder = encoder;
-
- return new AbstractUdpClient<String>(host, port, bindPort, executorService) {
-
- @Override
- protected void decode(ByteBuf buf, List<String> messages) {
- finalDecoder.accept(buf, messages);
- }
-
- @Override
- protected void encode(String message, ByteBuf buf) {
- finalEncoder.accept(message, buf);
- }
- };
+ return processedValue != null ? processedValue.toString() : null;
}
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/udp/AbstractUdpClient.java b/agent/src/main/java/org/openremote/agent/protocol/udp/UdpIoClient.java
similarity index 78%
rename from agent/src/main/java/org/openremote/agent/protocol/udp/AbstractUdpClient.java
rename to agent/src/main/java/org/openremote/agent/protocol/udp/UdpIoClient.java
index a7fbdc492..64622d43a 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/udp/AbstractUdpClient.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/udp/UdpIoClient.java
@@ -20,14 +20,10 @@
package org.openremote.agent.protocol.udp;
import io.netty.buffer.ByteBuf;
-import io.netty.channel.Channel;
-import io.netty.channel.ChannelFuture;
-import io.netty.channel.ChannelHandlerContext;
-import io.netty.channel.EventLoopGroup;
+import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.nio.NioDatagramChannel;
-import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.MessageToMessageEncoder;
import org.openremote.agent.protocol.ProtocolExecutorService;
import org.openremote.agent.protocol.io.AbstractNettyIoClient;
@@ -43,19 +39,21 @@ import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
/**
* This is a {@link IoClient} implementation for UDP.
+ * <p>
+ * Users of this {@link IoClient} are responsible for adding encoders for converting messages of type &lt;T&gt; to
+ * {@link io.netty.buffer.ByteBuf} (see {@link MessageToByteEncoder}) and adding decoders to convert from
+ * {@link io.netty.buffer.ByteBuf} to messages of type &lt;T&gt; and ensuring these decoded messages are passed back
+ * to this client via {@link AbstractNettyIoClient#onMessageReceived} (see {@link ByteToMessageDecoder and
+ * {@link MessageToMessageDecoder}).
*/
-public abstract class AbstractUdpClient<T> extends AbstractNettyIoClient<T, InetSocketAddress> {
+public class UdpIoClient<T> extends AbstractNettyIoClient<T, InetSocketAddress> {
- private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, AbstractUdpClient.class);
+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, UdpIoClient.class);
protected String host;
protected int port;
protected int bindPort;
- public AbstractUdpClient(String host, int port, ProtocolExecutorService executorService) {
- this(host, port, null, executorService);
- }
-
- public AbstractUdpClient(String host, int port, Integer bindPort, ProtocolExecutorService executorService) {
+ public UdpIoClient(String host, int port, Integer bindPort, ProtocolExecutorService executorService) {
super(executorService);
TextUtil.requireNonNullAndNonEmpty(host);
@@ -75,7 +73,7 @@ public abstract class AbstractUdpClient<T> extends AbstractNettyIoClient<T, Inet
}
@Override
- protected void addEncoders(Channel channel) {
+ protected void addEncodersDecoders(Channel channel) {
channel.pipeline().addLast(new MessageToMessageEncoder<ByteBuf>() {
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
@@ -83,14 +81,9 @@ public abstract class AbstractUdpClient<T> extends AbstractNettyIoClient<T, Inet
}
});
- super.addEncoders(channel);
- }
-
- @Override
- protected void addDecoders(Channel channel) {
- super.addDecoders(channel);
+ super.addEncodersDecoders(channel);
- channel.pipeline().addFirst(new MessageToMessageDecoder<DatagramPacket>() {
+ channel.pipeline().addFirst(new io.netty.handler.codec.MessageToMessageDecoder<DatagramPacket>() {
@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception {
out.add(msg.content().retain());
@@ -109,7 +102,7 @@ public abstract class AbstractUdpClient<T> extends AbstractNettyIoClient<T, Inet
}
@Override
- protected String getSocketAddressString() {
+ public String getClientUri() {
return "udp://" + host + ":" + port + " (bindPort: " + bindPort + ")";
}
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 ed84ac0ae..e3e21b23b 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
@@ -33,8 +33,10 @@ import org.openremote.model.asset.agent.AgentLink;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.attribute.*;
import org.openremote.model.file.FileInfo;
+import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.EnumUtil;
import org.openremote.model.util.Pair;
+import org.openremote.model.value.Value;
import org.openremote.model.value.ValueType;
import org.openremote.model.value.Values;
import org.w3c.dom.Document;
@@ -51,6 +53,7 @@ import java.util.logging.Logger;
import static org.openremote.agent.protocol.velbus.VelbusConfiguration.*;
import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_INTEGER_POSITIVE_NON_ZERO;
import static org.openremote.model.util.TextUtil.isNullOrEmpty;
@@ -63,7 +66,7 @@ public abstract class AbstractVelbusProtocol extends AbstractProtocol implements
public static final String META_VELBUS_DEVICE_VALUE_LINK = PROTOCOL_BASE_NAME + ":deviceValueLink";
public static final String META_VELBUS_TIME_INJECTION_INTERVAL_SECONDS = PROTOCOL_BASE_NAME + ":timeInjectionInterval";
public static final int DEFAULT_TIME_INJECTION_INTERVAL_SECONDS = 3600 * 6; //
- public static final Logger LOG = Logger.getLogger(AbstractVelbusProtocol.class.getName());
+ public static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, AbstractVelbusProtocol.class);
protected final Map<String, VelbusNetwork> networkMap = new HashMap<>();
protected final Map<AttributeRef, Pair<VelbusNetwork, Consumer<ConnectionStatus>>> networkConfigurationMap = new HashMap<>();
protected final Map<AttributeRef, Consumer<DevicePropertyValue>> attributePropertyValueConsumers = new HashMap<>();
@@ -111,9 +114,7 @@ public abstract class AbstractVelbusProtocol extends AbstractProtocol implements
@Override
protected List<MetaItemDescriptor> getProtocolConfigurationMetaItemDescriptors() {
- List<MetaItemDescriptor> descriptors = new ArrayList<>();
- descriptors.addAll(META_ITEM_DESCRIPTORS);
- return descriptors;
+ return new ArrayList<>(META_ITEM_DESCRIPTORS);
}
@Override
@@ -125,11 +126,6 @@ public abstract class AbstractVelbusProtocol extends AbstractProtocol implements
protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
final AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
- if (!protocolConfiguration.isEnabled()) {
- LOG.info("Protocol configuration is disabled so ignoring: " + protocolConfiguration.getReferenceOrThrow());
- return;
- }
-
// Look for existing network
String networkIdentifier = getUniqueNetworkIdentifier(protocolConfiguration);
VelbusNetwork velbusNetwork = null;
@@ -139,7 +135,7 @@ public abstract class AbstractVelbusProtocol extends AbstractProtocol implements
velbusNetwork = networkMap.get(networkIdentifier);
} else {
try {
- IoClient<VelbusPacket> messageProcessor = createClient(protocolConfiguration);
+ IoClient<VelbusPacket> messageProcessor = createIoClient(protocolConfiguration);
int timeInjectionSeconds = getTimeInjectionIntervalSeconds(protocolConfiguration);
LOG.fine("Creating new VELBUS network instance for protocolConfiguration: " + protocolRef);
velbusNetwork = new VelbusNetwork(messageProcessor, executorService, timeInjectionSeconds);
@@ -239,7 +235,7 @@ public abstract class AbstractVelbusProtocol extends AbstractProtocol implements
}
@Override
- protected void processLinkedAttributeWrite(AttributeEvent event, AssetAttribute protocolConfiguration) {
+ protected void processLinkedAttributeWrite(AttributeEvent event, Value processedValue, AssetAttribute protocolConfiguration) {
Pair<VelbusNetwork, Consumer<ConnectionStatus>> velbusNetworkConsumerPair = networkConfigurationMap.get(protocolConfiguration.getReferenceOrThrow());
if (velbusNetworkConsumerPair == null) {
@@ -346,22 +342,10 @@ public abstract class AbstractVelbusProtocol extends AbstractProtocol implements
return devices.stream().map(AssetTreeNode::new).toArray(AssetTreeNode[]::new);
}
- ProtocolExecutorService getExecutorService() {
- return executorService;
- }
-
- void updateAttribute(AttributeState attributeState) {
- updateLinkedAttribute(attributeState);
- }
-
- AssetAttribute getAttribute(AttributeRef attributeRef) {
- return getLinkedAttribute(attributeRef);
- }
-
/**
* Should return an instance of {@link IoClient} for the supplied protocolConfiguration
*/
- protected abstract IoClient<VelbusPacket> createClient(AssetAttribute protocolConfiguration) throws RuntimeException;
+ protected abstract IoClient<VelbusPacket> createIoClient(AssetAttribute protocolConfiguration) throws RuntimeException;
/**
diff --git a/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusConfiguration.java b/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusConfiguration.java
index eb741881e..721c780e3 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusConfiguration.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusConfiguration.java
@@ -32,6 +32,7 @@ import static org.openremote.model.attribute.MetaItem.isMetaNameEqualTo;
import static org.openremote.model.util.TextUtil.isNullOrEmpty;
public final class VelbusConfiguration {
+
private VelbusConfiguration() {}
public static int getTimeInjectionIntervalSeconds(AssetAttribute protocolConfiguration) {
diff --git a/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusPacketEncoderDecoder.java b/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusPacketDecoder.java
similarity index 87%
rename from agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusPacketEncoderDecoder.java
rename to agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusPacketDecoder.java
index 7ab35539f..c759ee4e6 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusPacketEncoderDecoder.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusPacketDecoder.java
@@ -20,15 +20,17 @@
package org.openremote.agent.protocol.velbus;
import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageDecoder;
import java.util.List;
import static org.openremote.agent.protocol.velbus.VelbusPacket.MAX_PACKET_SIZE;
-public final class VelbusPacketEncoderDecoder {
- private VelbusPacketEncoderDecoder() {}
+public final class VelbusPacketDecoder extends ByteToMessageDecoder {
- public static void decode(ByteBuf buf, List<VelbusPacket> messages) {
+ @Override
+ protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> messages) throws Exception {
int startIndex = buf.indexOf(0, buf.capacity() - 1, VelbusPacket.STX);
if (startIndex < 0) {
@@ -70,8 +72,4 @@ public final class VelbusPacketEncoderDecoder {
messages.add(packet);
}
}
-
- public static void encode(VelbusPacket message, ByteBuf buf) {
- buf.writeBytes(message.pack());
- }
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusTcpClient.java b/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusPacketEncoder.java
similarity index 60%
rename from agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusTcpClient.java
rename to agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusPacketEncoder.java
index a0c896c58..6dac0df93 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusTcpClient.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusPacketEncoder.java
@@ -20,23 +20,17 @@
package org.openremote.agent.protocol.velbus;
import io.netty.buffer.ByteBuf;
-import org.openremote.agent.protocol.tcp.AbstractTcpClient;
-import org.openremote.agent.protocol.ProtocolExecutorService;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.MessageToByteEncoder;
import java.util.List;
-public class VelbusTcpClient extends AbstractTcpClient<VelbusPacket> {
+import static org.openremote.agent.protocol.velbus.VelbusPacket.MAX_PACKET_SIZE;
- public VelbusTcpClient(String host, int port, ProtocolExecutorService executorService) {
- super(host, port, executorService);
- }
- @Override
- protected void decode(ByteBuf buf, List<VelbusPacket> messages) {
- VelbusPacketEncoderDecoder.decode(buf, messages);
- }
+public final class VelbusPacketEncoder extends MessageToByteEncoder<VelbusPacket> {
@Override
- protected void encode(VelbusPacket message, ByteBuf buf) {
- VelbusPacketEncoderDecoder.encode(message, buf);
+ protected void encode(ChannelHandlerContext ctx, VelbusPacket message, ByteBuf buf) throws Exception {
+ buf.writeBytes(message.pack());
}
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusSerialClient.java b/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusSerialClient.java
deleted file mode 100644
index 87df29cfa..000000000
--- a/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusSerialClient.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * 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.agent.protocol.velbus;
-
-import io.netty.buffer.ByteBuf;
-import org.openremote.agent.protocol.serial.AbstractSerialClient;
-import org.openremote.agent.protocol.ProtocolExecutorService;
-
-import java.util.List;
-
-public class VelbusSerialClient extends AbstractSerialClient<VelbusPacket> {
-
- public VelbusSerialClient(String port, Integer baudRate, ProtocolExecutorService executorService) {
- super(port, baudRate, executorService);
- }
-
- @Override
- protected void decode(ByteBuf buf, List<VelbusPacket> messages) {
- VelbusPacketEncoderDecoder.decode(buf, messages);
- }
-
- @Override
- protected void encode(VelbusPacket message, ByteBuf buf) {
- VelbusPacketEncoderDecoder.encode(message, buf);
- }
-}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusSerialProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusSerialProtocol.java
index 199523117..0300fccf0 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusSerialProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusSerialProtocol.java
@@ -19,24 +19,23 @@
*/
package org.openremote.agent.protocol.velbus;
-import org.openremote.agent.protocol.io.IoClient;
-import org.openremote.agent.protocol.serial.NrJavaSerialAddress;
+import io.netty.channel.ChannelHandler;
import org.openremote.agent.protocol.ProtocolConfigurationDiscovery;
+import org.openremote.agent.protocol.io.AbstractNettyIoClient;
+import org.openremote.agent.protocol.io.IoClient;
+import org.openremote.agent.protocol.serial.SerialIoClient;
import org.openremote.model.AbstractValueHolder;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.attribute.AttributeValidationResult;
import org.openremote.model.attribute.MetaItem;
import org.openremote.model.attribute.MetaItemDescriptor;
-import org.openremote.model.attribute.MetaItemDescriptorImpl;
import org.openremote.model.util.TextUtil;
-import org.openremote.model.value.ValueType;
import org.openremote.model.value.Values;
import java.util.Arrays;
import java.util.List;
import static org.openremote.model.asset.agent.ProtocolConfiguration.initProtocolConfiguration;
-import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_INTEGER_POSITIVE_NON_ZERO;
public class VelbusSerialProtocol extends AbstractVelbusProtocol implements ProtocolConfigurationDiscovery {
@@ -45,27 +44,9 @@ public class VelbusSerialProtocol extends AbstractVelbusProtocol implements Prot
public static final String META_VELBUS_SERIAL_PORT = PROTOCOL_NAME + ":port";
public static final String META_VELBUS_SERIAL_BAUDRATE = PROTOCOL_NAME + ":baudRate";
public static final int DEFAULT_BAUDRATE = 38400;
- public static final List<MetaItemDescriptorImpl> PROTOCOL_META_ITEM_DESCRIPTORS = Arrays.asList(
- new MetaItemDescriptorImpl(
- META_VELBUS_SERIAL_PORT,
- ValueType.STRING,
- true,
- null,
- null,
- 1,
- null,
- false,
- null, null, null),
- new MetaItemDescriptorImpl(
- META_VELBUS_SERIAL_BAUDRATE,
- ValueType.NUMBER,
- false,
- REGEXP_PATTERN_INTEGER_POSITIVE_NON_ZERO,
- MetaItemDescriptor.PatternFailure.INTEGER_POSITIVE_NON_ZERO.name(),
- 1,
- Values.create(DEFAULT_BAUDRATE),
- false,
- null, null, null)
+ public static final List<MetaItemDescriptor> PROTOCOL_META_ITEM_DESCRIPTORS = Arrays.asList(
+ META_PROTOCOL_SERIAL_PORT,
+ META_PROTOCOL_SERIAL_BAUDRATE
);
@Override
@@ -101,15 +82,23 @@ public class VelbusSerialProtocol extends AbstractVelbusProtocol implements Prot
}
@Override
- protected IoClient<VelbusPacket> createClient(AssetAttribute protocolConfiguration) throws RuntimeException {
+ protected IoClient<VelbusPacket> createIoClient(AssetAttribute protocolConfiguration) throws RuntimeException {
// Extract port and baud rate
String port = protocolConfiguration.getMetaItem(META_VELBUS_SERIAL_PORT).flatMap(AbstractValueHolder::getValueAsString).orElse(null);
Integer baudRate = protocolConfiguration.getMetaItem(META_VELBUS_SERIAL_BAUDRATE).flatMap(AbstractValueHolder::getValueAsInteger).orElse(DEFAULT_BAUDRATE);
TextUtil.requireNonNullAndNonEmpty(port, "Port cannot be null or empty");
-
- return new VelbusSerialClient(port, baudRate, executorService);
+ SerialIoClient<VelbusPacket> client = new SerialIoClient<>(port, baudRate, executorService);
+
+ client.setEncoderDecoderProvider(
+ () -> new ChannelHandler[]{
+ new VelbusPacketEncoder(),
+ new VelbusPacketDecoder(),
+ new AbstractNettyIoClient.MessageToMessageDecoder<>(VelbusPacket.class, client)
+ }
+ );
+ return client;
}
@Override
diff --git a/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusTcpProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusTcpProtocol.java
index 539ee7fb9..cbbe2ed7a 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusTcpProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/velbus/VelbusTcpProtocol.java
@@ -19,7 +19,10 @@
*/
package org.openremote.agent.protocol.velbus;
+import io.netty.channel.ChannelHandler;
+import org.openremote.agent.protocol.io.AbstractNettyIoClient;
import org.openremote.agent.protocol.io.IoClient;
+import org.openremote.agent.protocol.tcp.TcpIoClient;
import org.openremote.model.AbstractValueHolder;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.attribute.AttributeValidationResult;
@@ -37,8 +40,8 @@ import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_INTEGER_POSITIVE
public class VelbusTcpProtocol extends AbstractVelbusProtocol {
- public static final String PROTOCOL_NAME = PROTOCOL_BASE_NAME + "Socket";
- public static final String PROTOCOL_DISPLAY_NAME = "VELBUS Socket";
+ public static final String PROTOCOL_NAME = PROTOCOL_BASE_NAME + "Tcp";
+ public static final String PROTOCOL_DISPLAY_NAME = "VELBUS TCP";
public static final String META_VELBUS_HOST = PROTOCOL_NAME + ":host";
public static final String META_VELBUS_PORT = PROTOCOL_NAME + ":port";
public static final List<MetaItemDescriptorImpl> META_ITEM_DESCRIPTORS = Arrays.asList(
@@ -100,7 +103,7 @@ public class VelbusTcpProtocol extends AbstractVelbusProtocol {
}
@Override
- protected IoClient<VelbusPacket> createClient(AssetAttribute protocolConfiguration) throws RuntimeException {
+ protected IoClient<VelbusPacket> createIoClient(AssetAttribute protocolConfiguration) throws RuntimeException {
// Extract IP and Host
String host = protocolConfiguration.getMetaItem(META_VELBUS_HOST).flatMap(AbstractValueHolder::getValueAsString).orElse(null);
@@ -108,8 +111,15 @@ public class VelbusTcpProtocol extends AbstractVelbusProtocol {
TextUtil.requireNonNullAndNonEmpty(host, "Host cannot be null or empty");
Objects.requireNonNull(port, "Port cannot be null");
-
- return new VelbusTcpClient(host, port, executorService);
+ TcpIoClient<VelbusPacket> client = new TcpIoClient<>(host, port, executorService);
+ client.setEncoderDecoderProvider(
+ () -> new ChannelHandler[] {
+ new VelbusPacketEncoder(),
+ new VelbusPacketDecoder(),
+ new AbstractNettyIoClient.MessageToMessageDecoder<>(VelbusPacket.class, client)
+ }
+ );
+ return client;
}
@Override
diff --git a/agent/src/main/java/org/openremote/agent/protocol/websocket/AbstractWebsocketClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/websocket/AbstractWebsocketClientProtocol.java
new file mode 100644
index 000000000..2e21783af
--- /dev/null
+++ b/agent/src/main/java/org/openremote/agent/protocol/websocket/AbstractWebsocketClientProtocol.java
@@ -0,0 +1,391 @@
+/*
+ * 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.agent.protocol.websocket;
+
+import org.apache.http.HttpHeaders;
+import org.jboss.resteasy.client.jaxrs.ResteasyClient;
+import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
+import org.jboss.resteasy.util.BasicAuthHelper;
+import org.openremote.agent.protocol.Protocol;
+import org.openremote.agent.protocol.http.HttpClientProtocol;
+import org.openremote.agent.protocol.http.OAuthGrant;
+import org.openremote.agent.protocol.http.WebTargetBuilder;
+import org.openremote.agent.protocol.io.AbstractIoClientProtocol;
+import org.openremote.container.Container;
+import org.openremote.model.AbstractValueHolder;
+import org.openremote.model.asset.AssetAttribute;
+import org.openremote.model.asset.agent.ConnectionStatus;
+import org.openremote.model.asset.agent.ProtocolConfiguration;
+import org.openremote.model.attribute.*;
+import org.openremote.model.syslog.SyslogCategory;
+import org.openremote.model.util.Pair;
+import org.openremote.model.util.TextUtil;
+import org.openremote.model.value.StringValue;
+import org.openremote.model.value.Values;
+
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.Invocation;
+import javax.ws.rs.core.MultivaluedHashMap;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.*;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import static org.openremote.agent.protocol.http.HttpClientProtocol.*;
+import static org.openremote.agent.protocol.http.WebTargetBuilder.createClient;
+import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
+import static org.openremote.model.attribute.MetaItemDescriptor.Access.ACCESS_PRIVATE;
+import static org.openremote.model.attribute.MetaItemDescriptorImpl.*;
+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
+import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_STRING_NON_EMPTY;
+
+/**
+ * This is a base class for websocket client protocols for communicating with Websocket servers; it uses the
+ * {@link WebsocketIoClient} to handle the communication, but also uses the {@link WebTargetBuilder} factory to send
+ * {@link WebsocketHttpSubscription}s when the connection is initially established.
+ * <p>
+ * Implementations of this protocol must provide the required encoders and decoders via {@link #getEncoderDecoderProvider}.
+ * <h1>Protocol Configurations</h1>
+ * <p>
+ * {@link Attribute}s that are configured as {@link ProtocolConfiguration}s for this protocol support the meta
+ * items defined in {@link #PROTOCOL_META_ITEM_DESCRIPTORS}.
+ * <h1>Linked Attributes</h1>
+ * <p>
+ * {@link Attribute}s that are linked to this protocol using an {@link MetaItemType#AGENT_LINK} {@link MetaItem} support
+ * the meta items defined in {@link #ATTRIBUTE_META_ITEM_DESCRIPTORS}.
+ * <h1>Protocol Specifics</h1>
+ * When the websocket connection is established it is possible to subscribe to events by specifying the
+ * {@link #META_SUBSCRIPTIONS} {@link MetaItem}, a subscription can be either a message sent over the websocket
+ * or a HTTP REST API call.
+ */
+public abstract class AbstractWebsocketClientProtocol<T> extends AbstractIoClientProtocol<T, WebsocketIoClient<T>> {
+
+ public static final String PROTOCOL_NAME = PROTOCOL_NAMESPACE + ":abstractWebsocketClient";
+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, AbstractWebsocketClientProtocol.class);
+
+ /*--------------- META ITEMS TO BE USED ON PROTOCOL CONFIGURATIONS ---------------*/
+
+ /**
+ * Websocket connect endpoint URI
+ */
+ public static final MetaItemDescriptor META_PROTOCOL_CONNECT_URI = metaItemString(
+ PROTOCOL_NAME + ":uri",
+ ACCESS_PRIVATE,
+ true,
+ REGEXP_PATTERN_STRING_NON_EMPTY,
+ PatternFailure.STRING_EMPTY
+ );
+
+ /**
+ * Headers for websocket connect call (see {@link HttpClientProtocol#META_HEADERS} for details)
+ */
+ public static final MetaItemDescriptor META_PROTOCOL_CONNECT_HEADERS = metaItemObject(
+ PROTOCOL_NAME + ":headers",
+ ACCESS_PRIVATE,
+ false,
+ null);
+
+ /**
+ * Array of {@link WebsocketSubscription}s that should be executed either once the websocket connection is
+ * established (when used on a linked {@link ProtocolConfiguration}) or when a linked attribute is linked (when
+ * used on a linked {@link Attribute}); the subscriptions are executed in the order specified in the array
+ */
+ public static final MetaItemDescriptor META_SUBSCRIPTIONS = metaItemArray(
+ PROTOCOL_NAME + ":subscriptions",
+ ACCESS_PRIVATE,
+ false,
+ null);
+
+ /*--------------- META ITEMS TO BE USED ON LINKED ATTRIBUTES ---------------*/
+
+ public static final List<MetaItemDescriptor> ATTRIBUTE_META_ITEM_DESCRIPTORS = Arrays.asList(
+ META_SUBSCRIPTIONS);
+
+ public static final List<MetaItemDescriptor> PROTOCOL_META_ITEM_DESCRIPTORS = Arrays.asList(
+ META_PROTOCOL_CONNECT_URI,
+ META_PROTOCOL_CONNECT_HEADERS,
+ META_PROTOCOL_USERNAME,
+ META_PROTOCOL_PASSWORD,
+ META_PROTOCOL_OAUTH_GRANT,
+ META_SUBSCRIPTIONS);
+
+ public static final int CONNECTED_SEND_DELAY_MILLIS = 2000;
+ protected ResteasyClient client;
+ protected final Map<AttributeRef, List<Runnable>> protocolConnectedTasks = new HashMap<>();
+ protected final Map<AttributeRef, Map<AttributeRef, Runnable>> attributeConnectedTasks = new HashMap<>();
+ protected Map<AttributeRef, MultivaluedMap<String, String>> clientHeaders = new HashMap<>();
+
+ @Override
+ public void init(Container container) throws Exception {
+ super.init(container);
+ client = createClient(executorService);
+ }
+
+ @Override
+ protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ super.doUnlinkProtocolConfiguration(protocolConfiguration);
+ AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
+ clientHeaders.remove(protocolRef);
+ synchronized (protocolConnectedTasks) {
+ protocolConnectedTasks.remove(protocolRef);
+ }
+ synchronized (attributeConnectedTasks) {
+ attributeConnectedTasks.remove(protocolRef);
+ }
+ }
+
+ @Override
+ protected WebsocketIoClient<T> createIoClient(AssetAttribute protocolConfiguration) throws Exception {
+ final AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
+
+ String uriStr = protocolConfiguration.getMetaItem(META_PROTOCOL_CONNECT_URI)
+ .flatMap(AbstractValueHolder::getValueAsString).orElseThrow(() ->
+ new IllegalArgumentException("Missing or invalid required meta item: " + META_PROTOCOL_CONNECT_URI));
+
+ URI uri = new URI(uriStr);
+
+ /* We're going to fail hard and fast if optional meta items are incorrectly configured */
+
+ Optional<OAuthGrant> oAuthGrant = Protocol.getOAuthGrant(protocolConfiguration);
+ Optional<Pair<StringValue, StringValue>> usernameAndPassword = getUsernameAndPassword(protocolConfiguration);
+
+ MultivaluedMap<String, String> headers = Values.getMetaItemValueOrThrow(
+ protocolConfiguration,
+ META_PROTOCOL_CONNECT_HEADERS,
+ false,
+ true)
+ .flatMap(Values::getObject)
+ .flatMap(objectValue -> getMultivaluedMap(objectValue, true))
+ .orElse(null);
+
+ Optional<WebsocketSubscription[]> subscriptions = getSubscriptions(protocolConfiguration);
+
+ if (!oAuthGrant.isPresent() && usernameAndPassword.isPresent()) {
+ String authValue = BasicAuthHelper.createHeader(usernameAndPassword.get().key.getString(), usernameAndPassword.get().value.getString());
+ if (headers == null) {
+ headers = new MultivaluedHashMap<>();
+ headers.add(HttpHeaders.AUTHORIZATION, authValue);
+ } else {
+ headers.remove(HttpHeaders.AUTHORIZATION);
+ headers.replace(HttpHeaders.AUTHORIZATION, Collections.singletonList(authValue));
+ }
+ }
+
+ WebsocketIoClient<T> websocketClient = new WebsocketIoClient<>(uri, headers, oAuthGrant.orElse(null), executorService);
+
+ MultivaluedMap<String, String> finalHeaders = headers;
+ clientHeaders.put(protocolRef, headers);
+ subscriptions.ifPresent(websocketSubscriptions ->
+ addProtocolConnectedTask(protocolRef, () -> doSubscriptions(protocolRef, websocketClient, finalHeaders, websocketSubscriptions))
+ );
+
+ return websocketClient;
+ }
+
+ @Override
+ protected void onConnectionStatusChanged(AttributeRef protocolRef, ConnectionStatus connectionStatus) {
+ super.onConnectionStatusChanged(protocolRef, connectionStatus);
+ if (connectionStatus == ConnectionStatus.CONNECTED) {
+ onConnected(protocolRef);
+ }
+ }
+
+ @Override
+ protected void doLinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
+ AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
+ ProtocolIoClient<T, WebsocketIoClient<T>> protocolClient = protocolIoClientMap.get(protocolRef);
+ WebsocketIoClient client = protocolClient.client;
+ Optional<WebsocketSubscription[]> subscriptions = getSubscriptions(attribute);
+
+ subscriptions.ifPresent(websocketSubscriptions -> {
+ MultivaluedMap<String, String> headers = clientHeaders.get(protocolRef);
+ Runnable task = () -> doSubscriptions(protocolRef, client, headers, websocketSubscriptions);
+ addAttributeConnectedTask(protocolRef, attribute.getReferenceOrThrow(), task);
+ if (client.getConnectionStatus() == ConnectionStatus.CONNECTED) {
+ executorService.schedule(task, 1000);
+ }
+ });
+ }
+
+ @Override
+ protected void doUnlinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
+ AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
+ AttributeRef attributeRef = attribute.getReferenceOrThrow();
+
+ synchronized (attributeConnectedTasks) {
+ attributeConnectedTasks.compute(protocolRef, (ref, tasks) -> {
+ if (tasks != null) {
+ tasks.remove(attributeRef);
+ }
+ return tasks;
+ });
+ }
+ }
+
+ protected void onConnected(AttributeRef protocolRef) {
+ // Look for any subscriptions that need to be processed
+ List<Runnable> protocolTasks;
+ Map<AttributeRef, Runnable> attributeTasks;
+
+ synchronized (protocolConnectedTasks) {
+ protocolTasks = protocolConnectedTasks.get(protocolRef);
+ }
+ if (protocolTasks != null) {
+ // Execute after a delay to ensure connection is properly initialised
+ executorService.schedule(() -> protocolTasks.forEach(Runnable::run), CONNECTED_SEND_DELAY_MILLIS);
+ }
+
+ synchronized (attributeConnectedTasks) {
+ attributeTasks = attributeConnectedTasks.get(protocolRef);
+ }
+ if (attributeTasks != null) {
+ // Execute after a delay to ensure connection is properly initialised
+ executorService.schedule(() -> attributeTasks.forEach((ref, task) -> task.run()), CONNECTED_SEND_DELAY_MILLIS);
+ }
+ }
+
+ protected void addProtocolConnectedTask(AttributeRef protocolRef, Runnable task) {
+ synchronized (protocolConnectedTasks) {
+ protocolConnectedTasks.compute(protocolRef, (ref, tasks) -> {
+ if (tasks == null) {
+ tasks = new ArrayList<>();
+ }
+ tasks.add(task);
+ return tasks;
+ });
+ }
+ }
+
+ protected void addAttributeConnectedTask(AttributeRef protocolRef, AttributeRef attributeRef, Runnable task) {
+ synchronized (attributeConnectedTasks) {
+ attributeConnectedTasks.compute(protocolRef, (ref, tasks) -> {
+ if (tasks == null) {
+ tasks = new HashMap<>();
+ }
+ tasks.put(attributeRef, task);
+ return tasks;
+ });
+ }
+ }
+
+ protected void doSubscriptions(AttributeRef protocolRef, WebsocketIoClient websocketClient, MultivaluedMap<String, String> headers, WebsocketSubscription[] subscriptions) {
+ LOG.info("Executing subscriptions for websocket: " + websocketClient.getClientUri());
+
+ // Inject OAuth header
+ if (!TextUtil.isNullOrEmpty(websocketClient.authHeaderValue)) {
+ if (headers == null) {
+ headers = new MultivaluedHashMap<>();
+ }
+ headers.remove(HttpHeaders.AUTHORIZATION);
+ headers.add(HttpHeaders.AUTHORIZATION, websocketClient.authHeaderValue);
+ }
+
+ MultivaluedMap<String, String> finalHeaders = headers;
+ Arrays.stream(subscriptions).forEach(
+ subscription -> doSubscription(websocketClient, finalHeaders, subscription)
+ );
+ }
+
+ protected void doSubscription(WebsocketIoClient websocketClient, MultivaluedMap<String, String> headers, WebsocketSubscription subscription) {
+ if (subscription instanceof WebsocketHttpSubscription) {
+ WebsocketHttpSubscription httpSubscription = (WebsocketHttpSubscription)subscription;
+
+ if (TextUtil.isNullOrEmpty(httpSubscription.uri)) {
+ LOG.warning("Websocket subscription missing or empty URI so skipping: " + subscription);
+ return;
+ }
+
+ URI uri;
+
+ try {
+ uri = new URI(httpSubscription.uri);
+ } catch (URISyntaxException e) {
+ LOG.warning("Websocket subscription invalid URI so skipping: " + subscription);
+ return;
+ }
+
+ if (httpSubscription.method == null) {
+ httpSubscription.method = WebsocketHttpSubscription.Method.valueOf(DEFAULT_HTTP_METHOD);
+ }
+
+ if (TextUtil.isNullOrEmpty(httpSubscription.contentType)) {
+ httpSubscription.contentType = DEFAULT_CONTENT_TYPE;
+ }
+
+ if (httpSubscription.headers != null) {
+ headers = headers != null ? new MultivaluedHashMap<String, String>(headers) : new MultivaluedHashMap<>();
+ @SuppressWarnings("OptionalGetWithoutIsPresent")
+ MultivaluedMap<String, String> subscriptionHeaders = getMultivaluedMap(httpSubscription.headers, false).get();
+ MultivaluedMap<String, String> finalHeaders = headers;
+ subscriptionHeaders.forEach((header, values) -> {
+ if (values == null || values.isEmpty()) {
+ finalHeaders.remove(header);
+ } else {
+ finalHeaders.addAll(header, values);
+ }
+ });
+ }
+
+ WebTargetBuilder webTargetBuilder = new WebTargetBuilder(client, uri);
+
+ if (headers != null) {
+ webTargetBuilder.setInjectHeaders(headers);
+ }
+
+ LOG.fine("Creating web target client for subscription '" + uri + "'");
+ ResteasyWebTarget target = webTargetBuilder.build();
+
+ Invocation invocation;
+
+ if (httpSubscription.body == null) {
+ invocation = target.request().build(httpSubscription.method.toString());
+ } else {
+ invocation = target.request().build(httpSubscription.method.toString(), Entity.entity(httpSubscription.body, httpSubscription.contentType));
+ }
+ Response response = invocation.invoke();
+ if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
+ LOG.warning("WebsocketHttpSubscription returned an un-successful response code: " + response.getStatus());
+ }
+ } else {
+ websocketClient.sendMessage(subscription.body);
+ }
+ }
+
+ protected static Optional<WebsocketSubscription[]> getSubscriptions(Attribute attribute) {
+ return Values.getMetaItemValueOrThrow(
+ attribute,
+ META_SUBSCRIPTIONS,
+ false,
+ true)
+ .flatMap(Values::getArray)
+ .map(arrValue -> {
+ try {
+ return Container.JSON.readValue(arrValue.toJson(), WebsocketSubscription[].class);
+ } catch (IOException e) {
+ LOG.log(Level.WARNING, "Failed to deserialize WebsocketSubscription[]", e);
+ return null;
+ }
+ });
+ }
+}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketClientProtocol.java
index 3e4e56113..d62cc8d4c 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketClientProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketClientProtocol.java
@@ -19,428 +19,54 @@
*/
package org.openremote.agent.protocol.websocket;
-import org.apache.http.HttpHeaders;
-import org.jboss.resteasy.client.jaxrs.ResteasyClient;
-import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
-import org.jboss.resteasy.util.BasicAuthHelper;
-import org.openremote.agent.protocol.AbstractProtocol;
+import io.netty.channel.ChannelHandler;
import org.openremote.agent.protocol.Protocol;
-import org.openremote.agent.protocol.http.HttpClientProtocol;
-import org.openremote.agent.protocol.http.OAuthGrant;
-import org.openremote.agent.protocol.http.WebTargetBuilder;
-import org.openremote.container.Container;
-import org.openremote.model.AbstractValueHolder;
import org.openremote.model.ValidationFailure;
import org.openremote.model.ValueHolder;
import org.openremote.model.asset.AssetAttribute;
-import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.asset.agent.ProtocolConfiguration;
import org.openremote.model.attribute.*;
-import org.openremote.model.query.AssetQuery;
-import org.openremote.model.query.filter.StringPredicate;
import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.Pair;
-import org.openremote.model.util.TextUtil;
-import org.openremote.model.value.*;
+import org.openremote.model.value.Value;
+import org.openremote.model.value.Values;
-import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.Invocation;
-import javax.ws.rs.core.MultivaluedHashMap;
-import javax.ws.rs.core.MultivaluedMap;
-import javax.ws.rs.core.Response;
-import java.io.IOException;
-import java.net.URI;
-import java.net.URISyntaxException;
import java.util.*;
import java.util.function.Consumer;
-import java.util.logging.Level;
+import java.util.function.Supplier;
import java.util.logging.Logger;
-import static org.openremote.agent.protocol.http.HttpClientProtocol.*;
-import static org.openremote.agent.protocol.http.WebTargetBuilder.createClient;
+import static org.openremote.agent.protocol.http.HttpClientProtocol.getUsernameAndPassword;
+import static org.openremote.container.util.Util.joinCollections;
import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
-import static org.openremote.model.attribute.MetaItemDescriptor.Access.ACCESS_PRIVATE;
-import static org.openremote.model.attribute.MetaItemDescriptorImpl.*;
import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
-import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_STRING_NON_EMPTY;
/**
+ * This is a generic implementation of {@link AbstractWebsocketClientProtocol} for communicating with a Websocket server
+ * using {@link String} based messages; messages coming from the server are written through to {@link Attribute}s by using
+ * the {@link #META_ATTRIBUTE_MATCH_FILTERS} and {@link #META_ATTRIBUTE_MATCH_PREDICATE} {@link MetaItem}s.
* <p>
- * This is a Websocket client protocol for communicating with Websocket servers; it uses the {@link WebsocketClient}
- * to handle the communication, but also uses the {@link WebTargetBuilder} factory to send
- * {@link WebsocketHttpSubscription}s when the connection is initially established.
- * <h1>Protocol Configurations</h1>
+ * For supported {@link ProtocolConfiguration} {@link MetaItem}s see {@link #PROTOCOL_META_ITEM_DESCRIPTORS}
* <p>
- * {@link Attribute}s that are configured as {@link ProtocolConfiguration}s for this protocol support the meta
- * items defined in {@link #PROTOCOL_META_ITEM_DESCRIPTORS}.
- * <h1>Linked Attributes</h1>
- * <p>
- * {@link Attribute}s that are linked to this protocol using an {@link MetaItemType#AGENT_LINK} {@link MetaItem} support
- * the meta items defined in {@link #ATTRIBUTE_META_ITEM_DESCRIPTORS}.
- * <h1>Protocol -> Attribute</h1>
- * <p>
- * When a new value comes from the protocol destined for a linked {@link Attribute} the actual value written to the
- * attribute can be filtered in the standard way using {@link ValueFilter}s via the{@link Protocol#META_ATTRIBUTE_VALUE_FILTERS}
- * {@link MetaItem}.
- * <h1>Attribute -> Protocol</h1>
- * <p>
- * When a linked {@link Attribute} is written to, the actual value written to the protocol can either be the exact value
- * written to the linked {@link Attribute} or the {@link Protocol#META_ATTRIBUTE_WRITE_VALUE} {@link MetaItem} can be
- * used to inject the written value into a bigger payload using the {@link Protocol#DYNAMIC_VALUE_PLACEHOLDER} and then
- * this bigger payload will be written to the protocol.
- * <h1>Executable Attributes</h1>
- * When a linked {@link Attribute} that has an {@link MetaItemType#EXECUTABLE} {@link MetaItem} is executed the
- * {@link Value} stored in the {@link Protocol#META_ATTRIBUTE_WRITE_VALUE} {@link MetaItem} is actually written to the
- * protocol (note dynamic value injection doesn't work in this scenario as there is no dynamic value to inject).
- * <p>
- * <h1>Protocol Specifics</h1>
- * When the websocket connection is established it is possible to subscribe to events by specifying the
- * {@link #META_SUBSCRIPTIONS} {@link MetaItem}, a subscription can be either a message sent over the websocket
- * or a HTTP REST API call.
- * <p>
- * For a linked {@link Attribute} to display a value from the protocol the {@link #META_ATTRIBUTE_MESSAGE_MATCH_FILTERS}
- * and {@link #META_ATTRIBUTE_MESSAGE_MATCH_PREDICATE} {@link MetaItem}s must be specified these allow incoming messages to
- * be matched to the attribute.
- * <p>
- *
+ * For supported linked {@link Attribute} {@link MetaItem}s see {@link #ATTRIBUTE_META_ITEM_DESCRIPTORS}
*/
-public class WebsocketClientProtocol extends AbstractProtocol {
+public class WebsocketClientProtocol extends AbstractWebsocketClientProtocol<String> {
public static final String PROTOCOL_NAME = PROTOCOL_NAMESPACE + ":websocketClient";
public static final String PROTOCOL_DISPLAY_NAME = "Websocket Client";
public static final String PROTOCOL_VERSION = "1.0";
- private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, WebsocketClientProtocol.class.getName());
-
- /*--------------- META ITEMS TO BE USED ON PROTOCOL CONFIGURATIONS ---------------*/
-
- /**
- * Websocket connect endpoint URI
- */
- public static final MetaItemDescriptor META_PROTOCOL_CONNECT_URI = metaItemString(
- PROTOCOL_NAME + ":uri",
- ACCESS_PRIVATE,
- true,
- REGEXP_PATTERN_STRING_NON_EMPTY,
- PatternFailure.STRING_EMPTY
- );
-
- /**
- * Headers for websocket connect call (see {@link HttpClientProtocol#META_HEADERS} for details)
- */
- public static final MetaItemDescriptor META_PROTOCOL_CONNECT_HEADERS = metaItemObject(
- PROTOCOL_NAME + ":headers",
- ACCESS_PRIVATE,
- false,
- null);
-
- /**
- * Array of {@link WebsocketSubscription}s that should be executed either once the websocket connection is
- * established (when used on a linked {@link ProtocolConfiguration}) or when a linked attribute is linked (when
- * used on a linked {@link AssetAttribute}); the subscriptions are executed in the order specified in the array
- */
- public static final MetaItemDescriptor META_SUBSCRIPTIONS = metaItemArray(
- PROTOCOL_NAME + ":subscriptions",
- ACCESS_PRIVATE,
- false,
- null);
-
- /*--------------- META ITEMS TO BE USED ON LINKED ATTRIBUTES ---------------*/
-
- /**
- * {@link ValueFilter}s to apply to incoming messages with the final result being compared to the
- * {@link #META_ATTRIBUTE_MESSAGE_MATCH_PREDICATE}, if they are equal (e.g. {@link String#equals}) then the message
- * is intended for this linked attribute and the message can be once again filtered using the
- * {@link Protocol#META_ATTRIBUTE_VALUE_FILTERS} {@link MetaItem}. The {@link MetaItem} value should be an {@link ArrayValue}
- * of {@link ObjectValue}s where each {@link ObjectValue} represents a serialised {@link ValueFilter}. The message
- * will pass through the filters in array order
- */
- public static final MetaItemDescriptor META_ATTRIBUTE_MESSAGE_MATCH_FILTERS = metaItemArray(
- PROTOCOL_NAME + ":messageMatchFilters",
- ACCESS_PRIVATE,
- false,
- null);
-
- /**
- * The predicate to use on filtered message to determine if the message is intended for this
- * linked attribute
- */
- public static final MetaItemDescriptor META_ATTRIBUTE_MESSAGE_MATCH_PREDICATE = metaItemObject(
- PROTOCOL_NAME + ":messageMatchPredicate",
- ACCESS_PRIVATE,
- false,
- new StringPredicate(AssetQuery.Match.EXACT, false, "").toModelValue());
-
- public static final List<MetaItemDescriptor> ATTRIBUTE_META_ITEM_DESCRIPTORS = Arrays.asList(
- META_ATTRIBUTE_MESSAGE_MATCH_FILTERS,
- META_ATTRIBUTE_MESSAGE_MATCH_PREDICATE,
- META_ATTRIBUTE_WRITE_VALUE,
- META_SUBSCRIPTIONS);
-
- public static final List<MetaItemDescriptor> PROTOCOL_META_ITEM_DESCRIPTORS = Arrays.asList(
- META_PROTOCOL_CONNECT_URI,
- META_PROTOCOL_CONNECT_HEADERS,
- META_PROTOCOL_USERNAME,
- META_PROTOCOL_PASSWORD,
- META_PROTOCOL_OAUTH_GRANT,
- META_SUBSCRIPTIONS);
-
- public static final int CONNECTED_SEND_DELAY_MILLIS = 2000;
- protected ResteasyClient client;
- protected Map<AttributeRef, WebsocketClient> websocketClients = new HashMap<>();
- protected Map<AttributeRef, List<Pair<AttributeRef, Consumer<String>>>> protocolMessageConsumers = new HashMap<>();
- protected final Map<AttributeRef, List<Runnable>> protocolConnectedTasks = new HashMap<>();
- protected Map<AttributeRef, MultivaluedMap<String, String>> clientHeaders = new HashMap<>();
- protected Map<AttributeRef, Consumer<Value>> attributeWriteConsumers = new HashMap<>();
-
- @Override
- public void init(Container container) throws Exception {
- super.init(container);
- client = createClient(executorService);
- }
-
- @Override
- protected void doStop(Container container) {
- websocketClients.forEach((ref, client) -> client.disconnect());
- }
-
- @Override
- protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
- final AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
-
- if (!protocolConfiguration.isEnabled()) {
- updateStatus(protocolRef, ConnectionStatus.DISABLED);
- return;
- }
-
- String uriStr = protocolConfiguration.getMetaItem(META_PROTOCOL_CONNECT_URI)
- .flatMap(AbstractValueHolder::getValueAsString).orElseThrow(() ->
- new IllegalArgumentException("Missing or invalid required meta item: " + META_PROTOCOL_CONNECT_URI));
-
- URI uri;
-
- try {
- uri = new URI(uriStr);
- } catch (URISyntaxException e) {
- LOG.log(Level.WARNING, "Missing or invalid required meta item: " + META_PROTOCOL_CONNECT_URI, e);
- updateStatus(protocolRef, ConnectionStatus.ERROR_CONFIGURATION);
- return;
- }
-
- /* We're going to fail hard and fast if optional meta items are incorrectly configured */
-
- Optional<OAuthGrant> oAuthGrant = Protocol.getOAuthGrant(protocolConfiguration);
- Optional<Pair<StringValue, StringValue>> usernameAndPassword = getUsernameAndPassword(protocolConfiguration);
-
- MultivaluedMap<String, String> headers = Values.getMetaItemValueOrThrow(
- protocolConfiguration,
- META_PROTOCOL_CONNECT_HEADERS,
- false,
- true)
- .flatMap(Values::getObject)
- .flatMap(objectValue -> getMultivaluedMap(objectValue, true))
- .orElse(null);
-
- Optional<WebsocketSubscription[]> subscriptions = Values.getMetaItemValueOrThrow(
- protocolConfiguration,
- META_SUBSCRIPTIONS,
- false,
- true)
- .flatMap(Values::getArray)
- .map(arrValue -> {
- try {
- return Container.JSON.readValue(arrValue.toJson(), WebsocketSubscription[].class);
- } catch (IOException e) {
- LOG.log(Level.WARNING, "Failed to deserialize WebsocketSubscription[]", e);
- return null;
- }
- });
-
- if (!oAuthGrant.isPresent() && usernameAndPassword.isPresent()) {
- String authValue = BasicAuthHelper.createHeader(usernameAndPassword.get().key.getString(), usernameAndPassword.get().value.getString());
- if (headers == null) {
- headers = new MultivaluedHashMap<>();
- headers.add(HttpHeaders.AUTHORIZATION, authValue);
- } else {
- headers.remove(HttpHeaders.AUTHORIZATION);
- headers.replace(HttpHeaders.AUTHORIZATION, Collections.singletonList(authValue));
- }
- }
-
- try {
- WebsocketClient websocketClient = new WebsocketClient(uri, headers, oAuthGrant.orElse(null), executorService);
- websocketClient.addConnectionStatusConsumer(connectionStatus -> this.onClientConnectionStatusChanged(protocolRef, connectionStatus));
- websocketClient.addMessageConsumer(msg -> this.onClientMessage(protocolRef, msg));
- websocketClients.put(protocolRef, websocketClient);
-
- MultivaluedMap<String, String> finalHeaders = headers;
- clientHeaders.put(protocolRef, headers);
- subscriptions.ifPresent(websocketSubscriptions ->
- addConnectedTask(protocolRef, () -> doSubscriptions(protocolRef, websocketClient, finalHeaders, websocketSubscriptions))
- );
-
- websocketClient.connect();
- } catch (Exception e) {
- LOG.log(Level.WARNING, "Failed to establish websocket connection, protocol is unusable", e);
- updateStatus(protocolRef, ConnectionStatus.ERROR);
- }
- }
-
- @Override
- protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
- AttributeRef protocolConfigurationRef = protocolConfiguration.getReferenceOrThrow();
- WebsocketClient client = websocketClients.remove(protocolConfigurationRef);
- if (client != null) {
- client.removeAllMessageConsumers();
- client.removeAllConnectionStatusConsumers();
- client.disconnect();
- }
- clientHeaders.remove(protocolConfigurationRef);
- protocolMessageConsumers.remove(protocolConfigurationRef);
- protocolConnectedTasks.remove(protocolConfigurationRef);
- }
-
- @Override
- protected void doLinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
-
- AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
- WebsocketClient client = websocketClients.get(protocolRef);
-
- if (client == null) {
- LOG.warning("Linked attribute protocol configuration is not valid or disabled so cannot link attribute");
- return;
- }
-
- ValueFilter[] filters = Values.getMetaItemValueOrThrow(
- attribute,
- META_ATTRIBUTE_MESSAGE_MATCH_FILTERS,
- false,
- true)
- .map(Value::toJson)
- .map(json -> {
- try {
- return Container.JSON.readValue(json, ValueFilter[].class);
- } catch (IOException e) {
- LOG.log(Level.WARNING, "Failed to deserialize ValueFilter[]", e);
- return null;
- }
- }).orElse(null);
-
- StringPredicate matchPredicate = Values.getMetaItemValueOrThrow(
- attribute,
- META_ATTRIBUTE_MESSAGE_MATCH_PREDICATE,
- false,
- true)
- .map(Value::toJson)
- .map(s -> {
- try {
- return Container.JSON.readValue(s, StringPredicate.class);
- } catch (IOException e) {
- LOG.log(Level.WARNING, "Failed to deserialise StringPredicate", e);
- return null;
- }
- })
- .orElse(null);
-
- Optional<WebsocketSubscription[]> subscriptions = Values.getMetaItemValueOrThrow(
- attribute,
- META_SUBSCRIPTIONS,
- false,
- true)
- .flatMap(Values::getArray)
- .map(arrValue -> {
- try {
- return Container.JSON.readValue(arrValue.toJson(), WebsocketSubscription[].class);
- } catch (IOException e) {
- LOG.log(Level.WARNING, "Failed to deserialize WebsocketSubscription[]", e);
- return null;
- }
- });
-
- boolean hasFilters = filters != null || matchPredicate != null;
-
- if (!attribute.isReadOnly()) {
- Consumer<Value> writeConsumer = Protocol.createDynamicAttributeWriteConsumer(attribute, str -> {
- LOG.fine("Sending message to websocket client: " + client.getSocketAddressString());
- client.sendMessage(str);
- });
- attributeWriteConsumers.put(attribute.getReferenceOrThrow(), writeConsumer);
- } else if (!hasFilters) {
- LOG.warning("Readonly attribute doesn't have any message filters and will therefore display nothing: " + attribute.getReferenceOrThrow());
- }
-
- if (hasFilters && matchPredicate == null) {
- LOG.warning("Invalid or missing '" + META_ATTRIBUTE_MESSAGE_MATCH_PREDICATE.getUrn() + "': " + attribute.getReferenceOrThrow());
- return;
- }
-
- if (hasFilters) {
- protocolMessageConsumers.compute(protocolRef, (ref, consumers) -> {
- if (consumers == null) {
- consumers = new ArrayList<>();
- }
- consumers.add(new Pair<>(
- attribute.getReferenceOrThrow(),
- createAttributeWebsocketMessageConsumer(attribute.getReferenceOrThrow(), filters, matchPredicate)));
- return consumers;
- });
- }
-
- subscriptions.ifPresent(websocketSubscriptions -> {
- MultivaluedMap<String, String> headers = clientHeaders.get(protocolRef);
- Runnable task = () -> doSubscriptions(protocolRef, client, headers, websocketSubscriptions);
- if (client.getConnectionStatus() == ConnectionStatus.CONNECTED) {
- executorService.schedule(task, 1000);
- } else {
- addConnectedTask(protocolRef, task);
- }
- });
- }
-
- @Override
- protected void doUnlinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
- AttributeRef attributeRef = attribute.getReferenceOrThrow();
- attributeWriteConsumers.remove(attributeRef);
- protocolMessageConsumers.compute(protocolConfiguration.getReferenceOrThrow(), (ref, consumers) -> {
- if (consumers != null) {
- consumers.removeIf((attrRefConsumer) -> attrRefConsumer.key.equals(attributeRef));
- }
- return consumers;
- });
- }
-
- @Override
- protected void processLinkedAttributeWrite(AttributeEvent event, AssetAttribute protocolConfiguration) {
-
- Consumer<Value> writeConsumer = attributeWriteConsumers.get(event.getAttributeRef());
-
- if (writeConsumer == null) {
- LOG.fine("Attempt to write to an attribute that doesn't support writes: " + event.getAttributeRef());
- return;
- }
-
- AssetAttribute attribute = getLinkedAttribute(event.getAttributeRef());
- AttributeExecuteStatus status = null;
-
- if (attribute.isExecutable()) {
- status = event.getValue()
- .flatMap(Values::getString)
- .flatMap(AttributeExecuteStatus::fromString)
- .orElse(null);
-
- if (status != null && status != AttributeExecuteStatus.REQUEST_START) {
- LOG.fine("Unsupported execution status: " + status);
- return;
- }
- }
+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, WebsocketClientProtocol.class);
+ protected final Map<AttributeRef, List<Pair<AttributeRef, Consumer<String>>>> protocolMessageConsumers = new HashMap<>();
- Value value = status != null ? null : event.getValue().orElse(null);
- LOG.fine("Attribute write being sent to the websocket client: " + event.getAttributeRef());
- writeConsumer.accept(value);
+ public static final List<MetaItemDescriptor> PROTOCOL_META_ITEM_DESCRIPTORS = joinCollections(
+ AbstractWebsocketClientProtocol.PROTOCOL_META_ITEM_DESCRIPTORS,
+ PROTOCOL_GENERIC_META_ITEM_DESCRIPTORS);
- if (status != null) {
- updateLinkedAttribute(new AttributeState(event.getAttributeRef(), AttributeExecuteStatus.COMPLETED.asValue()));
- }
- }
+ public static final List<MetaItemDescriptor> ATTRIBUTE_META_ITEM_DESCRIPTORS = joinCollections(
+ Arrays.asList(
+ META_ATTRIBUTE_MATCH_FILTERS,
+ META_ATTRIBUTE_MATCH_PREDICATE),
+ AbstractWebsocketClientProtocol.ATTRIBUTE_META_ITEM_DESCRIPTORS);
@Override
public String getProtocolName() {
@@ -491,146 +117,92 @@ public class WebsocketClientProtocol extends AbstractProtocol {
return result;
}
- protected void onClientMessage(AttributeRef protocolRef, String msg) {
- List<Pair<AttributeRef, Consumer<String>>> consumers = protocolMessageConsumers.get(protocolRef);
- if (consumers != null) {
- consumers.forEach(c -> {
- if (c.value != null) {
- c.value.accept(msg);
- }
- });
+ @Override
+ protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ synchronized (protocolMessageConsumers) {
+ protocolMessageConsumers.remove(protocolConfiguration.getReferenceOrThrow());
}
+ super.doUnlinkProtocolConfiguration(protocolConfiguration);
}
- protected void onClientConnectionStatusChanged(AttributeRef protocolRef, ConnectionStatus connectionStatus) {
- updateStatus(protocolRef, connectionStatus);
-
- if (connectionStatus == ConnectionStatus.CONNECTED) {
- onConnected(protocolRef);
- }
- }
+ @Override
+ protected void doLinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
+ super.doLinkAttribute(attribute, protocolConfiguration);
- protected void onConnected(AttributeRef protocolRef) {
- // Look for any subscriptions that need to be processed
- List<Runnable> connectedTasks;
+ AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
+ Consumer<String> messageConsumer = Protocol.createGenericAttributeMessageConsumer(attribute, assetService, this::updateLinkedAttribute);
- synchronized (protocolConnectedTasks) {
- connectedTasks = protocolConnectedTasks.remove(protocolRef);
- }
- if (connectedTasks != null) {
- // Execute after a delay to ensure connection is properly initialised
- executorService.schedule(() -> connectedTasks.forEach(Runnable::run), CONNECTED_SEND_DELAY_MILLIS);
+ if (messageConsumer != null) {
+ synchronized (protocolMessageConsumers) {
+ protocolMessageConsumers.compute(protocolRef, (ref, consumers) -> {
+ if (consumers == null) {
+ consumers = new ArrayList<>();
+ }
+ consumers.add(new Pair<>(
+ attribute.getReferenceOrThrow(),
+ messageConsumer
+ ));
+ return consumers;
+ });
+ }
}
}
- protected void addConnectedTask(AttributeRef protocolRef, Runnable task) {
- synchronized (protocolConnectedTasks) {
- protocolConnectedTasks.compute(protocolRef, (ref, tasks) -> {
- if (tasks == null) {
- tasks = new ArrayList<>();
+ @Override
+ protected void doUnlinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
+ AttributeRef attributeRef = attribute.getReferenceOrThrow();
+ synchronized (protocolMessageConsumers) {
+ protocolMessageConsumers.compute(protocolConfiguration.getReferenceOrThrow(), (ref, consumers) -> {
+ if (consumers != null) {
+ consumers.removeIf((attrRefConsumer) -> attrRefConsumer.key.equals(attributeRef));
}
- tasks.add(task);
- return tasks;
+ return consumers;
});
}
+ super.doUnlinkAttribute(attribute, protocolConfiguration);
}
- protected void doSubscriptions(AttributeRef protocolRef, WebsocketClient websocketClient, MultivaluedMap<String, String> headers, WebsocketSubscription[] subscriptions) {
- LOG.info("Executing subscriptions for websocket: " + websocketClient.getSocketAddressString());
-
- // Inject OAuth header
- if (!TextUtil.isNullOrEmpty(websocketClient.authHeaderValue)) {
- if (headers == null) {
- headers = new MultivaluedHashMap<>();
- }
- headers.remove(HttpHeaders.AUTHORIZATION);
- headers.add(HttpHeaders.AUTHORIZATION, websocketClient.authHeaderValue);
- }
-
- MultivaluedMap<String, String> finalHeaders = headers;
- Arrays.stream(subscriptions).forEach(
- subscription -> doSubscription(websocketClient, finalHeaders, subscription)
- );
+ @Override
+ protected Supplier<ChannelHandler[]> getEncoderDecoderProvider(WebsocketIoClient<String> client, AssetAttribute protocolConfiguration) {
+ return getGenericStringEncodersAndDecoders(client, protocolConfiguration);
}
- protected void doSubscription(WebsocketClient websocketClient, MultivaluedMap<String, String> headers, WebsocketSubscription subscription) {
- if (subscription instanceof WebsocketHttpSubscription) {
- WebsocketHttpSubscription httpSubscription = (WebsocketHttpSubscription)subscription;
-
- if (TextUtil.isNullOrEmpty(httpSubscription.uri)) {
- LOG.warning("Websocket subscription missing or empty URI so skipping: " + subscription);
- return;
- }
-
- URI uri;
-
- try {
- uri = new URI(httpSubscription.uri);
- } catch (URISyntaxException e) {
- LOG.warning("Websocket subscription invalid URI so skipping: " + subscription);
- return;
- }
-
- if (httpSubscription.method == null) {
- httpSubscription.method = WebsocketHttpSubscription.Method.valueOf(DEFAULT_HTTP_METHOD);
- }
+ @Override
+ protected void onMessageReceived(AttributeRef protocolRef, String message) {
+ List<Pair<AttributeRef, Consumer<String>>> consumers;
- if (TextUtil.isNullOrEmpty(httpSubscription.contentType)) {
- httpSubscription.contentType = DEFAULT_CONTENT_TYPE;
- }
+ synchronized (protocolMessageConsumers) {
+ consumers = protocolMessageConsumers.get(protocolRef);
- if (httpSubscription.headers != null) {
- headers = headers != null ? new MultivaluedHashMap<String, String>(headers) : new MultivaluedHashMap<>();
- @SuppressWarnings("OptionalGetWithoutIsPresent")
- MultivaluedMap<String, String> subscriptionHeaders = getMultivaluedMap(httpSubscription.headers, false).get();
- MultivaluedMap<String, String> finalHeaders = headers;
- subscriptionHeaders.forEach((header, values) -> {
- if (values == null || values.isEmpty()) {
- finalHeaders.remove(header);
- } else {
- finalHeaders.addAll(header, values);
+ if (consumers != null) {
+ consumers.forEach(c -> {
+ if (c.value != null) {
+ c.value.accept(message);
}
});
}
+ }
+ }
- WebTargetBuilder webTargetBuilder = new WebTargetBuilder(client, uri);
-
- if (headers != null) {
- webTargetBuilder.setInjectHeaders(headers);
- }
-
- LOG.fine("Creating web target client for subscription '" + uri + "'");
- ResteasyWebTarget target = webTargetBuilder.build();
+ @Override
+ protected String createWriteMessage(AssetAttribute protocolConfiguration, AssetAttribute attribute, AttributeEvent event, Value processedValue) {
+ if (attribute.isReadOnly()) {
+ LOG.fine("Attempt to write to an attribute that doesn't support writes: " + event.getAttributeRef());
+ return null;
+ }
- Invocation invocation;
+ if (attribute.isExecutable()) {
+ AttributeExecuteStatus status = event.getValue()
+ .flatMap(Values::getString)
+ .flatMap(AttributeExecuteStatus::fromString)
+ .orElse(null);
- if (httpSubscription.body == null) {
- invocation = target.request().build(httpSubscription.method.toString());
- } else {
- invocation = target.request().build(httpSubscription.method.toString(), Entity.entity(httpSubscription.body, httpSubscription.contentType));
- }
- Response response = invocation.invoke();
- if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
- LOG.warning("WebsocketHttpSubscription returned an un-successful response code: " + response.getStatus());
+ if (status != null && status != AttributeExecuteStatus.REQUEST_START) {
+ LOG.fine("Unsupported execution status: " + status);
+ return null;
}
- } else {
- websocketClient.sendMessage(subscription.body);
}
- }
- protected Consumer<String> createAttributeWebsocketMessageConsumer(AttributeRef attributeRef, ValueFilter[] filters, StringPredicate matchPredicate) {
-
- return message -> {
- if (!TextUtil.isNullOrEmpty(message)) {
- StringValue stringValue = Values.create(message);
- Value val = assetService.applyValueFilters(stringValue, filters);
- if (val != null) {
- if (StringPredicate.asPredicate(matchPredicate).test(val.toString())) {
- LOG.fine("Websocket message matches attribute so writing message to the attribute: " + attributeRef);
- updateLinkedAttribute(new AttributeState(attributeRef, stringValue));
- }
- }
- }
- };
+ return processedValue != null ? processedValue.toString() : null;
}
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketClient.java b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketIoClient.java
similarity index 83%
rename from agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketClient.java
rename to agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketIoClient.java
index 87a42633c..dc79f7b34 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketClient.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketIoClient.java
@@ -31,6 +31,7 @@ import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import io.netty.util.CharsetUtil;
+import io.netty.util.ReferenceCountUtil;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.openremote.agent.protocol.ProtocolExecutorService;
import org.openremote.agent.protocol.http.OAuthFilter;
@@ -52,9 +53,17 @@ import static org.openremote.agent.protocol.http.WebTargetBuilder.CONNECTION_TIM
import static org.openremote.agent.protocol.http.WebTargetBuilder.createClient;
import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
-public class WebsocketClient extends AbstractNettyIoClient<String, InetSocketAddress> {
+/**
+ * This is an {@link org.openremote.agent.protocol.io.IoClient} implementation based on {@link AbstractNettyIoClient}.
+ * For custom decoders, the initial message type is of type {@link String}.
+ * For custom encoders, the final message type must be of type {@link String}.
+ */
+public class WebsocketIoClient<T> extends AbstractNettyIoClient<T, InetSocketAddress> {
- public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object> {
+ /**
+ * Extracts the text from the {@link WebSocketFrame} and sends it to the next handler in the pipeline
+ */
+ protected class WebSocketClientHandler extends SimpleChannelInboundHandler<Object> {
private final WebSocketClientHandshaker handshaker;
private ChannelPromise handshakeFuture;
@@ -80,17 +89,18 @@ public class WebsocketClient extends AbstractNettyIoClient<String, InetSocketAdd
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
Channel ch = ctx.channel();
+
if (!handshaker.isHandshakeComplete()) {
try {
handshaker.finishHandshake(ch, (FullHttpResponse) msg);
- synchronized (WebsocketClient.this) {
- LOG.fine("Connected: " + getSocketAddressString());
+ synchronized (WebsocketIoClient.this) {
+ LOG.fine("Connected: " + getClientUri());
onConnectionStatusChanged(ConnectionStatus.CONNECTED);
}
handshakeFuture.setSuccess();
} catch (WebSocketHandshakeException e) {
- LOG.log(Level.SEVERE, "Connection failed: " + getSocketAddressString(), e);
+ LOG.log(Level.SEVERE, "Connection failed: " + getClientUri(), e);
setPermanentError("Connection failed: " + e.getMessage());
handshakeFuture.setFailure(e);
}
@@ -100,13 +110,14 @@ public class WebsocketClient extends AbstractNettyIoClient<String, InetSocketAdd
if (msg instanceof FullHttpResponse) {
FullHttpResponse response = (FullHttpResponse) msg;
LOG.severe("Websocket client unexpected FullHttpResponse (getStatus=" + response.status() +
- ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');
+ ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');
}
WebSocketFrame frame = (WebSocketFrame) msg;
if (frame instanceof TextWebSocketFrame) {
TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
- WebsocketClient.this.onMessageReceived((InetSocketAddress)ch.remoteAddress(), textFrame.text());
+ String str = textFrame.text();
+ ctx.fireChannelRead(str);
} else if (frame instanceof PongWebSocketFrame) {
LOG.finest("Websocket client pong received");
} else if (frame instanceof CloseWebSocketFrame) {
@@ -122,10 +133,11 @@ public class WebsocketClient extends AbstractNettyIoClient<String, InetSocketAdd
handshakeFuture.setFailure(cause);
}
ctx.close();
+ WebsocketIoClient.this.onDecodeException(ctx, cause);
}
}
- private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, WebsocketClient.class);
+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, WebsocketIoClient.class);
protected boolean useSsl;
protected URI uri;
protected SslContext sslCtx;
@@ -136,11 +148,7 @@ public class WebsocketClient extends AbstractNettyIoClient<String, InetSocketAdd
protected String host;
protected int port;
- public WebsocketClient(URI uri, ProtocolExecutorService executorService) {
- this(uri, null, null, executorService);
- }
-
- public WebsocketClient(URI uri, MultivaluedMap<String, String> headers, OAuthGrant oAuthGrant, ProtocolExecutorService executorService) {
+ public WebsocketIoClient(URI uri, MultivaluedMap<String, String> headers, OAuthGrant oAuthGrant, ProtocolExecutorService executorService) {
super(executorService);
this.uri = uri;
@@ -178,7 +186,7 @@ public class WebsocketClient extends AbstractNettyIoClient<String, InetSocketAdd
}
@Override
- protected String getSocketAddressString() {
+ public String getClientUri() {
return uri.toString();
}
@@ -221,7 +229,7 @@ public class WebsocketClient extends AbstractNettyIoClient<String, InetSocketAdd
uri, WebSocketVersion.V13, null, true, hdrs));
} catch (Exception e) {
- LOG.log(Level.SEVERE, "Failed to initialise channel: " + getSocketAddressString(), e);
+ LOG.log(Level.SEVERE, "Failed to initialise channel: " + getClientUri(), e);
setPermanentError(e.getMessage());
return;
}
@@ -230,35 +238,34 @@ public class WebsocketClient extends AbstractNettyIoClient<String, InetSocketAdd
}
@Override
- protected void addDecoders(Channel channel) {
+ protected void addEncodersDecoders(Channel channel) {
+
if (sslCtx != null) {
channel.pipeline().addLast(sslCtx.newHandler(channel.alloc(), host, port));
}
+
channel.pipeline().addLast(
new HttpClientCodec(),
new HttpObjectAggregator(8192),
WebSocketClientCompressionHandler.INSTANCE,
handler);
- }
- @Override
- protected void addEncoders(Channel channel) {
- channel.pipeline().addLast(new MessageToMessageEncoder<String>() {
+ channel.pipeline().addLast(new MessageToMessageEncoder<ByteBuf>() {
@Override
- protected void encode(ChannelHandlerContext ctx, String msg, List<Object> out) throws Exception {
+ protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
out.add(new TextWebSocketFrame(msg));
}
});
- }
- @Override
- protected void decode(ByteBuf buf, List<String> messages) {
- // Not used
- }
+ super.addEncodersDecoders(channel);
- @Override
- protected void encode(String message, ByteBuf buf) {
- // Not used
+ // Put string encoder first (encoders are called in reverse to decoders)
+ channel.pipeline().addLast(new MessageToMessageEncoder<String>() {
+ @Override
+ protected void encode(ChannelHandlerContext ctx, String msg, List<Object> out) throws Exception {
+ out.add(new TextWebSocketFrame(msg));
+ }
+ });
}
@Override
@@ -270,7 +277,7 @@ public class WebsocketClient extends AbstractNettyIoClient<String, InetSocketAdd
}
if (oAuthGrant != null) {
- LOG.fine("Retrieving OAuth access token: " + getSocketAddressString());
+ LOG.fine("Retrieving OAuth access token: " + getClientUri());
ResteasyClient client = createClient(executorService, 1, CONNECTION_TIMEOUT_MILLISECONDS, null);
@@ -281,10 +288,10 @@ public class WebsocketClient extends AbstractNettyIoClient<String, InetSocketAdd
if (TextUtil.isNullOrEmpty(authHeaderValue)) {
throw new Exception("Returned access token is null");
}
- LOG.fine("Retrieved access token via OAuth: " + getSocketAddressString());
+ LOG.fine("Retrieved access token via OAuth: " + getClientUri());
} catch (Exception e) {
- LOG.log(Level.SEVERE, "Failed to retrieve OAuth access token: " + getSocketAddressString(), e);
+ LOG.log(Level.SEVERE, "Failed to retrieve OAuth access token: " + getClientUri(), e);
setPermanentError(e.getMessage());
return;
} finally {
diff --git a/agent/src/main/resources/META-INF/services/org.openremote.container.ContainerService b/agent/src/main/resources/META-INF/services/org.openremote.container.ContainerService
index fb9268cae..5f73737c5 100644
--- a/agent/src/main/resources/META-INF/services/org.openremote.container.ContainerService
+++ b/agent/src/main/resources/META-INF/services/org.openremote.container.ContainerService
@@ -7,4 +7,6 @@ org.openremote.agent.protocol.velbus.VelbusSerialProtocol
org.openremote.agent.protocol.http.HttpClientProtocol
org.openremote.agent.protocol.controller.ControllerProtocol
org.openremote.agent.protocol.udp.UdpClientProtocol
+org.openremote.agent.protocol.tcp.TcpClientProtocol
+org.openremote.agent.protocol.serial.SerialClientProtocol
org.openremote.agent.protocol.websocket.WebsocketClientProtocol
\ No newline at end of file
diff --git a/container/src/main/java/org/openremote/container/util/Util.java b/container/src/main/java/org/openremote/container/util/Util.java
index 1384fdfac..b7147519e 100644
--- a/container/src/main/java/org/openremote/container/util/Util.java
+++ b/container/src/main/java/org/openremote/container/util/Util.java
@@ -9,9 +9,7 @@ import javax.validation.constraints.NotNull;
import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.Optional;
-import java.util.Set;
+import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
@@ -166,4 +164,26 @@ public class Util {
Set<Object> seen = ConcurrentHashMap.newKeySet();
return t -> seen.add(keyExtractor.apply(t));
}
+
+ @SafeVarargs
+ public static <T> List<T> joinCollections(Collection<T>...collections) {
+ if (collections == null || collections.length == 0) {
+ return Collections.emptyList();
+ }
+
+ List<T> newCollection = null;
+
+ for (Collection<T> collection : collections) {
+ if (collection == null) {
+ continue;
+ }
+
+ if (newCollection == null) {
+ newCollection = new ArrayList<>(collection);
+ } else {
+ newCollection.addAll(collection);
+ }
+ }
+ return newCollection;
+ }
}
diff --git a/manager/src/main/java/org/openremote/manager/agent/AgentService.java b/manager/src/main/java/org/openremote/manager/agent/AgentService.java
index c7fc156a3..f46cf5411 100644
--- a/manager/src/main/java/org/openremote/manager/agent/AgentService.java
+++ b/manager/src/main/java/org/openremote/manager/agent/AgentService.java
@@ -769,33 +769,9 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
});
}
- public ConvertedValue applyValueConverter(Value value, ObjectValue converter) {
-
- if (converter == null) {
- return new ConvertedValue(false, value);
- }
-
- String converterKey = value == null ? "@NULL" : value.toString().toUpperCase(Locale.ROOT);
- return converter.get(converterKey)
- .map(v -> {
- if (v.getType() == ValueType.STRING) {
- String valStr = v.toString();
- if (ConvertedValue.IGNORE.equalsIgnoreCase(valStr)) {
- return new ConvertedValue(true, null);
- }
-
- if (ConvertedValue.NULL.equalsIgnoreCase(valStr)) {
- return new ConvertedValue(false, null);
- }
- }
-
- return new ConvertedValue(false, v);
- })
- .orElse(new ConvertedValue(true, value));
- }
-
- @SuppressWarnings("unchecked")
- @Override
+ /**
+ * Apply the specified set of {@link ValueFilter}s to the specified {@link Value}
+ */
public Value applyValueFilters(Value value, ValueFilter... filters) {
if (filters == null) {
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetAttributeLinkingService.java b/manager/src/main/java/org/openremote/manager/asset/AssetAttributeLinkingService.java
index a5205a8e9..af00fa5fd 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetAttributeLinkingService.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetAttributeLinkingService.java
@@ -19,6 +19,7 @@
*/
package org.openremote.manager.asset;
+import org.openremote.agent.protocol.Protocol;
import org.openremote.container.Container;
import org.openremote.container.ContainerService;
import org.openremote.manager.agent.AgentService;
@@ -28,6 +29,7 @@ import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.attribute.*;
import org.openremote.model.attribute.AttributeEvent.Source;
import org.openremote.model.query.AssetQuery;
+import org.openremote.model.util.Pair;
import org.openremote.model.value.*;
import javax.persistence.EntityManager;
@@ -150,19 +152,19 @@ public class AssetAttributeLinkingService implements ContainerService, AssetUpda
}
// Convert the value as required
- ConvertedValue sendConvertedValue = convertValueForLinkedAttribute(
+ Pair<Boolean, Value> sendConvertedValue = convertValueForLinkedAttribute(
em,
assetStorageService,
attributeState.getValue().orElse(null),
attributeLink
);
- if (sendConvertedValue.isIgnore()) {
- LOG.info("Value converter match was '" + ConvertedValue.IGNORE + "' so ignoring value");
+ if (sendConvertedValue.key) {
+ LOG.fine("Value converter matched ignore value");
return;
}
- Value value = sendConvertedValue.getValue();
+ Value value = sendConvertedValue.value;
Optional<AssetAttribute> attribute = getAttribute(em, assetStorageService, attributeLink.getAttributeRef());
if (attribute.isPresent()) {
@@ -187,7 +189,7 @@ public class AssetAttributeLinkingService implements ContainerService, AssetUpda
sendAttributeEvent(new AttributeEvent(attributeLink.getAttributeRef(), value));
}
- protected ConvertedValue convertValueForLinkedAttribute(EntityManager em,
+ protected Pair<Boolean, Value> convertValueForLinkedAttribute(EntityManager em,
AssetStorageService assetStorageService,
Value originalValue,
AttributeLink attributeLink) throws AssetProcessingException {
@@ -202,14 +204,14 @@ public class AssetAttributeLinkingService implements ContainerService, AssetUpda
return attributeLink.getConverter()
.map(
converter -> {
- ConvertedValue converterValue = agentService.applyValueConverter(finalOriginalValue, converter);
+ Pair<Boolean, Value> converterValue = Protocol.applyValueConverter(finalOriginalValue, converter);
- if (converterValue.isIgnore()) {
+ if (converterValue.key) {
return converterValue;
}
// Do special value conversion
- return getSpecialConverter(converterValue.getValue()).map(
+ return getSpecialConverter(converterValue.value).map(
specialConverter -> doSpecialConversion(
em,
assetStorageService,
@@ -218,7 +220,7 @@ public class AssetAttributeLinkingService implements ContainerService, AssetUpda
)
).orElse(converterValue);
})
- .orElse(new ConvertedValue(false, originalValue));
+ .orElse(new Pair<>(false, originalValue));
}
protected static Optional<AttributeLink.ConverterType> getSpecialConverter(Value value) {
@@ -228,7 +230,7 @@ public class AssetAttributeLinkingService implements ContainerService, AssetUpda
return Optional.empty();
}
- protected static ConvertedValue doSpecialConversion(EntityManager em,
+ protected static Pair<Boolean, Value> doSpecialConversion(EntityManager em,
AssetStorageService assetStorageService,
AttributeLink.ConverterType converter,
AttributeRef linkedAttributeRef) throws AssetProcessingException {
@@ -243,10 +245,10 @@ public class AssetAttributeLinkingService implements ContainerService, AssetUpda
"cannot toggle value as attribute is not of type BOOLEAN: " + linkedAttributeRef
);
}
- return new ConvertedValue(false, Values.create(!((BooleanValue) currentValue).getBoolean()));
+ return new Pair<>(false, Values.create(!((BooleanValue) currentValue).getBoolean()));
} catch (NoSuchElementException e) {
LOG.fine("The attribute doesn't exist so ignoring toggle value request: " + linkedAttributeRef);
- return new ConvertedValue(true, null);
+ return new Pair<>(true, null);
}
case INCREMENT:
case DECREMENT:
@@ -260,10 +262,10 @@ public class AssetAttributeLinkingService implements ContainerService, AssetUpda
);
}
int change = converter == AttributeLink.ConverterType.INCREMENT ? +1 : -1;
- return new ConvertedValue(false, Values.create(((NumberValue) currentValue).getNumber() + change));
+ return new Pair<>(false, Values.create(((NumberValue) currentValue).getNumber() + change));
} catch (NoSuchElementException e) {
LOG.fine("The attribute doesn't exist so ignoring increment/decrement value request: " + linkedAttributeRef);
- return new ConvertedValue(true, null);
+ return new Pair<>(true, null);
}
default:
throw new AssetProcessingException(
diff --git a/model/src/main/java/org/openremote/model/asset/AssetAttribute.java b/model/src/main/java/org/openremote/model/asset/AssetAttribute.java
index 66c291133..e6709d38a 100644
--- a/model/src/main/java/org/openremote/model/asset/AssetAttribute.java
+++ b/model/src/main/java/org/openremote/model/asset/AssetAttribute.java
@@ -206,6 +206,13 @@ public class AssetAttribute extends Attribute {
return this;
}
+ public AssetAttribute removeMeta(MetaItemDescriptor metaItemDescriptor) {
+ if (metaItemDescriptor != null) {
+ getMeta().removeIf(metaItem -> metaItem.getName().orElse("").equals(metaItemDescriptor.getUrn()));
+ }
+ return this;
+ }
+
/**
* @return The current value and its timestamp represented as an attribute event.
*/
diff --git a/model/src/main/java/org/openremote/model/value/ConvertedValue.java b/model/src/main/java/org/openremote/model/value/ConvertedValue.java
deleted file mode 100644
index ff894b5e4..000000000
--- a/model/src/main/java/org/openremote/model/value/ConvertedValue.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * 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.value;
-
-public class ConvertedValue {
-
-
- /**
- * Ignore this value (do not send to the linked attribute)
- */
- public static final String IGNORE = "@IGNORE";
-
- /**
- * Send a null value to the linked attribute
- */
- public static final String NULL = "@NULL";
-
- protected boolean ignore;
- protected Value value;
-
- public ConvertedValue(boolean ignore, Value value) {
- this.ignore = ignore;
- this.value = value;
- }
-
- public boolean isIgnore() {
- return ignore;
- }
-
- public Value getValue() {
- return value;
- }
-}
diff --git a/model/src/main/java/org/openremote/model/value/Values.java b/model/src/main/java/org/openremote/model/value/Values.java
index f09e8b78f..54446d5ca 100644
--- a/model/src/main/java/org/openremote/model/value/Values.java
+++ b/model/src/main/java/org/openremote/model/value/Values.java
@@ -37,6 +37,8 @@ import java.util.stream.Stream;
*/
public class Values {
+ public static final String NULL_LITERAL = "null";
+
public static StringValue create(String string) {
return instance().create(string);
}
diff --git a/model/src/main/java/org/openremote/model/value/impl/ValueFactoryImpl.java b/model/src/main/java/org/openremote/model/value/impl/ValueFactoryImpl.java
index 051834835..f17f64b03 100644
--- a/model/src/main/java/org/openremote/model/value/impl/ValueFactoryImpl.java
+++ b/model/src/main/java/org/openremote/model/value/impl/ValueFactoryImpl.java
@@ -19,6 +19,8 @@ import org.openremote.model.value.*;
import java.util.Optional;
+import static org.openremote.model.value.Values.NULL_LITERAL;
+
public class ValueFactoryImpl implements ValueFactory {
public static final ValueFactoryImpl INSTANCE = new ValueFactoryImpl();
@@ -51,9 +53,10 @@ public class ValueFactoryImpl implements ValueFactory {
@Override
public <T extends Value> Optional<T> parse(String jsonString) throws ValueException {
- if (jsonString.isEmpty()) {
+ if (jsonString.isEmpty() || NULL_LITERAL.equals(jsonString)) {
return Optional.empty();
}
+ // TODO: Remove this
if (jsonString.startsWith("(") && jsonString.endsWith(")")) {
// some clients send in (json) expecting an eval is required
jsonString = jsonString.substring(1, jsonString.length() - 1);
diff --git a/model/src/main/java/org/openremote/model/value/impl/ValueTokenizer.java b/model/src/main/java/org/openremote/model/value/impl/ValueTokenizer.java
index f2ee49090..b2b7b3100 100644
--- a/model/src/main/java/org/openremote/model/value/impl/ValueTokenizer.java
+++ b/model/src/main/java/org/openremote/model/value/impl/ValueTokenizer.java
@@ -102,9 +102,6 @@ class ValueTokenizer {
while (true) {
c = next();
switch (c) {
- case '\r':
- case '\n':
- throw new ValueException("");
case INVALID_CHAR:
throw new ValueException("Invalid string: closing " + startChar + " is not found");
case '\\':
diff --git a/test/src/main/groovy/org/openremote/test/MockVelbusClient.java b/test/src/main/groovy/org/openremote/test/MockVelbusClient.java
index 4c7b81d68..686bb27eb 100644
--- a/test/src/main/groovy/org/openremote/test/MockVelbusClient.java
+++ b/test/src/main/groovy/org/openremote/test/MockVelbusClient.java
@@ -19,6 +19,7 @@
*/
package org.openremote.test;
+import io.netty.channel.ChannelHandler;
import org.openremote.agent.protocol.io.IoClient;
import org.openremote.agent.protocol.velbus.VelbusPacket;
import org.openremote.model.asset.agent.ConnectionStatus;
@@ -27,6 +28,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
+import java.util.function.Supplier;
public class MockVelbusClient implements IoClient<VelbusPacket> {
protected final List<Consumer<VelbusPacket>> messageConsumers = new ArrayList<>();
@@ -118,4 +120,14 @@ public class MockVelbusClient implements IoClient<VelbusPacket> {
public void disconnect() {
setConnectionStatus(ConnectionStatus.DISCONNECTED);
}
+
+ @Override
+ public String getClientUri() {
+ return "velbus://mock";
+ }
+
+ @Override
+ public void setEncoderDecoderProvider(Supplier<ChannelHandler[]> encoderDecoderProvider) throws UnsupportedOperationException {
+
+ }
}
diff --git a/test/src/main/groovy/org/openremote/test/protocol/http/TestHttpServerProtocol.java b/test/src/main/groovy/org/openremote/test/protocol/http/TestHttpServerProtocol.java
index 63bbacee1..88a575d8c 100644
--- a/test/src/main/groovy/org/openremote/test/protocol/http/TestHttpServerProtocol.java
+++ b/test/src/main/groovy/org/openremote/test/protocol/http/TestHttpServerProtocol.java
@@ -23,6 +23,7 @@ import org.openremote.agent.protocol.http.AbstractHttpServerProtocol;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.attribute.MetaItemDescriptor;
+import org.openremote.model.value.Value;
import java.util.Collections;
import java.util.HashSet;
@@ -58,7 +59,7 @@ public class TestHttpServerProtocol extends AbstractHttpServerProtocol {
}
@Override
- protected void processLinkedAttributeWrite(AttributeEvent event, AssetAttribute protocolConfiguration) {
+ protected void processLinkedAttributeWrite(AttributeEvent event, Value processedValue, AssetAttribute protocolConfiguration) {
}
@Override
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy
index 4b8237682..ed0aa005a 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy
@@ -10,6 +10,7 @@ import org.openremote.model.asset.AssetAttribute
import org.openremote.model.asset.AssetType
import org.openremote.model.asset.agent.ProtocolConfiguration
import org.openremote.model.attribute.*
+import org.openremote.model.value.Value
import org.openremote.model.value.Values
import org.openremote.test.ManagerContainerTrait
import spock.lang.Specification
@@ -71,7 +72,7 @@ class AssetProcessingTest extends Specification implements ManagerContainerTrait
}
@Override
- protected void processLinkedAttributeWrite(AttributeEvent event, AssetAttribute protocolConfiguration) {
+ protected void processLinkedAttributeWrite(AttributeEvent event, Value processedValue, AssetAttribute protocolConfiguration) {
LOG.info("Mock Protocol: processLinkedAttributeWrite")
sendToActuatorEvents.add(event)
}
diff --git a/test/src/test/groovy/org/openremote/test/event/ClientEventTest.groovy b/test/src/test/groovy/org/openremote/test/event/ClientEventTest.groovy
index 318fd1f0b..f91d2c14c 100644
--- a/test/src/test/groovy/org/openremote/test/event/ClientEventTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/event/ClientEventTest.groovy
@@ -99,7 +99,7 @@ class ClientEventTest extends Specification implements ManagerContainerTrait, Gw
then: "the agent status change event should have been received"
conditions.eventually {
- assert collectedSharedEvents.size() == 4
+ assert collectedSharedEvents.size() == 3
assert collectedSharedEvents[0] instanceof AgentStatusEvent
assert (collectedSharedEvents[0] as AgentStatusEvent).protocolConfiguration.entityId == managerDemoSetup.agentId
assert (collectedSharedEvents[0] as AgentStatusEvent).protocolConfiguration.attributeName == managerDemoSetup.agentProtocolConfigName
@@ -111,11 +111,7 @@ class ClientEventTest extends Specification implements ManagerContainerTrait, Gw
assert collectedSharedEvents[2] instanceof AgentStatusEvent
assert (collectedSharedEvents[2] as AgentStatusEvent).protocolConfiguration.entityId == managerDemoSetup.agentId
assert (collectedSharedEvents[2] as AgentStatusEvent).protocolConfiguration.attributeName == managerDemoSetup.agentProtocolConfigName
- assert (collectedSharedEvents[2] as AgentStatusEvent).connectionStatus == ConnectionStatus.CONNECTING
- assert collectedSharedEvents[3] instanceof AgentStatusEvent
- assert (collectedSharedEvents[3] as AgentStatusEvent).protocolConfiguration.entityId == managerDemoSetup.agentId
- assert (collectedSharedEvents[3] as AgentStatusEvent).protocolConfiguration.attributeName == managerDemoSetup.agentProtocolConfigName
- assert (collectedSharedEvents[3] as AgentStatusEvent).connectionStatus == ConnectionStatus.DISABLED
+ assert (collectedSharedEvents[2] as AgentStatusEvent).connectionStatus == ConnectionStatus.DISABLED
}
when: "the subscription is removed"
diff --git a/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovy
index 95dea792d..669af3474 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovy
@@ -33,6 +33,7 @@ import org.openremote.model.asset.agent.ProtocolConfiguration
import org.openremote.model.attribute.*
import org.openremote.model.value.RegexValueFilter
import org.openremote.model.value.SubStringValueFilter
+import org.openremote.model.value.Value
import org.openremote.model.value.Values
import org.openremote.test.GwtClientTrait
import org.openremote.test.ManagerContainerTrait
@@ -96,10 +97,8 @@ class BasicProtocolTest extends Specification implements ManagerContainerTrait,
updateStatus(protocolConfiguration.getReferenceOrThrow(), ConnectionStatus.ERROR)
} else if(protocolConfiguration.getMetaItem("MOCK_THROW_EXCEPTION").isPresent()) {
throw new IllegalStateException("Exception occurred whilst linking the protocol configuration")
- } else if (protocolConfiguration.isEnabled()) {
- updateStatus(protocolConfiguration.getReferenceOrThrow(), ConnectionStatus.CONNECTED)
} else {
- updateStatus(protocolConfiguration.getReferenceOrThrow(), ConnectionStatus.DISABLED)
+ updateStatus(protocolConfiguration.getReferenceOrThrow(), ConnectionStatus.CONNECTED)
}
}
@@ -142,7 +141,7 @@ class BasicProtocolTest extends Specification implements ManagerContainerTrait,
}
@Override
- protected void processLinkedAttributeWrite(AttributeEvent event, AssetAttribute protocolConfiguration) {
+ protected void processLinkedAttributeWrite(AttributeEvent event, Value processedValue, AssetAttribute protocolConfiguration) {
protocolMethodCalls.add("ATTRIBUTE_WRITE")
if (!(protocolLinkedAttributes[protocolConfiguration.getName().orElse("")])
.any {(it.getReferenceOrThrow() == event.attributeRef)}) {
@@ -199,7 +198,7 @@ class BasicProtocolTest extends Specification implements ManagerContainerTrait,
then: "the protocol configurations should be linked and their deployment status should be available in the agent service"
conditions.eventually {
- assert protocolLinkedConfigurations.size() == 4
+ assert protocolLinkedConfigurations.size() == 3
def config1 = protocolLinkedConfigurations.find { (it.getName().orElse("") == "mockConfig1")}
def config2 = protocolLinkedConfigurations.find { (it.getName().orElse("") == "mockConfig2")}
def config3 = protocolLinkedConfigurations.find { (it.getName().orElse("") == "mockConfig3")}
@@ -207,11 +206,11 @@ class BasicProtocolTest extends Specification implements ManagerContainerTrait,
assert config1 != null
assert config2 != null
assert config3 != null
- assert config4 != null
+ assert config4 == null
assert agentService.getProtocolConnectionStatus(config1.getReferenceOrThrow()) == ConnectionStatus.CONNECTED
assert agentService.getProtocolConnectionStatus(config2.getReferenceOrThrow()) == ConnectionStatus.ERROR
assert agentService.getProtocolConnectionStatus(config3.getReferenceOrThrow()) == ConnectionStatus.ERROR
- assert agentService.getProtocolConnectionStatus(config4.getReferenceOrThrow()) == ConnectionStatus.DISABLED
+ assert agentService.getProtocolConnectionStatus(new AttributeRef(mockAgent.id, "mockConfig4")) == ConnectionStatus.DISABLED
}
when: "a mock thing asset is created that links to the mock protocol configurations"
@@ -349,15 +348,14 @@ class BasicProtocolTest extends Specification implements ManagerContainerTrait,
assert protocolLinkedAttributes['mockConfig1'].size() == protocolExpectedLinkedAttributeCount['mockConfig1']
assert protocolLinkedAttributes['mockConfig2'].size() == protocolExpectedLinkedAttributeCount['mockConfig2']
assert protocolLinkedAttributes['mockConfig3'].size() == protocolExpectedLinkedAttributeCount['mockConfig3']
- assert protocolLinkedAttributes['mockConfig4'].size() == protocolExpectedLinkedAttributeCount['mockConfig4']
+ assert protocolLinkedAttributes['mockConfig4'].size() == 0
}
and: "the deployment should have occurred in the correct order"
- assert protocolMethodCalls.size() == 16
+ assert protocolMethodCalls.size() == 13
assert protocolMethodCalls[0] == "LINK_PROTOCOL"
assert protocolMethodCalls[1] == "LINK_PROTOCOL"
assert protocolMethodCalls[2] == "LINK_PROTOCOL"
- assert protocolMethodCalls[3] == "LINK_PROTOCOL"
assert protocolMethodCalls[4] == "LINK_ATTRIBUTE"
assert protocolMethodCalls[5] == "LINK_ATTRIBUTE"
assert protocolMethodCalls[6] == "LINK_ATTRIBUTE"
@@ -367,9 +365,6 @@ class BasicProtocolTest extends Specification implements ManagerContainerTrait,
assert protocolMethodCalls[10] == "LINK_ATTRIBUTE"
assert protocolMethodCalls[11] == "LINK_ATTRIBUTE"
assert protocolMethodCalls[12] == "LINK_ATTRIBUTE"
- assert protocolMethodCalls[13] == "LINK_ATTRIBUTE"
- assert protocolMethodCalls[14] == "LINK_ATTRIBUTE"
- assert protocolMethodCalls[15] == "LINK_ATTRIBUTE"
and: "the linked attributes values should have been updated by the protocol"
conditions.eventually {
@@ -522,13 +517,10 @@ class BasicProtocolTest extends Specification implements ManagerContainerTrait,
then: "the newly enabled protocol configuration should be unlinked and re-linked"
conditions.eventually {
- assert protocolMethodCalls.size() == 6
- assert protocolMethodCalls[0] == "UNLINK_ATTRIBUTE"
- assert protocolMethodCalls[1] == "UNLINK_ATTRIBUTE"
- assert protocolMethodCalls[2] == "UNLINK_PROTOCOL"
- assert protocolMethodCalls[3] == "LINK_PROTOCOL"
- assert protocolMethodCalls[4] == "LINK_ATTRIBUTE"
- assert protocolMethodCalls[5] == "LINK_ATTRIBUTE"
+ assert protocolMethodCalls.size() == 3
+ assert protocolMethodCalls[0] == "LINK_PROTOCOL"
+ assert protocolMethodCalls[1] == "LINK_ATTRIBUTE"
+ assert protocolMethodCalls[2] == "LINK_ATTRIBUTE"
}
cleanup: "the server should be stopped"
diff --git a/test/src/test/groovy/org/openremote/test/protocol/TcpClientTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/TcpClientTest.groovy
index 3162045e3..5f661f599 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/TcpClientTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/TcpClientTest.groovy
@@ -19,9 +19,12 @@
*/
package org.openremote.test.protocol
-import io.netty.buffer.ByteBuf
+import io.netty.channel.ChannelHandler
+import io.netty.handler.codec.string.StringDecoder
+import io.netty.handler.codec.string.StringEncoder
import io.netty.util.CharsetUtil
-import org.openremote.agent.protocol.tcp.AbstractTcpClient
+import org.openremote.agent.protocol.io.AbstractNettyIoClient
+import org.openremote.agent.protocol.tcp.TcpIoClient
import org.openremote.agent.protocol.tcp.TcpStringServer
import org.openremote.model.asset.agent.ConnectionStatus
import org.openremote.manager.concurrent.ManagerExecutorService
@@ -31,7 +34,7 @@ import spock.lang.Specification
import spock.util.concurrent.PollingConditions
/**
- * This tests the {@link AbstractTcpClient} by creating a simple echo server that the client communicates with
+ * This tests the {@link TcpIoClient} by creating a simple echo server that the client communicates with
*/
class TcpClientTest extends Specification implements ManagerContainerTrait {
@@ -53,24 +56,15 @@ class TcpClientTest extends Specification implements ManagerContainerTrait {
})
and: "a simple TCP client"
- def client = new AbstractTcpClient<String>(
+ TcpIoClient<String> client = new TcpIoClient<String>(
"localhost",
echoServerPort,
- protocolExecutorService) {
-
- @Override
- protected void decode(ByteBuf buf, List<String> messages) {
- ByteBuf bytes = buf.readBytes(buf.readableBytes())
- String msg = bytes.toString(CharsetUtil.UTF_8)
- bytes.release()
- messages.add(msg)
- }
-
- @Override
- protected void encode(String message, ByteBuf buf) {
- buf.writeBytes(message.getBytes(CharsetUtil.UTF_8))
- }
- }
+ protocolExecutorService)
+ client.setEncoderDecoderProvider({
+ [new StringEncoder(CharsetUtil.UTF_8),
+ new StringDecoder(CharsetUtil.UTF_8),
+ new AbstractNettyIoClient.MessageToMessageDecoder<String>(String.class, client)].toArray(new ChannelHandler[0])
+ })
and: "we add callback consumers to the client"
def connectionStatus = client.getConnectionStatus()
diff --git a/test/src/test/groovy/org/openremote/test/protocol/http/HttpClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/http/HttpClientProtocolTest.groovy
index 0b2c64245..1e424f2a9 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/http/HttpClientProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/http/HttpClientProtocolTest.groovy
@@ -400,6 +400,10 @@ class HttpClientProtocolTest extends Specification implements ManagerContainerTr
HttpClientProtocol.META_PROTOCOL_PING_PATH,
Values.create("pingPost")
),
+ new MetaItem(
+ HttpClientProtocol.META_PROTOCOL_PING_CONTENT_TYPE,
+ Values.create(MediaType.APPLICATION_JSON)
+ ),
new MetaItem(
HttpClientProtocol.META_PROTOCOL_PING_MILLIS,
Values.create(100) // Note for testing time unit is set to milliseconds
@@ -464,7 +468,11 @@ class HttpClientProtocolTest extends Specification implements ManagerContainerTr
new MetaItem(HttpClientProtocol.META_ATTRIBUTE_METHOD, Values.create(HttpMethod.PUT)),
new MetaItem(
Protocol.META_ATTRIBUTE_WRITE_VALUE,
- Values.<ObjectValue>parse('{"prop1": "{$value}", "prop2": "prop2Value"}').get()
+ Values.create('{"prop1": {$value}, "prop2": "prop2Value"}')
+ ),
+ new MetaItem(
+ HttpClientProtocol.META_ATTRIBUTE_CONTENT_TYPE,
+ Values.create(MediaType.APPLICATION_JSON)
),
new MetaItem(
HttpClientProtocol.META_HEADERS,
@@ -486,7 +494,7 @@ class HttpClientProtocolTest extends Specification implements ManagerContainerTr
.addMeta(
new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agent.id, "protocolConfig").toArrayValue()),
new MetaItem(HttpClientProtocol.META_ATTRIBUTE_PATH, Values.create("get_poll_slow")),
- new MetaItem(HttpClientProtocol.META_POLLING_MILLIS, Values.create(50)), // This is ms in testing
+ new MetaItem(HttpClientProtocol.META_ATTRIBUTE_POLLING_MILLIS, Values.create(50)), // This is ms in testing
new MetaItem(
Protocol.META_ATTRIBUTE_VALUE_FILTERS,
Values.createArray().add(Util.objectToValue(new RegexValueFilter("\\d+", 0, 0)).get())
@@ -497,7 +505,7 @@ class HttpClientProtocolTest extends Specification implements ManagerContainerTr
.addMeta(
new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agent.id, "protocolConfig").toArrayValue()),
new MetaItem(HttpClientProtocol.META_ATTRIBUTE_PATH, Values.create("get_poll_fast")),
- new MetaItem(HttpClientProtocol.META_POLLING_MILLIS, Values.create(40)), // This is ms in testing
+ new MetaItem(HttpClientProtocol.META_ATTRIBUTE_POLLING_MILLIS, Values.create(40)), // This is ms in testing
new MetaItem(
Protocol.META_ATTRIBUTE_VALUE_FILTERS,
Values.createArray().add(Util.objectToValue(new RegexValueFilter("\\d+", 0, 1)).get())
@@ -531,7 +539,7 @@ class HttpClientProtocolTest extends Specification implements ManagerContainerTr
when: "a linked attribute value is updated"
def attributeEvent = new AttributeEvent(asset.id,
"putRequestWithHeaders",
- Values.<ObjectValue>parse(/{"myProp1": 123,"myProp2": true}/).get())
+ Values.<ObjectValue>parse('{"myProp1": 123,"myProp2": true}').get())
assetProcessingService.sendAttributeEvent(attributeEvent)
then: "the server should have received the request"
@@ -634,7 +642,7 @@ class HttpClientProtocolTest extends Specification implements ManagerContainerTr
.addMeta(
new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agent.id, "protocolConfig3").toArrayValue()),
new MetaItem(HttpClientProtocol.META_ATTRIBUTE_PATH, Values.create("get_failure_401")),
- new MetaItem(HttpClientProtocol.META_POLLING_MILLIS, Values.create(50)), // This is ms in testing
+ new MetaItem(HttpClientProtocol.META_ATTRIBUTE_POLLING_MILLIS, Values.create(50)), // This is ms in testing
),
new AssetAttribute("getSuccess2", AttributeValueType.STRING)
.addMeta(
diff --git a/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientProtocolTest.groovy
index 47d258e01..56a5697fb 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientProtocolTest.groovy
@@ -21,11 +21,11 @@ package org.openremote.test.protocol.udp
import io.netty.buffer.Unpooled
import io.netty.channel.ChannelHandlerContext
-import io.netty.channel.ChannelOutboundHandler
import io.netty.channel.socket.DatagramChannel
import io.netty.handler.codec.FixedLengthFrameDecoder
import io.netty.handler.codec.MessageToMessageEncoder
import io.netty.handler.codec.bytes.ByteArrayDecoder
+import org.openremote.agent.protocol.Protocol
import org.openremote.agent.protocol.ProtocolExecutorService
import org.openremote.agent.protocol.udp.AbstractUdpServer
import org.openremote.agent.protocol.udp.UdpClientProtocol
@@ -39,6 +39,8 @@ import org.openremote.model.asset.AssetAttribute
import org.openremote.model.asset.AssetType
import org.openremote.model.asset.agent.ConnectionStatus
import org.openremote.model.attribute.*
+import org.openremote.model.query.AssetQuery
+import org.openremote.model.query.filter.StringPredicate
import org.openremote.model.value.Values
import org.openremote.test.ManagerContainerTrait
import spock.lang.Specification
@@ -53,9 +55,6 @@ class UdpClientProtocolTest extends Specification implements ManagerContainerTra
given: "expected conditions"
def conditions = new PollingConditions(timeout: 10, delay: 1)
- and: "the UDP client protocol min times are adjusted for testing"
- UdpClientProtocol.MIN_POLLING_MILLIS = 10
-
when: "the container starts"
def serverPort = findEphemeralPort()
def container = startContainerNoDemoImport(defaultConfig(serverPort), defaultServices())
@@ -120,8 +119,11 @@ class UdpClientProtocolTest extends Specification implements ManagerContainerTra
Values.create(clientPort)
),
new MetaItem(
- UdpClientProtocol.META_RESPONSE_TIMEOUT_MILLIS,
- Values.create(200)
+ Protocol.META_PROTOCOL_DELIMITER,
+ Values.create(";")
+ ),
+ new MetaItem(
+ Protocol.META_PROTOCOL_STRIP_DELIMITER
)
)
)
@@ -138,48 +140,39 @@ class UdpClientProtocolTest extends Specification implements ManagerContainerTra
when: "an asset is created with attributes linked to the protocol configuration"
def asset = new Asset("Test Asset", AssetType.THING, agent)
asset.setAttributes(
- // simple send with no retries
new AssetAttribute("echoHello", AttributeValueType.STRING)
.addMeta(
new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agent.id, "protocolConfig").toArrayValue()),
- new MetaItem(UdpClientProtocol.META_ATTRIBUTE_WRITE_VALUE, Values.create('Hello {$value};')),
- new MetaItem(UdpClientProtocol.META_SEND_RETRIES, Values.create(0)),
- new MetaItem(UdpClientProtocol.META_SERVER_ALWAYS_RESPONDS, Values.create(true)),
+ new MetaItem(UdpClientProtocol.META_ATTRIBUTE_WRITE_VALUE, Values.create('"Hello {$value};"')),
new MetaItem(MetaItemType.EXECUTABLE)
),
- // attribute send with 10 retries
new AssetAttribute("echoWorld", AttributeValueType.STRING)
.addMeta(
new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agent.id, "protocolConfig").toArrayValue()),
- new MetaItem(UdpClientProtocol.META_ATTRIBUTE_WRITE_VALUE, Values.create("World;")),
- new MetaItem(UdpClientProtocol.META_SEND_RETRIES, Values.create(10)),
- new MetaItem(UdpClientProtocol.META_SERVER_ALWAYS_RESPONDS, Values.create(true)),
- new MetaItem(MetaItemType.EXECUTABLE)
+ new MetaItem(UdpClientProtocol.META_ATTRIBUTE_WRITE_VALUE, Values.create("World;"))
),
- // attribute poll with 3 retries
- new AssetAttribute("pollTest", AttributeValueType.STRING)
+ new AssetAttribute("responseHello", AttributeValueType.STRING)
.addMeta(
new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agent.id, "protocolConfig").toArrayValue()),
- new MetaItem(UdpClientProtocol.META_ATTRIBUTE_WRITE_VALUE, Values.create("Test;")),
- new MetaItem(UdpClientProtocol.META_POLLING_MILLIS, Values.create(100)),
- new MetaItem(UdpClientProtocol.META_SEND_RETRIES, Values.create(3))
+ new MetaItem(Protocol.META_ATTRIBUTE_MATCH_PREDICATE,
+ new StringPredicate(AssetQuery.Match.BEGIN, true, "Hello").toModelValue())
),
+ new AssetAttribute("responseWorld", AttributeValueType.STRING)
+ .addMeta(
+ new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agent.id, "protocolConfig").toArrayValue()),
+ new MetaItem(Protocol.META_ATTRIBUTE_MATCH_PREDICATE,
+ new StringPredicate(AssetQuery.Match.BEGIN, true, "Hello").toModelValue())
+ )
)
and: "the asset is merged into the asset service"
asset = assetStorageService.merge(asset)
- then: "the polling attribute should have the correct values"
- def messageCount = 0
- conditions.eventually {
- asset = assetStorageService.find(asset.getId(), true)
- assert asset.getAttribute("pollTest").flatMap({ it.getValueAsString() }).orElse(null) == "Test"
- messageCount = receivedMessages.size()
- }
-
- and: "the attribute should continue sending to the server"
+ then: "the protocol should be linked"
conditions.eventually {
- receivedMessages.size() > messageCount + 1
+ assert udpClientProtocol.protocolIoClientMap.size() == 1
+ assert udpClientProtocol.protocolMessageConsumers.size() == 1
+ assert udpClientProtocol.protocolMessageConsumers.get(new AttributeRef(agent.id, "protocolConfig")).size() == 2
}
when: "a linked attribute value is updated"
@@ -199,8 +192,8 @@ class UdpClientProtocolTest extends Specification implements ManagerContainerTra
then: "the protocol should be unlinked"
conditions.eventually {
- assert udpClientProtocol.clientMap.isEmpty()
- assert udpClientProtocol.attributeInfoMap.isEmpty()
+ assert udpClientProtocol.protocolIoClientMap.isEmpty()
+ assert udpClientProtocol.protocolMessageConsumers.isEmpty()
}
when: "the received messages are cleared"
@@ -211,62 +204,15 @@ class UdpClientProtocolTest extends Specification implements ManagerContainerTra
assert receivedMessages.isEmpty()
}
- when: "the polling attribute is removed"
- asset.removeAttribute("pollTest")
- asset = assetStorageService.merge(asset)
-
- and: "the protocol configuration is re-enabled"
+ when: "the protocol configuration is re-enabled"
agent.getAttribute("protocolConfig").ifPresent{it.meta.removeIf{it.name.orElse(null) == MetaItemType.DISABLED.urn}}
agent = assetStorageService.merge(agent)
- then: "the two remaining attributes should be linked"
+ then: "the attributes should be re-linked"
conditions.eventually {
- assert udpClientProtocol.clientMap.size() == 1
- assert udpClientProtocol.attributeInfoMap.size() == 2
- }
-
- when: "the server stops responding to the next request"
- echoSkipCount = 1
- lastSend = null
-
- and: "the attribute that is configured for no retries is executed"
- attributeEvent = new AttributeEvent(asset.id,
- "echoHello",
- AttributeExecuteStatus.REQUEST_START.asValue())
- assetProcessingService.sendAttributeEvent(attributeEvent)
-
- then: "the server should have received the attribute write value but it doesn't reply"
- conditions.eventually {
- assert receivedMessages.size() == 1
- assert lastSend == null
- }
-
- then: "after a while no more messages should be received by the server and the send queue is clear"
- new PollingConditions(initialDelay: 1, timeout: 2).eventually {
- assert receivedMessages.size() == 1
- assert udpClientProtocol.clientMap.get(new AttributeRef(agent.id, "protocolConfig")).actionQueue.isEmpty()
- }
-
- when: "the server stops responding to the next 3 requests"
- echoSkipCount = 3
- lastSend = null
- receivedMessages.clear()
-
- and: "the attribute that is configured for retries is executed"
- attributeEvent = new AttributeEvent(asset.id,
- "echoWorld",
- AttributeExecuteStatus.REQUEST_START.asValue())
- assetProcessingService.sendAttributeEvent(attributeEvent)
-
- then: "the server should have received the attribute write value 4 times and responded on the 4th"
- conditions.eventually {
- assert receivedMessages.size() == 4
- assert lastSend == "World"
- }
-
- then: "after a while no more messages should be received by the server"
- new PollingConditions(initialDelay: 1, timeout: 2).eventually {
- assert receivedMessages.size() == 4
+ assert udpClientProtocol.protocolIoClientMap.size() == 1
+ assert udpClientProtocol.protocolMessageConsumers.size() == 1
+ assert udpClientProtocol.protocolMessageConsumers.get(new AttributeRef(agent.id, "protocolConfig")).size() == 2
}
when: "the echo server is changed to a byte based server"
@@ -304,26 +250,29 @@ class UdpClientProtocolTest extends Specification implements ManagerContainerTra
}
when: "the protocol configuration is updated to use HEX mode"
- def client = udpClientProtocol.clientMap.get(new AttributeRef(agent.id, "protocolConfig"))
+ def client = udpClientProtocol.protocolIoClientMap.get(new AttributeRef(agent.id, "protocolConfig"))
agent.getAttribute("protocolConfig").ifPresent{it.addMeta(UdpClientProtocol.META_PROTOCOL_CONVERT_HEX)}
+ agent.getAttribute("protocolConfig").ifPresent{it.getMeta().removeIf({Protocol.META_PROTOCOL_DELIMITER.getUrn().equals(it.name.orElse(""))})}
agent = assetStorageService.merge(agent)
then: "the protocol should be relinked"
conditions.eventually {
- assert udpClientProtocol.clientMap.size() == 1
- assert udpClientProtocol.clientMap.get(new AttributeRef(agent.id, "protocolConfig")) != client
+ assert udpClientProtocol.protocolIoClientMap.size() == 1
+ assert udpClientProtocol.protocolIoClientMap.get(new AttributeRef(agent.id, "protocolConfig")) != client
+ assert udpClientProtocol.protocolMessageConsumers.size() == 1
}
when: "the linked attributes are also updated to work with hex server"
- def attributeInfo = udpClientProtocol.attributeInfoMap.get(new AttributeRef(asset.id, "echoHello"))
- asset.getAttribute("echoHello").ifPresent({it.meta.replaceAll{it.name.get() == UdpClientProtocol.META_ATTRIBUTE_WRITE_VALUE.urn ? new MetaItem(UdpClientProtocol.META_ATTRIBUTE_WRITE_VALUE, Values.create("abcdef")) : it}})
- asset.getAttribute("echoWorld").ifPresent({it.meta.replaceAll{it.name.get() == UdpClientProtocol.META_ATTRIBUTE_WRITE_VALUE.urn ? new MetaItem(UdpClientProtocol.META_ATTRIBUTE_WRITE_VALUE, Values.create("123456")) : it}})
+ def consumer = udpClientProtocol.protocolMessageConsumers.get(new AttributeRef(agent.id, "protocolConfig")).stream().filter({it.key.equals(new AttributeRef(asset.id, "echoHello"))})
+ asset.getAttribute("echoHello").ifPresent({it.meta.replaceAll{it.name.get() == UdpClientProtocol.META_ATTRIBUTE_WRITE_VALUE.urn ? new MetaItem(UdpClientProtocol.META_ATTRIBUTE_WRITE_VALUE, Values.create('"abcdef"')) : it}})
+ asset.getAttribute("echoWorld").ifPresent({it.meta.replaceAll{it.name.get() == UdpClientProtocol.META_ATTRIBUTE_WRITE_VALUE.urn ? new MetaItem(UdpClientProtocol.META_ATTRIBUTE_WRITE_VALUE, Values.create('"123456"')) : it}})
asset = assetStorageService.merge(asset)
then: "the attributes should be relinked"
conditions.eventually {
- assert udpClientProtocol.attributeInfoMap.size() == 2
- assert udpClientProtocol.attributeInfoMap.get(new AttributeRef(asset.id, "echoHello")) != attributeInfo
+ assert udpClientProtocol.protocolMessageConsumers.size() == 1
+ assert udpClientProtocol.protocolMessageConsumers.get(new AttributeRef(agent.id, "protocolConfig")).size() == 2
+ assert udpClientProtocol.protocolMessageConsumers.get(new AttributeRef(agent.id, "protocolConfig")).stream().filter({it.key.equals(new AttributeRef(asset.id, "echoHello"))}) != consumer
}
when: "the hello linked attribute is executed"
@@ -342,7 +291,9 @@ class UdpClientProtocolTest extends Specification implements ManagerContainerTra
}
cleanup: "the server should be stopped"
- echoServer.stop()
+ if (echoServer != null) {
+ echoServer.stop()
+ }
stopContainer(container)
}
}
\ No newline at end of file
diff --git a/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientTest.groovy
index 9e40777f7..3a406cdda 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientTest.groovy
@@ -19,11 +19,14 @@
*/
package org.openremote.test.protocol.udp
-import io.netty.buffer.ByteBuf
+import io.netty.channel.ChannelHandler
+import io.netty.handler.codec.string.StringDecoder
+import io.netty.handler.codec.string.StringEncoder
import io.netty.util.CharsetUtil
import io.netty.util.internal.SocketUtils
import org.openremote.agent.protocol.ProtocolExecutorService
-import org.openremote.agent.protocol.udp.AbstractUdpClient
+import org.openremote.agent.protocol.io.AbstractNettyIoClient
+import org.openremote.agent.protocol.udp.UdpIoClient
import org.openremote.agent.protocol.udp.UdpStringServer
import org.openremote.manager.concurrent.ManagerExecutorService
import org.openremote.model.asset.agent.ConnectionStatus
@@ -34,7 +37,7 @@ import spock.util.concurrent.PollingConditions
import java.util.function.Consumer
/**
- * This tests the {@link AbstractUdpClient} by creating a simple echo server that the client communicates with
+ * This tests the {@link UdpIoClient} by creating a simple echo server that the client communicates with
*/
class UdpClientTest extends Specification implements ManagerContainerTrait {
@@ -63,25 +66,18 @@ class UdpClientTest extends Specification implements ManagerContainerTrait {
})
and: "a simple UDP broadcast client"
- def client = new AbstractUdpClient<String>(
+ UdpIoClient<String> client = new UdpIoClient<String>(
"255.255.255.255",
echoServerPort,
clientPort,
- protocolExecutorService) {
-
- @Override
- protected void decode(ByteBuf buf, List<String> messages) {
- ByteBuf bytes = buf.readBytes(buf.readableBytes())
- String msg = bytes.toString(CharsetUtil.UTF_8)
- bytes.release()
- messages.add(msg)
- }
-
- @Override
- protected void encode(String message, ByteBuf buf) {
- buf.writeBytes(message.getBytes(CharsetUtil.UTF_8))
- }
- }
+ protocolExecutorService)
+ client.setEncoderDecoderProvider({
+ [
+ new StringEncoder(CharsetUtil.UTF_8),
+ new StringDecoder(CharsetUtil.UTF_8),
+ new AbstractNettyIoClient.MessageToMessageDecoder<String>(String.class, client)
+ ].toArray(new ChannelHandler[0])
+ })
and: "we add callback consumers to the client"
def connectionStatus = client.getConnectionStatus()
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 198d78cdb..25d1ef054 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
@@ -42,7 +42,7 @@ class VelbusProtocolTest extends Specification implements ManagerContainerTrait
static AbstractVelbusProtocol velbusProtocol = new AbstractVelbusProtocol() {
@Override
- protected IoClient<VelbusPacket> createClient(AssetAttribute protocolConfiguration) throws RuntimeException {
+ protected IoClient<VelbusPacket> createIoClient(AssetAttribute protocolConfiguration) throws RuntimeException {
return messageProcessor
}
diff --git a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy
index ba79e01d2..802ef1a72 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy
@@ -195,19 +195,19 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContai
new AssetAttribute("readWriteTargetTemp", AttributeValueType.NUMBER)
.addMeta(
new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agent.id, "protocolConfig").toArrayValue()),
- new MetaItem(Protocol.META_ATTRIBUTE_WRITE_VALUE, Values.create(SharedEvent.MESSAGE_PREFIX +
+ new MetaItem(Protocol.META_ATTRIBUTE_WRITE_VALUE, Values.create("\'" + SharedEvent.MESSAGE_PREFIX +
Container.JSON.writeValueAsString(new AttributeEvent(
managerDemoSetup.apartment1LivingroomId,
"targetTemperature",
- Values.create(0.12345))).replace("0.12345", Protocol.DYNAMIC_VALUE_PLACEHOLDER))),
- new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MESSAGE_MATCH_FILTERS,
+ Values.create(0.12345))).replace("0.12345", Protocol.DYNAMIC_VALUE_PLACEHOLDER) + "\'")),
+ new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MATCH_FILTERS,
Values.convert(
[
new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),
- new JsonPathFilter("\$..attributeState.attributeRef", false)
+ new JsonPathFilter("\$..attributeState.attributeRef.attributeName", false)
] as ValueFilter[]
, Container.JSON).orElse(null)),
- new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MESSAGE_MATCH_PREDICATE,
+ new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MATCH_PREDICATE,
new StringPredicate(AssetQuery.Match.CONTAINS, true, "targetTemperature").toModelValue()),
new MetaItem(Protocol.META_ATTRIBUTE_VALUE_FILTERS,
Values.convert(
@@ -228,14 +228,14 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContai
.addMeta(
new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agent.id, "protocolConfig").toArrayValue()),
new MetaItem(MetaItemType.READ_ONLY),
- new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MESSAGE_MATCH_FILTERS,
+ new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MATCH_FILTERS,
Values.convert(
[
new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),
- new JsonPathFilter("\$..attributeState.attributeRef", false)
+ new JsonPathFilter("\$..attributeState.attributeRef.attributeName", false)
] as ValueFilter[]
, Container.JSON).orElse(null)),
- new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MESSAGE_MATCH_PREDICATE,
+ new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MATCH_PREDICATE,
new StringPredicate(AssetQuery.Match.CONTAINS, "co2Level").toModelValue()),
new MetaItem(Protocol.META_ATTRIBUTE_VALUE_FILTERS,
Values.convert(
diff --git a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy
index 564f43458..83c4e540e 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy
@@ -19,9 +19,11 @@
*/
package org.openremote.test.protocol.websocket
+import io.netty.channel.ChannelHandler
import org.apache.http.client.utils.URIBuilder
import org.openremote.agent.protocol.http.OAuthPasswordGrant
-import org.openremote.agent.protocol.websocket.WebsocketClient
+import org.openremote.agent.protocol.io.AbstractNettyIoClient
+import org.openremote.agent.protocol.websocket.WebsocketIoClient
import org.openremote.container.Container
import org.openremote.manager.asset.AssetProcessingService
import org.openremote.manager.concurrent.ManagerExecutorService
@@ -45,7 +47,7 @@ import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID
import static org.openremote.model.Constants.MASTER_REALM_ADMIN_USER
/**
- * This tests the {@link WebsocketClient} by connecting to the manager web socket
+ * This tests the {@link WebsocketIoClient} by connecting to the manager web socket
*/
class WebsocketClientTest extends Specification implements ManagerContainerTrait {
@@ -62,7 +64,7 @@ class WebsocketClientTest extends Specification implements ManagerContainerTrait
def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)
and: "a simple Websocket client"
- def client = new WebsocketClient(
+ def client = new WebsocketIoClient<String>(
new URIBuilder("ws://localhost:$serverPort/websocket/events?Auth-Realm=master").build(),
null,
new OAuthPasswordGrant("http://localhost:$serverPort/auth/realms/master/protocol/openid-connect/token",
@@ -72,6 +74,9 @@ class WebsocketClientTest extends Specification implements ManagerContainerTrait
MASTER_REALM_ADMIN_USER,
getString(container.getConfig(), SETUP_ADMIN_PASSWORD, SETUP_ADMIN_PASSWORD_DEFAULT)),
protocolExecutorService)
+ client.setEncoderDecoderProvider({
+ [new AbstractNettyIoClient.MessageToMessageDecoder<String>(String.class, client)].toArray(new ChannelHandler[0])
+ })
and: "we add callback consumers to the client"
def connectionStatus = client.getConnectionStatus()
diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
index 036edc02a..205b29c2c 100644
--- a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
@@ -217,7 +217,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
and: "a notification should have been sent to the console"
conditions.eventually {
- assert notificationMessages.size() == 1
+ assert notificationMessages.findAll {it.data == null || !it.data.getString("action").orElse("").equals("GEOFENCE_REFRESH")}.size() == 1
assert targets[0].type == Notification.TargetType.ASSET
assert targets[0].id == consoleRegistration.id
}