package de.ph87.homeautomation; import com.luckycatlabs.sunrisesunset.Zenith; import de.ph87.homeautomation.device.DeviceRepository; import de.ph87.homeautomation.device.DeviceWriteService; import de.ph87.homeautomation.device.devices.DeviceDto; import de.ph87.homeautomation.knx.group.KnxGroup; import de.ph87.homeautomation.knx.group.KnxGroupRepository; import de.ph87.homeautomation.knx.group.KnxGroupWriteService; import de.ph87.homeautomation.property.Property; import de.ph87.homeautomation.property.PropertyDto; import de.ph87.homeautomation.schedule.Schedule; import de.ph87.homeautomation.schedule.ScheduleRepository; import de.ph87.homeautomation.schedule.entry.ScheduleEntry; import de.ph87.homeautomation.schedule.entry.ScheduleEntryType; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import tuwien.auto.calimero.GroupAddress; import java.time.ZonedDateTime; @SuppressWarnings({"unchecked", "UnusedReturnValue", "SameParameterValue", "UnusedAssignment", "RedundantSuppression"}) @Slf4j @Service @Transactional @RequiredArgsConstructor public class DemoDataService { private static final int MIN30 = 30 * 60; private static final Zenith BETWEEN_OFFICIAL_AND_CIVIL = new Zenith(93.0); private final KnxGroupWriteService knxGroupWriteService; private final ScheduleRepository scheduleRepository; private final KnxGroupRepository knxGroupRepository; private final DeviceWriteService deviceWriteService; private final DeviceRepository deviceRepository; public void insertDemoData() { final KnxGroup eg_flur_licht_schalten = createKnxGroupIfNotExists("EG Flur Licht Schalten", 0, 5, 14, "1.001", false, false); final KnxGroup ambiente_eg_status = createKnxGroupIfNotExists("Ambiente EG Status", 0, 3, 81, "1.001", true, false); final KnxGroup ambiente_eg_schalten = createKnxGroupIfNotExists("Ambiente EG Schalten", 0, 3, 80, "1.001", false, false); final KnxGroup ambiente_og_status = createKnxGroupIfNotExists("Ambiente OG Status", 0, 6, 2, "1.001", true, false); final KnxGroup ambiente_og_schalten = createKnxGroupIfNotExists("Ambiente OG Schalten", 0, 6, 3, "1.001", false, false); final KnxGroup wohnzimmer_rollladen_position_anfahren = createKnxGroupIfNotExists("Wohnzimmer Rollladen Position Anfahren", 0, 4, 24, "5.001", false, false); final KnxGroup schlafzimmer_rollladen_position_anfahren = createKnxGroupIfNotExists("Schlafzimmer Rollladen Position Anfahren", 0, 3, 3, "5.001", false, false); final KnxGroup flur_og_rollladen_position_anfahren = createKnxGroupIfNotExists("Flur Rollladen Position Anfahren", 0, 5, 13, "5.001", false, false); final KnxGroup bad_licht_status = createKnxGroupIfNotExists("Bad Licht Status", 0, 5, 19, "1.001", true, false); final KnxGroup bad_licht_schalten = createKnxGroupIfNotExists("Bad Licht Schalten", 0, 3, 73, "1.001", false, false); final KnxGroup bad_licht_mitte_status = createKnxGroupIfNotExists("Bad Licht Mitte Status", 0, 3, 30, "1.001", true, false); final KnxGroup bad_licht_mitte_schalten = createKnxGroupIfNotExists("Bad Licht Mitte Schalten", 0, 3, 29, "1.001", false, false); final KnxGroup helligkeit = createKnxGroupIfNotExists("Helligkeit", 0, 5, 6, "9.004", false, true); if (deviceRepository.count() == 0) { createDeviceSwitch("Ambiente EG", ambiente_eg_status, ambiente_eg_schalten); createDeviceSwitch("Ambiente OG", ambiente_og_status, ambiente_og_schalten); createDeviceSwitch("Bad Licht", bad_licht_status, bad_licht_schalten); createDeviceShutter("Wohnzimmer Rollladen", null, wohnzimmer_rollladen_position_anfahren); createDeviceShutter("Schlafzimmer Rollladen", null, schlafzimmer_rollladen_position_anfahren); createDeviceShutter("Flur Rollladen", null, flur_og_rollladen_position_anfahren); } if (scheduleRepository.count() == 0) { final Schedule scheduleEgFlurLicht = createSchedule(true, "EG Flur Licht", eg_flur_licht_schalten); createTime(scheduleEgFlurLicht, true, 1, 0, 0, MIN30, true); createTime(scheduleEgFlurLicht, true, 2, 0, 0, MIN30, false); createTime(scheduleEgFlurLicht, true, 7, 30, 0, MIN30, true); createTime(scheduleEgFlurLicht, true, 8, 30, 0, MIN30, false); createTime(scheduleEgFlurLicht, true, 13, 30, 0, MIN30, true); createTime(scheduleEgFlurLicht, true, 14, 30, 0, MIN30, false); createTime(scheduleEgFlurLicht, true, 19, 0, 0, MIN30, true); createTime(scheduleEgFlurLicht, true, 20, 0, 0, MIN30, false); scheduleRepository.save(scheduleEgFlurLicht); final Schedule scheduleEgAmbiente = createSchedule(false, "Ambiente EG", ambiente_eg_schalten); createTime(scheduleEgAmbiente, true, 7, 15, 0, MIN30, true); createTime(scheduleEgAmbiente, true, 9, 30, 0, MIN30, false); createSunset(scheduleEgAmbiente, true, Zenith.OFFICIAL, MIN30, true); createSunset(scheduleEgAmbiente, true, Zenith.ASTRONOMICAL, MIN30, false); scheduleRepository.save(scheduleEgAmbiente); final Schedule scheduleOgAmbiente = createSchedule(false, "Ambiente OG", ambiente_og_schalten); createTime(scheduleOgAmbiente, true, 7, 15, 0, MIN30, true); createTime(scheduleOgAmbiente, true, 9, 30, 0, MIN30, false); createSunset(scheduleOgAmbiente, true, Zenith.OFFICIAL, MIN30, true); createSunset(scheduleOgAmbiente, true, Zenith.ASTRONOMICAL, MIN30, false); scheduleRepository.save(scheduleOgAmbiente); final Schedule scheduleWohnzimmerRollladen = createSchedule(true, "Rollläden Wohnzimmer", wohnzimmer_rollladen_position_anfahren); createSunrise(scheduleWohnzimmerRollladen, true, BETWEEN_OFFICIAL_AND_CIVIL, 0, 0); createSunset(scheduleWohnzimmerRollladen, true, BETWEEN_OFFICIAL_AND_CIVIL, 0, 100); scheduleRepository.save(scheduleWohnzimmerRollladen); final Schedule scheduleSchlafzimmerRollladen = createSchedule(true, "Rollläden Schlafzimmer", schlafzimmer_rollladen_position_anfahren); createTime(scheduleSchlafzimmerRollladen, true, 7, 0, 0, 0, 0); createSunset(scheduleSchlafzimmerRollladen, true, BETWEEN_OFFICIAL_AND_CIVIL, 0, 100); scheduleRepository.save(scheduleSchlafzimmerRollladen); final Schedule scheduleFlurRollladen = createSchedule(true, "Rollladen Flur", flur_og_rollladen_position_anfahren); createSunrise(scheduleFlurRollladen, true, BETWEEN_OFFICIAL_AND_CIVIL, 0, 0); createSunset(scheduleFlurRollladen, true, BETWEEN_OFFICIAL_AND_CIVIL, 0, 100); scheduleRepository.save(scheduleFlurRollladen); final Schedule scheduleBadLichtMitte = createSchedule(false, "Bad Licht Mitte", bad_licht_mitte_schalten); createTime(scheduleBadLichtMitte, true, 10, 30, 0, MIN30, true); createTime(scheduleBadLichtMitte, true, 11, 30, 0, MIN30, false); createTime(scheduleBadLichtMitte, true, 15, 30, 0, MIN30, true); createTime(scheduleBadLichtMitte, true, 16, 30, 0, MIN30, false); createTime(scheduleBadLichtMitte, true, 21, 0, 0, MIN30, true); createTime(scheduleBadLichtMitte, true, 22, 0, 0, MIN30, false); createTime(scheduleBadLichtMitte, true, 0, 0, 0, MIN30, true); createTime(scheduleBadLichtMitte, true, 1, 0, 0, MIN30, false); scheduleRepository.save(scheduleBadLichtMitte); } } private DeviceDto createDeviceSwitch(final String title, final Property getPercent, final Property setPercent) { return deviceWriteService.createDeviceSwitch(title, getPercent, setPercent); } private DeviceDto createDeviceShutter(final String title, final Property getPercent, final Property setPercent) { return deviceWriteService.createDeviceShutter(title, getPercent, setPercent); } private Schedule createSchedule(final boolean enabled, final String title, final PropertyDto propertyDto) { final Schedule schedule = new Schedule(); schedule.setEnabled(enabled); schedule.setTitle(title); schedule.setPropertyName(propertyDto.getName()); schedule.setPropertyType(propertyDto.getPropertyType()); return schedule; } private KnxGroup createKnxGroupIfNotExists(final String name, final int main, final int mid, final int sub, final String dpt, final boolean readable, final boolean multiGroup) { final GroupAddress address = new GroupAddress(main, mid, sub); return knxGroupRepository.findByAddressRaw(address.getRawAddress()).orElseGet(() -> knxGroupWriteService.create(name, address, dpt, readable, multiGroup)); } private ScheduleEntry createRelative(final Schedule schedule, final boolean enabled, final int inSeconds, final int fuzzySeconds, final Object value) { final ZonedDateTime now = ZonedDateTime.now().plusSeconds(inSeconds).withNano(0); return createTime(schedule, enabled, now.getHour(), now.getMinute(), now.getSecond(), fuzzySeconds, value); } private ScheduleEntry createTime(final Schedule schedule, final boolean enabled, final int hour, final int minute, final int second, final int fuzzySeconds, final Object value) { return newScheduleEntry(schedule, enabled, ScheduleEntryType.TIME, null, hour, minute, second, fuzzySeconds, value); } private ScheduleEntry createSunrise(final Schedule schedule, final boolean enabled, final Zenith zenith, final int fuzzySeconds, final Object value) { return newScheduleEntry(schedule, enabled, ScheduleEntryType.SUNRISE, zenith, 0, 0, 0, fuzzySeconds, value); } private ScheduleEntry createSunset(final Schedule schedule, final boolean enabled, final Zenith zenith, final int fuzzySeconds, final Object value) { return newScheduleEntry(schedule, enabled, ScheduleEntryType.SUNSET, zenith, 0, 0, 0, fuzzySeconds, value); } private ScheduleEntry newScheduleEntry(final Schedule schedule, final boolean enabled, final ScheduleEntryType type, final Zenith zenith, final int hour, final int minute, final int second, final int fuzzySeconds, final Object value) { final ScheduleEntry entry = new ScheduleEntry(); entry.setEnabled(enabled); entry.setType(type); if (zenith != null) { entry.setZenith(zenith.degrees().doubleValue()); } entry.setHour(hour); entry.setMinute(minute); entry.setSecond(second); entry.setFuzzySeconds(fuzzySeconds); if (value instanceof Boolean) { entry.setValue((boolean) value ? 1.0 : 0.0); } else if (value instanceof Double) { entry.setValue((Double) value); } else if (value instanceof Integer) { entry.setValue((Integer) value); } else { throw new RuntimeException(); } schedule.getEntries().add(entry); return entry; } }