2 * Copyright (c) 2010-2020 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.neato.internal.discovery;
15 import java.util.HashMap;
16 import java.util.List;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
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;
33 * The {@link NeatoAccountDiscoveryService} is responsible for starting the discovery procedure
34 * that connects to Neato Web and imports all registered vacuum cleaners.
36 * @author Patrik Wimnell - Initial contribution
37 * @author Jeff Lauterbach - Start discovery service from bridge
39 public class NeatoAccountDiscoveryService extends AbstractDiscoveryService {
41 private final Logger logger = LoggerFactory.getLogger(NeatoAccountDiscoveryService.class);
43 private static final int TIMEOUT = 15;
45 private NeatoAccountHandler handler;
46 private ThingUID bridgeUID;
48 private ScheduledFuture<?> scanTask;
50 public NeatoAccountDiscoveryService(NeatoAccountHandler handler) {
51 super(NeatoHandlerFactory.DISCOVERABLE_THING_TYPE_UIDS, TIMEOUT);
52 this.handler = handler;
55 private void findRobots() {
56 List<Robot> robots = handler.getRobotsFromNeato();
58 for (Robot robot : robots) {
64 protected void startBackgroundDiscovery() {
69 protected void startScan() {
70 if (this.scanTask != null) {
71 scanTask.cancel(true);
73 this.scanTask = scheduler.schedule(() -> findRobots(), 0, TimeUnit.SECONDS);
77 protected void stopScan() {
80 if (this.scanTask != null) {
81 this.scanTask.cancel(true);
86 private void addThing(Robot robot) {
87 if (robot == null || !robot.discoveryInformationPresent()) {
91 logger.debug("addThing(): Adding new Neato unit {} to the smarthome inbox", robot.getName());
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());
102 DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID).withProperties(properties).build());