]> git.basschouten.com Git - openhab-addons.git/blob
2219753604d64ed34b0ab03ea319da531af5b4cb
[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.yeelight.internal.lib.device;
14
15 import org.openhab.binding.yeelight.internal.lib.enums.MethodAction;
16
17 /**
18  * @author Coaster Li - Initial contribution
19  */
20 public class DeviceMethod {
21
22     public static final String EFFECT_SMOOTH = "smooth";
23     public static final String EFFECT_SUDDEN = "sudden";
24
25     public static final int CF_END_STATE_RECOVER = 0;
26     public static final int CF_END_STATE_STAY = 1;
27     public static final int CF_END_STATE_TURNOFF = 2;
28
29     public static final int CF_ITEM_MODE_COLOR = 1;
30     public static final int CF_ITEM_MODE_CT = 2;
31     public static final int CF_ITEM_MODE_SLEEP = 7;
32
33     public static final String SCENE_TYPE_COLOR = "color";
34     public static final String SCENE_TYPE_HSV = "hsv";
35     public static final String SCENE_TYPE_CT = "ct";
36     public static final String SCENE_TYPE_CF = "cf";
37     public static final String SCENE_TYPE_DELAY = "auto_delay_off";
38
39     public static final String ADJUST_ACTION_INCREASE = "increase";
40     public static final String ADJUST_ACTION_DECREASE = "decrease";
41     public static final String ADJUST_ACTION_CIRCLE = "circle";
42
43     public static final String ADJUST_PROP_BRIGHT = "bright";
44     public static final String ADJUST_PROP_CT = "ct";
45     public static final String ADJUST_PROP_COLOR = "color";
46
47     public static final int MUSIC_ACTION_ON = 1;
48     public static final int MUSIC_ACTION_OFF = 0;
49
50     private static int sIndex = 0;
51
52     private String mMethodAction;
53     private Object[] mMethodParams;
54     private String mCustomMethodParams;
55     private int mIndex;
56
57     public DeviceMethod(MethodAction action, Object[] params) {
58         this.mMethodAction = action.action;
59         this.mMethodParams = params;
60         this.mCustomMethodParams = "";
61         mIndex = ++sIndex;
62     }
63
64     public DeviceMethod(String action, String params) {
65         this.mMethodAction = action;
66         this.mMethodParams = null;
67         this.mCustomMethodParams = params;
68         mIndex = ++sIndex;
69     }
70
71     public String getParamsStr() {
72         StringBuilder cmdBuilder = new StringBuilder();
73         cmdBuilder.append("{\"id\":").append(mIndex).append(",");
74         cmdBuilder.append("\"method\":\"").append(mMethodAction).append("\",");
75         cmdBuilder.append("\"params\":[");
76         if (mMethodParams != null && mMethodParams.length > 0) {
77             for (Object param : mMethodParams) {
78                 if (param instanceof String) {
79                     cmdBuilder.append("\"" + param.toString() + "\"");
80                 } else {
81                     cmdBuilder.append(Integer.parseInt(param.toString()));
82                 }
83                 cmdBuilder.append(",");
84             }
85             // delete last ","
86             cmdBuilder.deleteCharAt(cmdBuilder.length() - 1);
87         }
88         cmdBuilder.append("]}\r\n");
89         return cmdBuilder.toString();
90     }
91
92     public String getCustomParamsStr() {
93         StringBuilder cmdBuilder = new StringBuilder();
94         cmdBuilder.append("{\"id\":").append(mIndex).append(",");
95         cmdBuilder.append("\"method\":\"").append(mMethodAction).append("\",");
96         cmdBuilder.append("\"params\":[").append(mCustomMethodParams).append("]}\r\n");
97         return cmdBuilder.toString();
98     }
99
100     public String getCmdId() {
101         return String.valueOf(mIndex);
102     }
103 }