]> git.basschouten.com Git - openhab-addons.git/blob
13f533218a5a4b16972f69a4762ecdebbfaae062
[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 MonoDevice} contains methods for handling the mono color device.
26  *
27  * @author Coaster Li - Initial contribution
28  */
29 public class MonoDevice extends DeviceBase {
30     private final Logger logger = LoggerFactory.getLogger(MonoDevice.class);
31
32     public MonoDevice(String id) {
33         super(id);
34         mDeviceType = DeviceType.mono;
35         mConnection = new WifiConnection(this);
36     }
37
38     @Override
39     public void onNotify(String msg) {
40         JsonObject result = JsonParser.parseString(msg).getAsJsonObject();
41         try {
42             if (result.has("id")) {
43                 String id = result.get("id").getAsString();
44                 // for cmd transaction.
45
46                 if (mQueryList.contains(id)) {
47                     mQueryList.remove(id);
48                     // DeviceMethod(MethodAction.PROP, new Object[] { "power", "name", "bright" });
49                     JsonArray status = result.get("result").getAsJsonArray();
50
51                     // power:
52                     if (status.get(0).toString().equals("\"off\"")) {
53                         mDeviceStatus.setPowerOff(true);
54                     } else if (status.get(0).toString().equals("\"on\"")) {
55                         mDeviceStatus.setPowerOff(false);
56                     }
57
58                     // name:
59                     mDeviceStatus.setName(status.get(1).getAsString());
60
61                     // brightness:
62                     mDeviceStatus.setBrightness(status.get(2).getAsInt());
63                 }
64             }
65         } catch (Exception e) {
66             logger.debug("Exception", e);
67         }
68
69         super.onNotify(msg);
70     }
71 }