2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.yeelight.internal.lib.device;
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;
20 import com.google.gson.JsonArray;
21 import com.google.gson.JsonObject;
22 import com.google.gson.JsonParser;
25 * The {@link PitayaDevice} contains methods for handling the light strip device.
27 * @author Coaster Li - Initial contribution
29 public class PitayaDevice extends DeviceBase {
30 private final Logger logger = LoggerFactory.getLogger(PitayaDevice.class);
32 public PitayaDevice(String id) {
34 mDeviceType = DeviceType.stripe;
35 mConnection = new WifiConnection(this);
41 public void onNotify(String msg) {
42 JsonObject result = JsonParser.parseString(msg).getAsJsonObject();
44 if (result.has("id")) {
45 String id = result.get("id").getAsString();
46 // for cmd transaction.
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();
55 if ("\"off\"".equals(status.get(0).toString())) {
56 mDeviceStatus.setPowerOff(true);
57 } else if ("\"on\"".equals(status.get(0).toString())) {
58 mDeviceStatus.setPowerOff(false);
62 mDeviceStatus.setName(status.get(1).getAsString());
65 mDeviceStatus.setBrightness(status.get(2).getAsInt());
68 mDeviceStatus.setCt(status.get(3).getAsInt());
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());
81 } catch (Exception e) {
82 logger.debug("Exception", e);