Search in sources :

Example 1 with Value

use of org.openremote.model.value.Value in project openremote by openremote.

the class KNXConnection method sendCommand.

public void sendCommand(Datapoint datapoint, Optional<Value> value) {
    try {
        if (this.connectionStatus == ConnectionStatus.CONNECTED && value.isPresent()) {
            LOG.fine("Sending to KNX action datapoint '" + datapoint + "': " + value);
            Value val = value.get();
            DPTXlator translator = TypeMapper.toDPTXlator(datapoint, val);
            processCommunicator.write(datapoint.getMainAddress(), translator);
        }
    } catch (KNXAckTimeoutException e) {
        LOG.log(Level.INFO, "Failed to send KNX value: " + datapoint + " : " + value, e);
        onConnectionError();
    } catch (Exception e) {
        LOG.severe(e.getMessage());
    }
}
Also used : DPTXlator(tuwien.auto.calimero.dptxlator.DPTXlator) Value(org.openremote.model.value.Value) UnknownHostException(java.net.UnknownHostException)

Example 2 with Value

use of org.openremote.model.value.Value in project openremote by openremote.

the class KNXConnection method updateConsumer.

protected void updateConsumer(byte[] data, StateDP datapoint, Consumer<Value> consumer) {
    // Convert to OR Value and notify the consumer
    Value value = null;
    if (data != null) {
        try {
            value = TypeMapper.toORValue(datapoint, data);
        } catch (Exception ex) {
            LOG.log(Level.WARNING, "Couldn't translate Group address value to DPT type: " + datapoint, ex);
        }
    }
    consumer.accept(value);
}
Also used : Value(org.openremote.model.value.Value) UnknownHostException(java.net.UnknownHostException)

Example 3 with Value

use of org.openremote.model.value.Value in project openremote by openremote.

the class MacroProtocol method doLinkAttribute.

@Override
protected void doLinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
    AttributeRef macroRef = protocolConfiguration.getReferenceOrThrow();
    // Check for executable meta item
    if (attribute.isExecutable()) {
        LOG.fine("Macro linked attribute is marked as executable so it will be linked to the firing of the macro");
        // Update the command Status of this attribute
        updateLinkedAttribute(new AttributeState(attribute.getReferenceOrThrow(), protocolConfiguration.isEnabled() ? AttributeExecuteStatus.READY.asValue() : AttributeExecuteStatus.DISABLED.asValue()));
        return;
    }
    // Check for action index or default to index 0
    int actionIndex = getMacroActionIndex(attribute).orElse(0);
    // Pull the macro action value out with the same type as the linked attribute
    // otherwise push a null value through to the attribute
    List<MacroAction> actions = getMacroActions(macroRef);
    Value actionValue = null;
    if (actions.isEmpty()) {
        LOG.fine("No actions are available for the linked macro, maybe it is disabled?: " + macroRef);
    } else {
        actionIndex = Math.min(actions.size(), Math.max(0, actionIndex));
        actionValue = actions.get(actionIndex).getAttributeState().getValue().orElse(null);
        LOG.fine("Attribute is linked to the value of macro action index: actionIndex");
    }
    if (actionValue != null) {
        // Verify the type of the attribute matches the action value
        if (attribute.getType().map(AttributeType::getValueType).orElse(null) != actionValue.getType()) {
            // Use a value of null so it is clear that the attribute isn't linked correctly
            actionValue = null;
        }
    }
    // Push the value of this macro action into the attribute
    updateLinkedAttribute(new AttributeState(attribute.getReferenceOrThrow(), actionValue));
}
Also used : Value(org.openremote.model.value.Value)

Example 4 with Value

use of org.openremote.model.value.Value in project openremote by openremote.

the class AssetProcessingService method storeAttributeValue.

protected void storeAttributeValue(EntityManager em, Asset asset, AssetAttribute attribute) throws AssetProcessingException {
    String attributeName = attribute.getName().orElseThrow(() -> new AssetProcessingException(STATE_STORAGE_FAILED, "cannot store asset state for attribute with no name on: " + asset));
    Value value = attribute.getValue().orElse(null);
    // If there is no timestamp, use system time (0 or -1 are "no timestamp")
    Optional<Long> timestamp = attribute.getValueTimestamp();
    String valueTimestamp = Long.toString(timestamp.filter(ts -> ts > 0).orElseGet(() -> timerService.getCurrentTimeMillis()));
    if (!assetStorageService.storeAttributeValue(em, asset.getId(), attributeName, value, valueTimestamp)) {
        throw new AssetProcessingException(STATE_STORAGE_FAILED, "database update failed, no rows updated");
    }
}
Also used : Value(org.openremote.model.value.Value)

Example 5 with Value

use of org.openremote.model.value.Value in project openremote by openremote.

the class AssetResourceImpl method writeAttributeValue.

@Override
public void writeAttributeValue(RequestParams requestParams, String assetId, String attributeName, String rawJson) {
    try {
        try {
            Value value = Values.instance().parse(rawJson).orElse(// When parsing literal JSON "null"
            null);
            AttributeEvent event = new AttributeEvent(new AttributeRef(assetId, attributeName), value, timerService.getCurrentTimeMillis());
            // Process asynchronously but block for a little while waiting for the result
            Map<String, Object> headers = new HashMap<>();
            headers.put(AttributeEvent.HEADER_SOURCE, CLIENT);
            headers.put(Constants.AUTH_CONTEXT, getAuthContext());
            Object result = messageBrokerService.getProducerTemplate().requestBodyAndHeaders(AssetProcessingService.ASSET_QUEUE, event, headers);
            if (result instanceof AssetProcessingException) {
                AssetProcessingException processingException = (AssetProcessingException) result;
                switch(processingException.getReason()) {
                    case ILLEGAL_SOURCE:
                    case NO_AUTH_CONTEXT:
                    case INSUFFICIENT_ACCESS:
                        throw new WebApplicationException(FORBIDDEN);
                    case ASSET_NOT_FOUND:
                    case ATTRIBUTE_NOT_FOUND:
                        throw new WebApplicationException(NOT_FOUND);
                    case INVALID_AGENT_LINK:
                    case ILLEGAL_AGENT_UPDATE:
                    case INVALID_ATTRIBUTE_EXECUTE_STATUS:
                        throw new IllegalStateException(processingException);
                    default:
                        throw processingException;
                }
            }
        } catch (ValueException ex) {
            throw new IllegalStateException("Error parsing JSON", ex);
        }
    } catch (IllegalStateException ex) {
        throw new WebApplicationException(ex, BAD_REQUEST);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ValueException(org.openremote.model.value.ValueException) Value(org.openremote.model.value.Value)

Aggregations

Value (org.openremote.model.value.Value)15 ValidationFailure (org.openremote.model.ValidationFailure)6 java.util (java.util)5 Values (org.openremote.model.value.Values)5 AssetAttribute (org.openremote.model.asset.AssetAttribute)4 ValueType (org.openremote.model.value.ValueType)4 IsWidget (com.google.gwt.user.client.ui.IsWidget)3 Collectors (java.util.stream.Collectors)3 FlowPanel (com.google.gwt.user.client.ui.FlowPanel)2 UnknownHostException (java.net.UnknownHostException)2 Date (java.util.Date)2 Level (java.util.logging.Level)2 Logger (java.util.logging.Logger)2 IntStream (java.util.stream.IntStream)2 EntityManager (javax.persistence.EntityManager)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 Environment (org.openremote.app.client.Environment)2 org.openremote.app.client.widget (org.openremote.app.client.widget)2 Container (org.openremote.container.Container)2 ContainerService (org.openremote.container.ContainerService)2