]> git.basschouten.com Git - openhab-addons.git/blob
4214d73b558a11cba3a066b9e222a24378eb07b5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.homematic.internal.model;
14
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Map;
20
21 import org.apache.commons.lang.builder.ToStringBuilder;
22 import org.apache.commons.lang.builder.ToStringStyle;
23 import org.openhab.binding.homematic.internal.misc.HomematicConstants;
24
25 /**
26  * Object that represents a Homematic channel.
27  *
28  * @author Gerhard Riegler - Initial contribution
29  */
30 public class HmChannel {
31     public static final String TYPE_GATEWAY_EXTRAS = "GATEWAY-EXTRAS";
32     public static final String TYPE_GATEWAY_VARIABLE = "GATEWAY-VARIABLE";
33     public static final String TYPE_GATEWAY_SCRIPT = "GATEWAY-SCRIPT";
34
35     public static final Integer CHANNEL_NUMBER_EXTRAS = 0;
36     public static final Integer CHANNEL_NUMBER_VARIABLE = 1;
37     public static final Integer CHANNEL_NUMBER_SCRIPT = 2;
38
39     private final Integer number;
40     private final String type;
41     private HmDevice device;
42     private boolean initialized;
43     private Integer lastFunction;
44     private Map<HmDatapointInfo, HmDatapoint> datapoints = new HashMap<>();
45
46     public HmChannel(String type, Integer number) {
47         this.type = type;
48         this.number = number;
49     }
50
51     /**
52      * Returns the channel number.
53      */
54     public Integer getNumber() {
55         return number;
56     }
57
58     /**
59      * Returns the device of the channel.
60      */
61     public HmDevice getDevice() {
62         return device;
63     }
64
65     /**
66      * Sets the device of the channel.
67      */
68     public void setDevice(HmDevice device) {
69         this.device = device;
70     }
71
72     /**
73      * Sets the type of the channel.
74      */
75     public String getType() {
76         return type;
77     }
78
79     /**
80      * Sets the flag, if the values for all datapoints has been loaded.
81      *
82      * @param initialized
83      */
84     public void setInitialized(boolean initialized) {
85         this.initialized = initialized;
86     }
87
88     /**
89      * Returns true, if the values for all datapoints has been loaded.
90      */
91     public boolean isInitialized() {
92         return initialized;
93     }
94
95     /**
96      * Returns true, if the channel contains gateway scripts.
97      */
98     public boolean isGatewayScript() {
99         return device.isGatewayExtras() && TYPE_GATEWAY_SCRIPT.equals(type);
100     }
101
102     /**
103      * Returns true, if the channel contains gateway variables.
104      */
105     public boolean isGatewayVariable() {
106         return device.isGatewayExtras() && TYPE_GATEWAY_VARIABLE.equals(type);
107     }
108
109     /**
110      * Returns all datapoints.
111      */
112     public List<HmDatapoint> getDatapoints() {
113         synchronized (datapoints) {
114             return new ArrayList<>(datapoints.values());
115         }
116     }
117
118     /**
119      * Adds a datapoint to the channel.
120      */
121     public void addDatapoint(HmDatapoint dp) {
122         dp.setChannel(this);
123         synchronized (datapoints) {
124             datapoints.put(new HmDatapointInfo(dp), dp);
125         }
126     }
127
128     /**
129      * Removes all datapoints with VALUES param set type from the channel.
130      */
131     public void removeValueDatapoints() {
132         synchronized (datapoints) {
133             Iterator<Map.Entry<HmDatapointInfo, HmDatapoint>> iterator = datapoints.entrySet().iterator();
134             while (iterator.hasNext()) {
135                 if (iterator.next().getKey().getParamsetType() == HmParamsetType.VALUES) {
136                     iterator.remove();
137                 }
138             }
139         }
140     }
141
142     /**
143      * Returns the HmDatapoint with the given HmDatapointInfo.
144      */
145     public HmDatapoint getDatapoint(HmDatapointInfo dpInfo) {
146         synchronized (datapoints) {
147             return datapoints.get(dpInfo);
148         }
149     }
150
151     /**
152      * Returns the HmDatapoint with the given datapoint name.
153      */
154     public HmDatapoint getDatapoint(HmParamsetType type, String datapointName) {
155         return getDatapoint(new HmDatapointInfo(type, this, datapointName));
156     }
157
158     /**
159      * Returns true, if the channel has the given datapoint.
160      */
161     public boolean hasDatapoint(HmDatapointInfo dpInfo) {
162         return getDatapoint(dpInfo) != null;
163     }
164
165     /**
166      * Returns true, if the channel's datapoint set contains a
167      * channel function datapoint.
168      */
169     public boolean isReconfigurable() {
170         return getDatapoint(HmParamsetType.MASTER, HomematicConstants.DATAPOINT_NAME_CHANNEL_FUNCTION) != null;
171     }
172
173     /**
174      * Returns the numeric value of the function this channel is currently configured to.
175      * Returns null if the channel is not yet initialized or does not support dynamic reconfiguration.
176      */
177     public Integer getCurrentFunction() {
178         HmDatapoint functionDp = getDatapoint(HmParamsetType.MASTER,
179                 HomematicConstants.DATAPOINT_NAME_CHANNEL_FUNCTION);
180         return functionDp == null ? null : (Integer) functionDp.getValue();
181     }
182
183     /**
184      * Checks whether the function this channel is configured to changed since this method was last invoked.
185      * Returns false if the channel is not reconfigurable or was not initialized yet.
186      */
187     public synchronized boolean checkForChannelFunctionChange() {
188         Integer currentFunction = getCurrentFunction();
189         if (currentFunction == null) {
190             return false;
191         }
192         if (lastFunction == null) {
193             // We were called from initialization, which was preceded by initial metadata fetch, so everything
194             // should be fine by now
195             lastFunction = currentFunction;
196             return false;
197         }
198         if (lastFunction.equals(currentFunction)) {
199             return false;
200         }
201         lastFunction = currentFunction;
202         return true;
203     }
204
205     /**
206      * Returns true, if the channel has at least one PRESS_ datapoint.
207      */
208     public boolean hasPressDatapoint() {
209         for (HmDatapoint dp : getDatapoints()) {
210             if (dp.isPressDatapoint()) {
211                 return true;
212             }
213         }
214         return false;
215     }
216
217     @Override
218     public String toString() {
219         return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("number", number).append("type", type)
220                 .append("initialized", initialized).toString();
221     }
222 }