Skip to content

Commit

Permalink
[lcn] Improve logging (openhab#13460)
Browse files Browse the repository at this point in the history
Signed-off-by: Fabian Wolter <github@fabian-wolter.de>
  • Loading branch information
fwolter authored and seime committed Oct 16, 2022
1 parent 1e44b70 commit b3de3a3
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,12 @@ public void updateChannel(LcnChannelGroup channelGroup, String channelId, State

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
*/
@NonNullByDefault
public class VariableValue {
private static final String SENSOR_DEFECTIVE_STATE = "DEFECTIVE";

/** The absolute, native LCN value. */
private final long nativeValue;

Expand Down Expand Up @@ -88,9 +86,9 @@ public boolean isConfigured() {
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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public class Converter {
*
* @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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
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.
Expand All @@ -32,7 +30,6 @@
*/
@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;
Expand Down Expand Up @@ -102,20 +99,20 @@ public DecimalType onCommandFromItem(QuantityType<?> quantityType) throws LcnExc
*
* @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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ public void testRvar2() {
@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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,15 @@ public void testHandleCommandLegacyTvarNegative() throws LcnException {
@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());
}
}

0 comments on commit b3de3a3

Please sign in to comment.