From 0302c67b3c0b0bd7937b44e5249c2692927f3fa8 Mon Sep 17 00:00:00 2001 From: rschoene <rene.schoene@tu-dresden.de> Date: Fri, 12 Apr 2019 16:11:46 +0200 Subject: [PATCH] Add option to publish all item changes. --- ESH-INF/thing/thing-types.xml | 9 ++- .../binding/openlicht/BindingConstants.java | 1 + .../handler/AbstractMqttHandler.java | 3 + .../openlicht/handler/EraserHandler.java | 59 +++++++++++++------ 4 files changed, 52 insertions(+), 20 deletions(-) diff --git a/ESH-INF/thing/thing-types.xml b/ESH-INF/thing/thing-types.xml index a2f7099..3b9bf05 100644 --- a/ESH-INF/thing/thing-types.xml +++ b/ESH-INF/thing/thing-types.xml @@ -31,9 +31,14 @@ <description>MQTT topic prefix for messages leaving openHAB to eraser.</description> <default>oh/out/</default> </parameter> - <parameter name="publish-group" type="text" required="true"> + <parameter name="publish-all" type="boolean" required="true"> + <label>Publish All</label> + <description>If enabled, changes of every item will be published. Otherwise only those of the members of group set in the parameter "Publish Group"</description> + <default>true</default> + </parameter> + <parameter name="publish-group" type="text" required="false"> <label>Publish group</label> - <description>Group whose member will trigger an update</description> + <description>Group whose members will trigger an update</description> <context>item</context> </parameter> </config-description> diff --git a/src/main/java/org/openhab/binding/openlicht/BindingConstants.java b/src/main/java/org/openhab/binding/openlicht/BindingConstants.java index aa9fb76..6e66ee7 100644 --- a/src/main/java/org/openhab/binding/openlicht/BindingConstants.java +++ b/src/main/java/org/openhab/binding/openlicht/BindingConstants.java @@ -33,6 +33,7 @@ public class BindingConstants { public static final String CONFIG_TIMEOUT_MQTT_UNSUPPORTED_CATEGORIES = "unsupported-category-reset"; public static final String CONFIG_ERASER_OUT_TOPIC = "outTopic"; public static final String CONFIG_ERASER_PUBLISH_GROUP = "publish-group"; + public static final String CONFIG_ERASER_PUBLISH_ALL = "publish-all"; // List of all Thing Type UIDs public static final ThingTypeUID THING_TYPE_SKYWRITER_HAT = new ThingTypeUID(BINDING_ID, "skywriter-hat"); diff --git a/src/main/java/org/openhab/binding/openlicht/handler/AbstractMqttHandler.java b/src/main/java/org/openhab/binding/openlicht/handler/AbstractMqttHandler.java index 4f2f186..7fcd087 100644 --- a/src/main/java/org/openhab/binding/openlicht/handler/AbstractMqttHandler.java +++ b/src/main/java/org/openhab/binding/openlicht/handler/AbstractMqttHandler.java @@ -103,6 +103,9 @@ public abstract class AbstractMqttHandler extends BaseThingHandler logger.warn("Broker name of {} not set", me()); return false; } + if (thing == null) { + return false; + } if (!"mqtt:systemBroker".equals(thing.getThingTypeUID().getAsString()) && !"mqtt:broker".equals(thing.getThingTypeUID().getAsString())) { return false; 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 704471f..04899e4 100644 --- a/src/main/java/org/openhab/binding/openlicht/handler/EraserHandler.java +++ b/src/main/java/org/openhab/binding/openlicht/handler/EraserHandler.java @@ -33,6 +33,7 @@ public class EraserHandler extends AbstractMqttHandler implements StateChangeLis private final @NonNull Logger logger = LoggerFactory.getLogger(EraserHandler.class); private @Nullable ItemRegistry itemRegistry; private String outTopic; + private boolean publishAll = false; private String publishGroupName; private boolean publishGroupFound = false; private Set<GenericItem> observedItems = new HashSet<>(); @@ -49,6 +50,8 @@ public class EraserHandler extends AbstractMqttHandler implements StateChangeLis delegateListener = new WeakReference<DelegateEraserRegistryChangeListener>( new DelegateEraserRegistryChangeListener(this)); registry.addRegistryChangeListener(delegateListener.get()); + } else { + logger.warn("No item registry set, so no updates for item changes"); } } @@ -56,19 +59,28 @@ public class EraserHandler extends AbstractMqttHandler implements StateChangeLis protected void moreInitializeBefore() { this.outTopic = getConfigValueAsString(BindingConstants.CONFIG_ERASER_OUT_TOPIC); this.publishGroupName = getConfigValueAsString(BindingConstants.CONFIG_ERASER_PUBLISH_GROUP); + this.publishAll = getConfigValueAsBoolean(BindingConstants.CONFIG_ERASER_PUBLISH_ALL); if (this.outTopic.charAt(this.outTopic.length() - 1) != '/') { this.outTopic += "/"; } - if (this.itemRegistry != null) { - try { - Item publishGroupItem = this.itemRegistry.getItem(this.publishGroupName); - myGroupFound(publishGroupItem); - } catch (ItemNotFoundException e) { - this.publishGroupFound = false; - logger.warn("Could not find group with name '" + this.publishGroupName + "'"); + if (this.publishAll) { + if (this.itemRegistry != null) { + for (Item item : this.itemRegistry.getAll()) { + updated(item, item); + } } } else { - logger.warn("No item registry set, so no updates for item changes"); + if (this.itemRegistry != null) { + try { + Item publishGroupItem = this.itemRegistry.getItem(this.publishGroupName); + myGroupFound(publishGroupItem); + } catch (ItemNotFoundException e) { + this.publishGroupFound = false; + logger.warn("Could not find group with name '" + this.publishGroupName + "'"); + } + } else { + logger.warn("No item registry set, so could not search for publish group"); + } } } @@ -89,6 +101,10 @@ public class EraserHandler extends AbstractMqttHandler implements StateChangeLis this.lastStatusUpdatePhase = phase; this.lastStatusUpdateSuccess = success; this.lastStatusUpdateDescription = description; + if (this.publishAll) { + super.statusUpdate(phase, success, description); + return; + } String newDescription = description; if (!this.publishGroupFound) { // append information to description @@ -149,7 +165,6 @@ public class EraserHandler extends AbstractMqttHandler implements StateChangeLis } else { logger.warn("Could not set state for {} using '{}'", genericItem, new String(payload)); } - genericItem.setState(new StringType(new String(payload))); } else { logger.debug("No item registry to process message"); } @@ -199,23 +214,31 @@ public class EraserHandler extends AbstractMqttHandler implements StateChangeLis public void added(Item element) { GenericItem genericItem = (GenericItem) element; - if (!this.publishGroupFound && genericItem.getName().equals(this.publishGroupName)) { - myGroupFound(genericItem); - // update status with last known value (to remove the clause about missing group item) - statusUpdate(lastStatusUpdatePhase, lastStatusUpdateSuccess, lastStatusUpdateDescription); - } - List<@NonNull String> groupNames = genericItem.getGroupNames(); - if (groupNames.contains(this.publishGroupName)) { + if (this.publishAll) { if (observedItems.add(genericItem)) { genericItem.addStateChangeListener(this); } + } else { + if (!this.publishGroupFound && genericItem.getName().equals(this.publishGroupName)) { + myGroupFound(genericItem); + // update status with last known value (to remove the clause about missing group item) + statusUpdate(lastStatusUpdatePhase, lastStatusUpdateSuccess, lastStatusUpdateDescription); + } + List<@NonNull String> groupNames = genericItem.getGroupNames(); + if (groupNames.contains(this.publishGroupName)) { + if (observedItems.add(genericItem)) { + genericItem.addStateChangeListener(this); + } + } } } public void removed(Item element) { GenericItem genericItem = (GenericItem) element; - genericItem.removeStateChangeListener(this); - if (this.publishGroupFound && genericItem.getName().equals(this.publishGroupName)) { + if (observedItems.remove(genericItem)) { + genericItem.removeStateChangeListener(this); + } + if (!this.publishAll && this.publishGroupFound && genericItem.getName().equals(this.publishGroupName)) { // someone evil deleted the publish group this.publishGroupFound = false; GroupItem groupItem = (GroupItem) genericItem; -- GitLab