]> git.basschouten.com Git - openhab-addons.git/blob
463ae5822abcaa09db16c8d1e8ae2d9a531af89e
[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.DeviceType;
16 import org.openhab.binding.yeelight.internal.lib.enums.MethodAction;
17
18 /**
19  * @author Coaster Li - Initial contribution
20  * @author Nikita Pogudalov - Added DeviceMethod for Ceiling 1
21  */
22 public class MethodFactory {
23     public static DeviceMethod buildBrightnessMethd(int brightness, String effect, int duration) {
24         return new DeviceMethod(MethodAction.BRIGHTNESS, new Object[] { brightness, effect, duration });
25     }
26
27     public static DeviceMethod buildRgbMethod(int color, String effect, int duration) {
28         return new DeviceMethod(MethodAction.RGB, new Object[] { color, effect, duration });
29     }
30
31     public static DeviceMethod buildCTMethod(int colorTemperature, String effect, int duration) {
32         return new DeviceMethod(MethodAction.COLORTEMPERATURE, new Object[] { colorTemperature, effect, duration });
33     }
34
35     public static DeviceMethod buildHsvMethod(int hue, int sat, String effect, int duration) {
36         return new DeviceMethod(MethodAction.HSV, new Object[] { hue, sat, effect, duration });
37     }
38
39     public static DeviceMethod buildToggle() {
40         return new DeviceMethod(MethodAction.HSV, null);
41     }
42
43     public static DeviceMethod buildSetDefault() {
44         return new DeviceMethod(MethodAction.DEFAULT, null);
45     }
46
47     public static DeviceMethod buildStartCF(int count, int endAction, ColorFlowItem[] items) {
48         if (items == null || items.length < 1) {
49             return null;
50         }
51
52         String itemStr = "\"";
53         StringBuilder builder;
54         for (int i = 0; i < items.length; i++) {
55             builder = new StringBuilder();
56             ColorFlowItem item = items[i];
57             builder.append(item.duration).append(",").append(item.mode).append(item.value).append(item.brightness);
58             if (i < items.length - 1) {
59                 builder.append(",");
60             }
61         }
62         itemStr += "\"";
63
64         return new DeviceMethod(MethodAction.STARTCF, new Object[] { count, endAction, itemStr });
65     }
66
67     public static DeviceMethod buildStopCf() {
68         return new DeviceMethod(MethodAction.STOPCF, null);
69     }
70
71     public static DeviceMethod buildScnene(String type, int value, int brightness) {
72         return buildScene(type, value, brightness, 0, 0, null);
73     }
74
75     /**
76      * @param type scene type {@link DeviceMethod#SCENE_TYPE_COLOR}
77      *            {@link DeviceMethod#SCENE_TYPE_CT}
78      *            {@link DeviceMethod#SCENE_TYPE_DELAY}
79      *            {@link DeviceMethod#SCENE_TYPE_HSV}
80      *            {@link DeviceMethod#SCENE_TYPE_CF}
81      *
82      */
83     public static DeviceMethod buildScene(String type, int value, int brightness, int count, int endAction,
84             ColorFlowItem[] items) {
85         if (DeviceMethod.SCENE_TYPE_CF.equals(type) && items == null) {
86             throw new IllegalArgumentException("Type is colorFlow, but no flow tuples given");
87         }
88         Object[] params = null;
89         switch (type) {
90             case DeviceMethod.SCENE_TYPE_COLOR:
91                 params = new Object[] { "color", value, brightness };
92                 break;
93             case DeviceMethod.SCENE_TYPE_CT:
94                 params = new Object[] { "ct", value, brightness };
95                 break;
96             case DeviceMethod.SCENE_TYPE_DELAY:
97                 params = new Object[] { "auto_delay_off", brightness, value };
98                 break;
99             case DeviceMethod.SCENE_TYPE_HSV:
100                 params = new Object[] { "hsv", value, brightness };
101                 break;
102             case DeviceMethod.SCENE_TYPE_CF:
103                 String itemStr = "\"";
104                 StringBuilder builder;
105                 for (int i = 0; i < items.length; i++) {
106                     builder = new StringBuilder();
107                     ColorFlowItem item = items[i];
108                     builder.append(item.duration).append(",").append(item.mode).append(item.value)
109                             .append(item.brightness);
110                     if (i < items.length - 1) {
111                         builder.append(",");
112                     }
113                 }
114                 itemStr += "\"";
115                 params = new Object[] { "cf", count, endAction, itemStr };
116                 break;
117             default:
118                 return null;
119         }
120
121         return new DeviceMethod(MethodAction.SCENE, params);
122     }
123
124     public static DeviceMethod buildCronAdd(int value) {
125         return new DeviceMethod(MethodAction.CRON_ADD, new Object[] { 0, value });
126     }
127
128     public static DeviceMethod buildCronGet() {
129         return new DeviceMethod(MethodAction.CRON_ADD, new Object[] { 0 });
130     }
131
132     public static DeviceMethod buildCronDel() {
133         return new DeviceMethod(MethodAction.CRON_DEL, new Object[] { 0 });
134     }
135
136     public static DeviceMethod buildAdjust(String action, String prop) {
137         if (DeviceMethod.ADJUST_PROP_COLOR.equals(prop) && !DeviceMethod.ADJUST_ACTION_CIRCLE.equals(action)) {
138             throw new IllegalArgumentException("When prop is COLOR, the action can only be CIRCLE!!!");
139         }
140         return new DeviceMethod(MethodAction.ADJUST, new Object[] { action, prop });
141     }
142
143     public static DeviceMethod buildMusic(int action, String ipAddress, int port) {
144         if (action == DeviceMethod.MUSIC_ACTION_ON) {
145             return new DeviceMethod(MethodAction.MUSIC, new Object[] { action, ipAddress, port });
146         } else {
147             return new DeviceMethod(MethodAction.MUSIC, new Object[] { action });
148         }
149     }
150
151     public static DeviceMethod buildName(String name) {
152         return new DeviceMethod(MethodAction.NAME, new Object[] { name });
153     }
154
155     public static DeviceMethod buildBackgroundHSVMethod(int hue, int sat, String effect, int duration) {
156         return new DeviceMethod(MethodAction.BG_HSV, new Object[] { hue, sat, effect, duration });
157     }
158
159     public static DeviceMethod buildBackgroundBrightnessMethd(int brightness, String effect, int duration) {
160         return new DeviceMethod(MethodAction.BG_BRIGHTNESS, new Object[] { brightness, effect, duration });
161     }
162
163     public static DeviceMethod buildQuery(DeviceBase device) {
164         DeviceType type = device.getDeviceType();
165         switch (type) {
166             case mono:
167                 return new DeviceMethod(MethodAction.PROP, new Object[] { "power", "name", "bright" });
168             case color:
169             case stripe:
170                 return new DeviceMethod(MethodAction.PROP,
171                         new Object[] { "power", "name", "bright", "ct", "rgb", "hue", "sat" });
172             case ceiling:
173             case desklamp:
174             case ct_bulb:
175                 return new DeviceMethod(MethodAction.PROP, new Object[] { "power", "name", "bright", "ct" });
176             case ceiling1:
177             case ceiling4:
178                 return new DeviceMethod(MethodAction.PROP, new Object[] { "power", "name", "bright", "ct", "bg_power",
179                         "bg_bright", "bg_hue", "bg_sat", "active_mode" });
180             default:
181                 return null;
182         }
183     }
184 }