]> git.basschouten.com Git - openhab-addons.git/blob
ebaff0b977d887416595ac6d646ee46b6859fd0f
[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.structure.impl;
14
15 import java.util.LinkedList;
16 import java.util.List;
17
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;
23
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonObject;
26
27 /**
28  * The {@link JSONZoneImpl} is the implementation of the {@link Zone}.
29  *
30  * @author Alexander Betker - Initial contribution
31  * @author Michael Ochel - change from SimpleJSON to GSON
32  * @author Matthias Siegele - change from SimpleJSON to GSON
33  */
34 public class JSONZoneImpl implements Zone {
35
36     private int zoneId = -1;
37     private String name;
38
39     private final List<DetailedGroupInfo> groupList;
40     private final List<Device> deviceList;
41
42     /**
43      * Creates a new {@link JSONZoneImpl} through the {@link JsonObject}.
44      *
45      * @param jObject of the server response, must not be null
46      */
47     public JSONZoneImpl(JsonObject jObject) {
48         this.groupList = new LinkedList<>();
49         this.deviceList = new LinkedList<>();
50
51         if (jObject.get(JSONApiResponseKeysEnum.NAME.getKey()) != null) {
52             this.name = jObject.get(JSONApiResponseKeysEnum.NAME.getKey()).getAsString();
53         }
54         if (jObject.get(JSONApiResponseKeysEnum.ID.getKey()) != null) {
55             zoneId = jObject.get(JSONApiResponseKeysEnum.ID.getKey()).getAsInt();
56         }
57         if (zoneId == -1) {
58             if (jObject.get(JSONApiResponseKeysEnum.ZONE_ID.getKey()) != null) {
59                 zoneId = jObject.get(JSONApiResponseKeysEnum.ZONE_ID.getKey()).getAsInt();
60             }
61         }
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)));
66             }
67         }
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)));
72             }
73         }
74     }
75
76     @Override
77     public int getZoneId() {
78         return zoneId;
79     }
80
81     @Override
82     public synchronized void setZoneId(int id) {
83         if (id > 0) {
84             this.zoneId = id;
85         }
86     }
87
88     @Override
89     public String getName() {
90         return name;
91     }
92
93     @Override
94     public synchronized void setName(String name) {
95         this.name = name;
96     }
97
98     @Override
99     public List<DetailedGroupInfo> getGroups() {
100         return groupList;
101     }
102
103     @Override
104     public void addGroup(DetailedGroupInfo group) {
105         if (group != null) {
106             synchronized (groupList) {
107                 if (!groupList.contains(group)) {
108                     groupList.add(group);
109                 }
110             }
111         }
112     }
113
114     @Override
115     public List<Device> getDevices() {
116         return deviceList;
117     }
118
119     @Override
120     public void addDevice(Device device) {
121         if (device != null) {
122             synchronized (deviceList) {
123                 if (!deviceList.contains(device)) {
124                     deviceList.add(device);
125                 }
126             }
127         }
128     }
129
130     @Override
131     public boolean equals(Object obj) {
132         if (obj instanceof Zone) {
133             Zone other = (Zone) obj;
134             return (other.getZoneId() == this.getZoneId());
135         }
136         return false;
137     }
138 }