Property.slug + writing to channel via slug

This commit is contained in:
Patrick Haßel 2023-04-04 11:13:24 +02:00
parent fd5ceced45
commit 6353dd6f9e
10 changed files with 75 additions and 9 deletions

View File

@ -1,18 +1,29 @@
import {validateListOrEmpty, validateNumberNotNull, validateStringNotEmptyNotNull} from "../validators"; import {validateListOrEmpty, validateNumberNotNull, validateStringEmptyToNull, validateStringNotEmptyNotNull} from "../validators";
import {Channel} from "../channel/Channel"; import {Channel} from "../channel/Channel";
import {SearchResult} from "../SearchResult"; import {SearchResult} from "../SearchResult";
import {environment} from "../../../environments/environment";
export class Property { export class Property {
readonly hrefId: string | null;
readonly hrefSlug: string | null;
constructor( constructor(
public id: number, public id: number,
public type: string, public type: string,
public title: string, public title: string,
public slug: string | null,
public readChannel: Channel | null, public readChannel: Channel | null,
public writeChannel: Channel | null, public writeChannel: Channel | null,
public usages: SearchResult[], public usages: SearchResult[],
) { ) {
// nothing if (writeChannel || true) {
this.hrefId = environment.restBase + '/property/write/id/' + id + '/'
if (slug) {
this.hrefSlug = environment.restBase + '/property/write/slug/' + slug + '/';
}
}
} }
static fromJsonAllowNull(json: any): Property | null { static fromJsonAllowNull(json: any): Property | null {
@ -27,6 +38,7 @@ export class Property {
validateNumberNotNull(json['id']), validateNumberNotNull(json['id']),
validateStringNotEmptyNotNull(json['type']), validateStringNotEmptyNotNull(json['type']),
validateStringNotEmptyNotNull(json['title']), validateStringNotEmptyNotNull(json['title']),
validateStringEmptyToNull(json['slug']),
Channel.fromJsonAllowNull(json['readChannel']), Channel.fromJsonAllowNull(json['readChannel']),
Channel.fromJsonAllowNull(json['writeChannel']), Channel.fromJsonAllowNull(json['writeChannel']),
validateListOrEmpty(json['usages'], SearchResult.fromJson), validateListOrEmpty(json['usages'], SearchResult.fromJson),

View File

@ -9,6 +9,7 @@
<table> <table>
<tr> <tr>
<th>Bezeichnung</th> <th>Bezeichnung</th>
<th>Links</th>
<th>Typ</th> <th>Typ</th>
<th>Wert</th> <th>Wert</th>
<th>Zeitstempel</th> <th>Zeitstempel</th>
@ -23,6 +24,20 @@
<td> <td>
<app-text [initial]="property.title" (valueChange)="edit(property, 'title', $event)"></app-text> <app-text [initial]="property.title" (valueChange)="edit(property, 'title', $event)"></app-text>
<div class="links" *ngIf="property.type === 'BOOLEAN'">
<div *ngIf="property.hrefId">{{property.hrefId}}0</div>
<div *ngIf="property.hrefId">{{property.hrefId}}1</div>
<div *ngIf="property.hrefSlug">{{property.hrefSlug}}0</div>
<div *ngIf="property.hrefSlug">{{property.hrefSlug}}1</div>
</div>
<div class="links" *ngIf="property.type === 'SHUTTER'">
<div *ngIf="property.hrefId">{{property.hrefId}}0</div>
<div *ngIf="property.hrefId">{{property.hrefId}}90</div>
<div *ngIf="property.hrefId">{{property.hrefId}}100</div>
<div *ngIf="property.hrefSlug">{{property.hrefSlug}}0</div>
<div *ngIf="property.hrefSlug">{{property.hrefSlug}}90</div>
<div *ngIf="property.hrefSlug">{{property.hrefSlug}}100</div>
</div>
</td> </td>
<td> <td>

View File

@ -1,3 +1,8 @@
table { table {
width: 100%; width: 100%;
} }
.links {
color: gray;
font-size: 80%;
}

View File

@ -5,6 +5,7 @@ import {Scene} from "../../../api/scene/Scene";
import {SceneService} from "../../../api/scene/scene.service"; import {SceneService} from "../../../api/scene/scene.service";
import {ChannelService} from "../../../api/channel/channel.service"; import {ChannelService} from "../../../api/channel/channel.service";
import {faTimesCircle} from "@fortawesome/free-regular-svg-icons"; import {faTimesCircle} from "@fortawesome/free-regular-svg-icons";
import {environment} from 'src/environments/environment';
@Component({ @Component({
selector: 'app-property-list', selector: 'app-property-list',
@ -21,6 +22,8 @@ export class PropertyListComponent implements OnInit {
scenes: Scene[] = []; scenes: Scene[] = [];
readonly environment = environment;
constructor( constructor(
readonly propertyService: PropertyService, readonly propertyService: PropertyService,
readonly sceneService: SceneService, readonly sceneService: SceneService,

View File

@ -43,11 +43,11 @@ public class DemoDataService {
if (!config.isInsertDemoData()) { if (!config.isInsertDemoData()) {
return; return;
} }
final Property propertyDirect = createProperty("propertyDirect", PropertyType.BOOLEAN, null, null); final Property propertyDirect = createProperty("propertyDirect", "direct", PropertyType.BOOLEAN, null, null);
final Property propertyBulkBoolean = createProperty("propertyBulkBoolean", PropertyType.BOOLEAN, null, null); final Property propertyBulkBoolean = createProperty("propertyBulkBoolean", null, PropertyType.BOOLEAN, null, null);
final Property propertyBulkShutter = createProperty("propertyBulkShutter", PropertyType.SHUTTER, null, null); final Property propertyBulkShutter = createProperty("propertyBulkShutter", null, PropertyType.SHUTTER, null, null);
final Property propertyBulkBrightness = createProperty("propertyBulkBrightness", PropertyType.BRIGHTNESS_PERCENT, null, null); final Property propertyBulkBrightness = createProperty("propertyBulkBrightness", null, PropertyType.BRIGHTNESS_PERCENT, null, null);
final Property propertyBulkColorTemperature = createProperty("propertyBulkColorTemperature", PropertyType.COLOR_TEMPERATURE, null, null); final Property propertyBulkColorTemperature = createProperty("propertyBulkColorTemperature", null, PropertyType.COLOR_TEMPERATURE, null, null);
final BulkDto bulk = bulkController.create(new BulkCreateDto("bulk", true)); 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(), propertyBulkBoolean.getId(), 1, 0));
bulkEntryController.create(new BulkEntryCreateDto(bulk.getId(), propertyBulkShutter.getId(), 35, 0)); bulkEntryController.create(new BulkEntryCreateDto(bulk.getId(), propertyBulkShutter.getId(), 35, 0));
@ -58,8 +58,9 @@ public class DemoDataService {
createTime(scheduleId, true, now.getHour(), now.getMinute(), now.getSecond(), 0, propertyDirect, 1, bulk); createTime(scheduleId, true, now.getHour(), now.getMinute(), now.getSecond(), 0, propertyDirect, 1, bulk);
} }
private Property createProperty(final String title, final PropertyType type, final Channel readChannel, final Channel writeChannel) { 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))); 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.setReadChannel(readChannel);
property.setWriteChannel(writeChannel); property.setWriteChannel(writeChannel);
return property; return property;

View File

@ -24,6 +24,9 @@ public final class Property {
@Column(nullable = false, unique = true) @Column(nullable = false, unique = true)
private String title; private String title;
@Column(unique = true)
private String slug;
@ManyToOne @ManyToOne
private Channel readChannel; private Channel readChannel;

View File

@ -39,6 +39,11 @@ public class PropertyController implements ISearchController {
return propertyWriter.set(id, Property::setTitle, propertyTitle); return propertyWriter.set(id, Property::setTitle, propertyTitle);
} }
@PostMapping("set/{id}/slug")
public PropertyDto setPropertySlug(@PathVariable final long id, @RequestBody final String propertySlug) {
return propertyWriter.set(id, Property::setSlug, propertySlug);
}
@PostMapping("set/{id}/value") @PostMapping("set/{id}/value")
public PropertyDto setValue(@PathVariable final long id, @RequestBody final double value) { public PropertyDto setValue(@PathVariable final long id, @RequestBody final double value) {
return propertyWriter.set(id, propertyWriter::writeToChannel, value); return propertyWriter.set(id, propertyWriter::writeToChannel, value);
@ -89,4 +94,16 @@ public class PropertyController implements ISearchController {
return new SearchResult(Property.class, propertyDto.getId(), propertyDto.getTitle()); return new SearchResult(Property.class, propertyDto.getId(), propertyDto.getTitle());
} }
@PostMapping("write/id/{id}/{value}")
public void write(@PathVariable final long id, @PathVariable final double value) {
final Property property = propertyReader.getById(id);
propertyWriter.writeToChannel(property, value);
}
@PostMapping("write/slug/{slug}/{value}")
public void write(@PathVariable final String slug, @PathVariable final double value) {
final Property property = propertyReader.getBySlug(slug);
propertyWriter.writeToChannel(property, value);
}
} }

View File

@ -16,6 +16,8 @@ public final class PropertyDto implements Serializable {
private final String title; private final String title;
private final String slug;
private final ChannelDto readChannel; private final ChannelDto readChannel;
private final ChannelDto writeChannel; private final ChannelDto writeChannel;
@ -26,6 +28,7 @@ public final class PropertyDto implements Serializable {
this.id = property.getId(); this.id = property.getId();
this.type = property.getType(); this.type = property.getType();
this.title = property.getTitle(); this.title = property.getTitle();
this.slug = property.getSlug();
this.readChannel = readChannel; this.readChannel = readChannel;
this.writeChannel = writeChannel; this.writeChannel = writeChannel;
this.usages = usages; this.usages = usages;

View File

@ -1,5 +1,6 @@
package de.ph87.homeautomation.property; package de.ph87.homeautomation.property;
import de.ph87.homeautomation.web.NotFoundException;
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;
@ -30,7 +31,11 @@ public class PropertyReader {
} }
public Property getById(final long id) { public Property getById(final long id) {
return propertyRepository.findById(id).orElseThrow(RuntimeException::new); return propertyRepository.findById(id).orElseThrow(() -> new NotFoundException("Property.id=%d", id));
}
public Property getBySlug(final String slug) {
return propertyRepository.findBySlug(slug).orElseThrow(() -> new NotFoundException("Property.slug=%s", slug));
} }
public List<PropertyDto> findAllDto() { public List<PropertyDto> findAllDto() {

View File

@ -17,4 +17,6 @@ public interface PropertyRepository extends JpaRepository<Property, Long> {
List<Property> findAllByReadChannelNotNull(); List<Property> findAllByReadChannelNotNull();
Optional<Property> findBySlug(String slug);
} }