2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.foobot.internal.discovery;
15 import java.util.HashMap;
16 import java.util.List;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.foobot.internal.FoobotApiException;
22 import org.openhab.binding.foobot.internal.FoobotBindingConstants;
23 import org.openhab.binding.foobot.internal.FoobotHandlerFactory;
24 import org.openhab.binding.foobot.internal.handler.FoobotAccountHandler;
25 import org.openhab.binding.foobot.internal.handler.FoobotDeviceHandler;
26 import org.openhab.binding.foobot.internal.json.FoobotDevice;
27 import org.openhab.core.config.discovery.AbstractDiscoveryService;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.config.discovery.DiscoveryService;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerService;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link FoobotAccountDiscoveryService} is responsible for starting the discovery procedure
39 * that retrieves Foobot account and imports all registered Foobot devices.
41 * @author George Katsis - Initial contribution
42 * @author Hilbrand Bouwkamp - Completed implementation
45 public class FoobotAccountDiscoveryService extends AbstractDiscoveryService
46 implements DiscoveryService, ThingHandlerService {
48 private static final int TIMEOUT_SECONDS = 5;
50 private final Logger logger = LoggerFactory.getLogger(FoobotAccountDiscoveryService.class);
52 private @Nullable FoobotAccountHandler handler;
53 private @NonNullByDefault({}) ThingUID bridgeUID;
55 public FoobotAccountDiscoveryService() {
56 super(FoobotHandlerFactory.DISCOVERABLE_THING_TYPE_UIDS, TIMEOUT_SECONDS, false);
60 protected void startScan() {
61 scheduler.execute(this::retrieveFoobots);
64 private void retrieveFoobots() {
65 if (handler == null) {
69 final List<FoobotDeviceHandler> footbotHandlers = handler.getFootbotHandlers();
71 handler.getDeviceList().stream()
72 .filter(d -> !footbotHandlers.stream().anyMatch(h -> h.getUuid().equals(d.getUuid())))
73 .forEach(this::addThing);
74 } catch (final FoobotApiException e) {
75 logger.debug("Footbot Api connection failed: {}", e.getMessage(), e);
76 logger.warn("Discovering new footbot devices failed: {}", e.getMessage());
81 public void deactivate() {
85 private void addThing(final FoobotDevice foobot) {
86 logger.debug("Adding new Foobot '{}' with uuid: {}", foobot.getName(), foobot.getUuid());
88 final ThingUID thingUID = new ThingUID(FoobotBindingConstants.THING_TYPE_FOOBOT, bridgeUID, foobot.getUuid());
89 final Map<String, Object> properties = new HashMap<>();
90 properties.put(Thing.PROPERTY_SERIAL_NUMBER, foobot.getUuid());
91 properties.put(FoobotBindingConstants.CONFIG_UUID, foobot.getUuid());
92 properties.put(Thing.PROPERTY_MAC_ADDRESS, foobot.getMac());
93 properties.put(FoobotBindingConstants.PROPERTY_NAME, foobot.getName());
95 thingDiscovered(DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID).withProperties(properties)
96 .withLabel(foobot.getName()).withRepresentationProperty(foobot.getUuid()).build());
100 public void setThingHandler(@Nullable final ThingHandler handler) {
101 if (handler instanceof FoobotAccountHandler accountHandler) {
102 this.handler = accountHandler;
107 public @Nullable ThingHandler getThingHandler() {