From 64290b9e16537cf81230e5134d19e407c8e61d78 Mon Sep 17 00:00:00 2001 From: Massimo Valla Date: Sat, 29 May 2021 17:02:04 +0200 Subject: [PATCH] added conversion to/from Celsius as required by openwebnet lib. Renamed handleSetpoint and handleSetFanSpeed Signed-off-by: Massimo Valla --- .../OpenWebNetThermoregulationHandler.java | 53 ++++++++----------- .../handler/OpenWebNetThingHandler.java | 17 ++++++ 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/handler/OpenWebNetThermoregulationHandler.java b/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/handler/OpenWebNetThermoregulationHandler.java index 3a2ac9a906d9e..01a16d3aacb13 100644 --- a/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/handler/OpenWebNetThermoregulationHandler.java +++ b/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/handler/OpenWebNetThermoregulationHandler.java @@ -12,25 +12,16 @@ */ package org.openhab.binding.openwebnet.handler; -import static org.openhab.binding.openwebnet.OpenWebNetBindingConstants.CHANNEL_ACTUATOR; -import static org.openhab.binding.openwebnet.OpenWebNetBindingConstants.CHANNEL_CONDITIONING_VALVE; -import static org.openhab.binding.openwebnet.OpenWebNetBindingConstants.CHANNEL_FAN_SPEED; -import static org.openhab.binding.openwebnet.OpenWebNetBindingConstants.CHANNEL_FUNCTION; -import static org.openhab.binding.openwebnet.OpenWebNetBindingConstants.CHANNEL_HEATING_VALVE; -import static org.openhab.binding.openwebnet.OpenWebNetBindingConstants.CHANNEL_MODE; -import static org.openhab.binding.openwebnet.OpenWebNetBindingConstants.CHANNEL_TEMPERATURE; -import static org.openhab.binding.openwebnet.OpenWebNetBindingConstants.CHANNEL_TEMP_SETPOINT; - -import java.math.BigDecimal; -import java.util.Set; +import static org.openhab.binding.openwebnet.OpenWebNetBindingConstants.*; -import javax.measure.quantity.Temperature; +import java.util.Set; import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.binding.openwebnet.OpenWebNetBindingConstants; import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.QuantityType; import org.openhab.core.library.types.StringType; +import org.openhab.core.library.unit.SIUnits; import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.Thing; import org.openhab.core.thing.ThingStatus; @@ -87,7 +78,7 @@ public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) { protected void handleChannelCommand(ChannelUID channel, Command command) { switch (channel.getId()) { case CHANNEL_TEMP_SETPOINT: - handleSetpointCommand(command); + handleSetpoint(command); break; case CHANNEL_FUNCTION: handleFunction(command); @@ -96,7 +87,7 @@ protected void handleChannelCommand(ChannelUID channel, Command command) { handleMode(command); break; case CHANNEL_FAN_SPEED: - handleSetFanSpeedCommand(command); + handleSetFanSpeed(command); break; default: { logger.warn("handleChannelCommand() Unsupported ChannelUID {}", channel.getId()); @@ -123,7 +114,7 @@ protected String ownIdPrefix() { return Who.THERMOREGULATION.value().toString(); } - private void handleSetFanSpeedCommand(Command command) { + private void handleSetFanSpeed(Command command) { if (command instanceof StringType) { Where w = deviceWhere; if (w != null) { @@ -131,37 +122,39 @@ private void handleSetFanSpeedCommand(Command command) { Thermoregulation.FanCoilSpeed speed = Thermoregulation.FanCoilSpeed.valueOf(command.toString()); send(Thermoregulation.requestWriteFanCoilSpeed(w.value(), speed)); } catch (OWNException e) { - logger.warn("handleSetFanSpeedCommand() {}", e.getMessage()); + logger.warn("handleSetFanSpeed() {}", e.getMessage()); } catch (IllegalArgumentException e) { - logger.warn("handleSetFanSpeedCommand() Unsupported command {} for thing {}", command, + logger.warn("handleSetFanSpeed() Unsupported command {} for thing {}", command, getThing().getUID()); return; } } } else { - logger.warn("handleSetFanSpeedCommand() Unsupported command {} for thing {}", command, getThing().getUID()); + logger.warn("handleSetFanSpeed() Unsupported command {} for thing {}", command, getThing().getUID()); } } - private void handleSetpointCommand(Command command) { + private void handleSetpoint(Command command) { if (command instanceof QuantityType || command instanceof DecimalType) { Where w = deviceWhere; if (w != null) { - BigDecimal value = BigDecimal.ZERO; + double newTemp = 0; if (command instanceof QuantityType) { - value = new QuantityType(command.toFullString()).toBigDecimal(); + QuantityType tempCelsius = ((QuantityType) command).toUnit(SIUnits.CELSIUS); + if (tempCelsius != null) { + newTemp = tempCelsius.doubleValue(); + } } else { - value = ((DecimalType) command).toBigDecimal(); + newTemp = ((DecimalType) command).doubleValue(); } try { - send(Thermoregulation.requestWriteSetpointTemperature(w.value(), value.floatValue(), - currentFunction)); + send(Thermoregulation.requestWriteSetpointTemperature(w.value(), newTemp, currentFunction)); } catch (MalformedFrameException | OWNException e) { - logger.warn("handleSetpointCommand() {}", e.getMessage()); + logger.warn("handleSetpoint() {}", e.getMessage()); } } } else { - logger.warn("handleSetpointCommand() Unsupported command {} for thing {}", command, getThing().getUID()); + logger.warn("handleSetpoint() Unsupported command {} for thing {}", command, getThing().getUID()); } } @@ -268,7 +261,7 @@ private void updateModeAndFunction(Thermoregulation tmsg) { private void updateTemperature(Thermoregulation tmsg) { try { double temp = Thermoregulation.parseTemperature(tmsg); - updateState(CHANNEL_TEMPERATURE, new DecimalType(temp)); + updateState(CHANNEL_TEMPERATURE, getAsQuantityTypeOrNull(temp, SIUnits.CELSIUS)); } catch (FrameException e) { logger.warn("updateTemperature() FrameException on frame {}: {}", tmsg, e.getMessage()); updateState(CHANNEL_TEMPERATURE, UnDefType.UNDEF); @@ -276,15 +269,13 @@ private void updateTemperature(Thermoregulation tmsg) { } private void updateSetpoint(Thermoregulation tmsg) { - String channelID = CHANNEL_TEMP_SETPOINT; try { double temp = Thermoregulation.parseTemperature(tmsg); - updateState(channelID, new DecimalType(temp)); - // store current setPoint T + updateState(CHANNEL_TEMP_SETPOINT, getAsQuantityTypeOrNull(temp, SIUnits.CELSIUS)); currentSetPointTemp = temp; } catch (FrameException e) { logger.warn("updateSetpoint() FrameException on frame {}: {}", tmsg, e.getMessage()); - updateState(channelID, UnDefType.UNDEF); + updateState(CHANNEL_TEMP_SETPOINT, UnDefType.UNDEF); } } diff --git a/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/handler/OpenWebNetThingHandler.java b/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/handler/OpenWebNetThingHandler.java index 03d94a71db48e..1ff15ef96cc6c 100644 --- a/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/handler/OpenWebNetThingHandler.java +++ b/bundles/org.openhab.binding.openwebnet/src/main/java/org/openhab/binding/openwebnet/handler/OpenWebNetThingHandler.java @@ -18,8 +18,12 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +import javax.measure.Quantity; +import javax.measure.Unit; + import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.library.types.QuantityType; import org.openhab.core.thing.Bridge; import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.Thing; @@ -28,6 +32,8 @@ import org.openhab.core.thing.binding.BaseThingHandler; import org.openhab.core.types.Command; import org.openhab.core.types.RefreshType; +import org.openhab.core.types.State; +import org.openhab.core.types.UnDefType; import org.openwebnet4j.OpenGateway; import org.openwebnet4j.communication.OWNException; import org.openwebnet4j.communication.Response; @@ -221,6 +227,17 @@ public void dispose() { super.dispose(); } + /** + * Helper method to return a Quantity from a Number value or UnDefType.NULL if value is null + * + * @param value to be used + * @param unit to be used + * @return Quantity + */ + protected > State getAsQuantityTypeOrNull(@Nullable Number value, Unit unit) { + return value == null ? UnDefType.NULL : new QuantityType<>(value, unit); + } + /** * Returns a prefix String for ownId specific for each handler. To be implemented by sub-classes. *