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;
19 import java.util.Objects;
21 import org.openhab.binding.homematic.internal.misc.MiscUtils;
24 * Object that represents a Homematic device.
26 * @author Gerhard Riegler - Initial contribution
28 public class HmDevice {
29 public static final String TYPE_GATEWAY_EXTRAS = "GATEWAY-EXTRAS";
30 public static final String ADDRESS_GATEWAY_EXTRAS = "GWE00000000";
32 private final HmInterface hmInterface;
33 private final String address;
34 private final String type;
36 private final String firmware;
37 private final String gatewayId;
38 private final String homegearId;
40 private List<HmChannel> channels = new ArrayList<>();
42 public HmDevice(String address, HmInterface hmInterface, String type, String gatewayId, String homegearId,
44 this.address = address;
45 this.hmInterface = hmInterface;
47 this.gatewayId = gatewayId;
48 this.homegearId = homegearId;
49 this.firmware = firmware;
53 * Returns the address of the device.
55 public String getAddress() {
60 * Returns the interface of the device.
62 public HmInterface getHmInterface() {
67 * Returns the name of the device.
69 public String getName() {
74 * Sets the name of the device.
76 public void setName(String name) {
81 * Returns the type of the device.
83 public String getType() {
88 * Returns all channels of the device.
90 public List<HmChannel> getChannels() {
95 * Returns the firmware of the device.
97 public String getFirmware() {
102 * Returns the gatewayId of the device.
104 public String getGatewayId() {
109 * Returns the homegearId of the device.
111 public String getHomegearId() {
116 * Adds a channel to this device.
118 public void addChannel(HmChannel channel) {
119 channel.setDevice(this);
120 channels.add(channel);
124 * Returns the channel with the given channelNumber.
126 public HmChannel getChannel(int channelNumber) {
127 for (HmChannel hmChannel : channels) {
128 if (hmChannel.getNumber() == channelNumber) {
136 * Returns the number of datapoints.
138 public int getDatapointCount() {
140 for (HmChannel channel : channels) {
141 dpCounter += channel.getDatapoints().size();
147 * Returns true, if the device is the Homematic gateway.
149 public boolean isGatewayExtras() {
150 return ADDRESS_GATEWAY_EXTRAS.equals(address);
154 * Returns true, if the device can not be reached (offline).
156 public boolean isUnreach() {
157 return isStatusDatapointEnabled(DATAPOINT_NAME_UNREACH);
161 * Returns true, if the gateway has a config to transfer to the device.
163 public boolean isConfigPending() {
164 return isStatusDatapointEnabled(DATAPOINT_NAME_CONFIG_PENDING);
168 * Returns true, if the gateway has a update to transfer to the device.
170 public boolean isUpdatePending() {
171 return isStatusDatapointEnabled(DATAPOINT_NAME_UPDATE_PENDING);
175 * Returns true, if the device is in firmware update mode.
177 public boolean isFirmwareUpdating() {
178 return isStatusDatapointEnabled(DATAPOINT_NAME_DEVICE_IN_BOOTLOADER);
182 * Returns true, if the device is offline.
184 public boolean isOffline() {
185 return isFirmwareUpdating() || isUnreach();
188 private boolean isStatusDatapointEnabled(String datapointName) {
189 HmChannel channel = getChannel(0);
190 if (channel != null && channel.isInitialized()) {
191 HmDatapointInfo dpInfo = HmDatapointInfo.createValuesInfo(channel, datapointName);
192 HmDatapoint dp = channel.getDatapoint(dpInfo);
194 return MiscUtils.isTrueValue(dp.getValue());
201 public int hashCode() {
202 return Objects.hash(address);
206 public boolean equals(Object obj) {
207 if (obj == null || !(obj instanceof HmDevice)) {
210 HmDevice comp = (HmDevice) obj;
211 return Objects.equals(address, comp.getAddress());
215 public String toString() {
216 return String.format("%s[hmInterface=%s,address=%s,type=%s,name=%s,firmware=%s,gatewayId=%s]",
217 getClass().getSimpleName(), hmInterface, address, type, name, firmware, gatewayId);