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.Collections;
16 import java.util.HashMap;
17 import java.util.List;
20 import java.util.concurrent.CompletableFuture;
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;
39 * The {@link RevogiSmartStripDiscoveryService} helps to discover new smart strips
41 * @author Andi Bräu - Initial contribution
43 @Component(service = DiscoveryService.class, configurationPid = "discovery.revogi")
45 public class RevogiSmartStripDiscoveryService extends AbstractDiscoveryService {
46 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections
47 .singleton(RevogiSmartStripControlBindingConstants.SMART_STRIP_THING_TYPE);
49 private final RevogiDiscoveryService revogiDiscoveryService;
51 private static final int SEARCH_TIMEOUT_SEC = 10;
53 public RevogiSmartStripDiscoveryService() {
54 super(SUPPORTED_THING_TYPES, SEARCH_TIMEOUT_SEC);
55 revogiDiscoveryService = new RevogiDiscoveryService(
56 new UdpSenderService(new DatagramSocketWrapper(), scheduler));
60 public Set<ThingTypeUID> getSupportedThingTypes() {
61 return SUPPORTED_THING_TYPES;
65 protected void startScan() {
66 CompletableFuture<List<DiscoveryRawResponseDTO>> discoveryResponses = revogiDiscoveryService
67 .discoverSmartStrips();
68 discoveryResponses.thenAccept(this::applyDiscoveryResults);
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);
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());