use of org.openremote.model.value.Value in project openremote by openremote.
the class KNXConnection method onGroupAddressUpdated.
protected void onGroupAddressUpdated(GroupAddress groupAddress, byte[] value) {
synchronized (groupAddressStateMap) {
// Update the state map and notify consumers
groupAddressStateMap.compute(groupAddress, (ga, oldValue) -> value);
}
synchronized (groupAddressConsumerMap) {
groupAddressConsumerMap.computeIfPresent(groupAddress, (ga, datapointAndConsumerList) -> {
datapointAndConsumerList.forEach(datapointAndConsumer -> {
StateDP datapoint = datapointAndConsumer.key;
Consumer<Value> consumer = datapointAndConsumer.value;
updateConsumer(value, datapoint, consumer);
});
return datapointAndConsumerList;
});
}
}
use of org.openremote.model.value.Value in project openremote by openremote.
the class TypeMapper method toORValue.
public static Value toORValue(Datapoint datapoint, byte[] data) throws Exception {
DPTXlator translator = TranslatorTypes.createTranslator(0, datapoint.getDPT());
translator.setData(data);
LOG.info("Received KNX data: " + translator.getType().getID() + " (" + translator.getType().getDescription() + ") is " + translator.getValue() + " (" + translator.getNumericValue() + ") - " + datapoint.getName());
Value value;
if (translator instanceof DPTXlatorBoolean) {
value = Values.create(((DPTXlatorBoolean) translator).getValueBoolean());
} else if (translator instanceof DPTXlator2ByteFloat) {
value = Values.create(new BigDecimal(translator.getNumericValue()).setScale(2, RoundingMode.HALF_UP).doubleValue());
} else if (translator instanceof DPTXlator8BitUnsigned) {
value = Values.create(translator.getNumericValue());
} else {
// TODO depending on the DPTXlator a more sophisticated translation to value is needed
value = Values.create(translator.getValue());
}
return value;
}
use of org.openremote.model.value.Value in project openremote by openremote.
the class SimulatorProtocol method putValue.
/**
* Call this to simulate a send to actuator.
*/
public boolean putValue(AttributeState attributeState) {
Boolean result = withLockReturning(getProtocolName() + "::putValue", () -> {
AttributeRef attributeRef = attributeState.getAttributeRef();
AttributeRef instanceRef = attributeInstanceMap.get(attributeRef);
if (instanceRef == null) {
LOG.warning("Attribute is not referenced by an instance:" + attributeRef);
return false;
}
Instance instance = instances.get(instanceRef);
if (instance == null) {
LOG.warning("No instance found by name '" + instanceRef + "'");
return false;
}
if (!instance.isEnabled()) {
LOG.fine("Simulator protocol configuration is disabled so cannot process request");
return false;
}
LOG.info("Put simulator value: " + attributeState);
SimulatorElement element = elements.get(attributeRef);
if (element == null) {
LOG.warning("No simulated element for: " + attributeRef);
return false;
}
Optional<Value> oldValue = element.getValue();
element.setValue(attributeState.getValue().orElse(null));
List<ValidationFailure> failures = element.getValidationFailures();
if (!failures.isEmpty()) {
// Reset to old value
oldValue.ifPresent(element::setValue);
LOG.warning("Failed to update simulator element, state validation failures " + failures + ": " + attributeRef);
return false;
}
if (instance.getMode() != Mode.MANUAL) {
updateSensor(attributeRef, instance.getMode() == Mode.WRITE_THROUGH_IMMEDIATE ? 0 : instance.getDelayMilliseconds());
}
return true;
});
return result != null ? result : false;
}
use of org.openremote.model.value.Value in project openremote by openremote.
the class TimerProtocol method doLinkAttribute.
@Override
protected void doLinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
TimerValue timerValue = TimerConfiguration.getValue(attribute).orElse(null);
if (timerValue == null) {
LOG.warning("Attribute doesn't have a valid timer value: " + attribute.getReferenceOrThrow());
return;
}
LOG.fine("Attribute is linked to timer value: " + timerValue);
switch(timerValue) {
case ENABLED:
updateLinkedAttribute(new AttributeState(attribute.getReferenceOrThrow(), Values.create(protocolConfiguration.isEnabled())));
break;
case CRON_EXPRESSION:
case TIME:
CronExpressionParser parser = cronExpressionMap.get(protocolConfiguration.getReferenceOrThrow());
if (parser == null) {
LOG.info("Attribute is linked to an invalid timer so it will be ignored");
return;
}
Value value = timerValue == TimerValue.CRON_EXPRESSION ? Values.create(parser.buildCronExpression()) : Values.create(parser.getFormattedTime());
updateLinkedAttribute(new AttributeState(attribute.getReferenceOrThrow(), value));
break;
}
}
use of org.openremote.model.value.Value in project openremote by openremote.
the class ProgramsProcessor method getPropertyWritePackets.
@Override
public List<VelbusPacket> getPropertyWritePackets(VelbusDevice device, String property, Value value) {
Optional<Pair<Integer, String>> channelNumberAndPropertySuffix = getChannelNumberAndPropertySuffix(device, CHANNEL_REGEX, property);
if (channelNumberAndPropertySuffix.isPresent()) {
switch(channelNumberAndPropertySuffix.get().value) {
case PROGRAM_STEPS_ENABLED_SUFFIX:
return Values.getBooleanCoerced(value).map(enabled -> getProgramStepsPackets(device, channelNumberAndPropertySuffix.get().key, enabled, 0xFFFFF)).orElse(null);
case PROGRAM_STEPS_DISABLED_SECONDS_SUFFIX:
return Values.getIntegerCoerced(value).map(durationSeconds -> getProgramStepsPackets(device, channelNumberAndPropertySuffix.get().key, false, durationSeconds)).orElse(null);
}
return null;
}
if (property.equals("ALL" + PROGRAM_STEPS_ENABLED_SUFFIX)) {
return Values.getBooleanCoerced(value).map(enabled -> getProgramStepsPackets(device, 0xFF, enabled, 0xFFFFF)).orElse(null);
}
if (property.equals("ALL" + PROGRAM_STEPS_DISABLED_SECONDS_SUFFIX)) {
return Values.getIntegerCoerced(value).map(durationSeconds -> getProgramStepsPackets(device, 0xFF, false, durationSeconds)).orElse(null);
}
if (property.equals("PROGRAM")) {
return EnumUtil.enumFromValue(Program.class, value).map(program -> Collections.singletonList(new VelbusPacket(device.getBaseAddress(), VelbusPacket.OutboundCommand.PROGRAM_SELECT.getCode(), VelbusPacket.PacketPriority.LOW, (byte) program.getCode()))).orElse(null);
}
if (property.equals("SUNRISE_ENABLED") || property.equals("SUNSET_ENABLED")) {
return Values.getBooleanCoerced(value).map(enabled -> {
boolean sunriseEnabled = device.getPropertyValue("SUNRISE_ENABLED") != null && ((BooleanDevicePropertyValue) device.getPropertyValue("SUNRISE_ENABLED")).getPropertyValue();
boolean sunsetEnabled = device.getPropertyValue("SUNSET_ENABLED") != null && ((BooleanDevicePropertyValue) device.getPropertyValue("SUNSET_ENABLED")).getPropertyValue();
if (property.equals("SUNRISE_ENABLED")) {
sunriseEnabled = enabled;
} else {
sunsetEnabled = enabled;
}
int sunriseSunsetByte = 0;
if (sunriseEnabled) {
sunriseSunsetByte += 1;
}
if (sunsetEnabled) {
sunriseSunsetByte += 2;
}
// Push new values into the device cache so they are instantly available
device.setProperty("SUNRISE_ENABLED", sunriseEnabled ? BooleanDevicePropertyValue.TRUE : BooleanDevicePropertyValue.FALSE);
device.setProperty("SUNSET_ENABLED", sunsetEnabled ? BooleanDevicePropertyValue.TRUE : BooleanDevicePropertyValue.FALSE);
return Collections.singletonList(new VelbusPacket(device.getBaseAddress(), VelbusPacket.OutboundCommand.SET_SUNRISE_SUNSET.getCode(), VelbusPacket.PacketPriority.LOW, (byte) 0xFF, (byte) sunriseSunsetByte));
}).orElse(null);
}
if (property.startsWith("ALARM")) {
int alarmNumber;
try {
alarmNumber = Integer.parseInt(property.substring(5, 6));
} catch (NumberFormatException e) {
LOG.log(Level.INFO, "Alarm number must be number 1 or 2");
return null;
}
String alarmProperty = property.substring(7);
switch(alarmProperty) {
case "ENABLED":
case "WAKE_TIME":
case "BED_TIME":
String alarmEnabledProperty = "ALARM" + alarmNumber + "_ENABLED";
String alarmWakeTimeProperty = "ALARM" + alarmNumber + "_WAKE_TIME";
String alarmBedTimeProperty = "ALARM" + alarmNumber + "_BED_TIME";
String alarmMasterProperty = "ALARM" + alarmNumber + "_MASTER";
if (!device.hasPropertyValue(alarmEnabledProperty) || !device.hasPropertyValue(alarmWakeTimeProperty) || !device.hasPropertyValue(alarmBedTimeProperty) || !device.hasPropertyValue(alarmMasterProperty)) {
LOG.info("Device cache doesn't contain alarm enabled, type and/or time properties");
return null;
}
boolean enabled = ((BooleanDevicePropertyValue) device.getPropertyValue(alarmEnabledProperty)).getPropertyValue();
String wakeTime = ((StringDevicePropertyValue) device.getPropertyValue(alarmWakeTimeProperty)).getPropertyValue();
String bedTime = ((StringDevicePropertyValue) device.getPropertyValue(alarmBedTimeProperty)).getPropertyValue();
boolean isGlobal = ((BooleanDevicePropertyValue) device.getPropertyValue(alarmMasterProperty)).getPropertyValue();
if ("ENABLED".equals(alarmProperty)) {
Optional<Boolean> setEnabled = Values.getBooleanCoerced(value);
if (!setEnabled.isPresent()) {
return null;
}
enabled = setEnabled.get();
} else if ("WAKE_TIME".equals(alarmProperty)) {
if (value == null) {
return null;
}
wakeTime = value.toString();
} else if ("BED_TIME".equals(alarmProperty)) {
if (value == null) {
return null;
}
bedTime = value.toString();
}
String[] wakeTimeValues = wakeTime.split(":");
String[] bedTimeValues = bedTime.split(":");
if (wakeTimeValues.length != 2 || bedTimeValues.length != 2) {
LOG.info("Time property values must be in the format HH:mm");
return null;
}
int wakeHours, wakeMins, bedHours, bedMins;
try {
wakeHours = Integer.parseInt(wakeTimeValues[0]);
wakeMins = Integer.parseInt(wakeTimeValues[1]);
bedHours = Integer.parseInt(bedTimeValues[0]);
bedMins = Integer.parseInt(bedTimeValues[1]);
if (wakeHours < 0 || wakeHours > 23 || wakeMins < 0 || wakeMins > 59 || bedHours < 0 || bedHours > 23 || bedMins < 0 || bedMins > 59) {
throw new NumberFormatException("Hours and/or minutes out of allowed range");
}
} catch (NumberFormatException e) {
LOG.log(Level.INFO, "Time property values must be in the format HH:mm", e);
return null;
}
// Update property values and also send memory read request to ensure update occurred
device.setProperty(alarmEnabledProperty, enabled ? BooleanDevicePropertyValue.TRUE : BooleanDevicePropertyValue.FALSE);
device.setProperty(alarmWakeTimeProperty, new StringDevicePropertyValue(wakeTime));
device.setProperty(alarmBedTimeProperty, new StringDevicePropertyValue(bedTime));
device.velbusNetwork.scheduleTask(() -> {
List<VelbusPacket> packets = getStatusRequestPackets(device);
device.velbusNetwork.sendPackets(packets.toArray(new VelbusPacket[packets.size()]));
}, 500);
return Collections.singletonList(new VelbusPacket(isGlobal ? 0x00 : device.getBaseAddress(), VelbusPacket.OutboundCommand.SET_ALARM.getCode(), VelbusPacket.PacketPriority.LOW, (byte) alarmNumber, (byte) wakeHours, (byte) wakeMins, (byte) bedHours, (byte) bedMins, (byte) (enabled ? 1 : 0)));
}
}
return null;
}
Aggregations