org.eclipse.smarthome.config.discovery.DiscoveryService Java Examples

The following examples show how to use org.eclipse.smarthome.config.discovery.DiscoveryService. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TradfriDiscoveryServiceTest.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
@Before
public void setUp() {
    initMocks(this);
    when(handler.getThing()).thenReturn(BridgeBuilder.create(GATEWAY_TYPE_UID, "1").build());
    discovery = new TradfriDiscoveryService(handler);

    listener = new DiscoveryListener() {
        @Override
        public void thingRemoved(DiscoveryService source, ThingUID thingUID) {
        }

        @Override
        public void thingDiscovered(DiscoveryService source, DiscoveryResult result) {
            discoveryResult = result;
        }

        @Override
        public Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp,
                Collection<ThingTypeUID> thingTypeUIDs, ThingUID bridgeUID) {
            return null;
        }
    };
    discovery.addDiscoveryListener(listener);
}
 
Example #2
Source File: DiscoveryServiceRegistryImpl.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
private boolean startScans(Set<DiscoveryService> discoveryServices, @Nullable ScanListener listener) {
    boolean atLeastOneDiscoveryServiceHasBeenStarted = false;

    if (discoveryServices.size() > 1) {
        logger.debug("Trying to start {} scans with an aggregating listener.", discoveryServices.size());
        AggregatingScanListener aggregatingScanListener = new AggregatingScanListener(discoveryServices.size(),
                listener);
        for (DiscoveryService discoveryService : discoveryServices) {
            if (startScan(discoveryService, aggregatingScanListener)) {
                atLeastOneDiscoveryServiceHasBeenStarted = true;
            } else {
                logger.debug(
                        "Reducing number of discovery services in aggregating listener, because discovery service failed to start scan.");
                aggregatingScanListener.reduceNumberOfDiscoveryServices();
            }
        }
    } else {
        if (startScan(discoveryServices.iterator().next(), listener)) {
            atLeastOneDiscoveryServiceHasBeenStarted = true;
        }

    }

    return atLeastOneDiscoveryServiceHasBeenStarted;
}
 
Example #3
Source File: DiscoveryServiceRegistryImpl.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
private boolean abortScans(Set<DiscoveryService> discoveryServices) {
    boolean allServicesAborted = true;

    for (DiscoveryService discoveryService : discoveryServices) {
        Collection<ThingTypeUID> supportedThingTypes = discoveryService.getSupportedThingTypes();
        try {
            logger.debug("Abort scan for thing types '{}' on '{}'...", supportedThingTypes,
                    discoveryService.getClass().getName());

            discoveryService.abortScan();

            logger.debug("Scan for thing types '{}' aborted on '{}'.", supportedThingTypes,
                    discoveryService.getClass().getName());
        } catch (Exception ex) {
            logger.error("Cannot abort scan for thing types '{}' on '{}'!", supportedThingTypes,
                    discoveryService.getClass().getName(), ex);
            allServicesAborted = false;
        }
    }

    return allServicesAborted;
}
 
Example #4
Source File: DiscoveryServiceRegistryImpl.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public @Nullable Collection<ThingUID> removeOlderResults(final DiscoveryService source, final long timestamp,
        final @Nullable Collection<ThingTypeUID> thingTypeUIDs, @Nullable ThingUID bridgeUID) {
    HashSet<ThingUID> removedResults = new HashSet<>();
    for (final DiscoveryListener listener : this.listeners) {
        try {
            Collection<ThingUID> olderResults = AccessController
                    .doPrivileged(new PrivilegedAction<@Nullable Collection<ThingUID>>() {
                        @Override
                        public @Nullable Collection<ThingUID> run() {
                            return listener.removeOlderResults(source, timestamp, thingTypeUIDs, bridgeUID);
                        }
                    });
            if (olderResults != null) {
                removedResults.addAll(olderResults);
            }
        } catch (Exception ex) {
            logger.error("Cannot notify the DiscoveryListener '{}' on all things removed event!",
                    listener.getClass().getName(), ex);
        }
    }

    return removedResults;
}
 
Example #5
Source File: DiscoveryServiceRegistryImpl.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public synchronized void thingRemoved(final DiscoveryService source, final ThingUID thingUID) {
    synchronized (cachedResults) {
        Iterator<DiscoveryResult> it = cachedResults.getOrDefault(source, Collections.emptySet()).iterator();
        while (it.hasNext()) {
            if (it.next().getThingUID().equals(thingUID)) {
                it.remove();
            }
        }
    }
    for (final DiscoveryListener listener : this.listeners) {
        try {
            AccessController.doPrivileged(new PrivilegedAction<@Nullable Void>() {
                @Override
                public @Nullable Void run() {
                    listener.thingRemoved(source, thingUID);
                    return null;
                }
            });
        } catch (Exception ex) {
            logger.error("Cannot notify the DiscoveryListener '{}' on Thing removed event!",
                    listener.getClass().getName(), ex);
        }
    }
}
 
Example #6
Source File: DiscoveryServiceRegistryImpl.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public synchronized void thingDiscovered(final DiscoveryService source, final DiscoveryResult result) {
    synchronized (cachedResults) {
        cachedResults.computeIfAbsent(source, unused -> new HashSet<>()).add(result);
    }
    for (final DiscoveryListener listener : this.listeners) {
        try {
            AccessController.doPrivileged(new PrivilegedAction<@Nullable Void>() {
                @Override
                public @Nullable Void run() {
                    listener.thingDiscovered(source, result);
                    return null;
                }
            });
        } catch (Exception ex) {
            logger.error("Cannot notify the DiscoveryListener {} on Thing discovered event!",
                    listener.getClass().getName(), ex);
        }
    }
}
 
Example #7
Source File: OpenWeatherMapHandlerFactory.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected @Nullable ThingHandler createHandler(Thing thing) {
    ThingTypeUID thingTypeUID = thing.getThingTypeUID();

    if (THING_TYPE_WEATHER_API.equals(thingTypeUID)) {
        OpenWeatherMapAPIHandler handler = new OpenWeatherMapAPIHandler((Bridge) thing, httpClient, localeProvider);
        // register discovery service
        OpenWeatherMapDiscoveryService discoveryService = new OpenWeatherMapDiscoveryService(handler,
                locationProvider, localeProvider, i18nProvider);
        discoveryServiceRegs.put(handler.getThing().getUID(), bundleContext
                .registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
        return handler;
    } else if (THING_TYPE_WEATHER_AND_FORECAST.equals(thingTypeUID)) {
        return new OpenWeatherMapWeatherAndForecastHandler(thing);
    } else if (THING_TYPE_UVINDEX.equals(thingTypeUID)) {
        return new OpenWeatherMapUVIndexHandler(thing);
    }

    return null;
}
 
Example #8
Source File: PersistentInbox.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public @Nullable Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp,
        @Nullable Collection<ThingTypeUID> thingTypeUIDs, @Nullable 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;
}
 
Example #9
Source File: HueLightDiscoveryServiceOSGiTest.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
@Before
public void setUp() {
    registerVolatileStorageService();

    thingRegistry = getService(ThingRegistry.class, ThingRegistry.class);
    assertThat(thingRegistry, is(notNullValue()));

    Configuration configuration = new Configuration();
    configuration.put(HOST, "1.2.3.4");
    configuration.put(USER_NAME, "testUserName");
    configuration.put(PROPERTY_SERIAL_NUMBER, "testSerialNumber");

    hueBridge = (Bridge) thingRegistry.createThingOfType(BRIDGE_THING_TYPE_UID, BRIDGE_THING_UID, null, "Bridge",
            configuration);

    assertThat(hueBridge, is(notNullValue()));
    thingRegistry.add(hueBridge);

    hueBridgeHandler = getThingHandler(hueBridge, HueBridgeHandler.class);
    assertThat(hueBridgeHandler, is(notNullValue()));

    discoveryService = getService(DiscoveryService.class, HueLightDiscoveryService.class);
    assertThat(discoveryService, is(notNullValue()));
}
 
Example #10
Source File: DiscoveryConsoleCommandExtension.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
private void configureBackgroundDiscovery(Console console, String discoveryServicePID, boolean enabled) {
    try {
        Configuration configuration = configurationAdmin.getConfiguration(discoveryServicePID);
        Dictionary<String, Object> properties = configuration.getProperties();
        if (properties == null) {
            properties = new Hashtable<>();
        }
        properties.put(DiscoveryService.CONFIG_PROPERTY_BACKGROUND_DISCOVERY, enabled);
        configuration.update(properties);
        console.println("Background discovery for discovery service '" + discoveryServicePID + "' was set to "
                + enabled + ".");
    } catch (IOException ex) {
        String errorText = "Error occurred while trying to configure background discovery with PID '"
                + discoveryServicePID + "': " + ex.getMessage();
        logger.error(errorText, ex);
        console.println(errorText);
    }
}
 
Example #11
Source File: DiscoveryServiceManager.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Registers all {@link SceneDiscoveryService}s and {@link DeviceDiscoveryService}s of this
 * {@link DiscoveryServiceManager} to the given {@link BundleContext}.
 *
 * @param bundleContext (must not be null)
 */
public void registerDiscoveryServices(BundleContext bundleContext) {
    if (discoveryServices != null) {
        for (AbstractDiscoveryService service : discoveryServices.values()) {
            if (service instanceof SceneDiscoveryService) {
                this.discoveryServiceRegs.put(bridgeUID + ((SceneDiscoveryService) service).getID(),
                        bundleContext.registerService(DiscoveryService.class.getName(), service,
                                new Hashtable<String, Object>()));
            }
            if (service instanceof DeviceDiscoveryService) {
                this.discoveryServiceRegs.put(bridgeUID + ((DeviceDiscoveryService) service).getID(),
                        bundleContext.registerService(DiscoveryService.class.getName(), service,
                                new Hashtable<String, Object>()));
            }
            if (service instanceof ZoneTemperatureControlDiscoveryService) {
                this.discoveryServiceRegs.put(
                        bridgeUID + ((ZoneTemperatureControlDiscoveryService) service).getID(),
                        bundleContext.registerService(DiscoveryService.class.getName(), service,
                                new Hashtable<String, Object>()));
            }
        }
    }
}
 
Example #12
Source File: HueBridgeNupnpDiscoveryOSGITest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    registerService(volatileStorageService);

    sut = getService(DiscoveryService.class, HueBridgeNupnpDiscovery.class);
    assertThat(sut, is(notNullValue()));

    inbox = getService(Inbox.class);
    assertThat(inbox, is(notNullValue()));

    unregisterCurrentDiscoveryListener();
}
 
Example #13
Source File: WeatherUndergroundHandlerFactory.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
private synchronized void registerDiscoveryService(ThingUID bridgeUID) {
    WeatherUndergroundDiscoveryService discoveryService = new WeatherUndergroundDiscoveryService(bridgeUID,
            localeProvider, locationProvider);
    discoveryService.activate(null);
    discoveryServiceRegs.put(bridgeUID, bundleContext.registerService(DiscoveryService.class.getName(),
            discoveryService, new Hashtable<String, Object>()));
}
 
Example #14
Source File: DiscoveryServiceRegistryImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
private int getMaxScanTimeout(Set<DiscoveryService> discoveryServices) {
    int result = 0;

    for (DiscoveryService discoveryService : discoveryServices) {
        if (discoveryService.getScanTimeout() > result) {
            result = discoveryService.getScanTimeout();
        }
    }

    return result;
}
 
Example #15
Source File: DiscoveryServiceRegistryImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
private void removeDiscoveryServiceActivated(DiscoveryService discoveryService) {
    this.discoveryServices.remove(discoveryService);
    discoveryService.removeDiscoveryListener(this);
    synchronized (cachedResults) {
        this.cachedResults.remove(discoveryService);
    }
}
 
Example #16
Source File: DiscoveryServiceRegistryImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
private void addDiscoveryServiceActivated(final DiscoveryService discoveryService) {
    discoveryService.addDiscoveryListener(this);
    if (discoveryService instanceof ExtendedDiscoveryService) {
        safeCaller.create((ExtendedDiscoveryService) discoveryService, ExtendedDiscoveryService.class).build()
                .setDiscoveryServiceCallback(new DiscoveryServiceCallback() {
                });
    }
    this.discoveryServices.add(discoveryService);
}
 
Example #17
Source File: DiscoveryServiceRegistryImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
protected void addDiscoveryService(final DiscoveryService discoveryService) {
    discoveryServicesAll.add(discoveryService);
    if (active.get()) {
        addDiscoveryServiceActivated(discoveryService);
    }
}
 
Example #18
Source File: DiscoveryServiceRegistryImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
private synchronized Set<DiscoveryService> getDiscoveryServices(String bindingId) throws IllegalStateException {
    Set<DiscoveryService> discoveryServices = new HashSet<>();

    for (DiscoveryService discoveryService : this.discoveryServices) {
        Collection<ThingTypeUID> discoveryThingTypes = discoveryService.getSupportedThingTypes();
        for (ThingTypeUID thingTypeUID : discoveryThingTypes) {
            if (thingTypeUID.getBindingId().equals(bindingId)) {
                discoveryServices.add(discoveryService);
            }
        }
    }

    return discoveryServices;
}
 
Example #19
Source File: DiscoveryServiceRegistryImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
private synchronized Set<DiscoveryService> getDiscoveryServices(ThingTypeUID thingTypeUID)
        throws IllegalStateException {
    Set<DiscoveryService> discoveryServices = new HashSet<>();

    for (DiscoveryService discoveryService : this.discoveryServices) {
        Collection<ThingTypeUID> discoveryThingTypes = discoveryService.getSupportedThingTypes();
        if (discoveryThingTypes.contains(thingTypeUID)) {
            discoveryServices.add(discoveryService);
        }
    }

    return discoveryServices;
}
 
Example #20
Source File: HueLightDiscoveryServiceOSGiTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@After
public void cleanUp() {
    thingRegistry.remove(BRIDGE_THING_UID);
    waitForAssert(() -> {
        assertNull(getService(DiscoveryService.class, HueLightDiscoveryService.class));
    });
}
 
Example #21
Source File: HomematicBridgeHandler.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Registers the DeviceDiscoveryService.
 */
private void registerDeviceDiscoveryService() {
    if (bundleContext != null) {
        logger.trace("Registering HomematicDeviceDiscoveryService for bridge '{}'", getThing().getUID().getId());
        discoveryService = new HomematicDeviceDiscoveryService(this);
        discoveryServiceRegistration = bundleContext.registerService(DiscoveryService.class.getName(),
                discoveryService, new Hashtable<String, Object>());
        discoveryService.activate();
    }
}
 
Example #22
Source File: WemoHandlerFactory.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
private synchronized void registerDeviceDiscoveryService(WemoBridgeHandler wemoBridgeHandler,
        WemoHttpCall wemoHttpCaller) {
    WemoLinkDiscoveryService discoveryService = new WemoLinkDiscoveryService(wemoBridgeHandler, upnpIOService,
            wemoHttpCaller);
    this.discoveryServiceRegs.put(wemoBridgeHandler.getThing().getUID(), bundleContext
            .registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<String, Object>()));
}
 
Example #23
Source File: DiscoveryServiceRegistryImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public List<String> getSupportedBindings() {
    List<String> bindings = new ArrayList<>();
    for (DiscoveryService discoveryService : this.discoveryServices) {
        Collection<ThingTypeUID> supportedThingTypes = discoveryService.getSupportedThingTypes();
        for (ThingTypeUID thingTypeUID : supportedThingTypes) {
            bindings.add(thingTypeUID.getBindingId());
        }
    }
    return bindings;
}
 
Example #24
Source File: HeosBridgeHandler.java    From org.openhab.binding.heos with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * If a thing is discovered, the method checks if the thing is already known
 * and the state is only temporary set to OFFLINE. If so initialize() of the
 * thing is called.
 */

@Override
public void thingDiscovered(DiscoveryService source, DiscoveryResult result) {
    if (handlerList.containsKey(result.getThingUID())) {
        if (thingOnlineState.containsKey(result.getThingUID())) {
            if (thingOnlineState.get(result.getThingUID()).equals(ThingStatus.OFFLINE)) {
                handlerList.get(result.getThingUID()).initialize();
            }
        }
    }
}
 
Example #25
Source File: HeosBridgeHandler.java    From org.openhab.binding.heos with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * If a thing is discovered, the method checks if the thing is already known
 * and the state is only temporary set to OFFLINE. If so initialize() of the
 * thing is called.
 */

@Override
public void thingDiscovered(DiscoveryService source, DiscoveryResult result) {
    if (handlerList.containsKey(result.getThingUID())) {
        if (thingOnlineState.containsKey(result.getThingUID())) {
            if (thingOnlineState.get(result.getThingUID()).equals(ThingStatus.OFFLINE)) {
                handlerList.get(result.getThingUID()).initialize();
            }
        }
    }
}
 
Example #26
Source File: DiscoveryServiceRegistryImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Activate
protected void activate() {
    active.set(true);
    for (final DiscoveryService discoveryService : discoveryServicesAll) {
        addDiscoveryServiceActivated(discoveryService);
    }
}
 
Example #27
Source File: DiscoveryServiceRegistryImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Deactivate
protected void deactivate() {
    active.set(false);
    for (final DiscoveryService discoveryService : discoveryServicesAll) {
        removeDiscoveryServiceActivated(discoveryService);
    }
    this.listeners.clear();
    this.cachedResults.clear();
}
 
Example #28
Source File: DiscoveryServiceRegistryImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean abortScan(ThingTypeUID thingTypeUID) throws IllegalStateException {
    Set<DiscoveryService> discoveryServicesForThingType = getDiscoveryServices(thingTypeUID);

    if (discoveryServicesForThingType.isEmpty()) {
        logger.warn("No discovery service for thing type '{}' found!", thingTypeUID);
        return false;
    }

    return abortScans(discoveryServicesForThingType);
}
 
Example #29
Source File: DiscoveryServiceRegistryImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean abortScan(String bindingId) throws IllegalStateException {
    Set<DiscoveryService> discoveryServicesForBinding = getDiscoveryServices(bindingId);

    if (discoveryServicesForBinding.isEmpty()) {
        logger.warn("No discovery service for binding '{}' found!", bindingId);
        return false;
    }

    return abortScans(discoveryServicesForBinding);
}
 
Example #30
Source File: DiscoveryServiceRegistryImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean startScan(ThingTypeUID thingTypeUID, @Nullable ScanListener listener) throws IllegalStateException {
    Set<DiscoveryService> discoveryServicesForThingType = getDiscoveryServices(thingTypeUID);

    if (discoveryServicesForThingType.isEmpty()) {
        logger.warn("No discovery service for thing type '{}' found!", thingTypeUID);
        return false;
    }

    return startScans(discoveryServicesForThingType, listener);
}