use of org.openhab.binding.modbus.internal.ValueTransformation in project openhab-addons by openhab.
the class ModbusDataThingHandler method processUpdatedValue.
/**
* Update linked channels
*
* @param numericState numeric state corresponding to polled data (or UNDEF with floating point NaN or infinity)
* @param boolValue boolean value corresponding to polled data
* @return updated channel data
*/
private Map<ChannelUID, State> processUpdatedValue(State numericState, boolean boolValue) {
ValueTransformation localReadTransformation = readTransformation;
if (localReadTransformation == null) {
// We should always have transformation available if thing is initalized properly
logger.trace("No transformation available, aborting processUpdatedValue");
return Collections.emptyMap();
}
Map<ChannelUID, State> states = new HashMap<>();
CHANNEL_ID_TO_ACCEPTED_TYPES.keySet().stream().forEach(channelId -> {
ChannelUID channelUID = getChannelUID(channelId);
if (!isLinked(channelUID)) {
return;
}
List<Class<? extends State>> acceptedDataTypes = CHANNEL_ID_TO_ACCEPTED_TYPES.get(channelId);
if (acceptedDataTypes.isEmpty()) {
return;
}
State boolLikeState;
if (containsOnOff(acceptedDataTypes)) {
boolLikeState = boolValue ? OnOffType.ON : OnOffType.OFF;
} else if (containsOpenClosed(acceptedDataTypes)) {
boolLikeState = boolValue ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
} else {
boolLikeState = null;
}
State transformedState;
if (localReadTransformation.isIdentityTransform()) {
if (boolLikeState != null) {
// A bit of smartness for ON/OFF and OPEN/CLOSED with boolean like items
transformedState = boolLikeState;
} else {
// Numeric states always go through transformation. This allows value of 17.5 to be
// converted to
// 17.5% with percent types (instead of raising error)
transformedState = localReadTransformation.transformState(bundleContext, acceptedDataTypes, numericState);
}
} else {
transformedState = localReadTransformation.transformState(bundleContext, acceptedDataTypes, numericState);
}
if (transformedState != null) {
logger.trace("Channel {} will be updated to '{}' (type {}). Input data: number value {} (value type '{}' taken into account) and bool value {}. Transformation: {}", channelId, transformedState, transformedState.getClass().getSimpleName(), numericState, readValueType, boolValue, localReadTransformation.isIdentityTransform() ? "<identity>" : localReadTransformation);
states.put(channelUID, transformedState);
} else {
String types = String.join(", ", acceptedDataTypes.stream().map(cls -> cls.getSimpleName()).toArray(String[]::new));
logger.warn("Channel {} will not be updated since transformation was unsuccessful. Channel is expecting the following data types [{}]. Input data: number value {} (value type '{}' taken into account) and bool value {}. Transformation: {}", channelId, types, numericState, readValueType, boolValue, localReadTransformation.isIdentityTransform() ? "<identity>" : localReadTransformation);
}
});
ChannelUID lastReadSuccessUID = getChannelUID(ModbusBindingConstantsInternal.CHANNEL_LAST_READ_SUCCESS);
if (isLinked(lastReadSuccessUID)) {
states.put(lastReadSuccessUID, new DateTimeType());
}
updateExpiredChannels(states);
return states;
}
use of org.openhab.binding.modbus.internal.ValueTransformation in project openhab-addons by openhab.
the class ModbusDataThingHandler method transformCommandAndProcessJSON.
/**
* Transform received command using the transformation.
*
* In case of JSON as transformation output, the output processed using {@link processJsonTransform}.
*
* @param channelUID channel UID corresponding to received command
* @param command command to be transformed
* @return transformed command. Null is returned with JSON transformation outputs and configuration errors
*
* @see processJsonTransform
*/
@Nullable
private Optional<Command> transformCommandAndProcessJSON(ChannelUID channelUID, Command command) {
String transformOutput;
Optional<Command> transformedCommand;
ValueTransformation writeTransformation = this.writeTransformation;
if (writeTransformation == null || writeTransformation.isIdentityTransform()) {
transformedCommand = Optional.of(command);
} else {
transformOutput = writeTransformation.transform(bundleContext, command.toString());
if (transformOutput.contains("[")) {
processJsonTransform(command, transformOutput);
return null;
} else if (writeParametersHavingTransformationOnly) {
updateStatusIfChanged(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, String.format("Seems to have writeTransformation but no other write parameters. Since the transformation did not return a JSON for command '%s' (channel %s), this is a configuration error", command, channelUID));
return null;
} else {
transformedCommand = SingleValueTransformation.tryConvertToCommand(transformOutput);
logger.trace("Converted transform output '{}' to command '{}' (type {})", transformOutput, transformedCommand.map(c -> c.toString()).orElse("<conversion failed>"), transformedCommand.map(c -> c.getClass().getName()).orElse("<conversion failed>"));
}
}
return transformedCommand;
}
Aggregations