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