2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.cbus.internal.discovery;
15 import java.util.ArrayList;
16 import java.util.HashMap;
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;
31 import com.daveoxley.cbus.Application;
32 import com.daveoxley.cbus.CGateException;
33 import com.daveoxley.cbus.Group;
34 import com.daveoxley.cbus.Network;
37 * The {@link CBusGroupDiscovery} class is used to discover CBus
38 * groups that are in the CBus Network
40 * @author Scott Linton - Initial contribution
43 public class CBusGroupDiscovery extends AbstractDiscoveryService {
45 private final Logger logger = LoggerFactory.getLogger(CBusGroupDiscovery.class);
47 private final CBusNetworkHandler cbusNetworkHandler;
49 public CBusGroupDiscovery(CBusNetworkHandler cbusNetworkHandler) {
50 super(CBusBindingConstants.SUPPORTED_THING_TYPES_UIDS, 30, false);
51 this.cbusNetworkHandler = cbusNetworkHandler;
55 protected synchronized void startScan() {
56 if (cbusNetworkHandler.getThing().getStatus().equals(ThingStatus.ONLINE)) {
57 ThingUID bridgeUid = cbusNetworkHandler.getThing().getBridgeUID();
58 if (bridgeUid == null) {
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);
71 Network network = cbusNetworkHandler.getNetwork();
72 if (network == null) {
76 for (Map.Entry<Integer, ThingTypeUID> applicationItem : applications.entrySet()) {
77 Application application = network.getApplication(applicationItem.getKey());
78 if (application == null) {
81 ArrayList<Group> groups = application.getGroups(false);
82 for (Group group : groups) {
83 logger.debug("Found group: {} {} {}", application.getName(), group.getGroupID(),
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()));
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);
101 } catch (CGateException e) {
102 logger.debug("Failed to discover groups", e);
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