Search in sources :

Example 1 with DiscoveryResult

use of org.eclipse.smarthome.config.discovery.DiscoveryResult in project smarthome by eclipse.

the class AutomaticInboxProcessor method thingAdded.

@Override
public void thingAdded(Inbox inbox, DiscoveryResult result) {
    if (autoIgnore) {
        String value = getRepresentationValue(result);
        if (value != null) {
            Thing thing = thingRegistry.stream().filter(t -> Objects.equals(value, getRepresentationPropertyValueForThing(t))).filter(t -> Objects.equals(t.getThingTypeUID(), result.getThingTypeUID())).findFirst().orElse(null);
            if (thing != null) {
                logger.debug("Auto-ignoring the inbox entry for the representation value {}", value);
                inbox.setFlag(result.getThingUID(), DiscoveryResultFlag.IGNORED);
            }
        }
    }
    if (autoApprove) {
        inbox.approve(result.getThingUID(), result.getLabel());
    }
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) InboxListener(org.eclipse.smarthome.config.discovery.inbox.InboxListener) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) RegistryChangeListener(org.eclipse.smarthome.core.common.registry.RegistryChangeListener) ThingStatusInfoChangedEvent(org.eclipse.smarthome.core.thing.events.ThingStatusInfoChangedEvent) Component(org.osgi.service.component.annotations.Component) Inbox(org.eclipse.smarthome.config.discovery.inbox.Inbox) Nullable(org.eclipse.jdt.annotation.Nullable) Map(java.util.Map) Thing(org.eclipse.smarthome.core.thing.Thing) Configuration(org.eclipse.smarthome.config.core.Configuration) InboxPredicates(org.eclipse.smarthome.config.discovery.inbox.InboxPredicates) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) Logger(org.slf4j.Logger) Collectors(java.util.stream.Collectors) ThingTypeRegistry(org.eclipse.smarthome.core.thing.type.ThingTypeRegistry) Objects(java.util.Objects) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) List(java.util.List) DiscoveryResultFlag(org.eclipse.smarthome.config.discovery.DiscoveryResultFlag) AbstractTypedEventSubscriber(org.eclipse.smarthome.core.events.AbstractTypedEventSubscriber) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) ThingType(org.eclipse.smarthome.core.thing.type.ThingType) Reference(org.osgi.service.component.annotations.Reference) DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) ThingStatus(org.eclipse.smarthome.core.thing.ThingStatus) Thing(org.eclipse.smarthome.core.thing.Thing)

Example 2 with DiscoveryResult

use of org.eclipse.smarthome.config.discovery.DiscoveryResult in project smarthome by eclipse.

the class PersistentInbox method add.

@Override
public synchronized boolean add(DiscoveryResult result) throws IllegalStateException {
    if (result != null) {
        ThingUID thingUID = result.getThingUID();
        Thing thing = this.thingRegistry.get(thingUID);
        if (thing == null) {
            DiscoveryResult inboxResult = get(thingUID);
            if (inboxResult == null) {
                discoveryResultStorage.put(result.getThingUID().toString(), result);
                notifyListeners(result, EventType.added);
                logger.info("Added new thing '{}' to inbox.", thingUID);
                return true;
            } else {
                if (inboxResult instanceof DiscoveryResultImpl) {
                    DiscoveryResultImpl resultImpl = (DiscoveryResultImpl) inboxResult;
                    resultImpl.synchronize(result);
                    discoveryResultStorage.put(result.getThingUID().toString(), resultImpl);
                    notifyListeners(resultImpl, EventType.updated);
                    logger.debug("Updated discovery result for '{}'.", thingUID);
                    return true;
                } else {
                    logger.warn("Cannot synchronize result with implementation class '{}'.", inboxResult.getClass().getName());
                }
            }
        } else {
            logger.debug("Discovery result with thing '{}' not added as inbox entry." + " It is already present as thing in the ThingRegistry.", thingUID);
            boolean updated = synchronizeConfiguration(result.getThingTypeUID(), result.getProperties(), thing.getConfiguration());
            if (updated) {
                logger.debug("The configuration for thing '{}' is updated...", thingUID);
                this.managedThingProvider.update(thing);
            }
        }
    }
    return false;
}
Also used : DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) InboxPredicates.forThingUID(org.eclipse.smarthome.config.discovery.inbox.InboxPredicates.forThingUID) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) Thing(org.eclipse.smarthome.core.thing.Thing)

Example 3 with DiscoveryResult

use of org.eclipse.smarthome.config.discovery.DiscoveryResult in project smarthome by eclipse.

the class PersistentInbox method remove.

@Override
public synchronized boolean remove(ThingUID thingUID) throws IllegalStateException {
    if (thingUID != null) {
        DiscoveryResult discoveryResult = get(thingUID);
        if (discoveryResult != null) {
            if (!isInRegistry(thingUID)) {
                removeResultsForBridge(thingUID);
            }
            resultDiscovererMap.remove(discoveryResult);
            this.discoveryResultStorage.remove(thingUID.toString());
            notifyListeners(discoveryResult, EventType.removed);
            return true;
        }
    }
    return false;
}
Also used : DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult)

Example 4 with DiscoveryResult

use of org.eclipse.smarthome.config.discovery.DiscoveryResult in project smarthome by eclipse.

the class PersistentInbox method removeOlderResults.

@Override
public Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp, Collection<ThingTypeUID> thingTypeUIDs, ThingUID bridgeUID) {
    HashSet<ThingUID> removedThings = new HashSet<>();
    for (DiscoveryResult discoveryResult : getAll()) {
        Class<?> discoverer = resultDiscovererMap.get(discoveryResult);
        if (thingTypeUIDs != null && thingTypeUIDs.contains(discoveryResult.getThingTypeUID()) && discoveryResult.getTimestamp() < timestamp && (discoverer == null || source.getClass() == discoverer)) {
            ThingUID thingUID = discoveryResult.getThingUID();
            if (bridgeUID == null || bridgeUID.equals(discoveryResult.getBridgeUID())) {
                removedThings.add(thingUID);
                remove(thingUID);
                logger.debug("Removed {} from inbox because it was older than {}", thingUID, new Date(timestamp));
            }
        }
    }
    return removedThings;
}
Also used : DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) InboxPredicates.forThingUID(org.eclipse.smarthome.config.discovery.inbox.InboxPredicates.forThingUID) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) Date(java.util.Date) HashSet(java.util.HashSet)

Example 5 with DiscoveryResult

use of org.eclipse.smarthome.config.discovery.DiscoveryResult in project smarthome by eclipse.

the class PersistentInbox method approve.

@Override
public Thing approve(ThingUID thingUID, String label) {
    if (thingUID == null) {
        throw new IllegalArgumentException("Thing UID must not be null");
    }
    List<DiscoveryResult> results = stream().filter(forThingUID(thingUID)).collect(Collectors.toList());
    if (results.isEmpty()) {
        throw new IllegalArgumentException("No Thing with UID " + thingUID.getAsString() + " in inbox");
    }
    DiscoveryResult result = results.get(0);
    final Map<String, String> properties = new HashMap<>();
    final Map<String, Object> configParams = new HashMap<>();
    getPropsAndConfigParams(result, properties, configParams);
    final Configuration config = new Configuration(configParams);
    ThingTypeUID thingTypeUID = result.getThingTypeUID();
    Thing newThing = ThingFactory.createThing(thingUID, config, properties, result.getBridgeUID(), thingTypeUID, this.thingHandlerFactories);
    if (newThing == null) {
        logger.warn("Cannot create thing. No binding found that supports creating a thing" + " of type {}.", thingTypeUID);
        return null;
    }
    if (label != null && !label.isEmpty()) {
        newThing.setLabel(label);
    } else {
        newThing.setLabel(result.getLabel());
    }
    addThingSafely(newThing);
    return newThing;
}
Also used : DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) Configuration(org.eclipse.smarthome.config.core.Configuration) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) Thing(org.eclipse.smarthome.core.thing.Thing)

Aggregations

DiscoveryResult (org.eclipse.smarthome.config.discovery.DiscoveryResult)42 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)29 HashMap (java.util.HashMap)16 Test (org.junit.Test)14 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)12 Collection (java.util.Collection)4 DiscoveryListener (org.eclipse.smarthome.config.discovery.DiscoveryListener)4 DiscoveryService (org.eclipse.smarthome.config.discovery.DiscoveryService)4 ThingStatusInfo (org.eclipse.smarthome.core.thing.ThingStatusInfo)4 DiscoveryResultFlag (org.eclipse.smarthome.config.discovery.DiscoveryResultFlag)3 InboxPredicates.forThingUID (org.eclipse.smarthome.config.discovery.inbox.InboxPredicates.forThingUID)3 Thing (org.eclipse.smarthome.core.thing.Thing)3 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)3 Date (java.util.Date)2 List (java.util.List)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Collectors (java.util.stream.Collectors)2 Nullable (org.eclipse.jdt.annotation.Nullable)2 Configuration (org.eclipse.smarthome.config.core.Configuration)2