]> git.basschouten.com Git - openhab-addons.git/blob
641df6fd5d6d604e71d031e426e729b0f698d872
[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.digitalstrom.internal.lib.structure.devices;
14
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;
19
20 import com.google.gson.JsonObject;
21
22 /**
23  * The {@link AbstractGeneralDeviceInformations} is an abstract implementation of {@link GeneralDeviceInformation}s and
24  * can be implement by subclasses which contains the same device informations like dSID and/or mechanismen like the
25  * {@link DeviceStatusListener}.
26  *
27  * @author Michael Ochel - Initial contribution
28  * @author Matthias Siegele - Initial contribution
29  */
30 public abstract class AbstractGeneralDeviceInformations implements GeneralDeviceInformation {
31
32     protected DSID dsid;
33     protected String dSUID;
34     protected Boolean isPresent;
35     protected Boolean isValide;
36     protected String name;
37     protected String displayID;
38     protected DeviceStatusListener listener;
39
40     /**
41      * Creates a new {@link AbstractGeneralDeviceInformations} through the digitalSTROM json response as
42      * {@link JsonObject}.
43      *
44      * @param jsonDeviceObject json response of the digitalSTROM-Server, must not be null
45      */
46     public AbstractGeneralDeviceInformations(JsonObject jsonDeviceObject) {
47         if (jsonDeviceObject.get(JSONApiResponseKeysEnum.NAME.getKey()) != null) {
48             name = jsonDeviceObject.get(JSONApiResponseKeysEnum.NAME.getKey()).getAsString();
49         }
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());
56         }
57         if (jsonDeviceObject.get(JSONApiResponseKeysEnum.DSUID.getKey()) != null) {
58             dSUID = jsonDeviceObject.get(JSONApiResponseKeysEnum.DSUID.getKey()).getAsString();
59         }
60         if (jsonDeviceObject.get(JSONApiResponseKeysEnum.DISPLAY_ID.getKey()) != null) {
61             displayID = jsonDeviceObject.get(JSONApiResponseKeysEnum.DISPLAY_ID.getKey()).getAsString();
62         }
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();
67         }
68         if (jsonDeviceObject.get(JSONApiResponseKeysEnum.IS_VALID.getKey()) != null) {
69             isValide = jsonDeviceObject.get(JSONApiResponseKeysEnum.IS_VALID.getKey()).getAsBoolean();
70         }
71     }
72
73     @Override
74     public synchronized String getName() {
75         return this.name;
76     }
77
78     @Override
79     public synchronized void setName(String name) {
80         this.name = name;
81         if (listener != null) {
82             listener.onDeviceConfigChanged(ChangeableDeviceConfigEnum.DEVICE_NAME);
83         }
84     }
85
86     @Override
87     public DSID getDSID() {
88         return dsid;
89     }
90
91     @Override
92     public String getDSUID() {
93         return this.dSUID;
94     }
95
96     @Override
97     public Boolean isPresent() {
98         return isPresent;
99     }
100
101     @Override
102     public void setIsPresent(boolean isPresent) {
103         this.isPresent = isPresent;
104         if (listener != null) {
105             if (!isPresent) {
106                 listener.onDeviceRemoved(this);
107             } else {
108                 listener.onDeviceAdded(this);
109             }
110         }
111     }
112
113     @Override
114     public Boolean isValid() {
115         return isValide;
116     }
117
118     @Override
119     public void setIsValid(boolean isValide) {
120         this.isValide = isValide;
121     }
122
123     @Override
124     public void registerDeviceStatusListener(DeviceStatusListener listener) {
125         if (listener != null) {
126             this.listener = listener;
127             listener.onDeviceAdded(this);
128         }
129     }
130
131     @Override
132     public DeviceStatusListener unregisterDeviceStatusListener() {
133         DeviceStatusListener listener = this.listener;
134         this.listener = null;
135         return listener;
136     }
137
138     @Override
139     public boolean isListenerRegisterd() {
140         return listener != null;
141     }
142
143     @Override
144     public DeviceStatusListener getDeviceStatusListener() {
145         return listener;
146     }
147
148     /*
149      * (non-Javadoc)
150      *
151      * @see java.lang.Object#hashCode()
152      */
153     @Override
154     public int hashCode() {
155         final int prime = 31;
156         int result = 1;
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());
163         return result;
164     }
165
166     /*
167      * (non-Javadoc)
168      *
169      * @see java.lang.Object#equals(java.lang.Object)
170      */
171     @Override
172     public boolean equals(Object obj) {
173         if (this == obj) {
174             return true;
175         }
176         if (obj == null) {
177             return false;
178         }
179         if (!(obj instanceof AbstractGeneralDeviceInformations)) {
180             return false;
181         }
182         AbstractGeneralDeviceInformations other = (AbstractGeneralDeviceInformations) obj;
183         if (dSUID == null) {
184             if (other.dSUID != null) {
185                 return false;
186             }
187         } else if (!dSUID.equals(other.dSUID)) {
188             return false;
189         }
190         if (displayID == null) {
191             if (other.displayID != null) {
192                 return false;
193             }
194         } else if (!displayID.equals(other.displayID)) {
195             return false;
196         }
197         if (dsid == null) {
198             if (other.dsid != null) {
199                 return false;
200             }
201         } else if (!dsid.equals(other.dsid)) {
202             return false;
203         }
204         if (isPresent == null) {
205             if (other.isPresent != null) {
206                 return false;
207             }
208         } else if (!isPresent.equals(other.isPresent)) {
209             return false;
210         }
211         if (isValide == null) {
212             if (other.isValide != null) {
213                 return false;
214             }
215         } else if (!isValide.equals(other.isValide)) {
216             return false;
217         }
218         if (name == null) {
219             if (other.name != null) {
220                 return false;
221             }
222         } else if (!name.equals(other.name)) {
223             return false;
224         }
225         return true;
226     }
227
228     @Override
229     public String getDisplayID() {
230         return displayID;
231     }
232 }