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());
}
}
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);
}
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));
}
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");
}
}
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);
}
}
Aggregations