]> git.basschouten.com Git - openhab-addons.git/blob
ecdbcd6b9d09383cb8c541dce2d130b8a9563887
[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.openhab.binding.boschindego.internal.IndegoTypeDatabase;
23 import org.openhab.binding.boschindego.internal.dto.response.DevicePropertiesResponse;
24 import org.openhab.binding.boschindego.internal.exceptions.IndegoException;
25 import org.openhab.binding.boschindego.internal.handler.BoschAccountHandler;
26 import org.openhab.core.config.discovery.AbstractThingHandlerDiscoveryService;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.osgi.service.component.annotations.Component;
33 import org.osgi.service.component.annotations.ServiceScope;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * The {@link IndegoDiscoveryService} is responsible for discovering Indego mowers.
39  *
40  * @author Jacob Laursen - Initial contribution
41  */
42 @Component(scope = ServiceScope.PROTOTYPE, service = IndegoDiscoveryService.class)
43 @NonNullByDefault
44 public class IndegoDiscoveryService extends AbstractThingHandlerDiscoveryService<BoschAccountHandler> {
45     private static final int TIMEOUT_SECONDS = 60;
46
47     private final Logger logger = LoggerFactory.getLogger(IndegoDiscoveryService.class);
48
49     public IndegoDiscoveryService() {
50         super(BoschAccountHandler.class, Set.of(THING_TYPE_ACCOUNT), TIMEOUT_SECONDS, false);
51     }
52
53     @Override
54     public Set<ThingTypeUID> getSupportedThingTypes() {
55         return Set.of(THING_TYPE_INDEGO);
56     }
57
58     @Override
59     public void startScan() {
60         try {
61             Collection<DevicePropertiesResponse> devices = thingHandler.getDevices();
62
63             ThingUID bridgeUID = thingHandler.getThing().getUID();
64             for (DevicePropertiesResponse device : devices) {
65                 ThingUID thingUID = new ThingUID(THING_TYPE_INDEGO, bridgeUID, device.serialNumber);
66                 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
67                         .withProperty(Thing.PROPERTY_SERIAL_NUMBER, device.serialNumber).withBridge(bridgeUID)
68                         .withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER)
69                         .withLabel(IndegoTypeDatabase.nameFromTypeNumber(device.bareToolNumber)).build();
70
71                 thingDiscovered(discoveryResult);
72             }
73         } catch (IndegoException e) {
74             logger.debug("Failed to retrieve serial numbers: {}", e.getMessage());
75         }
76     }
77
78     @Override
79     protected synchronized void stopScan() {
80         super.stopScan();
81         removeOlderResults(getTimestampOfLastScan());
82     }
83
84     @Override
85     public void dispose() {
86         super.dispose();
87         removeOlderResults(Instant.now().getEpochSecond());
88     }
89 }