]> git.basschouten.com Git - openhab-addons.git/blob
36a3b4eb1dd02d77555d0a893e0476336a7b9190
[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 CeilingDevice} contains methods for handling the ceiling device.
26  *
27  * @author Coaster Li - Initial contribution
28  */
29 public class CeilingDevice extends DeviceBase {
30     private final Logger logger = LoggerFactory.getLogger(CeilingDevice.class);
31
32     public CeilingDevice(String id) {
33         super(id);
34         mDeviceType = DeviceType.ceiling;
35         mConnection = new WifiConnection(this);
36         mMinCt = 2700;
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, new Object[] { "power", "name", "bright", "ct" });
51                     JsonArray status = result.get("result").getAsJsonArray();
52
53                     // power:
54                     if ("\"off\"".equals(status.get(0).toString())) {
55                         mDeviceStatus.setPowerOff(true);
56                     } else if ("\"on\"".equals(status.get(0).toString())) {
57                         mDeviceStatus.setPowerOff(false);
58                     }
59
60                     // name:
61                     mDeviceStatus.setName(status.get(1).getAsString());
62
63                     // brightness:
64                     mDeviceStatus.setBrightness(status.get(2).getAsInt());
65
66                     // ct:
67                     mDeviceStatus.setCt(status.get(3).getAsInt());
68                 }
69             }
70         } catch (Exception e) {
71             logger.debug("Problem setting values", e);
72         }
73
74         super.onNotify(msg);
75     }
76 }