Astro entity removed

This commit is contained in:
Patrick Haßel 2024-09-11 16:26:51 +02:00
parent 80e61a5c6c
commit 188d13591f
7 changed files with 2 additions and 177 deletions

View File

@ -1,6 +1,5 @@
import {validateBooleanNotNull, validateListOrEmpty, validateNumberNotNull, validateStringNotEmptyNotNull} from "../validators"; import {validateBooleanNotNull, validateListOrEmpty, validateNumberNotNull, validateStringNotEmptyNotNull} from "../validators";
import {ScheduleEntry} from "./entry/ScheduleEntry"; import {ScheduleEntry} from "./entry/ScheduleEntry";
import {Astro} from "../astro/Astro";
export class Schedule { export class Schedule {
@ -13,7 +12,6 @@ export class Schedule {
readonly enabled: boolean, readonly enabled: boolean,
readonly title: string, readonly title: string,
readonly entries: ScheduleEntry[], readonly entries: ScheduleEntry[],
readonly astros: Astro[],
) { ) {
this.next = entries.filter(e => e.nextFuzzyTimestamp).sort((a, b) => a.nextFuzzyTimestamp.date.getTime() - b.nextFuzzyTimestamp.date.getTime())[0]; this.next = entries.filter(e => e.nextFuzzyTimestamp).sort((a, b) => a.nextFuzzyTimestamp.date.getTime() - b.nextFuzzyTimestamp.date.getTime())[0];
this.last = entries.filter(e => e.lastFuzzyTimestamp).sort((a, b) => b.lastFuzzyTimestamp.date.getTime() - a.lastFuzzyTimestamp.date.getTime())[0]; this.last = entries.filter(e => e.lastFuzzyTimestamp).sort((a, b) => b.lastFuzzyTimestamp.date.getTime() - a.lastFuzzyTimestamp.date.getTime())[0];
@ -25,7 +23,6 @@ export class Schedule {
validateBooleanNotNull(json['enabled']), validateBooleanNotNull(json['enabled']),
validateStringNotEmptyNotNull(json['title']), validateStringNotEmptyNotNull(json['title']),
validateListOrEmpty(json['entries'], ScheduleEntry.fromJson, ScheduleEntry.comparePosition), validateListOrEmpty(json['entries'], ScheduleEntry.fromJson, ScheduleEntry.comparePosition),
validateListOrEmpty(json['astros'], Astro.fromJson),
); );
} }

View File

@ -1,11 +1,9 @@
package de.ph87.homeautomation.schedule; package de.ph87.homeautomation.schedule;
import de.ph87.homeautomation.schedule.astro.AstroDto;
import de.ph87.homeautomation.schedule.entry.ScheduleEntryDto; import de.ph87.homeautomation.schedule.entry.ScheduleEntryDto;
import lombok.Getter; import lombok.Getter;
import java.io.Serializable; import java.io.Serializable;
import java.util.List;
import java.util.Set; import java.util.Set;
@Getter @Getter
@ -19,14 +17,11 @@ public class ScheduleDto implements Serializable {
private final Set<ScheduleEntryDto> entries; private final Set<ScheduleEntryDto> entries;
private final List<AstroDto> astros; public ScheduleDto(final Schedule schedule, final Set<ScheduleEntryDto> entries) {
public ScheduleDto(final Schedule schedule, final Set<ScheduleEntryDto> entries, final List<AstroDto> astros) {
this.id = schedule.getId(); this.id = schedule.getId();
this.enabled = schedule.isEnabled(); this.enabled = schedule.isEnabled();
this.title = schedule.getTitle(); this.title = schedule.getTitle();
this.entries = entries; this.entries = entries;
this.astros = astros;
} }
} }

View File

@ -1,7 +1,5 @@
package de.ph87.homeautomation.schedule; package de.ph87.homeautomation.schedule;
import de.ph87.homeautomation.schedule.astro.AstroDto;
import de.ph87.homeautomation.schedule.astro.AstroService;
import de.ph87.homeautomation.schedule.entry.ScheduleEntryDto; import de.ph87.homeautomation.schedule.entry.ScheduleEntryDto;
import de.ph87.homeautomation.schedule.entry.ScheduleEntryMapper; import de.ph87.homeautomation.schedule.entry.ScheduleEntryMapper;
import de.ph87.homeautomation.web.WebSocketService; import de.ph87.homeautomation.web.WebSocketService;
@ -10,7 +8,6 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -24,12 +21,9 @@ public class ScheduleMapper {
private final WebSocketService webSocketService; private final WebSocketService webSocketService;
private final AstroService astroService;
public ScheduleDto toDto(final Schedule schedule) { public ScheduleDto toDto(final Schedule schedule) {
final Set<ScheduleEntryDto> entries = schedule.getEntries().stream().map(scheduleEntryMapper::toDto).collect(Collectors.toSet()); final Set<ScheduleEntryDto> entries = schedule.getEntries().stream().map(scheduleEntryMapper::toDto).collect(Collectors.toSet());
final List<AstroDto> astros = astroService.findAllNext(); return new ScheduleDto(schedule, entries);
return new ScheduleDto(schedule, entries, astros);
} }
public void publish(final Schedule schedule, final boolean existing) { public void publish(final Schedule schedule, final boolean existing) {

View File

@ -1,44 +0,0 @@
package de.ph87.homeautomation.schedule.astro;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
@Entity
@Getter
@ToString
@NoArgsConstructor
public class Astro {
@Id
@GeneratedValue
private long id;
@Version
private long version;
@Setter
private boolean enabled = true;
@Setter
private String error;
@Column(unique = true)
private double zenith;
@Column(nullable = false)
private String name;
private String differentNameForSunset;
public Astro(final double zenith, @NonNull final String name, @Nullable final String differentNameForSunset) {
this.zenith = zenith;
this.name = name;
this.differentNameForSunset = differentNameForSunset;
}
}

View File

@ -1,37 +0,0 @@
package de.ph87.homeautomation.schedule.astro;
import lombok.Getter;
import lombok.ToString;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import java.time.ZonedDateTime;
@Getter
@ToString
public class AstroDto {
@NonNull
private final double zenith;
@NonNull
private final ZonedDateTime sunrise;
@NonNull
private final ZonedDateTime sunset;
@NonNull
private final String sunriseName;
@Nullable
private final String sunsetName;
public AstroDto(@NonNull final Astro astro, @NonNull final ZonedDateTime sunrise, @NonNull final ZonedDateTime sunset) {
this.zenith = astro.getZenith();
this.sunrise = sunrise;
this.sunset = sunset;
this.sunriseName = astro.getName();
this.sunsetName = astro.getDifferentNameForSunset() == null ? astro.getName() : astro.getDifferentNameForSunset();
}
}

View File

@ -1,11 +0,0 @@
package de.ph87.homeautomation.schedule.astro;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface AstroRepository extends JpaRepository<Astro, Long> {
List<Astro> findAllByEnabledTrue();
}

View File

@ -1,69 +0,0 @@
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);
}
}