State convertedState = state;
if (converter != null) {
- convertedState = converter.onStateUpdateFromHandler(state);
+ try {
+ convertedState = converter.onStateUpdateFromHandler(state);
+ } catch (LcnException e) {
+ logger.warn("{}: {}{}: Value conversion failed: {}", moduleAddress, channelGroup, channelId,
+ e.getMessage());
+ }
}
updateState(channelUid, convertedState);
*/
@NonNullByDefault
public class VariableValue {
- private static final String SENSOR_DEFECTIVE_STATE = "DEFECTIVE";
-
/** The absolute, native LCN value. */
private final long nativeValue;
public State getState(Variable variable) {
State stateValue;
if (variable.useLcnSpecialValues() && isSensorDefective()) {
- stateValue = new StringType(SENSOR_DEFECTIVE_STATE);
+ stateValue = new StringType("Sensor defective: " + variable);
} else if (variable.useLcnSpecialValues() && !isConfigured()) {
- stateValue = new StringType("Not configured in LCN-PRO");
+ stateValue = new StringType("Not configured in LCN-PRO: " + variable);
} else {
stateValue = new DecimalType(toNative(variable.useLcnSpecialValues()));
}
*
* @param state from the Thing
* @return human readable representational State
+ * @throws LcnException
*/
- public State onStateUpdateFromHandler(State state) {
+ public State onStateUpdateFromHandler(State state) throws LcnException {
return state;
}
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.types.State;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
/**
* Base class for all LCN variable value converters.
*/
@NonNullByDefault
public class ValueConverter extends Converter {
- private final Logger logger = LoggerFactory.getLogger(ValueConverter.class);
private @Nullable final Unit<?> unit;
private final Function<Long, Double> toHuman;
private final Function<Double, Long> toNative;
*
* @param state from the Thing
* @return human readable State
+ * @throws LcnException
*/
@Override
- public State onStateUpdateFromHandler(State state) {
- State result = state;
-
+ public State onStateUpdateFromHandler(State state) throws LcnException {
if (state instanceof DecimalType) {
Unit<?> localUnit = unit;
if (localUnit != null) {
- result = QuantityType.valueOf(toHumanReadable(((DecimalType) state).longValue()), localUnit);
+ return QuantityType.valueOf(toHumanReadable(((DecimalType) state).longValue()), localUnit);
}
+
+ return state;
} else {
- logger.warn("Unexpected state type: {}", state.getClass().getSimpleName());
+ throw new LcnException("Unexpected state type: Was " + state.getClass().getSimpleName()
+ + " but expected DecimalType: " + state);
}
-
- return result;
}
}
@Test
public void testRvar1SensorDefective() {
tryParseAllHandlers("=M000005.S132512");
- verify(handler).updateChannel(LcnChannelGroup.RVARSETPOINT, "1", new StringType("DEFECTIVE"));
+ verify(handler).updateChannel(LcnChannelGroup.RVARSETPOINT, "1",
+ new StringType("Sensor defective: RVARSETPOINT1"));
verify(handler).updateChannel(LcnChannelGroup.RVARLOCK, "1", OnOffType.OFF);
verify(handler, times(2)).updateChannel(any(), any(), any());
}
@Test
public void testStatusVariable10SensorDefective() {
tryParseAllHandlers("=M000005.A01032512");
- verify(handler).updateChannel(LcnChannelGroup.VARIABLE, "10", new StringType("DEFECTIVE"));
+ verify(handler).updateChannel(LcnChannelGroup.VARIABLE, "10", new StringType("Sensor defective: VARIABLE10"));
verify(handler).updateChannel(any(), any(), any());
}
@Test
public void testStatusVariable8NotConfigured() {
tryParseAllHandlers("=M000005.A00865535");
- verify(handler).updateChannel(LcnChannelGroup.VARIABLE, "8", new StringType("Not configured in LCN-PRO"));
+ verify(handler).updateChannel(LcnChannelGroup.VARIABLE, "8",
+ new StringType("Not configured in LCN-PRO: VARIABLE8"));
verify(handler).updateChannel(any(), any(), any());
}
}