2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.innogysmarthome.internal.client.entity.device;
15 import static org.openhab.binding.innogysmarthome.internal.InnogyBindingConstants.*;
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;
23 import com.google.gson.annotations.SerializedName;
26 * Defines the structure of a {@link Device}.
28 * @author Oliver Kuhl - Initial contribution
32 public static final String DEVICE_MANUFACTURER_RWE = "RWE";
33 public static final String DEVICE_MANUFACTURER_INNOGY = "innogy";
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";
40 * Unique id for the device, always available in model.
45 * Identifier of the manufacturer, always available in model
47 private String manufacturer;
50 * Version number of the device for the domain model.
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.
56 private String version;
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.
65 private String product;
68 * Device number or id like SGTIN given by the manufacturer. Optional.
70 private String serialnumber;
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.
79 private DeviceConfig config;
81 private List<String> capabilities;
83 private Map<String, Capability> capabilityMap;
85 private DeviceState deviceState;
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.
95 // private List<Property> tagList;
98 * The location contains the link to the location of the device. Optional.
100 @SerializedName("location")
101 private String locationLink;
103 private transient Location location;
105 private List<Message> messageList;
107 private boolean lowBattery;
110 * Stores, if the {@link Device} is battery powered.
112 private boolean batteryPowered = false;
117 public String getId() {
122 * @param id the id to set
124 public void setId(String id) {
129 * @return the manufacturer
131 public String getManufacturer() {
136 * @param manufacturer the manufacturer to set
138 public void setManufacturer(String manufacturer) {
139 this.manufacturer = manufacturer;
143 * @return the version
145 public String getVersion() {
150 * @param version the version to set
152 public void setVersion(String version) {
153 this.version = version;
157 * @return the product
159 public String getProduct() {
164 * @param product the product to set
166 public void setProduct(String product) {
167 this.product = product;
171 * @return the serialnumber
173 public String getSerialnumber() {
178 * @param serialnumber the serialnumber to set
180 public void setSerialnumber(String serialnumber) {
181 this.serialnumber = serialnumber;
185 * Returns true, if the {@link Device} has a serial number.
189 public boolean hasSerialNumber() {
190 return (serialnumber != null && serialnumber != "");
196 public String getType() {
201 * @param type the type to set
203 public void setType(String type) {
210 public DeviceConfig getConfig() {
215 * @param config the config to set
217 public void setConfig(DeviceConfig config) {
218 this.config = config;
222 * Returns the {@link DeviceState}. Only available, if device has a state. Better check with
223 * {@link Device#hasDeviceState()} first!
225 * @return the entityState or null
227 public DeviceState getDeviceState() {
232 * @param deviceState the deviceState to set
234 public void setDeviceState(DeviceState deviceState) {
235 this.deviceState = deviceState;
239 * Returns, if the {@link Device} has a state. Not all {@link Device}s have a state.
243 public boolean hasDeviceState() {
244 return deviceState != null;
248 * @return the capabilityList
250 public List<String> getCapabilities() {
251 return Objects.requireNonNullElse(capabilities, Collections.emptyList());
255 * @param capabilityList the capabilityList to set
257 public void setCapabilities(List<String> capabilityList) {
258 this.capabilities = capabilityList;
262 * @param capabilityMap the capabilityMap to set
264 public void setCapabilityMap(Map<String, Capability> capabilityMap) {
265 this.capabilityMap = capabilityMap;
269 * @return the capabilityMap
271 public Map<String, Capability> getCapabilityMap() {
272 return this.capabilityMap;
276 * Returns the {@link Capability} with the given id.
281 public Capability getCapabilityWithId(String id) {
282 return this.capabilityMap.get(id);
286 * @return the locationLink
288 public String getLocationLink() {
293 * @param locationLink the locationList to set
295 public void setLocation(String locationLink) {
296 this.locationLink = locationLink;
300 * Returns the id of the {@link Location}
304 public String getLocationId() {
305 if (locationLink != null) {
306 return locationLink.replace("/location/", "");
312 * Returns the {@link Location} of the {@link Device}. Better check with {@link Device#hasLocation()} first, as not
313 * all devices have one.
315 * @return the location
317 public Location getLocation() {
322 * @param location the location to set
324 public void setLocation(Location location) {
325 this.location = location;
329 * Returns, if the {@link Device} has a {@link Location}. Not all devices have a {@link Location}...
331 * @return boolean true, if a {@link Location} is set, else false
333 public boolean hasLocation() {
334 return location != null;
338 * @return the messageList
340 public List<Message> getMessageList() {
345 * @param messageList the messageList to set
347 public void setMessageList(List<Message> messageList) {
348 this.messageList = messageList;
349 applyMessageList(messageList);
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;
361 case Message.TYPE_DEVICE_LOW_BATTERY:
362 isLowBatteryMessageFound = true;
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)
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)
378 * Sets if the {@link Device} is reachable;
382 private void setReachable(boolean isReachable) {
383 if (getDeviceState().hasIsReachableState()) {
384 getDeviceState().setReachable(isReachable);
389 * Returns if the {@link Device} is reachable.
393 public boolean isReachable() {
394 return getDeviceState().getState().getIsReachable().getValue();
398 * Sets the low battery state for the {@link Device}.
400 * @param hasLowBattery
402 private void setLowBattery(boolean hasLowBattery) {
403 this.lowBattery = hasLowBattery;
407 * Returns true if the {@link Device} has a low battery warning. Only available on battery devices.
411 public boolean hasLowBattery() {
416 * Returns true, if the {@link Device} is battery powered.
420 public boolean isBatteryPowered() {
421 return batteryPowered;
425 * Sets if the device is battery powered.
429 public void setIsBatteryPowered(boolean hasBattery) {
430 batteryPowered = hasBattery;
434 * Returns true, if the {@link Device} has {@link Message}s.
438 public boolean hasMessages() {
439 return (messageList != null && !messageList.isEmpty());
443 * Returns true if the device is a controller (SHC).
447 public boolean isController() {
448 return DEVICE_SHC.equals(type) || DEVICE_SHCA.equals(type);
452 * Returns true, if the device is made by RWE.
456 public boolean isRWEDevice() {
457 return DEVICE_MANUFACTURER_RWE.equals(manufacturer);
461 * Returns true, if the device is made by innogy.
465 public boolean isInnogyDevice() {
466 return DEVICE_MANUFACTURER_INNOGY.equals(manufacturer);
470 * Returns true, if the {@link Device} is a virtual device (e.g. a VariableActuator).
474 public boolean isVirtualDevice() {
475 return PROTOCOL_ID_VIRTUAL.equals(getConfig().getProtocolId());
479 * Returns true, if the {@link Device} is a radio device.
483 public boolean isRadioDevice() {
484 return PROTOCOL_ID_COSIP.equals(getConfig().getProtocolId())
485 || PROTOCOL_ID_WMBUS.equals(getConfig().getProtocolId());
489 * Returns true, if the {@link Device} is a CoSip device.
493 public boolean isCoSipDevice() {
494 return PROTOCOL_ID_COSIP.equals(getConfig().getProtocolId());
498 * Returns true, if the {@link Device} is a W-Mbus device.
502 public boolean isWMBusDevice() {
503 return PROTOCOL_ID_WMBUS.equals(getConfig().getProtocolId());
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() + "]";