]> git.basschouten.com Git - openhab-addons.git/blob
105e17583db1ef316f7aca3fff1c4e9a9f72c7ff
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.mynice.internal.discovery;
14
15 import static org.openhab.binding.mynice.internal.MyNiceBindingConstants.*;
16
17 import java.util.List;
18 import java.util.Optional;
19 import java.util.Set;
20
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;
35
36 /**
37  * The {@link MyNiceDiscoveryService} is responsible for discovering all things
38  * except the It4Wifi bridge itself
39  *
40  * @author GaĆ«l L'hopital - Initial contribution
41  */
42 @NonNullByDefault
43 public class MyNiceDiscoveryService extends AbstractDiscoveryService
44         implements MyNiceDataListener, ThingHandlerService {
45
46     private static final int SEARCH_TIME = 5;
47     private final Logger logger = LoggerFactory.getLogger(MyNiceDiscoveryService.class);
48
49     private Optional<It4WifiHandler> bridgeHandler = Optional.empty();
50
51     /**
52      * Creates a MyNiceDiscoveryService with background discovery disabled.
53      */
54     public MyNiceDiscoveryService() {
55         super(Set.of(THING_TYPE_SWING, THING_TYPE_SLIDING), SEARCH_TIME, false);
56     }
57
58     @Override
59     public void setThingHandler(ThingHandler handler) {
60         if (handler instanceof It4WifiHandler it4Handler) {
61             bridgeHandler = Optional.of(it4Handler);
62         }
63     }
64
65     @Override
66     public @Nullable ThingHandler getThingHandler() {
67         return bridgeHandler.orElse(null);
68     }
69
70     @Override
71     public void activate() {
72         super.activate(null);
73         bridgeHandler.ifPresent(h -> h.registerDataListener(this));
74     }
75
76     @Override
77     public void deactivate() {
78         bridgeHandler.ifPresent(h -> h.unregisterDataListener(this));
79         bridgeHandler = Optional.empty();
80         super.deactivate();
81     }
82
83     @Override
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);
91                     default -> null;
92                 };
93
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);
99                 } else {
100                     logger.info("`{}` type of device is not yet supported", device.type);
101                 }
102             });
103         });
104     }
105
106     @Override
107     protected void startScan() {
108         bridgeHandler.ifPresent(h -> h.sendCommand(CommandType.INFO));
109     }
110 }