package de.ph87.data.demo; import de.ph87.data.series.Series; import de.ph87.data.series.SeriesMode; import de.ph87.data.series.SeriesRepository; import de.ph87.data.series.measure.MeasureEvent; import de.ph87.data.series.period.PeriodRepository; import de.ph87.data.series.period.consumption.ConsumptionRepository; import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.ZoneId; import java.time.ZonedDateTime; import static de.ph87.data.grid.GridReceiver.GRID_DELIVERY_SERIES_NAME; import static de.ph87.data.grid.GridReceiver.GRID_PURCHASE_SERIES_NAME; import static de.ph87.data.oil.OilController.OIL_SERIES_NAME; import static de.ph87.data.photovoltaic.PhotovoltaicMqttReceiver.PHOTOVOLTAIC_ENERGY_SERIES_NAME; @Slf4j @Service @Transactional @RequiredArgsConstructor @SuppressWarnings("SameParameterValue") public class DemoService { private final ApplicationEventPublisher applicationEventPublisher; private final SeriesRepository seriesRepository; private final PeriodRepository periodRepository; private final ConsumptionRepository consumptionRepository; @EventListener(ApplicationStartedEvent.class) public void startup() { series(PHOTOVOLTAIC_ENERGY_SERIES_NAME, SeriesMode.INCREASING); series(GRID_PURCHASE_SERIES_NAME, SeriesMode.INCREASING); series(GRID_DELIVERY_SERIES_NAME, SeriesMode.INCREASING); final Series oil = series(OIL_SERIES_NAME, SeriesMode.DECREASING); oil.setPeriod(null); consumptionRepository.deleteAllByIdPeriodSeries(oil); periodRepository.deleteAllBySeries(oil); oil(2020, 8, 10, 189, 4500); oil(2020, 8, 22, 4275); oil(2021, 5, 27, 1050, 4500); oil(2021, 11, 25, 3213); oil(2022, 3, 3, 999, 2664); oil(2022, 3, 7, 2550); oil(2022, 3, 11, 2439); oil(2022, 4, 12, 1968); oil(2022, 5, 6, 1767, 4464); oil(2022, 7, 20, 4275); oil(2022, 9, 5, 4200); oil(2022, 9, 27, 4071); oil(2022, 11, 22, 3675); oil(2022, 12, 4, 3513); oil(2022, 12, 15, 3342); oil(2022, 12, 19, 3213); oil(2023, 1, 8, 2964); oil(2023, 2, 14, 2334); oil(2023, 4, 3, 1734); oil(2023, 4, 12, 1500, 4500); oil(2023, 5, 8, 4389); oil(2023, 9, 24, 4071); oil(2024, 9, 12, 1275, 4575); } private Series series(@NonNull final String name, @NonNull final SeriesMode mode) { return seriesRepository.findByNameOrAliasesContains(name, name).orElseGet(() -> seriesRepository.save(new Series(name, mode))); } private void oil(final int year, final int month, final int day, final int beforeRefill, final int afterRefill) { oil2(year, month, day, 0, beforeRefill); oil2(year, month, day, 1, afterRefill); } private void oil(final int year, final int month, final int day, final int value) { oil2(year, month, day, 0, value); } private void oil2(final int year, final int month, final int day, final int second, final int value) { final ZonedDateTime date = ZonedDateTime.of(year, month, day, 12, 0, second, 0, ZoneId.systemDefault()); applicationEventPublisher.publishEvent(new MeasureEvent(OIL_SERIES_NAME, "", date, value)); } }