From 9907eff8c75c6ac188553af24f39de1e834518e6 Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Wed, 27 Mar 2024 13:44:22 -0500 Subject: [PATCH] feat: add retry logic for switchbot calls, bump some libs --- VeraAutomationHub.iml | 6 +-- VeraAutomationHub.ipr | 30 +++++------ pom.xml | 4 +- .../govee/GoveeHumidifierController.java | 40 +++++++++----- .../switchbot/SwitchBotController.java | 53 ++++++++++++++----- 5 files changed, 87 insertions(+), 46 deletions(-) diff --git a/VeraAutomationHub.iml b/VeraAutomationHub.iml index aa0556b..ac4e9b5 100644 --- a/VeraAutomationHub.iml +++ b/VeraAutomationHub.iml @@ -59,7 +59,7 @@ - + @@ -176,7 +176,7 @@ - + @@ -185,7 +185,7 @@ - + diff --git a/VeraAutomationHub.ipr b/VeraAutomationHub.ipr index 7b7191f..ec8c305 100644 --- a/VeraAutomationHub.ipr +++ b/VeraAutomationHub.ipr @@ -326,28 +326,28 @@ - - + + - + - + - + - - + + - + - + - + @@ -1910,16 +1910,16 @@ - - + + - + - + - + diff --git a/pom.xml b/pom.xml index e7319c5..42070b0 100644 --- a/pom.xml +++ b/pom.xml @@ -107,12 +107,12 @@ com.bigboxer23 utils - 2.0.22 + 2.0.27 com.bigboxer23 switchbotapi-java - 1.1.3 + 1.1.5 com.bigboxer23 diff --git a/src/main/java/com/bigboxer23/lights/controllers/govee/GoveeHumidifierController.java b/src/main/java/com/bigboxer23/lights/controllers/govee/GoveeHumidifierController.java index 423d0ae..a9dc145 100644 --- a/src/main/java/com/bigboxer23/lights/controllers/govee/GoveeHumidifierController.java +++ b/src/main/java/com/bigboxer23/lights/controllers/govee/GoveeHumidifierController.java @@ -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; @@ -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); @@ -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); } diff --git a/src/main/java/com/bigboxer23/lights/controllers/switchbot/SwitchBotController.java b/src/main/java/com/bigboxer23/lights/controllers/switchbot/SwitchBotController.java index 965e3f8..ede3a4e 100644 --- a/src/main/java/com/bigboxer23/lights/controllers/switchbot/SwitchBotController.java +++ b/src/main/java/com/bigboxer23/lights/controllers/switchbot/SwitchBotController.java @@ -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; @@ -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; } @@ -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) @@ -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 { @@ -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); } }