]> git.basschouten.com Git - openhab-addons.git/blob
07c4585fa7f10425eb22746d7387697ae6d7cca5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.foobot.internal.discovery;
14
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18
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;
36
37 /**
38  * The {@link FoobotAccountDiscoveryService} is responsible for starting the discovery procedure
39  * that retrieves Foobot account and imports all registered Foobot devices.
40  *
41  * @author George Katsis - Initial contribution
42  * @author Hilbrand Bouwkamp - Completed implementation
43  */
44 @NonNullByDefault
45 public class FoobotAccountDiscoveryService extends AbstractDiscoveryService
46         implements DiscoveryService, ThingHandlerService {
47
48     private static final int TIMEOUT_SECONDS = 5;
49
50     private final Logger logger = LoggerFactory.getLogger(FoobotAccountDiscoveryService.class);
51
52     private @Nullable FoobotAccountHandler handler;
53     private @NonNullByDefault({}) ThingUID bridgeUID;
54
55     public FoobotAccountDiscoveryService() {
56         super(FoobotHandlerFactory.DISCOVERABLE_THING_TYPE_UIDS, TIMEOUT_SECONDS, false);
57     }
58
59     @Override
60     protected void startScan() {
61         scheduler.execute(this::retrieveFoobots);
62     }
63
64     private void retrieveFoobots() {
65         if (handler == null) {
66             return;
67         }
68         try {
69             final List<FoobotDeviceHandler> footbotHandlers = handler.getFootbotHandlers();
70
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());
77         }
78     }
79
80     @Override
81     public void deactivate() {
82         super.deactivate();
83     }
84
85     private void addThing(final FoobotDevice foobot) {
86         logger.debug("Adding new Foobot '{}' with uuid: {}", foobot.getName(), foobot.getUuid());
87
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());
94
95         thingDiscovered(DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID).withProperties(properties)
96                 .withLabel(foobot.getName()).withRepresentationProperty(foobot.getUuid()).build());
97     }
98
99     @Override
100     public void setThingHandler(@Nullable final ThingHandler handler) {
101         if (handler instanceof FoobotAccountHandler) {
102             this.handler = (FoobotAccountHandler) handler;
103         }
104     }
105
106     @Override
107     public @Nullable ThingHandler getThingHandler() {
108         return handler;
109     }
110 }