]> git.basschouten.com Git - openhab-addons.git/blob
c863e63ef644896d79f1800b795b35b47e719cea
[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.NeeoBrainConfig;
24 import org.openhab.binding.neeo.internal.NeeoConstants;
25 import org.openhab.binding.neeo.internal.handler.NeeoBrainHandler;
26 import org.openhab.binding.neeo.internal.models.NeeoBrain;
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 rooms in a NEEO brain;
39  *
40  * @author Tim Roberts - Initial contribution
41  */
42 @NonNullByDefault
43 public class NeeoRoomDiscoveryService extends AbstractDiscoveryService {
44     /** The logger */
45     private final Logger logger = LoggerFactory.getLogger(NeeoRoomDiscoveryService.class);
46
47     /** The room bridge type we support */
48     private static final Set<ThingTypeUID> DISCOVERABLE_THING_TYPES_UIDS = Collections
49             .singleton(NeeoConstants.BRIDGE_TYPE_ROOM);
50
51     /** The timeout (in seconds) for searching the brain */
52     private static final int SEARCH_TIME = 10;
53
54     /** The brain handler that we will use */
55     private final NeeoBrainHandler brainHandler;
56
57     /**
58      * Constructs the discover service from the brain handler
59      *
60      * @param brainHandler a non-null brain handler
61      */
62     public NeeoRoomDiscoveryService(NeeoBrainHandler brainHandler) {
63         super(DISCOVERABLE_THING_TYPES_UIDS, SEARCH_TIME);
64         Objects.requireNonNull(brainHandler, "brainHandler cannot be null");
65         this.brainHandler = brainHandler;
66     }
67
68     @Override
69     protected void startScan() {
70         final String brainId = brainHandler.getNeeoBrainId();
71
72         final Bridge brainBridge = brainHandler.getThing();
73         final ThingUID brainUid = brainBridge.getUID();
74
75         final NeeoBrainApi api = brainHandler.getNeeoBrainApi();
76         if (api == null) {
77             logger.debug("Brain API was not available for {} - skipping", brainId);
78             return;
79         }
80
81         try {
82             final NeeoBrain brain = api.getBrain();
83             final NeeoBrainConfig config = brainBridge.getConfiguration().as(NeeoBrainConfig.class);
84             final NeeoRoom[] rooms = brain.getRooms().getRooms();
85
86             if (rooms.length == 0) {
87                 logger.debug("Brain {} ({}) found - but there were no rooms - skipping", brain.getName(), brainId);
88                 return;
89             }
90
91             logger.debug("Brain {} ({}) found, scanning {} rooms in it", brain.getName(), brainId, rooms.length);
92             for (NeeoRoom room : rooms) {
93                 final String roomKey = room.getKey();
94                 if (roomKey == null || StringUtils.isEmpty(roomKey)) {
95                     logger.debug("Room didn't have a room key: {}", room);
96                     continue;
97                 }
98
99                 if (room.getDevices().getDevices().length == 0 && room.getRecipes().getRecipes().length == 0
100                         && !config.isDiscoverEmptyRooms()) {
101                     logger.debug("Room {} ({}) found but has no devices or recipes, ignoring - {}", roomKey, brainId,
102                             room.getName());
103                     continue;
104                 }
105
106                 final ThingUID thingUID = new ThingUID(NeeoConstants.BRIDGE_TYPE_ROOM, brainUid, roomKey);
107
108                 final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
109                         .withProperty(NeeoConstants.CONFIG_ROOMKEY, roomKey)
110                         .withProperty(NeeoConstants.CONFIG_EXCLUDE_THINGS, true).withBridge(brainUid)
111                         .withLabel(room.getName() + " (NEEO " + brainId + ")").build();
112                 thingDiscovered(discoveryResult);
113             }
114         } catch (IOException e) {
115             logger.debug("IOException occurred getting brain info ({}): {}", brainId, e.getMessage(), e);
116         }
117     }
118 }