Created
April 21, 2017 06:14
-
-
Save jvolkman/9d9f0f2e13617035359ecba9ff2b0cd1 to your computer and use it in GitHub Desktop.
Zwave update polling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/main/java/org/openhab/binding/zwave/internal/converter/ZWaveMultiLevelSwitchConverter.java b/src/main/java/org/openhab/binding/zwave/internal/converter/ZWaveMultiLevelSwitchConverter.java | |
index 6a0feb8..9c76a9c 100644 | |
--- a/src/main/java/org/openhab/binding/zwave/internal/converter/ZWaveMultiLevelSwitchConverter.java | |
+++ b/src/main/java/org/openhab/binding/zwave/internal/converter/ZWaveMultiLevelSwitchConverter.java | |
@@ -9,7 +9,9 @@ | |
package org.openhab.binding.zwave.internal.converter; | |
import java.util.ArrayList; | |
+import java.util.HashMap; | |
import java.util.List; | |
+import java.util.Map; | |
import org.eclipse.smarthome.core.library.types.OnOffType; | |
import org.eclipse.smarthome.core.library.types.PercentType; | |
@@ -38,6 +40,8 @@ | |
public class ZWaveMultiLevelSwitchConverter extends ZWaveCommandClassConverter { | |
private final static Logger logger = LoggerFactory.getLogger(ZWaveMultiLevelSwitchConverter.class); | |
+ | |
+ private static final Map<Integer, UpdatedValuePoller> valuePollers = new HashMap<>(); | |
/** | |
* Constructor. Creates a new instance of the {@link ZWaveMultiLevelSwitchConverter} class. | |
@@ -101,6 +105,7 @@ | |
if (((PercentType) state).intValue() >= 99) { | |
state = new PercentType(100); | |
} | |
+ | |
break; | |
case OnOffType: | |
if (value == 0) { | |
@@ -122,6 +127,18 @@ | |
default: | |
logger.warn("No conversion in {} to {}", getClass().getSimpleName(), channel.getDataType()); | |
break; | |
+ } | |
+ | |
+ if (valuePollers.containsKey(event.getNodeId())) { | |
+ if (valuePollers.get(event.getNodeId()).pollForCompletion(state)) { | |
+ // Finished | |
+ logger.debug("Polling FINISHED for node {}", event.getNodeId()); | |
+ valuePollers.remove(event.getNodeId()); | |
+ } else { | |
+ // Still polling | |
+ logger.debug("Polling CONTINUING for node {}", event.getNodeId()); | |
+ return null; | |
+ } | |
} | |
return state; | |
@@ -211,6 +228,41 @@ | |
// Don't poll immediately since some devices return the original value, and some the new value. | |
// This conflicts with OH that will move the slider immediately. | |
messages.add(node.encapsulate(commandClass.getValueMessage(), commandClass, channel.getEndpoint())); | |
+ valuePollers.put(node.getNodeId(), new UpdatedValuePoller(node, commandClass, channel.getEndpoint())); | |
+ | |
return messages; | |
} | |
+ | |
+ private class UpdatedValuePoller { | |
+ private final ZWaveNode node; | |
+ private final ZWaveMultiLevelSwitchCommandClass commandClass; | |
+ private final int endpoint; | |
+ private State lastState; | |
+ | |
+ private UpdatedValuePoller(ZWaveNode node, ZWaveMultiLevelSwitchCommandClass commandClass, int endpoint) { | |
+ this.node = node; | |
+ this.commandClass = commandClass; | |
+ this.endpoint = endpoint; | |
+ this.lastState = null; | |
+ } | |
+ | |
+ boolean pollForCompletion(State newState) { | |
+ if (newState == null || newState.equals(lastState)) { | |
+ return true; | |
+ } | |
+ // Poll after a delay. | |
+ new Thread() { | |
+ @Override | |
+ public void run() { | |
+ try { | |
+ Thread.sleep(200); | |
+ } catch (InterruptedException e) { | |
+ } | |
+ controller.sendData(node.encapsulate(commandClass.getValueMessage(), commandClass, endpoint)); | |
+ } | |
+ }.start(); | |
+ lastState = newState; | |
+ return false; | |
+ } | |
+ } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment