]> git.basschouten.com Git - openhab-addons.git/blob
e16ab76e5c8d0e11a7c39de378916488e0f1775c
[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.ActiveMode;
16 import org.openhab.binding.yeelight.internal.lib.enums.DeviceType;
17 import org.openhab.binding.yeelight.internal.lib.enums.MethodAction;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import com.google.gson.JsonArray;
22 import com.google.gson.JsonObject;
23 import com.google.gson.JsonParser;
24
25 /**
26  * The {@link CeilingDeviceWithAmbientDevice} contains methods for handling the ceiling device with ambient light.
27  *
28  * @author Viktor Koop - Initial contribution
29  */
30 public class CeilingDeviceWithAmbientDevice extends CeilingDevice
31         implements DeviceWithAmbientLight, DeviceWithNightlight {
32     private final Logger logger = LoggerFactory.getLogger(CeilingDeviceWithAmbientDevice.class);
33
34     public CeilingDeviceWithAmbientDevice(String id) {
35         super(id);
36
37         mDeviceType = DeviceType.ceiling4;
38     }
39
40     @Override
41     public void onNotify(String msg) {
42         logger.debug("Got state: {}", msg);
43
44         JsonObject result = JsonParser.parseString(msg).getAsJsonObject();
45
46         if (result.has("id")) {
47             String id = result.get("id").getAsString();
48             // for cmd transaction.
49
50             if (mQueryList.contains(id)) {
51                 JsonArray status = result.get("result").getAsJsonArray();
52
53                 final String backgroundPowerState = status.get(4).toString();
54                 if ("\"off\"".equals(backgroundPowerState)) {
55                     mDeviceStatus.setBackgroundIsPowerOff(true);
56                 } else if ("\"on\"".equals(backgroundPowerState)) {
57                     mDeviceStatus.setBackgroundIsPowerOff(false);
58                 }
59
60                 final int backgroundBrightness = status.get(5).getAsInt();
61                 mDeviceStatus.setBackgroundBrightness(backgroundBrightness);
62
63                 final int backgroundHue = status.get(6).getAsInt();
64                 mDeviceStatus.setBackgroundHue(backgroundHue);
65
66                 final int backgroundSaturation = status.get(7).getAsInt();
67                 mDeviceStatus.setBackgroundSat(backgroundSaturation);
68
69                 final int activeMode = status.get(8).getAsInt();
70                 mDeviceStatus.setActiveMode(ActiveMode.values()[activeMode]);
71             }
72         }
73
74         super.onNotify(msg);
75     }
76
77     @Override
78     public void setBackgroundColor(int hue, int saturation, int duration) {
79         mConnection
80                 .invoke(MethodFactory.buildBackgroundHSVMethod(hue, saturation, DeviceMethod.EFFECT_SMOOTH, duration));
81     }
82
83     @Override
84     public void setBackgroundBrightness(int brightness, int duration) {
85         mConnection
86                 .invoke(MethodFactory.buildBackgroundBrightnessMethd(brightness, DeviceMethod.EFFECT_SMOOTH, duration));
87     }
88
89     @Override
90     public void setBackgroundPower(boolean on, int duration) {
91         mConnection.invoke(new DeviceMethod(MethodAction.BG_SWITCH,
92                 new Object[] { on ? "on" : "off", DeviceMethod.EFFECT_SMOOTH, duration }));
93     }
94
95     @Override
96     public void toggleNightlightMode(boolean turnOn) {
97         if (turnOn) {
98             mConnection.invoke(
99                     new DeviceMethod(MethodAction.SCENE, new Object[] { "nightlight", mDeviceStatus.getBrightness() }));
100         } else {
101             mConnection.invoke(MethodFactory.buildCTMethod(mDeviceStatus.getCt(), DeviceMethod.EFFECT_SMOOTH, 500));
102         }
103     }
104 }