2 * Copyright (c) 2010-2024 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.IndegoTypeDatabase;
24 import org.openhab.binding.boschindego.internal.dto.response.DevicePropertiesResponse;
25 import org.openhab.binding.boschindego.internal.exceptions.IndegoException;
26 import org.openhab.binding.boschindego.internal.handler.BoschAccountHandler;
27 import org.openhab.core.config.discovery.AbstractDiscoveryService;
28 import org.openhab.core.config.discovery.DiscoveryResult;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerService;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * The {@link IndegoDiscoveryService} is responsible for discovering Indego mowers.
41 * @author Jacob Laursen - Initial contribution
44 public class IndegoDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
46 private static final int TIMEOUT_SECONDS = 60;
48 private final Logger logger = LoggerFactory.getLogger(IndegoDiscoveryService.class);
50 private @NonNullByDefault({}) BoschAccountHandler accountHandler;
52 public IndegoDiscoveryService() {
53 super(Set.of(THING_TYPE_ACCOUNT), TIMEOUT_SECONDS, false);
57 public @Nullable ThingHandler getThingHandler() {
58 return accountHandler;
62 public void setThingHandler(ThingHandler handler) {
63 if (handler instanceof BoschAccountHandler accountHandler) {
64 this.accountHandler = accountHandler;
69 public Set<ThingTypeUID> getSupportedThingTypes() {
70 return Set.of(THING_TYPE_INDEGO);
74 public void startScan() {
76 Collection<DevicePropertiesResponse> devices = accountHandler.getDevices();
78 ThingUID bridgeUID = accountHandler.getThing().getUID();
79 for (DevicePropertiesResponse device : devices) {
80 ThingUID thingUID = new ThingUID(THING_TYPE_INDEGO, bridgeUID, device.serialNumber);
81 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
82 .withProperty(Thing.PROPERTY_SERIAL_NUMBER, device.serialNumber).withBridge(bridgeUID)
83 .withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER)
84 .withLabel(IndegoTypeDatabase.nameFromTypeNumber(device.bareToolNumber)).build();
86 thingDiscovered(discoveryResult);
88 } catch (IndegoException e) {
89 logger.debug("Failed to retrieve serial numbers: {}", e.getMessage());
94 protected synchronized void stopScan() {
96 removeOlderResults(getTimestampOfLastScan());
100 public void deactivate() {
101 removeOlderResults(Instant.now().getEpochSecond());