]> git.basschouten.com Git - openhab-addons.git/blob
af61c6f2cdf5695585de0ac527702924f3ebfcb8
[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.meater.internal.discovery;
14
15 import static org.openhab.binding.meater.internal.MeaterBindingConstants.*;
16
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.meater.internal.MeaterConfiguration;
22 import org.openhab.binding.meater.internal.handler.MeaterBridgeHandler;
23 import org.openhab.core.config.discovery.AbstractDiscoveryService;
24 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
25 import org.openhab.core.thing.ThingUID;
26 import org.openhab.core.thing.binding.ThingHandler;
27 import org.openhab.core.thing.binding.ThingHandlerService;
28
29 /**
30  * The {@link MeaterDiscoveryService} searches for available
31  * Meater probes discoverable through MEATER REST API.
32  *
33  * @author Jan Gustafsson - Initial contribution
34  */
35 @NonNullByDefault
36 public class MeaterDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
37     private static final int SEARCH_TIME = 2;
38     private @Nullable MeaterBridgeHandler handler;
39
40     public MeaterDiscoveryService() {
41         super(SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME);
42     }
43
44     @Override
45     public void setThingHandler(@Nullable ThingHandler handler) {
46         if (handler instanceof MeaterBridgeHandler) {
47             this.handler = (MeaterBridgeHandler) handler;
48             i18nProvider = ((MeaterBridgeHandler) handler).getI18nProvider();
49             localeProvider = ((MeaterBridgeHandler) handler).getLocaleProvider();
50         }
51     }
52
53     @Override
54     public @Nullable ThingHandler getThingHandler() {
55         return handler;
56     }
57
58     @Override
59     public void activate(@Nullable Map<String, Object> configProperties) {
60         super.activate(configProperties);
61     }
62
63     @Override
64     public void deactivate() {
65         super.deactivate();
66     }
67
68     @Override
69     protected void startScan() {
70         MeaterBridgeHandler bridgeHandler = this.handler;
71         if (bridgeHandler != null) {
72             ThingUID bridgeUID = bridgeHandler.getThing().getUID();
73             bridgeHandler.getMeaterThings().entrySet().stream().forEach(thing -> {
74                 thingDiscovered(
75                         DiscoveryResultBuilder.create(new ThingUID(THING_TYPE_MEATER_PROBE, bridgeUID, thing.getKey()))
76                                 .withLabel("@text/discovery.probe.label").withBridge(bridgeUID)
77                                 .withProperty(MeaterConfiguration.DEVICE_ID_LABEL, thing.getKey())
78                                 .withRepresentationProperty(MeaterConfiguration.DEVICE_ID_LABEL).build());
79             });
80         }
81     }
82 }