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