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.yeelight.internal.lib.device;
15 import org.openhab.binding.yeelight.internal.lib.enums.DeviceType;
16 import org.openhab.binding.yeelight.internal.lib.enums.MethodAction;
19 * @author Coaster Li - Initial contribution
20 * @author Nikita Pogudalov - Added DeviceMethod for Ceiling 1
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 });
27 public static DeviceMethod buildRgbMethod(int color, String effect, int duration) {
28 return new DeviceMethod(MethodAction.RGB, new Object[] { color, effect, duration });
31 public static DeviceMethod buildCTMethod(int colorTemperature, String effect, int duration) {
32 return new DeviceMethod(MethodAction.COLORTEMPERATURE, new Object[] { colorTemperature, effect, duration });
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 });
39 public static DeviceMethod buildToggle() {
40 return new DeviceMethod(MethodAction.HSV, null);
43 public static DeviceMethod buildSetDefault() {
44 return new DeviceMethod(MethodAction.DEFAULT, null);
47 public static DeviceMethod buildStartCF(int count, int endAction, ColorFlowItem[] items) {
48 if (items == null || items.length < 1) {
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) {
64 return new DeviceMethod(MethodAction.STARTCF, new Object[] { count, endAction, itemStr });
67 public static DeviceMethod buildStopCf() {
68 return new DeviceMethod(MethodAction.STOPCF, null);
71 public static DeviceMethod buildScnene(String type, int value, int brightness) {
72 return buildScene(type, value, brightness, 0, 0, null);
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}
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");
88 Object[] params = null;
90 case DeviceMethod.SCENE_TYPE_COLOR:
91 params = new Object[] { "color", value, brightness };
93 case DeviceMethod.SCENE_TYPE_CT:
94 params = new Object[] { "ct", value, brightness };
96 case DeviceMethod.SCENE_TYPE_DELAY:
97 params = new Object[] { "auto_delay_off", brightness, value };
99 case DeviceMethod.SCENE_TYPE_HSV:
100 params = new Object[] { "hsv", value, brightness };
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) {
115 params = new Object[] { "cf", count, endAction, itemStr };
121 return new DeviceMethod(MethodAction.SCENE, params);
124 public static DeviceMethod buildCronAdd(int value) {
125 return new DeviceMethod(MethodAction.CRON_ADD, new Object[] { 0, value });
128 public static DeviceMethod buildCronGet() {
129 return new DeviceMethod(MethodAction.CRON_ADD, new Object[] { 0 });
132 public static DeviceMethod buildCronDel() {
133 return new DeviceMethod(MethodAction.CRON_DEL, new Object[] { 0 });
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!!!");
140 return new DeviceMethod(MethodAction.ADJUST, new Object[] { action, prop });
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 });
147 return new DeviceMethod(MethodAction.MUSIC, new Object[] { action });
151 public static DeviceMethod buildName(String name) {
152 return new DeviceMethod(MethodAction.NAME, new Object[] { name });
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 });
159 public static DeviceMethod buildBackgroundBrightnessMethd(int brightness, String effect, int duration) {
160 return new DeviceMethod(MethodAction.BG_BRIGHTNESS, new Object[] { brightness, effect, duration });
163 public static DeviceMethod buildQuery(DeviceBase device) {
164 DeviceType type = device.getDeviceType();
167 return new DeviceMethod(MethodAction.PROP, new Object[] { "power", "name", "bright" });
170 return new DeviceMethod(MethodAction.PROP,
171 new Object[] { "power", "name", "bright", "ct", "rgb", "hue", "sat" });
175 return new DeviceMethod(MethodAction.PROP, new Object[] { "power", "name", "bright", "ct" });
178 return new DeviceMethod(MethodAction.PROP, new Object[] { "power", "name", "bright", "ct", "bg_power",
179 "bg_bright", "bg_hue", "bg_sat", "active_mode" });