Homeautomation/src/main/java/de/ph87/homeautomation/schedule/astro/AstroService.java

70 lines
3.1 KiB
Java

package de.ph87.homeautomation.schedule.astro;
import lombok.*;
import org.springframework.boot.context.event.*;
import org.springframework.context.event.EventListener;
import org.springframework.transaction.annotation.*;
import org.springframework.web.bind.annotation.*;
import java.time.*;
import java.util.*;
@RestController
@Transactional
@RequestMapping("Astro")
@RequiredArgsConstructor
public class AstroService {
private final AstroRepository astroRepository;
private final AstroCalculator astroCalculator;
@EventListener(ApplicationStartedEvent.class)
public void startup() {
if (astroRepository.count() == 0) {
astroRepository.save(new Astro(71.0000, "Aufgang +++++", "Untergang +++++"));
astroRepository.save(new Astro(75.0000, "Aufgang ++++", "Untergang ++++"));
astroRepository.save(new Astro(80.0000, "Aufgang +++", "Untergang +++"));
astroRepository.save(new Astro(85.0000, "Aufgang ++", "Untergang ++"));
astroRepository.save(new Astro(90.0000, "Aufgang +", "Untergang +"));
astroRepository.save(new Astro(90.8333, "Aufgang", "Untergang"));
astroRepository.save(new Astro(92.0000, "Aufgang -", "Untergang -"));
astroRepository.save(new Astro(93.0000, "Aufgang --", "Untergang --"));
astroRepository.save(new Astro(94.0000, "Bürgerlich ++", null));
astroRepository.save(new Astro(95.0000, "Bürgerlich +", null));
astroRepository.save(new Astro(96.0000, "Bürgerlich", null));
astroRepository.save(new Astro(97.0000, "Bürgerlich -", null));
astroRepository.save(new Astro(98.0000, "Bürgerlich --", null));
astroRepository.save(new Astro(99.0000, "Bürgerlich ---", null));
astroRepository.save(new Astro(100.000, "Nautisch ++", null));
astroRepository.save(new Astro(101.000, "Nautisch +", null));
astroRepository.save(new Astro(102.000, "Nautisch", null));
astroRepository.save(new Astro(103.000, "Nautisch -", null));
astroRepository.save(new Astro(104.000, "Nautisch --", null));
astroRepository.save(new Astro(105.000, "Nautisch ---", null));
astroRepository.save(new Astro(106.000, "Astronomisch ++", null));
astroRepository.save(new Astro(107.000, "Astronomisch +", null));
astroRepository.save(new Astro(108.000, "Astronomisch", null));
astroRepository.save(new Astro(110.000, "Astronomisch -", null));
astroRepository.save(new Astro(120.000, "Astronomisch --", null));
}
}
public List<AstroDto> findAllNext() {
final ZonedDateTime now = ZonedDateTime.now();
return astroRepository.findAllByEnabledTrue().stream().map(astro -> next(now, astro)).filter(Objects::nonNull).toList();
}
private AstroDto next(final ZonedDateTime now, final Astro astro) {
final ZonedDateTime sunrise = astroCalculator.next(now, true, astro.getZenith());
final ZonedDateTime sunset = astroCalculator.next(now, false, astro.getZenith());
if (sunrise == null || sunset == null) {
astro.setEnabled(false);
astro.setError("sunrise (%s) or sunset (%s) NULL for %s".formatted(sunrise, sunset, now));
return null;
}
return new AstroDto(astro, sunrise, sunset);
}
}