Skip to content

armgit / core

Public Imported from GitHub
0

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

Protocol updates (refactoring and added support for ValueFilters to linked attributes as well as full JsonPath support in JsonPathFilter)

Rich Turner02393f2da978
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 612e17189..fd7fa1b18 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
@@ -111,7 +111,7 @@ public abstract class AbstractProtocol implements Protocol {
public static final int PRIORITY = ContainerService.DEFAULT_PRIORITY - 100;
protected final Map<AttributeRef, AssetAttribute> linkedAttributes = new HashMap<>();
protected final Map<AttributeRef, LinkedProtocolInfo> linkedProtocolConfigurations = new HashMap<>();
- protected final Map<AttributeRef, List<ValueFilter>> linkedAttributeFilters = new HashMap<>();
+ protected final Map<AttributeRef, ValueFilter[]> linkedAttributeFilters = new HashMap<>();
protected static final List<MetaItemDescriptor> attributeMetaItemDescriptors;
protected MessageBrokerContext messageBrokerContext;
protected ProducerTemplate producerTemplate;
@@ -121,7 +121,7 @@ public abstract class AbstractProtocol implements Protocol {
static {
attributeMetaItemDescriptors = Arrays.asList(
- META_VALUE_FILTERS
+ MetaItemType.VALUE_FILTERS
);
}
@@ -217,7 +217,7 @@ public abstract class AbstractProtocol implements Protocol {
// linking process and without entry in the map any update would be blocked
linkedAttributes.put(attributeRef, attribute);
- Optional<List<ValueFilter>> messageFilters = Protocol.getLinkedAttributeMessageFilters(attribute);
+ Optional<ValueFilter[]> messageFilters = Protocol.getLinkedAttributeMessageFilters(attribute);
messageFilters.ifPresent(mFilters -> {
linkedAttributeFilters.put(attributeRef, mFilters);
});
@@ -324,54 +324,11 @@ public abstract class AbstractProtocol implements Protocol {
}
if (state.getValue().isPresent()) {
- List<ValueFilter> filters;
Value value = state.getValue().get();
-
- filters = linkedAttributeFilters.get(state.getAttributeRef());
+ ValueFilter[] filters = linkedAttributeFilters.get(state.getAttributeRef());
if (filters != null) {
- LOG.fine("Applying message filters to sensor value...");
-
- for (ValueFilter filter : filters) {
- boolean filterOk = filter.getMessageType() == value.getType().getModelType();
-
- if (!filterOk) {
- // Try and convert the value
- ValueType filterValueType = ValueType.fromModelType(filter.getMessageType());
- if (filterValueType == null) {
- LOG.fine("Message filter type unknown: " + filter.getMessageType().getName());
- value = null;
- } else {
- Optional<Value> val = Values.convert(value, filterValueType);
- if (!val.isPresent()) {
- LOG.fine("Message filter type '" + filter.getMessageType().getName()
- + "' is not compatible with actual message type '" + value.getType().getModelType().getName()
- + "': " + filter.getClass().getName());
- } else {
- filterOk = true;
- }
- value = val.orElse(null);
- }
- }
-
- if (filterOk) {
- try {
- LOG.finest("Applying message filter: " + filter.getClass().getName());
- value = filter.process(value);
- } catch (Exception e) {
- LOG.log(
- Level.SEVERE,
- "Message filter threw an exception during processing of message: "
- + filter.getClass().getName(),
- e);
- value = null;
- }
- }
-
- if (value == null) {
- break;
- }
- }
+ value = assetService.applyValueFilters(value, filters);
}
// Do basic value conversion
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 60632c485..62038c201 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/Protocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/Protocol.java
@@ -21,7 +21,8 @@ package org.openremote.agent.protocol;
import org.apache.commons.codec.binary.BinaryCodec;
import org.apache.commons.codec.binary.Hex;
-import org.openremote.model.value.ValueFilter;
+import org.openremote.agent.protocol.http.OAuthGrant;
+import org.openremote.model.value.*;
import org.openremote.container.Container;
import org.openremote.container.ContainerService;
import org.openremote.model.AbstractValueHolder;
@@ -33,15 +34,10 @@ import org.openremote.model.asset.agent.ProtocolConfiguration;
import org.openremote.model.asset.agent.ProtocolDescriptor;
import org.openremote.model.attribute.*;
import org.openremote.model.util.TextUtil;
-import org.openremote.model.value.ArrayValue;
-import org.openremote.model.value.ObjectValue;
-import org.openremote.model.value.ValueType;
import java.io.IOException;
import java.nio.charset.Charset;
-import java.util.ArrayList;
import java.util.Collection;
-import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.logging.Level;
@@ -96,7 +92,7 @@ import static org.openremote.model.attribute.MetaItemDescriptorImpl.*;
* receive and send value change messages with values of that type.
* <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_VALUE_FILTERS}
+ * protocol updates the linked attribute, to implement this protocols should use the {@link MetaItemType#VALUE_FILTERS}
* {@link MetaItem}.
* <p>
* <h1>Dynamic value injection</h1>
@@ -175,9 +171,19 @@ public interface Protocol extends ContainerService {
Charset.availableCharsets().keySet().toArray(new String[0])
);
+ /**
+ * OAuth grant ({@link OAuthGrant} stored as {@link ObjectValue})
+ */
+ MetaItemDescriptor META_PROTOCOL_OAUTH_GRANT = metaItemObject(
+ PROTOCOL_NAMESPACE + ":oAuthGrant",
+ ACCESS_PRIVATE,
+ false,
+ null);
+
/**
* Value to be used for attribute writes, protocols that support this should also support dynamic value insertion,
- * see interface javadoc for more details
+ * see interface javadoc for more details; use the {@link #createDynamicAttributeWriteConsumer} helper method where
+ * possible.
*/
MetaItemDescriptor META_ATTRIBUTE_WRITE_VALUE = metaItemAny(
PROTOCOL_NAMESPACE + ":writeValue",
@@ -198,19 +204,6 @@ public interface Protocol extends ContainerService {
1000,
null);
- /**
- * {@link MetaItem} for defining {@link ValueFilter}s to apply to values before they are sent on the
- * {@link #SENSOR_QUEUE} (i.e. 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.
- */
- MetaItemDescriptor META_VALUE_FILTERS = metaItemArray(
- PROTOCOL_NAMESPACE + ":valueFilters",
- 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";
@@ -222,7 +215,6 @@ public interface Protocol extends ContainerService {
String DYNAMIC_VALUE_PLACEHOLDER_REGEXP = "\"?\\{\\$value}\"?";
-
/**
* Get the name for this protocol
*/
@@ -291,12 +283,12 @@ public interface Protocol extends ContainerService {
/**
* Extract the {@link ValueFilter}s from the specified {@link Attribute}
*/
- static Optional<List<ValueFilter>> getLinkedAttributeMessageFilters(Attribute attribute) {
+ static Optional<ValueFilter[]> getLinkedAttributeMessageFilters(Attribute attribute) {
if (attribute == null) {
return Optional.empty();
}
- Optional<ArrayValue> arrayValueOptional = attribute.getMetaItem(META_VALUE_FILTERS)
+ Optional<ArrayValue> arrayValueOptional = attribute.getMetaItem(MetaItemType.VALUE_FILTERS)
.flatMap(AbstractValueHolder::getValueAsArray);
if (!arrayValueOptional.isPresent()) {
@@ -304,33 +296,29 @@ public interface Protocol extends ContainerService {
}
try {
- ArrayValue arrayValue = arrayValueOptional.get();
- List<ValueFilter> messageFilters = new ArrayList<>(arrayValue.length());
- for (int i = 0; i < arrayValue.length(); i++) {
- ObjectValue objValue = arrayValue.getObject(i).orElseThrow(() ->
- new IllegalArgumentException("Attribute protocol filters meta item is invalid"));
- ValueFilter filter = deserialiseMessageFilter(objValue);
- messageFilters.add(filter);
- }
- return messageFilters.isEmpty() ? Optional.empty() : Optional.of(messageFilters);
- } catch (IllegalArgumentException e) {
+ String json = arrayValueOptional.get().toJson();
+ return Optional.of(Container.JSON.readValue(json, ValueFilter[].class));
+ } catch (Exception e) {
LOG.log(Level.WARNING, e.getMessage(), e);
}
return Optional.empty();
}
- /**
- * Deserialise a {@link ValueFilter} from an {@link ObjectValue}
- */
- static ValueFilter deserialiseMessageFilter(ObjectValue objectValue) throws IllegalArgumentException {
- String json = objectValue.toJson();
- try {
- return Container.JSON.readValue(json, ValueFilter.class);
- } catch (IOException ioException) {
- LOG.log(Level.WARNING, "Failed to deserialise message filter", ioException);
- throw new IllegalArgumentException(ioException);
- }
+ static Optional<OAuthGrant> getOAuthGrant(AssetAttribute attribute) throws IllegalArgumentException {
+ return !attribute.hasMetaItem(META_PROTOCOL_OAUTH_GRANT)
+ ? Optional.empty()
+ : Optional.of(attribute.getMetaItem(META_PROTOCOL_OAUTH_GRANT)
+ .flatMap(AbstractValueHolder::getValueAsObject)
+ .map(objValue -> {
+ String json = objValue.toJson();
+ try {
+ return Container.JSON.readValue(json, OAuthGrant.class);
+ } catch (IOException e) {
+ throw new IllegalArgumentException("OAuth Grant meta item is not valid", e);
+ }
+ })
+ .orElseThrow(() -> new IllegalArgumentException("OAuth grant meta item must be an ObjectValue")));
}
static String bytesToHexString(byte[] bytes) {
@@ -358,4 +346,25 @@ public interface Protocol extends ContainerService {
return new byte[0];
}
}
+
+ static Consumer<Value> createDynamicAttributeWriteConsumer(AssetAttribute attribute, Consumer<String> writeConsumer) {
+
+ final String writeValue = Values.getMetaItemValueOrThrow(attribute, META_ATTRIBUTE_WRITE_VALUE, false, true)
+ .map(Object::toString).orElse(null);
+
+ return value -> {
+
+ String str = value != null ? value.toString() : "";
+
+ if (!TextUtil.isNullOrEmpty(writeValue)) {
+ if (str.isEmpty()) {
+ str = writeValue;
+ } else if (writeValue.contains(DYNAMIC_VALUE_PLACEHOLDER)) {
+ str = writeValue.replaceAll(DYNAMIC_VALUE_PLACEHOLDER_REGEXP, str);
+ }
+ }
+
+ writeConsumer.accept(str);
+ };
+ }
}
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 a354c11e9..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,6 +23,8 @@ 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.Value;
+import org.openremote.model.value.ValueFilter;
import java.util.function.Predicate;
@@ -140,4 +142,9 @@ public interface ProtocolAssetService extends ContainerService {
* belongs to.
*/
Asset getAgent(AssetAttribute protocolConfiguration);
+
+ /**
+ * Apply the specified set of {@link ValueFilter}s to the specified {@link Value}
+ */
+ Value applyValueFilters(Value value, ValueFilter... filters);
}
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 0864bf6f3..4a65c4fa2 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
@@ -48,7 +48,6 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
-import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.net.URI;
@@ -78,7 +77,7 @@ import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_STRING_NON_EMPTY
* <p>
* {@link Attribute}s that are configured as {@link ProtocolConfiguration}s for this protocol support the following meta
* items: <ul> <li>{@link #META_PROTOCOL_BASE_URI} (<b>required</b>)</li> <li>{@link #META_PROTOCOL_USERNAME}</li>
- * <li>{@link #META_PROTOCOL_PASSWORD}</li> <li>{@link #META_PROTOCOL_OAUTH_GRANT}</li> <li>{@link
+ * <li>{@link #META_PROTOCOL_PASSWORD}</li> <li>{@link Protocol#META_PROTOCOL_OAUTH_GRANT}</li> <li>{@link
* #META_PROTOCOL_PING_PATH}</li> <li>{@link #META_PROTOCOL_PING_METHOD}</li> <li>{@link #META_PROTOCOL_PING_BODY}</li>
* <li>{@link #META_PROTOCOL_PING_QUERY_PARAMETERS}</li> <li>{@link #META_PROTOCOL_PING_MILLIS}</li> <li>{@link
* #META_PROTOCOL_FOLLOW_REDIRECTS}</li> <li>{@link #META_FAILURE_CODES}</li> <li>{@link #META_HEADERS}</li> </ul>
@@ -91,13 +90,13 @@ import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_STRING_NON_EMPTY
* 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>
- * <li>{@link Protocol#META_VALUE_FILTERS}</li>
+ * <li>{@link MetaItemType#META_VALUE_FILTERS}</li>
* </ul>
* <p>
* <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_VALUE_FILTERS} in
+ * #META_POLLING_MILLIS} {@link MetaItem}) can use the standard {@link MetaItemType#META_VALUE_FILTERS} in
* order to filter the received HTTP response.
* <p>
* <h1>Connection Status</h1>
@@ -370,19 +369,10 @@ public class HttpClientProtocol extends AbstractProtocol {
public static final MetaItemDescriptor META_PROTOCOL_PASSWORD = metaItemString(
PROTOCOL_NAME + ":password",
ACCESS_PRIVATE,
- true,
+ false,
REGEXP_PATTERN_STRING_NON_EMPTY,
MetaItemDescriptor.PatternFailure.STRING_EMPTY);
- /**
- * OAuth grant ({@link OAuthGrant} stored as {@link ObjectValue})
- */
- public static final MetaItemDescriptor META_PROTOCOL_OAUTH_GRANT = metaItemObject(
- PROTOCOL_NAME + ":oAuthGrant",
- ACCESS_PRIVATE,
- false,
- null);
-
/**
* Relative path to endpoint that should be used for connection status ping (string)
*/
@@ -580,22 +570,6 @@ public class HttpClientProtocol extends AbstractProtocol {
protected final Map<AttributeRef, ScheduledFuture> pollingMap = new HashMap<>();
protected ResteasyClient client;
- public static Optional<OAuthGrant> getOAuthGrant(AssetAttribute attribute) throws IllegalArgumentException {
- return !attribute.hasMetaItem(META_PROTOCOL_OAUTH_GRANT)
- ? Optional.empty()
- : Optional.of(attribute.getMetaItem(META_PROTOCOL_OAUTH_GRANT)
- .flatMap(AbstractValueHolder::getValueAsObject)
- .map(objValue -> {
- String json = objValue.toJson();
- try {
- return Container.JSON.readValue(json, OAuthGrant.class);
- } catch (IOException e) {
- throw new IllegalArgumentException("OAuth Grant meta item is not valid", e);
- }
- })
- .orElseThrow(() -> new IllegalArgumentException("OAuth grant meta item must be an ObjectValue")));
- }
-
public static Optional<Pair<StringValue, StringValue>> getUsernameAndPassword(AssetAttribute attribute) throws IllegalArgumentException {
Optional<StringValue> username = Values.getMetaItemValueOrThrow(
attribute,
@@ -706,7 +680,7 @@ public class HttpClientProtocol extends AbstractProtocol {
/* We're going to fail hard and fast if optional meta items are incorrectly configured */
- Optional<OAuthGrant> oAuthGrant = getOAuthGrant(protocolConfiguration);
+ Optional<OAuthGrant> oAuthGrant = Protocol.getOAuthGrant(protocolConfiguration);
Optional<Pair<StringValue, StringValue>> usernameAndPassword = getUsernameAndPassword(protocolConfiguration);
boolean followRedirects = Values.getMetaItemValueOrThrow(
@@ -770,7 +744,7 @@ public class HttpClientProtocol extends AbstractProtocol {
ResteasyWebTarget client = webTargetBuilder.build();
clientMap.put(protocolRef, new Pair<>(client, failureCodes));
- updateStatus(protocolRef, ConnectionStatus.UNKNOWN);
+ updateStatus(protocolRef, ConnectionStatus.CONNECTED);
if (pingPath == null) {
return;
@@ -1072,7 +1046,7 @@ public class HttpClientProtocol extends AbstractProtocol {
AttributeValidationResult result = super.validateProtocolConfiguration(protocolConfiguration);
if (result.isValid()) {
try {
- getOAuthGrant(protocolConfiguration);
+ Protocol.getOAuthGrant(protocolConfiguration);
getUsernameAndPassword(protocolConfiguration);
getPingMillis(protocolConfiguration);
} catch (IllegalArgumentException e) {
@@ -1220,7 +1194,7 @@ public class HttpClientProtocol extends AbstractProtocol {
});
}
- protected void updateConnectionStatus(HttpClientRequest request, AttributeRef protocolConfigurationRef, int responseCode) {
+ public static ConnectionStatus getStatusFromResponseCode(int responseCode) {
Response.Status status = Response.Status.fromStatusCode(responseCode);
ConnectionStatus connectionStatus = ConnectionStatus.CONNECTED;
if (status == null) {
@@ -1247,6 +1221,11 @@ public class HttpClientProtocol extends AbstractProtocol {
}
}
+ return connectionStatus;
+ }
+
+ protected void updateConnectionStatus(HttpClientRequest request, AttributeRef protocolConfigurationRef, int responseCode) {
+ ConnectionStatus connectionStatus = getStatusFromResponseCode(responseCode);
LOG.fine("Updating connection status based on polling response code: URI=" + request +
", ResponseCode=" + responseCode + ", Status=" + connectionStatus);
updateStatus(protocolConfigurationRef, connectionStatus);
diff --git a/agent/src/main/java/org/openremote/agent/protocol/http/OAuthFilter.java b/agent/src/main/java/org/openremote/agent/protocol/http/OAuthFilter.java
index 9a8c4ed46..62ea10763 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/http/OAuthFilter.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/http/OAuthFilter.java
@@ -20,6 +20,7 @@
package org.openremote.agent.protocol.http;
import org.openremote.model.syslog.SyslogCategory;
+import org.openremote.model.util.TextUtil;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
@@ -55,7 +56,12 @@ public class OAuthFilter implements ClientRequestFilter {
}
public String getAuthHeader() {
- return AUTH_TYPE + " " + getAccessToken();
+ String accessToken = getAccessToken();
+ if (!TextUtil.isNullOrEmpty(accessToken)) {
+ return AUTH_TYPE + " " + getAccessToken();
+ }
+
+ return null;
}
protected synchronized String getAccessToken() {
diff --git a/agent/src/main/java/org/openremote/agent/protocol/http/OAuthGrant.java b/agent/src/main/java/org/openremote/agent/protocol/http/OAuthGrant.java
index de2bb87fa..137825229 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/http/OAuthGrant.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/http/OAuthGrant.java
@@ -24,6 +24,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.openremote.container.Container;
+import org.openremote.model.util.TextUtil;
import org.openremote.model.value.ObjectValue;
import org.openremote.model.value.Values;
@@ -52,12 +53,15 @@ public abstract class OAuthGrant {
requireNonNullAndNonEmpty(tokenEndpointUri);
requireNonNullAndNonEmpty(grantType);
requireNonNullAndNonEmpty(clientId);
- requireNonNullAndNonEmpty(clientSecret);
this.tokenEndpointUri = tokenEndpointUri;
valueMap.add(VALUE_KEY_GRANT_TYPE, grantType);
valueMap.add(VALUE_KEY_CLIENT_ID, clientId);
- valueMap.add(VALUE_KEY_CLIENT_SECRET, clientSecret);
- valueMap.add(VALUE_KEY_SCOPE, scope);
+ if(!TextUtil.isNullOrEmpty(clientSecret)) {
+ valueMap.add(VALUE_KEY_CLIENT_SECRET, clientSecret);
+ }
+ if(!TextUtil.isNullOrEmpty(scope)) {
+ valueMap.add(VALUE_KEY_SCOPE, scope);
+ }
}
public MultivaluedMap<String, String> getValueMap() {
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 a5958daf8..1a6382076 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
@@ -90,6 +90,7 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen
AbstractNettyIoClient.this.encode(msg, out);
}
+ @SuppressWarnings("deprecation")
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
@@ -111,6 +112,7 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen
protected ProtocolExecutorService executorService;
protected ScheduledFuture reconnectTask;
protected int reconnectDelayMilliseconds = INITIAL_RECONNECT_DELAY_MILLIS;
+ protected boolean permanentError;
public AbstractNettyIoClient(ProtocolExecutorService executorService) {
this.executorService = executorService;
@@ -193,13 +195,13 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen
@Override
public synchronized void disconnect() {
- if (connectionStatus == ConnectionStatus.DISCONNECTING || connectionStatus == ConnectionStatus.DISCONNECTED) {
+ if ((permanentError && connectionStatus == ConnectionStatus.ERROR) || connectionStatus == ConnectionStatus.DISCONNECTING || connectionStatus == ConnectionStatus.DISCONNECTED) {
LOG.finest("Already disconnecting or disconnected: " + getSocketAddressString());
return;
}
LOG.finest("Disconnecting IO client: " + getSocketAddressString());
- onConnectionStatusChanged(ConnectionStatus.DISCONNECTING);
+ onConnectionStatusChanged(permanentError ? ConnectionStatus.ERROR : ConnectionStatus.DISCONNECTING);
try {
if (reconnectTask != null) {
@@ -397,6 +399,16 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen
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);
+ this.permanentError = true;
+ disconnect();
+ }
+
@Override
public String toString() {
return getSocketAddressString();
diff --git a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIoServer.java b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIoServer.java
index 7283e9d27..1b4df2dff 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIoServer.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIoServer.java
@@ -307,6 +307,7 @@ public abstract class AbstractNettyIoServer<T, U extends Channel, V extends Abst
});
channel.pipeline().addLast(new ChannelOutboundHandlerAdapter() {
+ @SuppressWarnings("deprecation")
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
onEncodeException(ctx, cause);
@@ -326,6 +327,7 @@ public abstract class AbstractNettyIoServer<T, U extends Channel, V extends Abst
});
}
+ @SuppressWarnings("unchecked")
protected void handleMessageReceived(U channel, T message) {
onMessageReceived(message, channel, (W)channel.remoteAddress());
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/serial/AbstractSerialClient.java b/agent/src/main/java/org/openremote/agent/protocol/serial/AbstractSerialClient.java
index 5f0b25aec..3261686dc 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/serial/AbstractSerialClient.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/serial/AbstractSerialClient.java
@@ -21,7 +21,7 @@ package org.openremote.agent.protocol.serial;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
-import io.netty.channel.oio.OioEventLoopGroup;
+import io.netty.channel.nio.NioEventLoopGroup;
import org.openremote.agent.protocol.ProtocolExecutorService;
import org.openremote.agent.protocol.io.AbstractNettyIoClient;
import org.openremote.agent.protocol.io.IoClient;
@@ -58,9 +58,8 @@ public abstract class AbstractSerialClient<T> extends AbstractNettyIoClient<T, N
return "serial://" + port;
}
- @SuppressWarnings("deprecation")
@Override
protected io.netty.channel.EventLoopGroup getWorkerGroup() {
- return new OioEventLoopGroup(1);
+ return new NioEventLoopGroup(1);
}
}
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 9d528f124..f13c811f7 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
@@ -31,6 +31,7 @@ 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.ValueFilter;
import org.openremote.model.value.Values;
import java.nio.charset.Charset;
@@ -49,30 +50,35 @@ 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.
- * <p>
- * This is a generic protocol that supports
- * {@link org.openremote.agent.protocol.Protocol#META_PROTOCOL_CONVERT_HEX} or
- * {@link org.openremote.agent.protocol.Protocol#META_PROTOCOL_CONVERT_BINARY} to facilitate working with UDP servers
- * that handle binary data.
- * <p>
* <h1>Protocol Configurations</h1>
- * An instance is created by defining a {@link ProtocolConfiguration} with the following {@link MetaItem}s:
- * <ul>
- * <li>{@link #META_PROTOCOL_HOST} <b>(required)</b></li>
- * <li>{@link #META_PROTOCOL_PORT} <b>(required)</b></li>
- * <li>{@link #META_PROTOCOL_BIND_PORT}</li>
- * <li>{@link #META_PROTOCOL_CHARSET} (defaults to UTF8 if not specified)</li>
- * </ul>
* <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 following meta items:
- * <ul>
- * <li>{@link #META_ATTRIBUTE_WRITE_VALUE} (if specified also supports dynamic value injection as described in {@link Protocol})</li>
- * <li>{@link #META_VALUE_FILTERS}</li>
- * <li>{@link #META_POLLING_MILLIS}</li>
- * </ul>
- *
+ * 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 MetaItemType#META_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 UdpClientProtocol extends AbstractProtocol {
@@ -264,7 +270,7 @@ public class UdpClientProtocol extends AbstractProtocol {
public static final List<MetaItemDescriptor> ATTRIBUTE_META_ITEM_DESCRIPTORS = Arrays.asList(
META_ATTRIBUTE_WRITE_VALUE,
- META_VALUE_FILTERS,
+ MetaItemType.VALUE_FILTERS,
META_POLLING_MILLIS,
META_RESPONSE_TIMEOUT_MILLIS,
META_SEND_RETRIES,
@@ -400,9 +406,6 @@ public class UdpClientProtocol extends AbstractProtocol {
return;
}
- final String writeValue = Values.getMetaItemValueOrThrow(attribute, META_ATTRIBUTE_WRITE_VALUE, false, true)
- .map(Object::toString).orElse(null);
-
final Integer pollingMillis = Values.getMetaItemValueOrThrow(attribute, META_POLLING_MILLIS, false, true)
.flatMap(Values::getIntegerCoerced)
.orElse(null);
@@ -439,13 +442,13 @@ public class UdpClientProtocol extends AbstractProtocol {
AttributeInfo info = new AttributeInfo(attribute.getReferenceOrThrow(), sendRetries, responseTimeoutMillis);
if (!attribute.isReadOnly()) {
- sendConsumer = createWriteConsumer(
- clientAndQueue,
- info,
- writeValue,
- serverAlwaysResponds ? str -> {
+ 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);
+ } : null,
+ info));
}
if (pollingMillis != null && pollingMillis < MIN_POLLING_MILLIS) {
@@ -453,6 +456,9 @@ public class UdpClientProtocol extends AbstractProtocol {
return;
}
+ final String writeValue = Values.getMetaItemValueOrThrow(attribute, META_ATTRIBUTE_WRITE_VALUE, false, true)
+ .map(Object::toString).orElse(null);
+
if (pollingMillis != null && TextUtil.isNullOrEmpty(writeValue)) {
LOG.warning("Polling requires the META_ATTRIBUTE_WRITE_VALUE meta item to be set");
return;
@@ -466,8 +472,8 @@ public class UdpClientProtocol extends AbstractProtocol {
attribute.getReferenceOrThrow(),
str != null ? Values.create(str) : null));
};
- Consumer<Value> pollingWriter = createWriteConsumer(clientAndQueue, info, null, responseConsumer);
- Runnable pollingRunnable = () -> pollingWriter.accept(Values.create(writeValue));
+
+ Runnable pollingRunnable = () -> clientAndQueue.send(writeValue, responseConsumer, info);
pollingTask = schedulePollingRequest(clientAndQueue, attribute.getReferenceOrThrow(), pollingRunnable, pollingMillis);
}
@@ -521,23 +527,6 @@ public class UdpClientProtocol extends AbstractProtocol {
updateStatus(protocolRef, connectionStatus);
}
- protected Consumer<Value> createWriteConsumer(ClientAndQueue clientAndQueue, AttributeInfo attributeInfo, String writeValue, Consumer<String> responseConsumer) {
- return value -> {
-
- String str = value != null ? value.toString() : "";
-
- if (!TextUtil.isNullOrEmpty(writeValue)) {
- if (str.isEmpty()) {
- str = writeValue;
- } else if (writeValue.contains(DYNAMIC_VALUE_PLACEHOLDER)) {
- str = writeValue.replaceAll(DYNAMIC_VALUE_PLACEHOLDER_REGEXP, str);
- }
- }
-
- clientAndQueue.send(str, responseConsumer, attributeInfo);
- };
- }
-
protected ScheduledFuture schedulePollingRequest(ClientAndQueue clientAndQueue,
AttributeRef attributeRef,
Runnable pollingTask,
diff --git a/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditActivity.java b/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditActivity.java
index d17c76658..0acd0f8bc 100644
--- a/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditActivity.java
+++ b/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditActivity.java
@@ -54,6 +54,7 @@ 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 javax.inject.Inject;
import java.util.*;
@@ -83,6 +84,7 @@ public class AssetEditActivity
protected final FileInfoMapper fileInfoMapper;
protected final AttributeValidationResultMapper attributeValidationResultMapper;
protected final AssetAttributeMapper assetAttributeMapper;
+ protected final AttributeLinkMapper attributeLinkMapper;
protected final Consumer<ConstraintViolation[]> validationErrorHandler;
protected List<ProtocolDescriptor> protocolDescriptors = new ArrayList<>();
protected List<MetaItemDescriptor> metaItemDescriptors = new ArrayList<>(Arrays.asList(MetaItemType.values())); // TODO Get meta item descriptors from server
@@ -105,6 +107,7 @@ public class AssetEditActivity
AttributeValidationResultMapper attributeValidationResultMapper,
AssetAttributeMapper assetAttributeMapper,
FileInfoMapper fileInfoMapper,
+ AttributeLinkMapper attributeLinkMapper,
MapResource mapResource,
ObjectValueMapper objectValueMapper) {
super(environment, assetBrowserPresenter, objectValueMapper, mapResource, true);
@@ -122,6 +125,7 @@ public class AssetEditActivity
this.attributeValidationResultMapper = attributeValidationResultMapper;
this.assetAttributeMapper = assetAttributeMapper;
this.fileInfoMapper = fileInfoMapper;
+ this.attributeLinkMapper = attributeLinkMapper;
validationErrorHandler = violations -> {
for (ConstraintViolation violation : violations) {
@@ -448,8 +452,8 @@ public class AssetEditActivity
parentView,
this::createValueEditor,
this::showValidationError,
- valueHolder.getValue().flatMap(AttributeLink::fromValue).orElse(null),
- attrLink -> onValueModified.accept(attrLink != null ? attrLink.toObjectValue() : null),
+ valueHolder.getValueAsString().map(attributeLinkMapper::read).orElse(null),
+ attrLink -> onValueModified.accept(attrLink != null ? Values.create(attributeLinkMapper.write(attrLink)) : null),
isReadOnly,
assetAttributeConsumer -> getLinkableAssetsAndAttributes(valueHolder, assetAttributeConsumer),
assetWatermark,
diff --git a/client/src/main/java/org/openremote/app/client/assets/attributes/AttributeLinkMapper.java b/client/src/main/java/org/openremote/app/client/assets/attributes/AttributeLinkMapper.java
new file mode 100644
index 000000000..3041498bb
--- /dev/null
+++ b/client/src/main/java/org/openremote/app/client/assets/attributes/AttributeLinkMapper.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2016, 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.app.client.assets.attributes;
+
+import com.github.nmorel.gwtjackson.client.ObjectMapper;
+import com.github.nmorel.gwtjackson.client.annotation.JsonMixIns;
+import org.openremote.app.client.interop.jackson.DefaultJsonMixin;
+import org.openremote.app.client.rest.EntityReader;
+import org.openremote.app.client.rest.EntityWriter;
+import org.openremote.model.attribute.AttributeLink;
+import org.openremote.model.attribute.AttributeValidationResult;
+
+@JsonMixIns({@JsonMixIns.JsonMixIn(target = AttributeLink.class, mixIn = DefaultJsonMixin.class)})
+public interface AttributeLinkMapper
+ extends ObjectMapper<AttributeLink>,
+ EntityReader<AttributeLink>, EntityWriter<AttributeLink> {
+}
diff --git a/client/src/main/java/org/openremote/app/client/widget/AttributeLinkEditor.java b/client/src/main/java/org/openremote/app/client/widget/AttributeLinkEditor.java
index 04bb13dc6..df116b908 100644
--- a/client/src/main/java/org/openremote/app/client/widget/AttributeLinkEditor.java
+++ b/client/src/main/java/org/openremote/app/client/widget/AttributeLinkEditor.java
@@ -318,7 +318,7 @@ public class AttributeLinkEditor extends FlowPanel {
if (!validationResult.isValid()) {
onValueModified.accept(null);
} else {
- onValueModified.accept(new AttributeLink(attributeRef, convertMetaToConverter(converterAttribute.getMeta())));
+ onValueModified.accept(new AttributeLink(attributeRef, convertMetaToConverter(converterAttribute.getMeta()), null));
}
}
diff --git a/gradle.properties b/gradle.properties
index 2c05fb220..b5b6ba06b 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -40,11 +40,12 @@ uuidGeneratorVersion = 3.1.3
nrjavaserialVersion = 3.13.0
saxonHEVersion = 9.8.0-3
jmxtermVersion = 1.0.0
-nettyVersion = 4.1.19.Final
+nettyVersion = 4.1.37.Final
flywaydbVersion = 4.2.0
ical4jVersion = 2.1.5
jafamaVersion = 2.3.1
friendlyIdVersion = 1.0.1
geotoolsVersion = 19.1
firebaseAdminVersion = 6.1.0
-simpleJavaMailVersion = 5.0.4
\ No newline at end of file
+simpleJavaMailVersion = 5.0.4
+jaywayJsonPathVersion = 2.4.0
\ No newline at end of file
diff --git a/manager/build.gradle b/manager/build.gradle
index 4b73267a6..255df98e2 100644
--- a/manager/build.gradle
+++ b/manager/build.gradle
@@ -13,11 +13,16 @@ dependencies {
compile "org.xerial:sqlite-jdbc:$sqliteVersion"
compile "org.mnode.ical4j:ical4j:$ical4jVersion"
+ compile "com.jayway.jsonpath:json-path:$jaywayJsonPathVersion"
compile "net.jafama:jafama:$jafamaVersion"
compile "org.geotools:gt-main:$geotoolsVersion"
compile "com.google.firebase:firebase-admin:$firebaseAdminVersion"
compile "org.simplejavamail:simple-java-mail:$simpleJavaMailVersion"
+
+ compile ("io.netty:netty-codec-http:$nettyVersion") {
+ force = true
+ }
}
// Load version properties file and use same version as source code
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 a4a9cf717..26505175d 100644
--- a/manager/src/main/java/org/openremote/manager/agent/AgentService.java
+++ b/manager/src/main/java/org/openremote/manager/agent/AgentService.java
@@ -19,6 +19,12 @@
*/
package org.openremote.manager.agent;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.jayway.jsonpath.Configuration;
+import com.jayway.jsonpath.JsonPath;
+import com.jayway.jsonpath.ParseContext;
+import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider;
+import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import org.apache.camel.builder.RouteBuilder;
import org.openremote.agent.protocol.Protocol;
import org.openremote.agent.protocol.ProtocolAssetService;
@@ -37,18 +43,19 @@ import org.openremote.manager.security.ManagerIdentityService;
import org.openremote.manager.web.ManagerWebService;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
-import org.openremote.model.attribute.MetaItemType;
import org.openremote.model.asset.AssetType;
import org.openremote.model.asset.agent.*;
import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.attribute.AttributeEvent.Source;
import org.openremote.model.attribute.AttributeRef;
+import org.openremote.model.attribute.MetaItemType;
import org.openremote.model.event.shared.TenantFilter;
import org.openremote.model.query.AssetQuery;
import org.openremote.model.query.filter.AttributeRefPredicate;
import org.openremote.model.security.ClientRole;
import org.openremote.model.util.Pair;
-import org.openremote.model.value.ObjectValue;
+import org.openremote.model.util.TextUtil;
+import org.openremote.model.value.*;
import javax.persistence.EntityManager;
import java.util.*;
@@ -56,6 +63,7 @@ import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
+import java.util.regex.Matcher;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -95,6 +103,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
protected final Map<AttributeRef, List<AttributeRef>> linkedAttributes = new HashMap<>();
protected LocalAgentConnector localAgentConnector;
protected Map<String, Asset> agentMap;
+ protected ParseContext jsonPathParser;
/**
* It's important that {@link Protocol}s have a lower priority than this service so they are fully initialized
@@ -129,6 +138,13 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
assetStorageService,
this)
);
+
+ jsonPathParser = JsonPath.using(
+ new Configuration.ConfigurationBuilder()
+ .jsonProvider(new JacksonJsonNodeJsonProvider())
+ .mappingProvider(new JacksonMappingProvider())
+ .build()
+ );
}
@Override
@@ -756,4 +772,114 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
return agentMap;
});
}
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public Value applyValueFilters(Value value, ValueFilter... filters) {
+
+ LOG.fine("Applying value filters to value...");
+
+ for (ValueFilter filter : filters) {
+ boolean filterOk = filter.getValueType() == Value.class || filter.getValueType() == value.getType().getModelType();
+
+ if (!filterOk) {
+ // Try and convert the value
+ ValueType filterValueType = ValueType.fromModelType(filter.getValueType());
+ if (filterValueType == null) {
+ LOG.fine("Value filter type unknown: " + filter.getValueType().getName());
+ value = null;
+ } else {
+ Optional<Value> val = Values.convert(value, filterValueType);
+ if (!val.isPresent()) {
+ LOG.fine("Value filter type '" + filter.getValueType().getName()
+ + "' is not compatible with actual value type '" + value.getType().getModelType().getName()
+ + "': " + filter.getClass().getName());
+ } else {
+ filterOk = true;
+ }
+ value = val.orElse(null);
+ }
+ }
+
+ if (filterOk) {
+ try {
+ Protocol.LOG.finest("Applying value filter: " + filter.getClass().getName());
+ if (filter instanceof RegexValueFilter) {
+ value = applyRegexFilter((StringValue)value, (RegexValueFilter)filter);
+ } else if (filter instanceof SubStringValueFilter) {
+ value = applySubstringFilter((StringValue)value, (SubStringValueFilter)filter);
+ } else if (filter instanceof JsonPathFilter) {
+ value = applyJsonPathFilter(value, (JsonPathFilter)filter);
+ } else {
+ throw new UnsupportedOperationException("Unsupported filter: " + filter);
+ }
+ } catch (Exception e) {
+ LOG.log(
+ Level.SEVERE,
+ "Value filter threw an exception during processing: "
+ + filter.getClass().getName(),
+ e);
+ value = null;
+ }
+ }
+
+ if (value == null) {
+ break;
+ }
+ }
+
+ return value;
+ }
+
+ protected Value applySubstringFilter(StringValue value, SubStringValueFilter filter) {
+ if (value == null) {
+ return null;
+ }
+
+ String result = null;
+
+ try {
+ if (filter.endIndex != null) {
+ result = value.getString().substring(filter.beginIndex, filter.endIndex);
+ } else {
+ result = value.getString().substring(filter.beginIndex);
+ }
+ } catch (IndexOutOfBoundsException ignored) {}
+
+ return result == null ? null : Values.create(result);
+ }
+
+ protected Value applyRegexFilter(StringValue value, RegexValueFilter filter) {
+ if (value == null || filter.pattern == null) {
+ return null;
+ }
+
+ String filteredStr = null;
+ Matcher matcher = filter.pattern.matcher(value.getString());
+ int matchIndex = 0;
+ boolean matched = matcher.find();
+
+ while(matched && matchIndex<filter.matchIndex) {
+ matched = matcher.find();
+ matchIndex++;
+ }
+
+ if (matched) {
+ if (filter.matchGroup <= matcher.groupCount()) {
+ filteredStr = matcher.group(filter.matchGroup);
+ }
+ }
+
+ return filteredStr == null ? null : Values.create(filteredStr);
+ }
+
+ protected Value applyJsonPathFilter(Value value, JsonPathFilter filter) {
+ if (value == null || TextUtil.isNullOrEmpty(filter.path)) {
+ return null;
+ }
+
+ JsonNode node = jsonPathParser.parse(value.toJson()).read(filter.path);
+ String pathJson = node != null ? node.toString() : null;
+ return TextUtil.isNullOrEmpty(pathJson) ? null : Values.parse(pathJson).orElse(null);
+ }
}
\ No newline at end of file
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 97af7da88..2e299edd4 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetAttributeLinkingService.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetAttributeLinkingService.java
@@ -21,6 +21,7 @@ package org.openremote.manager.asset;
import org.openremote.container.Container;
import org.openremote.container.ContainerService;
+import org.openremote.manager.agent.AgentService;
import org.openremote.manager.asset.AssetProcessingException.Reason;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
@@ -32,9 +33,11 @@ import org.openremote.model.util.Pair;
import org.openremote.model.value.*;
import javax.persistence.EntityManager;
+import java.io.IOException;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.Optional;
+import java.util.logging.Level;
import java.util.logging.Logger;
import static org.openremote.model.query.AssetQuery.Include;
@@ -89,6 +92,7 @@ public class AssetAttributeLinkingService implements ContainerService, AssetUpda
private static final Logger LOG = Logger.getLogger(AssetAttributeLinkingService.class.getName());
protected AssetProcessingService assetProcessingService;
protected AssetStorageService assetStorageService;
+ protected AgentService agentService;
@Override
public int getPriority() {
@@ -99,6 +103,7 @@ public class AssetAttributeLinkingService implements ContainerService, AssetUpda
public void init(Container container) throws Exception {
assetProcessingService = container.getService(AssetProcessingService.class);
assetStorageService = container.getService(AssetStorageService.class);
+ agentService = container.getService(AgentService.class);
}
@Override
@@ -135,9 +140,16 @@ public class AssetAttributeLinkingService implements ContainerService, AssetUpda
if (attributeState == null)
return;
LOG.fine("Processing attribute state for linked attribute");
- Optional<AttributeLink> attributeLink = metaItem.getValue().flatMap(AttributeLink::fromValue);
- if (!attributeLink.isPresent()) {
+ AttributeLink attributeLink = null;
+
+ try {
+ attributeLink = Container.JSON.readValue(metaItem.getValue().map(Value::toJson).orElse(""), AttributeLink.class);
+ } catch (IOException e) {
+ LOG.log(Level.WARNING, "Failed to deserialize AttributeLink", e);
+ }
+
+ if (attributeLink == null) {
throw new AssetProcessingException(Reason.INVALID_ATTRIBUTE_LINK);
}
@@ -146,7 +158,7 @@ public class AssetAttributeLinkingService implements ContainerService, AssetUpda
em,
assetStorageService,
attributeState.getValue().orElse(null),
- attributeLink.get()
+ attributeLink
);
if (!sendConvertedValue.key) {
@@ -154,14 +166,14 @@ public class AssetAttributeLinkingService implements ContainerService, AssetUpda
return;
}
- sendAttributeEvent(new AttributeEvent(attributeLink.get().getAttributeRef(), sendConvertedValue.value));
+ sendAttributeEvent(new AttributeEvent(attributeLink.getAttributeRef(), sendConvertedValue.value));
}
- protected static Pair<Boolean, Value> convertValueForLinkedAttribute(EntityManager em,
+ protected Pair<Boolean, Value> convertValueForLinkedAttribute(EntityManager em,
AssetStorageService assetStorageService,
Value originalValue,
AttributeLink attributeLink) throws AssetProcessingException {
- return attributeLink.getConverter()
+ Pair<Boolean, Value> ignoreValuePair = attributeLink.getConverter()
.map(
converter -> {
String converterKey = originalValue == null ? "NULL" : originalValue.toString().toUpperCase(Locale.ROOT);
@@ -175,6 +187,12 @@ public class AssetAttributeLinkingService implements ContainerService, AssetUpda
.orElseGet(() -> new Pair<>(true, originalValue)); // use the original value
})
.orElse(new Pair<>(true, originalValue)); // use the original value
+
+ if (ignoreValuePair.key && attributeLink.getFilters() != null) {
+ ignoreValuePair.value = agentService.applyValueFilters(ignoreValuePair.value, attributeLink.getFilters());
+ }
+
+ return ignoreValuePair;
}
protected static Optional<AttributeLink.ConverterType> getSpecialConverter(Value value) {
@@ -234,9 +252,9 @@ public class AssetAttributeLinkingService implements ContainerService, AssetUpda
}
}
- protected static Value getCurrentValue(EntityManager em,
- AssetStorageService assetStorageService,
- AttributeRef attributeRef) throws NoSuchElementException {
+ protected static Optional<AssetAttribute> getAttribute(EntityManager em,
+ AssetStorageService assetStorageService,
+ AttributeRef attributeRef) {
Asset asset = assetStorageService.find(
em,
new AssetQuery()
@@ -244,12 +262,20 @@ public class AssetAttributeLinkingService implements ContainerService, AssetUpda
.select(new Select(Include.ALL, false, attributeRef.getAttributeName()))
);
- Optional<AssetAttribute> attribute;
- if (asset == null || !(attribute = asset.getAttribute(attributeRef.getAttributeName())).isPresent()) {
- throw new NoSuchElementException("Attribute or asset could not be found: " + attributeRef);
+ AssetAttribute attribute = asset != null ? asset.getAttribute(attributeRef.getAttributeName()).orElse(null) : null;
+
+ if (attribute == null) {
+ LOG.warning("Attribute or asset could not be found: " + attributeRef);
}
- return attribute.get().getValue().orElse(null);
+ return Optional.ofNullable(attribute);
+ }
+
+ protected static Value getCurrentValue(EntityManager em,
+ AssetStorageService assetStorageService,
+ AttributeRef attributeRef) {
+ Optional<AssetAttribute> attribute = getAttribute(em, assetStorageService, attributeRef);
+ return attribute.flatMap(AssetAttribute::getValue).orElse(null);
}
@Override
diff --git a/manager/src/main/java/org/openremote/manager/event/EventTypeConverters.java b/manager/src/main/java/org/openremote/manager/event/EventTypeConverters.java
index 2170ef3cf..a69aedd19 100644
--- a/manager/src/main/java/org/openremote/manager/event/EventTypeConverters.java
+++ b/manager/src/main/java/org/openremote/manager/event/EventTypeConverters.java
@@ -67,11 +67,7 @@ public class EventTypeConverters implements TypeConverters {
@Converter
public String writeTriggeredEventSubscription(TriggeredEventSubscription triggeredEventSubscription, Exchange exchange) throws Exception {
- if (!TextUtil.isNullOrEmpty(triggeredEventSubscription.getSubscriptionId())) {
- return SharedEvent.MESSAGE_PREFIX + triggeredEventSubscription.getSubscriptionId() + ":" + Container.JSON.writeValueAsString(triggeredEventSubscription.getEvents());
- }
-
- return SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(triggeredEventSubscription.getEvents());
+ return SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(triggeredEventSubscription);
}
@Converter
diff --git a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java
index cd704037e..deafd8d85 100644
--- a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java
+++ b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java
@@ -91,7 +91,7 @@ public class AssetQueryPredicate implements Predicate<AssetState> {
if (query.names != null && query.names.length > 0) {
if (Arrays.stream(query.names)
- .map(AssetQueryPredicate::asPredicate)
+ .map(StringPredicate::asPredicate)
.noneMatch(np -> np.test(assetState.getName()))) {
return false;
}
@@ -107,7 +107,7 @@ public class AssetQueryPredicate implements Predicate<AssetState> {
if (query.types != null && query.types.length > 0) {
if (Arrays.stream(query.types)
- .map(AssetQueryPredicate::asPredicate)
+ .map(StringPredicate::asPredicate)
.noneMatch(np -> np.test(assetState.getTypeString()))) {
return false;
}
@@ -144,30 +144,6 @@ public class AssetQueryPredicate implements Predicate<AssetState> {
return true;
}
- public static Predicate<String> asPredicate(StringPredicate predicate) {
- return string -> {
- if (string == null && predicate.value == null)
- return true;
- if (string == null)
- return false;
- if (predicate.value == null)
- return false;
-
- String shouldMatch = predicate.caseSensitive ? predicate.value : predicate.value.toUpperCase(Locale.ROOT);
- String have = predicate.caseSensitive ? string : string.toUpperCase(Locale.ROOT);
-
- switch (predicate.match) {
- case BEGIN:
- return predicate.negate != have.startsWith(shouldMatch);
- case END:
- return predicate.negate != have.endsWith(shouldMatch);
- case CONTAINS:
- return predicate.negate != have.contains(shouldMatch);
- }
- return predicate.negate != have.equals(shouldMatch);
- };
- }
-
public static Predicate<ArrayValue> asPredicate(ArrayPredicate predicate) {
return arrayValue -> {
if (arrayValue == null) {
@@ -235,7 +211,7 @@ public class AssetQueryPredicate implements Predicate<AssetState> {
return false;
for (int i = 0; i < predicate.predicates.length; i++) {
StringPredicate p = predicate.predicates[i];
- if (!asPredicate(p).test(strings[i]))
+ if (!StringPredicate.asPredicate(p).test(strings[i]))
return false;
}
return true;
@@ -397,7 +373,7 @@ public class AssetQueryPredicate implements Predicate<AssetState> {
public static Predicate<AssetState> asPredicate(Supplier<Long> currentMillisProducer, AttributePredicate predicate) {
Predicate<String> namePredicate = predicate.name != null
- ? asPredicate(predicate.name) : str -> true;
+ ? StringPredicate.asPredicate(predicate.name) : str -> true;
Predicate<Value> valuePredicate = value -> {
if (predicate.value == null) {
@@ -455,7 +431,7 @@ public class AssetQueryPredicate implements Predicate<AssetState> {
} else if (predicate instanceof StringPredicate) {
StringPredicate p = (StringPredicate) predicate;
- return asPredicate(p).test(Values.getString(value).orElse(null));
+ return StringPredicate.asPredicate(p).test(Values.getString(value).orElse(null));
} else if (predicate instanceof BooleanPredicate) {
@@ -503,7 +479,7 @@ public class AssetQueryPredicate implements Predicate<AssetState> {
Predicate<MetaItem> metaItemPredicate = metaItem -> {
if (predicate.itemNamePredicate != null) {
- if (!metaItem.getName().map(name -> asPredicate(predicate.itemNamePredicate).test(name)).orElse(false)) {
+ if (!metaItem.getName().map(name -> StringPredicate.asPredicate(predicate.itemNamePredicate).test(name)).orElse(false)) {
return false;
}
}
diff --git a/model/src/main/java/org/openremote/model/attribute/AttributeLink.java b/model/src/main/java/org/openremote/model/attribute/AttributeLink.java
index 840541b09..1a70fa8f0 100644
--- a/model/src/main/java/org/openremote/model/attribute/AttributeLink.java
+++ b/model/src/main/java/org/openremote/model/attribute/AttributeLink.java
@@ -19,10 +19,9 @@
*/
package org.openremote.model.attribute;
-import org.openremote.model.value.ObjectValue;
-import org.openremote.model.value.Value;
-import org.openremote.model.value.ValueType;
-import org.openremote.model.value.Values;
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.openremote.model.value.*;
import java.util.Optional;
@@ -87,17 +86,15 @@ public class AttributeLink {
protected AttributeRef attributeRef;
protected ObjectValue converter;
+ protected ValueFilter[] filters;
- public AttributeLink(String entityId, String attributeName, ObjectValue converter) {
- this(new AttributeRef(entityId, attributeName), converter);
- }
-
- /**
- * @param converter can be <code>null</code> if no conversions are defined (i.e. value is direct write through).
- */
- public AttributeLink(AttributeRef attributeRef, ObjectValue converter) {
+ @JsonCreator
+ public AttributeLink(@JsonProperty("attributeRef") AttributeRef attributeRef,
+ @JsonProperty("converter") ObjectValue converter,
+ @JsonProperty("filters") ValueFilter[] filters) {
this.attributeRef = requireNonNull(attributeRef);
this.converter = converter;
+ this.filters = filters;
}
public AttributeRef getAttributeRef() {
@@ -112,11 +109,16 @@ public class AttributeLink {
this.converter = converter;
}
- public ObjectValue toObjectValue() {
- ObjectValue objectValue = Values.createObject();
- objectValue.put("attributeRef", getAttributeRef().toArrayValue());
- objectValue.put("converter", converter);
- return objectValue;
+ public void setAttributeRef(AttributeRef attributeRef) {
+ this.attributeRef = attributeRef;
+ }
+
+ public ValueFilter[] getFilters() {
+ return filters;
+ }
+
+ public void setFilters(ValueFilter[] filters) {
+ this.filters = filters;
}
@Override
@@ -135,14 +137,4 @@ public class AttributeLink {
})
.orElse(false);
}
-
- @SuppressWarnings("ConstantConditions")
- public static Optional<AttributeLink> fromValue(Value value) {
- return Values.getObject(value)
- .filter(AttributeLink::isAttributeLink)
- .flatMap(objectValue ->
- AttributeRef.fromValue(objectValue.get("attributeRef").get())
- .map(attrRef -> new AttributeLink(attrRef, objectValue.getObject("converter").orElse(null)))
- );
- }
}
\ No newline at end of file
diff --git a/model/src/main/java/org/openremote/model/attribute/AttributeRef.java b/model/src/main/java/org/openremote/model/attribute/AttributeRef.java
index d2ab78e0b..0227ae504 100644
--- a/model/src/main/java/org/openremote/model/attribute/AttributeRef.java
+++ b/model/src/main/java/org/openremote/model/attribute/AttributeRef.java
@@ -19,6 +19,9 @@
*/
package org.openremote.model.attribute;
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.JsonProperty;
import org.openremote.model.AbstractValueHolder;
import org.openremote.model.value.ArrayValue;
import org.openremote.model.value.Value;
@@ -37,6 +40,7 @@ import static org.openremote.model.util.TextUtil.requireNonNullAndNonEmpty;
* Two attribute references are {@link #equals} if they reference the same entity
* and attribute.
*/
+@JsonFormat(shape= JsonFormat.Shape.ARRAY)
public class AttributeRef {
protected String entityId;
@@ -45,7 +49,9 @@ public class AttributeRef {
protected AttributeRef() {
}
- public AttributeRef(String entityId, String attributeName) {
+ @JsonCreator
+ public AttributeRef(@JsonProperty("entityId") String entityId,
+ @JsonProperty("attributeName") String attributeName) {
requireNonNullAndNonEmpty(entityId);
requireNonNullAndNonEmpty(attributeName);
this.entityId = entityId;
diff --git a/model/src/main/java/org/openremote/model/attribute/MetaItemType.java b/model/src/main/java/org/openremote/model/attribute/MetaItemType.java
index dcca2304b..9b38aa210 100644
--- a/model/src/main/java/org/openremote/model/attribute/MetaItemType.java
+++ b/model/src/main/java/org/openremote/model/attribute/MetaItemType.java
@@ -24,16 +24,16 @@ import org.openremote.model.asset.AssetModelProvider;
import org.openremote.model.asset.UserAsset;
import org.openremote.model.rules.AssetState;
import org.openremote.model.rules.TemporaryFact;
-import org.openremote.model.value.Value;
-import org.openremote.model.value.ValueType;
-import org.openremote.model.value.Values;
+import org.openremote.model.value.*;
import java.util.Optional;
import java.util.function.Function;
import static org.openremote.model.Constants.ASSET_META_NAMESPACE;
+import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
import static org.openremote.model.attribute.MetaItem.MetaItemFailureReason.META_ITEM_VALUE_MISMATCH;
import static org.openremote.model.attribute.MetaItemDescriptor.Access.ACCESS_PRIVATE;
+import static org.openremote.model.attribute.MetaItemDescriptorImpl.metaItemArray;
import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_DOUBLE;
import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_INTEGER_POSITIVE_NON_ZERO;
@@ -401,6 +401,23 @@ public enum MetaItemType implements MetaItemDescriptor {
null,
null,
null,
+ false),
+
+ /**
+ * {@link MetaItem} for defining {@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 or to filter value when updating an attribute
+ * linked attribute see {@link #ATTRIBUTE_LINK}); 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.
+ */
+ VALUE_FILTERS(
+ ASSET_META_NAMESPACE + ":valueFilters",
+ new Access(true, false, true),
+ ValueType.ARRAY,
+ null,
+ null,
+ null,
false);
final protected String urn;
diff --git a/model/src/main/java/org/openremote/model/event/shared/EventSubscription.java b/model/src/main/java/org/openremote/model/event/shared/EventSubscription.java
index 2e0eb9ce8..24b8afb34 100644
--- a/model/src/main/java/org/openremote/model/event/shared/EventSubscription.java
+++ b/model/src/main/java/org/openremote/model/event/shared/EventSubscription.java
@@ -19,6 +19,7 @@
*/
package org.openremote.model.event.shared;
+import com.fasterxml.jackson.annotation.JsonIgnore;
import org.openremote.model.event.Event;
import org.openremote.model.event.TriggeredEventSubscription;
@@ -48,6 +49,7 @@ public class EventSubscription<E extends SharedEvent> {
/**
* Optional only set when an internal subscription is made
*/
+ @JsonIgnore
protected Consumer<TriggeredEventSubscription<E>> internalConsumer;
protected EventSubscription() {
@@ -116,7 +118,7 @@ public class EventSubscription<E extends SharedEvent> {
return getClass().getSimpleName() + "{" +
"eventType='" + eventType + '\'' +
", filter=" + filter +
- "subscriptionId='" + subscriptionId + '\'' +
+ ", subscriptionId='" + subscriptionId + '\'' +
'}';
}
}
diff --git a/model/src/main/java/org/openremote/model/query/filter/StringPredicate.java b/model/src/main/java/org/openremote/model/query/filter/StringPredicate.java
index 08c0db348..443eb5691 100644
--- a/model/src/main/java/org/openremote/model/query/filter/StringPredicate.java
+++ b/model/src/main/java/org/openremote/model/query/filter/StringPredicate.java
@@ -24,6 +24,7 @@ import org.openremote.model.value.ObjectValue;
import org.openremote.model.value.Values;
import java.util.Locale;
+import java.util.function.Predicate;
public class StringPredicate implements ValuePredicate {
@@ -68,6 +69,30 @@ public class StringPredicate implements ValuePredicate {
return stringPredicate;
}
+ public static Predicate<String> asPredicate(StringPredicate predicate) {
+ return string -> {
+ if (string == null && predicate.value == null)
+ return true;
+ if (string == null)
+ return false;
+ if (predicate.value == null)
+ return false;
+
+ String shouldMatch = predicate.caseSensitive ? predicate.value : predicate.value.toUpperCase(Locale.ROOT);
+ String have = predicate.caseSensitive ? string : string.toUpperCase(Locale.ROOT);
+
+ switch (predicate.match) {
+ case BEGIN:
+ return predicate.negate != have.startsWith(shouldMatch);
+ case END:
+ return predicate.negate != have.endsWith(shouldMatch);
+ case CONTAINS:
+ return predicate.negate != have.contains(shouldMatch);
+ }
+ return predicate.negate != have.equals(shouldMatch);
+ };
+ }
+
public StringPredicate match(BaseAssetQuery.Match match) {
this.match = match;
return this;
diff --git a/model/src/main/java/org/openremote/model/value/JsonPathFilter.java b/model/src/main/java/org/openremote/model/value/JsonPathFilter.java
new file mode 100644
index 000000000..0dbcf467b
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/value/JsonPathFilter.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2017, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.model.value;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonTypeName;
+import org.openremote.model.util.TextUtil;
+
+import java.util.Arrays;
+import java.util.List;
+
+import static org.openremote.model.value.RegexValueFilter.NAME;
+
+@JsonTypeName(NAME)
+public class JsonPathFilter extends ValueFilter<Value> {
+
+ public static final String NAME = "jsonPath";
+
+ @JsonProperty
+ public String path;
+
+ @JsonCreator
+ public JsonPathFilter(@JsonProperty("path") String path) {
+ this.path = path;
+ }
+
+ @Override
+ public Class<Value> getValueType() {
+ return Value.class;
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/value/JsonValueFilter.java b/model/src/main/java/org/openremote/model/value/JsonValueFilter.java
deleted file mode 100644
index 3478c3600..000000000
--- a/model/src/main/java/org/openremote/model/value/JsonValueFilter.java
+++ /dev/null
@@ -1,84 +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.model.value;
-
-import com.fasterxml.jackson.annotation.JsonCreator;
-import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.annotation.JsonTypeName;
-import org.openremote.model.util.TextUtil;
-
-import java.util.Arrays;
-import java.util.List;
-
-import static org.openremote.model.value.RegexValueFilter.NAME;
-
-@JsonTypeName(NAME)
-public class JsonValueFilter extends ValueFilter<ObjectValue> {
-
- public static final String NAME = "json";
-
- @JsonProperty
- protected List<String> path;
-
- @JsonCreator
- public JsonValueFilter(@JsonProperty("path") List<String> path) {
- this.path = path;
- }
-
- public JsonValueFilter(String... path) {
- this(Arrays.asList(path));
- }
-
- @Override
- public Class<ObjectValue> getMessageType() {
- return ObjectValue.class;
- }
-
- @Override
- public Value process(ObjectValue value) {
- if (value == null || path == null || path.isEmpty()) {
- return null;
- }
-
- Value currentValue = value;
- for (int i = 0; i < path.size(); i++) {
- String pathSegment = path.get(i);
- if (currentValue == null || TextUtil.isNullOrEmpty(pathSegment)) {
- return null;
- }
-
- if (TextUtil.INTEGER_POSITIVE_VALIDATOR.test(pathSegment)) {
- // Check value is an array
- if (currentValue.getType() != ValueType.ARRAY) {
- // Integer key requires an array value
- return null;
- }
- currentValue = ((ArrayValue)currentValue).get(Integer.parseInt(pathSegment)).orElse(null);
- } else {
- if (currentValue.getType() != ValueType.OBJECT) {
- return null;
- }
- currentValue = ((ObjectValue)currentValue).get(pathSegment).orElse(null);
- }
- }
-
- return currentValue;
- }
-}
diff --git a/model/src/main/java/org/openremote/model/value/RegexValueFilter.java b/model/src/main/java/org/openremote/model/value/RegexValueFilter.java
index 12d566ed6..201955bc3 100644
--- a/model/src/main/java/org/openremote/model/value/RegexValueFilter.java
+++ b/model/src/main/java/org/openremote/model/value/RegexValueFilter.java
@@ -34,12 +34,9 @@ public class RegexValueFilter extends ValueFilter<StringValue> {
public static final String NAME = "regex";
- @JsonProperty
- protected Pattern pattern;
- @JsonProperty
- protected int matchGroup;
- @JsonProperty
- protected int matchIndex;
+ public Pattern pattern;
+ public int matchGroup;
+ public int matchIndex;
@JsonCreator
public RegexValueFilter(@JsonProperty("pattern") String regex,
@@ -59,32 +56,7 @@ public class RegexValueFilter extends ValueFilter<StringValue> {
}
@Override
- public Class<StringValue> getMessageType() {
+ public Class<StringValue> getValueType() {
return StringValue.class;
}
-
- @Override
- public Value process(StringValue value) {
- if (value == null || pattern == null) {
- return null;
- }
-
- String filteredStr = null;
- Matcher matcher = pattern.matcher(value.getString());
- int matchIndex = 0;
- boolean matched = matcher.find();
-
- while(matched && matchIndex<this.matchIndex) {
- matched = matcher.find();
- matchIndex++;
- }
-
- if (matched) {
- if (matchGroup <= matcher.groupCount()) {
- filteredStr = matcher.group(matchGroup);
- }
- }
-
- return filteredStr == null ? null : Values.create(filteredStr);
- }
}
diff --git a/model/src/main/java/org/openremote/model/value/SubStringValueFilter.java b/model/src/main/java/org/openremote/model/value/SubStringValueFilter.java
index 5f84cd57f..27acb9d43 100644
--- a/model/src/main/java/org/openremote/model/value/SubStringValueFilter.java
+++ b/model/src/main/java/org/openremote/model/value/SubStringValueFilter.java
@@ -30,10 +30,8 @@ public class SubStringValueFilter extends ValueFilter<StringValue> {
public static final String NAME = "substring";
- @JsonProperty
- protected int beginIndex;
- @JsonProperty
- protected Integer endIndex;
+ public int beginIndex;
+ public Integer endIndex;
@JsonCreator
public SubStringValueFilter(@JsonProperty("beginIndex") int beginIndex, @JsonProperty("endIndex") Integer endIndex) {
@@ -46,26 +44,7 @@ public class SubStringValueFilter extends ValueFilter<StringValue> {
}
@Override
- public Class<StringValue> getMessageType() {
+ public Class<StringValue> getValueType() {
return StringValue.class;
}
-
- @Override
- public Value process(StringValue value) {
- if (value == null) {
- return null;
- }
-
- String result = null;
-
- try {
- if (endIndex != null) {
- result = value.getString().substring(beginIndex, endIndex);
- } else {
- result = value.getString().substring(beginIndex);
- }
- } catch (IndexOutOfBoundsException ignored) {}
-
- return result == null ? null : Values.create(result);
- }
}
diff --git a/model/src/main/java/org/openremote/model/value/ValueFilter.java b/model/src/main/java/org/openremote/model/value/ValueFilter.java
index c4b7775c7..693b9bbc2 100644
--- a/model/src/main/java/org/openremote/model/value/ValueFilter.java
+++ b/model/src/main/java/org/openremote/model/value/ValueFilter.java
@@ -32,22 +32,16 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo;
* <li>If filter throws an exception then handle and treat as if filter returned null</li>
* </ul>
*/
-@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property = "type")
+@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(name = RegexValueFilter.NAME, value = RegexValueFilter.class),
@JsonSubTypes.Type(name = SubStringValueFilter.NAME, value = SubStringValueFilter.class),
- @JsonSubTypes.Type(name = JsonValueFilter.NAME, value = JsonValueFilter.class)
+ @JsonSubTypes.Type(name = JsonPathFilter.NAME, value = JsonPathFilter.class)
})
public abstract class ValueFilter<T extends Value> {
/**
- * Get the message type
+ * Get the value type
*/
- public abstract Class<T> getMessageType();
-
- /**
- * Apply this filter to the supplied message
- */
- public abstract Value process(T message);
-
+ public abstract Class<T> getValueType();
}
diff --git a/test/src/main/groovy/org/openremote/test/ManagerContainerTrait.groovy b/test/src/main/groovy/org/openremote/test/ManagerContainerTrait.groovy
index de2edcdf5..603a94be4 100644
--- a/test/src/main/groovy/org/openremote/test/ManagerContainerTrait.groovy
+++ b/test/src/main/groovy/org/openremote/test/ManagerContainerTrait.groovy
@@ -26,8 +26,7 @@ trait ManagerContainerTrait extends ContainerTrait {
static Map<String, String> defaultConfig(int serverPort) {
[
(WEBSERVER_LISTEN_PORT) : Integer.toString(serverPort),
- (IDENTITY_NETWORK_HOST) : KeycloakIdentityProvider.KEYCLOAK_HOST_DEFAULT,
- (IDENTITY_NETWORK_WEBSERVER_PORT): Integer.toString(KeycloakIdentityProvider.KEYCLOAK_PORT_DEFAULT),
+ (IDENTITY_NETWORK_WEBSERVER_PORT): Integer.toString(serverPort),
(SETUP_IMPORT_DEMO_SCENES) : "false",
(SETUP_IMPORT_DEMO_RULES) : "false"
]
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetAttributeLinkingTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetAttributeLinkingTest.groovy
index 7f573bbc7..26ca960a5 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetAttributeLinkingTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetAttributeLinkingTest.groovy
@@ -1,5 +1,6 @@
package org.openremote.test.assets
+import org.openremote.container.Container
import org.openremote.manager.asset.AssetProcessingService
import org.openremote.manager.asset.AssetStorageService
@@ -8,6 +9,8 @@ import org.openremote.model.asset.Asset
import org.openremote.model.asset.AssetAttribute
import org.openremote.model.asset.AssetType
import org.openremote.model.attribute.*
+import org.openremote.model.value.JsonPathFilter
+import org.openremote.model.value.ValueFilter
import org.openremote.model.value.Values
import org.openremote.test.ManagerContainerTrait
import spock.lang.Specification
@@ -37,13 +40,15 @@ class AssetAttributeLinkingTest extends Specification implements ManagerContaine
when: "assets are created"
def asset1 = new Asset("Asset 1", AssetType.THING, null, Constants.MASTER_REALM)
asset1.setAttributes(
- new AssetAttribute("button", AttributeValueType.STRING, Values.create("RELEASED"), getClockTimeOf(container))
+ new AssetAttribute("button", AttributeValueType.STRING, Values.create("RELEASED"), getClockTimeOf(container)),
+ new AssetAttribute("array", AttributeValueType.ARRAY, null)
)
asset1 = assetStorageService.merge(asset1)
def asset2 = new Asset("Asset 2", AssetType.THING, null, Constants.MASTER_REALM)
asset2.setAttributes(
- new AssetAttribute("lightOnOff", AttributeValueType.BOOLEAN, Values.create(false), getClockTimeOf(container)),
- new AssetAttribute("counter", AttributeValueType.NUMBER, Values.create(0), getClockTimeOf(container))
+ new AssetAttribute("lightOnOff", AttributeValueType.BOOLEAN, Values.create(false), getClockTimeOf(container)),
+ new AssetAttribute("counter", AttributeValueType.NUMBER, Values.create(0), getClockTimeOf(container)),
+ new AssetAttribute("item2Prop1", AttributeValueType.BOOLEAN, null)
)
asset2 = assetStorageService.merge(asset2)
@@ -51,7 +56,7 @@ class AssetAttributeLinkingTest extends Specification implements ManagerContaine
assert asset1.id != null
assert asset2.id != null
- when: "an attribute from one asset is linked to an attribute on the other"
+ when: "attributes from one asset is linked to attributes on the other"
def converterOnOff = Values.createObject()
converterOnOff.put("PRESSED", "@TOGGLE")
converterOnOff.put("RELEASED", "@IGNORE")
@@ -68,8 +73,14 @@ class AssetAttributeLinkingTest extends Specification implements ManagerContaine
attributeLinkCounter.put("attributeRef", new AttributeRef(asset2.id, "counter").toArrayValue())
attributeLinkCounter.put("converter", converterCounter)
+ def attributeLinkProp = Values.parse(Container.JSON.writeValueAsString(new AttributeLink(
+ new AttributeRef(asset2.id, "item2Prop1"), null, [
+ new JsonPathFilter("\$[1].prop1")
+ ] as ValueFilter[]))).orElse(null)
+
asset1.getAttribute("button").get().addMeta(new MetaItem(MetaItemType.ATTRIBUTE_LINK, attributeLinkOnOff))
asset1.getAttribute("button").get().addMeta(new MetaItem(MetaItemType.ATTRIBUTE_LINK, attributeLinkCounter))
+ asset1.getAttribute("array").get().addMeta(new MetaItem(MetaItemType.ATTRIBUTE_LINK, attributeLinkProp))
asset1 = assetStorageService.merge(asset1)
and: "the button is pressed for a short period"
@@ -189,6 +200,15 @@ class AssetAttributeLinkingTest extends Specification implements ManagerContaine
}
*/
+ when: "the array attribute is written to"
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(asset1.id, "array", Values.parse("[{\"prop1\": true, \"prop2\": \"a\"},{\"prop1\": false, \"prop2\": \"b\"}]").orElse(null)))
+
+ then: "the linked attribute on the other asset should contain the value from the json path"
+ conditions.eventually {
+ asset2 = assetStorageService.find(asset2.id, true)
+ assert !asset2.getAttribute("item2Prop1").get().getValueAsBoolean().orElse(true)
+ }
+
cleanup: "the server should be stopped"
stopContainer(container)
}
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 50bace3e3..fe578e93b 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovy
@@ -20,7 +20,6 @@
package org.openremote.test.protocol
import org.openremote.agent.protocol.AbstractProtocol
-import org.openremote.agent.protocol.Protocol
import org.openremote.container.util.Util
import org.openremote.manager.agent.AgentService
import org.openremote.manager.asset.AssetProcessingService
@@ -311,7 +310,7 @@ class BasicProtocolTest extends Specification implements ManagerContainerTrait,
new AttributeRef(mockAgent.getId(), "mockConfig1").toArrayValue()
),
new MetaItem(
- Protocol.META_VALUE_FILTERS,
+ MetaItemType.META_VALUE_FILTERS,
Values.createArray().add(Util.objectToValue(new RegexValueFilter("\\w(\\d+)", 1, 2)).get())
)
),
@@ -323,7 +322,7 @@ class BasicProtocolTest extends Specification implements ManagerContainerTrait,
new AttributeRef(mockAgent.getId(), "mockConfig1").toArrayValue()
),
new MetaItem(
- Protocol.META_VALUE_FILTERS,
+ MetaItemType.META_VALUE_FILTERS,
Values.createArray().add(Util.objectToValue(new SubStringValueFilter(10, 12)).get())
)
),
@@ -335,7 +334,7 @@ class BasicProtocolTest extends Specification implements ManagerContainerTrait,
new AttributeRef(mockAgent.getId(), "mockConfig1").toArrayValue()
),
new MetaItem(
- Protocol.META_VALUE_FILTERS,
+ MetaItemType.META_VALUE_FILTERS,
Values.createArray()
.add(Util.objectToValue(new SubStringValueFilter(23)).get())
.add(Util.objectToValue(new RegexValueFilter("[a-z|\\s]+(\\d+)%\"}", 1, 0)).get())
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 f2d2460ee..3162045e3 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/TcpClientTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/TcpClientTest.groovy
@@ -35,7 +35,7 @@ import spock.util.concurrent.PollingConditions
*/
class TcpClientTest extends Specification implements ManagerContainerTrait {
- def "Check socket client"() {
+ def "Check client"() {
given: "expected conditions"
def conditions = new PollingConditions(timeout: 10, delay: 1)
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 c2b4b7daf..e3118ef8b 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
@@ -305,7 +305,7 @@ class HttpClientProtocolTest extends Specification implements ManagerContainerTr
Values.create("https://mockapi")
),
new MetaItem(
- HttpClientProtocol.META_PROTOCOL_OAUTH_GRANT,
+ Protocol.META_PROTOCOL_OAUTH_GRANT,
new OAuthPasswordGrant("https://mockapi/token",
"TestClient",
"TestSecret",
@@ -376,7 +376,7 @@ class HttpClientProtocolTest extends Specification implements ManagerContainerTr
Values.create("https://mockapi")
),
new MetaItem(
- HttpClientProtocol.META_PROTOCOL_OAUTH_GRANT,
+ Protocol.META_PROTOCOL_OAUTH_GRANT,
new OAuthPasswordGrant("https://mockapi/token",
"TestClient",
"TestSecret",
@@ -488,7 +488,7 @@ class HttpClientProtocolTest extends Specification implements ManagerContainerTr
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(
- Protocol.META_VALUE_FILTERS,
+ MetaItemType.META_VALUE_FILTERS,
Values.createArray().add(Util.objectToValue(new RegexValueFilter("\\d+", 0, 0)).get())
)
),
@@ -499,7 +499,7 @@ class HttpClientProtocolTest extends Specification implements ManagerContainerTr
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(
- Protocol.META_VALUE_FILTERS,
+ MetaItemType.META_VALUE_FILTERS,
Values.createArray().add(Util.objectToValue(new RegexValueFilter("\\d+", 0, 1)).get())
)
)
@@ -548,7 +548,7 @@ class HttpClientProtocolTest extends Specification implements ManagerContainerTr
Values.create("https://mockapi")
),
new MetaItem(
- HttpClientProtocol.META_PROTOCOL_OAUTH_GRANT,
+ Protocol.META_PROTOCOL_OAUTH_GRANT,
new OAuthPasswordGrant("https://mockapi/token",
"TestClient",
"TestSecret",
@@ -570,7 +570,7 @@ class HttpClientProtocolTest extends Specification implements ManagerContainerTr
Values.create("https://mockapi")
),
new MetaItem(
- HttpClientProtocol.META_PROTOCOL_OAUTH_GRANT,
+ Protocol.META_PROTOCOL_OAUTH_GRANT,
new OAuthPasswordGrant("https://mockapi/token",
"TestClient",
"TestSecret",
@@ -592,7 +592,7 @@ class HttpClientProtocolTest extends Specification implements ManagerContainerTr
Values.create("https://mockapi")
),
new MetaItem(
- HttpClientProtocol.META_PROTOCOL_OAUTH_GRANT,
+ Protocol.META_PROTOCOL_OAUTH_GRANT,
new OAuthPasswordGrant("https://mockapi/token",
"TestClient",
"TestSecret",
@@ -609,12 +609,12 @@ class HttpClientProtocolTest extends Specification implements ManagerContainerTr
def protocolCount = httpClientProtocol.linkedProtocolConfigurations.size()
agent = assetStorageService.merge(agent)
- then: "the protocol configuration should be linked with a connection status of unknown"
+ then: "the protocol configuration should be linked with a connection status of connected"
conditions.eventually {
assert httpClientProtocol.linkedProtocolConfigurations.size() == protocolCount + 3
- assert agentService.getProtocolConnectionStatus(new AttributeRef(agent.id, "protocolConfig2")) == ConnectionStatus.UNKNOWN
- assert agentService.getProtocolConnectionStatus(new AttributeRef(agent.id, "protocolConfig3")) == ConnectionStatus.UNKNOWN
- assert agentService.getProtocolConnectionStatus(new AttributeRef(agent.id, "protocolConfig4")) == ConnectionStatus.UNKNOWN
+ assert agentService.getProtocolConnectionStatus(new AttributeRef(agent.id, "protocolConfig2")) == ConnectionStatus.CONNECTED
+ assert agentService.getProtocolConnectionStatus(new AttributeRef(agent.id, "protocolConfig3")) == ConnectionStatus.CONNECTED
+ assert agentService.getProtocolConnectionStatus(new AttributeRef(agent.id, "protocolConfig4")) == ConnectionStatus.CONNECTED
}
when: "attributes are linked to these protocol configurations"
diff --git a/ui/component/core/src/event.ts b/ui/component/core/src/event.ts
index 55e091687..32d6aae49 100644
--- a/ui/component/core/src/event.ts
+++ b/ui/component/core/src/event.ts
@@ -8,7 +8,8 @@ import {
SharedEvent,
ReadAssetAttributesEvent,
AssetEvent,
- ReadAssetEvent
+ ReadAssetEvent,
+ TriggeredEventSubscription
} from "@openremote/model";
export enum EventProviderStatus {
@@ -501,20 +502,11 @@ export class WebSocketEventProvider extends EventProviderImpl {
}
} else if (msg.startsWith(EVENT_MESSAGE_PREFIX)) {
let str = msg.substring(EVENT_MESSAGE_PREFIX.length);
- let seperatorPos = str.indexOf(":");
- if (seperatorPos > 0) {
- let strArr = [str.substring(0, seperatorPos), str.substring(seperatorPos + 1)];
- let subscriptionId = strArr[0];
- let jsonStr = strArr[1];
- if (jsonStr.startsWith("[")) {
- let events = JSON.parse(jsonStr) as SharedEvent[];
- events.forEach(event => {
- this._onMessageReceived(subscriptionId, event);
- });
- } else {
- let event = JSON.parse(jsonStr) as SharedEvent;
- this._onMessageReceived(subscriptionId, event);
- }
+ let triggered = JSON.parse(str) as TriggeredEventSubscription<SharedEvent>;
+ if (triggered.events) {
+ triggered.events.forEach(event => {
+ this._onMessageReceived(triggered.subscriptionId!, event);
+ });
}
}
};