Skip to content

Commit

Permalink
Fixed activation of inbox (openhab#1051)
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Weitkamp <[email protected]>
GitOrigin-RevId: 978be4c
  • Loading branch information
cweitkamp authored and splatch committed Jul 11, 2023
1 parent 698153c commit ffb466c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.core.ConfigurableService;
import org.eclipse.smarthome.config.core.Configuration;
import org.eclipse.smarthome.config.discovery.DiscoveryResult;
import org.eclipse.smarthome.config.discovery.DiscoveryResultFlag;
Expand All @@ -39,9 +40,11 @@
import org.eclipse.smarthome.core.thing.events.ThingStatusInfoChangedEvent;
import org.eclipse.smarthome.core.thing.type.ThingType;
import org.eclipse.smarthome.core.thing.type.ThingTypeRegistry;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
Expand Down Expand Up @@ -83,16 +86,18 @@
* @author Henning Sudbrock - added hook for selectively auto-approving inbox entries
*/
@Component(immediate = true, configurationPid = "org.eclipse.smarthome.inbox", service = EventSubscriber.class, property = {
"service.config.description.uri=system:inbox", "service.config.label=Inbox", "service.config.category=system",
"service.pid=org.eclipse.smarthome.inbox" })
Constants.SERVICE_PID + "=org.eclipse.smarthome.inbox",
ConfigurableService.SERVICE_PROPERTY_CATEGORY + "=system",
ConfigurableService.SERVICE_PROPERTY_LABEL + "=Inbox",
ConfigurableService.SERVICE_PROPERTY_DESCRIPTION_URI + "=system:inbox" })
@NonNullByDefault
public class AutomaticInboxProcessor extends AbstractTypedEventSubscriber<ThingStatusInfoChangedEvent>
implements InboxListener, RegistryChangeListener<Thing> {

public static final String AUTO_IGNORE_CONFIG_PROPERTY = "autoIgnore";
public static final String ALWAYS_AUTO_APPROVE_CONFIG_PROPERTY = "autoApprove";

private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final Logger logger = LoggerFactory.getLogger(AutomaticInboxProcessor.class);

private final ThingRegistry thingRegistry;
private final ThingTypeRegistry thingTypeRegistry;
Expand All @@ -112,9 +117,22 @@ public AutomaticInboxProcessor(final @Reference ThingTypeRegistry thingTypeRegis
}

@Activate
protected void activate() {
protected void activate(@Nullable Map<String, @Nullable Object> properties) {
this.thingRegistry.addRegistryChangeListener(this);
this.inbox.addInboxListener(this);

modified(properties);
}

@Modified
protected void modified(@Nullable Map<String, @Nullable Object> properties) {
if (properties != null) {
Object value = properties.get(AUTO_IGNORE_CONFIG_PROPERTY);
autoIgnore = value == null || !value.toString().equals("false");
value = properties.get(ALWAYS_AUTO_APPROVE_CONFIG_PROPERTY);
alwaysAutoApprove = value != null && value.toString().equals("true");
autoApproveInboxEntries();
}
}

@Deactivate
Expand Down Expand Up @@ -256,16 +274,6 @@ private boolean isToBeAutoApproved(DiscoveryResult result) {
return inboxAutoApprovePredicates.stream().anyMatch(predicate -> predicate.test(result));
}

protected void activate(@Nullable Map<String, @Nullable Object> properties) {
if (properties != null) {
Object value = properties.get(AUTO_IGNORE_CONFIG_PROPERTY);
autoIgnore = value == null || !value.toString().equals("false");
value = properties.get(ALWAYS_AUTO_APPROVE_CONFIG_PROPERTY);
alwaysAutoApprove = value != null && value.toString().equals("true");
autoApproveInboxEntries();
}
}

@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
protected void addInboxAutoApprovePredicate(InboxAutoApprovePredicate inboxAutoApprovePredicate) {
inboxAutoApprovePredicates.add(inboxAutoApprovePredicate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void setUp() throws Exception {
inbox.activate();

automaticInboxProcessor = new AutomaticInboxProcessor(thingTypeRegistry, thingRegistry, inbox);
automaticInboxProcessor.activate();
automaticInboxProcessor.activate(null);
}

@After
Expand Down

0 comments on commit ffb466c

Please sign in to comment.