package de.ph87.homeautomation; import com.luckycatlabs.sunrisesunset.Zenith; import de.ph87.homeautomation.knx.group.KnxGroupDto; import de.ph87.homeautomation.knx.group.KnxGroupRepository; import de.ph87.homeautomation.knx.group.KnxGroupWriteService; import de.ph87.homeautomation.property.PropertyType; 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 tuwien.auto.calimero.GroupAddress; import javax.annotation.PostConstruct; import java.time.ZonedDateTime; @SuppressWarnings({"unchecked", "UnusedReturnValue", "SameParameterValue", "UnusedAssignment", "RedundantSuppression"}) @Slf4j @Service @RequiredArgsConstructor public class DemoDataService { public static final int MIN30 = 30 * 60; private final KnxGroupWriteService knxGroupWriteService; private final ScheduleRepository scheduleRepository; private final KnxGroupRepository knxGroupRepository; @PostConstruct public void postConstruct() { final KnxGroupDto eg_flur_licht_schalten = createKnxGroupIfNotExists("EG Flur Licht Schalten", 1294, "1.001", PropertyType.ON_OFF, false, false); final KnxGroupDto eg_ambiente_schalten = createKnxGroupIfNotExists("EG Ambiente Schalten", 848, "1.001", PropertyType.ON_OFF, false, false); final KnxGroupDto og_ambiente_schalten = createKnxGroupIfNotExists("OG Ambiente Schalten", 1539, "1.001", PropertyType.ON_OFF, false, false); final KnxGroupDto wohnzimmer_rollladen_position_anfahren = createKnxGroupIfNotExists("Wohnzimmer Rollladen Position Anfahren", new GroupAddress(0, 4, 24), "5.001", PropertyType.PERCENT, false, false); final KnxGroupDto schlafzimmer_rollladen_position_anfahren = createKnxGroupIfNotExists("Schlafzimmer Rollladen Position Anfahren", new GroupAddress(0, 3, 3), "5.001", PropertyType.PERCENT, false, false); final KnxGroupDto flur_rollladen_position_anfahren = createKnxGroupIfNotExists("Flur Rollladen Position Anfahren", new GroupAddress(0, 5, 13), "5.001", PropertyType.PERCENT, false, false); final KnxGroupDto bad_licht_mitte_schalten = createKnxGroupIfNotExists("Bad Licht Mitte Schalten", 797, "1.001", PropertyType.ON_OFF, false, false); final KnxGroupDto helligkeit = createKnxGroupIfNotExists("Helligkeit", 1286, "9.004", PropertyType.LUX, false, true); if (scheduleRepository.count() == 0) { final Schedule scheduleEgFlurLicht = createSchedule("EG Flur Licht", eg_flur_licht_schalten); createTime(scheduleEgFlurLicht, 11, 30, 0, MIN30, true); createTime(scheduleEgFlurLicht, 12, 30, 0, MIN30, false); createTime(scheduleEgFlurLicht, 16, 30, 0, MIN30, true); createTime(scheduleEgFlurLicht, 17, 30, 0, MIN30, false); createTime(scheduleEgFlurLicht, 22, 0, 0, MIN30, true); createTime(scheduleEgFlurLicht, 23, 0, 0, MIN30, false); createTime(scheduleEgFlurLicht, 1, 0, 0, MIN30, true); createTime(scheduleEgFlurLicht, 2, 0, 0, MIN30, false); scheduleRepository.save(scheduleEgFlurLicht); final Schedule scheduleEgAmbiente = createSchedule("EG Ambiente", eg_ambiente_schalten); createTime(scheduleEgAmbiente, 7, 15, 0, MIN30, true); createTime(scheduleEgAmbiente, 9, 30, 0, MIN30, false); createSunset(scheduleEgAmbiente, Zenith.OFFICIAL, MIN30, true); createSunset(scheduleEgAmbiente, Zenith.ASTRONOMICAL, MIN30, false); scheduleRepository.save(scheduleEgAmbiente); final Schedule scheduleOgAmbiente = createSchedule("OG Ambiente", og_ambiente_schalten); createTime(scheduleOgAmbiente, 7, 15, 0, MIN30, true); createTime(scheduleOgAmbiente, 9, 30, 0, MIN30, false); createSunset(scheduleOgAmbiente, Zenith.OFFICIAL, MIN30, true); createSunset(scheduleOgAmbiente, Zenith.ASTRONOMICAL, MIN30, false); scheduleRepository.save(scheduleOgAmbiente); final Schedule scheduleWohnzimmerRollladen = createSchedule("Rollläden Wohnzimmer", wohnzimmer_rollladen_position_anfahren); createSunrise(scheduleWohnzimmerRollladen, Zenith.CIVIL, 0, 0); createSunset(scheduleWohnzimmerRollladen, Zenith.CIVIL, 0, 100); scheduleRepository.save(scheduleWohnzimmerRollladen); final Schedule scheduleSchlafzimmerRollladen = createSchedule("Rollläden Schlafzimmer", schlafzimmer_rollladen_position_anfahren); createTime(scheduleSchlafzimmerRollladen, 7, 0, 0, 0, 0); createSunset(scheduleSchlafzimmerRollladen, Zenith.CIVIL, 0, 100); scheduleRepository.save(scheduleSchlafzimmerRollladen); final Schedule scheduleFlurRollladen = createSchedule("Rollläden Flur", flur_rollladen_position_anfahren); createSunrise(scheduleFlurRollladen, Zenith.CIVIL, 0, 0); createSunset(scheduleFlurRollladen, Zenith.CIVIL, 0, 100); scheduleRepository.save(scheduleFlurRollladen); final Schedule scheduleBadLichtMitte = createSchedule("Bad Licht Mitte", bad_licht_mitte_schalten); createTime(scheduleBadLichtMitte, 10, 30, 0, MIN30, true); createTime(scheduleBadLichtMitte, 11, 30, 0, MIN30, false); createTime(scheduleBadLichtMitte, 15, 30, 0, MIN30, true); createTime(scheduleBadLichtMitte, 16, 30, 0, MIN30, false); createTime(scheduleBadLichtMitte, 21, 0, 0, MIN30, true); createTime(scheduleBadLichtMitte, 22, 0, 0, MIN30, false); createTime(scheduleBadLichtMitte, 0, 0, 0, MIN30, true); createTime(scheduleBadLichtMitte, 1, 0, 0, MIN30, false); scheduleRepository.save(scheduleBadLichtMitte); } } private Schedule createSchedule(final String s, final KnxGroupDto knxGroupDto) { final Schedule schedule = new Schedule(); schedule.setEnabled(true); schedule.setName(s); schedule.setPropertyName(knxGroupDto.propertyName); schedule.setPropertyType(knxGroupDto.getPropertyType()); return schedule; } private KnxGroupDto createKnxGroupIfNotExists(final String name, final int address, final String dpt, final PropertyType type, final boolean readable, final boolean multiGroup) { return createKnxGroupIfNotExists(name, new GroupAddress(address), dpt, type, readable, multiGroup); } private KnxGroupDto createKnxGroupIfNotExists(final String name, final GroupAddress address, final String dpt, final PropertyType type, final boolean readable, final boolean multiGroup) { return knxGroupRepository.findByAddressRaw(address.getRawAddress()).map(KnxGroupDto::new).orElseGet(() -> knxGroupWriteService.create(name, address, dpt, type, readable, multiGroup)); } private ScheduleEntry createRelative(final Schedule schedule, final int inSeconds, final int fuzzySeconds, final Object value) { final ZonedDateTime now = ZonedDateTime.now().plusSeconds(inSeconds).withNano(0); return newScheduleEntry(schedule, ScheduleEntryType.TIME, null, now.getHour(), now.getMinute(), now.getSecond(), fuzzySeconds, value); } private ScheduleEntry createTime(final Schedule schedule, final int hour, final int minute, final int second, final int fuzzySeconds, final Object value) { return newScheduleEntry(schedule, ScheduleEntryType.TIME, null, hour, minute, second, fuzzySeconds, value); } private ScheduleEntry createSunrise(final Schedule schedule, final Zenith zenith, final int fuzzySeconds, final Object value) { return newScheduleEntry(schedule, ScheduleEntryType.SUNRISE, zenith, 0, 0, 0, fuzzySeconds, value); } private ScheduleEntry createSunset(final Schedule schedule, final Zenith zenith, final int fuzzySeconds, final Object value) { return newScheduleEntry(schedule, ScheduleEntryType.SUNSET, zenith, 0, 0, 0, fuzzySeconds, value); } private ScheduleEntry newScheduleEntry(final Schedule schedule, 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(true); 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; } }