]> git.basschouten.com Git - openhab-addons.git/blob
571bfdd2a07bf2e37346f6c74ed164a5c366bfe6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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 NeatoAccountHandler handler;
46     private 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     }
54
55     private void findRobots() {
56         List<Robot> robots = handler.getRobotsFromNeato();
57
58         for (Robot robot : robots) {
59             addThing(robot);
60         }
61     }
62
63     @Override
64     protected void startBackgroundDiscovery() {
65         findRobots();
66     }
67
68     @Override
69     protected void startScan() {
70         if (this.scanTask != null) {
71             scanTask.cancel(true);
72         }
73         this.scanTask = scheduler.schedule(() -> findRobots(), 0, TimeUnit.SECONDS);
74     }
75
76     @Override
77     protected void stopScan() {
78         super.stopScan();
79
80         if (this.scanTask != null) {
81             this.scanTask.cancel(true);
82             this.scanTask = null;
83         }
84     }
85
86     private void addThing(Robot robot) {
87         if (robot == null || !robot.discoveryInformationPresent()) {
88             return;
89         }
90
91         logger.debug("addThing(): Adding new Neato unit {} to the inbox", robot.getName());
92
93         Map<String, Object> properties = new HashMap<>();
94         String serial = robot.getSerial();
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(
102                 DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID).withProperties(properties).build());
103     }
104 }