Skip to content

Commit

Permalink
Extend functions of gateway light
Browse files Browse the repository at this point in the history
  • Loading branch information
valashko committed Nov 6, 2018
1 parent 12e1732 commit cc0a514
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'com.valashko.xaapi'
version '0.6'
version '0.7'

sourceCompatibility = 1.8

Expand Down
19 changes: 18 additions & 1 deletion src/main/java/com/valashko/xaapi/device/XiaomiGatewayLight.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<IInteractiveDevice.SubscriptionToken, Consumer<Byte>> brightnessCallbacks = new HashMap<>();
Expand Down Expand Up @@ -40,6 +41,10 @@ void update(String data) {
}
}

public boolean getOn() {
return getBrightness() > 0;
}

public byte getBrightness() {
return brightness; // TODO query from device
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
Expand Down

0 comments on commit cc0a514

Please sign in to comment.