2 * Copyright (c) 2010-2023 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.digitalstrom.internal.lib.structure.devices;
15 import org.openhab.binding.digitalstrom.internal.lib.listener.DeviceStatusListener;
16 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.constants.JSONApiResponseKeysEnum;
17 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.ChangeableDeviceConfigEnum;
18 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.impl.DSID;
20 import com.google.gson.JsonObject;
23 * The {@link AbstractGeneralDeviceInformations} is an abstract implementation of {@link GeneralDeviceInformations} and
24 * can be implement by subclasses which contains the same device informations like dSID and/or mechanismen like the
25 * {@link DeviceStatusListener}.
27 * @author Michael Ochel - initial contributer
28 * @author Matthias Siegele - initial contributer
30 public abstract class AbstractGeneralDeviceInformations implements GeneralDeviceInformation {
33 protected String dSUID;
34 protected Boolean isPresent;
35 protected Boolean isValide;
36 protected String name;
37 protected String displayID;
38 protected DeviceStatusListener listener;
41 * Creates a new {@link AbstractGeneralDeviceInformations} through the digitalSTROM json response as
44 * @param jsonDeviceObject json response of the digitalSTROM-Server, must not be null
46 public AbstractGeneralDeviceInformations(JsonObject jsonDeviceObject) {
47 if (jsonDeviceObject.get(JSONApiResponseKeysEnum.NAME.getKey()) != null) {
48 name = jsonDeviceObject.get(JSONApiResponseKeysEnum.NAME.getKey()).getAsString();
50 if (jsonDeviceObject.get(JSONApiResponseKeysEnum.ID.getKey()) != null) {
51 dsid = new DSID(jsonDeviceObject.get(JSONApiResponseKeysEnum.ID.getKey()).getAsString());
52 } else if (jsonDeviceObject.get(JSONApiResponseKeysEnum.DSID.getKey()) != null) {
53 dsid = new DSID(jsonDeviceObject.get(JSONApiResponseKeysEnum.DSID.getKey()).getAsString());
54 } else if (jsonDeviceObject.get(JSONApiResponseKeysEnum.DSID_LOWER_CASE.getKey()) != null) {
55 dsid = new DSID(jsonDeviceObject.get(JSONApiResponseKeysEnum.DSID_LOWER_CASE.getKey()).getAsString());
57 if (jsonDeviceObject.get(JSONApiResponseKeysEnum.DSUID.getKey()) != null) {
58 dSUID = jsonDeviceObject.get(JSONApiResponseKeysEnum.DSUID.getKey()).getAsString();
60 if (jsonDeviceObject.get(JSONApiResponseKeysEnum.DISPLAY_ID.getKey()) != null) {
61 displayID = jsonDeviceObject.get(JSONApiResponseKeysEnum.DISPLAY_ID.getKey()).getAsString();
63 if (jsonDeviceObject.get(JSONApiResponseKeysEnum.IS_PRESENT.getKey()) != null) {
64 isPresent = jsonDeviceObject.get(JSONApiResponseKeysEnum.IS_PRESENT.getKey()).getAsBoolean();
65 } else if (jsonDeviceObject.get(JSONApiResponseKeysEnum.PRESENT.getKey()) != null) {
66 isPresent = jsonDeviceObject.get(JSONApiResponseKeysEnum.PRESENT.getKey()).getAsBoolean();
68 if (jsonDeviceObject.get(JSONApiResponseKeysEnum.IS_VALID.getKey()) != null) {
69 isValide = jsonDeviceObject.get(JSONApiResponseKeysEnum.IS_VALID.getKey()).getAsBoolean();
74 public synchronized String getName() {
79 public synchronized void setName(String name) {
81 if (listener != null) {
82 listener.onDeviceConfigChanged(ChangeableDeviceConfigEnum.DEVICE_NAME);
87 public DSID getDSID() {
92 public String getDSUID() {
97 public Boolean isPresent() {
102 public void setIsPresent(boolean isPresent) {
103 this.isPresent = isPresent;
104 if (listener != null) {
106 listener.onDeviceRemoved(this);
108 listener.onDeviceAdded(this);
114 public Boolean isValid() {
119 public void setIsValid(boolean isValide) {
120 this.isValide = isValide;
124 public void registerDeviceStatusListener(DeviceStatusListener listener) {
125 if (listener != null) {
126 this.listener = listener;
127 listener.onDeviceAdded(this);
132 public DeviceStatusListener unregisterDeviceStatusListener() {
133 DeviceStatusListener listener = this.listener;
134 this.listener = null;
139 public boolean isListenerRegisterd() {
140 return listener != null;
144 public DeviceStatusListener getDeviceStatusListener() {
151 * @see java.lang.Object#hashCode()
154 public int hashCode() {
155 final int prime = 31;
157 result = prime * result + ((dSUID == null) ? 0 : dSUID.hashCode());
158 result = prime * result + ((displayID == null) ? 0 : displayID.hashCode());
159 result = prime * result + ((dsid == null) ? 0 : dsid.hashCode());
160 result = prime * result + ((isPresent == null) ? 0 : isPresent.hashCode());
161 result = prime * result + ((isValide == null) ? 0 : isValide.hashCode());
162 result = prime * result + ((name == null) ? 0 : name.hashCode());
169 * @see java.lang.Object#equals(java.lang.Object)
172 public boolean equals(Object obj) {
179 if (!(obj instanceof AbstractGeneralDeviceInformations)) {
182 AbstractGeneralDeviceInformations other = (AbstractGeneralDeviceInformations) obj;
184 if (other.dSUID != null) {
187 } else if (!dSUID.equals(other.dSUID)) {
190 if (displayID == null) {
191 if (other.displayID != null) {
194 } else if (!displayID.equals(other.displayID)) {
198 if (other.dsid != null) {
201 } else if (!dsid.equals(other.dsid)) {
204 if (isPresent == null) {
205 if (other.isPresent != null) {
208 } else if (!isPresent.equals(other.isPresent)) {
211 if (isValide == null) {
212 if (other.isValide != null) {
215 } else if (!isValide.equals(other.isValide)) {
219 if (other.name != null) {
222 } else if (!name.equals(other.name)) {
229 public String getDisplayID() {