From da31a892e1667fc240ecae69986821b79af86dcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Fri, 29 Oct 2021 11:24:58 +0200 Subject: [PATCH] REFACTOR: Replaced ScheduleEntry properties by single value + UI ScheduleEntry.value --- src/main/angular/src/app/api/Timestamp.ts | 35 ++++ .../angular/src/app/api/schedule/Schedule.ts | 6 +- .../app/api/schedule/entry/ScheduleEntry.ts | 51 ++---- src/main/angular/src/app/api/validators.ts | 33 ++-- src/main/angular/src/app/app.component.html | 2 +- .../pages/schedule/schedule.component.html | 12 +- .../pages/schedule/schedule.component.less | 4 + .../app/pages/schedule/schedule.component.ts | 1 - .../ph87/homeautomation/DemoDataService.java | 153 +++++++++--------- .../homeautomation/knx/group/KnxGroup.java | 11 +- .../homeautomation/knx/group/KnxGroupDto.java | 5 + .../knx/group/KnxGroupFormatException.java | 6 +- .../knx/group/KnxGroupSetService.java | 2 +- .../knx/group/KnxGroupWriteService.java | 18 +-- .../property/IPropertyOwner.java | 2 +- .../property/PropertyService.java | 2 +- .../property/PropertySetException.java | 4 +- .../homeautomation/property/PropertyType.java | 5 + .../homeautomation/schedule/Schedule.java | 7 + .../homeautomation/schedule/ScheduleDto.java | 15 +- .../schedule/ScheduleExecutionService.java | 14 +- .../schedule/entry/ScheduleEntry.java | 12 +- .../entry/ScheduleEntryController.java | 10 ++ .../schedule/entry/ScheduleEntryDto.java | 43 +++-- .../entry/ScheduleNextExecutionDto.java | 5 +- 25 files changed, 264 insertions(+), 194 deletions(-) create mode 100644 src/main/angular/src/app/api/Timestamp.ts create mode 100644 src/main/java/de/ph87/homeautomation/property/PropertyType.java diff --git a/src/main/angular/src/app/api/Timestamp.ts b/src/main/angular/src/app/api/Timestamp.ts new file mode 100644 index 0000000..8d736e5 --- /dev/null +++ b/src/main/angular/src/app/api/Timestamp.ts @@ -0,0 +1,35 @@ +import {prefix} from "../helpers"; + +export class Timestamp { + + public readonly WEEKDAY: string[] = ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"]; + + public readonly dayName; + + public readonly timeString; + + public constructor( + readonly date: Date, + ) { + const now = new Date(); + const minutes: string = prefix(this.date.getMinutes(), '0', 2); + if (date.getDate() === now.getDate()) { + this.dayName = "Heute"; + this.timeString = date.getHours() + ":" + minutes; + } else if (date.getDate() === now.getDate() + 1) { + this.dayName = "Morgen"; + this.timeString = date.getHours() + ":" + minutes; + } else { + this.dayName = this.WEEKDAY[date.getDay()]; + this.timeString = date.getHours() + ":" + minutes; + } + } + + public static fromDateOrNull(date: Date | null): Timestamp | null { + if (date === null) { + return null; + } + return new Timestamp(date); + } + +} diff --git a/src/main/angular/src/app/api/schedule/Schedule.ts b/src/main/angular/src/app/api/schedule/Schedule.ts index a1eb2f5..f873a65 100644 --- a/src/main/angular/src/app/api/schedule/Schedule.ts +++ b/src/main/angular/src/app/api/schedule/Schedule.ts @@ -7,6 +7,8 @@ export class Schedule { public id: number, public enabled: boolean, public name: string, + public propertyName: string, + public propertyType: string, public entries: ScheduleEntry[], ) { // nothing @@ -17,7 +19,9 @@ export class Schedule { validateNumberNotNull(json['id']), validateBooleanNotNull(json['enabled']), validateStringNotEmptyNotNull(json['name']), - validateListOrEmpty(json['entries'], ScheduleEntry.fromJson, ScheduleEntry.compareId), + validateStringNotEmptyNotNull(json['propertyName']), + validateStringNotEmptyNotNull(json['propertyType']), + validateListOrEmpty(json['entries'], ScheduleEntry.fromJson, ScheduleEntry.compare), ); } diff --git a/src/main/angular/src/app/api/schedule/entry/ScheduleEntry.ts b/src/main/angular/src/app/api/schedule/entry/ScheduleEntry.ts index 9818ff3..5b1252e 100644 --- a/src/main/angular/src/app/api/schedule/entry/ScheduleEntry.ts +++ b/src/main/angular/src/app/api/schedule/entry/ScheduleEntry.ts @@ -1,39 +1,5 @@ -import {validateBooleanNotNull, validateDateAllowNull, validateMap, validateNumberNotNull, validateStringNotEmptyNotNull} from "../../validators"; -import {prefix} from "../../../helpers"; - -class Timestamp { - - public readonly WEEKDAY: string[] = ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"]; - - public readonly dayName; - - public readonly timeString; - - public constructor( - readonly date: Date, - ) { - const now = new Date(); - const minutes: string = prefix(this.date.getMinutes(), '0', 2); - if (date.getDate() === now.getDate()) { - this.dayName = "Heute"; - this.timeString = date.getHours() + ":" + minutes; - } else if (date.getDate() === now.getDate() + 1) { - this.dayName = "Morgen"; - this.timeString = date.getHours() + ":" + minutes; - } else { - this.dayName = this.WEEKDAY[date.getDay()]; - this.timeString = date.getHours() + ":" + minutes; - } - } - - public static fromDateOrNull(date: Date | null): Timestamp | null { - if (date === null) { - return null; - } - return new Timestamp(date); - } - -} +import {validateBooleanNotNull, validateDateAllowNull, validateNumberNotNull, validateStringNotEmptyNotNull} from "../../validators"; +import {Timestamp} from "../../Timestamp"; export class ScheduleEntry { @@ -56,7 +22,7 @@ export class ScheduleEntry { public lastClearTimestamp: Timestamp | null, public nextClearTimestamp: Timestamp | null, public nextFuzzyTimestamp: Timestamp | null, - public properties: Map, + public value: number, ) { // nothing } @@ -81,7 +47,7 @@ export class ScheduleEntry { Timestamp.fromDateOrNull(validateDateAllowNull(json['lastClearTimestamp'])), Timestamp.fromDateOrNull(validateDateAllowNull(json['nextClearTimestamp'])), Timestamp.fromDateOrNull(validateDateAllowNull(json['nextFuzzyTimestamp'])), - validateMap(json['properties'], validateStringNotEmptyNotNull), + validateNumberNotNull(json['value']), ); } @@ -89,8 +55,13 @@ export class ScheduleEntry { return item.id; } - public static compareId(a: ScheduleEntry, b: ScheduleEntry): number { - return a.id - b.id; + public static compare(a: ScheduleEntry, b: ScheduleEntry): number { + if (a.nextFuzzyTimestamp === null) { + return +1; + } else if (b.nextFuzzyTimestamp === null) { + return -1; + } + return a.nextFuzzyTimestamp.date.getTime() - b.nextFuzzyTimestamp.date.getTime(); } } diff --git a/src/main/angular/src/app/api/validators.ts b/src/main/angular/src/app/api/validators.ts index 0d92f0b..c5f2189 100644 --- a/src/main/angular/src/app/api/validators.ts +++ b/src/main/angular/src/app/api/validators.ts @@ -5,6 +5,13 @@ export function validateBooleanNotNull(value: any): boolean { return value; } +export function validateBooleanAllowNull(value: any): boolean | null { + if (value === null || value === undefined) { + return null; + } + return validateBooleanNotNull(value); +} + export function validateNumberNotNull(value: any): number { const number: number = parseFloat(value); if (isNaN(number)) { @@ -13,6 +20,13 @@ export function validateNumberNotNull(value: any): number { return number; } +export function validateNumberAllowNull(value: any): number | null { + if (value === null || value === undefined) { + return null; + } + return validateNumberNotNull(value); +} + export function validateStringNotEmptyNotNull(value: any): string { if (!(typeof value === 'string')) { throw new Error("Not a string: " + value); @@ -23,25 +37,26 @@ export function validateStringNotEmptyNotNull(value: any): string { return value; } -export function validateStringNotEmpty_Nullable(value: any): string | null { +export function validateStringNotEmpty_AllowNull(value: any): string | null { if (value === null || value === undefined || value === '') { return null; } - if (!(typeof value === 'string')) { - throw new Error("Not a string: " + value); + return validateStringNotEmptyNotNull(value); +} + +function validateDateNotNull(value: any): Date { + const number: number = Date.parse(value); + if (isNaN(number)) { + throw new Error("Not a Date: " + value); } - return value; + return new Date(number); } export function validateDateAllowNull(value: any): Date | null { if (value === null || value === undefined || value === 0 || value === '') { return null; } - const number: number = Date.parse(value); - if (isNaN(number)) { - throw new Error("Not a Date: " + value); - } - return new Date(number); + return validateDateNotNull(value); } export function validateListOrEmpty(json: any, fromJson: (json: any) => T, compare: (a: T, b: T) => number): T[] { diff --git a/src/main/angular/src/app/app.component.html b/src/main/angular/src/app/app.component.html index a8b1f0d..d089a1c 100644 --- a/src/main/angular/src/app/app.component.html +++ b/src/main/angular/src/app/app.component.html @@ -1,7 +1,7 @@