[lcn] Improve logging (#13460)

Signed-off-by: Fabian Wolter <github@fabian-wolter.de>
This commit is contained in:
Fabian Wolter 2022-09-29 09:01:20 +02:00 committed by GitHub
parent c244391d08
commit 08375cd3a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 20 deletions

View File

@ -335,7 +335,12 @@ public class LcnModuleHandler extends BaseThingHandler {
State convertedState = state; State convertedState = state;
if (converter != null) { if (converter != null) {
try {
convertedState = converter.onStateUpdateFromHandler(state); convertedState = converter.onStateUpdateFromHandler(state);
} catch (LcnException e) {
logger.warn("{}: {}{}: Value conversion failed: {}", moduleAddress, channelGroup, channelId,
e.getMessage());
}
} }
updateState(channelUid, convertedState); updateState(channelUid, convertedState);

View File

@ -30,8 +30,6 @@ import org.openhab.core.types.State;
*/ */
@NonNullByDefault @NonNullByDefault
public class VariableValue { public class VariableValue {
private static final String SENSOR_DEFECTIVE_STATE = "DEFECTIVE";
/** The absolute, native LCN value. */ /** The absolute, native LCN value. */
private final long nativeValue; private final long nativeValue;
@ -88,9 +86,9 @@ public class VariableValue {
public State getState(Variable variable) { public State getState(Variable variable) {
State stateValue; State stateValue;
if (variable.useLcnSpecialValues() && isSensorDefective()) { if (variable.useLcnSpecialValues() && isSensorDefective()) {
stateValue = new StringType(SENSOR_DEFECTIVE_STATE); stateValue = new StringType("Sensor defective: " + variable);
} else if (variable.useLcnSpecialValues() && !isConfigured()) { } else if (variable.useLcnSpecialValues() && !isConfigured()) {
stateValue = new StringType("Not configured in LCN-PRO"); stateValue = new StringType("Not configured in LCN-PRO: " + variable);
} else { } else {
stateValue = new DecimalType(toNative(variable.useLcnSpecialValues())); stateValue = new DecimalType(toNative(variable.useLcnSpecialValues()));
} }

View File

@ -30,8 +30,9 @@ public class Converter {
* *
* @param state from the Thing * @param state from the Thing
* @return human readable representational State * @return human readable representational State
* @throws LcnException
*/ */
public State onStateUpdateFromHandler(State state) { public State onStateUpdateFromHandler(State state) throws LcnException {
return state; return state;
} }

View File

@ -22,8 +22,6 @@ import org.openhab.binding.lcn.internal.common.LcnException;
import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.QuantityType; import org.openhab.core.library.types.QuantityType;
import org.openhab.core.types.State; import org.openhab.core.types.State;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* Base class for all LCN variable value converters. * Base class for all LCN variable value converters.
@ -32,7 +30,6 @@ import org.slf4j.LoggerFactory;
*/ */
@NonNullByDefault @NonNullByDefault
public class ValueConverter extends Converter { public class ValueConverter extends Converter {
private final Logger logger = LoggerFactory.getLogger(ValueConverter.class);
private @Nullable final Unit<?> unit; private @Nullable final Unit<?> unit;
private final Function<Long, Double> toHuman; private final Function<Long, Double> toHuman;
private final Function<Double, Long> toNative; private final Function<Double, Long> toNative;
@ -102,20 +99,20 @@ public class ValueConverter extends Converter {
* *
* @param state from the Thing * @param state from the Thing
* @return human readable State * @return human readable State
* @throws LcnException
*/ */
@Override @Override
public State onStateUpdateFromHandler(State state) { public State onStateUpdateFromHandler(State state) throws LcnException {
State result = state;
if (state instanceof DecimalType) { if (state instanceof DecimalType) {
Unit<?> localUnit = unit; Unit<?> localUnit = unit;
if (localUnit != null) { if (localUnit != null) {
result = QuantityType.valueOf(toHumanReadable(((DecimalType) state).longValue()), localUnit); return QuantityType.valueOf(toHumanReadable(((DecimalType) state).longValue()), localUnit);
}
} else {
logger.warn("Unexpected state type: {}", state.getClass().getSimpleName());
} }
return result; return state;
} else {
throw new LcnException("Unexpected state type: Was " + state.getClass().getSimpleName()
+ " but expected DecimalType: " + state);
}
} }
} }

View File

@ -121,7 +121,8 @@ public class LcnModuleRvarSetpointSubHandlerTest extends AbstractTestLcnModuleSu
@Test @Test
public void testRvar1SensorDefective() { public void testRvar1SensorDefective() {
tryParseAllHandlers("=M000005.S132512"); 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).updateChannel(LcnChannelGroup.RVARLOCK, "1", OnOffType.OFF);
verify(handler, times(2)).updateChannel(any(), any(), any()); verify(handler, times(2)).updateChannel(any(), any(), any());
} }

View File

@ -82,14 +82,15 @@ public class LcnModuleVariableSubHandlerTest extends AbstractTestLcnModuleSubHan
@Test @Test
public void testStatusVariable10SensorDefective() { public void testStatusVariable10SensorDefective() {
tryParseAllHandlers("=M000005.A01032512"); 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()); verify(handler).updateChannel(any(), any(), any());
} }
@Test @Test
public void testStatusVariable8NotConfigured() { public void testStatusVariable8NotConfigured() {
tryParseAllHandlers("=M000005.A00865535"); 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()); verify(handler).updateChannel(any(), any(), any());
} }
} }