From 0906fa7669ac86682dbd3c8045820c41cbf9bc26 Mon Sep 17 00:00:00 2001
From: rschoene <rene.schoene@tu-dresden.de>
Date: Thu, 25 Jul 2019 13:33:20 +0200
Subject: [PATCH] EraserHandler: Also try to set state directly if new mqtt
 message is received (and not only try to post a new command).

- This enables to correctly handle ContactItems
---
 .../openlicht/handler/EraserHandler.java      | 20 ++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/src/main/java/org/openhab/binding/openlicht/handler/EraserHandler.java b/src/main/java/org/openhab/binding/openlicht/handler/EraserHandler.java
index b9478a6..0842930 100644
--- a/src/main/java/org/openhab/binding/openlicht/handler/EraserHandler.java
+++ b/src/main/java/org/openhab/binding/openlicht/handler/EraserHandler.java
@@ -149,10 +149,11 @@ public class EraserHandler extends AbstractMqttHandler implements StateChangeLis
     @Override
     public void processMessage(String topic, byte[] payload) {
         // check topic, and forward payload to respective item
-        logger.debug("Got message, in topc: '" + topic + "'");
+        final String stringPayload = new String(payload);
+        logger.debug("{} got message, in topc: '{}' with payload '{}'", this, topic, stringPayload);
         int indexOfLastSlash = topic.lastIndexOf('/');
         String itemName = topic.substring(indexOfLastSlash + 1);
-        logger.debug("Using item name: '" + itemName + "'");
+        logger.debug("{} using item name: '{}'", this, itemName);
         ItemRegistry registry = this.itemRegistry;
         if (registry != null) {
             Item item;
@@ -165,13 +166,22 @@ public class EraserHandler extends AbstractMqttHandler implements StateChangeLis
             // assume this is a GenericItem
             GenericItem genericItem = (GenericItem) item;
             // State newState = parseState(genericItem, new String(payload));
-            Command command = TypeParser.parseCommand(genericItem.getAcceptedCommandTypes(), new String(payload));
+            Command command = TypeParser.parseCommand(genericItem.getAcceptedCommandTypes(), stringPayload);
             EventPublisher localEventPublisher = this.eventPublisher;
             if (command != null && localEventPublisher != null) {
                 localEventPublisher.post(ItemEventFactory.createCommandEvent(itemName, command));
             } else {
-                logger.debug("Could not create command for event-publisher {}, item {} and payload {}",
-                        localEventPublisher, itemName, new String(payload));
+                logger.debug(
+                        "Could not create command for event-publisher {}, item {} with accecpted command types {} and payload {}",
+                        localEventPublisher, itemName, genericItem.getAcceptedCommandTypes(), stringPayload);
+                logger.debug("Trying to set state instead");
+                State state = TypeParser.parseState(genericItem.getAcceptedDataTypes(), stringPayload);
+                if (state != null) {
+                    genericItem.setState(state);
+                } else {
+                    logger.debug("Could not create new state for item {} with accepted date types {} and payload {}",
+                            itemName, genericItem.getAcceptedDataTypes(), stringPayload);
+                }
             }
             // if (newState != null) {
             // logger.debug("Setting state {} of type {} for {}", newState, newState.getClass().getSimpleName(),
-- 
GitLab