]> git.basschouten.com Git - openhab-addons.git/blob
aa5086d50f679bb30718e430a580cd451af916a6
[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.wled.internal;
14
15 import static org.openhab.binding.wled.internal.WLedBindingConstants.*;
16
17 import java.util.List;
18 import java.util.Map;
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.wled.internal.api.WledApi;
24 import org.openhab.binding.wled.internal.handlers.WLedBridgeHandler;
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.config.discovery.DiscoveryService;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerService;
34
35 /**
36  * The {@link WLedSegmentDiscoveryService} Discovers and adds any Wled segments found by the bridge device.
37  *
38  * @author Matthew Skinner - Initial contribution
39  */
40 @NonNullByDefault
41 public class WLedSegmentDiscoveryService extends AbstractDiscoveryService
42         implements DiscoveryService, ThingHandlerService {
43     private @Nullable WLedBridgeHandler bridgeHandler;
44     private @Nullable ThingUID bridgeUID;
45     private static final int SEARCH_TIME = 10;
46
47     public WLedSegmentDiscoveryService() {
48         super(SUPPORTED_THING_TYPES, SEARCH_TIME);
49     }
50
51     public WLedSegmentDiscoveryService(Set<ThingTypeUID> supportedThingTypes, int timeout)
52             throws IllegalArgumentException {
53         super(supportedThingTypes, timeout);
54     }
55
56     private void buildThing(int segmentIndex, String segmentName) {
57         ThingUID localBridgeUID = bridgeUID;
58         if (localBridgeUID == null) {
59             return;
60         }
61         String newThingUID = localBridgeUID.getId() + "-" + segmentIndex;
62         ThingUID thingUID = new ThingUID(THING_TYPE_SEGMENT, localBridgeUID, newThingUID);
63         Map<String, Object> properties = Map.of(Thing.PROPERTY_SERIAL_NUMBER, newThingUID, CONFIG_SEGMENT_INDEX,
64                 segmentIndex);
65         DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withLabel(segmentName)
66                 .withProperties(properties).withBridge(bridgeUID)
67                 .withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER).build();
68         thingDiscovered(discoveryResult);
69     }
70
71     @Override
72     protected void startScan() {
73         WLedBridgeHandler localBridgeHandler = bridgeHandler;
74         if (localBridgeHandler != null) {
75             WledApi localAPI = localBridgeHandler.api;
76             if (localAPI != null) {
77                 List<String> names = localAPI.getSegmentNames();
78                 for (int count = 0; count < names.size(); count++) {
79                     buildThing(count, names.get(count));
80                 }
81             }
82         }
83     }
84
85     @Override
86     public void setThingHandler(@Nullable ThingHandler handler) {
87         if (handler instanceof WLedBridgeHandler) {
88             bridgeHandler = (WLedBridgeHandler) handler;
89             bridgeUID = bridgeHandler.getThing().getUID();
90         }
91     }
92
93     @Override
94     public @Nullable ThingHandler getThingHandler() {
95         return bridgeHandler;
96     }
97
98     @Override
99     public void deactivate() {
100     }
101 }