Search in sources :

Example 1 with ControllerCommandBasic

use of org.openremote.agent.protocol.controller.command.ControllerCommandBasic in project openremote by openremote.

the class ControllerProtocol method doLinkAttribute.

@Override
protected void doLinkAttribute(String assetId, Attribute<?> attribute, ControllerAgentLink agentLink) {
    AttributeRef attributeRef = new AttributeRef(assetId, attribute.getName());
    String deviceName = agentLink.getDeviceName().orElse(null);
    String sensorName = agentLink.getSensorName().orElse(null);
    String commandDeviceName = agentLink.getCommandDeviceName().orElse(null);
    String commandName = agentLink.getCommandName().orElse(null);
    Map<String, List<String>> commandsMap = agentLink.getCommandsMap().orElse(null);
    /*
         * Build Sensor Status info for polling request
         */
    if (sensorName != null) {
        LOG.finer("### Adding new sensor [" + deviceName + "," + sensorName + "] linked to " + agent.getId() + " (" + agent.getName() + ")");
        controller.addSensor(attributeRef, new ControllerSensor(deviceName, sensorName));
        // Properly stop previously existing polling on device name --> use of false parameter
        if (pollingSensorList.containsKey(deviceName)) {
            pollingSensorList.get(deviceName).cancel(true);
        }
        this.initStatusDone.put(attributeRef, false);
        // Get initial status of sensor
        collectInitialStatus(attributeRef, deviceName, sensorName);
        // Put new polling on a new device name or update previous
        this.schedulePollingTask(deviceName);
    }
    /*
         * If linked Attribute contains command info, we build {@link org.openremote.agent.protocol.controller.ControllerCommand } depending on
         * attribute information.
         */
    if (commandName != null || commandsMap != null) {
        // If no command specific device name is set, then we're using deviceName
        if (commandDeviceName == null && deviceName != null) {
            commandDeviceName = deviceName;
        }
        if (commandName != null) {
            controller.addCommand(attributeRef, new ControllerCommandBasic(commandDeviceName, commandName));
        } else {
            assert commandsMap.size() > 0;
            controller.addCommand(attributeRef, new ControllerCommandMapped(commandDeviceName, computeCommandsMapFromMultiValue(commandsMap)));
        }
    }
}
Also used : AttributeRef(org.openremote.model.attribute.AttributeRef) ControllerCommandMapped(org.openremote.agent.protocol.controller.command.ControllerCommandMapped) ControllerCommandBasic(org.openremote.agent.protocol.controller.command.ControllerCommandBasic)

Example 2 with ControllerCommandBasic

use of org.openremote.agent.protocol.controller.command.ControllerCommandBasic in project openremote by openremote.

the class ControllerProtocol method doLinkedAttributeWrite.

/**
 * Write action on a linked attribute mean we execute a command on the Controller. It induce a HTTP request and
 * manage it's return code. (No value is returned from the execution of a command)
 */
@Override
protected void doLinkedAttributeWrite(Attribute<?> attribute, ControllerAgentLink agentLink, AttributeEvent event, Object processedValue) {
    LOG.finer("### Process Linked Attribute Write");
    AttributeRef attributeRef = event.getAttributeRef();
    ControllerCommand controllerCommand = controller.getCommand(attributeRef);
    HTTPProtocol.HttpClientRequest request = RequestBuilder.buildCommandRequest(controllerCommand, event, controllerWebTarget);
    String body = null;
    if (controllerCommand instanceof ControllerCommandBasic) {
        body = event.getValue().map(v -> {
            ObjectNode objectValue = ValueUtil.JSON.createObjectNode();
            objectValue.putPOJO("parameter", processedValue);
            return objectValue.toString();
        }).orElse(null);
    }
    executeAttributeWriteRequest(request, body, this::onAttributeWriteResponse);
}
Also used : HTTPProtocol(org.openremote.agent.protocol.http.HTTPProtocol) AttributeRef(org.openremote.model.attribute.AttributeRef) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ControllerCommandBasic(org.openremote.agent.protocol.controller.command.ControllerCommandBasic)

Aggregations

ControllerCommandBasic (org.openremote.agent.protocol.controller.command.ControllerCommandBasic)2 AttributeRef (org.openremote.model.attribute.AttributeRef)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ControllerCommandMapped (org.openremote.agent.protocol.controller.command.ControllerCommandMapped)1 HTTPProtocol (org.openremote.agent.protocol.http.HTTPProtocol)1