]> git.basschouten.com Git - openhab-addons.git/blob
19dc182975e7bdd8ed9b7520950885f1e4f22ab6
[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.mynice.internal.discovery;
14
15 import static org.openhab.binding.mynice.internal.MyNiceBindingConstants.*;
16
17 import java.util.List;
18 import java.util.Set;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.mynice.internal.handler.It4WifiHandler;
23 import org.openhab.binding.mynice.internal.handler.MyNiceDataListener;
24 import org.openhab.binding.mynice.internal.xml.dto.CommandType;
25 import org.openhab.binding.mynice.internal.xml.dto.Device;
26 import org.openhab.core.config.discovery.AbstractDiscoveryService;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.thing.ThingUID;
30 import org.openhab.core.thing.binding.ThingHandler;
31 import org.openhab.core.thing.binding.ThingHandlerService;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * The {@link MyNiceDiscoveryService} is responsible for discovering all things
37  * except the It4Wifi bridge itself
38  *
39  * @author GaĆ«l L'hopital - Initial contribution
40  */
41 @NonNullByDefault
42 public class MyNiceDiscoveryService extends AbstractDiscoveryService
43         implements MyNiceDataListener, ThingHandlerService {
44
45     private static final int SEARCH_TIME = 5;
46     private final Logger logger = LoggerFactory.getLogger(MyNiceDiscoveryService.class);
47
48     private @Nullable It4WifiHandler bridgeHandler;
49
50     /**
51      * Creates a MyNiceDiscoveryService with background discovery disabled.
52      */
53     public MyNiceDiscoveryService() {
54         super(Set.of(THING_TYPE_SWING), SEARCH_TIME, false);
55     }
56
57     @Override
58     public void setThingHandler(ThingHandler handler) {
59         if (handler instanceof It4WifiHandler it4Handler) {
60             bridgeHandler = it4Handler;
61         }
62     }
63
64     @Override
65     public @Nullable ThingHandler getThingHandler() {
66         return bridgeHandler;
67     }
68
69     @Override
70     public void activate() {
71         super.activate(null);
72         It4WifiHandler handler = bridgeHandler;
73         if (handler != null) {
74             handler.registerDataListener(this);
75         }
76     }
77
78     @Override
79     public void deactivate() {
80         It4WifiHandler handler = bridgeHandler;
81         if (handler != null) {
82             handler.unregisterDataListener(this);
83         }
84         super.deactivate();
85     }
86
87     @Override
88     public void onDataFetched(List<Device> devices) {
89         It4WifiHandler handler = bridgeHandler;
90         if (handler != null) {
91             ThingUID bridgeUID = handler.getThing().getUID();
92             devices.stream().filter(device -> device.type != null).forEach(device -> {
93                 ThingUID thingUID = switch (device.type) {
94                     case SWING -> new ThingUID(THING_TYPE_SWING, bridgeUID, device.id);
95                     case SLIDING -> new ThingUID(THING_TYPE_SLIDING, bridgeUID, device.id);
96                     default -> null;
97                 };
98
99                 if (thingUID != null) {
100                     DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID)
101                             .withLabel(String.format("%s %s", device.manuf, device.prod))
102                             .withRepresentationProperty(DEVICE_ID).withProperty(DEVICE_ID, device.id).build();
103                     thingDiscovered(discoveryResult);
104                 } else {
105                     logger.info("`{}` type of device is not yet supported", device.type);
106                 }
107             });
108         }
109     }
110
111     @Override
112     protected void startScan() {
113         It4WifiHandler handler = bridgeHandler;
114         if (handler != null) {
115             handler.sendCommand(CommandType.INFO);
116         }
117     }
118 }