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