From de95a6aed1560efc1d7715f9c875565c430a4ac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Tue, 9 Nov 2021 13:07:12 +0100 Subject: [PATCH] removed Property.name + UI fixes + Search id fix + Code clean --- .../angular/src/app/api/property/Property.ts | 2 - .../app/pages/device/device.component.html | 16 +++--- .../property-list.component.html | 4 -- .../pages/schedule/schedule.component.html | 2 +- .../app/shared/search/search.component.html | 9 ---- .../src/app/shared/search/search.component.ts | 3 -- .../ph87/homeautomation/DemoDataService.java | 49 +++++++++---------- .../device/DeviceController.java | 16 +++--- .../property/IPropertyOwner.java | 9 ---- .../homeautomation/property/Property.java | 9 ++-- .../property/PropertyController.java | 5 -- .../homeautomation/property/PropertyDto.java | 3 -- .../PropertyOwnerValidatorService.java | 44 ----------------- .../property/PropertyReadService.java | 6 +-- .../property/PropertyRepository.java | 6 +-- .../property/PropertyWriteService.java | 18 +++---- .../schedule/ScheduleController.java | 6 +-- 17 files changed, 58 insertions(+), 149 deletions(-) delete mode 100644 src/main/java/de/ph87/homeautomation/property/IPropertyOwner.java delete mode 100644 src/main/java/de/ph87/homeautomation/property/PropertyOwnerValidatorService.java diff --git a/src/main/angular/src/app/api/property/Property.ts b/src/main/angular/src/app/api/property/Property.ts index e8483c4..2f97a5e 100644 --- a/src/main/angular/src/app/api/property/Property.ts +++ b/src/main/angular/src/app/api/property/Property.ts @@ -5,7 +5,6 @@ export class Property { constructor( public id: number, - public name: string, public type: string, public title: string, public value: number | null, @@ -26,7 +25,6 @@ export class Property { static fromJson(json: any): Property { return new Property( validateNumberNotNull(json['id']), - validateStringNotEmptyNotNull(json['name']), validateStringNotEmptyNotNull(json['type']), validateStringNotEmptyNotNull(json['title']), validateNumberAllowNull(json['value']), diff --git a/src/main/angular/src/app/pages/device/device.component.html b/src/main/angular/src/app/pages/device/device.component.html index 2f5263c..a95b73e 100644 --- a/src/main/angular/src/app/pages/device/device.component.html +++ b/src/main/angular/src/app/pages/device/device.component.html @@ -12,8 +12,8 @@ Eigenschaft - - + + @@ -29,14 +29,14 @@ Status-Eigenschaft - - + + Szene-Eigenschaft - - + + @@ -52,8 +52,8 @@ Eigenschaft - - + + diff --git a/src/main/angular/src/app/pages/property-list/property-list.component.html b/src/main/angular/src/app/pages/property-list/property-list.component.html index add2705..22b9a2a 100644 --- a/src/main/angular/src/app/pages/property-list/property-list.component.html +++ b/src/main/angular/src/app/pages/property-list/property-list.component.html @@ -5,7 +5,6 @@ - @@ -17,9 +16,6 @@ -
BezeichnungName Typ Wert Zeitstempel - - - + {{schedule.property?.type}} diff --git a/src/main/angular/src/app/shared/search/search.component.html b/src/main/angular/src/app/shared/search/search.component.html index 35eddda..42c82c7 100644 --- a/src/main/angular/src/app/shared/search/search.component.html +++ b/src/main/angular/src/app/shared/search/search.component.html @@ -3,9 +3,6 @@
{{selected.title}} - - [{{selected.id}}] - -
@@ -18,15 +15,9 @@
{{selected.title}} - - [{{selected.id}}] -
{{result.title}} - - [{{result.id}}] -
diff --git a/src/main/angular/src/app/shared/search/search.component.ts b/src/main/angular/src/app/shared/search/search.component.ts index 3fc7f0c..9c298e0 100644 --- a/src/main/angular/src/app/shared/search/search.component.ts +++ b/src/main/angular/src/app/shared/search/search.component.ts @@ -26,9 +26,6 @@ export class SearchComponent implements OnInit { @Input() initial?: number; - @Input() - showKey: boolean = false; - @Input() allowEmpty: boolean = true; diff --git a/src/main/java/de/ph87/homeautomation/DemoDataService.java b/src/main/java/de/ph87/homeautomation/DemoDataService.java index 9eeca93..1e4b071 100644 --- a/src/main/java/de/ph87/homeautomation/DemoDataService.java +++ b/src/main/java/de/ph87/homeautomation/DemoDataService.java @@ -49,42 +49,42 @@ public class DemoDataService { private final SceneWriteService sceneWriteService; public void insertDemoData() { - final Property eg = createProperty("eg.status", PropertyType.BOOLEAN, "Erdgeschoss", knx(0, 4, 2), null); - final Property eg_szene = createProperty("eg.szene", PropertyType.SCENE, "Erdgeschoss Szene", null, knx(0, 0, 1)); - final Property og = createProperty("og.status", PropertyType.BOOLEAN, "Obergeschoss", knx(0, 6, 6), null); - final Property og_szene = createProperty("og.szene", PropertyType.SCENE, "Obergeschoss Szene", null, knx(0, 3, 2)); + final Property erdgeschoss = createProperty("Erdgeschoss", PropertyType.BOOLEAN, knx(0, 4, 2), null); + final Property erdgeschoss_szene = createProperty("Erdgeschoss Szene", PropertyType.SCENE, null, knx(0, 0, 1)); + final Property obergeschoss = createProperty("Obergeschoss", PropertyType.BOOLEAN, knx(0, 6, 6), null); + final Property obergeschoss_szene = createProperty("Obergeschoss Szene", PropertyType.SCENE, null, knx(0, 3, 2)); - final Property fernseher = createProperty("fernseher.eg", PropertyType.BOOLEAN, "Fernseher", knx(0, 0, 20), knx(0, 0, 4)); - final Property verstaerker = createProperty("verstaerker", PropertyType.BOOLEAN, "Verstärker", knx(0, 3, 57), knx(0, 3, 56)); - final Property garten_steckdosen = createProperty("garten.steckdosen", PropertyType.BOOLEAN, "Garten Steckdosen", knx(0, 4, 12), knx(0, 4, 11)); - final Property terrasse = createProperty("terrasse", PropertyType.BOOLEAN, "Terrasse Licht", knx(0, 4, 1), knx(0, 4, 0)); + final Property fernseher = createProperty("Fernseher", PropertyType.BOOLEAN, knx(0, 0, 20), knx(0, 0, 4)); + final Property verstaerker = createProperty("Verstärker", PropertyType.BOOLEAN, knx(0, 3, 57), knx(0, 3, 56)); + final Property aussendekoration = createProperty("Außendekoration", PropertyType.BOOLEAN, knx(0, 4, 12), knx(0, 4, 11)); + final Property terrasse = createProperty("Terrasse Licht", PropertyType.BOOLEAN, knx(0, 4, 1), knx(0, 4, 0)); - final Property ambiente_eg = createProperty("ambiente.eg", PropertyType.BOOLEAN, "Ambiente EG", knx(0, 3, 81), knx(0, 3, 80)); - final Property ambiente_og = createProperty("ambiente.og", PropertyType.BOOLEAN, "Ambiente OG", knx(0, 6, 2), knx(0, 6, 3)); + final Property ambiente_eg = createProperty("Ambiente EG", PropertyType.BOOLEAN, knx(0, 3, 81), knx(0, 3, 80)); + final Property ambiente_og = createProperty("Ambiente OG", PropertyType.BOOLEAN, knx(0, 6, 2), knx(0, 6, 3)); - final Property bad_licht = createProperty("bad.licht", PropertyType.BOOLEAN, "Bad Licht", knx(0, 5, 19), knx(0, 3, 73)); - final Property flur_eg_licht = createProperty("flur.eg.licht", PropertyType.BOOLEAN, "Flur EG Licht", knx(0, 4, 8), knx(0, 5, 14)); + final Property bad_licht = createProperty("Bad Licht", PropertyType.BOOLEAN, knx(0, 5, 19), knx(0, 3, 73)); + final Property flur_eg_licht = createProperty("Flur EG Licht", PropertyType.BOOLEAN, 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 wohnzimmer_rollladen = createProperty("Wohnzimmer Rollladen", PropertyType.SHUTTER, null, knx(0, 4, 24)); + final Property schlafzimmer_rollladen = createProperty("Schlafzimmer Rollladen", PropertyType.SHUTTER, null, knx(0, 3, 3)); + final Property flur_og_rollladen = createProperty("Flur OG Rollladen", PropertyType.SHUTTER, 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)); + final Property helligkeit = createProperty("Helligkeit", PropertyType.LUX, knx(0, 5, 6), null); + final Property szene_haus = createProperty("Szene Haus ", PropertyType.SCENE, null, knx(0, 0, 21)); if (sceneRepository.count() == 0) { final SceneDto alles_aus = sceneWriteService.create(1, "Alles AUS"); final SceneDto nachtlicht = sceneWriteService.create(2, "Nachtlicht"); - final SceneDto dekoration_aus = sceneWriteService.create(30, "Dekoration AUS"); - final SceneDto dekoration_an = sceneWriteService.create(31, "Dekoration AN"); + final SceneDto aussendekoration_aus = sceneWriteService.create(30, "Außendekoration AUS"); + final SceneDto aussendekoration_an = sceneWriteService.create(31, "Außendekoration AN"); if (deviceRepository.count() == 0) { - deviceWriteService.createDeviceStateScene(eg, eg_szene, alles_aus); - deviceWriteService.createDeviceStateScene(og, og_szene, alles_aus); + deviceWriteService.createDeviceStateScene(erdgeschoss, erdgeschoss_szene, alles_aus); + deviceWriteService.createDeviceStateScene(obergeschoss, obergeschoss_szene, alles_aus); deviceWriteService.createDeviceSwitch(fernseher); deviceWriteService.createDeviceSwitch(verstaerker); - deviceWriteService.createDeviceSwitch(garten_steckdosen); + deviceWriteService.createDeviceSwitch(aussendekoration); deviceWriteService.createDeviceSwitch(terrasse); deviceWriteService.createDeviceSwitch(ambiente_eg); @@ -153,9 +153,8 @@ public class DemoDataService { 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); + private Property createProperty(final String title, final PropertyType type, final Channel readChannel, final Channel writeChannel) { + final Property property = propertyRepository.findByTitle(title).orElseGet(() -> propertyRepository.save(new Property(title, type))); property.setReadChannel(readChannel); property.setWriteChannel(writeChannel); return property; diff --git a/src/main/java/de/ph87/homeautomation/device/DeviceController.java b/src/main/java/de/ph87/homeautomation/device/DeviceController.java index 4d43243..5f20857 100644 --- a/src/main/java/de/ph87/homeautomation/device/DeviceController.java +++ b/src/main/java/de/ph87/homeautomation/device/DeviceController.java @@ -52,23 +52,23 @@ public class DeviceController { } @PostMapping("set/{id}/DeviceSwitch/stateProperty") - public DeviceDto setDeviceSwitchStateProperty(@PathVariable final long id, @RequestBody(required = false) final String name) { - return deviceWriteService.setDeviceSwitch(id, (device, v) -> device.setStateProperty(mapIfNotNull(v, propertyReadService::getByName)), name); + public DeviceDto setDeviceSwitchStateProperty(@PathVariable final long id, @RequestBody(required = false) final Long propertyId) { + return deviceWriteService.setDeviceSwitch(id, (device, v) -> device.setStateProperty(mapIfNotNull(v, propertyReadService::getById)), propertyId); } @PostMapping("set/{id}/DeviceStateScene/stateProperty") - public DeviceDto setDeviceStateSceneStateProperty(@PathVariable final long id, @RequestBody(required = false) final String name) { - return deviceWriteService.setDeviceStateScene(id, (device, v) -> device.setStateProperty(mapIfNotNull(v, propertyReadService::getByName)), name); + public DeviceDto setDeviceStateSceneStateProperty(@PathVariable final long id, @RequestBody(required = false) final Long propertyId) { + return deviceWriteService.setDeviceStateScene(id, (device, v) -> device.setStateProperty(mapIfNotNull(v, propertyReadService::getById)), propertyId); } @PostMapping("set/{id}/DeviceStateScene/sceneProperty") - public DeviceDto setDeviceStateSceneSceneProperty(@PathVariable final long id, @RequestBody(required = false) final String name) { - return deviceWriteService.setDeviceStateScene(id, (device, v) -> device.setSceneProperty(mapIfNotNull(v, propertyReadService::getByName)), name); + public DeviceDto setDeviceStateSceneSceneProperty(@PathVariable final long id, @RequestBody(required = false) final Long propertyId) { + return deviceWriteService.setDeviceStateScene(id, (device, v) -> device.setSceneProperty(mapIfNotNull(v, propertyReadService::getById)), propertyId); } @PostMapping("set/{id}/DeviceShutter/positionProperty") - public DeviceDto setDeviceShutterPositionProperty(@PathVariable final long id, @RequestBody(required = false) final String name) { - return deviceWriteService.setDeviceShutter(id, (device, v) -> device.setPositionProperty(mapIfNotNull(v, propertyReadService::getByName)), name); + public DeviceDto setDeviceShutterPositionProperty(@PathVariable final long id, @RequestBody(required = false) final Long propertyId) { + return deviceWriteService.setDeviceShutter(id, (device, v) -> device.setPositionProperty(mapIfNotNull(v, propertyReadService::getById)), propertyId); } } diff --git a/src/main/java/de/ph87/homeautomation/property/IPropertyOwner.java b/src/main/java/de/ph87/homeautomation/property/IPropertyOwner.java deleted file mode 100644 index 4640a6c..0000000 --- a/src/main/java/de/ph87/homeautomation/property/IPropertyOwner.java +++ /dev/null @@ -1,9 +0,0 @@ -package de.ph87.homeautomation.property; - -public interface IPropertyOwner { - - String getPropertyOwnerName(); - - Property write(final String name, final double value); - -} diff --git a/src/main/java/de/ph87/homeautomation/property/Property.java b/src/main/java/de/ph87/homeautomation/property/Property.java index d05c1d1..6ef5a07 100644 --- a/src/main/java/de/ph87/homeautomation/property/Property.java +++ b/src/main/java/de/ph87/homeautomation/property/Property.java @@ -22,10 +22,7 @@ public final class Property { @Enumerated(EnumType.STRING) private PropertyType type; - @Column(nullable = false) - private String name; - - @Column(unique = true) + @Column(nullable = false, unique = true) private String title; private ZonedDateTime timestamp; @@ -38,8 +35,8 @@ public final class Property { @ManyToOne private Channel writeChannel; - public Property(final String name, final PropertyType type) { - this.name = name; + public Property(final String title, final PropertyType type) { + this.title = title; this.type = type; } diff --git a/src/main/java/de/ph87/homeautomation/property/PropertyController.java b/src/main/java/de/ph87/homeautomation/property/PropertyController.java index 102c68b..9e0f0e3 100644 --- a/src/main/java/de/ph87/homeautomation/property/PropertyController.java +++ b/src/main/java/de/ph87/homeautomation/property/PropertyController.java @@ -27,11 +27,6 @@ public class PropertyController implements ISearchController { return propertyWriteService.set(id, Property::setType, PropertyType.valueOf(propertyType)); } - @PostMapping("set/{id}/name") - public PropertyDto setPropertyName(@PathVariable final long id, @RequestBody final String propertyName) { - return propertyWriteService.set(id, Property::setName, propertyName); - } - @PostMapping("set/{id}/title") public PropertyDto setPropertyTitle(@PathVariable final long id, @RequestBody final String propertyTitle) { return propertyWriteService.set(id, Property::setTitle, propertyTitle); diff --git a/src/main/java/de/ph87/homeautomation/property/PropertyDto.java b/src/main/java/de/ph87/homeautomation/property/PropertyDto.java index 76fce3f..3185401 100644 --- a/src/main/java/de/ph87/homeautomation/property/PropertyDto.java +++ b/src/main/java/de/ph87/homeautomation/property/PropertyDto.java @@ -13,8 +13,6 @@ public final class PropertyDto implements Serializable { private final PropertyType type; - private final String name; - private final String title; private final Double value; @@ -28,7 +26,6 @@ public final class PropertyDto implements Serializable { public PropertyDto(final Property property, final ChannelDto readChannel, final ChannelDto writeChannel) { this.id = property.getId(); this.type = property.getType(); - this.name = property.getName(); this.title = property.getTitle(); this.value = property.getValue(); this.timestamp = property.getTimestamp(); diff --git a/src/main/java/de/ph87/homeautomation/property/PropertyOwnerValidatorService.java b/src/main/java/de/ph87/homeautomation/property/PropertyOwnerValidatorService.java deleted file mode 100644 index 432e984..0000000 --- a/src/main/java/de/ph87/homeautomation/property/PropertyOwnerValidatorService.java +++ /dev/null @@ -1,44 +0,0 @@ -package de.ph87.homeautomation.property; - -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Service; - -import javax.annotation.PostConstruct; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; - -@Slf4j -@Service -@RequiredArgsConstructor -public class PropertyOwnerValidatorService { - - private final Set owners; - - @PostConstruct - public void postConstruct() { - final Map> overlapping = owners.stream().collect(Collectors.toMap(owner -> owner, this::getOverlappingList)); - if (!overlapping.values().stream().allMatch(List::isEmpty)) { - throw new RuntimeException( - String.format( - "Following IPropertyOwners have overlapping names:\n\t%s", - overlapping.entrySet().stream() - .map(entry -> { - final String prefixClassName = entry.getKey().getClass().getCanonicalName(); - final String prefixPropertyName = entry.getKey().getPropertyOwnerName(); - final String overlappingList = entry.getValue().stream().map(owner -> owner.getClass().getCanonicalName() + "(" + owner.getPropertyOwnerName() + ")").collect(Collectors.joining("\n\t\t")); - return String.format("%s(%s):\n\t\t%s", prefixClassName, prefixPropertyName, overlappingList); - }) - .collect(Collectors.joining("\n\t")) - ) - ); - } - } - - private List getOverlappingList(final IPropertyOwner a) { - return owners.stream().filter(b -> b.getPropertyOwnerName().startsWith(a.getPropertyOwnerName())).collect(Collectors.toList()); - } - -} diff --git a/src/main/java/de/ph87/homeautomation/property/PropertyReadService.java b/src/main/java/de/ph87/homeautomation/property/PropertyReadService.java index fab98b9..87223db 100644 --- a/src/main/java/de/ph87/homeautomation/property/PropertyReadService.java +++ b/src/main/java/de/ph87/homeautomation/property/PropertyReadService.java @@ -18,16 +18,12 @@ public class PropertyReadService { private final PropertyMapper propertyMapper; - public Property getByName(final String name) { - return propertyRepository.findByName(name).orElseThrow(RuntimeException::new); - } - public List findAllByReadChannel_Id(final long readChannelId) { return propertyRepository.findAllByReadChannel_Id(readChannelId); } public List findAllDtoLike(final String like) { - return propertyRepository.findAllByNameLike(like).stream().map(propertyMapper::toDto).collect(Collectors.toList()); + return propertyRepository.findAllByTitleLike(like).stream().map(propertyMapper::toDto).collect(Collectors.toList()); } public PropertyDto getDtoById(final long id) { diff --git a/src/main/java/de/ph87/homeautomation/property/PropertyRepository.java b/src/main/java/de/ph87/homeautomation/property/PropertyRepository.java index 9c45daa..16d39ad 100644 --- a/src/main/java/de/ph87/homeautomation/property/PropertyRepository.java +++ b/src/main/java/de/ph87/homeautomation/property/PropertyRepository.java @@ -7,15 +7,15 @@ import java.util.Optional; public interface PropertyRepository extends CrudRepository { - Optional findByName(String name); + Optional findByTitle(String title); List findAllByReadChannel_Id(long readChannelId); List findAll(); - List findAllByNameLike(final String like); + List findAllByTitleLike(final String like); - boolean existsByName(String name); + boolean existsByTitle(String title); List findAllByReadChannelNotNull(); diff --git a/src/main/java/de/ph87/homeautomation/property/PropertyWriteService.java b/src/main/java/de/ph87/homeautomation/property/PropertyWriteService.java index 14c9c11..b6210d8 100644 --- a/src/main/java/de/ph87/homeautomation/property/PropertyWriteService.java +++ b/src/main/java/de/ph87/homeautomation/property/PropertyWriteService.java @@ -18,7 +18,7 @@ import java.util.function.BiConsumer; @RequiredArgsConstructor public class PropertyWriteService { - private static final String NAME_PREFIX = "NEU "; + private static final String TITLE_PREFIX = "NEU "; private final PropertyReadService propertyReadService; @@ -30,10 +30,6 @@ public class PropertyWriteService { private final PropertyRepository propertyRepository; - public void write(final String name, final double value) { - write(propertyReadService.getByName(name), value); - } - public void write(final Property property, final double value) { channelService.write(property, value); } @@ -52,17 +48,17 @@ public class PropertyWriteService { public PropertyDto create() { final Property entry = new Property(); - entry.setTitle(generateUnusedName()); + entry.setTitle(generateUnusedTitle()); return publish(propertyRepository.save(entry), true); } - private String generateUnusedName() { + private String generateUnusedTitle() { int index = 0; - String name = null; - while (name == null || propertyRepository.existsByName(name)) { - name = PropertyWriteService.NAME_PREFIX + ++index; + String title = null; + while (title == null || propertyRepository.existsByTitle(title)) { + title = PropertyWriteService.TITLE_PREFIX + ++index; } - return name; + return title; } public PropertyDto set(final long id, final BiConsumer setter, final T value) { diff --git a/src/main/java/de/ph87/homeautomation/schedule/ScheduleController.java b/src/main/java/de/ph87/homeautomation/schedule/ScheduleController.java index 4e8940c..d99a896 100644 --- a/src/main/java/de/ph87/homeautomation/schedule/ScheduleController.java +++ b/src/main/java/de/ph87/homeautomation/schedule/ScheduleController.java @@ -49,9 +49,9 @@ public class ScheduleController { return scheduleWriteService.set(id, Schedule::setTitle, title); } - @PostMapping("set/{id}/propertyName") - public ScheduleDto setPropertyName(@PathVariable final long id, @RequestBody(required = false) final String propertyName) { - return scheduleWriteService.set(id, (schedule, v) -> schedule.setProperty(mapIfNotNull(v, propertyReadService::getByName)), propertyName); + @PostMapping("set/{id}/property") + public ScheduleDto setPropertyName(@PathVariable final long id, @RequestBody(required = false) final Long propertyId) { + return scheduleWriteService.set(id, (schedule, v) -> schedule.setProperty(mapIfNotNull(v, propertyReadService::getById)), propertyId); } }