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.handler;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.cbus.CBusBindingConstants;
20 import org.openhab.binding.cbus.internal.CBusGroupConfiguration;
21 import org.openhab.core.thing.Bridge;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.thing.ThingStatusDetail;
26 import org.openhab.core.thing.binding.BaseThingHandler;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.openhab.core.types.Command;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import com.daveoxley.cbus.Application;
33 import com.daveoxley.cbus.CGateException;
34 import com.daveoxley.cbus.Group;
35 import com.daveoxley.cbus.Network;
38 * The {@link CBusGroupHandler} is responsible for handling commands, which are
39 * sent to one of the channels.
41 * @author Scott Linton - Initial contribution
45 public abstract class CBusGroupHandler extends BaseThingHandler {
47 private final Logger logger = LoggerFactory.getLogger(CBusGroupHandler.class);
48 protected @Nullable CBusNetworkHandler cBusNetworkHandler;
49 protected @Nullable Group group;
50 protected int applicationId = -1;
51 protected int groupId = -1;
53 public CBusGroupHandler(Thing thing, int applicationId) {
55 this.applicationId = applicationId;
59 public abstract void handleCommand(ChannelUID channelUID, Command command);
62 public void initialize() {
64 * Cast to Nullable in map to avoid compiler warnings
66 CBusGroupConfiguration configuration = getConfigAs(CBusGroupConfiguration.class);
67 logger.debug("Using configuration {}", configuration);
68 groupId = configuration.group;
70 CBusNetworkHandler cBusNetworkHandler = getCBusNetworkHandler();
71 this.cBusNetworkHandler = cBusNetworkHandler;
72 if (cBusNetworkHandler == null) {
73 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
74 "No CBusNetworkHandler Bridge available");
79 Map<String, String> updatedProperties = editProperties();
80 updatedProperties.put(CBusBindingConstants.PROPERTY_APPLICATION_ID, Integer.toString(applicationId));
81 updatedProperties.put(CBusBindingConstants.PROPERTY_NETWORK_ID,
82 Integer.toString(cBusNetworkHandler.getNetworkId()));
83 updateProperties(updatedProperties);
86 public void updateStatus() {
88 logger.debug("updateStatus UID: {} applicaton: {} group: {}", getThing().getUID(), applicationId, groupId);
89 CBusNetworkHandler networkHandler = cBusNetworkHandler;
90 if (networkHandler == null || !networkHandler.getThing().getStatus().equals(ThingStatus.ONLINE)) {
91 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
93 Group group = this.group;
99 logger.debug("Set state to configuration error -no group");
100 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
101 "No Group object available");
102 } else if (group.getNetwork().isOnline()) {
103 updateStatus(ThingStatus.ONLINE);
106 Map<String, String> updatedProperties = editProperties();
107 updatedProperties.put(CBusBindingConstants.PROPERTY_GROUP_NAME, group.getName());
108 updateProperties(updatedProperties);
109 } catch (CGateException ignore) {
110 // Cant get name so properties wont be updated
113 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
114 "Network is not reporting online");
117 } catch (CGateException e) {
118 logger.debug("Problem checking network state for network {}", e.getMessage());
119 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
123 public abstract void updateGroup(int application, int group, String value);
125 private @Nullable Group getGroup() {
127 CBusNetworkHandler networkHandler = cBusNetworkHandler;
128 if (networkHandler == null) {
131 Network network = networkHandler.getNetwork();
132 if (network != null) {
133 Application application = network.getApplication(applicationId);
134 if (application == null) {
135 logger.debug("getGroup() Cant get application for id {}", applicationId);
138 logger.debug("GetGroup for {}/id {}", applicationId, groupId);
139 return application.getGroup(groupId);
141 } catch (CGateException e) {
142 logger.debug("GetGroup for id {}/{} failed {}", applicationId, groupId, e.getMessage());
147 private @Nullable CBusNetworkHandler getCBusNetworkHandler() {
148 Bridge bridge = getBridge();
149 if (bridge == null) {
150 logger.debug("Required bridge not defined for device .");
153 ThingHandler handler = bridge.getHandler();
154 if (handler instanceof CBusNetworkHandler) {
155 return (CBusNetworkHandler) handler;
157 logger.debug("No available bridge handler found for bridge: {}", bridge.getUID());