CHANGE: autochange topics with custom ending in mind

This commit is contained in:
2024-07-31 13:06:38 +02:00
parent 06e2d77f08
commit 897120028c
4 changed files with 27 additions and 31 deletions

View File

@@ -4,7 +4,7 @@ import { EntityService } from '../_services/entity.service';
@Injectable() @Injectable()
export class MQTTEntity implements iMQTTEntityBase { export class MQTTEntity implements iMQTTEntityBase {
protected _name: string = ''; protected _name: string = '';
protected _stat_t: string = ''; protected _stat_t: string = 'state/topic';
protected _uniq_id: string = ''; protected _uniq_id: string = '';
attrs = new Set(['name', 'stat_t', 'uniq_id']); attrs = new Set(['name', 'stat_t', 'uniq_id']);

View File

@@ -16,7 +16,7 @@ export class GeneratorService {
@Input() device_name: string = ''; @Input() device_name: string = '';
@Input() device_id: string = ''; @Input() device_id: string = '';
@Input() device_standalone: boolean = false; @Input() device_standalone: boolean = false;
@Input() auto_topic: number = 2; @Input() auto_topic: boolean = true;
_upperTopic: string = ''; _upperTopic: string = '';
@@ -37,9 +37,9 @@ export class GeneratorService {
set selected_entity(entity: MQTTEntity | null) { set selected_entity(entity: MQTTEntity | null) {
this._selected_entity = entity; this._selected_entity = entity;
console.log('added');
entity?.topic_updates.subscribe((topic: string) => { entity?.topic_updates.subscribe((topic: string) => {
entity.setProperty(topic, this.updateTopic(entity, topic)); if (this.auto_topic)
entity.setProperty(topic, this.updateTopic(entity, topic));
}); });
} }
@@ -47,14 +47,18 @@ export class GeneratorService {
return this._selected_entity; return this._selected_entity;
} }
//TODO: add dynamic topic gen -> use already set topic if there are changes
updateTopic(entity: MQTTEntity, topic: string) { updateTopic(entity: MQTTEntity, topic: string) {
let topicStr: string = entity.getProperty(topic).split('/').pop();
let customTopic =
topicStr == 'topic' || topicStr == topic ? topic : topicStr;
return join( return join(
'/', '/',
this.upperTopic, this.upperTopic,
entity.ent_type, entity.ent_type,
this.device_name, this.device_name,
entity.display_name, entity.display_name,
topic customTopic
).toLowerCase(); ).toLowerCase();
} }

View File

@@ -12,17 +12,15 @@
<div class="property"> <div class="property">
<p>Automatische Topics</p> <p>Automatische Topics</p>
<div class="customCheckboxContainer"> <div class="customCheckboxContainer">
<input [ngModel]="auto_topic" (ngModelChange)="auto_topic=$event" id="auto_topic_0" type="radio" name="auto_topic" value="2"> <input [ngModel]="generatorService.auto_topic" (ngModelChange)="generatorService.auto_topic=$event" id="auto_topic_0" type="radio" name="auto_topic" [value]="true">
<label class="customCheckbox" for="auto_topic_0">Alles</label> <label class="customCheckbox" for="auto_topic_0">Alles</label>
<input [ngModel]="auto_topic" (ngModelChange)="auto_topic=$event" id="auto_topic_1" type="radio" name="auto_topic" value="1"> <input [ngModel]="generatorService.auto_topic" (ngModelChange)="generatorService.auto_topic=$event" id="auto_topic_2" type="radio" name="auto_topic" [value]="false">
<label class="customCheckbox" for="auto_topic_1">Änderungen übernehmen</label>
<input [ngModel]="auto_topic" (ngModelChange)="auto_topic=$event" id="auto_topic_2" type="radio" name="auto_topic" value="0">
<label class="customCheckbox" for="auto_topic_2">Aus</label> <label class="customCheckbox" for="auto_topic_2">Aus</label>
</div> </div>
</div> </div>
<div class="col-span-2 property"> <div class="col-span-2 property">
<h3>EntityTyp:</h3> <h3>EntityTyp:</h3>
<select [ngModel]="entity_type" (ngModelChange)="select_type($event)"> <select #typeinput autofocus [ngModel]="entity_type" (ngModelChange)="select_type($event)">
<option *ngFor="let key of useObject.keys(generatorService.entity_types)" value="{{key}}">{{generatorService.entity_types[key][0]}}</option> <option *ngFor="let key of useObject.keys(generatorService.entity_types)" value="{{key}}">{{generatorService.entity_types[key][0]}}</option>
</select> </select>
</div> </div>

View File

@@ -1,4 +1,4 @@
import { Component } from '@angular/core'; import { Component, Input, ViewChild } from '@angular/core';
import { EntityService } from '../_services/entity.service'; import { EntityService } from '../_services/entity.service';
import { GeneratorService, randomString } from '../_services/generator.service'; import { GeneratorService, randomString } from '../_services/generator.service';
import { MQTTEntity } from '../_models/mqtt_base'; import { MQTTEntity } from '../_models/mqtt_base';
@@ -17,33 +17,27 @@ export class GeneratorComponent {
readonly useObject = Object; readonly useObject = Object;
readonly useRandomString = randomString; readonly useRandomString = randomString;
set auto_topic(value: number) { @ViewChild('typeinput') typeinput: any;
this.generatorService.auto_topic = value; @Input() entity_type: number = 0;
console.log(this.generatorService.auto_topic);
}
get auto_topic(): string {
return String(this.generatorService.auto_topic);
}
get created_entities(): MQTTEntity[] { get created_entities(): MQTTEntity[] {
return Array.from(this.generatorService.created_enteties).reverse(); return Array.from(this.generatorService.created_enteties).reverse();
} }
entity_type: number = 0; select_type(entity_num: number) {
this.entity_type = entity_num;
select_type(etype: number) { let entity_type =
let ent_type =
this.generatorService.entity_types[ this.generatorService.entity_types[
etype as keyof typeof this.generatorService.entity_types entity_num as keyof typeof this.generatorService.entity_types
]; ];
let ent_class = ent_type[1]; let entity_class = entity_type[1];
if (typeof ent_class === 'function' && etype != 0) {
let entity_model = new ent_class(); if (typeof entity_class === 'function' && entity_num != 0) {
this.generatorService.selected_entity = entity_model; this.generatorService.selected_entity = new entity_class();
} else this.generatorService.selected_entity = null; } else {
this.entity_type = etype; this.generatorService.selected_entity = null;
console.log(this.generatorService.selected_entity); this.typeinput.nativeElement.focus();
}
} }
log(event: any) { log(event: any) {