]> git.basschouten.com Git - openhab-addons.git/blob
ed4c9bfd60cf18c01deddf9ebcede6356783b9b3
[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.handler;
14
15 import java.util.Map;
16
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;
31
32 import com.daveoxley.cbus.Application;
33 import com.daveoxley.cbus.CGateException;
34 import com.daveoxley.cbus.Group;
35 import com.daveoxley.cbus.Network;
36
37 /**
38  * The {@link CBusGroupHandler} is responsible for handling commands, which are
39  * sent to one of the channels.
40  *
41  * @author Scott Linton - Initial contribution
42  */
43
44 @NonNullByDefault
45 public abstract class CBusGroupHandler extends BaseThingHandler {
46
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;
52
53     public CBusGroupHandler(Thing thing, int applicationId) {
54         super(thing);
55         this.applicationId = applicationId;
56     }
57
58     @Override
59     public abstract void handleCommand(ChannelUID channelUID, Command command);
60
61     @Override
62     public void initialize() {
63         /*
64          * Cast to Nullable in map to avoid compiler warnings
65          */
66         CBusGroupConfiguration configuration = getConfigAs(CBusGroupConfiguration.class);
67         logger.debug("Using configuration {}", configuration);
68         groupId = configuration.group;
69
70         CBusNetworkHandler cBusNetworkHandler = getCBusNetworkHandler();
71         this.cBusNetworkHandler = cBusNetworkHandler;
72         if (cBusNetworkHandler == null) {
73             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
74                     "No CBusNetworkHandler Bridge available");
75             return;
76         }
77         updateStatus();
78
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);
84     }
85
86     public void updateStatus() {
87         try {
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);
92             } else {
93                 Group group = this.group;
94                 if (group == null) {
95                     group = getGroup();
96                     this.group = group;
97                 }
98                 if (group == null) {
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);
104
105                     try {
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
111                     }
112                 } else {
113                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
114                             "Network is not reporting online");
115                 }
116             }
117         } catch (CGateException e) {
118             logger.debug("Problem checking network state for network {}", e.getMessage());
119             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
120         }
121     }
122
123     public abstract void updateGroup(int application, int group, String value);
124
125     private @Nullable Group getGroup() {
126         try {
127             CBusNetworkHandler networkHandler = cBusNetworkHandler;
128             if (networkHandler == null) {
129                 return null;
130             }
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);
136                     return null;
137                 }
138                 logger.debug("GetGroup for {}/id {}", applicationId, groupId);
139                 return application.getGroup(groupId);
140             }
141         } catch (CGateException e) {
142             logger.debug("GetGroup for id {}/{} failed {}", applicationId, groupId, e.getMessage());
143         }
144         return null;
145     }
146
147     private @Nullable CBusNetworkHandler getCBusNetworkHandler() {
148         Bridge bridge = getBridge();
149         if (bridge == null) {
150             logger.debug("Required bridge not defined for device .");
151             return null;
152         }
153         ThingHandler handler = bridge.getHandler();
154         if (handler instanceof CBusNetworkHandler) {
155             return (CBusNetworkHandler) handler;
156         }
157         logger.debug("No available bridge handler found for bridge: {}", bridge.getUID());
158         return null;
159     }
160 }