Skip to content

Commit 6ba92b6

Browse files
authored
feat: revise error handling, add plug mini (us) (#32)
1 parent a937f86 commit 6ba92b6

File tree

7 files changed

+113
-103
lines changed

7 files changed

+113
-103
lines changed

pom.xml

+51-47
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.1</version>
7+
<version>1.1.2</version>
88

99
<name>switchbotapi-java</name>
1010
<url>https://github.com/bigboxer23/switchbotapi-java</url>
@@ -103,53 +103,57 @@
103103
<artifactId>maven-project-info-reports-plugin</artifactId>
104104
<version>3.5.0</version>
105105
</plugin>
106-
<plugin>
107-
<groupId>com.diffplug.spotless</groupId>
108-
<artifactId>spotless-maven-plugin</artifactId>
109-
<version>2.43.0</version>
110-
<configuration>
111-
<java>
112-
<googleJavaFormat>
113-
<version>1.15.0</version>
114-
<style>AOSP</style>
115-
<reflowLongStrings>true</reflowLongStrings>
116-
</googleJavaFormat>
117-
<palantirJavaFormat/>
118-
<indent>
119-
<tabs>true</tabs>
120-
<spacesPerTab>4</spacesPerTab>
121-
</indent>
122-
<removeUnusedImports/>
123-
<formatAnnotations/>
124-
</java>
125-
<pom>
126-
<includes>
127-
<include>pom.xml</include>
128-
</includes>
129-
<sortPom>
130-
<expandEmptyElements>false</expandEmptyElements>
131-
</sortPom>
132-
</pom>
133-
<markdown>
134-
<includes>
135-
<include>*.md</include>
136-
</includes>
137-
<excludes>
138-
<exclude>license.md</exclude>
139-
</excludes>
140-
<flexmark/>
141-
</markdown>
142-
</configuration>
143-
<executions>
144-
<execution>
145-
<goals>
146-
<goal>apply</goal>
147-
</goals>
148-
<phase>compile</phase>
149-
</execution>
150-
</executions>
151-
</plugin>
152106
</plugins>
153107
</pluginManagement>
108+
<plugins>
109+
<plugin>
110+
<groupId>com.diffplug.spotless</groupId>
111+
<artifactId>spotless-maven-plugin</artifactId>
112+
<version>2.43.0</version>
113+
<configuration>
114+
<java>
115+
<googleJavaFormat>
116+
<version>1.17.0</version>
117+
<style>AOSP</style>
118+
<reflowLongStrings>true</reflowLongStrings>
119+
</googleJavaFormat>
120+
<palantirJavaFormat>
121+
<version>2.35.0</version>
122+
</palantirJavaFormat>
123+
<indent>
124+
<tabs>true</tabs>
125+
<spacesPerTab>4</spacesPerTab>
126+
</indent>
127+
<removeUnusedImports/>
128+
<formatAnnotations/>
129+
</java>
130+
<pom>
131+
<includes>
132+
<include>pom.xml</include>
133+
</includes>
134+
<sortPom>
135+
<expandEmptyElements>false</expandEmptyElements>
136+
</sortPom>
137+
</pom>
138+
<markdown>
139+
<includes>
140+
<include>*.md</include>
141+
</includes>
142+
<excludes>
143+
<exclude>license.md</exclude>
144+
</excludes>
145+
<flexmark/>
146+
</markdown>
147+
</configuration>
148+
<executions>
149+
<execution>
150+
<goals>
151+
<goal>apply</goal>
152+
</goals>
153+
<phase>compile</phase>
154+
</execution>
155+
</executions>
156+
</plugin>
157+
</plugins>
154158
</build>
155159
</project>

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
import com.bigboxer23.switch_bot.data.DeviceCommand;
44

5-
/**
6-
*
7-
*/
8-
public interface IDeviceCommands
9-
{
5+
/** */
6+
public interface IDeviceCommands {
107
String TURN_OFF = "turnOff";
118

129
String TURN_ON = "turnOn";
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.bigboxer23.switch_bot;
22

3-
/**
4-
*
5-
*/
6-
public interface IDeviceTypes
7-
{
3+
/** */
4+
public interface IDeviceTypes {
85
String CURTAIN = "Curtain";
96
String HUB2 = "Hub 2";
107
String METER = "Meter";
118

129
String WOIOSENSOR = "WoIOSensor";
10+
11+
String PLUG_MINI = "Plug Mini (US)";
1312
}

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

+12-5
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,17 @@ protected RequestBuilderCallback addAuth() {
8080
* @return true if error occurs
8181
*/
8282
protected boolean checkForError(IApiResponse apiResponse) {
83-
if (Optional.ofNullable(apiResponse).map(IApiResponse::getStatusCode).orElse(-1) != 100) {
84-
logger.error("error code: " + apiResponse.getStatusCode() + " : " + apiResponse.getMessage());
85-
return true;
86-
}
87-
return false;
83+
return Optional.ofNullable(apiResponse)
84+
.map(response -> {
85+
if (response.getStatusCode() != 100) {
86+
logger.error("error code: " + response.getStatusCode() + " : " + response.getMessage());
87+
return false;
88+
}
89+
return true;
90+
})
91+
.orElseGet(() -> {
92+
logger.error("null api response");
93+
return false;
94+
});
8895
}
8996
}

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

+17-38
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
import java.io.IOException;
66
import java.net.URLDecoder;
77
import java.nio.charset.StandardCharsets;
8-
import java.util.Collections;
98
import java.util.List;
10-
import java.util.Optional;
119
import okhttp3.RequestBody;
1210
import okhttp3.Response;
1311
import org.slf4j.Logger;
@@ -21,6 +19,7 @@ public class SwitchBotDeviceApi {
2119
protected SwitchBotDeviceApi(SwitchBotApi provider) {
2220
this.provider = provider;
2321
}
22+
2423
/**
2524
* @see <a href="https://github.com/OpenWonderLabs/SwitchBotAPI#get-device-list">Get device
2625
* list</a>
@@ -29,19 +28,7 @@ protected SwitchBotDeviceApi(SwitchBotApi provider) {
2928
*/
3029
public List<Device> getDevices() throws IOException {
3130
try (Response response = OkHttpUtil.getSynchronous(SwitchBotApi.baseUrl + "v1.1/devices", provider.addAuth())) {
32-
if (!response.isSuccessful()) {
33-
throw new IOException(response.message());
34-
}
35-
ApiResponse apiResponse = provider.getMoshi()
36-
.adapter(ApiResponse.class)
37-
.fromJson(response.body().string());
38-
if (provider.checkForError(apiResponse)) {
39-
return Collections.emptyList();
40-
}
41-
return Optional.ofNullable(apiResponse)
42-
.map(ApiResponse::getBody)
43-
.map(ApiResponseBody::getDeviceList)
44-
.orElse(Collections.emptyList());
31+
return parseResponse(response, ApiResponse.class).getBody().getDeviceList();
4532
}
4633
}
4734

@@ -57,22 +44,7 @@ public Device getDeviceStatus(String deviceId) throws IOException {
5744
}
5845
try (Response response = OkHttpUtil.getSynchronous(
5946
SwitchBotApi.baseUrl + "v1.1/devices/" + deviceId + "/status", provider.addAuth())) {
60-
if (!response.isSuccessful()) {
61-
throw new IOException(response.message());
62-
}
63-
DeviceApiResponse apiResponse = provider.getMoshi()
64-
.adapter(DeviceApiResponse.class)
65-
.fromJson(response.body().string());
66-
if (Optional.ofNullable(apiResponse)
67-
.map(DeviceApiResponse::getStatusCode)
68-
.orElse(-1)
69-
!= 100) {
70-
logger.error("error code: " + apiResponse.getStatusCode() + " : " + apiResponse.getMessage());
71-
return null;
72-
}
73-
return provider.checkForError(apiResponse)
74-
? null
75-
: Optional.of(apiResponse).map(DeviceApiResponse::getBody).orElse(null);
47+
return parseResponse(response, DeviceApiResponse.class).getBody();
7648
}
7749
}
7850

@@ -89,13 +61,20 @@ public void sendDeviceControlCommands(String deviceId, DeviceCommand command) th
8961
RequestBody.create(URLDecoder.decode(stringCommand, StandardCharsets.UTF_8.displayName())
9062
.getBytes(StandardCharsets.UTF_8)),
9163
provider.addAuth())) {
92-
if (!response.isSuccessful()) {
93-
throw new IOException(response.message());
94-
}
95-
DeviceApiResponse apiResponse = provider.getMoshi()
96-
.adapter(DeviceApiResponse.class)
97-
.fromJson(response.body().string());
98-
provider.checkForError(apiResponse);
64+
parseResponse(response, DeviceApiResponse.class);
65+
}
66+
}
67+
68+
private <T extends IApiResponse> T parseResponse(Response response, Class<T> clazz) throws IOException {
69+
if (!response.isSuccessful()) {
70+
throw new IOException(response.code() + " " + response.message() + " "
71+
+ response.body().string());
72+
}
73+
String body = response.body().string();
74+
T apiResponse = provider.getMoshi().adapter(clazz).fromJson(body);
75+
if (!provider.checkForError(apiResponse)) {
76+
throw new IOException(response.code() + " " + response.message() + " " + body);
9977
}
78+
return apiResponse;
10079
}
10180
}

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

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.bigboxer23.switch_bot.data;
22

3+
import com.squareup.moshi.Json;
34
import lombok.Data;
45

56
/** */
@@ -28,4 +29,15 @@ public class Device {
2829
private int slidePosition;
2930

3031
private boolean master;
32+
33+
private String power;
34+
35+
private float voltage;
36+
37+
@Json(name = "weight")
38+
private float watts;
39+
40+
private int electricityOfDay;
41+
42+
private float electricCurrent; // amps / 10
3143
}

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

+15-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ public void testGetDevices() throws IOException {
2424
@Test
2525
public void testDeviceStatus() throws IOException {
2626
SwitchBotApi instance = SwitchBotApi.getInstance(token, secret);
27-
assertNull(instance.getDeviceApi().getDeviceStatus("123"));
27+
28+
try {
29+
instance.getDeviceApi().getDeviceStatus("123");
30+
fail();
31+
} catch (IOException e) {
32+
33+
}
2834
for (Device device : instance.getDeviceApi().getDevices()) {
2935
assertNotNull(device.getDeviceId());
3036
Device status = instance.getDeviceApi().getDeviceStatus(device.getDeviceId());
@@ -42,6 +48,13 @@ public void testDeviceStatus() throws IOException {
4248
assertTrue(status.getMoving() >= 0);
4349
assertTrue(status.getBattery() >= 0);
4450
}
51+
case IDeviceTypes.PLUG_MINI -> {
52+
assertTrue("on".equals(status.getPower()) || "off".equals(status.getPower()));
53+
assertTrue(status.getVoltage() > 0);
54+
assertTrue(status.getWatts() > -1);
55+
assertTrue(status.getElectricityOfDay() > 0);
56+
assertTrue(status.getElectricCurrent() > -1);
57+
}
4558
}
4659
}
4760
}
@@ -56,7 +69,6 @@ public void testDeviceCommands() throws IOException {
5669
.orElse(null);
5770
assertNotNull(curtain);
5871
assertNotNull(curtain.getDeviceId());
59-
instance.getDeviceApi()
60-
.sendDeviceControlCommands(curtain.getDeviceId(), IDeviceCommands.CLOSE_CURTAIN);
72+
instance.getDeviceApi().sendDeviceControlCommands(curtain.getDeviceId(), IDeviceCommands.CLOSE_CURTAIN);
6173
}
6274
}

0 commit comments

Comments
 (0)