]> git.basschouten.com Git - openhab-addons.git/blob
c2487d92ade310a52dee5fc3a14e7d6a4cd41d06
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.neato.internal.discovery;
14
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
20
21 import org.openhab.binding.neato.internal.NeatoBindingConstants;
22 import org.openhab.binding.neato.internal.NeatoHandlerFactory;
23 import org.openhab.binding.neato.internal.classes.Robot;
24 import org.openhab.binding.neato.internal.handler.NeatoAccountHandler;
25 import org.openhab.core.config.discovery.AbstractDiscoveryService;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingUID;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * The {@link NeatoAccountDiscoveryService} is responsible for starting the discovery procedure
34  * that connects to Neato Web and imports all registered vacuum cleaners.
35  *
36  * @author Patrik Wimnell - Initial contribution
37  * @author Jeff Lauterbach - Start discovery service from bridge
38  */
39 public class NeatoAccountDiscoveryService extends AbstractDiscoveryService {
40
41     private final Logger logger = LoggerFactory.getLogger(NeatoAccountDiscoveryService.class);
42
43     private static final int TIMEOUT = 15;
44
45     private final NeatoAccountHandler handler;
46     private final ThingUID bridgeUID;
47
48     private ScheduledFuture<?> scanTask;
49
50     public NeatoAccountDiscoveryService(NeatoAccountHandler handler) {
51         super(NeatoHandlerFactory.DISCOVERABLE_THING_TYPE_UIDS, TIMEOUT);
52         this.handler = handler;
53         this.bridgeUID = handler.getThing().getUID();
54     }
55
56     private void findRobots() {
57         List<Robot> robots = handler.getRobotsFromNeato();
58
59         for (Robot robot : robots) {
60             addThing(robot);
61         }
62     }
63
64     @Override
65     protected void startBackgroundDiscovery() {
66         findRobots();
67     }
68
69     @Override
70     protected void startScan() {
71         if (this.scanTask != null) {
72             scanTask.cancel(true);
73         }
74         this.scanTask = scheduler.schedule(this::findRobots, 0, TimeUnit.SECONDS);
75     }
76
77     @Override
78     protected void stopScan() {
79         super.stopScan();
80
81         if (this.scanTask != null) {
82             this.scanTask.cancel(true);
83             this.scanTask = null;
84         }
85     }
86
87     private void addThing(Robot robot) {
88         if (robot == null || !robot.discoveryInformationPresent()) {
89             return;
90         }
91
92         logger.debug("addThing(): Adding new Neato unit ({}) to the inbox", robot.getName());
93
94         Map<String, Object> properties = new HashMap<>();
95         ThingUID thingUID = new ThingUID(NeatoBindingConstants.THING_TYPE_VACUUMCLEANER, bridgeUID, robot.getSerial());
96         properties.put(NeatoBindingConstants.CONFIG_SECRET, robot.getSecretKey());
97         properties.put(NeatoBindingConstants.CONFIG_SERIAL, robot.getSerial());
98         properties.put(Thing.PROPERTY_MODEL_ID, robot.getModel());
99         properties.put(NeatoBindingConstants.PROPERTY_NAME, robot.getName());
100
101         thingDiscovered(DiscoveryResultBuilder.create(thingUID).withLabel(robot.getName()).withBridge(bridgeUID)
102                 .withProperties(properties).build());
103     }
104 }