Skip to content
Snippets Groups Projects
Commit 0302c67b authored by René Schöne's avatar René Schöne
Browse files

Add option to publish all item changes.

parent 08227a2f
No related branches found
No related tags found
No related merge requests found
......@@ -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>
......
......@@ -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");
......
......@@ -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;
......
......@@ -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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment