]> git.basschouten.com Git - openhab-addons.git/blob
4d9d911403e92abd23b6816368a0bc62bdaa1fba
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.boschindego.internal.discovery;
14
15 import static org.openhab.binding.boschindego.internal.BoschIndegoBindingConstants.*;
16
17 import java.time.Instant;
18 import java.util.Collection;
19 import java.util.Set;
20
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;
35
36 /**
37  * The {@link IndegoDiscoveryService} is responsible for discovering Indego mowers.
38  *
39  * @author Jacob Laursen - Initial contribution
40  */
41 @NonNullByDefault
42 public class IndegoDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
43
44     private static final int TIMEOUT_SECONDS = 60;
45
46     private final Logger logger = LoggerFactory.getLogger(IndegoDiscoveryService.class);
47
48     private @NonNullByDefault({}) BoschAccountHandler accountHandler;
49
50     public IndegoDiscoveryService() {
51         super(Set.of(THING_TYPE_ACCOUNT), TIMEOUT_SECONDS, false);
52     }
53
54     @Override
55     public @Nullable ThingHandler getThingHandler() {
56         return accountHandler;
57     }
58
59     @Override
60     public void setThingHandler(ThingHandler handler) {
61         if (handler instanceof BoschAccountHandler accountHandler) {
62             this.accountHandler = accountHandler;
63         }
64     }
65
66     @Override
67     public Set<ThingTypeUID> getSupportedThingTypes() {
68         return Set.of(THING_TYPE_INDEGO);
69     }
70
71     @Override
72     public void startScan() {
73         try {
74             Collection<String> serialNumbers = accountHandler.getSerialNumbers();
75
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();
83
84                 thingDiscovered(discoveryResult);
85             }
86         } catch (IndegoException e) {
87             logger.debug("Failed to retrieve serial numbers: {}", e.getMessage());
88         }
89     }
90
91     @Override
92     protected synchronized void stopScan() {
93         super.stopScan();
94         removeOlderResults(getTimestampOfLastScan());
95     }
96
97     @Override
98     public void deactivate() {
99         removeOlderResults(Instant.now().getEpochSecond());
100     }
101 }