Schedule notification after execution + Code Clean
This commit is contained in:
parent
7bd2e2f93d
commit
233aa2a821
@ -5,9 +5,9 @@ import {ScheduleEditorComponent} from "./pages/schedule/editor/schedule-editor.c
|
|||||||
import {DeviceListComponent} from "./pages/device/list/device-list.component";
|
import {DeviceListComponent} from "./pages/device/list/device-list.component";
|
||||||
import {DeviceEditorComponent} from "./pages/device/editor/device-editor.component";
|
import {DeviceEditorComponent} from "./pages/device/editor/device-editor.component";
|
||||||
import {PropertyListComponent} from "./pages/property/list/property-list.component";
|
import {PropertyListComponent} from "./pages/property/list/property-list.component";
|
||||||
import {ChannelListComponent} from "./pages/channel-list/channel-list.component";
|
import {ChannelListComponent} from "./pages/channel/list/channel-list.component";
|
||||||
import {BulkListComponent} from "./pages/bulk/bulk-list/bulk-list.component";
|
import {BulkListComponent} from "./pages/bulk/list/bulk-list.component";
|
||||||
import {BulkComponent} from "./pages/bulk/bulk/bulk.component";
|
import {BulkEditorComponent} from "./pages/bulk/editor/bulk-editor.component";
|
||||||
|
|
||||||
const routes: Routes = [
|
const routes: Routes = [
|
||||||
{path: 'Device', component: DeviceEditorComponent},
|
{path: 'Device', component: DeviceEditorComponent},
|
||||||
@ -22,7 +22,7 @@ const routes: Routes = [
|
|||||||
{path: 'Schedule', component: ScheduleEditorComponent},
|
{path: 'Schedule', component: ScheduleEditorComponent},
|
||||||
{path: 'ScheduleList', component: ScheduleListComponent},
|
{path: 'ScheduleList', component: ScheduleListComponent},
|
||||||
|
|
||||||
{path: 'Bulk', component: BulkComponent},
|
{path: 'Bulk', component: BulkEditorComponent},
|
||||||
{path: 'BulkList', component: BulkListComponent},
|
{path: 'BulkList', component: BulkListComponent},
|
||||||
|
|
||||||
{path: '**', redirectTo: '/ScheduleList'},
|
{path: '**', redirectTo: '/ScheduleList'},
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import {AppRoutingModule} from './app-routing.module';
|
|||||||
import {AppComponent} from './app.component';
|
import {AppComponent} from './app.component';
|
||||||
import {HttpClientModule} from "@angular/common/http";
|
import {HttpClientModule} from "@angular/common/http";
|
||||||
import {FormsModule} from "@angular/forms";
|
import {FormsModule} from "@angular/forms";
|
||||||
import {EditFieldComponent} from './shared/edit-field/edit-field.component';
|
import {TextComponent} from './shared/text/text.component';
|
||||||
import {ScheduleListComponent} from './pages/schedule/list/schedule-list.component';
|
import {ScheduleListComponent} from './pages/schedule/list/schedule-list.component';
|
||||||
import {FontAwesomeModule} from '@fortawesome/angular-fontawesome';
|
import {FontAwesomeModule} from '@fortawesome/angular-fontawesome';
|
||||||
import {NumberComponent} from './shared/number/number.component';
|
import {NumberComponent} from './shared/number/number.component';
|
||||||
@ -14,14 +14,14 @@ import {SearchComponent} from './shared/search/search.component';
|
|||||||
import {DeviceListComponent} from './pages/device/list/device-list.component';
|
import {DeviceListComponent} from './pages/device/list/device-list.component';
|
||||||
import {DeviceEditorComponent} from './pages/device/editor/device-editor.component';
|
import {DeviceEditorComponent} from './pages/device/editor/device-editor.component';
|
||||||
import {PropertyListComponent} from './pages/property/list/property-list.component';
|
import {PropertyListComponent} from './pages/property/list/property-list.component';
|
||||||
import {ChannelListComponent} from './pages/channel-list/channel-list.component';
|
import {ChannelListComponent} from './pages/channel/list/channel-list.component';
|
||||||
import {BulkListComponent} from './pages/bulk/bulk-list/bulk-list.component';
|
import {BulkListComponent} from './pages/bulk/list/bulk-list.component';
|
||||||
import {BulkComponent} from './pages/bulk/bulk/bulk.component';
|
import {BulkEditorComponent} from './pages/bulk/editor/bulk-editor.component';
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
declarations: [
|
declarations: [
|
||||||
AppComponent,
|
AppComponent,
|
||||||
EditFieldComponent,
|
TextComponent,
|
||||||
ScheduleEditorComponent,
|
ScheduleEditorComponent,
|
||||||
ScheduleListComponent,
|
ScheduleListComponent,
|
||||||
NumberComponent,
|
NumberComponent,
|
||||||
@ -32,7 +32,7 @@ import {BulkComponent} from './pages/bulk/bulk/bulk.component';
|
|||||||
ChannelListComponent,
|
ChannelListComponent,
|
||||||
DeviceListComponent,
|
DeviceListComponent,
|
||||||
BulkListComponent,
|
BulkListComponent,
|
||||||
BulkComponent,
|
BulkEditorComponent,
|
||||||
],
|
],
|
||||||
imports: [
|
imports: [
|
||||||
BrowserModule,
|
BrowserModule,
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
<ng-container *ngIf="bulk">
|
<ng-container *ngIf="bulk">
|
||||||
<h1>
|
<h1>
|
||||||
<app-edit-field [initial]="bulk.name" (valueChange)="set(bulk, 'name', $event)"></app-edit-field>
|
<app-text [initial]="bulk.name" (valueChange)="set(bulk, 'name', $event)"></app-text>
|
||||||
</h1>
|
</h1>
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
@ -7,11 +7,11 @@ import {BulkEntry} from "../../../api/bulk/BulkEntry";
|
|||||||
import {faTimesCircle} from "@fortawesome/free-regular-svg-icons";
|
import {faTimesCircle} from "@fortawesome/free-regular-svg-icons";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-bulk',
|
selector: 'app-bulk-editor',
|
||||||
templateUrl: './bulk.component.html',
|
templateUrl: './bulk-editor.component.html',
|
||||||
styleUrls: ['./bulk.component.less']
|
styleUrls: ['./bulk-editor.component.less']
|
||||||
})
|
})
|
||||||
export class BulkComponent implements OnInit {
|
export class BulkEditorComponent implements OnInit {
|
||||||
|
|
||||||
readonly faTimes = faTimesCircle;
|
readonly faTimes = faTimesCircle;
|
||||||
|
|
||||||
@ -1,7 +1,7 @@
|
|||||||
import {Component, OnInit} from '@angular/core';
|
import {Component, OnInit} from '@angular/core';
|
||||||
import {ChannelService} from "../../api/channel/channel.service";
|
import {ChannelService} from "../../../api/channel/channel.service";
|
||||||
import {Channel, KnxGroup, Logic} from "../../api/channel/Channel";
|
import {Channel, KnxGroup, Logic} from "../../../api/channel/Channel";
|
||||||
import {Update} from "../../api/Update";
|
import {Update} from "../../../api/Update";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-channel-list',
|
selector: 'app-channel-list',
|
||||||
@ -7,7 +7,7 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<th>Name</th>
|
<th>Name</th>
|
||||||
<td>
|
<td>
|
||||||
<app-edit-field [initial]="deviceSwitch.title" (valueChange)="set('title', $event)"></app-edit-field>
|
<app-text [initial]="deviceSwitch.title" (valueChange)="set('title', $event)"></app-text>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
@ -24,7 +24,7 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<th>Name</th>
|
<th>Name</th>
|
||||||
<td>
|
<td>
|
||||||
<app-edit-field [initial]="deviceStateScene.title" (valueChange)="set('title', $event)"></app-edit-field>
|
<app-text [initial]="deviceStateScene.title" (valueChange)="set('title', $event)"></app-text>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
@ -47,7 +47,7 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<th>Name</th>
|
<th>Name</th>
|
||||||
<td>
|
<td>
|
||||||
<app-edit-field [initial]="deviceShutter.title" (valueChange)="set('title', $event)"></app-edit-field>
|
<app-text [initial]="deviceShutter.title" (valueChange)="set('title', $event)"></app-text>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|||||||
@ -22,7 +22,7 @@
|
|||||||
<tr>
|
<tr>
|
||||||
|
|
||||||
<td>
|
<td>
|
||||||
<app-edit-field [initial]="property.title" (valueChange)="edit(property, 'title', $event)"></app-edit-field>
|
<app-text [initial]="property.title" (valueChange)="edit(property, 'title', $event)"></app-text>
|
||||||
</td>
|
</td>
|
||||||
|
|
||||||
<td>
|
<td>
|
||||||
|
|||||||
@ -11,7 +11,7 @@
|
|||||||
<tr class="header">
|
<tr class="header">
|
||||||
<ng-container *ngTemplateOutlet="boolean;context:{schedule: schedule, value: schedule.enabled, key:'enabled'}"></ng-container>
|
<ng-container *ngTemplateOutlet="boolean;context:{schedule: schedule, value: schedule.enabled, key:'enabled'}"></ng-container>
|
||||||
<td colspan="24">
|
<td colspan="24">
|
||||||
<app-edit-field [initial]="schedule.title" (valueChange)="set(null, 'title', $event)"></app-edit-field>
|
<app-text [initial]="schedule.title" (valueChange)="set(null, 'title', $event)"></app-text>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr [class.disabled]="!schedule.enabled">
|
<tr [class.disabled]="!schedule.enabled">
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
import {Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
|
import {Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-edit-field',
|
selector: 'app-text',
|
||||||
templateUrl: './edit-field.component.html',
|
templateUrl: './text.component.html',
|
||||||
styleUrls: ['./edit-field.component.less']
|
styleUrls: ['./text.component.less']
|
||||||
})
|
})
|
||||||
export class EditFieldComponent implements OnInit {
|
export class TextComponent implements OnInit {
|
||||||
|
|
||||||
@ViewChild('input')
|
@ViewChild('input')
|
||||||
input?: ElementRef;
|
input?: ElementRef;
|
||||||
@ -1,10 +1,7 @@
|
|||||||
package de.ph87.homeautomation.schedule;
|
package de.ph87.homeautomation.schedule;
|
||||||
|
|
||||||
import de.ph87.homeautomation.schedule.entry.ScheduleEntry;
|
import de.ph87.homeautomation.schedule.entry.ScheduleEntry;
|
||||||
import lombok.AccessLevel;
|
import lombok.*;
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
import lombok.ToString;
|
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
@ -14,6 +11,7 @@ import java.util.Set;
|
|||||||
@Setter
|
@Setter
|
||||||
@ToString
|
@ToString
|
||||||
@Entity
|
@Entity
|
||||||
|
@NoArgsConstructor
|
||||||
public class Schedule {
|
public class Schedule {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@ -30,4 +28,8 @@ public class Schedule {
|
|||||||
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER, mappedBy = "schedule")
|
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER, mappedBy = "schedule")
|
||||||
private Set<ScheduleEntry> entries = new HashSet<>();
|
private Set<ScheduleEntry> entries = new HashSet<>();
|
||||||
|
|
||||||
|
public Schedule(final String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -33,6 +33,8 @@ public class ScheduleCalculator {
|
|||||||
|
|
||||||
private final ApplicationEventPublisher applicationEventPublisher;
|
private final ApplicationEventPublisher applicationEventPublisher;
|
||||||
|
|
||||||
|
private final ScheduleMapper scheduleMapper;
|
||||||
|
|
||||||
@EventListener(ApplicationStartedEvent.class)
|
@EventListener(ApplicationStartedEvent.class)
|
||||||
public void calculateAllNext() {
|
public void calculateAllNext() {
|
||||||
final ZonedDateTime now = ZonedDateTime.now();
|
final ZonedDateTime now = ZonedDateTime.now();
|
||||||
@ -40,7 +42,7 @@ public class ScheduleCalculator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void calculateSchedule(final Schedule schedule, final ZonedDateTime now) {
|
public void calculateSchedule(final Schedule schedule, final ZonedDateTime now) {
|
||||||
schedule.getEntries().forEach(scheduleEntry -> calculateEntry(schedule, scheduleEntry, now));
|
schedule.getEntries().forEach(entry -> calculateEntry(schedule, entry, now));
|
||||||
final Optional<ScheduleEntry> nextEntry = schedule.getEntries().stream()
|
final Optional<ScheduleEntry> nextEntry = schedule.getEntries().stream()
|
||||||
.filter(entry -> entry.getNextFuzzyTimestamp() != null && entry.getNextFuzzyTimestamp().isAfter(now))
|
.filter(entry -> entry.getNextFuzzyTimestamp() != null && entry.getNextFuzzyTimestamp().isAfter(now))
|
||||||
.min(Comparator.comparing(ScheduleEntry::getNextFuzzyTimestamp));
|
.min(Comparator.comparing(ScheduleEntry::getNextFuzzyTimestamp));
|
||||||
@ -50,6 +52,7 @@ public class ScheduleCalculator {
|
|||||||
log.info("Next schedule for \"{}\": {}", schedule.getTitle(), nextEntry.get().getNextFuzzyTimestamp());
|
log.info("Next schedule for \"{}\": {}", schedule.getTitle(), nextEntry.get().getNextFuzzyTimestamp());
|
||||||
}
|
}
|
||||||
applicationEventPublisher.publishEvent(new ScheduleThreadWakeUpEvent());
|
applicationEventPublisher.publishEvent(new ScheduleThreadWakeUpEvent());
|
||||||
|
scheduleMapper.publish(schedule, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void calculateEntry(final Schedule schedule, final ScheduleEntry entry, final ZonedDateTime now) {
|
private void calculateEntry(final Schedule schedule, final ScheduleEntry entry, final ZonedDateTime now) {
|
||||||
|
|||||||
@ -15,40 +15,40 @@ import java.util.Comparator;
|
|||||||
@Service
|
@Service
|
||||||
@Transactional
|
@Transactional
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class ScheduleExecutionService {
|
public class ScheduleExecutor {
|
||||||
|
|
||||||
private final ScheduleReader scheduleReader;
|
private final ScheduleReader scheduleReader;
|
||||||
|
|
||||||
private final ScheduleCalculator scheduleCalculator;
|
private final PropertyWriteService propertyWriter;
|
||||||
|
|
||||||
private final PropertyWriteService propertyWriteService;
|
|
||||||
|
|
||||||
private final BulkExecutor bulkExecutor;
|
private final BulkExecutor bulkExecutor;
|
||||||
|
|
||||||
|
private final ScheduleWriter scheduleWriter;
|
||||||
|
|
||||||
public void executeAllLastDue() {
|
public void executeAllLastDue() {
|
||||||
final ZonedDateTime now = ZonedDateTime.now();
|
scheduleReader.findAll().forEach(this::executeLastDue);
|
||||||
scheduleReader.findAll().forEach(schedule -> executeLastDue(schedule, now));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void executeLastDue(final Schedule schedule, final ZonedDateTime now) {
|
private void executeLastDue(final Schedule schedule) {
|
||||||
|
final ZonedDateTime now = ZonedDateTime.now();
|
||||||
schedule.getEntries().stream()
|
schedule.getEntries().stream()
|
||||||
.filter(entry -> entry.getNextFuzzyTimestamp() != null && !entry.getNextFuzzyTimestamp().isAfter(now))
|
.filter(entry -> entry.getNextFuzzyTimestamp() != null && !entry.getNextFuzzyTimestamp().isAfter(now))
|
||||||
.max(Comparator.comparing(ScheduleEntry::getNextFuzzyTimestamp))
|
.max(Comparator.comparing(ScheduleEntry::getNextFuzzyTimestamp))
|
||||||
.ifPresent(entry -> executeEntry(schedule, entry, now));
|
.ifPresent(entry -> executeEntry(schedule, entry));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void executeEntry(final Schedule schedule, final ScheduleEntry entry, final ZonedDateTime now) {
|
private void executeEntry(final Schedule schedule, final ScheduleEntry entry) {
|
||||||
log.info("Executing Schedule \"{}\" Entry {}", schedule.getTitle(), entry);
|
log.info("Executing Schedule \"{}\" Entry {}", schedule.getTitle(), entry);
|
||||||
entry.setLastClearTimestamp(entry.getNextClearTimestamp());
|
|
||||||
scheduleCalculator.calculateSchedule(schedule, now);
|
|
||||||
if (entry.getProperty() != null) {
|
if (entry.getProperty() != null) {
|
||||||
log.debug("Schedule setting property: {} = {}", entry.getProperty().getTitle(), entry.getValue());
|
log.debug("Schedule setting property: {} = {}", entry.getProperty().getTitle(), entry.getValue());
|
||||||
propertyWriteService.writeToChannel(entry.getProperty(), entry.getValue());
|
propertyWriter.writeToChannel(entry.getProperty(), entry.getValue());
|
||||||
}
|
}
|
||||||
if (entry.getBulk() != null) {
|
if (entry.getBulk() != null) {
|
||||||
log.debug("Schedule executing Bulk: {}", entry.getBulk());
|
log.debug("Schedule executing Bulk: {}", entry.getBulk());
|
||||||
bulkExecutor.execute(entry.getBulk());
|
bulkExecutor.execute(entry.getBulk());
|
||||||
}
|
}
|
||||||
|
entry.setLastClearTimestamp(entry.getNextClearTimestamp());
|
||||||
|
scheduleWriter.notifyChanged(schedule);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -15,7 +15,7 @@ import java.time.ZonedDateTime;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class ScheduleThreadService extends AbstractThreadService {
|
public class ScheduleThreadService extends AbstractThreadService {
|
||||||
|
|
||||||
private final ScheduleExecutionService scheduleExecutionService;
|
private final ScheduleExecutor scheduleExecutor;
|
||||||
|
|
||||||
private final ScheduleEntryReader scheduleEntryReader;
|
private final ScheduleEntryReader scheduleEntryReader;
|
||||||
|
|
||||||
@ -25,13 +25,13 @@ public class ScheduleThreadService extends AbstractThreadService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doStart() throws Exception {
|
protected void doStart() {
|
||||||
// nothing
|
// nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected long doStep() throws InterruptedException {
|
protected long doStep() {
|
||||||
scheduleExecutionService.executeAllLastDue();
|
scheduleExecutor.executeAllLastDue();
|
||||||
return scheduleEntryReader.getNextTimestamp().map(nextTimestamp -> Duration.between(ZonedDateTime.now(), nextTimestamp).toMillis()).orElse(0L);
|
return scheduleEntryReader.getNextTimestamp().map(nextTimestamp -> Duration.between(ZonedDateTime.now(), nextTimestamp).toMillis()).orElse(0L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -25,9 +25,9 @@ public class ScheduleWriter {
|
|||||||
private final ScheduleRepository scheduleRepository;
|
private final ScheduleRepository scheduleRepository;
|
||||||
|
|
||||||
public ScheduleDto create() {
|
public ScheduleDto create() {
|
||||||
final Schedule entry = new Schedule();
|
final Schedule schedule = scheduleRepository.save(new Schedule(generateUnusedName()));
|
||||||
entry.setTitle(generateUnusedName());
|
notifyChanged(schedule);
|
||||||
return scheduleMapper.publish(scheduleRepository.save(entry), true);
|
return scheduleMapper.toDto(schedule);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String generateUnusedName() {
|
private String generateUnusedName() {
|
||||||
@ -42,14 +42,17 @@ public class ScheduleWriter {
|
|||||||
public <T> ScheduleDto set(final long id, final BiConsumer<Schedule, T> setter, final T value) {
|
public <T> ScheduleDto set(final long id, final BiConsumer<Schedule, T> setter, final T value) {
|
||||||
final Schedule schedule = scheduleReader.getById(id);
|
final Schedule schedule = scheduleReader.getById(id);
|
||||||
setter.accept(schedule, value);
|
setter.accept(schedule, value);
|
||||||
scheduleCalculator.calculateSchedule(schedule, ZonedDateTime.now());
|
notifyChanged(schedule);
|
||||||
return scheduleMapper.publish(schedule, true);
|
return scheduleMapper.toDto(schedule);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete(final long id) {
|
public void delete(final long id) {
|
||||||
final Schedule schedule = scheduleReader.getById(id);
|
final Schedule schedule = scheduleReader.getById(id);
|
||||||
scheduleRepository.delete(schedule);
|
scheduleRepository.delete(schedule);
|
||||||
scheduleMapper.publish(schedule, false);
|
}
|
||||||
|
|
||||||
|
public void notifyChanged(final Schedule schedule) {
|
||||||
|
scheduleCalculator.calculateSchedule(schedule, ZonedDateTime.now());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,17 +3,14 @@ package de.ph87.homeautomation.schedule.entry;
|
|||||||
import de.ph87.homeautomation.bulk.BulkReader;
|
import de.ph87.homeautomation.bulk.BulkReader;
|
||||||
import de.ph87.homeautomation.property.PropertyReader;
|
import de.ph87.homeautomation.property.PropertyReader;
|
||||||
import de.ph87.homeautomation.schedule.Schedule;
|
import de.ph87.homeautomation.schedule.Schedule;
|
||||||
import de.ph87.homeautomation.schedule.ScheduleCalculator;
|
|
||||||
import de.ph87.homeautomation.schedule.ScheduleMapper;
|
|
||||||
import de.ph87.homeautomation.schedule.ScheduleReader;
|
import de.ph87.homeautomation.schedule.ScheduleReader;
|
||||||
|
import de.ph87.homeautomation.schedule.ScheduleWriter;
|
||||||
import de.ph87.homeautomation.web.BadRequestException;
|
import de.ph87.homeautomation.web.BadRequestException;
|
||||||
import de.ph87.homeautomation.web.WebSocketService;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.time.ZonedDateTime;
|
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
@ -31,36 +28,33 @@ public class ScheduleEntryWriter {
|
|||||||
|
|
||||||
private final ScheduleReader scheduleReader;
|
private final ScheduleReader scheduleReader;
|
||||||
|
|
||||||
private final ScheduleCalculator scheduleCalculationService;
|
|
||||||
|
|
||||||
private final ScheduleEntryMapper scheduleEntryMapper;
|
private final ScheduleEntryMapper scheduleEntryMapper;
|
||||||
|
|
||||||
private final ScheduleEntryRepository scheduleEntryRepository;
|
private final ScheduleEntryRepository scheduleEntryRepository;
|
||||||
|
|
||||||
private final WebSocketService webSocketService;
|
|
||||||
|
|
||||||
private final ScheduleMapper scheduleMapper;
|
|
||||||
|
|
||||||
private final PropertyReader propertyReader;
|
private final PropertyReader propertyReader;
|
||||||
|
|
||||||
private final BulkReader bulkReader;
|
private final BulkReader bulkReader;
|
||||||
|
|
||||||
|
private final ScheduleWriter scheduleWriter;
|
||||||
|
|
||||||
public ScheduleEntryDto create(final long scheduleId) {
|
public ScheduleEntryDto create(final long scheduleId) {
|
||||||
final Schedule schedule = scheduleReader.getById(scheduleId);
|
final Schedule schedule = scheduleReader.getById(scheduleId);
|
||||||
final ScheduleEntry entry = scheduleEntryRepository.save(new ScheduleEntry(schedule));
|
final ScheduleEntry entry = scheduleEntryRepository.save(new ScheduleEntry(schedule));
|
||||||
schedule.getEntries().add(entry);
|
schedule.getEntries().add(entry);
|
||||||
return publish(entry, true, true);
|
scheduleWriter.notifyChanged(schedule);
|
||||||
|
return scheduleEntryMapper.toDto(entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> ScheduleEntryDto setValue(final long id, final BiConsumer<ScheduleEntry, T> setter, final T value) {
|
private <T> ScheduleEntryDto modifyValue(final long id, final BiConsumer<ScheduleEntry, T> setter, final T value) {
|
||||||
return modify(id, entry -> setter.accept(entry, value));
|
return modify(id, entry -> setter.accept(entry, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
private ScheduleEntryDto modify(final long id, final Consumer<ScheduleEntry> setter) {
|
private ScheduleEntryDto modify(final long id, final Consumer<ScheduleEntry> setter) {
|
||||||
final ScheduleEntry entry = scheduleEntryReader.getById(id);
|
final ScheduleEntry entry = scheduleEntryReader.getById(id);
|
||||||
setter.accept(entry);
|
setter.accept(entry);
|
||||||
scheduleCalculationService.calculateSchedule(entry.getSchedule(), ZonedDateTime.now());
|
scheduleWriter.notifyChanged(entry.getSchedule());
|
||||||
return publish(entry, true, true);
|
return scheduleEntryMapper.toDto(entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete(final long id) {
|
public void delete(final long id) {
|
||||||
@ -68,7 +62,7 @@ public class ScheduleEntryWriter {
|
|||||||
setPosition(entry, entry.getSchedule().getEntries().size() - 1);
|
setPosition(entry, entry.getSchedule().getEntries().size() - 1);
|
||||||
entry.getSchedule().getEntries().remove(entry);
|
entry.getSchedule().getEntries().remove(entry);
|
||||||
scheduleEntryRepository.delete(entry);
|
scheduleEntryRepository.delete(entry);
|
||||||
publish(entry, false, true);
|
scheduleWriter.notifyChanged(entry.getSchedule());
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScheduleEntryDto setPosition(final long id, final int newPosition) {
|
public ScheduleEntryDto setPosition(final long id, final int newPosition) {
|
||||||
@ -81,71 +75,71 @@ public class ScheduleEntryWriter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ScheduleEntryDto setEnabled(final long id, final boolean value) {
|
public ScheduleEntryDto setEnabled(final long id, final boolean value) {
|
||||||
return setValue(id, ScheduleEntry::setEnabled, value);
|
return modifyValue(id, ScheduleEntry::setEnabled, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScheduleEntryDto setMonday(final long id, final boolean value) {
|
public ScheduleEntryDto setMonday(final long id, final boolean value) {
|
||||||
return setValue(id, ScheduleEntry::setMonday, value);
|
return modifyValue(id, ScheduleEntry::setMonday, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScheduleEntryDto setTuesday(final long id, final boolean value) {
|
public ScheduleEntryDto setTuesday(final long id, final boolean value) {
|
||||||
return setValue(id, ScheduleEntry::setTuesday, value);
|
return modifyValue(id, ScheduleEntry::setTuesday, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScheduleEntryDto setWednesday(final long id, final boolean value) {
|
public ScheduleEntryDto setWednesday(final long id, final boolean value) {
|
||||||
return setValue(id, ScheduleEntry::setWednesday, value);
|
return modifyValue(id, ScheduleEntry::setWednesday, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScheduleEntryDto setThursday(final long id, final boolean value) {
|
public ScheduleEntryDto setThursday(final long id, final boolean value) {
|
||||||
return setValue(id, ScheduleEntry::setThursday, value);
|
return modifyValue(id, ScheduleEntry::setThursday, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScheduleEntryDto setFriday(final long id, final boolean value) {
|
public ScheduleEntryDto setFriday(final long id, final boolean value) {
|
||||||
return setValue(id, ScheduleEntry::setFriday, value);
|
return modifyValue(id, ScheduleEntry::setFriday, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScheduleEntryDto setSaturday(final long id, final boolean value) {
|
public ScheduleEntryDto setSaturday(final long id, final boolean value) {
|
||||||
return setValue(id, ScheduleEntry::setSaturday, value);
|
return modifyValue(id, ScheduleEntry::setSaturday, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScheduleEntryDto setSunday(final long id, final boolean value) {
|
public ScheduleEntryDto setSunday(final long id, final boolean value) {
|
||||||
return setValue(id, ScheduleEntry::setSunday, value);
|
return modifyValue(id, ScheduleEntry::setSunday, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScheduleEntryDto setType(final long id, final ScheduleEntryType value) {
|
public ScheduleEntryDto setType(final long id, final ScheduleEntryType value) {
|
||||||
return setValue(id, ScheduleEntry::setType, value);
|
return modifyValue(id, ScheduleEntry::setType, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScheduleEntryDto setZenith(final long id, final double value) {
|
public ScheduleEntryDto setZenith(final long id, final double value) {
|
||||||
return setValue(id, ScheduleEntry::setZenith, value);
|
return modifyValue(id, ScheduleEntry::setZenith, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScheduleEntryDto setHour(final long id, final int value) {
|
public ScheduleEntryDto setHour(final long id, final int value) {
|
||||||
return setValue(id, ScheduleEntry::setHour, value);
|
return modifyValue(id, ScheduleEntry::setHour, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScheduleEntryDto setMinute(final long id, final int value) {
|
public ScheduleEntryDto setMinute(final long id, final int value) {
|
||||||
return setValue(id, ScheduleEntry::setMinute, value);
|
return modifyValue(id, ScheduleEntry::setMinute, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScheduleEntryDto setSecond(final long id, final int value) {
|
public ScheduleEntryDto setSecond(final long id, final int value) {
|
||||||
return setValue(id, ScheduleEntry::setSecond, value);
|
return modifyValue(id, ScheduleEntry::setSecond, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScheduleEntryDto setFuzzySeconds(final long id, final int value) {
|
public ScheduleEntryDto setFuzzySeconds(final long id, final int value) {
|
||||||
return setValue(id, ScheduleEntry::setFuzzySeconds, value);
|
return modifyValue(id, ScheduleEntry::setFuzzySeconds, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScheduleEntryDto setValue(final long id, final double value) {
|
public ScheduleEntryDto setValue(final long id, final double value) {
|
||||||
return setValue(id, ScheduleEntry::setValue, value);
|
return modifyValue(id, ScheduleEntry::setValue, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScheduleEntryDto setProperty(final long id, final Long propertyId) {
|
public ScheduleEntryDto setProperty(final long id, final Long propertyId) {
|
||||||
return setValue(id, ScheduleEntry::setProperty, propertyId == null ? null : propertyReader.getById(propertyId));
|
return modifyValue(id, ScheduleEntry::setProperty, propertyId == null ? null : propertyReader.getById(propertyId));
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScheduleEntryDto setBulk(final long id, final Long bulkId) {
|
public ScheduleEntryDto setBulk(final long id, final Long bulkId) {
|
||||||
return setValue(id, ScheduleEntry::setBulk, bulkId == null ? null : bulkReader.getById(bulkId));
|
return modifyValue(id, ScheduleEntry::setBulk, bulkId == null ? null : bulkReader.getById(bulkId));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setPosition(final ScheduleEntry entry, final int newPosition) {
|
private void setPosition(final ScheduleEntry entry, final int newPosition) {
|
||||||
@ -158,19 +152,9 @@ public class ScheduleEntryWriter {
|
|||||||
for (final ScheduleEntry e : entry.getSchedule().getEntries()) {
|
for (final ScheduleEntry e : entry.getSchedule().getEntries()) {
|
||||||
if (e.getPosition() >= min && e.getPosition() <= max) {
|
if (e.getPosition() >= min && e.getPosition() <= max) {
|
||||||
e.setPosition(e.getPosition() + diff);
|
e.setPosition(e.getPosition() + diff);
|
||||||
publish(e, true, false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
entry.setPosition(newPosition);
|
entry.setPosition(newPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ScheduleEntryDto publish(final ScheduleEntry entry, final boolean existing, final boolean publishSchedule) {
|
|
||||||
final ScheduleEntryDto dto = scheduleEntryMapper.toDto(entry);
|
|
||||||
webSocketService.send(ScheduleEntryDto.class, dto, existing);
|
|
||||||
if (publishSchedule) {
|
|
||||||
scheduleMapper.publish(entry.getSchedule(), true);
|
|
||||||
}
|
|
||||||
return dto;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user