From cc0a5141841f017c1f397bd802fda458e7f62635 Mon Sep 17 00:00:00 2001 From: Volodymyr Lashko Date: Tue, 6 Nov 2018 22:22:13 +0200 Subject: [PATCH] Extend functions of gateway light --- build.gradle | 2 +- .../xaapi/device/XiaomiGatewayLight.java | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 07237c4..cf3bbb4 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ plugins { } group 'com.valashko.xaapi' -version '0.6' +version '0.7' sourceCompatibility = 1.8 diff --git a/src/main/java/com/valashko/xaapi/device/XiaomiGatewayLight.java b/src/main/java/com/valashko/xaapi/device/XiaomiGatewayLight.java index 5b555f7..6b17a41 100644 --- a/src/main/java/com/valashko/xaapi/device/XiaomiGatewayLight.java +++ b/src/main/java/com/valashko/xaapi/device/XiaomiGatewayLight.java @@ -11,6 +11,7 @@ public class XiaomiGatewayLight extends BuiltinDevice { private byte brightness; + private byte previousNonZeroBrightness = 100; private Color color = Color.BLACK; // TODO decide if this is an appropriate default value private HashMap> brightnessCallbacks = new HashMap<>(); @@ -40,6 +41,10 @@ void update(String data) { } } + public boolean getOn() { + return getBrightness() > 0; + } + public byte getBrightness() { return brightness; // TODO query from device } @@ -80,8 +85,19 @@ private void notifyWithColorChange(Color value) { } } + public void setOn(boolean on) throws XaapiException { + if(on) { + setBrightness(previousNonZeroBrightness); + } else { + setBrightness((byte)0); + } + } + public void setBrightness(byte brightness) throws XaapiException { writeBrightnessAndColor(brightness, this.color); + if(this.brightness != 0) { + previousNonZeroBrightness = this.brightness; + } this.brightness = brightness; } @@ -90,7 +106,8 @@ public void setColor(Color color) throws XaapiException { this.color = color; } - private void writeBrightnessAndColor(int brightness, Color color) throws XaapiException { + private void writeBrightnessAndColor(byte brightness, Color color) throws XaapiException { + // TODO verify brightness in range 0..100 JsonObject rgb = new JsonObject(); int rgbValue = brightness << 24 | color.getRGB(); rgb.addProperty("rgb", rgbValue);