]> git.basschouten.com Git - openhab-addons.git/blob
523a1b704d318a6d31299f6156e67421d77949ff
[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.wled.internal.api;
14
15 import static org.openhab.binding.wled.internal.WLedBindingConstants.*;
16
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Map.Entry;
20 import java.util.Set;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jetty.client.HttpClient;
24 import org.openhab.binding.wled.internal.WledState.PresetState;
25 import org.openhab.binding.wled.internal.handlers.WLedBridgeHandler;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.types.StateOption;
28
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonObject;
31 import com.google.gson.JsonSyntaxException;
32
33 /**
34  * The {@link WledApiV0110} is the json Api methods for firmware version 0.11.0 and newer
35  * as newer firmwares come out with breaking changes, extend this class into a newer firmware version class.
36  *
37  * @author Matthew Skinner - Initial contribution
38  */
39 @NonNullByDefault
40 public class WledApiV0110 extends WledApiV084 {
41
42     public WledApiV0110(WLedBridgeHandler handler, HttpClient httpClient) {
43         super(handler, httpClient);
44     }
45
46     @Override
47     public void initialize() throws ApiException {
48         super.initialize();
49         getPresets();
50     }
51
52     protected void getPresets() throws JsonSyntaxException, ApiException {
53         List<StateOption> presetsOptions = new ArrayList<>();
54         List<StateOption> playlistsOptions = new ArrayList<>();
55         JsonObject obj = gson.fromJson(sendGetRequest("/presets.json"), JsonObject.class);
56         if (obj == null) {
57             return;
58         }
59         Set<Entry<String, JsonElement>> set = obj.entrySet();
60         int counter = 0;
61         for (Entry<String, JsonElement> presetEntry : set) {
62             logger.trace("Preset:{} json:{}", presetEntry.getKey(), presetEntry.getValue());
63             PresetState preset = gson.fromJson(presetEntry.getValue(), PresetState.class);
64             if (preset != null && counter > 0) {
65                 if (presetEntry.getValue().toString().contains("playlist")) {
66                     playlistsOptions.add(new StateOption(presetEntry.getKey(), preset.n));
67                 } else {
68                     presetsOptions.add(new StateOption(presetEntry.getKey(), preset.n));
69                 }
70             }
71             counter++;
72         }
73         handler.stateDescriptionProvider.setStateOptions(new ChannelUID(handler.getThing().getUID(), CHANNEL_PRESETS),
74                 presetsOptions);
75         handler.stateDescriptionProvider.setStateOptions(new ChannelUID(handler.getThing().getUID(), CHANNEL_PLAYLISTS),
76                 playlistsOptions);
77     }
78
79     @Override
80     public void savePreset(int position, String presetName) throws ApiException {
81         if (position < 1) {
82             logger.warn("Preset position {} is not supported in this firmware version", position);
83             return;
84         }
85
86         String name = presetName;
87         if (name.isEmpty()) {
88             name = "Preset " + position;
89         }
90         postState("{\"psave\":" + position + ",\"n\":\"" + name + "\",\"ib\":true,\"sb\":true}");
91     }
92
93     @Override
94     public void setSleepMode(String value) throws ApiException {
95         postState("{\"nl\":{\"mode\":" + value + "}}");
96     }
97 }