Skip to content

Commit

Permalink
feat: add retry logic for switchbot calls, bump some libs
Browse files Browse the repository at this point in the history
  • Loading branch information
bigboxer23 committed Mar 27, 2024
1 parent 549a2b1 commit 9907eff
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 46 deletions.
6 changes: 3 additions & 3 deletions VeraAutomationHub.iml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.15.3" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains.kotlin:kotlin-reflect:1.9.21" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains.kotlin:kotlin-stdlib:1.9.21" level="project" />
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.30" level="project" />
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.32" level="project" />
<orderEntry type="library" name="Maven: net.logstash.logback:logstash-logback-encoder:7.4" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-starter-test:3.2.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test:3.2.1" level="project" />
Expand Down Expand Up @@ -176,7 +176,7 @@
<orderEntry type="library" name="Maven: com.google.oauth-client:google-oauth-client-jetty:1.34.1" level="project" />
<orderEntry type="library" name="Maven: com.google.oauth-client:google-oauth-client-java6:1.34.1" level="project" />
<orderEntry type="library" name="Maven: com.google.apis:google-api-services-calendar:v3-rev20230707-2.0.0" level="project" />
<orderEntry type="library" name="Maven: com.bigboxer23:utils:2.0.22" level="project" />
<orderEntry type="library" name="Maven: com.bigboxer23:utils:2.0.27" level="project" />
<orderEntry type="library" name="Maven: commons-io:commons-io:2.15.1" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:2.0.9" level="project" />
<orderEntry type="library" name="Maven: com.squareup.okhttp3:okhttp:4.12.0" level="project" />
Expand All @@ -185,7 +185,7 @@
<orderEntry type="library" name="Maven: org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.9.21" level="project" />
<orderEntry type="library" name="Maven: com.squareup.moshi:moshi:1.15.1" level="project" />
<orderEntry type="library" name="Maven: com.squareup.okio:okio-jvm:3.7.0" level="project" />
<orderEntry type="library" name="Maven: com.bigboxer23:switchbotapi-java:1.1.3" level="project" />
<orderEntry type="library" name="Maven: com.bigboxer23:switchbotapi-java:1.1.5" level="project" />
<orderEntry type="library" name="Maven: com.bigboxer23:govee-java-api:1.1.4" level="project" />
<orderEntry type="library" name="Maven: com.hivemq:hivemq-mqtt-client:1.3.3" level="project" />
<orderEntry type="library" name="Maven: io.reactivex.rxjava2:rxjava:2.2.21" level="project" />
Expand Down
30 changes: 15 additions & 15 deletions VeraAutomationHub.ipr
Original file line number Diff line number Diff line change
Expand Up @@ -326,28 +326,28 @@
<root url="jar://$MAVEN_REPOSITORY$/com/bigboxer23/govee-java-api/1.1.4/govee-java-api-1.1.4-sources.jar!/" />
</SOURCES>
</library>
<library name="Maven: com.bigboxer23:switchbotapi-java:1.1.3" type="java-imported" external-system-id="Maven">
<properties groupId="com.bigboxer23" artifactId="switchbotapi-java" version="1.1.3" />
<library name="Maven: com.bigboxer23:switchbotapi-java:1.1.5" type="java-imported" external-system-id="Maven">
<properties groupId="com.bigboxer23" artifactId="switchbotapi-java" version="1.1.5" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/bigboxer23/switchbotapi-java/1.1.3/switchbotapi-java-1.1.3.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/bigboxer23/switchbotapi-java/1.1.5/switchbotapi-java-1.1.5.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/com/bigboxer23/switchbotapi-java/1.1.3/switchbotapi-java-1.1.3-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/bigboxer23/switchbotapi-java/1.1.5/switchbotapi-java-1.1.5-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/com/bigboxer23/switchbotapi-java/1.1.3/switchbotapi-java-1.1.3-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/bigboxer23/switchbotapi-java/1.1.5/switchbotapi-java-1.1.5-sources.jar!/" />
</SOURCES>
</library>
<library name="Maven: com.bigboxer23:utils:2.0.22" type="java-imported" external-system-id="Maven">
<properties groupId="com.bigboxer23" artifactId="utils" version="2.0.22" />
<library name="Maven: com.bigboxer23:utils:2.0.27" type="java-imported" external-system-id="Maven">
<properties groupId="com.bigboxer23" artifactId="utils" version="2.0.27" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/bigboxer23/utils/2.0.22/utils-2.0.22.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/bigboxer23/utils/2.0.27/utils-2.0.27.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/com/bigboxer23/utils/2.0.22/utils-2.0.22-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/bigboxer23/utils/2.0.27/utils-2.0.27-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/com/bigboxer23/utils/2.0.22/utils-2.0.22-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/bigboxer23/utils/2.0.27/utils-2.0.27-sources.jar!/" />
</SOURCES>
</library>
<library name="Maven: com.carrotsearch:hppc:0.8.1" type="java-imported" external-system-id="Maven">
Expand Down Expand Up @@ -1910,16 +1910,16 @@
<root url="jar://$MAVEN_REPOSITORY$/org/ow2/asm/asm/9.3/asm-9.3-sources.jar!/" />
</SOURCES>
</library>
<library name="Maven: org.projectlombok:lombok:1.18.30" type="java-imported" external-system-id="Maven">
<properties groupId="org.projectlombok" artifactId="lombok" version="1.18.30" />
<library name="Maven: org.projectlombok:lombok:1.18.32" type="java-imported" external-system-id="Maven">
<properties groupId="org.projectlombok" artifactId="lombok" version="1.18.32" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/projectlombok/lombok/1.18.30/lombok-1.18.30.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/projectlombok/lombok/1.18.32/lombok-1.18.32.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/projectlombok/lombok/1.18.30/lombok-1.18.30-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/projectlombok/lombok/1.18.32/lombok-1.18.32-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/projectlombok/lombok/1.18.30/lombok-1.18.30-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/projectlombok/lombok/1.18.32/lombok-1.18.32-sources.jar!/" />
</SOURCES>
</library>
<library name="Maven: org.reactivestreams:reactive-streams:1.0.4" type="java-imported" external-system-id="Maven">
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@
<dependency>
<groupId>com.bigboxer23</groupId>
<artifactId>utils</artifactId>
<version>2.0.22</version>
<version>2.0.27</version>
</dependency>
<dependency>
<groupId>com.bigboxer23</groupId>
<artifactId>switchbotapi-java</artifactId>
<version>1.1.3</version>
<version>1.1.5</version>
</dependency>
<dependency>
<groupId>com.bigboxer23</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.bigboxer23.govee.data.GoveeEvent;
import com.bigboxer23.lights.controllers.switchbot.SwitchBotController;
import com.bigboxer23.switch_bot.IDeviceCommands;
import com.bigboxer23.utils.command.RetryingCommand;
import com.google.gson.*;
import java.io.IOException;
import org.slf4j.Logger;
Expand Down Expand Up @@ -77,10 +78,15 @@ public RefillAction(String pumpId, String humidifierModel, String humidifierId)
public void run() {
try {
logger.info("starting pump " + pumpId);
switchbotController
.getSwitchbotAPI()
.getDeviceApi()
.sendDeviceControlCommands(pumpId, IDeviceCommands.PLUG_MINI_ON);
RetryingCommand.execute(
() -> {
switchbotController
.getSwitchbotAPI()
.getDeviceApi()
.sendDeviceControlCommands(pumpId, IDeviceCommands.PLUG_MINI_ON);
return null;
},
pumpId);
Thread.sleep(5 * 1000);

logger.info("starting humidifier " + humidifierId);
Expand All @@ -89,18 +95,28 @@ public void run() {
Thread.sleep(2 * 60 * 1000); // 2 min

logger.info("stopping pump " + pumpId);
switchbotController
.getSwitchbotAPI()
.getDeviceApi()
.sendDeviceControlCommands(pumpId, IDeviceCommands.PLUG_MINI_OFF);
RetryingCommand.execute(
() -> {
switchbotController
.getSwitchbotAPI()
.getDeviceApi()
.sendDeviceControlCommands(pumpId, IDeviceCommands.PLUG_MINI_OFF);
return null;
},
pumpId);
} catch (IOException | InterruptedException e) {
logger.error("error refilling humidifier, attempting to turn off pump " + pumpId, e);
try {
Thread.sleep(5 * 1000); // 5 sec
switchbotController
.getSwitchbotAPI()
.getDeviceApi()
.sendDeviceControlCommands(pumpId, IDeviceCommands.PLUG_MINI_OFF);
RetryingCommand.execute(
() -> {
switchbotController
.getSwitchbotAPI()
.getDeviceApi()
.sendDeviceControlCommands(pumpId, IDeviceCommands.PLUG_MINI_OFF);
return null;
},
pumpId);
} catch (IOException | InterruptedException e2) {
logger.error("error turning off pump " + pumpId, e2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.bigboxer23.switch_bot.IDeviceTypes;
import com.bigboxer23.switch_bot.SwitchBotApi;
import com.bigboxer23.switch_bot.data.Device;
import com.bigboxer23.utils.command.RetryingCommand;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
Expand Down Expand Up @@ -50,12 +51,14 @@ public SwitchBotApi getSwitchbotAPI() throws IOException {
private String getCurtainId() throws IOException {
if (curtainId == null) {
logger.info("fetching curtain id");
curtainId = getSwitchbotAPI().getDeviceApi().getDevices().stream()
.filter(device -> IDeviceTypes.CURTAIN.equals(device.getDeviceType()))
.filter(Device::isMaster)
.findAny()
.map(Device::getDeviceId)
.orElse(null);
curtainId = RetryingCommand.execute(
() -> getSwitchbotAPI().getDeviceApi().getDevices().stream()
.filter(device -> IDeviceTypes.CURTAIN.equals(device.getDeviceType()))
.filter(Device::isMaster)
.findAny()
.map(Device::getDeviceId)
.orElse(null),
"CurtainIdFetch");
}
return curtainId;
}
Expand All @@ -68,7 +71,14 @@ private String getCurtainId() throws IOException {
})
public void openCurtains() throws IOException {
logger.info("open curtain requested");
getSwitchbotAPI().getDeviceApi().sendDeviceControlCommands(getCurtainId(), IDeviceCommands.CURTAIN_OPEN);
RetryingCommand.execute(
() -> {
getSwitchbotAPI()
.getDeviceApi()
.sendDeviceControlCommands(getCurtainId(), IDeviceCommands.CURTAIN_OPEN);
return null;
},
getCurtainId());
}

@GetMapping(value = "/S/switchbot/closeCurtain", produces = MediaType.APPLICATION_JSON_VALUE)
Expand All @@ -79,7 +89,14 @@ public void openCurtains() throws IOException {
})
public void closeCurtains() throws IOException {
logger.info("close curtain requested");
getSwitchbotAPI().getDeviceApi().sendDeviceControlCommands(getCurtainId(), IDeviceCommands.CURTAIN_CLOSE);
RetryingCommand.execute(
() -> {
getSwitchbotAPI()
.getDeviceApi()
.sendDeviceControlCommands(getCurtainId(), IDeviceCommands.CURTAIN_CLOSE);
return null;
},
getCurtainId());
}

private String getDeviceName(String deviceId) throws IOException {
Expand All @@ -102,12 +119,20 @@ public void plugMini(
@Parameter(description = "command to run. Possible values [0-100, ON, OFF]")
@PathVariable(value = "command")
String command)
throws IOException {
throws IOException, InterruptedException {
logger.info(getDeviceName(deviceId) + ":" + deviceId + ": " + command + " plug-mini requested");
getSwitchbotAPI()
.getDeviceApi()
.sendDeviceControlCommands(
deviceId,
"on".equalsIgnoreCase(command) ? IDeviceCommands.PLUG_MINI_ON : IDeviceCommands.PLUG_MINI_OFF);
RetryingCommand.execute(
() -> {
getSwitchbotAPI()
.getDeviceApi()
.sendDeviceControlCommands(
deviceId,
"on".equalsIgnoreCase(command)
? IDeviceCommands.PLUG_MINI_ON
: IDeviceCommands.PLUG_MINI_OFF);

return null;
},
deviceId);
}
}

0 comments on commit 9907eff

Please sign in to comment.