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.util;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Map.Entry;
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 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.impl.DeviceImpl;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
29 * digitalSTROM JSON Parser class. Externalizes code from {@link DeviceImpl}
31 * @author Rouven Schürch - Initial contribution
35 public final class DSJsonParser {
37 private DSJsonParser() {
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));
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));
59 return outputChannels;