]> git.basschouten.com Git - openhab-addons.git/commitdiff
[lcn] Improve logging (#13460)
authorFabian Wolter <github@fabian-wolter.de>
Thu, 29 Sep 2022 07:01:20 +0000 (09:01 +0200)
committerGitHub <noreply@github.com>
Thu, 29 Sep 2022 07:01:20 +0000 (09:01 +0200)
Signed-off-by: Fabian Wolter <github@fabian-wolter.de>
bundles/org.openhab.binding.lcn/src/main/java/org/openhab/binding/lcn/internal/LcnModuleHandler.java
bundles/org.openhab.binding.lcn/src/main/java/org/openhab/binding/lcn/internal/common/VariableValue.java
bundles/org.openhab.binding.lcn/src/main/java/org/openhab/binding/lcn/internal/converter/Converter.java
bundles/org.openhab.binding.lcn/src/main/java/org/openhab/binding/lcn/internal/converter/ValueConverter.java
bundles/org.openhab.binding.lcn/src/test/java/org/openhab/binding/lcn/internal/subhandler/LcnModuleRvarSetpointSubHandlerTest.java
bundles/org.openhab.binding.lcn/src/test/java/org/openhab/binding/lcn/internal/subhandler/LcnModuleVariableSubHandlerTest.java

index 13633b0c3b0b1e8e9393e48be31d437e4366df24..25a53beea1ab07b0c6c039f10b740544746a5b53 100644 (file)
@@ -335,7 +335,12 @@ public class LcnModuleHandler extends BaseThingHandler {
 
         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);
index f61af1d234a89a9a3cc8f3d5c4504dc2d406f4b4..e679fdc7db90ba75a6ec70508365fbabb66fe45d 100644 (file)
@@ -30,8 +30,6 @@ import org.openhab.core.types.State;
  */
 @NonNullByDefault
 public class VariableValue {
-    private static final String SENSOR_DEFECTIVE_STATE = "DEFECTIVE";
-
     /** The absolute, native LCN value. */
     private final long nativeValue;
 
@@ -88,9 +86,9 @@ public class VariableValue {
     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()));
         }
index 6e42d974ad93d42b5ffac37a7bb2fa438e493463..7b4c034fd1070c685fb5b31dc67e6cbccd7bf422 100644 (file)
@@ -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;
     }
 
index fc1809bbcee9ca128ee48789ea36e64a4624b1bd..20c4f2f1c5ed3059837c50f69c8326a1a8b829a9 100644 (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.QuantityType;
 import org.openhab.core.types.State;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Base class for all LCN variable value converters.
@@ -32,7 +30,6 @@ import org.slf4j.LoggerFactory;
  */
 @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;
@@ -102,20 +99,20 @@ public class ValueConverter extends Converter {
      *
      * @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;
     }
 }
index 1c41b65782fcaba148906831072f9bdde3a40372..eedb77560c9d8032d8d38f8f2d979f467f2971a7 100644 (file)
@@ -121,7 +121,8 @@ public class LcnModuleRvarSetpointSubHandlerTest extends AbstractTestLcnModuleSu
     @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());
     }
index 59062efe75aab66803750b81c6f14b81ec6ac4ce..0bc9bdf5a5bcfac453a46cd4ad7dadf69750d56e 100644 (file)
@@ -82,14 +82,15 @@ public class LcnModuleVariableSubHandlerTest extends AbstractTestLcnModuleSubHan
     @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());
     }
 }