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 static org.openhab.binding.homematic.internal.misc.HomematicConstants.*;
17 import java.util.ArrayList;
18 import java.util.List;
20 import org.apache.commons.lang.builder.EqualsBuilder;
21 import org.apache.commons.lang.builder.HashCodeBuilder;
22 import org.apache.commons.lang.builder.ToStringBuilder;
23 import org.apache.commons.lang.builder.ToStringStyle;
24 import org.openhab.binding.homematic.internal.misc.MiscUtils;
27 * Object that represents a Homematic device.
29 * @author Gerhard Riegler - Initial contribution
31 public class HmDevice {
32 public static final String TYPE_GATEWAY_EXTRAS = "GATEWAY-EXTRAS";
33 public static final String ADDRESS_GATEWAY_EXTRAS = "GWE00000000";
35 private final HmInterface hmInterface;
36 private final String address;
37 private final String type;
39 private final String firmware;
40 private final String gatewayId;
41 private final String homegearId;
43 private List<HmChannel> channels = new ArrayList<>();
45 public HmDevice(String address, HmInterface hmInterface, String type, String gatewayId, String homegearId,
47 this.address = address;
48 this.hmInterface = hmInterface;
50 this.gatewayId = gatewayId;
51 this.homegearId = homegearId;
52 this.firmware = firmware;
56 * Returns the address of the device.
58 public String getAddress() {
63 * Returns the interface of the device.
65 public HmInterface getHmInterface() {
70 * Returns the name of the device.
72 public String getName() {
77 * Sets the name of the device.
79 public void setName(String name) {
84 * Returns the type of the device.
86 public String getType() {
91 * Returns all channels of the device.
93 public List<HmChannel> getChannels() {
98 * Returns the firmware of the device.
100 public String getFirmware() {
105 * Returns the gatewayId of the device.
107 public String getGatewayId() {
112 * Returns the homegearId of the device.
114 public String getHomegearId() {
119 * Adds a channel to this device.
121 public void addChannel(HmChannel channel) {
122 channel.setDevice(this);
123 channels.add(channel);
127 * Returns the channel with the given channelNumber.
129 public HmChannel getChannel(int channelNumber) {
130 for (HmChannel hmChannel : channels) {
131 if (hmChannel.getNumber() == channelNumber) {
139 * Returns the number of datapoints.
141 public int getDatapointCount() {
143 for (HmChannel channel : channels) {
144 dpCounter += channel.getDatapoints().size();
150 * Returns true, if the device is the Homematic gateway.
152 public boolean isGatewayExtras() {
153 return ADDRESS_GATEWAY_EXTRAS.equals(address);
157 * Returns true, if the device can not be reached (offline).
159 public boolean isUnreach() {
160 return isStatusDatapointEnabled(DATAPOINT_NAME_UNREACH);
164 * Returns true, if the gateway has a config to transfer to the device.
166 public boolean isConfigPending() {
167 return isStatusDatapointEnabled(DATAPOINT_NAME_CONFIG_PENDING);
171 * Returns true, if the gateway has a update to transfer to the device.
173 public boolean isUpdatePending() {
174 return isStatusDatapointEnabled(DATAPOINT_NAME_UPDATE_PENDING);
178 * Returns true, if the device is in firmware update mode.
180 public boolean isFirmwareUpdating() {
181 return isStatusDatapointEnabled(DATAPOINT_NAME_DEVICE_IN_BOOTLOADER);
185 * Returns true, if the device is offline.
187 public boolean isOffline() {
188 return isFirmwareUpdating() || isUnreach();
191 private boolean isStatusDatapointEnabled(String datapointName) {
192 HmChannel channel = getChannel(0);
193 if (channel != null && channel.isInitialized()) {
194 HmDatapointInfo dpInfo = HmDatapointInfo.createValuesInfo(channel, datapointName);
195 HmDatapoint dp = channel.getDatapoint(dpInfo);
197 return MiscUtils.isTrueValue(dp.getValue());
204 public int hashCode() {
205 return new HashCodeBuilder().append(address).toHashCode();
209 public boolean equals(Object obj) {
210 if (obj == null || !(obj instanceof HmDevice)) {
213 HmDevice comp = (HmDevice) obj;
214 return new EqualsBuilder().append(address, comp.getAddress()).isEquals();
218 public String toString() {
219 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("hmInterface", hmInterface)
220 .append("address", address).append("type", type).append("name", name).append("firmware", firmware)
221 .append("gatewayId", gatewayId).toString();