]> git.basschouten.com Git - openhab-addons.git/blob
7b0b483962f14b4b91a08d05e7f877307eef5a14
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.neeo.internal.discovery;
14
15 import java.io.IOException;
16 import java.util.Collections;
17 import java.util.Objects;
18 import java.util.Set;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.neeo.internal.NeeoBrainApi;
22 import org.openhab.binding.neeo.internal.NeeoConstants;
23 import org.openhab.binding.neeo.internal.NeeoRoomConfig;
24 import org.openhab.binding.neeo.internal.UidUtils;
25 import org.openhab.binding.neeo.internal.handler.NeeoRoomHandler;
26 import org.openhab.binding.neeo.internal.models.NeeoDevice;
27 import org.openhab.binding.neeo.internal.models.NeeoRoom;
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.thing.Bridge;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Implementation of {@link AbstractDiscoveryService} that will discover the devices in a NEEO room;
39  *
40  * @author Tim Roberts - Initial contribution
41  */
42 @NonNullByDefault
43 public class NeeoDeviceDiscoveryService extends AbstractDiscoveryService {
44
45     /** The logger */
46     private final Logger logger = LoggerFactory.getLogger(NeeoDeviceDiscoveryService.class);
47
48     /** The device thing type we support */
49     private static final Set<ThingTypeUID> DISCOVERABLE_THING_TYPES_UIDS = Collections
50             .singleton(NeeoConstants.THING_TYPE_DEVICE);
51
52     /** The timeout (in seconds) for searching the room */
53     private static final int SEARCH_TIME = 10;
54
55     /** The room handler to search */
56     private final NeeoRoomHandler roomHandler;
57
58     /**
59      * Constructs the discovery service from the room handler
60      *
61      * @param roomHandler a non-null room handler
62      */
63     public NeeoDeviceDiscoveryService(NeeoRoomHandler roomHandler) {
64         super(DISCOVERABLE_THING_TYPES_UIDS, SEARCH_TIME);
65         Objects.requireNonNull(roomHandler, "roomHandler cannot be null");
66         this.roomHandler = roomHandler;
67     }
68
69     @Override
70     protected void startScan() {
71         final Bridge roomBridge = roomHandler.getThing();
72         final ThingUID roomUid = roomBridge.getUID();
73
74         final String brainId = roomHandler.getNeeoBrainId();
75         if (brainId == null || brainId.isEmpty()) {
76             logger.debug("Unknown brain ID for roomHandler: {}", roomHandler);
77             return;
78         }
79
80         final NeeoBrainApi api = roomHandler.getNeeoBrainApi();
81         if (api == null) {
82             logger.debug("Brain API was not available for {} - skipping", brainId);
83             return;
84         }
85
86         final NeeoRoomConfig config = roomBridge.getConfiguration().as(NeeoRoomConfig.class);
87         final String roomKey = config.getRoomKey();
88         if (roomKey == null || roomKey.isEmpty()) {
89             logger.debug("RoomKey wasn't configured for {} - skipping", brainId);
90             return;
91         }
92
93         try {
94             final NeeoRoom room = api.getRoom(roomKey);
95             final NeeoDevice[] devices = room.getDevices().getDevices();
96
97             if (devices.length == 0) {
98                 logger.debug("Room {} found - but there were no devices - skipping", room.getName());
99                 return;
100             }
101
102             logger.debug("Room {} found, scanning {} devices in it", room.getName(), devices.length);
103             for (NeeoDevice device : devices) {
104                 final String deviceKey = device.getKey();
105                 if (deviceKey == null || deviceKey.isEmpty()) {
106                     logger.debug("Device key wasn't found for device: {}", device);
107                     continue;
108                 }
109
110                 if (config.isExcludeThings() && UidUtils.isThing(device)) {
111                     logger.debug("Found openHAB thing but ignoring per configuration: {}", device);
112                     continue;
113                 }
114
115                 logger.debug("Device #{} found - {}", deviceKey, device.getName());
116
117                 final ThingUID thingUID = new ThingUID(NeeoConstants.THING_TYPE_DEVICE, roomUid, deviceKey);
118
119                 final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
120                         .withProperty(NeeoConstants.CONFIG_DEVICEKEY, deviceKey).withBridge(roomUid)
121                         .withLabel(device.getName() + " (NEEO " + brainId + ")").build();
122                 thingDiscovered(discoveryResult);
123             }
124         } catch (IOException e) {
125             logger.debug("IOException occurred getting brain info ({}): {}", brainId, e.getMessage(), e);
126         }
127     }
128 }