2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.revogi.internal;
15 import java.util.HashMap;
16 import java.util.List;
19 import java.util.concurrent.CompletableFuture;
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;
38 * The {@link RevogiSmartStripDiscoveryService} helps to discover new smart strips
40 * @author Andi Bräu - Initial contribution
42 @Component(service = DiscoveryService.class, configurationPid = "discovery.revogi")
44 public class RevogiSmartStripDiscoveryService extends AbstractDiscoveryService {
45 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set
46 .of(RevogiSmartStripControlBindingConstants.SMART_STRIP_THING_TYPE);
48 private final RevogiDiscoveryService revogiDiscoveryService;
50 private static final int SEARCH_TIMEOUT_SEC = 10;
52 public RevogiSmartStripDiscoveryService() {
53 super(SUPPORTED_THING_TYPES, SEARCH_TIMEOUT_SEC);
54 revogiDiscoveryService = new RevogiDiscoveryService(
55 new UdpSenderService(new DatagramSocketWrapper(), scheduler));
59 public Set<ThingTypeUID> getSupportedThingTypes() {
60 return SUPPORTED_THING_TYPES;
64 protected void startScan() {
65 CompletableFuture<List<DiscoveryRawResponseDTO>> discoveryResponses = revogiDiscoveryService
66 .discoverSmartStrips();
67 discoveryResponses.thenAccept(this::applyDiscoveryResults);
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);
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());