use of org.openremote.model.util.TextUtil in project openremote by openremote.
the class TimerProtocol method processLinkedAttributeWrite.
@Override
protected void processLinkedAttributeWrite(AttributeEvent event, AssetAttribute protocolConfiguration) {
AssetAttribute attribute = getLinkedAttribute(event.getAttributeRef());
TimerValue timerValue = TimerConfiguration.getValue(attribute).orElse(null);
if (timerValue == null) {
LOG.warning("Attribute doesn't have a valid timer value so ignoring write request: " + attribute.getReferenceOrThrow());
return;
}
// Don't remove or alter any running timer just push update back through the system and wait for link/unlink
// protocol configuration method call
Optional<String> writeValue = event.getValue().flatMap(Values::getString).flatMap(TextUtil::asNonNullAndNonEmpty);
switch(timerValue) {
case ENABLED:
// check event value is a boolean
boolean enabled = Values.getBoolean(event.getValue().orElse(null)).orElseThrow(() -> new IllegalStateException("Writing to protocol configuration CONNECTED property requires a boolean value"));
if (enabled == protocolConfiguration.isEnabled()) {
LOG.finer("Protocol configuration enabled status is already: " + enabled);
} else {
LOG.fine("Updating protocol configuration enabled status");
updateLinkedProtocolConfiguration(protocolConfiguration, protocolConfig -> protocolConfig.setDisabled(!enabled));
}
break;
case CRON_EXPRESSION:
// but that is handled gracefully
if (!writeValue.isPresent()) {
LOG.warning("Send to actuator value for time trigger must be a non empty string");
return;
}
updateTimerValue(new AttributeState(protocolConfiguration.getReferenceOrThrow(), Values.create(writeValue.get().trim())));
break;
case TIME:
if (!writeValue.isPresent()) {
LOG.warning("Send to actuator value for time trigger must be a non empty string");
return;
}
CronExpressionParser parser = cronExpressionMap.get(protocolConfiguration.getReferenceOrThrow());
if (parser == null) {
LOG.info("Ignoring trigger update because current cron expression is invalid");
return;
}
String[] writeTimeValues = writeValue.get().trim().split(":");
Integer hours;
Integer minutes;
Integer seconds;
if (writeTimeValues.length != 3 || (hours = CronExpressionParser.parseNumberExpression(writeTimeValues[0])) == null || (minutes = CronExpressionParser.parseNumberExpression(writeTimeValues[1])) == null || (seconds = CronExpressionParser.parseNumberExpression(writeTimeValues[2])) == null) {
LOG.info("Expected value to be in format HH:MM:SS, actual: " + writeValue);
return;
}
parser.setTime(hours, minutes, seconds);
updateTimerValue(new AttributeState(protocolConfiguration.getReferenceOrThrow(), Values.create(parser.buildCronExpression())));
break;
default:
throw new NotSupportedException("Unsupported timer value: " + timerValue);
}
}
Aggregations