]> git.basschouten.com Git - openhab-addons.git/blob
8a0f2014e111f055796cfa4f4465be160b2d0008
[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.innogysmarthome.internal.client.entity.device;
14
15 import static org.openhab.binding.innogysmarthome.internal.InnogyBindingConstants.*;
16
17 import java.util.*;
18
19 import org.openhab.binding.innogysmarthome.internal.client.entity.capability.Capability;
20 import org.openhab.binding.innogysmarthome.internal.client.entity.location.Location;
21 import org.openhab.binding.innogysmarthome.internal.client.entity.message.Message;
22
23 import com.google.gson.annotations.SerializedName;
24
25 /**
26  * Defines the structure of a {@link Device}.
27  *
28  * @author Oliver Kuhl - Initial contribution
29  */
30 public class Device {
31
32     public static final String DEVICE_MANUFACTURER_RWE = "RWE";
33     public static final String DEVICE_MANUFACTURER_INNOGY = "innogy";
34
35     protected static final String PROTOCOL_ID_COSIP = "Cosip";
36     protected static final String PROTOCOL_ID_VIRTUAL = "Virtual";
37     protected static final String PROTOCOL_ID_WMBUS = "wMBus";
38
39     /**
40      * Unique id for the device, always available in model.
41      */
42     private String id;
43
44     /**
45      * Identifier of the manufacturer, always available in model
46      */
47     private String manufacturer;
48
49     /**
50      * Version number of the device for the domain model.
51      *
52      * If the functionality of the device changes, the version must
53      * be increased to indicate that there are new or changed attributes
54      * of the device. Always available in model.
55      */
56     private String version;
57
58     /**
59      * Defines the product, which is used as an identifier for selecting the
60      * right add-in to support the functionality of the device.
61      * Remark: one add-in can support multiple devices, e.g.
62      * core.RWE, which supports all RWE hardware devices (also referred to as core devices).
63      * Always available in model.
64      */
65     private String product;
66
67     /**
68      * Device number or id like SGTIN given by the manufacturer. Optional.
69      */
70     private String serialnumber;
71
72     /**
73      * Specifies the type of the device, which is defined by the manufacturer. The triple of device type, manufacturer
74      * and the version must be unique.
75      * Always available in model.
76      */
77     private String type;
78
79     private DeviceConfig config;
80
81     private List<String> capabilities;
82
83     private Map<String, Capability> capabilityMap;
84
85     private DeviceState deviceState;
86
87     /**
88      * The tag container can contain any number of properties for grouping of the devices in the client, e.g. category
89      * of device like “security related”. The tags can be freely chosen by the client and will not be considered by the
90      * system for any business logic.
91      *
92      * Optional.
93      */
94     // @Key("tags")
95     // private List<Property> tagList;
96
97     /**
98      * The location contains the link to the location of the device. Optional.
99      */
100     @SerializedName("location")
101     private String locationLink;
102
103     private transient Location location;
104
105     private List<Message> messageList;
106
107     private boolean lowBattery;
108
109     /**
110      * Stores, if the {@link Device} is battery powered.
111      */
112     private boolean batteryPowered = false;
113
114     /**
115      * @return the id
116      */
117     public String getId() {
118         return id;
119     }
120
121     /**
122      * @param id the id to set
123      */
124     public void setId(String id) {
125         this.id = id;
126     }
127
128     /**
129      * @return the manufacturer
130      */
131     public String getManufacturer() {
132         return manufacturer;
133     }
134
135     /**
136      * @param manufacturer the manufacturer to set
137      */
138     public void setManufacturer(String manufacturer) {
139         this.manufacturer = manufacturer;
140     }
141
142     /**
143      * @return the version
144      */
145     public String getVersion() {
146         return version;
147     }
148
149     /**
150      * @param version the version to set
151      */
152     public void setVersion(String version) {
153         this.version = version;
154     }
155
156     /**
157      * @return the product
158      */
159     public String getProduct() {
160         return product;
161     }
162
163     /**
164      * @param product the product to set
165      */
166     public void setProduct(String product) {
167         this.product = product;
168     }
169
170     /**
171      * @return the serialnumber
172      */
173     public String getSerialnumber() {
174         return serialnumber;
175     }
176
177     /**
178      * @param serialnumber the serialnumber to set
179      */
180     public void setSerialnumber(String serialnumber) {
181         this.serialnumber = serialnumber;
182     }
183
184     /**
185      * Returns true, if the {@link Device} has a serial number.
186      *
187      * @return
188      */
189     public boolean hasSerialNumber() {
190         return (serialnumber != null && serialnumber != "");
191     }
192
193     /**
194      * @return the type
195      */
196     public String getType() {
197         return type;
198     }
199
200     /**
201      * @param type the type to set
202      */
203     public void setType(String type) {
204         this.type = type;
205     }
206
207     /**
208      * @return the config
209      */
210     public DeviceConfig getConfig() {
211         return config;
212     }
213
214     /**
215      * @param config the config to set
216      */
217     public void setConfig(DeviceConfig config) {
218         this.config = config;
219     }
220
221     /**
222      * Returns the {@link DeviceState}. Only available, if device has a state. Better check with
223      * {@link Device#hasDeviceState()} first!
224      *
225      * @return the entityState or null
226      */
227     public DeviceState getDeviceState() {
228         return deviceState;
229     }
230
231     /**
232      * @param deviceState the deviceState to set
233      */
234     public void setDeviceState(DeviceState deviceState) {
235         this.deviceState = deviceState;
236     }
237
238     /**
239      * Returns, if the {@link Device} has a state. Not all {@link Device}s have a state.
240      *
241      * @return
242      */
243     public boolean hasDeviceState() {
244         return deviceState != null;
245     }
246
247     /**
248      * @return the capabilityList
249      */
250     public List<String> getCapabilities() {
251         return Objects.requireNonNullElse(capabilities, Collections.emptyList());
252     }
253
254     /**
255      * @param capabilityList the capabilityList to set
256      */
257     public void setCapabilities(List<String> capabilityList) {
258         this.capabilities = capabilityList;
259     }
260
261     /**
262      * @param capabilityMap the capabilityMap to set
263      */
264     public void setCapabilityMap(Map<String, Capability> capabilityMap) {
265         this.capabilityMap = capabilityMap;
266     }
267
268     /**
269      * @return the capabilityMap
270      */
271     public Map<String, Capability> getCapabilityMap() {
272         return this.capabilityMap;
273     }
274
275     /**
276      * Returns the {@link Capability} with the given id.
277      *
278      * @param id
279      * @return
280      */
281     public Capability getCapabilityWithId(String id) {
282         return this.capabilityMap.get(id);
283     }
284
285     /**
286      * @return the locationLink
287      */
288     public String getLocationLink() {
289         return locationLink;
290     }
291
292     /**
293      * @param locationLink the locationList to set
294      */
295     public void setLocation(String locationLink) {
296         this.locationLink = locationLink;
297     }
298
299     /**
300      * Returns the id of the {@link Location}
301      *
302      * @return
303      */
304     public String getLocationId() {
305         if (locationLink != null) {
306             return locationLink.replace("/location/", "");
307         }
308         return null;
309     }
310
311     /**
312      * Returns the {@link Location} of the {@link Device}. Better check with {@link Device#hasLocation()} first, as not
313      * all devices have one.
314      *
315      * @return the location
316      */
317     public Location getLocation() {
318         return location;
319     }
320
321     /**
322      * @param location the location to set
323      */
324     public void setLocation(Location location) {
325         this.location = location;
326     }
327
328     /**
329      * Returns, if the {@link Device} has a {@link Location}. Not all devices have a {@link Location}...
330      *
331      * @return boolean true, if a {@link Location} is set, else false
332      */
333     public boolean hasLocation() {
334         return location != null;
335     }
336
337     /**
338      * @return the messageList
339      */
340     public List<Message> getMessageList() {
341         return messageList;
342     }
343
344     /**
345      * @param messageList the messageList to set
346      */
347     public void setMessageList(List<Message> messageList) {
348         this.messageList = messageList;
349         applyMessageList(messageList);
350     }
351
352     private void applyMessageList(List<Message> messageList) {
353         if (messageList != null && !messageList.isEmpty()) {
354             boolean isUnreachableMessageFound = false;
355             boolean isLowBatteryMessageFound = false;
356             for (final Message message : messageList) {
357                 switch (message.getType()) {
358                     case Message.TYPE_DEVICE_UNREACHABLE:
359                         isUnreachableMessageFound = true;
360                         break;
361                     case Message.TYPE_DEVICE_LOW_BATTERY:
362                         isLowBatteryMessageFound = true;
363                         break;
364                 }
365             }
366             if (isUnreachableMessageFound) {
367                 setReachable(false); // overwrite only when there is a corresponding message (to keep the state of the
368                                      // API in other cases)
369             }
370             if (isLowBatteryMessageFound) {
371                 setLowBattery(true); // overwrite only when there is a corresponding message (to keep the state of the
372                                      // API in other cases)
373             }
374         }
375     }
376
377     /**
378      * Sets if the {@link Device} is reachable;
379      *
380      * @param isReachable
381      */
382     private void setReachable(boolean isReachable) {
383         if (getDeviceState().hasIsReachableState()) {
384             getDeviceState().setReachable(isReachable);
385         }
386     }
387
388     /**
389      * Returns if the {@link Device} is reachable.
390      *
391      * @return
392      */
393     public boolean isReachable() {
394         return getDeviceState().getState().getIsReachable().getValue();
395     }
396
397     /**
398      * Sets the low battery state for the {@link Device}.
399      *
400      * @param hasLowBattery
401      */
402     private void setLowBattery(boolean hasLowBattery) {
403         this.lowBattery = hasLowBattery;
404     }
405
406     /**
407      * Returns true if the {@link Device} has a low battery warning. Only available on battery devices.
408      *
409      * @return
410      */
411     public boolean hasLowBattery() {
412         return lowBattery;
413     }
414
415     /**
416      * Returns true, if the {@link Device} is battery powered.
417      *
418      * @return
419      */
420     public boolean isBatteryPowered() {
421         return batteryPowered;
422     }
423
424     /**
425      * Sets if the device is battery powered.
426      *
427      * @param hasBattery
428      */
429     public void setIsBatteryPowered(boolean hasBattery) {
430         batteryPowered = hasBattery;
431     }
432
433     /**
434      * Returns true, if the {@link Device} has {@link Message}s.
435      *
436      * @return
437      */
438     public boolean hasMessages() {
439         return (messageList != null && !messageList.isEmpty());
440     }
441
442     /**
443      * Returns true if the device is a controller (SHC).
444      *
445      * @return
446      */
447     public boolean isController() {
448         return DEVICE_SHC.equals(type) || DEVICE_SHCA.equals(type);
449     }
450
451     /**
452      * Returns true, if the device is made by RWE.
453      *
454      * @return
455      */
456     public boolean isRWEDevice() {
457         return DEVICE_MANUFACTURER_RWE.equals(manufacturer);
458     }
459
460     /**
461      * Returns true, if the device is made by innogy.
462      *
463      * @return
464      */
465     public boolean isInnogyDevice() {
466         return DEVICE_MANUFACTURER_INNOGY.equals(manufacturer);
467     }
468
469     /**
470      * Returns true, if the {@link Device} is a virtual device (e.g. a VariableActuator).
471      *
472      * @return
473      */
474     public boolean isVirtualDevice() {
475         return PROTOCOL_ID_VIRTUAL.equals(getConfig().getProtocolId());
476     }
477
478     /**
479      * Returns true, if the {@link Device} is a radio device.
480      *
481      * @return
482      */
483     public boolean isRadioDevice() {
484         return PROTOCOL_ID_COSIP.equals(getConfig().getProtocolId())
485                 || PROTOCOL_ID_WMBUS.equals(getConfig().getProtocolId());
486     }
487
488     /**
489      * Returns true, if the {@link Device} is a CoSip device.
490      *
491      * @return
492      */
493     public boolean isCoSipDevice() {
494         return PROTOCOL_ID_COSIP.equals(getConfig().getProtocolId());
495     }
496
497     /**
498      * Returns true, if the {@link Device} is a W-Mbus device.
499      *
500      * @return
501      */
502     public boolean isWMBusDevice() {
503         return PROTOCOL_ID_WMBUS.equals(getConfig().getProtocolId());
504     }
505
506     @Override
507     public String toString() {
508         final String string = "Device [" + "id=" + getId() + " manufacturer=" + getManufacturer() + " version="
509                 + getVersion() + " product=" + getProduct() + " serialnumber=" + getSerialnumber() + " type="
510                 + getType() + " name=" + getConfig().getName() + "]";
511         return string;
512     }
513 }