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