]> git.basschouten.com Git - openhab-addons.git/blob
150f0a72c25c35dda6972af429a99f8ccad0646d
[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.util;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Map.Entry;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.constants.JSONApiResponseKeysEnum;
21 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.OutputChannelEnum;
22
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26
27 /**
28  * digitalSTROM JSON Parser class. Externalizes code from
29  * {@link org.openhab.binding.digitalstrom.internal.lib.structure.devices.impl.DeviceImpl}
30  *
31  * @author Rouven Schürch - Initial contribution
32  *
33  */
34 @NonNullByDefault
35 public final class DSJsonParser {
36
37     private DSJsonParser() {
38     }
39
40     public static List<OutputChannelEnum> getOutputChannels(JsonObject jsonObject) {
41         List<OutputChannelEnum> outputChannels = new ArrayList<>();
42         JsonElement jsonOutputChannels = jsonObject.get(JSONApiResponseKeysEnum.OUTPUT_CHANNELS.getKey());
43         if (jsonOutputChannels != null && jsonOutputChannels.isJsonArray()) {
44             JsonArray array = jsonObject.get(JSONApiResponseKeysEnum.OUTPUT_CHANNELS.getKey()).getAsJsonArray();
45             for (int i = 0; i < array.size(); i++) {
46                 if (array.get(i) != null) {
47                     int channelId = array.get(i).getAsJsonObject().get("channelID").getAsInt();
48                     outputChannels.add(OutputChannelEnum.getChannel(channelId));
49                 }
50             }
51         } else if (jsonOutputChannels != null && jsonOutputChannels.isJsonObject()) {
52             for (Entry<String, JsonElement> entry : jsonObject.get(JSONApiResponseKeysEnum.OUTPUT_CHANNELS.getKey())
53                     .getAsJsonObject().entrySet()) {
54                 int channelId = entry.getValue().getAsJsonObject().get("channelID").getAsInt();
55                 outputChannels.add(OutputChannelEnum.getChannel(channelId));
56             }
57         }
58
59         return outputChannels;
60     }
61 }