Skip to content

Commit

Permalink
simplify logic by calculate each rollerShutter on its own
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreKoepke committed Jul 13, 2024
1 parent fa31557 commit e8ef702
Showing 1 changed file with 18 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,34 +127,18 @@ private List<Completable> handleWeatherUpdate(Weather weather) {

if (newBrightness > 300) {
highSunLock.blockFor(Duration.ofMinutes(30));
var sunDirection = QuarkusTransaction.requiringNew().call(weatherService::getCurrentSunDirection);
var compassDirection = resolveCompassDirection(sunDirection);

log.info("Sun angles. Zenith %100.0f Azimuth %100.0f or %s".formatted(sunDirection.getZenithAngle(),
sunDirection.getAzimuth(),
compassDirection));

return configs.stream()
.map(config -> handleHighBrightness(config, sunDirection, compassDirection, weather))
.toList();

} else if (newBrightness < 300 && newBrightness > 10 && highSunLock.isGateOpen()) {
return configs.stream()
.filter(RollerShutterService::isOkToOpen)
.map(this::getRollerShutter)
.filter(RollerShutterService::hasNoManualAction)
.map(rollerShutter -> rollerShutter.open("not much light outside"))
.toList();
} else if (newBrightness == 0 && weatherService.outSideDarkFor().compareTo(KEEP_OPEN_AFTER_DARKNESS_FOR) > 0) {
return configs.stream()
.filter(RollerShutterService::isOkToClose)
.map(this::getRollerShutter)
.filter(RollerShutterService::hasNoManualAction)
.map(rollerShutter -> rollerShutter.close("night"))
.toList();
}

return new ArrayList<>();
var sunDirection = QuarkusTransaction.requiringNew().call(weatherService::getCurrentSunDirection);
var compassDirection = resolveCompassDirection(sunDirection);

log.info("Sun angles. Zenith %5.0f Azimuth %5.0f (%s)".formatted(sunDirection.getZenithAngle(),
sunDirection.getAzimuth(),
compassDirection));

return configs.stream()
.map(config -> handleWeatherUpdate(config, sunDirection, compassDirection, weather))
.toList();
}

@NotNull
Expand All @@ -173,26 +157,27 @@ private static boolean hasNoManualAction(RollerShutter rollerShutter) {
}

@NotNull
private Completable handleHighBrightness(RollerShutterConfig config,
private Completable handleWeatherUpdate(RollerShutterConfig config,
AzimuthZenithAngle sunDirection,
CompassDirection compassDirection,
Weather weather) {
var rollerShutter = getRollerShutter(config);
var light = weather.getLight();

if (!hasNoManualAction(rollerShutter)) {
return Completable.complete();
}

if (!isOkToOpen(config)) {
if (!hasNoManualAction(rollerShutter) || !isOkToOpen(config)) {
return Completable.complete();
}

if (!config.getCompassDirection().contains(compassDirection)) {
return rollerShutter.open("wrong compass direction");
}

if (weather.getLight().isBiggerThan(config.getHighSunLevel(), KILO_LUX)) {
if (light.isBiggerThan(config.getHighSunLevel(), KILO_LUX)) {
return openBasedOnZenithAngle(config, rollerShutter, sunDirection.getZenithAngle());
} else if (light.isBiggerThan(10, KILO_LUX) && highSunLock.isGateOpen()) {
return rollerShutter.open("not much light outside");
} else if (isOkToClose(config) && weatherService.outSideDarkFor().compareTo(KEEP_OPEN_AFTER_DARKNESS_FOR) > 0) {
return rollerShutter.close("night");
}

return Completable.complete();
Expand Down

0 comments on commit e8ef702

Please sign in to comment.