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