diff --git a/build.gradle b/build.gradle index 5e05bce..81ce16d 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ plugins { } group 'com.valashko.xaapi' -version '0.2' +version '0.3' sourceCompatibility = 1.8 diff --git a/src/main/java/com/valashko/xaapi/device/XiaomiDoorWindowSensor.java b/src/main/java/com/valashko/xaapi/device/XiaomiDoorWindowSensor.java index 6645667..1262656 100644 --- a/src/main/java/com/valashko/xaapi/device/XiaomiDoorWindowSensor.java +++ b/src/main/java/com/valashko/xaapi/device/XiaomiDoorWindowSensor.java @@ -4,17 +4,22 @@ import com.google.gson.JsonSyntaxException; import com.valashko.xaapi.XaapiException; -public class XiaomiDoorWindowSensor extends SlaveDevice { +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; - public enum Status { +public class XiaomiDoorWindowSensor extends SlaveDevice implements IInteractiveDevice { + + public enum Action { Open, Close } - private Status lastStatus; + private Action lastAction; + private HashMap> actionsCallbacks = new HashMap<>(); - public XiaomiDoorWindowSensor(XiaomiGateway gateway, String sid) { - super(gateway, sid, Type.XiaomiCube); + XiaomiDoorWindowSensor(XiaomiGateway gateway, String sid) { + super(gateway, sid, Type.XiaomiDoorWindowSensor); } @Override @@ -22,17 +27,18 @@ void update(String data) { try { JsonObject o = JSON_PARSER.parse(data).getAsJsonObject(); if (o.has("status")) { - String status = o.get("status").getAsString(); - switch(status) { + String action = o.get("status").getAsString(); + switch(action) { case "open": - lastStatus = Status.Open; + lastAction = Action.Open; break; case "close": - lastStatus = Status.Close; + lastAction = Action.Close; break; default: - throw new XaapiException("Unexpected status: " + status); + throw new XaapiException("Unexpected action: " + action); } + notifyWithAction(action); } } catch (XaapiException e) { e.printStackTrace(); @@ -41,7 +47,12 @@ void update(String data) { } } - public Status getLastStatus() { - return lastStatus; + @Override + public Map> getActionsCallbacks() { + return actionsCallbacks; + } + + public Action getLastAction() { + return lastAction; } } diff --git a/src/main/java/com/valashko/xaapi/device/XiaomiMotionSensor.java b/src/main/java/com/valashko/xaapi/device/XiaomiMotionSensor.java index 19fada9..4dd7ed3 100644 --- a/src/main/java/com/valashko/xaapi/device/XiaomiMotionSensor.java +++ b/src/main/java/com/valashko/xaapi/device/XiaomiMotionSensor.java @@ -4,15 +4,20 @@ import com.google.gson.JsonSyntaxException; import com.valashko.xaapi.XaapiException; -public class XiaomiMotionSensor extends SlaveDevice { +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; - public enum Status { +public class XiaomiMotionSensor extends SlaveDevice implements IInteractiveDevice { + + public enum Action { Motion } - private Status lastStatus; + private Action lastAction; + private HashMap> actionsCallbacks = new HashMap<>(); - public XiaomiMotionSensor(XiaomiGateway gateway, String sid) { + XiaomiMotionSensor(XiaomiGateway gateway, String sid) { super(gateway, sid, Type.XiaomiMotionSensor); } @@ -21,14 +26,15 @@ void update(String data) { try { JsonObject o = JSON_PARSER.parse(data).getAsJsonObject(); if (o.has("status")) { - String status = o.get("status").getAsString(); - switch(status) { + String action = o.get("status").getAsString(); + switch(action) { case "motion": - lastStatus = Status.Motion; + lastAction = Action.Motion; break; default: - throw new XaapiException("Unknown status: " + status); + throw new XaapiException("Unknown action: " + action); } + notifyWithAction(action); } } catch (XaapiException e) { e.printStackTrace(); @@ -37,7 +43,12 @@ void update(String data) { } } - public Status getLastStatus() { - return lastStatus; + @Override + public Map> getActionsCallbacks() { + return actionsCallbacks; + } + + public Action getLastAction() { + return lastAction; } } diff --git a/src/main/java/com/valashko/xaapi/device/XiaomiSocket.java b/src/main/java/com/valashko/xaapi/device/XiaomiSocket.java index cfb6ed9..b8142dc 100644 --- a/src/main/java/com/valashko/xaapi/device/XiaomiSocket.java +++ b/src/main/java/com/valashko/xaapi/device/XiaomiSocket.java @@ -4,17 +4,22 @@ import com.google.gson.JsonSyntaxException; import com.valashko.xaapi.XaapiException; -public class XiaomiSocket extends SlaveDevice { +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; - public enum Status { +public class XiaomiSocket extends SlaveDevice implements IInteractiveDevice { + + public enum Action { On, Off, Unknown // probably device is offline } - private Status lastStatus; + private Action lastAction; + private HashMap> actionsCallbacks = new HashMap<>(); - public XiaomiSocket(XiaomiGateway gateway, String sid) { + XiaomiSocket(XiaomiGateway gateway, String sid) { super(gateway, sid, Type.XiaomiSocket); } @@ -23,20 +28,21 @@ void update(String data) { try { JsonObject o = JSON_PARSER.parse(data).getAsJsonObject(); if (o.has("status")) { - String status = o.get("status").getAsString(); - switch(status) { + String action = o.get("status").getAsString(); + switch(action) { case "on": - lastStatus = Status.On; + lastAction = Action.On; break; case "off": - lastStatus = Status.Off; + lastAction = Action.Off; break; case "unknown": - lastStatus = Status.Unknown; + lastAction = Action.Unknown; break; default: - throw new XaapiException("Unknown status: " + status); + throw new XaapiException("Unknown action: " + action); } + notifyWithAction(action); } } catch (XaapiException e) { e.printStackTrace(); @@ -45,8 +51,13 @@ void update(String data) { } } - public Status getLastStatus() { - return lastStatus; + @Override + public Map> getActionsCallbacks() { + return actionsCallbacks; + } + + public Action getLastAction() { + return lastAction; } public void turnOn() throws XaapiException {