2 * Copyright (c) 2010-2024 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.mynice.internal.discovery;
15 import static org.openhab.binding.mynice.internal.MyNiceBindingConstants.*;
17 import java.util.List;
18 import java.util.Optional;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.mynice.internal.handler.It4WifiHandler;
24 import org.openhab.binding.mynice.internal.handler.MyNiceDataListener;
25 import org.openhab.binding.mynice.internal.xml.dto.CommandType;
26 import org.openhab.binding.mynice.internal.xml.dto.Device;
27 import org.openhab.core.config.discovery.AbstractDiscoveryService;
28 import org.openhab.core.config.discovery.DiscoveryResult;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.thing.ThingUID;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerService;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * The {@link MyNiceDiscoveryService} is responsible for discovering all things
38 * except the It4Wifi bridge itself
40 * @author Gaƫl L'hopital - Initial contribution
43 public class MyNiceDiscoveryService extends AbstractDiscoveryService
44 implements MyNiceDataListener, ThingHandlerService {
46 private static final int SEARCH_TIME = 5;
47 private final Logger logger = LoggerFactory.getLogger(MyNiceDiscoveryService.class);
49 private Optional<It4WifiHandler> bridgeHandler = Optional.empty();
52 * Creates a MyNiceDiscoveryService with background discovery disabled.
54 public MyNiceDiscoveryService() {
55 super(Set.of(THING_TYPE_SWING, THING_TYPE_SLIDING), SEARCH_TIME, false);
59 public void setThingHandler(ThingHandler handler) {
60 if (handler instanceof It4WifiHandler it4Handler) {
61 bridgeHandler = Optional.of(it4Handler);
66 public @Nullable ThingHandler getThingHandler() {
67 return bridgeHandler.orElse(null);
71 public void activate() {
73 bridgeHandler.ifPresent(h -> h.registerDataListener(this));
77 public void deactivate() {
78 bridgeHandler.ifPresent(h -> h.unregisterDataListener(this));
79 bridgeHandler = Optional.empty();
84 public void onDataFetched(List<Device> devices) {
85 bridgeHandler.ifPresent(handler -> {
86 ThingUID bridgeUID = handler.getThing().getUID();
87 devices.stream().filter(device -> device.type != null).forEach(device -> {
88 ThingUID thingUID = switch (device.type) {
89 case SWING -> new ThingUID(THING_TYPE_SWING, bridgeUID, device.id);
90 case SLIDING -> new ThingUID(THING_TYPE_SLIDING, bridgeUID, device.id);
94 if (thingUID != null) {
95 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID)
96 .withLabel(String.format("%s %s", device.manuf, device.prod))
97 .withRepresentationProperty(DEVICE_ID).withProperty(DEVICE_ID, device.id).build();
98 thingDiscovered(discoveryResult);
100 logger.info("`{}` type of device is not yet supported", device.type);
107 protected void startScan() {
108 bridgeHandler.ifPresent(h -> h.sendCommand(CommandType.INFO));