]> git.basschouten.com Git - openhab-addons.git/blob
e77719d6e8a7ef870225d0fd74bab930c4f79354
[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.climate.jsonresponsecontainer.impl;
14
15 import java.util.Iterator;
16
17 import org.openhab.binding.digitalstrom.internal.lib.climate.jsonresponsecontainer.BaseSensorValues;
18 import org.openhab.binding.digitalstrom.internal.lib.climate.jsonresponsecontainer.ZoneIdentifier;
19 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.constants.JSONApiResponseKeysEnum;
20
21 import com.google.gson.JsonArray;
22 import com.google.gson.JsonElement;
23 import com.google.gson.JsonObject;
24
25 /**
26  * The {@link SensorValues} acts as container for the digitalSTROM json-method <i>getSensorValues</i>. So the
27  * {@link SensorValues} contains all {@link CachedSensorValue}s of a zone.
28  *
29  * @author Michael Ochel - Initial contribution
30  * @author Matthias Siegele - Initial contribution
31  */
32 public class SensorValues extends BaseSensorValues implements ZoneIdentifier {
33
34     private Integer zoneID;
35     private String zoneName;
36
37     /**
38      * Creates a new {@link SensorValues} through the {@link JsonObject} that will be returned by an apartment call.
39      *
40      * @param jObject must not be null
41      */
42     public SensorValues(JsonObject jObject) {
43         if (jObject.get(JSONApiResponseKeysEnum.ID.getKey()) != null) {
44             this.zoneID = jObject.get(JSONApiResponseKeysEnum.ID.getKey()).getAsInt();
45         }
46         if (jObject.get(JSONApiResponseKeysEnum.NAME.getKey()) != null) {
47             this.zoneName = jObject.get(JSONApiResponseKeysEnum.NAME.getKey()).getAsString();
48         }
49         init(jObject);
50     }
51
52     /**
53      * Creates a new {@link SensorValues} through the {@link JsonObject} which will be returned by a zone call.
54      * Because of zone calls does not include a zoneID or zoneName in the json response, the zoneID and zoneName have to
55      * be handed over the constructor.
56      *
57      * @param jObject must not be null
58      * @param zoneID must not be null
59      * @param zoneName can be null
60      */
61     public SensorValues(JsonObject jObject, Integer zoneID, String zoneName) {
62         this.zoneID = zoneID;
63         this.zoneName = zoneName;
64         init(jObject);
65     }
66
67     private void init(JsonObject jObject) {
68         if (jObject.get(JSONApiResponseKeysEnum.VALUES.getKey()).isJsonArray()) {
69             JsonArray jArray = jObject.get(JSONApiResponseKeysEnum.VALUES.getKey()).getAsJsonArray();
70             if (jArray.size() != 0) {
71                 Iterator<JsonElement> iter = jArray.iterator();
72                 while (iter.hasNext()) {
73                     JsonObject cachedSensorValue = iter.next().getAsJsonObject();
74                     super.addSensorValue(cachedSensorValue, false);
75                 }
76             }
77         }
78     }
79
80     /*
81      * (non-Javadoc)
82      *
83      * @see java.lang.Object#toString()
84      */
85     @Override
86     public String toString() {
87         return "SensorValues [zoneID=" + zoneID + ", zoneName=" + zoneName + ", " + super.toString() + "]";
88     }
89
90     @Override
91     public Integer getZoneID() {
92         return zoneID;
93     }
94
95     @Override
96     public String getZoneName() {
97         return zoneName;
98     }
99 }