Skip to content

armgit / core

Public Imported from GitHub
0

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

Incomplete work on Simulator

Christian Bauerf364f23993f8
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 04d6189c9..150fa4886 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
@@ -21,19 +21,23 @@ package org.openremote.agent.protocol.simulator;
import org.openremote.agent.protocol.AbstractProtocol;
import org.openremote.agent.protocol.ConnectionStatus;
-import org.openremote.agent.protocol.simulator.element.*;
+import org.openremote.agent.protocol.simulator.element.ColorSimulatorElement;
+import org.openremote.agent.protocol.simulator.element.NumberSimulatorElement;
+import org.openremote.model.simulator.SimulatorElement;
+import org.openremote.agent.protocol.simulator.element.SwitchSimulatorElement;
import org.openremote.model.AbstractValueHolder;
+import org.openremote.model.ValidationFailure;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.attribute.AttributeRef;
import org.openremote.model.attribute.AttributeState;
+import org.openremote.model.simulator.SimulatorState;
+import org.openremote.model.util.Pair;
import org.openremote.model.value.Value;
-import java.util.HashMap;
-import java.util.Locale;
-import java.util.Map;
-import java.util.Optional;
+import java.util.*;
import java.util.logging.Logger;
+import java.util.stream.Collectors;
import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
import static org.openremote.model.asset.AssetMeta.RANGE_MAX;
@@ -89,8 +93,6 @@ public class SimulatorProtocol extends AbstractProtocol {
}
}
- protected static final Map<AttributeRef, SimulatorElement> elements = new HashMap<>();
-
private static final Logger LOG = Logger.getLogger(SimulatorProtocol.class.getName());
public static final int DEFAULT_WRITE_DELAY = 1000;
@@ -114,8 +116,8 @@ public class SimulatorProtocol extends AbstractProtocol {
public static final String CONFIG_WRITE_DELAY_MILLISECONDS = PROTOCOL_NAME + ":delayMilliseconds";
static final protected Map<AttributeRef, Instance> instances = new HashMap<>();
-
static final protected Map<AttributeRef, AttributeRef> attributeInstanceMap = new HashMap<>();
+ static final protected Map<AttributeRef, SimulatorElement> elements = new HashMap<>();
@Override
public String getProtocolName() {
@@ -171,32 +173,32 @@ public class SimulatorProtocol extends AbstractProtocol {
protected void doLinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
// Get element type from the attribute meta
- String elementType = getElementType(attribute)
- .orElseThrow(
- () -> new IllegalArgumentException(
- "Can't configure simulator, missing " + SIMULATOR_ELEMENT + " meta item on: " + attribute
- )
- );
+ Optional<String> elementType = getElementType(attribute);
+ if (!elementType.isPresent()) {
+ LOG.warning("Can't configure simulator, missing " + SIMULATOR_ELEMENT + " meta item on: " + attribute);
+ return;
+ }
AttributeRef configRef = protocolConfiguration.getReferenceOrThrow();
AttributeRef attributeRef = attribute.getReferenceOrThrow();
- SimulatorElement element = createElement(elementType, attribute);
+ SimulatorElement element = createElement(elementType.get(), attribute);
+ if (element == null) {
+ LOG.warning("Can't simulate element '" + elementType + "': " + attribute);
+ return;
+ }
if (attribute.getValue().isPresent()) {
- try {
- element.setState(attribute.getValue().get());
- } catch (IllegalArgumentException ex) {
- throw new RuntimeException("Error setting initial state of: " + attribute, ex);
+ List<ValidationFailure> failures = element.setValue(attribute.getValue().get());
+ if (!failures.isEmpty()) {
+ LOG.warning("Failed to initialize simulator element, initial value validation failures " + failures + ": " + attribute);
+ return;
}
}
LOG.info("Putting element '" + element + "' for: " + attribute);
- synchronized (elements) {
+ synchronized (instances) {
elements.put(attributeRef, element);
- }
-
- synchronized (attributeInstanceMap) {
attributeInstanceMap.put(attributeRef, configRef);
}
}
@@ -205,18 +207,15 @@ public class SimulatorProtocol extends AbstractProtocol {
protected void doUnlinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
AttributeRef attributeRef = attribute.getReferenceOrThrow();
- synchronized (elements) {
+ synchronized (instances) {
elements.remove(attributeRef);
- }
-
- synchronized (attributeInstanceMap) {
attributeInstanceMap.remove(attributeRef);
}
}
@Override
protected void processLinkedAttributeWrite(AttributeEvent event, AssetAttribute protocolConfiguration) {
- putState(event.getAttributeState());
+ putValue(event.getAttributeState());
}
/**
@@ -244,12 +243,15 @@ public class SimulatorProtocol extends AbstractProtocol {
* Call this to simulate a sensor update after the specified delay (it uses the last value supplied from send to actuator).
*/
public void updateSensor(AttributeRef attributeRef, int updateSensorDelayMilliseconds) {
- Value value = getState(attributeRef);
- final AttributeState state = new AttributeState(attributeRef, value);
+ Optional<Value> value = getValue(attributeRef);
+ if (!value.isPresent())
+ return;
+ final AttributeState state = new AttributeState(attributeRef, value.get());
AttributeRef instanceRef = attributeInstanceMap.get(attributeRef);
if (instanceRef == null) {
- throw new IllegalArgumentException("Attribute is not referenced by an instance:" + attributeRef);
+ LOG.warning("Attribute is not referenced by an instance:" + attributeRef);
+ return;
}
Instance instance;
@@ -259,7 +261,8 @@ public class SimulatorProtocol extends AbstractProtocol {
}
if (instance == null) {
- throw new IllegalArgumentException("No instance found by name '" + instanceRef + "'");
+ LOG.warning("No instance found by name '" + instanceRef + "'");
+ return;
}
if (!instance.isEnabled()) {
@@ -277,58 +280,66 @@ public class SimulatorProtocol extends AbstractProtocol {
/**
* Call this to simulate a send to actuator.
*/
- public void putState(String entityId, String attributeName, Value value) {
- putState(new AttributeState(new AttributeRef(entityId, attributeName), value));
+ public void putValue(String entityId, String attributeName, Value value) {
+ putValue(new AttributeState(new AttributeRef(entityId, attributeName), value));
}
/**
* Call this to simulate a send to actuator.
*/
- protected void putState(AttributeRef attributeRef, Value value) {
- putState(new AttributeState(attributeRef, value));
+ protected void putValue(AttributeRef attributeRef, Value value) {
+ putValue(new AttributeState(attributeRef, value));
}
/**
* Call this to simulate a send to actuator.
*/
- public void putState(AttributeEvent event) {
- putState(event.getAttributeState());
+ public void putValue(AttributeEvent event) {
+ putValue(event.getAttributeState());
}
/**
* Call this to simulate a send to actuator.
*/
- public void putState(AttributeState attributeState) {
+ public void putValue(AttributeState attributeState) {
AttributeRef attributeRef = attributeState.getAttributeRef();
AttributeRef instanceRef = attributeInstanceMap.get(attributeRef);
if (instanceRef == null) {
- throw new IllegalArgumentException("Attribute is not referenced by an instance:" + attributeRef);
+ LOG.warning("Attribute is not referenced by an instance:" + attributeRef);
+ return;
}
Instance instance;
synchronized (instances) {
instance = instances.get(instanceRef);
- }
- if (instance == null) {
- throw new IllegalArgumentException("No instance found by name '" + instanceRef + "'");
- }
+ if (instance == null) {
+ LOG.warning("No instance found by name '" + instanceRef + "'");
+ return;
+ }
- if (!instance.isEnabled()) {
- LOG.fine("Simulator protocol configuration is disabled so cannot process request");
- return;
- }
+ if (!instance.isEnabled()) {
+ LOG.fine("Simulator protocol configuration is disabled so cannot process request");
+ return;
+ }
- synchronized (elements) {
- LOG.info("Put simulator state: " + attributeState);
+ LOG.info("Put simulator value: " + attributeState);
SimulatorElement element = elements.get(attributeRef);
if (element == null) {
- throw new IllegalArgumentException("No simulated element for: " + attributeRef);
+ LOG.warning("No simulated element for: " + attributeRef);
+ return;
}
- element.setState(attributeState.getCurrentValue().orElse(null));
+ List<ValidationFailure> failures = element.setValue(attributeState.getCurrentValue().orElse(null));
+ if (!failures.isEmpty()) {
+ if (!failures.isEmpty()) {
+ LOG.warning("Failed to update simulator element, state validation failures " + failures + ": " + attributeRef);
+ return;
+ }
+ return;
+ }
}
if (instance.getMode() != Mode.MANUAL) {
@@ -339,36 +350,55 @@ public class SimulatorProtocol extends AbstractProtocol {
/**
* Call this to get the current value of an attribute.
*/
- public Value getState(String entityId, String attributeName) {
- return getState(new AttributeRef(entityId, attributeName));
+ public Optional<Value> getValue(String entityId, String attributeName) {
+ return getValue(new AttributeRef(entityId, attributeName));
}
/**
* Call this to get the current value of an attribute.
*/
- public Value getState(AttributeRef attributeRef) {
- synchronized (elements) {
+ public Optional<Value> getValue(AttributeRef attributeRef) {
+ synchronized (instances) {
SimulatorElement element = elements.get(attributeRef);
- if (element == null)
- throw new IllegalArgumentException("No simulated element for: " + attributeRef);
- return element.getState();
+ return element != null ? Optional.ofNullable(element.getValue()) : Optional.empty();
+ }
+ }
+
+ /**
+ * Get attributes linked to the given protocol configuration and their {@link SimulatorElement}s.
+ */
+ public Optional<SimulatorState> getSimulatorState(AttributeRef protocolConfigurationRef) {
+ synchronized (instances) {
+ if (!instances.containsKey(protocolConfigurationRef))
+ return Optional.empty();
+ List<AttributeRef> linkedAttributes = attributeInstanceMap.entrySet().stream()
+ .filter(entry -> entry.getValue().equals(protocolConfigurationRef))
+ .map(Map.Entry::getKey)
+ .collect(Collectors.toList());
+ List<SimulatorElement> result = new ArrayList<>();
+ for (AttributeRef linkedAttribute : linkedAttributes) {
+ if (elements.containsKey(linkedAttribute)) {
+ result.add(elements.get(linkedAttribute));
+ }
+ }
+ return Optional.of(new SimulatorState(result.toArray(new SimulatorElement[result.size()])));
}
}
protected SimulatorElement createElement(String elementType, AssetAttribute attribute) {
switch (elementType.toLowerCase(Locale.ROOT)) {
case SwitchSimulatorElement.ELEMENT_NAME:
- return new SwitchSimulatorElement();
+ return new SwitchSimulatorElement(attribute.getReferenceOrThrow());
case NumberSimulatorElement.ELEMENT_NAME:
- return new NumberSimulatorElement();
+ return new NumberSimulatorElement(attribute.getReferenceOrThrow());
case NumberSimulatorElement.ELEMENT_NAME_RANGE:
int min = attribute.getMetaItem(RANGE_MIN).flatMap(AbstractValueHolder::getValueAsInteger).orElse(0);
int max = attribute.getMetaItem(RANGE_MAX).flatMap(AbstractValueHolder::getValueAsInteger).orElse(100);
- return new NumberSimulatorElement(min, max);
+ return new NumberSimulatorElement(attribute.getReferenceOrThrow(), min, max);
case ColorSimulatorElement.ELEMENT_NAME:
- return new ColorSimulatorElement();
+ return new ColorSimulatorElement(attribute.getReferenceOrThrow());
default:
- throw new UnsupportedOperationException("Can't simulate element '" + elementType + "': " + attribute);
+ return null;
}
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/simulator/element/ColorSimulatorElement.java b/agent/src/main/java/org/openremote/agent/protocol/simulator/element/ColorSimulatorElement.java
index d79f60bca..71da6cbb3 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/simulator/element/ColorSimulatorElement.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/simulator/element/ColorSimulatorElement.java
@@ -19,13 +19,15 @@
*/
package org.openremote.agent.protocol.simulator.element;
+import org.openremote.model.attribute.AttributeRef;
import org.openremote.model.attribute.AttributeType;
+import org.openremote.model.simulator.SimulatorElement;
public class ColorSimulatorElement extends SimulatorElement {
public static final String ELEMENT_NAME = "color";
- public ColorSimulatorElement() {
- super(AttributeType.COLOR_RGB);
+ public ColorSimulatorElement(AttributeRef attributeRef) {
+ super(attributeRef, AttributeType.COLOR_RGB);
}
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/simulator/element/NumberSimulatorElement.java b/agent/src/main/java/org/openremote/agent/protocol/simulator/element/NumberSimulatorElement.java
index afaa60c34..13d5f536b 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/simulator/element/NumberSimulatorElement.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/simulator/element/NumberSimulatorElement.java
@@ -19,24 +19,33 @@
*/
package org.openremote.agent.protocol.simulator.element;
+import org.openremote.model.ValidationFailure;
+import org.openremote.model.attribute.AttributeRef;
import org.openremote.model.attribute.AttributeType;
+import org.openremote.model.simulator.SimulatorElement;
import org.openremote.model.value.Value;
import org.openremote.model.value.Values;
+import java.util.List;
+
public class NumberSimulatorElement extends SimulatorElement {
+ public enum ValueValidationFailure implements ValidationFailure {
+ NUMBER_OUT_OF_RANGE
+ }
+
public static final String ELEMENT_NAME = "number";
public static final String ELEMENT_NAME_RANGE = "range";
final protected Integer min;
final protected Integer max;
- public NumberSimulatorElement() {
- this(null, null);
+ public NumberSimulatorElement(AttributeRef attributeRef) {
+ this(attributeRef, null, null);
}
- public NumberSimulatorElement(Integer min, Integer max) {
- super(AttributeType.NUMBER);
+ public NumberSimulatorElement(AttributeRef attributeRef, Integer min, Integer max) {
+ super(attributeRef, AttributeType.NUMBER);
this.min = min;
this.max = max;
}
@@ -50,14 +59,12 @@ public class NumberSimulatorElement extends SimulatorElement {
}
@Override
- protected boolean isValid(Value value) {
- boolean valid = super.isValid(value);
- return valid && (
- value == null || (
- (getMin() == null || Values.getNumber(value).filter(v -> v >= getMin()).isPresent())
- && (getMax() == null || Values.getNumber(value).filter(v -> v <= getMax()).isPresent())
- )
- );
+ protected List<ValidationFailure> getValidationFailures(Value value) {
+ List<ValidationFailure> failures = super.getValidationFailures(value);
+ if ((getMin() != null && Values.getNumber(value).filter(v -> v < getMin()).isPresent())
+ && (getMax() != null && Values.getNumber(value).filter(v -> v > getMax()).isPresent())) {
+ failures.add(ValueValidationFailure.NUMBER_OUT_OF_RANGE);
+ }
+ return failures;
}
-
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/simulator/element/SimulatorElement.java b/agent/src/main/java/org/openremote/agent/protocol/simulator/element/SimulatorElement.java
deleted file mode 100644
index deb0ce84a..000000000
--- a/agent/src/main/java/org/openremote/agent/protocol/simulator/element/SimulatorElement.java
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * 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.agent.protocol.simulator.element;
-
-import org.openremote.model.attribute.AttributeType;
-import org.openremote.model.value.Value;
-
-public abstract class SimulatorElement<T> {
-
- final protected AttributeType expectedType;
-
- protected Value state = null;
-
- public SimulatorElement(AttributeType expectedType) {
- this.expectedType = expectedType;
- }
-
- public Value getState() {
- return state;
- }
-
- public void setState(Value value) {
- if (value != null) {
- if (!isValid(value)) {
- throw new IllegalArgumentException(
- "Invalid state, expected JSON type '" + expectedType + "' but got '" + value.getType() + "' on: " + this
- );
- }
- this.state = value;
- } else {
- this.state = null;
- }
- }
-
- protected boolean isValid(Value value) {
- return value == null || value.getType() == expectedType.getValueType();
- }
-
- @Override
- public String toString() {
- return getClass().getSimpleName() + "{" +
- "state=" + (getState() != null ? getState().toJson() : "null") +
- "}";
- }
-}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/simulator/element/SwitchSimulatorElement.java b/agent/src/main/java/org/openremote/agent/protocol/simulator/element/SwitchSimulatorElement.java
index 6fa191cb2..23fcb0902 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/simulator/element/SwitchSimulatorElement.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/simulator/element/SwitchSimulatorElement.java
@@ -19,13 +19,15 @@
*/
package org.openremote.agent.protocol.simulator.element;
+import org.openremote.model.attribute.AttributeRef;
import org.openremote.model.attribute.AttributeType;
+import org.openremote.model.simulator.SimulatorElement;
public class SwitchSimulatorElement extends SimulatorElement {
public static final String ELEMENT_NAME = "switch";
- public SwitchSimulatorElement() {
- super(AttributeType.BOOLEAN);
+ public SwitchSimulatorElement(AttributeRef attributeRef) {
+ super(attributeRef, AttributeType.BOOLEAN);
}
}
diff --git a/manager/server/src/main/java/org/openremote/manager/server/simulator/SimulatorResourceImpl.java b/manager/server/src/main/java/org/openremote/manager/server/simulator/SimulatorResourceImpl.java
new file mode 100644
index 000000000..da56f4f91
--- /dev/null
+++ b/manager/server/src/main/java/org/openremote/manager/server/simulator/SimulatorResourceImpl.java
@@ -0,0 +1,61 @@
+/*
+ * 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.manager.server.simulator;
+
+import org.openremote.agent.protocol.simulator.SimulatorProtocol;
+import org.openremote.manager.server.asset.AssetStorageService;
+import org.openremote.manager.server.security.ManagerIdentityService;
+import org.openremote.manager.server.web.ManagerWebResource;
+import org.openremote.manager.shared.http.RequestParams;
+import org.openremote.manager.shared.simulator.SimulatorResource;
+import org.openremote.model.simulator.SimulatorState;
+import org.openremote.model.attribute.AttributeRef;
+
+public class SimulatorResourceImpl extends ManagerWebResource implements SimulatorResource {
+
+ final protected SimulatorProtocol simulatorProtocol;
+ final protected AssetStorageService assetStorageService;
+
+ public SimulatorResourceImpl(ManagerIdentityService identityService,
+ SimulatorProtocol simulatorProtocol,
+ AssetStorageService assetStorageService) {
+ super(identityService);
+ this.simulatorProtocol = simulatorProtocol;
+ this.assetStorageService = assetStorageService;
+ }
+
+ @Override
+ public SimulatorState getSimulatorState(RequestParams requestParams, String agentId, String protocolConfiguration) {
+ SimulatorState simulatorState =
+ simulatorProtocol.getSimulatorState(new AttributeRef(agentId, protocolConfiguration)).orElse(null);
+
+ simulatorState.updateAttributeDetails(
+ assetStorageService.findNames()
+ );
+
+
+ return simulatorState;
+ }
+
+ @Override
+ public void updateSimulatorState(RequestParams requestParams, AttributeRef protocolConfigurationRef, SimulatorState simulatorState) {
+
+ }
+}
diff --git a/manager/server/src/main/java/org/openremote/manager/server/web/ManagerWebService.java b/manager/server/src/main/java/org/openremote/manager/server/web/ManagerWebService.java
index ee45ba31b..b74ccc0d6 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/web/ManagerWebService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/web/ManagerWebService.java
@@ -21,9 +21,12 @@ package org.openremote.manager.server.web;
import io.undertow.server.HttpHandler;
import io.undertow.servlet.api.DeploymentInfo;
+import org.openremote.agent.protocol.simulator.SimulatorProtocol;
import org.openremote.container.Container;
import org.openremote.container.security.IdentityService;
import org.openremote.container.web.WebService;
+import org.openremote.manager.server.security.ManagerIdentityService;
+import org.openremote.manager.server.simulator.SimulatorResourceImpl;
import org.openremote.model.Constants;
import javax.ws.rs.core.UriBuilder;
@@ -83,6 +86,16 @@ public class ManagerWebService extends WebService {
CONSOLE_PATH, ManagerFileServlet.wrapHandler(consoleHandler, CONSOLE_PATTERN)
);
+ if (container.hasService(SimulatorProtocol.class)) {
+ LOG.info("Enabling simulator web resource");
+ container.getService(WebService.class).getApiSingletons().add(
+ new SimulatorResourceImpl(
+ container.getService(ManagerIdentityService.class),
+ container.getService(SimulatorProtocol.class)
+ )
+ );
+ }
+
super.init(container);
}
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/simulator/SimulatorResource.java b/manager/shared/src/main/java/org/openremote/manager/shared/simulator/SimulatorResource.java
new file mode 100644
index 000000000..cd53c92d0
--- /dev/null
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/simulator/SimulatorResource.java
@@ -0,0 +1,52 @@
+/*
+ * 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.manager.shared.simulator;
+
+import jsinterop.annotations.JsType;
+import org.openremote.manager.shared.http.RequestParams;
+import org.openremote.manager.shared.http.SuccessStatusCode;
+import org.openremote.model.attribute.AttributeRef;
+import org.openremote.model.simulator.SimulatorState;
+
+import javax.annotation.security.RolesAllowed;
+import javax.ws.rs.*;
+
+import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
+
+@Path("simulator")
+@JsType(isNative = true)
+public interface SimulatorResource {
+
+ @Path("{agentId}/{protocolConfiguration}")
+ @GET
+ @Produces(APPLICATION_JSON)
+ @SuccessStatusCode(200)
+ //@RolesAllowed({"read:admin", "write:admin"})
+ SimulatorState getSimulatorState(@BeanParam RequestParams requestParams,
+ @PathParam("agentId") String agentId,
+ @PathParam("protocolConfiguration") String protocolConfiguration);
+
+ @PUT
+ @Produces(APPLICATION_JSON)
+ @SuccessStatusCode(200)
+ @RolesAllowed({"read:admin", "write:admin"})
+ void updateSimulatorState(@BeanParam RequestParams requestParams, AttributeRef protocolConfigurationRef, SimulatorState simulatorState);
+
+}
diff --git a/manager/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy b/manager/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy
index 3ab011e6f..c77372e4c 100644
--- a/manager/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy
+++ b/manager/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy
@@ -28,17 +28,18 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
then: "the simulator elements should have the initial state"
conditions.eventually {
- assert Values.getNumber(simulatorProtocol.getState(managerDemoSetup.thingId, "light1PowerConsumption")).orElse(null) == 12.345d
+ def state = simulatorProtocol.getValue(managerDemoSetup.thingId, "light1PowerConsumption")
+ assert Values.getNumber(state.orElse(null)).orElse(null) == 12.345d
}
when: "a simulated sensor changes its value several times"
conditions = new PollingConditions(timeout: 10, initialDelay: 1)
sleep(10)
- simulatorProtocol.putState(managerDemoSetup.thingId, "light1PowerConsumption", Values.create(13.3))
+ simulatorProtocol.putValue(managerDemoSetup.thingId, "light1PowerConsumption", Values.create(13.3))
sleep(10)
- simulatorProtocol.putState(managerDemoSetup.thingId, "light1PowerConsumption", Values.create(14.4))
+ simulatorProtocol.putValue(managerDemoSetup.thingId, "light1PowerConsumption", Values.create(14.4))
sleep(10)
- simulatorProtocol.putState(managerDemoSetup.thingId, "light1PowerConsumption", Values.create(15.5))
+ simulatorProtocol.putValue(managerDemoSetup.thingId, "light1PowerConsumption", Values.create(15.5))
then: "the thing attribute value should be updated and the datapoints stored"
conditions.eventually {
diff --git a/manager/test/src/test/groovy/org/openremote/test/rules/apartment/ApartmentPresenceDetectionTest.groovy b/manager/test/src/test/groovy/org/openremote/test/rules/apartment/ApartmentPresenceDetectionTest.groovy
index a2646bf15..daeb1f534 100644
--- a/manager/test/src/test/groovy/org/openremote/test/rules/apartment/ApartmentPresenceDetectionTest.groovy
+++ b/manager/test/src/test/groovy/org/openremote/test/rules/apartment/ApartmentPresenceDetectionTest.groovy
@@ -72,7 +72,7 @@ class ApartmentPresenceDetectionTest extends Specification implements ManagerCon
managerDemoSetup.apartment1LivingroomId, "motionCount", Values.create(i), getClockTimeOf(apartment1Engine)
)
// Use the simulator to inject the update
- simulatorProtocol.putState(motionCountIncrement)
+ simulatorProtocol.putValue(motionCountIncrement)
// Wait until rules engine has the asset event
new PollingConditions(timeout: 3).eventually {
assert insertedAssetEvents.any() { it.matches(motionCountIncrement, SENSOR, true) }
@@ -106,7 +106,7 @@ class ApartmentPresenceDetectionTest extends Specification implements ManagerCon
def motionCountIncrement = new AttributeEvent(
managerDemoSetup.apartment1LivingroomId, "motionCount", Values.create(100+i), getClockTimeOf(apartment1Engine)
)
- simulatorProtocol.putState(motionCountIncrement)
+ simulatorProtocol.putValue(motionCountIncrement)
new PollingConditions(timeout: 3).eventually {
assert insertedAssetEvents.any() {
expectedLastMotionTimestamp = it.valueTimestamp
@@ -198,7 +198,7 @@ class ApartmentPresenceDetectionTest extends Specification implements ManagerCon
def motionCountIncrement = new AttributeEvent(
managerDemoSetup.apartment1LivingroomId, "motionCount", Values.create(100+i), getClockTimeOf(apartment1Engine)
)
- simulatorProtocol.putState(motionCountIncrement)
+ simulatorProtocol.putValue(motionCountIncrement)
new PollingConditions(timeout: 3).eventually {
assert insertedAssetEvents.any() {
expectedLastMotionTimestamp = it.valueTimestamp
@@ -216,7 +216,7 @@ class ApartmentPresenceDetectionTest extends Specification implements ManagerCon
def co2LevelIncrement = new AttributeEvent(
managerDemoSetup.apartment1LivingroomId, "co2Level", Values.create(400 + i), getClockTimeOf(apartment1Engine)
)
- simulatorProtocol.putState(co2LevelIncrement)
+ simulatorProtocol.putValue(co2LevelIncrement)
new PollingConditions(timeout: 3).eventually {
assert insertedAssetEvents.any() {
it.matches(co2LevelIncrement, SENSOR, true)
@@ -243,7 +243,7 @@ class ApartmentPresenceDetectionTest extends Specification implements ManagerCon
def co2LevelIncrement = new AttributeEvent(
managerDemoSetup.apartment1LivingroomId, "co2Level", Values.create(500 + i), getClockTimeOf(apartment1Engine)
)
- simulatorProtocol.putState(co2LevelIncrement)
+ simulatorProtocol.putValue(co2LevelIncrement)
new PollingConditions(timeout: 3).eventually {
assert insertedAssetEvents.any() {
it.matches(co2LevelIncrement, SENSOR, true)
diff --git a/model/src/main/java/org/openremote/model/simulator/SimulatorElement.java b/model/src/main/java/org/openremote/model/simulator/SimulatorElement.java
new file mode 100644
index 000000000..0c43a7aab
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/simulator/SimulatorElement.java
@@ -0,0 +1,78 @@
+/*
+ * 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.model.simulator;
+
+import org.openremote.model.ValidationFailure;
+import org.openremote.model.attribute.AttributeRef;
+import org.openremote.model.attribute.AttributeType;
+import org.openremote.model.value.Value;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public abstract class SimulatorElement {
+
+ final protected AttributeRef attributeRef;
+ final protected AttributeType expectedType;
+ protected Value value = null;
+
+ public SimulatorElement(AttributeRef attributeRef, AttributeType expectedType) {
+ this.attributeRef = attributeRef;
+ this.expectedType = expectedType;
+ }
+
+ public AttributeRef getAttributeRef() {
+ return attributeRef;
+ }
+
+ public AttributeType getExpectedType() {
+ return expectedType;
+ }
+
+ public Value getValue() {
+ return value;
+ }
+
+ public List<ValidationFailure> setValue(Value value) {
+ List<ValidationFailure> failures = new ArrayList<>();
+ if (value != null) {
+ failures.addAll(getValidationFailures(value));
+ if (failures.isEmpty()) {
+ this.value = value;
+ }
+ } else {
+ this.value = null;
+ }
+ return failures;
+ }
+
+ protected List<ValidationFailure> getValidationFailures(Value value) {
+ List<ValidationFailure> failures = new ArrayList<>();
+ expectedType.isValidValue(value).ifPresent(failures::add);
+ return failures;
+ }
+
+ @Override
+ public String toString() {
+ return getClass().getSimpleName() + "{" +
+ "value=" + (getValue() != null ? getValue().toJson() : "null") +
+ "}";
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/simulator/SimulatorState.java b/model/src/main/java/org/openremote/model/simulator/SimulatorState.java
new file mode 100644
index 000000000..5786ea542
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/simulator/SimulatorState.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.model.simulator;
+
+import org.openremote.model.attribute.AttributeRef;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class SimulatorState {
+
+ public class SimulatedAssetAttribute {
+
+ final protected String assetName;
+ final protected String attributeName;
+
+ public SimulatedAssetAttribute(String assetName, String attributeName) {
+ this.assetName = assetName;
+ this.attributeName = attributeName;
+ }
+
+ public String getAssetName() {
+ return assetName;
+ }
+
+ public String getAttributeName() {
+ return attributeName;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ SimulatedAssetAttribute that = (SimulatedAssetAttribute) o;
+
+ if (!assetName.equals(that.assetName)) return false;
+ return attributeName.equals(that.attributeName);
+ }
+
+ @Override
+ public int hashCode() {
+ int result = assetName.hashCode();
+ result = 31 * result + attributeName.hashCode();
+ return result;
+ }
+ }
+
+ protected Map<AttributeRef, SimulatedAssetAttribute> attributeDetails = new HashMap<>();
+ protected SimulatorElement[] elements = new SimulatorElement[0];
+
+ protected SimulatorState() {
+ }
+
+ public SimulatorState(SimulatorElement... elements) {
+ this.elements = elements;
+ }
+
+ public SimulatorElement[] getElements() {
+ return elements;
+ }
+
+ public void setAttributeDetails(Map<AttributeRef, SimulatedAssetAttribute> attributeDetails) {
+ this.attributeDetails = attributeDetails;
+ }
+
+ public void clearAttributeDetails() {
+ this.attributeDetails.clear();
+ }
+}