From 5ed32b8b82749344df7935d942f00eda7185efae Mon Sep 17 00:00:00 2001 From: tom Date: Fri, 28 Jun 2024 11:06:56 +0100 Subject: [PATCH] ADD: partial syncing for all topics --- src/app/entity/entity.component.html | 6 ++-- src/app/entity/entity.component.ts | 43 +++++++++++++++++++---- src/app/generator/generator.component.css | 1 + 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/app/entity/entity.component.html b/src/app/entity/entity.component.html index a421dc4..2382a68 100644 --- a/src/app/entity/entity.component.html +++ b/src/app/entity/entity.component.html @@ -25,11 +25,11 @@

Command Topic

- +

Brightness Command Topic

- +
@@ -60,7 +60,7 @@

State Topic

- +
diff --git a/src/app/entity/entity.component.ts b/src/app/entity/entity.component.ts index caa51d9..11f1ad9 100644 --- a/src/app/entity/entity.component.ts +++ b/src/app/entity/entity.component.ts @@ -23,11 +23,10 @@ export class EntityComponent { readonly entities = entity_types; readonly useRandomString = randomString; - auto_stat_t: boolean = true; showDiscovery: boolean = false; @Input() entity_type: number = 0; - @Input() state_topic: string = ''; + @Input() stat_t: string = ''; @Input() basemodel: MQTTEntity | null = null; @Input() entity_name: string = ''; @@ -48,12 +47,43 @@ export class EntityComponent { } } - update() { + update(model: keyof this = 'stat_t', event: Event | boolean = false): void { if (this.generatorService.auto_topic == 2) { - this.state_topic = this.updateTopic('stat'); + this.stat_t = this.updateTopic('stat'); this.entity_cmd_t = this.updateTopic('cmd'); this.entity_bri_cmd_t = this.updateTopic('bri_cmd'); + return; } + if (event instanceof Event) + var value = (event.target as HTMLInputElement).value; + else return; + if (this.generatorService.auto_topic == 1) { + this.sync_topcis(model, value); + } else { + if (!this.hasOwnProperty(model)) return; + if (typeof this[model] === 'string') { + this[model] = value as (typeof this)[keyof this]; + } + } + } + + sync_topcis(model: keyof this, value: string) { + let new_list = value.split('/'); + let old_list = String(this[model]).split('/'); + let missing = new_list.filter((item) => old_list.indexOf(item) < 0); + + if (old_list.filter((item) => new_list.indexOf(item) < 0).length > 1) + return (this.generatorService.auto_topic = 0); + if (missing.length == 0) return; //(this.generatorService.auto_topic = 2); + if (new_list.indexOf(missing[0]) == new_list.length - 1) { + this[model] = value as (typeof this)[keyof this]; + return; + } + let sync_topic = new_list.slice(0, -1).join('/'); + this.stat_t = sync_topic + '/stat'; + this.entity_cmd_t = sync_topic + '/cmd'; + this.entity_bri_cmd_t = sync_topic + '/bri_cmd'; + return; } updateTopic(topic: string) { @@ -68,7 +98,6 @@ export class EntityComponent { return topic_str; } - select_type(event: unknown) { let ent_type = entity_types[event as keyof typeof entity_types]; let ent_class = ent_type[1]; @@ -83,12 +112,12 @@ export class EntityComponent { this.update(); } - lockStateTopic(event: any) { + lock_auto_topic() { this.generatorService.auto_topic = 1; } create_entity() { - this.basemodel?.setProperty('stat_t', this.state_topic); + this.basemodel?.setProperty('stat_t', this.stat_t); this.basemodel?.setProperty('name', this.entity_name); this.basemodel?.setProperty('cmd_t', this.entity_cmd_t); diff --git a/src/app/generator/generator.component.css b/src/app/generator/generator.component.css index 4cddcfb..48c792f 100644 --- a/src/app/generator/generator.component.css +++ b/src/app/generator/generator.component.css @@ -20,6 +20,7 @@ text-align: center; background: #B3B3B3; transition: background .25s ease-in-out, color .25s ease-in-out; + user-select: none; } .customCheckboxContainer input:checked + label{