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.structure.impl;
15 import java.util.LinkedList;
16 import java.util.List;
18 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.constants.JSONApiResponseKeysEnum;
19 import org.openhab.binding.digitalstrom.internal.lib.structure.DetailedGroupInfo;
20 import org.openhab.binding.digitalstrom.internal.lib.structure.Zone;
21 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.Device;
22 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.impl.DeviceImpl;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonObject;
28 * The {@link JSONZoneImpl} is the implementation of the {@link Zone}.
30 * @author Alexander Betker - Initial contribution
31 * @author Michael Ochel - change from SimpleJSON to GSON
32 * @author Matthias Siegele - change from SimpleJSON to GSON
34 public class JSONZoneImpl implements Zone {
36 private int zoneId = -1;
39 private final List<DetailedGroupInfo> groupList;
40 private final List<Device> deviceList;
43 * Creates a new {@link JSONZoneImpl} through the {@link JsonObject}.
45 * @param jObject of the server response, must not be null
47 public JSONZoneImpl(JsonObject jObject) {
48 this.groupList = new LinkedList<>();
49 this.deviceList = new LinkedList<>();
51 if (jObject.get(JSONApiResponseKeysEnum.NAME.getKey()) != null) {
52 this.name = jObject.get(JSONApiResponseKeysEnum.NAME.getKey()).getAsString();
54 if (jObject.get(JSONApiResponseKeysEnum.ID.getKey()) != null) {
55 zoneId = jObject.get(JSONApiResponseKeysEnum.ID.getKey()).getAsInt();
58 if (jObject.get(JSONApiResponseKeysEnum.ZONE_ID.getKey()) != null) {
59 zoneId = jObject.get(JSONApiResponseKeysEnum.ZONE_ID.getKey()).getAsInt();
62 if (jObject.get(JSONApiResponseKeysEnum.DEVICES.getKey()) instanceof JsonArray) {
63 JsonArray list = (JsonArray) jObject.get(JSONApiResponseKeysEnum.DEVICES.getKey());
64 for (int i = 0; i < list.size(); i++) {
65 this.deviceList.add(new DeviceImpl((JsonObject) list.get(i)));
68 if (jObject.get(JSONApiResponseKeysEnum.GROUPS.getKey()) instanceof JsonArray) {
69 JsonArray groupList = (JsonArray) jObject.get(JSONApiResponseKeysEnum.GROUPS.getKey());
70 for (int i = 0; i < groupList.size(); i++) {
71 this.groupList.add(new JSONDetailedGroupInfoImpl((JsonObject) groupList.get(i)));
77 public int getZoneId() {
82 public synchronized void setZoneId(int id) {
89 public String getName() {
94 public synchronized void setName(String name) {
99 public List<DetailedGroupInfo> getGroups() {
104 public void addGroup(DetailedGroupInfo group) {
106 synchronized (groupList) {
107 if (!groupList.contains(group)) {
108 groupList.add(group);
115 public List<Device> getDevices() {
120 public void addDevice(Device device) {
121 if (device != null) {
122 synchronized (deviceList) {
123 if (!deviceList.contains(device)) {
124 deviceList.add(device);
131 public boolean equals(Object obj) {
132 if (obj instanceof Zone other) {
133 return (other.getZoneId() == this.getZoneId());