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