]> git.basschouten.com Git - openhab-addons.git/blob
0cd5bef636eb1e8a020908ddeb60b6c2cd183cc3
[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 {@link DeviceImpl}
29  *
30  * @author Rouven Schürch - Initial contribution
31  *
32  */
33 @NonNullByDefault
34 public final class DSJsonParser {
35
36     private DSJsonParser() {
37     }
38
39     public static List<OutputChannelEnum> getOutputChannels(JsonObject jsonObject) {
40         List<OutputChannelEnum> outputChannels = new ArrayList<>();
41         JsonElement jsonOutputChannels = jsonObject.get(JSONApiResponseKeysEnum.OUTPUT_CHANNELS.getKey());
42         if (jsonOutputChannels != null && jsonOutputChannels.isJsonArray()) {
43             JsonArray array = jsonObject.get(JSONApiResponseKeysEnum.OUTPUT_CHANNELS.getKey()).getAsJsonArray();
44             for (int i = 0; i < array.size(); i++) {
45                 if (array.get(i) != null) {
46                     int channelId = array.get(i).getAsJsonObject().get("channelID").getAsInt();
47                     outputChannels.add(OutputChannelEnum.getChannel(channelId));
48                 }
49             }
50         } else if (jsonOutputChannels != null && jsonOutputChannels.isJsonObject()) {
51             for (Entry<String, JsonElement> entry : jsonObject.get(JSONApiResponseKeysEnum.OUTPUT_CHANNELS.getKey())
52                     .getAsJsonObject().entrySet()) {
53                 int channelId = entry.getValue().getAsJsonObject().get("channelID").getAsInt();
54                 outputChannels.add(OutputChannelEnum.getChannel(channelId));
55             }
56         }
57
58         return outputChannels;
59     }
60 }