Skip to content

Commit

Permalink
added conversion to/from Celsius as required by openwebnet lib. Renam…
Browse files Browse the repository at this point in the history
…ed handleSetpoint and handleSetFanSpeed

Signed-off-by: Massimo Valla <mvcode00@gmail.com>
  • Loading branch information
mvalla committed May 29, 2021
1 parent 957e71c commit 64290b9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand All @@ -123,45 +114,47 @@ 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) {
try {
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<Temperature>(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());
}
}

Expand Down Expand Up @@ -268,23 +261,21 @@ 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);
}
}

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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 <U extends Quantity<U>> State getAsQuantityTypeOrNull(@Nullable Number value, Unit<U> 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.
*
Expand Down

0 comments on commit 64290b9

Please sign in to comment.