Skip to content

armgit / core

Public Imported from GitHub
0

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

Cleanup of macro protocol and executable attributes

Rich Turnerb29a220832ae
diff --git a/model/src/main/java/org/openremote/model/asset/macro/MacroAction.java b/agent/src/main/java/org/openremote/agent3/protocol/macro/MacroAction.java
similarity index 70%
rename from model/src/main/java/org/openremote/model/asset/macro/MacroAction.java
rename to agent/src/main/java/org/openremote/agent3/protocol/macro/MacroAction.java
index 70054cda1..e95186205 100644
--- a/model/src/main/java/org/openremote/model/asset/macro/MacroAction.java
+++ b/agent/src/main/java/org/openremote/agent3/protocol/macro/MacroAction.java
@@ -17,42 +17,44 @@
* 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.asset.macro;
+package org.openremote.agent3.protocol.macro;
import elemental.json.Json;
import elemental.json.JsonObject;
import elemental.json.JsonValue;
import org.openremote.model.AttributeState;
import org.openremote.model.MetaItem;
-import org.openremote.model.asset.AssetMeta;
import java.util.function.Function;
+import static org.openremote.model.Constants.ASSET_META_NAMESPACE;
+
/**
- * A desired {@link AttributeState} and a delay in seconds (TODO?) before that state
+ * A desired {@link AttributeState} and a delay in milli seconds before that state
* is applied when the macro executes.
*/
public class MacroAction {
+ public static final String MACRO_ACTION = ASSET_META_NAMESPACE + ":macroAction";
protected AttributeState attributeState;
- protected int delay;
+ protected int delayMilliseconds;
static Function<MetaItem, MacroAction> getMacroActionFromMetaItem() {
return metaItem -> new MacroAction(metaItem.getValueAsObject());
}
static Function<MacroAction, MetaItem> getMetaItemFromMacroAction() {
- return macroAction -> new MetaItem(AssetMeta.MACRO_ACTION, macroAction.asJsonValue());
+ return macroAction -> new MetaItem(MACRO_ACTION, macroAction.asJsonValue());
}
public MacroAction(JsonObject jsonObject) {
attributeState = new AttributeState(jsonObject.getObject("attributeState"));
- delay = new Double(jsonObject.getNumber("delay")).intValue();
+ delayMilliseconds = new Double(jsonObject.getNumber("delay")).intValue();
}
- public MacroAction(AttributeState attributeState, int delay) {
+ public MacroAction(AttributeState attributeState, int delayMilliseconds) {
this.attributeState = attributeState;
- this.delay = delay;
+ this.delayMilliseconds = delayMilliseconds;
}
public AttributeState getAttributeState() {
@@ -63,18 +65,18 @@ public class MacroAction {
this.attributeState = attributeState;
}
- public int getDelay() {
- return delay;
+ public int getDelayMilliseconds() {
+ return delayMilliseconds;
}
- public void setDelay(int delay) {
- this.delay = delay;
+ public void setDelayMilliseconds(int delayMilliseconds) {
+ this.delayMilliseconds = delayMilliseconds;
}
public JsonValue asJsonValue() {
JsonObject jsonObect = Json.createObject();
jsonObect.put("attributeState", attributeState != null ? attributeState.asJsonValue() : Json.create(null));
- jsonObect.put("delay", Json.create(delay));
+ jsonObect.put("delay", Json.create(delayMilliseconds));
return jsonObect;
}
@@ -82,7 +84,7 @@ public class MacroAction {
public String toString() {
return getClass().getSimpleName() + "{" +
"attributeState='" + attributeState + '\'' +
- ", delay='" + delay + '\'' +
+ ", delay='" + delayMilliseconds + '\'' +
'}';
}
}
diff --git a/model/src/main/java/org/openremote/model/asset/macro/MacroConfiguration.java b/agent/src/main/java/org/openremote/agent3/protocol/macro/MacroConfiguration.java
similarity index 75%
rename from model/src/main/java/org/openremote/model/asset/macro/MacroConfiguration.java
rename to agent/src/main/java/org/openremote/agent3/protocol/macro/MacroConfiguration.java
index 3dacca3d8..bb72c690d 100644
--- a/model/src/main/java/org/openremote/model/asset/macro/MacroConfiguration.java
+++ b/agent/src/main/java/org/openremote/agent3/protocol/macro/MacroConfiguration.java
@@ -17,14 +17,13 @@
* 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.asset.macro;
+package org.openremote.agent3.protocol.macro;
import elemental.json.Json;
import org.openremote.model.Attribute;
import org.openremote.model.AttributeType;
import org.openremote.model.MetaItem;
import org.openremote.model.asset.AssetAttribute;
-import org.openremote.model.asset.AssetMeta;
import org.openremote.model.util.JsonUtil;
import java.util.List;
@@ -36,30 +35,24 @@ import java.util.stream.Stream;
import static org.openremote.model.Attribute.isAttributeType;
import static org.openremote.model.Attribute.isAttributeValid;
import static org.openremote.model.AttributeType.STRING;
-import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
import static org.openremote.model.asset.AssetAttribute.hasAssetMetaItem;
import static org.openremote.model.asset.AssetAttribute.isAssetMetaItem;
-import static org.openremote.model.asset.AssetMeta.MACRO_ACTION;
-import static org.openremote.model.asset.macro.MacroAction.getMacroActionFromMetaItem;
-import static org.openremote.model.asset.macro.MacroAction.getMetaItemFromMacroAction;
/**
* Agent attributes can be macro configurations.
* <p>
- * A macro configuration attribute has {@link AssetMeta#MACRO_ACTION} items attached to it, each
+ * A macro configuration attribute has {@link MacroAction#MACRO_ACTION} items attached to it, each
* item is a sequence of asset state changes to be executed.
*/
final public class MacroConfiguration {
- public static final String MACRO_PROTOCOL_NAME = PROTOCOL_NAMESPACE + ":macro";
-
private MacroConfiguration() {
}
public static Function<AssetAttribute, AssetAttribute> initMacroConfiguration() {
return attribute -> {
attribute.setType(AttributeType.STRING);
- attribute.setValue(Json.create(MACRO_PROTOCOL_NAME));
+ attribute.setValue(Json.create(MacroProtocol.MACRO_PROTOCOL_NAME));
return attribute;
};
}
@@ -67,7 +60,7 @@ final public class MacroConfiguration {
public static Predicate<Attribute> isMacroConfiguration() {
return attribute -> {
String value = attribute.getValueAsString();
- return value != null && value.equals(MACRO_PROTOCOL_NAME);
+ return value != null && value.equals(MacroProtocol.MACRO_PROTOCOL_NAME);
};
}
@@ -75,18 +68,18 @@ final public class MacroConfiguration {
return isAttributeValid()
.and(isAttributeType(STRING))
.and(isMacroConfiguration())
- .and(hasAssetMetaItem(MACRO_ACTION)); // Must have at least one macro action
+ .and(hasAssetMetaItem(MacroAction.MACRO_ACTION)); // Must have at least one macro action
}
public static Function<AssetAttribute, Stream<MacroAction>> getMacroActions() {
return attribute -> attribute.getMetaItemStream()
- .filter(isAssetMetaItem(MACRO_ACTION))
- .map(getMacroActionFromMetaItem());
+ .filter(isAssetMetaItem(MacroAction.MACRO_ACTION))
+ .map(MacroAction.getMacroActionFromMetaItem());
}
public static Function<AssetAttribute, AssetAttribute> addMacroAction(MacroAction action) {
return attribute -> attribute.setMeta(
- attribute.getMeta().add(getMetaItemFromMacroAction().apply(action))
+ attribute.getMeta().add(MacroAction.getMetaItemFromMacroAction().apply(action))
);
}
@@ -97,7 +90,7 @@ final public class MacroConfiguration {
}
List<MetaItem> metaItems = attribute.getMeta().all();
IntStream.range(0, metaItems.size())
- .filter(i -> isAssetMetaItem(MACRO_ACTION).test(metaItems.get(i)))
+ .filter(i -> isAssetMetaItem(MacroAction.MACRO_ACTION).test(metaItems.get(i)))
.filter(i -> JsonUtil.equals(metaItems.get(i).getValueAsObject(), action.asJsonValue()))
.forEach(i -> attribute.getMeta().remove(i));
diff --git a/agent/src/main/java/org/openremote/agent3/protocol/macro/MacroProtocol.java b/agent/src/main/java/org/openremote/agent3/protocol/macro/MacroProtocol.java
index 56a3730f9..8153d5ddb 100644
--- a/agent/src/main/java/org/openremote/agent3/protocol/macro/MacroProtocol.java
+++ b/agent/src/main/java/org/openremote/agent3/protocol/macro/MacroProtocol.java
@@ -22,13 +22,12 @@ package org.openremote.agent3.protocol.macro;
import elemental.json.JsonValue;
import org.openremote.agent3.protocol.AbstractProtocol;
import org.openremote.container.Container;
-import org.openremote.model.*;
+import org.openremote.model.AttributeEvent;
+import org.openremote.model.AttributeExecuteStatus;
+import org.openremote.model.AttributeRef;
+import org.openremote.model.AttributeState;
import org.openremote.model.asset.AssetAttribute;
-import org.openremote.model.asset.macro.MacroAction;
-import org.openremote.model.asset.macro.MacroAttributeCommand;
-import org.openremote.model.asset.macro.MacroConfiguration;
-import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -36,43 +35,44 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
-import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
-import static org.openremote.model.asset.macro.MacroConfiguration.getMacroActions;
+import static org.openremote.agent3.protocol.macro.MacroConfiguration.getMacroActions;
+import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
/**
* This protocol is responsible for executing macros.
* <p>
- * It expects a {@link MacroAttributeCommand} or the super type{@link AttributeCommand}
- * as the attribute event value on the sendToActuator. The protocol will then execute
- * the command and store the command status in the attribute value.
+ * It expects a {@link AttributeExecuteStatus} as the attribute event value on the
+ * sendToActuator. The protocol will then try to perform the requested status on the
+ * linked macro.
*/
public class MacroProtocol extends AbstractProtocol {
+
+ public static final String MACRO_PROTOCOL_NAME = PROTOCOL_NAMESPACE + ":macro";
+
class MacroExecutionTask {
AttributeRef attributeRef;
List<MacroAction> actions;
- List<MacroAttributeCommand.Execution> schedule;
boolean repeat;
boolean cancelled;
ScheduledFuture scheduledFuture;
int iteration = -1;
- public MacroExecutionTask(AttributeRef attributeRef, List<MacroAction> actions, List<MacroAttributeCommand.Execution> schedule, boolean repeat) {
+ public MacroExecutionTask(AttributeRef attributeRef, List<MacroAction> actions, boolean repeat) {
this.attributeRef = attributeRef;
this.actions = actions;
- this.schedule = schedule;
this.repeat = repeat;
}
void start() {
- synchronized (scheduleMap) {
- scheduleMap.put(attributeRef, this);
+ synchronized (executions) {
+ executions.put(attributeRef, this);
}
// Update the command Status of this attribute
- onSensorUpdate(new AttributeState(attributeRef, CommandStatus.ACTIVE.asJsonValue()));
+ onSensorUpdate(new AttributeState(attributeRef, AttributeExecuteStatus.RUNNING.asJsonValue()));
run();
}
@@ -80,12 +80,12 @@ public class MacroProtocol extends AbstractProtocol {
LOG.fine("Macro Execution cancel");
scheduledFuture.cancel(false);
cancelled = true;
- synchronized (scheduleMap) {
- scheduleMap.remove(attributeRef);
+ synchronized (executions) {
+ executions.remove(attributeRef);
}
// Update the command Status of this attribute
- onSensorUpdate(new AttributeState(attributeRef, CommandStatus.CANCELLED.asJsonValue()));
+ onSensorUpdate(new AttributeState(attributeRef, AttributeExecuteStatus.CANCELLED.asJsonValue()));
}
private void run() {
@@ -94,33 +94,15 @@ public class MacroProtocol extends AbstractProtocol {
}
if (iteration >= 0) {
- // Process the execution
- MacroAttributeCommand.Execution execution = schedule.get(iteration);
- actions.forEach(action -> {
- // Look for override value in execution
- AttributeState overrideState = execution.getActionOverrides() == null ? null : execution.getActionOverrides()
- .stream()
- .filter(actionOverride -> actionOverride.getAttributeRef().equals(action.getAttributeState().getAttributeRef()))
- .findFirst()
- .orElse(null);
-
- AttributeState actionState = overrideState != null ? overrideState : action.getAttributeState();
- if (action.getDelay() > 0) {
- // TODO: Better handling of macro action delays - this is ok for short delays
- try {
- Thread.sleep(action.getDelay());
- } catch (InterruptedException e) {
- LOG.log(Level.WARNING, "Macro action was cancelled assume the thread is being terminated", e);
- return;
- }
- }
+ // Process the execution of the next action
+ MacroAction action = actions.get(iteration);
+ AttributeState actionState = action.getAttributeState();
- // send attribute event
- sendAttributeEvent(actionState);
- });
+ // send attribute event
+ sendAttributeEvent(actionState);
}
- boolean isLast = iteration == schedule.size() - 1;
+ boolean isLast = iteration == actions.size() - 1;
boolean restart = isLast && repeat;
if (restart) {
@@ -130,27 +112,27 @@ public class MacroProtocol extends AbstractProtocol {
}
if ((isLast && !restart)) {
- synchronized (scheduleMap) {
- scheduleMap.remove(attributeRef);
+ synchronized (executions) {
+ executions.remove(attributeRef);
}
// Update the command Status of this attribute
- onSensorUpdate(new AttributeState(attributeRef, CommandStatus.COMPLETED.asJsonValue()));
+ onSensorUpdate(new AttributeState(attributeRef, AttributeExecuteStatus.COMPLETED.asJsonValue()));
return;
}
// Get next execution delay
- Integer delay = schedule.get(iteration).getDelay();
+ Integer delay = actions.get(iteration).getDelayMilliseconds();
// Schedule the next iteration
- scheduledFuture = executor.schedule(this::run, delay != null && delay >= 0 ? delay : 0, TimeUnit.MILLISECONDS);
+ scheduledFuture = executor.schedule(this::run, delay > 0 ? delay : 0, TimeUnit.MILLISECONDS);
}
}
private static final Logger LOG = Logger.getLogger(MacroProtocol.class.getName());
protected final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
protected final Map<AttributeRef, List<MacroAction>> actionMap = new HashMap<>();
- protected final Map<AttributeRef, MacroExecutionTask> scheduleMap = new HashMap<>();
+ protected final Map<AttributeRef, MacroExecutionTask> executions = new HashMap<>();
@Override
public void start(Container container) throws Exception {
@@ -159,26 +141,26 @@ public class MacroProtocol extends AbstractProtocol {
@Override
public String getProtocolName() {
- return MacroConfiguration.MACRO_PROTOCOL_NAME;
+ return MACRO_PROTOCOL_NAME;
}
@Override
protected void sendToActuator(AttributeEvent event) {
- AttributeRef attributeRef = event.getAttributeRef();
-
- // Check that incoming value is of type Attribute Command
+ // Asset processing service has already done sanity checks
JsonValue value = event.getValue();
- MacroAttributeCommand macroCommand = MacroAttributeCommand.fromJsonValue(value);
+ AttributeExecuteStatus status = AttributeExecuteStatus.fromString(value.asString());
+ AttributeRef attributeRef = event.getAttributeRef();
// Check if it's a cancellation request
- if (macroCommand.isCancel()) {
+ if (status == AttributeExecuteStatus.REQUEST_CANCEL) {
LOG.fine("Request received to cancel macro execution: " + event);
- synchronized (scheduleMap) {
- MacroExecutionTask executionTask = scheduleMap.get(attributeRef);
- if (executionTask != null) {
- LOG.fine("Cancelling macro execution");
- executionTask.cancel();
- }
+ synchronized (executions) {
+ executions.computeIfPresent(attributeRef,
+ (attributeRef1, macroExecutionTask) -> {
+ macroExecutionTask.cancel();
+ return macroExecutionTask;
+ }
+ );
}
return;
@@ -195,16 +177,7 @@ public class MacroProtocol extends AbstractProtocol {
return;
}
- // If the executing command has provided schedule information then include
- // this in the execution of the macro actions
- List<MacroAttributeCommand.Execution> schedule = macroCommand.getSchedule();
-
- if (schedule == null) {
- // Push in an empty execution
- schedule = Collections.singletonList(new MacroAttributeCommand.Execution(0));
- }
-
- executeMacro(attributeRef, actions, schedule, macroCommand.isRepeat());
+ executeMacro(attributeRef, actions, status == AttributeExecuteStatus.REQUEST_REPEATING);
}
@Override
@@ -235,8 +208,8 @@ public class MacroProtocol extends AbstractProtocol {
}
}
- protected void executeMacro(AttributeRef attributeRef, List<MacroAction> actions, List<MacroAttributeCommand.Execution> schedule, boolean repeat) {
- MacroExecutionTask task = new MacroExecutionTask(attributeRef, actions, schedule, repeat);
+ protected void executeMacro(AttributeRef attributeRef, List<MacroAction> actions, boolean repeat) {
+ MacroExecutionTask task = new MacroExecutionTask(attributeRef, actions, repeat);
task.start();
}
}
diff --git a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetProcessingService.java b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetProcessingService.java
index 9a36e1181..89ff87712 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/asset/AssetProcessingService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/asset/AssetProcessingService.java
@@ -19,6 +19,8 @@
*/
package org.openremote.manager.server.asset;
+import elemental.json.JsonString;
+import elemental.json.JsonValue;
import org.apache.camel.builder.RouteBuilder;
import org.openremote.container.Container;
import org.openremote.container.ContainerService;
@@ -32,6 +34,7 @@ import org.openremote.manager.server.rules.RulesService;
import org.openremote.manager.server.security.ManagerIdentityService;
import org.openremote.manager.shared.security.ClientRole;
import org.openremote.model.AttributeEvent;
+import org.openremote.model.AttributeExecuteStatus;
import org.openremote.model.asset.*;
import java.util.ArrayList;
@@ -43,9 +46,7 @@ import java.util.logging.Logger;
import static org.openremote.agent3.protocol.Protocol.SENSOR_QUEUE;
import static org.openremote.manager.server.event.EventService.INCOMING_EVENT_TOPIC;
import static org.openremote.manager.server.event.EventService.getWebsocketAuth;
-import static org.openremote.model.asset.AssetAttribute.containsAssetAttributeNamed;
-import static org.openremote.model.asset.AssetAttribute.findAssetAttribute;
-import static org.openremote.model.asset.AssetAttribute.isMatchingAttributeEvent;
+import static org.openremote.model.asset.AssetAttribute.*;
import static org.openremote.model.asset.agent.AgentLink.getAgentLink;
import static org.openremote.model.asset.agent.AgentLink.isValidAgentLink;
@@ -214,7 +215,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
if (isSensorUpdate) {
processSensorUpdate(event);
} else {
- updateAttributeValue(event);
+ updateAttributeValue(event, true);
}
});
@@ -224,7 +225,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
.process(exchange -> {
AttributeEvent event = exchange.getIn().getBody(AttributeEvent.class);
LOG.fine("Received on asset queue: " + event);
- updateAttributeValue(event);
+ updateAttributeValue(event, false);
});
// React if a client wants to write attribute state
@@ -297,8 +298,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
* entry point to initiate an attribute value change: Sensor updates from protocols, attribute
* write requests from clients, and attribute write dispatching as rules RHS action.
*/
- private void updateAttributeValue(AttributeEvent attributeEvent) {
-
+ private void updateAttributeValue(AttributeEvent attributeEvent, boolean fromProtocol) {
// Check this event relates to a valid asset
ServerAsset asset = assetStorageService.find(attributeEvent.getEntityId(), true);
@@ -330,6 +330,22 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
return;
}
+ // If attribute is marked as executable and not from northbound only allow write AttributeExecuteStatus to be sent
+ if (!fromProtocol && attribute.isExecutable()) {
+ JsonValue value = attributeEvent.getValue();
+ AttributeExecuteStatus status;
+
+ if (value == null || !(value instanceof JsonString) || (status = AttributeExecuteStatus.fromString(value.asString())) == null) {
+ LOG.warning("Attribute event doesn't contain a valid AttributeExecuteStatus value: " + attributeEvent);
+ return;
+ }
+
+ if (!status.isWrite()) {
+ LOG.warning("Only AttributeExecuteStatus write value can be written to an attribute: " + attributeEvent);
+ return;
+ }
+ }
+
processUpdate(asset, attribute, attributeEvent, false);
}
diff --git a/model/src/main/java/org/openremote/model/AttributeCommand.java b/model/src/main/java/org/openremote/model/AttributeCommand.java
deleted file mode 100644
index 4061b67ba..000000000
--- a/model/src/main/java/org/openremote/model/AttributeCommand.java
+++ /dev/null
@@ -1,89 +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;
-
-import elemental.json.Json;
-import elemental.json.JsonObject;
-import elemental.json.JsonValue;
-
-/**
- *
- */
-public class AttributeCommand {
- protected boolean cancel;
- protected boolean repeat;
- protected JsonValue value;
- public static final AttributeCommand CANCEL_ATTRIBUTE_COMMAND;
-
- static {
- AttributeCommand cancel = new AttributeCommand();
- cancel.setCancel(true);
- CANCEL_ATTRIBUTE_COMMAND = cancel;
- }
-
- public AttributeCommand() {
- }
-
- public AttributeCommand(boolean repeat) {
- this(repeat, false, null);
- }
-
- public AttributeCommand(JsonValue value) {
- this(false, false, value);
- }
-
- public AttributeCommand(boolean repeat, boolean cancel, JsonValue value) {
- this.repeat = repeat;
- this.cancel = cancel;
- this.value = value;
- }
-
- public boolean isCancel() {
- return cancel;
- }
-
- public void setCancel(boolean cancel) {
- this.cancel = cancel;
- }
-
- public boolean isRepeat() {
- return repeat;
- }
-
- public void setRepeat(boolean repeat) {
- this.repeat = repeat;
- }
-
- public JsonValue getValue() {
- return value;
- }
-
- public void setValue(JsonValue value) {
- this.value = value != null ? value : Json.createNull();
- }
-
- public JsonValue asJsonValue() {
- JsonObject jsonObject = Json.createObject();
- jsonObject.put("cancel", Json.create(cancel));
- jsonObject.put("repeat", Json.create(repeat));
- jsonObject.put("value", value);
- return jsonObject;
- }
-}
diff --git a/model/src/main/java/org/openremote/model/AttributeExecuteStatus.java b/model/src/main/java/org/openremote/model/AttributeExecuteStatus.java
new file mode 100644
index 000000000..148b6c697
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/AttributeExecuteStatus.java
@@ -0,0 +1,84 @@
+/*
+ * 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;
+
+import elemental.json.Json;
+import elemental.json.JsonValue;
+
+public enum AttributeExecuteStatus {
+ /**
+ * Request execution
+ */
+ REQUEST_START(true),
+
+ /**
+ * Request repeating execution
+ */
+ REQUEST_REPEATING(true),
+
+ /**
+ * Request that running execution be cancelled
+ */
+ REQUEST_CANCEL(true),
+
+ /**
+ * Execution completed
+ */
+ COMPLETED(false),
+
+ /**
+ * Execution is currently running
+ */
+ RUNNING(false),
+
+ /**
+ * Execution was cancelled
+ */
+ CANCELLED(false),
+
+ /**
+ * Command produced an error
+ */
+ ERROR(false);
+
+ private boolean write;
+ // Prevents cloning of values each time fromString is called
+ private static final AttributeExecuteStatus[] copyOfValues = values();
+
+ AttributeExecuteStatus(boolean write) {
+ this.write = write;
+ }
+
+ public JsonValue asJsonValue() {
+ return Json.create(this.toString());
+ }
+
+ public boolean isWrite() {
+ return write;
+ }
+
+ public static AttributeExecuteStatus fromString(String value) {
+ for (AttributeExecuteStatus status : copyOfValues) {
+ if (status.name().equalsIgnoreCase(value))
+ return status;
+ }
+ return null;
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/CommandStatus.java b/model/src/main/java/org/openremote/model/CommandStatus.java
deleted file mode 100644
index 66b3bc3c8..000000000
--- a/model/src/main/java/org/openremote/model/CommandStatus.java
+++ /dev/null
@@ -1,51 +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;
-
-import elemental.json.Json;
-import elemental.json.JsonObject;
-import elemental.json.JsonValue;
-
-public enum CommandStatus {
- /**
- * Command has completed
- */
- COMPLETED,
- /**
- * Command is currently executing
- */
- ACTIVE,
- /**
- * Command has been cancelled
- */
- CANCELLED,
-
- /**
- * Command produced an error
- */
- ERROR;
-
- public JsonValue asJsonValue() {
- // Use JsonObject so it can be pushed into executable attribute
- JsonObject jsonObject = Json.createObject();
- jsonObject.put("status", Json.create(this.toString()));
- return jsonObject;
- }
-}
diff --git a/model/src/main/java/org/openremote/model/asset/AssetMeta.java b/model/src/main/java/org/openremote/model/asset/AssetMeta.java
index 8815e4e87..26d3c1be0 100644
--- a/model/src/main/java/org/openremote/model/asset/AssetMeta.java
+++ b/model/src/main/java/org/openremote/model/asset/AssetMeta.java
@@ -138,19 +138,14 @@ public enum AssetMeta {
RULE_EVENT_EXPIRES(ASSET_META_NAMESPACE + ":ruleEventExpires", new Access(true, false, true), JsonType.STRING),
/**
- * Enable flag to be used by asset attributes that could require this functionality (e.g. ProtocolConfiguration)
+ * Enable flag to be used by asset attributes that could require this functionality (e.g. {@link org.openremote.model.asset.agent.ProtocolConfiguration})
*/
ENABLED(ASSET_META_NAMESPACE + ":enabled", new Access(true, false, true), JsonType.BOOLEAN),
/**
- * Identifies a meta item as a {@link org.openremote.model.asset.macro.MacroAction}.
+ * Marks an attribute as being executable so it then supports values of type {@link org.openremote.model.AttributeExecuteStatus}
*/
- MACRO_ACTION(ASSET_META_NAMESPACE + ":macroAction", new Access(true, false, true), JsonType.ARRAY),
-
- /**
- * Marks an attribute as being an executable command
- */
- COMMAND(ASSET_META_NAMESPACE + ":command", new Access(true, false, true), JsonType.BOOLEAN);
+ EXECUTABLE(ASSET_META_NAMESPACE + ":command", new Access(true, false, true), JsonType.BOOLEAN);
final protected String urn;
final protected Access access;
diff --git a/model/src/main/java/org/openremote/model/asset/macro/MacroAttributeCommand.java b/model/src/main/java/org/openremote/model/asset/macro/MacroAttributeCommand.java
deleted file mode 100644
index 7b731a019..000000000
--- a/model/src/main/java/org/openremote/model/asset/macro/MacroAttributeCommand.java
+++ /dev/null
@@ -1,176 +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.asset.macro;
-
-import elemental.json.*;
-import org.openremote.model.AttributeState;
-import org.openremote.model.AttributeCommand;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.stream.Collectors;
-import java.util.stream.IntStream;
-
-public class MacroAttributeCommand extends AttributeCommand {
- public static class Execution {
- protected List<AttributeState> actionOverrides;
- protected Integer delay;
-
- public Execution() {
- }
-
- public Execution(List<AttributeState> actionOverrides, Integer delay) {
- this.actionOverrides = actionOverrides;
- this.delay = delay;
- }
-
- public Execution(Integer delay) {
- this.delay = delay;
- }
-
- public Execution(List<AttributeState> actionOverrides) {
- this.actionOverrides = actionOverrides;
- }
-
- public List<AttributeState> getActionOverrides() {
- return actionOverrides;
- }
-
- public void setActionOverrides(List<AttributeState> actionOverrides) {
- this.actionOverrides = actionOverrides;
- }
-
- public Integer getDelay() {
- return delay;
- }
-
- public void setDelay(Integer delay) {
- this.delay = delay;
- }
-
- public JsonValue asJsonValue() {
- JsonObject jsonObject = Json.createObject();
-
- if (actionOverrides != null) {
- JsonArray jsonArray = Json.createArray();
-
- IntStream.range(0, actionOverrides.size())
- .forEach(i -> jsonArray.set(i, actionOverrides.get(i).asJsonValue()));
-
- jsonObject.put("actionOverrides", jsonArray);
- } else {
- jsonObject.put("actionOverrides", Json.createNull());
- }
-
- jsonObject.put("delay", delay == null ? Json.createNull() : Json.create(delay));
-
- return jsonObject;
- }
-
- public static Execution fromJsonValue(JsonValue jsonValue) {
- if (!(jsonValue instanceof JsonObject)) {
- return null;
- }
-
- JsonObject jsonObject = (JsonObject)jsonValue;
- List<AttributeState> actionOverrides = null;
- Integer delay = null;
-
- if (jsonObject.hasKey("actionOverrides")) {
- JsonArray actionOverridesArray = jsonObject.hasKey("actionOverrides") && !jsonObject.get("actionOverrides").jsEquals(Json.createNull()) ? jsonObject.getArray("actionOverrides") : null;
- if (actionOverridesArray != null && actionOverridesArray.length() > 0) {
- actionOverrides = new ArrayList<>(actionOverridesArray.length());
- List<AttributeState> finalActionOverrides = actionOverrides;
- IntStream.range(0, actionOverridesArray.length())
- .forEach(i -> finalActionOverrides.add(i, new AttributeState(actionOverridesArray.get(i))));
- }
- }
-
- if (jsonObject.hasKey("delay")) {
- JsonValue value = jsonObject.get("delay");
- if (value != null && !(value instanceof JsonNull)) {
- delay = new Double(value.asNumber()).intValue();
- }
- }
-
- return new Execution(actionOverrides, delay);
- }
- }
-
- public MacroAttributeCommand() {
- this(false, false, (List<Execution>) null);
- }
-
- public MacroAttributeCommand(Execution execution) {
- this(false, execution);
- }
-
- public MacroAttributeCommand(boolean repeat, Execution execution) {
- this(repeat, false, Collections.singletonList(execution));
- }
-
- public MacroAttributeCommand(boolean repeat, boolean cancel, List<Execution> schedule) {
- super(repeat, cancel, scheduleAsJsonValue(schedule));
- }
-
- public List<Execution> getSchedule() {
- return scheduleFromJsonValue(getValue());
- }
-
- public void setSchedule(List<Execution> schedule) {
- setValue(scheduleAsJsonValue(schedule));
- }
-
- protected static JsonValue scheduleAsJsonValue(List<Execution> schedule) {
- if (schedule == null) {
- return Json.createNull();
- }
-
- JsonArray jsonArray = Json.createArray();
- IntStream.range(0, schedule.size())
- .forEach(i -> jsonArray.set(i, schedule.get(i).asJsonValue()));
-
- return jsonArray;
- }
-
- protected static List<Execution> scheduleFromJsonValue(JsonValue jsonValue) {
- if (!(jsonValue instanceof JsonArray)) {
- return null;
- }
- JsonArray jsonArray = (JsonArray)jsonValue;
- return IntStream.range(0, jsonArray.length())
- .mapToObj(i -> Execution.fromJsonValue(jsonArray.get(i)))
- .collect(Collectors.toList());
- }
-
- public JsonValue asJsonValue() {
- return super.asJsonValue();
- }
-
- public static MacroAttributeCommand fromJsonValue(JsonValue jsonValue) {
- if (!(jsonValue instanceof JsonObject)) {
- return null;
- }
- JsonObject jsonObject = (JsonObject)jsonValue;
-
- return new MacroAttributeCommand(jsonObject.getBoolean("repeat"), jsonObject.getBoolean("cancel"), scheduleFromJsonValue(jsonObject.getArray("value")));
- }
-}