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