Homeautomation/src/main/java/de/ph87/homeautomation/DemoDataService.java

188 lines
10 KiB
Java

package de.ph87.homeautomation;
import com.luckycatlabs.sunrisesunset.Zenith;
import de.ph87.homeautomation.channel.Channel;
import de.ph87.homeautomation.device.DeviceRepository;
import de.ph87.homeautomation.device.DeviceWriteService;
import de.ph87.homeautomation.knx.group.KnxGroup;
import de.ph87.homeautomation.knx.group.KnxGroupReadService;
import de.ph87.homeautomation.property.Property;
import de.ph87.homeautomation.property.PropertyRepository;
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 org.springframework.transaction.annotation.Transactional;
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 ScheduleRepository scheduleRepository;
private final DeviceWriteService deviceWriteService;
private final DeviceRepository deviceRepository;
private final PropertyRepository propertyRepository;
private final KnxGroupReadService knxGroupReadService;
public void insertDemoData() {
final Property ambiente_eg = createProperty("ambiente.eg", PropertyType.SWITCH, "Ambiente EG", knx(0, 3, 81), knx(0, 3, 80));
final Property ambiente_og = createProperty("ambiente.og", PropertyType.SWITCH, "Ambiente OG", knx(0, 6, 2), knx(0, 6, 3));
final Property bad_licht = createProperty("bad.licht", PropertyType.SWITCH, "Bad Licht", knx(0, 5, 19), knx(0, 3, 73));
final Property bad_licht_mitte = createProperty("bad.licht.mitte", PropertyType.SWITCH, "Bad Licht Mitte", knx(0, 3, 30), knx(0, 3, 29));
final Property flur_eg_licht = createProperty("flur.eg.licht", PropertyType.SWITCH, "Flur EG Licht", knx(0, 4, 8), knx(0, 5, 14));
final Property wohnzimmer_rollladen = createProperty("wohnzimmer.rollladen", PropertyType.SHUTTER, "Wohnzimmer Rollladen", null, knx(0, 4, 24));
final Property schlafzimmer_rollladen = createProperty("schlafzimmer_rollladen", PropertyType.SHUTTER, "Schlafzimmer Rollladen", null, knx(0, 3, 3));
final Property flur_og_rollladen = createProperty("flur_og_rollladen", PropertyType.SHUTTER, "Flur OG Rollladen", null, knx(0, 5, 13));
final Property helligkeit = createProperty("helligkeit", PropertyType.LUX, "Helligkeit", knx(0, 5, 6), null);
final Property szene_haus = createProperty("szene_haus", PropertyType.SCENE, "Szene Haus ", null, knx(0, 0, 21));
if (deviceRepository.count() == 0) {
deviceWriteService.createDeviceSwitch("Ambiente EG", ambiente_eg);
deviceWriteService.createDeviceSwitch("Ambiente OG", ambiente_og);
deviceWriteService.createDeviceSwitch("Bad Licht", bad_licht);
deviceWriteService.createDeviceShutter("Wohnzimmer Rollladen", wohnzimmer_rollladen);
deviceWriteService.createDeviceShutter("Schlafzimmer Rollladen", schlafzimmer_rollladen);
deviceWriteService.createDeviceShutter("Flur Rollladen", flur_og_rollladen);
}
if (scheduleRepository.count() == 0) {
final Schedule scheduleEgFlurLicht = createSchedule(true, "EG Flur Licht", flur_eg_licht);
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);
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);
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);
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);
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);
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);
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);
final Schedule scheduleSzeneHaus = createSchedule(true, "Dekoration", szene_haus);
createTime(scheduleSzeneHaus, true, 6, 0, 0, 0, 31);
createTime(scheduleSzeneHaus, true, 8, 30, 0, 0, 30);
createSunset(scheduleSzeneHaus, true, Zenith.OFFICIAL, 0, 31);
createTime(scheduleSzeneHaus, true, 22, 0, 0, 0, 30);
scheduleRepository.save(scheduleSzeneHaus);
}
}
private KnxGroup knx(final int main, final int mid, final int sub) {
return knxGroupReadService.getByAddress(main, mid, sub);
}
private Property createProperty(final String name, final PropertyType type, final String title, final Channel readChannel, final Channel writeChannel) {
final Property property = propertyRepository.findByName(name).orElseGet(() -> propertyRepository.save(new Property(name, type)));
property.setTitle(title);
property.setReadChannel(readChannel);
property.setWriteChannel(writeChannel);
return property;
}
private Schedule createSchedule(final boolean enabled, final String title, final Property property) {
final Schedule schedule = new Schedule();
schedule.setEnabled(enabled);
schedule.setTitle(title);
schedule.setProperty(property);
return schedule;
}
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;
}
}