]> git.basschouten.com Git - openhab-addons.git/blob
f90a9ea4eb71c56d40c00b2c6ecd6338d1a781fb
[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.cbus.internal.discovery;
14
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.cbus.CBusBindingConstants;
21 import org.openhab.binding.cbus.handler.CBusNetworkHandler;
22 import org.openhab.core.config.discovery.AbstractDiscoveryService;
23 import org.openhab.core.config.discovery.DiscoveryResult;
24 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
25 import org.openhab.core.thing.ThingStatus;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.daveoxley.cbus.Application;
32 import com.daveoxley.cbus.CGateException;
33 import com.daveoxley.cbus.Group;
34 import com.daveoxley.cbus.Network;
35
36 /**
37  * The {@link CBusGroupDiscovery} class is used to discover CBus
38  * groups that are in the CBus Network
39  *
40  * @author Scott Linton - Initial contribution
41  */
42 @NonNullByDefault
43 public class CBusGroupDiscovery extends AbstractDiscoveryService {
44
45     private final Logger logger = LoggerFactory.getLogger(CBusGroupDiscovery.class);
46
47     private final CBusNetworkHandler cbusNetworkHandler;
48
49     public CBusGroupDiscovery(CBusNetworkHandler cbusNetworkHandler) {
50         super(CBusBindingConstants.SUPPORTED_THING_TYPES_UIDS, 30, false);
51         this.cbusNetworkHandler = cbusNetworkHandler;
52     }
53
54     @Override
55     protected synchronized void startScan() {
56         if (cbusNetworkHandler.getThing().getStatus().equals(ThingStatus.ONLINE)) {
57             ThingUID bridgeUid = cbusNetworkHandler.getThing().getBridgeUID();
58             if (bridgeUid == null) {
59                 scanFinished();
60                 return;
61             }
62             try {
63                 Map<Integer, ThingTypeUID> applications = new HashMap<Integer, ThingTypeUID>();
64                 applications.put(CBusBindingConstants.CBUS_APPLICATION_LIGHTING, CBusBindingConstants.THING_TYPE_LIGHT);
65                 applications.put(CBusBindingConstants.CBUS_APPLICATION_DALI, CBusBindingConstants.THING_TYPE_DALI);
66                 applications.put(CBusBindingConstants.CBUS_APPLICATION_TEMPERATURE,
67                         CBusBindingConstants.THING_TYPE_TEMPERATURE);
68                 applications.put(CBusBindingConstants.CBUS_APPLICATION_TRIGGER,
69                         CBusBindingConstants.THING_TYPE_TRIGGER);
70
71                 Network network = cbusNetworkHandler.getNetwork();
72                 if (network == null) {
73                     scanFinished();
74                     return;
75                 }
76                 for (Map.Entry<Integer, ThingTypeUID> applicationItem : applications.entrySet()) {
77                     Application application = network.getApplication(applicationItem.getKey());
78                     if (application == null) {
79                         continue;
80                     }
81                     ArrayList<Group> groups = application.getGroups(false);
82                     for (Group group : groups) {
83                         logger.debug("Found group: {} {} {}", application.getName(), group.getGroupID(),
84                                 group.getName());
85                         Map<String, Object> properties = new HashMap<>();
86                         properties.put(CBusBindingConstants.PROPERTY_APPLICATION_ID,
87                                 Integer.toString(applicationItem.getKey()));
88                         properties.put(CBusBindingConstants.CONFIG_GROUP_ID, Integer.toString(group.getGroupID()));
89                         properties.put(CBusBindingConstants.PROPERTY_GROUP_NAME, group.getName());
90                         properties.put(CBusBindingConstants.PROPERTY_NETWORK_ID,
91                                 Integer.toString(network.getNetworkID()));
92
93                         ThingUID uid = new ThingUID(applicationItem.getValue(), Integer.toString(group.getGroupID()),
94                                 bridgeUid.getId(), cbusNetworkHandler.getThing().getUID().getId());
95                         DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
96                                 .withLabel("CBUS " + group.getName() + "(" + group.getGroupID() + ")")
97                                 .withBridge(cbusNetworkHandler.getThing().getUID()).build();
98                         thingDiscovered(result);
99                     }
100                 }
101             } catch (CGateException e) {
102                 logger.debug("Failed to discover groups", e);
103             }
104         }
105         scanFinished();
106     }
107
108     private synchronized void scanFinished() {
109         stopScan();// this notifies the scan listener that the scan is finished
110         abortScan();// this clears the scheduled call to stopScan
111     }
112 }