]> git.basschouten.com Git - openhab-addons.git/blob
55e0407974f637041c537805aaa83b6a8b71a66e
[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 import java.util.LinkedList;
17 import java.util.List;
18
19 import org.openhab.binding.digitalstrom.internal.lib.climate.datatypes.AssignSensorType;
20 import org.openhab.binding.digitalstrom.internal.lib.climate.jsonresponsecontainer.BaseZoneIdentifier;
21 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.constants.JSONApiResponseKeysEnum;
22 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.SensorEnum;
23
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27
28 /**
29  * The {@link AssignedSensors} acts as container for the digitalSTROM json-method <i>getAssignedSensors</i>. So the
30  * {@link AssignedSensors} contains all {@link AssignSensorType}s of a zone.
31  *
32  * @author Michael Ochel - Initial contribution
33  * @author Matthias Siegele - Initial contribution
34  */
35 public class AssignedSensors extends BaseZoneIdentifier {
36
37     private List<AssignSensorType> sensors;
38
39     /**
40      * Creates a new {@link AssignedSensors} through the {@link JsonObject} that will be returned by an apartment call.
41      *
42      * @param jObject must not be null
43      */
44     public AssignedSensors(JsonObject jObject) {
45         super(jObject);
46         init(jObject);
47     }
48
49     /**
50      * Creates a new {@link AssignedSensors} through the {@link JsonObject} which will be returned by a zone call.
51      * Because of zone calls does not include a zoneID or zoneName in the json response, the zoneID and zoneName have to
52      * be handed over the constructor.
53      *
54      * @param jObject must not be null
55      * @param zoneID must not be null
56      * @param zoneName can be null
57      */
58     public AssignedSensors(JsonObject jObject, Integer zoneID, String zoneName) {
59         super(zoneID, zoneName);
60         init(jObject);
61     }
62
63     private void init(JsonObject jObject) {
64         if (jObject.get(JSONApiResponseKeysEnum.SENSORS.getKey()) != null
65                 && jObject.get(JSONApiResponseKeysEnum.SENSORS.getKey()).isJsonArray()) {
66             JsonArray jArray = jObject.get(JSONApiResponseKeysEnum.SENSORS.getKey()).getAsJsonArray();
67             if (jArray.size() != 0) {
68                 sensors = new LinkedList<>();
69                 Iterator<JsonElement> iter = jArray.iterator();
70                 while (iter.hasNext()) {
71                     JsonObject assignedSensor = iter.next().getAsJsonObject();
72                     Short sensorType = null;
73                     String meterDSUID = null;
74                     if (assignedSensor.get(JSONApiResponseKeysEnum.SENSOR_TYPE.getKey()) != null) {
75                         sensorType = assignedSensor.get(JSONApiResponseKeysEnum.SENSOR_TYPE.getKey()).getAsShort();
76                     }
77                     if (assignedSensor.get(JSONApiResponseKeysEnum.DSUID_LOWER_CASE.getKey()) != null) {
78                         meterDSUID = assignedSensor.get(JSONApiResponseKeysEnum.DSUID_LOWER_CASE.getKey())
79                                 .getAsString();
80                     }
81                     sensors.add(new AssignSensorType(SensorEnum.getSensor(sensorType), meterDSUID));
82                 }
83             }
84         }
85     }
86
87     /**
88      * Returns a {@link List} of all assigned sensor types as {@link SensorEnum} of a zone.
89      *
90      * @return list of all assigned sensor types
91      */
92     public List<SensorEnum> getAssignedZoneSensorTypes() {
93         List<SensorEnum> sensorTypes = new LinkedList<>();
94         if (sensors != null) {
95             for (AssignSensorType aSensorValue : sensors) {
96                 sensorTypes.add(aSensorValue.getSensorType());
97             }
98         }
99         return sensorTypes;
100     }
101
102     /**
103      * Returns a {@link List} of all {@link AssignSensorType} of a zone.
104      *
105      * @return list of {@link AssignSensorType}s
106      */
107     public List<AssignSensorType> getAssignedSensorTypes() {
108         return sensors;
109     }
110
111     /**
112      * Returns the {@link AssignSensorType} of the given sensorType or null, if the given sensorType does not exist.
113      *
114      * @param sensorType can be null
115      * @return the {@link AssignSensorType} of the given sensorType or null
116      */
117     public AssignSensorType getAssignedSensorType(SensorEnum sensorType) {
118         if (sensorType != null && sensors != null) {
119             for (AssignSensorType aSensorValue : sensors) {
120                 if (aSensorValue.getSensorType().equals(sensorType)) {
121                     return aSensorValue;
122                 }
123             }
124         }
125         return null;
126     }
127
128     /**
129      * Returns true, if the given sensorType exists at the zone, otherwise false.
130      *
131      * @param sensorType can be null
132      * @return true, if sensorType exists, otherwise false
133      */
134     public boolean existSensorType(SensorEnum sensorType) {
135         return getAssignedSensorType(sensorType) != null;
136     }
137
138     /**
139      * Returns true, if sensors exists at the zone, otherwise false.
140      *
141      * @return true, if sensors exists, otherwise false
142      */
143     public boolean existsAssignedSensors() {
144         return sensors != null;
145     }
146
147     /*
148      * (non-Javadoc)
149      *
150      * @see java.lang.Object#toString()
151      */
152     @Override
153     public String toString() {
154         return "AssignedSensors [sensors=" + sensors + "]";
155     }
156 }