Skip to content

Commit

Permalink
Introduce minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
valashko committed Oct 24, 2018
1 parent 3562287 commit 0b49531
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
4 changes: 2 additions & 2 deletions 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.4'
version '0.5'

sourceCompatibility = 1.8

Expand All @@ -12,6 +12,6 @@ repositories {
}

dependencies {
implementation 'com.google.code.gson:gson:2.8.+'
implementation 'com.google.code.gson:gson:2.8.5'
}

8 changes: 7 additions & 1 deletion src/main/java/com/valashko/xaapi/device/BuiltinDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ public enum Type {
}

protected static JsonParser JSON_PARSER = new JsonParser();
private String uid;
private Type type;

public BuiltinDevice(Type type) {
public BuiltinDevice(String uid, Type type) {
this.uid = uid;
this.type = type;
}

public String getUid() {
return uid;
}

public Type getType() {
return type;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/valashko/xaapi/device/XiaomiGateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public static XiaomiGateway discover() throws IOException, XaapiException {
public XiaomiGateway(String ip) throws IOException, XaapiException {
this.incomingMulticastChannel = new IncomingMulticastChannel(GROUP, PORT);
this.directChannel = new DirectChannel(ip, PORT);
configureBuiltinDevices();
queryDevices();
configureBuiltinDevices();
}
public XiaomiGateway(String ip, String password) throws IOException, XaapiException {
this(ip);
Expand All @@ -77,8 +77,8 @@ public Map<String, SlaveDevice> getKnownDevices() {
}

private void configureBuiltinDevices() {
builtinLight = new XiaomiGatewayLight();
builtinIlluminationSensor = new XiaomiGatewayIlluminationSensor();
builtinLight = new XiaomiGatewayLight(sid);
builtinIlluminationSensor = new XiaomiGatewayIlluminationSensor(sid);
}

private void configureCipher(String password) throws XaapiException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class XiaomiGatewayIlluminationSensor extends BuiltinDevice {
private int illumination;
private HashMap<IInteractiveDevice.SubscriptionToken, Consumer<Integer>> illuminationChangeCallbacks = new HashMap<>();

public XiaomiGatewayIlluminationSensor() {
super(Type.XiaomiGatewayIlluminationSensor);
public XiaomiGatewayIlluminationSensor(String gatewaySid) {
super(gatewaySid + ":illumination", Type.XiaomiGatewayIlluminationSensor);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public class XiaomiGatewayLight extends BuiltinDevice {
private byte brightness;
private Color color;

public XiaomiGatewayLight() {
super(Type.XiaomiGatewayLight);
public XiaomiGatewayLight(String gatewaySid) {
super(gatewaySid + ":light", Type.XiaomiGatewayLight);
}

@Override
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/valashko/xaapi/device/XiaomiMotionSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public enum Action {

private Action lastAction;
private HashMap<SubscriptionToken, Consumer<String>> actionsCallbacks = new HashMap<>();
private HashMap<SubscriptionToken, Runnable> motionCallbacks = new HashMap<>();

XiaomiMotionSensor(XiaomiGateway gateway, String sid) {
super(gateway, sid, Type.XiaomiMotionSensor);
Expand All @@ -30,6 +31,7 @@ void update(String data) {
switch(action) {
case "motion":
lastAction = Action.Motion;
notifyWithMotion();
break;
default:
throw new XaapiException("Unknown action: " + action);
Expand All @@ -51,4 +53,20 @@ public Map<SubscriptionToken, Consumer<String>> getActionsCallbacks() {
public Action getLastAction() {
return lastAction;
}

public SubscriptionToken subscribeForMotion(Runnable callback) {
SubscriptionToken token = new SubscriptionToken();
motionCallbacks.put(token, callback);
return token;
}

public void unsubscribeForMotion(SubscriptionToken token) {
motionCallbacks.remove(token);
}

private void notifyWithMotion() {
for(Runnable r : motionCallbacks.values()) {
r.run();
}
}
}

0 comments on commit 0b49531

Please sign in to comment.