]> git.basschouten.com Git - openhab-addons.git/blob
ab853cd9ed530c22c2b0ae085d167908ba14a14c
[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 CeilingDeviceWithNightDevice} contains methods for handling the ceiling device with ambient light.
27  *
28  * @author Nikita Pogudalov - Initial contribution
29  */
30 public class CeilingDeviceWithNightDevice extends CeilingDevice implements DeviceWithNightlight {
31     private final Logger logger = LoggerFactory.getLogger(CeilingDeviceWithNightDevice.class);
32
33     public CeilingDeviceWithNightDevice(String id) {
34         super(id);
35
36         mDeviceType = DeviceType.ceiling1;
37     }
38
39     @Override
40     public void onNotify(String msg) {
41         logger.debug("Got state: {}", msg);
42
43         JsonObject result = JsonParser.parseString(msg).getAsJsonObject();
44
45         if (result.has("id")) {
46             String id = result.get("id").getAsString();
47             // for cmd transaction.
48
49             if (mQueryList.contains(id)) {
50                 JsonArray status = result.get("result").getAsJsonArray();
51
52                 final int activeMode = status.get(8).getAsInt();
53                 mDeviceStatus.setActiveMode(ActiveMode.values()[activeMode]);
54             }
55         }
56
57         super.onNotify(msg);
58     }
59
60     @Override
61     public void toggleNightlightMode(boolean turnOn) {
62         if (turnOn) {
63             mConnection.invoke(
64                     new DeviceMethod(MethodAction.SCENE, new Object[] { "nightlight", mDeviceStatus.getBrightness() }));
65         } else {
66             mConnection.invoke(MethodFactory.buildCTMethod(mDeviceStatus.getCt(), DeviceMethod.EFFECT_SMOOTH, 500));
67         }
68     }
69 }