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.boschindego.internal.discovery;
15 import static org.openhab.binding.boschindego.internal.BoschIndegoBindingConstants.*;
17 import java.time.Instant;
18 import java.util.Collection;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.boschindego.internal.exceptions.IndegoException;
24 import org.openhab.binding.boschindego.internal.handler.BoschAccountHandler;
25 import org.openhab.core.config.discovery.AbstractDiscoveryService;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerService;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * The {@link IndegoDiscoveryService} is responsible for discovering Indego mowers.
39 * @author Jacob Laursen - Initial contribution
42 public class IndegoDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
44 private static final int TIMEOUT_SECONDS = 60;
46 private final Logger logger = LoggerFactory.getLogger(IndegoDiscoveryService.class);
48 private @NonNullByDefault({}) BoschAccountHandler accountHandler;
50 public IndegoDiscoveryService() {
51 super(Set.of(THING_TYPE_ACCOUNT), TIMEOUT_SECONDS, false);
55 public @Nullable ThingHandler getThingHandler() {
56 return accountHandler;
60 public void setThingHandler(ThingHandler handler) {
61 if (handler instanceof BoschAccountHandler accountHandler) {
62 this.accountHandler = accountHandler;
67 public Set<ThingTypeUID> getSupportedThingTypes() {
68 return Set.of(THING_TYPE_INDEGO);
72 public void startScan() {
74 Collection<String> serialNumbers = accountHandler.getSerialNumbers();
76 ThingUID bridgeUID = accountHandler.getThing().getUID();
77 for (String serialNumber : serialNumbers) {
78 ThingUID thingUID = new ThingUID(THING_TYPE_INDEGO, bridgeUID, serialNumber);
79 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
80 .withProperty(Thing.PROPERTY_SERIAL_NUMBER, serialNumber).withBridge(bridgeUID)
81 .withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER)
82 .withLabel("Indego (" + serialNumber + ")").build();
84 thingDiscovered(discoveryResult);
86 } catch (IndegoException e) {
87 logger.debug("Failed to retrieve serial numbers: {}", e.getMessage());
92 protected synchronized void stopScan() {
94 removeOlderResults(getTimestampOfLastScan());
98 public void deactivate() {
99 removeOlderResults(Instant.now().getEpochSecond());