]> git.basschouten.com Git - openhab-addons.git/blob
e445188a643706b8eb15ffc00e7ab9d7bc900280
[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.device.connection.WifiConnection;
16 import org.openhab.binding.yeelight.internal.lib.enums.DeviceType;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import com.google.gson.JsonArray;
21 import com.google.gson.JsonObject;
22 import com.google.gson.JsonParser;
23
24 /**
25  * The {@link WonderDevice} contains methods for handling the color bulb device.
26  *
27  * @author Coaster Li - Initial contribution
28  */
29 public class WonderDevice extends DeviceBase {
30     private final Logger logger = LoggerFactory.getLogger(WonderDevice.class);
31
32     public WonderDevice(String id) {
33         super(id);
34         mDeviceType = DeviceType.color;
35         mConnection = new WifiConnection(this);
36         mMinCt = 1700;
37         mMaxCt = 6500;
38     }
39
40     @Override
41     public void onNotify(String msg) {
42         JsonObject result = JsonParser.parseString(msg).getAsJsonObject();
43         try {
44             if (result.has("id")) {
45                 String id = result.get("id").getAsString();
46                 // for cmd transaction.
47
48                 if (mQueryList.contains(id)) {
49                     mQueryList.remove(id);
50                     // DeviceMethod(MethodAction.PROP,
51                     // new Object[] { "power", "name", "bright", "ct", "rgb", "hue", "sat" });
52                     JsonArray status = result.get("result").getAsJsonArray();
53
54                     // power:
55                     if (status.get(0).toString().equals("\"off\"")) {
56                         mDeviceStatus.setPowerOff(true);
57                     } else if (status.get(0).toString().equals("\"on\"")) {
58                         mDeviceStatus.setPowerOff(false);
59                     }
60
61                     // name:
62                     mDeviceStatus.setName(status.get(1).getAsString());
63
64                     // brightness:
65                     mDeviceStatus.setBrightness(status.get(2).getAsInt());
66
67                     // ct:
68                     mDeviceStatus.setCt(status.get(3).getAsInt());
69
70                     // color:
71                     int color = status.get(4).getAsInt();
72                     mDeviceStatus.setColor(color);
73                     mDeviceStatus.setR((color >> 16) & 0xFF);
74                     mDeviceStatus.setG((color >> 8) & 0xFF);
75                     mDeviceStatus.setB(color & 0xFF);
76                     mDeviceStatus.setColor(color);
77                     mDeviceStatus.setHue(status.get(5).getAsInt());
78                     mDeviceStatus.setSat(status.get(6).getAsInt());
79                 }
80             }
81         } catch (Exception e) {
82             logger.debug("Exception", e);
83         }
84
85         super.onNotify(msg);
86     }
87 }