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