2 * Copyright (c) 2010-2023 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 final NeatoAccountHandler handler;
46 private final ThingUID bridgeUID;
48 private ScheduledFuture<?> scanTask;
50 public NeatoAccountDiscoveryService(NeatoAccountHandler handler) {
51 super(NeatoHandlerFactory.DISCOVERABLE_THING_TYPE_UIDS, TIMEOUT);
52 this.handler = handler;
53 this.bridgeUID = handler.getThing().getUID();
56 private void findRobots() {
57 List<Robot> robots = handler.getRobotsFromNeato();
59 for (Robot robot : robots) {
65 protected void startBackgroundDiscovery() {
70 protected void startScan() {
71 if (this.scanTask != null) {
72 scanTask.cancel(true);
74 this.scanTask = scheduler.schedule(this::findRobots, 0, TimeUnit.SECONDS);
78 protected void stopScan() {
81 if (this.scanTask != null) {
82 this.scanTask.cancel(true);
87 private void addThing(Robot robot) {
88 if (robot == null || !robot.discoveryInformationPresent()) {
92 logger.debug("addThing(): Adding new Neato unit ({}) to the inbox", robot.getName());
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());
101 thingDiscovered(DiscoveryResultBuilder.create(thingUID).withLabel(robot.getName()).withBridge(bridgeUID)
102 .withProperties(properties).build());