2 * Copyright (c) 2010-2021 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.homematic.internal.model;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.List;
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;
26 * Object that represents a Homematic channel.
28 * @author Gerhard Riegler - Initial contribution
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";
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;
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<>();
46 public HmChannel(String type, Integer number) {
52 * Returns the channel number.
54 public Integer getNumber() {
59 * Returns the device of the channel.
61 public HmDevice getDevice() {
66 * Sets the device of the channel.
68 public void setDevice(HmDevice device) {
73 * Sets the type of the channel.
75 public String getType() {
80 * Sets the flag, if the values for all datapoints has been loaded.
84 public void setInitialized(boolean initialized) {
85 this.initialized = initialized;
89 * Returns true, if the values for all datapoints has been loaded.
91 public boolean isInitialized() {
96 * Returns true, if the channel contains gateway scripts.
98 public boolean isGatewayScript() {
99 return device.isGatewayExtras() && TYPE_GATEWAY_SCRIPT.equals(type);
103 * Returns true, if the channel contains gateway variables.
105 public boolean isGatewayVariable() {
106 return device.isGatewayExtras() && TYPE_GATEWAY_VARIABLE.equals(type);
110 * Returns all datapoints.
112 public List<HmDatapoint> getDatapoints() {
113 synchronized (datapoints) {
114 return new ArrayList<>(datapoints.values());
119 * Adds a datapoint to the channel.
121 public void addDatapoint(HmDatapoint dp) {
123 synchronized (datapoints) {
124 datapoints.put(new HmDatapointInfo(dp), dp);
129 * Removes all datapoints with VALUES param set type from the channel.
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) {
143 * Returns the HmDatapoint with the given HmDatapointInfo.
145 public HmDatapoint getDatapoint(HmDatapointInfo dpInfo) {
146 synchronized (datapoints) {
147 return datapoints.get(dpInfo);
152 * Returns the HmDatapoint with the given datapoint name.
154 public HmDatapoint getDatapoint(HmParamsetType type, String datapointName) {
155 return getDatapoint(new HmDatapointInfo(type, this, datapointName));
159 * Returns true, if the channel has the given datapoint.
161 public boolean hasDatapoint(HmDatapointInfo dpInfo) {
162 return getDatapoint(dpInfo) != null;
166 * Returns true, if the channel's datapoint set contains a
167 * channel function datapoint.
169 public boolean isReconfigurable() {
170 return getDatapoint(HmParamsetType.MASTER, HomematicConstants.DATAPOINT_NAME_CHANNEL_FUNCTION) != null;
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.
177 public Integer getCurrentFunction() {
178 HmDatapoint functionDp = getDatapoint(HmParamsetType.MASTER,
179 HomematicConstants.DATAPOINT_NAME_CHANNEL_FUNCTION);
180 return functionDp == null ? null : (Integer) functionDp.getValue();
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.
187 public synchronized boolean checkForChannelFunctionChange() {
188 Integer currentFunction = getCurrentFunction();
189 if (currentFunction == null) {
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;
198 if (lastFunction.equals(currentFunction)) {
201 lastFunction = currentFunction;
206 * Returns true, if the channel has at least one PRESS_ datapoint.
208 public boolean hasPressDatapoint() {
209 for (HmDatapoint dp : getDatapoints()) {
210 if (dp.isPressDatapoint()) {
218 public String toString() {
219 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("number", number).append("type", type)
220 .append("initialized", initialized).toString();