CHANGE: automatic topic generation in class

This commit is contained in:
2024-07-29 15:33:58 +02:00
parent 56365214b8
commit bbc4dd6e53
5 changed files with 166 additions and 26 deletions

View File

@@ -1,18 +1,49 @@
export class MQTTEntity {
name: string = '';
stat_t: string = '';
uniq_id: string = '';
// dev: MQTTDevice | null = null;
// entity_type: ENTITY_TYPE = 0;
import { EventEmitter, Injectable } from '@angular/core';
import { EntityService } from '../_services/entity.service';
setProperty(name: unknown, value: any): void {
if (!this.hasOwnProperty(String(name))) return;
this[name as keyof this] = value;
@Injectable()
export class MQTTEntity implements iMQTTEntityBase {
protected _name: string = '';
protected _stat_t: string = '';
protected _uniq_id: string = '';
attrs = new Set(['name', 'stat_t', 'uniq_id']);
topic_updates = new EventEmitter<string>();
readonly ent_type: string = 'base';
get name() {
return this._name;
}
getProperty(name: string): any {
if (!this.hasOwnProperty(name)) return;
return this[name as keyof this];
set name(name: string) {
this._name = name;
this._uniq_id = btoa(this._name).slice(-7).slice(0, 5);
this.topic_updates.next('stat_t');
}
get stat_t() {
return this._stat_t;
}
set stat_t(stat: string) {
this._stat_t = stat;
}
get uniq_id() {
return this._uniq_id;
}
set uniq_id(uniq_id: string) {
this._uniq_id = uniq_id;
this.topic_updates.next('stat_t');
}
get display_name() {
if (this._name != '' && this._uniq_id != '') {
return this._name + '_' + this._uniq_id;
} else {
return this._name;
}
}
toJSON(): any {
@@ -24,14 +55,15 @@ export class MQTTEntity {
}
return jsonObject;
}
getProperty(name: string): any {
if (!this.attrs.has(name)) return;
return this[name as keyof this];
}
// toJSON(): any {
// return {
// name: this.name,
// stat_t: this.stat_t,
// uniq_id: this.uniq_id,
// };
// }
setProperty(name: string, value: any) {
if (!this.attrs.has(name)) return;
this[name as keyof this] = value;
}
}
export class MQTTDevice {
@@ -53,3 +85,16 @@ export class DeviceClass {
return this.choices[this.value];
}
}
export interface iMQTTEntityBase {
// name: string;
// stat_t: string;
// uniq_id: string;
// display_name: string;
attrs: Set<string>;
readonly ent_type: string;
topic_updates: EventEmitter<string>;
toJSON: () => string;
}