]> git.basschouten.com Git - openhab-addons.git/blob
d31d4874d6a707fa59600a17930c891f8ba676f1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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 static org.openhab.binding.homematic.internal.misc.HomematicConstants.*;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
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;
25
26 /**
27  * Object that represents a Homematic device.
28  *
29  * @author Gerhard Riegler - Initial contribution
30  */
31 public class HmDevice {
32     public static final String TYPE_GATEWAY_EXTRAS = "GATEWAY-EXTRAS";
33     public static final String ADDRESS_GATEWAY_EXTRAS = "GWE00000000";
34
35     private final HmInterface hmInterface;
36     private final String address;
37     private final String type;
38     private String name;
39     private final String firmware;
40     private final String gatewayId;
41     private final String homegearId;
42
43     private List<HmChannel> channels = new ArrayList<>();
44
45     public HmDevice(String address, HmInterface hmInterface, String type, String gatewayId, String homegearId,
46             String firmware) {
47         this.address = address;
48         this.hmInterface = hmInterface;
49         this.type = type;
50         this.gatewayId = gatewayId;
51         this.homegearId = homegearId;
52         this.firmware = firmware;
53     }
54
55     /**
56      * Returns the address of the device.
57      */
58     public String getAddress() {
59         return address;
60     }
61
62     /**
63      * Returns the interface of the device.
64      */
65     public HmInterface getHmInterface() {
66         return hmInterface;
67     }
68
69     /**
70      * Returns the name of the device.
71      */
72     public String getName() {
73         return name;
74     }
75
76     /**
77      * Sets the name of the device.
78      */
79     public void setName(String name) {
80         this.name = name;
81     }
82
83     /**
84      * Returns the type of the device.
85      */
86     public String getType() {
87         return type;
88     }
89
90     /**
91      * Returns all channels of the device.
92      */
93     public List<HmChannel> getChannels() {
94         return channels;
95     }
96
97     /**
98      * Returns the firmware of the device.
99      */
100     public String getFirmware() {
101         return firmware;
102     }
103
104     /**
105      * Returns the gatewayId of the device.
106      */
107     public String getGatewayId() {
108         return gatewayId;
109     }
110
111     /**
112      * Returns the homegearId of the device.
113      */
114     public String getHomegearId() {
115         return homegearId;
116     }
117
118     /**
119      * Adds a channel to this device.
120      */
121     public void addChannel(HmChannel channel) {
122         channel.setDevice(this);
123         channels.add(channel);
124     }
125
126     /**
127      * Returns the channel with the given channelNumber.
128      */
129     public HmChannel getChannel(int channelNumber) {
130         for (HmChannel hmChannel : channels) {
131             if (hmChannel.getNumber() == channelNumber) {
132                 return hmChannel;
133             }
134         }
135         return null;
136     }
137
138     /**
139      * Returns the number of datapoints.
140      */
141     public int getDatapointCount() {
142         int dpCounter = 0;
143         for (HmChannel channel : channels) {
144             dpCounter += channel.getDatapoints().size();
145         }
146         return dpCounter;
147     }
148
149     /**
150      * Returns true, if the device is the Homematic gateway.
151      */
152     public boolean isGatewayExtras() {
153         return ADDRESS_GATEWAY_EXTRAS.equals(address);
154     }
155
156     /**
157      * Returns true, if the device can not be reached (offline).
158      */
159     public boolean isUnreach() {
160         return isStatusDatapointEnabled(DATAPOINT_NAME_UNREACH);
161     }
162
163     /**
164      * Returns true, if the gateway has a config to transfer to the device.
165      */
166     public boolean isConfigPending() {
167         return isStatusDatapointEnabled(DATAPOINT_NAME_CONFIG_PENDING);
168     }
169
170     /**
171      * Returns true, if the gateway has a update to transfer to the device.
172      */
173     public boolean isUpdatePending() {
174         return isStatusDatapointEnabled(DATAPOINT_NAME_UPDATE_PENDING);
175     }
176
177     /**
178      * Returns true, if the device is in firmware update mode.
179      */
180     public boolean isFirmwareUpdating() {
181         return isStatusDatapointEnabled(DATAPOINT_NAME_DEVICE_IN_BOOTLOADER);
182     }
183
184     /**
185      * Returns true, if the device is offline.
186      */
187     public boolean isOffline() {
188         return isFirmwareUpdating() || isUnreach();
189     }
190
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);
196             if (dp != null) {
197                 return MiscUtils.isTrueValue(dp.getValue());
198             }
199         }
200         return false;
201     }
202
203     @Override
204     public int hashCode() {
205         return new HashCodeBuilder().append(address).toHashCode();
206     }
207
208     @Override
209     public boolean equals(Object obj) {
210         if (obj == null || !(obj instanceof HmDevice)) {
211             return false;
212         }
213         HmDevice comp = (HmDevice) obj;
214         return new EqualsBuilder().append(address, comp.getAddress()).isEquals();
215     }
216
217     @Override
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();
222     }
223 }