Skip to content

Commit

Permalink
feat: update humidifier refill flow to cope with manual button press …
Browse files Browse the repository at this point in the history
…needed on govee side (#88)
  • Loading branch information
bigboxer23 authored Mar 29, 2024
1 parent 1d39630 commit f6ff8a4
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 14 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.1</version>
<version>3.2.4</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
Expand Down Expand Up @@ -62,17 +62,17 @@
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>6.2.2</version>
<version>6.2.3</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>6.2.2</version>
<version>6.2.3</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>6.2.2</version>
<version>6.2.3</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public String doAction(
@Parameter(description = "used with SetAutoCloseDelay. Seconds to set delay for", required = false)
@PathVariable(value = "delay", required = false)
Long delay) {
myLogger.error("Garage Door change requested: " + command);
myLogger.info("Garage Door change requested: " + command);
myGarageData = fromJson(myGarageURL + "/" + command + (delay != null ? "/" + delay : ""), VeraDeviceVO.class);
myGarageData.setName("Garage Opener");
myGarageData.setStatus(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz").format(new Date()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class GoveeHumidifierController implements InitializingBean {
@Value("${humidfier_to_pump_map}")
private String MAP_KEY;

private JsonObject humidifierToPumpMap;
private HumidifierData data;

private static final Logger logger = LoggerFactory.getLogger(GoveeHumidifierController.class);

Expand All @@ -40,19 +40,21 @@ public GoveeHumidifierController(SwitchBotController switchbotController) {

private void setupListeners() {
logger.warn("starting govee event listener");
humidifierToPumpMap = JsonParser.parseString(MAP_KEY).getAsJsonObject();
data = new GsonBuilder().create().fromJson(MAP_KEY, HumidifierData.class);
GoveeApi.getInstance(API_KEY).subscribeToGoveeEvents(new GoveeEventSubscriber() {
@Override
public void messageReceived(GoveeEvent event) {
if (event.isLackWaterEvent()) {
logger.warn(
"no water: " + event.getModel() + " " + event.getDeviceId() + " " + event.getDeviceName());
JsonPrimitive pump = humidifierToPumpMap.getAsJsonPrimitive(event.getDeviceId());
if (pump == null) {
logger.warn("No pump for " + event.getDeviceId());
HumidifierCluster cluster = data.get(event.getDeviceId());
if (cluster == null) {
logger.warn("No cluster for " + event.getDeviceId());
return;
}
new Thread(new RefillAction(pump.getAsString(), event.getModel(), event.getDeviceId())).start();
new Thread(new RefillAction(
cluster.getPump(), event.getModel(), event.getDeviceId(), cluster.getOutlet()))
.start();
}
}
});
Expand All @@ -68,15 +70,28 @@ private class RefillAction implements Runnable {
private final String humidifierModel;
private final String humidifierId;

public RefillAction(String pumpId, String humidifierModel, String humidifierId) {
private final String humidifierOutletId;

public RefillAction(String pumpId, String humidifierModel, String humidifierId, String humidifierOutletId) {
this.pumpId = pumpId;
this.humidifierModel = humidifierModel;
this.humidifierId = humidifierId;
this.humidifierOutletId = humidifierOutletId;
}

@Override
public void run() {
try {
logger.info("manual turn off of humidifier " + humidifierOutletId);
RetryingCommand.execute(
() -> {
switchbotController
.getSwitchbotAPI()
.getDeviceApi()
.sendDeviceControlCommands(humidifierOutletId, IDeviceCommands.PLUG_MINI_OFF);
return null;
},
humidifierOutletId);
logger.info("starting pump " + pumpId);
RetryingCommand.execute(
() -> {
Expand All @@ -89,10 +104,23 @@ public void run() {
pumpId);
Thread.sleep(5 * 1000);

logger.info("manual turn on of humidifier " + humidifierOutletId);
RetryingCommand.execute(
() -> {
switchbotController
.getSwitchbotAPI()
.getDeviceApi()
.sendDeviceControlCommands(humidifierOutletId, IDeviceCommands.PLUG_MINI_ON);
return null;
},
humidifierOutletId);

Thread.sleep(60 * 1000); // 1 min
logger.info("starting humidifier " + humidifierId);
GoveeDeviceCommandResponse response = GoveeApi.getInstance(API_KEY)
.sendDeviceCommand(IHumidifierCommands.turnOn(humidifierModel, humidifierId));
Thread.sleep(2 * 60 * 1000); // 2 min

Thread.sleep(60 * 1000); // 1 min

logger.info("stopping pump " + pumpId);
RetryingCommand.execute(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.bigboxer23.lights.controllers.govee;

import lombok.Data;

/** */
@Data
public class HumidifierCluster {
private String pump;

private String humidifier;

private String outlet;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.bigboxer23.lights.controllers.govee;

import java.util.HashMap;

/** */
public class HumidifierData extends HashMap<String, HumidifierCluster> {}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void enableTokenRequest(HttpServletRequest theRequest) {
})
public TokenResponse getToken(HttpServletResponse theResponse, HttpServletRequest theRequest) throws IOException {
if (myTokenValidTime + kTokenValidTime > System.currentTimeMillis()) {
myLogger.error("Token distributed to " + theRequest.getRemoteAddr());
myLogger.warn("Token distributed to " + theRequest.getRemoteAddr());
Cookie anAuthCookie = new Cookie("t", myToken);
anAuthCookie.setHttpOnly(true);
anAuthCookie.setSecure(true);
Expand Down

0 comments on commit f6ff8a4

Please sign in to comment.