Skip to content

Commit 83e32c3

Browse files
authored
feat: update tests to include plug mini (#36)
feat: update commands for per device
1 parent f95ae75 commit 83e32c3

File tree

6 files changed

+89
-19
lines changed

6 files changed

+89
-19
lines changed

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,32 @@ System.out.println("" + status.getBattery());
2727
```
2828
SwitchBotApi instance = SwitchBotApi.getInstance(token, secret);
2929
instance.getDeviceApi().getDevices().stream()
30-
.filter(device -> "Curtain".equals(device.getDeviceType()))
30+
.filter(device -> IDeviceTypes.CURTAIN.equals(device.getDeviceType()))
3131
.filter(Device::isMaster)
3232
.findAny()
3333
.ifPresent(curtain -> {
3434
try {
3535
instance.getDeviceApi()
3636
.sendDeviceControlCommands(
37-
curtain.getDeviceId(), new DeviceCommand("turnOff", "default"));
37+
curtain.getDeviceId(), IDeviceCommands.CURTAIN_CLOSE);
38+
} catch (IOException theE) {
39+
theE.printStackTrace();
40+
}
41+
});
42+
```
43+
44+
4. Send command to turn on a plug mini
45+
46+
```
47+
SwitchBotApi instance = SwitchBotApi.getInstance(token, secret);
48+
instance.getDeviceApi().getDevices().stream()
49+
.filter(device -> IDeviceTypes.PLUG_MINI.equals(device.getDeviceType()))
50+
.findAny()
51+
.ifPresent(plug -> {
52+
try {
53+
instance.getDeviceApi()
54+
.sendDeviceControlCommands(
55+
plug.getDeviceId(), IDeviceCommands.PLUG_MINI_ON);
3856
} catch (IOException theE) {
3957
theE.printStackTrace();
4058
}

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.bigboxer23</groupId>
66
<artifactId>switchbotapi-java</artifactId>
7-
<version>1.1.2</version>
7+
<version>1.1.3</version>
88

99
<name>switchbotapi-java</name>
1010
<url>https://github.com/bigboxer23/switchbotapi-java</url>
@@ -37,7 +37,7 @@
3737
<dependency>
3838
<groupId>com.bigboxer23</groupId>
3939
<artifactId>utils</artifactId>
40-
<version>2.0.19</version>
40+
<version>2.0.23</version>
4141
</dependency>
4242
<dependency>
4343
<groupId>org.projectlombok</groupId>

src/main/java/com/bigboxer23/switch_bot/IDeviceCommands.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ public interface IDeviceCommands {
88

99
String TURN_ON = "turnOn";
1010

11-
DeviceCommand CLOSE_CURTAIN = new DeviceCommand(TURN_OFF, "default");
11+
DeviceCommand CURTAIN_CLOSE = new DeviceCommand(TURN_OFF, "default");
1212

13-
DeviceCommand OPEN_CURTAIN = new DeviceCommand(TURN_ON, "default");
13+
DeviceCommand CURTAIN_OPEN = new DeviceCommand(TURN_ON, "default");
14+
15+
DeviceCommand PLUG_MINI_OFF = new DeviceCommand(TURN_OFF, "default");
16+
17+
DeviceCommand PLUG_MINI_ON = new DeviceCommand(TURN_ON, "default");
1418
}

src/main/java/com/bigboxer23/switch_bot/SwitchBotApi.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.bigboxer23.switch_bot.data.IApiResponse;
44
import com.bigboxer23.utils.http.RequestBuilderCallback;
55
import com.squareup.moshi.Moshi;
6-
import java.io.IOException;
76
import java.nio.charset.StandardCharsets;
87
import java.security.InvalidKeyException;
98
import java.security.NoSuchAlgorithmException;
@@ -36,10 +35,10 @@ private SwitchBotApi(String token, String secret) {
3635
deviceApi = new SwitchBotDeviceApi(this);
3736
}
3837

39-
public static SwitchBotApi getInstance(String token, String secret) throws IOException {
38+
public static SwitchBotApi getInstance(String token, String secret) {
4039
if (token == null || secret == null) {
4140
logger.error("need to define token and secret values.");
42-
throw new IOException("need to define token and secret values.");
41+
throw new RuntimeException("need to define token and secret values.");
4342
}
4443
return Optional.ofNullable(instance).orElseGet(() -> {
4544
instance = new SwitchBotApi(token, secret);

src/main/java/com/bigboxer23/switch_bot/data/Device.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class Device {
2424

2525
private boolean group;
2626

27-
private int moving;
27+
private boolean moving;
2828

2929
private int slidePosition;
3030

@@ -40,4 +40,8 @@ public class Device {
4040
private int electricityOfDay;
4141

4242
private float electricCurrent; // amps / 10
43+
44+
public boolean isPowerOn() {
45+
return "on".equalsIgnoreCase(getPower());
46+
}
4347
}

src/test/java/com/bigboxer23/switch_bot/SwitchBotApiTest.java

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,29 @@
33
import static org.junit.jupiter.api.Assertions.*;
44

55
import com.bigboxer23.switch_bot.data.Device;
6+
import com.bigboxer23.utils.command.Command;
7+
import com.bigboxer23.utils.properties.PropertyUtils;
68
import java.io.IOException;
79
import java.util.List;
810
import org.junit.jupiter.api.Test;
911

1012
/** Need to define environment variables for SwitchBotToken/SwitchBotSecret to run tests */
1113
public class SwitchBotApiTest {
12-
private final String token = System.getenv("SwitchBotToken");
14+
private static final String token = PropertyUtils.getProperty("switchbot_token");
1315

14-
private final String secret = System.getenv("SwitchBotSecret");
16+
private static final String secret = PropertyUtils.getProperty("switchbot_secret");
17+
18+
private static final SwitchBotApi instance = SwitchBotApi.getInstance(token, secret);
1519

1620
@Test
1721
public void testGetDevices() throws IOException {
18-
SwitchBotApi instance = SwitchBotApi.getInstance(token, secret);
1922
List<Device> devices = instance.getDeviceApi().getDevices();
2023
assertFalse(devices.isEmpty());
2124
assertNotNull(devices.get(0).getDeviceId());
2225
}
2326

2427
@Test
2528
public void testDeviceStatus() throws IOException {
26-
SwitchBotApi instance = SwitchBotApi.getInstance(token, secret);
27-
2829
try {
2930
instance.getDeviceApi().getDeviceStatus("123");
3031
fail();
@@ -45,7 +46,7 @@ public void testDeviceStatus() throws IOException {
4546
}
4647
case IDeviceTypes.CURTAIN -> {
4748
assertTrue(status.getSlidePosition() >= 0);
48-
assertTrue(status.getMoving() >= 0);
49+
assertTrue(status.isMoving());
4950
assertTrue(status.getBattery() >= 0);
5051
}
5152
case IDeviceTypes.PLUG_MINI -> {
@@ -60,15 +61,59 @@ public void testDeviceStatus() throws IOException {
6061
}
6162

6263
@Test
63-
public void testDeviceCommands() throws IOException {
64-
SwitchBotApi instance = SwitchBotApi.getInstance(token, secret);
64+
public void testCurtainDeviceCommands() throws IOException, InterruptedException {
6565
Device curtain = instance.getDeviceApi().getDevices().stream()
6666
.filter(device -> IDeviceTypes.CURTAIN.equals(device.getDeviceType()))
6767
.filter(Device::isMaster)
6868
.findAny()
6969
.orElse(null);
7070
assertNotNull(curtain);
7171
assertNotNull(curtain.getDeviceId());
72-
instance.getDeviceApi().sendDeviceControlCommands(curtain.getDeviceId(), IDeviceCommands.CLOSE_CURTAIN);
72+
System.out.println("slide position "
73+
+ instance.getDeviceApi().getDeviceStatus(curtain.getDeviceId()).getSlidePosition());
74+
75+
instance.getDeviceApi().sendDeviceControlCommands(curtain.getDeviceId(), IDeviceCommands.CURTAIN_CLOSE);
76+
pollForStatus(() -> {
77+
int slidePosition = instance.getDeviceApi()
78+
.getDeviceStatus(curtain.getDeviceId())
79+
.getSlidePosition();
80+
System.out.println("slide position " + slidePosition);
81+
return slidePosition >= 90;
82+
});
83+
instance.getDeviceApi().sendDeviceControlCommands(curtain.getDeviceId(), IDeviceCommands.CURTAIN_OPEN);
84+
pollForStatus(() -> {
85+
int slidePosition = instance.getDeviceApi()
86+
.getDeviceStatus(curtain.getDeviceId())
87+
.getSlidePosition();
88+
System.out.println("slide position " + slidePosition);
89+
return slidePosition == 0;
90+
});
91+
}
92+
93+
@Test
94+
public void testPlugDeviceCommands() throws IOException, InterruptedException {
95+
Device plug = instance.getDeviceApi().getDevices().stream()
96+
.filter(device -> IDeviceTypes.PLUG_MINI.equals(device.getDeviceType()))
97+
.findAny()
98+
.orElse(null);
99+
assertNotNull(plug);
100+
101+
instance.getDeviceApi().sendDeviceControlCommands(plug.getDeviceId(), IDeviceCommands.PLUG_MINI_OFF);
102+
pollForStatus(() ->
103+
!instance.getDeviceApi().getDeviceStatus(plug.getDeviceId()).isPowerOn());
104+
105+
instance.getDeviceApi().sendDeviceControlCommands(plug.getDeviceId(), IDeviceCommands.PLUG_MINI_ON);
106+
pollForStatus(() ->
107+
instance.getDeviceApi().getDeviceStatus(plug.getDeviceId()).isPowerOn());
108+
}
109+
110+
private void pollForStatus(Command<Boolean> command) throws IOException, InterruptedException {
111+
boolean result = command.execute();
112+
for (int ai = 0; ai < 10 && !result; ai++) {
113+
System.out.println("sleeping " + ai);
114+
Thread.sleep(2000);
115+
result = command.execute();
116+
}
117+
assertTrue(result);
73118
}
74119
}

0 commit comments

Comments
 (0)