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