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.climate.jsonresponsecontainer.impl;
15 import java.util.Iterator;
16 import java.util.LinkedList;
17 import java.util.List;
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;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
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.
32 * @author Michael Ochel - Initial contribution
33 * @author Matthias Siegele - Initial contribution
35 public class AssignedSensors extends BaseZoneIdentifier {
37 private List<AssignSensorType> sensors;
40 * Creates a new {@link AssignedSensors} through the {@link JsonObject} that will be returned by an apartment call.
42 * @param jObject must not be null
44 public AssignedSensors(JsonObject jObject) {
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.
54 * @param jObject must not be null
55 * @param zoneID must not be null
56 * @param zoneName can be null
58 public AssignedSensors(JsonObject jObject, Integer zoneID, String zoneName) {
59 super(zoneID, zoneName);
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();
77 if (assignedSensor.get(JSONApiResponseKeysEnum.DSUID_LOWER_CASE.getKey()) != null) {
78 meterDSUID = assignedSensor.get(JSONApiResponseKeysEnum.DSUID_LOWER_CASE.getKey())
81 sensors.add(new AssignSensorType(SensorEnum.getSensor(sensorType), meterDSUID));
88 * Returns a {@link List} of all assigned sensor types as {@link SensorEnum} of a zone.
90 * @return list of all assigned sensor types
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());
103 * Returns a {@link List} of all {@link AssignSensorType} of a zone.
105 * @return list of {@link AssignSensorType}s
107 public List<AssignSensorType> getAssignedSensorTypes() {
112 * Returns the {@link AssignSensorType} of the given sensorType or null, if the given sensorType does not exist.
114 * @param sensorType can be null
115 * @return the {@link AssignSensorType} of the given sensorType or null
117 public AssignSensorType getAssignedSensorType(SensorEnum sensorType) {
118 if (sensorType != null && sensors != null) {
119 for (AssignSensorType aSensorValue : sensors) {
120 if (aSensorValue.getSensorType().equals(sensorType)) {
129 * Returns true, if the given sensorType exists at the zone, otherwise false.
131 * @param sensorType can be null
132 * @return true, if sensorType exists, otherwise false
134 public boolean existSensorType(SensorEnum sensorType) {
135 return getAssignedSensorType(sensorType) != null;
139 * Returns true, if sensors exists at the zone, otherwise false.
141 * @return true, if sensors exists, otherwise false
143 public boolean existsAssignedSensors() {
144 return sensors != null;
150 * @see java.lang.Object#toString()
153 public String toString() {
154 return "AssignedSensors [sensors=" + sensors + "]";