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

97 lines
4.9 KiB
Java

package de.ph87.homeautomation;
import com.luckycatlabs.sunrisesunset.Zenith;
import de.ph87.homeautomation.bulk.BulkController;
import de.ph87.homeautomation.bulk.BulkCreateDto;
import de.ph87.homeautomation.bulk.BulkDto;
import de.ph87.homeautomation.bulk.entry.BulkEntryController;
import de.ph87.homeautomation.bulk.entry.BulkEntryCreateDto;
import de.ph87.homeautomation.channel.Channel;
import de.ph87.homeautomation.property.Property;
import de.ph87.homeautomation.property.PropertyRepository;
import de.ph87.homeautomation.property.PropertyType;
import de.ph87.homeautomation.schedule.ScheduleController;
import de.ph87.homeautomation.schedule.entry.ScheduleEntryController;
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 final Config config;
private final PropertyRepository propertyRepository;
private final BulkController bulkController;
private final BulkEntryController bulkEntryController;
private final ScheduleController scheduleController;
private final ScheduleEntryController scheduleEntryController;
public void insertDemoData() {
if (!config.isInsertDemoData()) {
return;
}
final Property propertyDirect = createProperty("propertyDirect", "direct", PropertyType.BOOLEAN, null, null);
final Property propertyBulkBoolean = createProperty("propertyBulkBoolean", null, PropertyType.BOOLEAN, null, null);
final Property propertyBulkShutter = createProperty("propertyBulkShutter", null, PropertyType.SHUTTER, null, null);
final Property propertyBulkBrightness = createProperty("propertyBulkBrightness", null, PropertyType.BRIGHTNESS_PERCENT, null, null);
final Property propertyBulkColorTemperature = createProperty("propertyBulkColorTemperature", null, PropertyType.COLOR_TEMPERATURE, null, null);
final BulkDto bulk = bulkController.create(new BulkCreateDto("bulk", true));
bulkEntryController.create(new BulkEntryCreateDto(bulk.getId(), propertyBulkBoolean.getId(), 1, 0));
bulkEntryController.create(new BulkEntryCreateDto(bulk.getId(), propertyBulkShutter.getId(), 35, 0));
bulkEntryController.create(new BulkEntryCreateDto(bulk.getId(), propertyBulkBrightness.getId(), 40, 0));
bulkEntryController.create(new BulkEntryCreateDto(bulk.getId(), propertyBulkColorTemperature.getId(), 55, 0));
final long scheduleId = createSchedule(true, "schedule");
final ZonedDateTime now = ZonedDateTime.now().plusSeconds(3);
createTime(scheduleId, true, now.getHour(), now.getMinute(), now.getSecond(), 0, propertyDirect, 1, bulk);
}
private Property createProperty(final String title, final String slug, final PropertyType type, final Channel readChannel, final Channel writeChannel) {
final Property property = propertyRepository.findByTitle(title).orElseGet(() -> propertyRepository.save(new Property(title, type)));
property.setSlug(slug == null || slug.isEmpty() ? null : slug);
property.setReadChannel(readChannel);
property.setWriteChannel(writeChannel);
return property;
}
private long createSchedule(final boolean enabled, final String title) {
final long id = scheduleController.create().getId();
scheduleController.setEnabled(id, enabled);
scheduleController.setTitle(id, title);
return id;
}
private void createTime(final long scheduleId, final boolean enabled, final int hour, final int minute, final int second, final int fuzzySeconds, final Property property, final double value, final BulkDto bulk) {
newScheduleEntry(scheduleId, enabled, ScheduleEntryType.TIME, null, hour, minute, second, fuzzySeconds, property, value, bulk);
}
private void newScheduleEntry(final long scheduleId, final boolean enabled, final ScheduleEntryType type, final Zenith zenith, final int hour, final int minute, final int second, final int fuzzySeconds, final Property property, final double value, final BulkDto bulk) {
final long id = scheduleEntryController.create(scheduleId).getId();
scheduleEntryController.setEnabled(id, enabled);
scheduleEntryController.setType(id, type.name());
if (zenith != null) {
scheduleEntryController.setZenith(id, zenith.degrees() + "");
}
scheduleEntryController.setHour(id, hour);
scheduleEntryController.setMinute(id, minute);
scheduleEntryController.setSecond(id, second);
scheduleEntryController.setFuzzySeconds(id, fuzzySeconds);
scheduleEntryController.setProperty(id, property == null ? null : property.getId());
scheduleEntryController.setValue(id, value);
scheduleEntryController.setBulk(id, bulk == null ? null : bulk.getId());
}
}