Skip to content

Commit

Permalink
Enhance several devices, bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
valashko committed Sep 14, 2018
1 parent 70a3585 commit 0a4c93a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 35 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.2'
version '0.3'

sourceCompatibility = 1.8

Expand Down
35 changes: 23 additions & 12 deletions src/main/java/com/valashko/xaapi/device/XiaomiDoorWindowSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,41 @@
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<SubscriptionToken, Consumer<String>> 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
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();
Expand All @@ -41,7 +47,12 @@ void update(String data) {
}
}

public Status getLastStatus() {
return lastStatus;
@Override
public Map<SubscriptionToken, Consumer<String>> getActionsCallbacks() {
return actionsCallbacks;
}

public Action getLastAction() {
return lastAction;
}
}
31 changes: 21 additions & 10 deletions src/main/java/com/valashko/xaapi/device/XiaomiMotionSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<SubscriptionToken, Consumer<String>> actionsCallbacks = new HashMap<>();

public XiaomiMotionSensor(XiaomiGateway gateway, String sid) {
XiaomiMotionSensor(XiaomiGateway gateway, String sid) {
super(gateway, sid, Type.XiaomiMotionSensor);
}

Expand All @@ -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();
Expand All @@ -37,7 +43,12 @@ void update(String data) {
}
}

public Status getLastStatus() {
return lastStatus;
@Override
public Map<SubscriptionToken, Consumer<String>> getActionsCallbacks() {
return actionsCallbacks;
}

public Action getLastAction() {
return lastAction;
}
}
35 changes: 23 additions & 12 deletions src/main/java/com/valashko/xaapi/device/XiaomiSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<SubscriptionToken, Consumer<String>> actionsCallbacks = new HashMap<>();

public XiaomiSocket(XiaomiGateway gateway, String sid) {
XiaomiSocket(XiaomiGateway gateway, String sid) {
super(gateway, sid, Type.XiaomiSocket);
}

Expand All @@ -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();
Expand All @@ -45,8 +51,13 @@ void update(String data) {
}
}

public Status getLastStatus() {
return lastStatus;
@Override
public Map<SubscriptionToken, Consumer<String>> getActionsCallbacks() {
return actionsCallbacks;
}

public Action getLastAction() {
return lastAction;
}

public void turnOn() throws XaapiException {
Expand Down

0 comments on commit 0a4c93a

Please sign in to comment.