]> git.basschouten.com Git - openhab-addons.git/blob
b4262645791486a74b851ac6d869c5fc50adb048
[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.revogi.internal;
14
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.concurrent.CompletableFuture;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.revogi.internal.api.DiscoveryRawResponseDTO;
25 import org.openhab.binding.revogi.internal.api.DiscoveryResponseDTO;
26 import org.openhab.binding.revogi.internal.api.RevogiDiscoveryService;
27 import org.openhab.binding.revogi.internal.udp.DatagramSocketWrapper;
28 import org.openhab.binding.revogi.internal.udp.UdpSenderService;
29 import org.openhab.core.config.discovery.AbstractDiscoveryService;
30 import org.openhab.core.config.discovery.DiscoveryResult;
31 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
32 import org.openhab.core.config.discovery.DiscoveryService;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.thing.ThingUID;
36 import org.osgi.service.component.annotations.Component;
37
38 /**
39  * The {@link RevogiSmartStripDiscoveryService} helps to discover new smart strips
40  *
41  * @author Andi Bräu - Initial contribution
42  */
43 @Component(service = DiscoveryService.class, configurationPid = "discovery.revogi")
44 @NonNullByDefault
45 public class RevogiSmartStripDiscoveryService extends AbstractDiscoveryService {
46     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections
47             .singleton(RevogiSmartStripControlBindingConstants.SMART_STRIP_THING_TYPE);
48
49     private final RevogiDiscoveryService revogiDiscoveryService;
50
51     private static final int SEARCH_TIMEOUT_SEC = 10;
52
53     public RevogiSmartStripDiscoveryService() {
54         super(SUPPORTED_THING_TYPES, SEARCH_TIMEOUT_SEC);
55         revogiDiscoveryService = new RevogiDiscoveryService(
56                 new UdpSenderService(new DatagramSocketWrapper(), scheduler));
57     }
58
59     @Override
60     public Set<ThingTypeUID> getSupportedThingTypes() {
61         return SUPPORTED_THING_TYPES;
62     }
63
64     @Override
65     protected void startScan() {
66         CompletableFuture<List<DiscoveryRawResponseDTO>> discoveryResponses = revogiDiscoveryService
67                 .discoverSmartStrips();
68         discoveryResponses.thenAccept(this::applyDiscoveryResults);
69     }
70
71     private void applyDiscoveryResults(final List<DiscoveryRawResponseDTO> discoveryRawResponses) {
72         discoveryRawResponses.forEach(response -> {
73             ThingUID thingUID = getThingUID(response.getData());
74             if (thingUID != null) {
75                 Map<String, Object> properties = new HashMap<>();
76                 properties.put(Thing.PROPERTY_MODEL_ID, response.getData().getRegId());
77                 properties.put(Thing.PROPERTY_MAC_ADDRESS, response.getData().getMacAddress());
78                 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, response.getData().getVersion());
79                 properties.put(Thing.PROPERTY_SERIAL_NUMBER, response.getData().getSerialNumber());
80                 properties.put("ipAddress", response.getIpAddress());
81                 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
82                         .withThingType(RevogiSmartStripControlBindingConstants.SMART_STRIP_THING_TYPE)
83                         .withProperties(properties).withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER).build();
84                 thingDiscovered(discoveryResult);
85             }
86         });
87     }
88
89     private @Nullable ThingUID getThingUID(DiscoveryResponseDTO response) {
90         if (getSupportedThingTypes().contains(RevogiSmartStripControlBindingConstants.SMART_STRIP_THING_TYPE)) {
91             return new ThingUID(RevogiSmartStripControlBindingConstants.SMART_STRIP_THING_TYPE,
92                     response.getSerialNumber());
93         } else {
94             return null;
95         }
96     }
97 }