Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a controller to check multiple sensors and control humidity… #119

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.bigboxer23.lights.controllers.aggregate;

import com.bigboxer23.lights.controllers.switchbot.SwitchBotController;
import com.bigboxer23.switch_bot.IDeviceCommands;
import com.bigboxer23.switch_bot.data.Device;
import com.bigboxer23.utils.command.RetryingCommand;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
* This component takes one or more sensors (switchbot) and takes an average humidity reading. It
* will turn on or off a switchbot smart switch which should be connected to a dehumidifier based on
* the reading and defined low/high values of RH
*/
@Component
public class MultipointDehumidifierController implements InitializingBean {
private static final Logger logger = LoggerFactory.getLogger(MultipointDehumidifierController.class);

@Value("${dehumidifierId}")
private String dehumidifierId;

@Value("${sensorIds}")
private String sensorIdString;

@Value("${lowHumidity}")
private float lowHumidity;

@Value("${highHumidity}")
private float highHumidity;

private List<String> sensorIds;

private final SwitchBotController switchbotController;

public MultipointDehumidifierController(SwitchBotController switchbotController) {
this.switchbotController = switchbotController;
logger.info("MultipointDehumidifierController initialized");
}

@Override
public void afterPropertiesSet() throws Exception {
sensorIds = Arrays.stream(sensorIdString.split(",")).map(String::trim).toList();
}

@Scheduled(fixedDelay = 300000) // 5min
private void checkHumidity() throws IOException {
double humidity = getHumidity();
logger.info("humidity: " + humidity);
Device dehumidifier = RetryingCommand.execute(
() -> switchbotController.getSwitchbotAPI().getDeviceApi().getDeviceStatus(dehumidifierId),
dehumidifierId);
if (humidity > highHumidity && !dehumidifier.isPowerOn()) {
logger.info("turning on dehumidifier " + dehumidifierId);
RetryingCommand.execute(
() -> {
switchbotController
.getSwitchbotAPI()
.getDeviceApi()
.sendDeviceControlCommands(dehumidifierId, IDeviceCommands.PLUG_MINI_ON);
return null;
},
dehumidifierId);
} else if (humidity < lowHumidity && dehumidifier.isPowerOn()) {
logger.info("turning off dehumidifier " + dehumidifierId);
RetryingCommand.execute(
() -> {
switchbotController
.getSwitchbotAPI()
.getDeviceApi()
.sendDeviceControlCommands(dehumidifierId, IDeviceCommands.PLUG_MINI_OFF);
return null;
},
dehumidifierId);
}
}

private double getHumidity() {
logger.debug("checking humidity");
return sensorIds.stream()
.map(id -> {
try {
return RetryingCommand.execute(
() -> switchbotController
.getSwitchbotAPI()
.getDeviceApi()
.getDeviceStatus(id),
id);
} catch (IOException e) {
logger.error("", e);
throw new RuntimeException(e);
}
})
.map(Device::getHumidity)
.mapToDouble(a -> a)
.average()
.orElse(-1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
Expand Down Expand Up @@ -163,7 +160,8 @@ private boolean findLateEvents(Events events) {
.truncatedTo(ChronoUnit.MINUTES)
.withMinute(30)
.withHour(22)
.atZone(ZoneId.of(event.getEnd().getTimeZone()))
.atZone(ZoneId.of(Optional.ofNullable(event.getEnd().getTimeZone())
.orElseGet(() -> ZoneId.systemDefault().getId())))
.toInstant());
boolean found = event.getEnd() != null
&& event.getEnd().getDateTime() != null
Expand Down