From bbc4dd6e53277dc7d4e29cf08a8cb2e030cc5fbc Mon Sep 17 00:00:00 2001 From: tom Date: Mon, 29 Jul 2024 15:33:58 +0200 Subject: [PATCH] CHANGE: automatic topic generation in class --- src/app/_models/mqtt-binary.ts | 12 ++++- src/app/_models/mqtt-light.ts | 45 ++++++++++++++++-- src/app/_models/mqtt-sensor.ts | 15 +++++- src/app/_models/mqtt-switch.ts | 37 ++++++++++++++- src/app/_models/mqtt_base.ts | 83 ++++++++++++++++++++++++++-------- 5 files changed, 166 insertions(+), 26 deletions(-) diff --git a/src/app/_models/mqtt-binary.ts b/src/app/_models/mqtt-binary.ts index f378ffe..4707f0c 100644 --- a/src/app/_models/mqtt-binary.ts +++ b/src/app/_models/mqtt-binary.ts @@ -1,6 +1,16 @@ -import { DeviceClass, MQTTEntity } from './mqtt_base'; +import { DeviceClass, iMQTTEntityBase, MQTTEntity } from './mqtt_base'; export class MqttBinary extends MQTTEntity { + override ent_type: string = 'binary'; + override attrs: Set = new Set([ + 'name', + 'uniq_id', + 'stat_t', + 'pl_on', + 'pl_off', + 'dev_cla', + ]); + dev_cla: DeviceClass = new DeviceClass([ 'battery', 'battery_charging', diff --git a/src/app/_models/mqtt-light.ts b/src/app/_models/mqtt-light.ts index 0313407..27108f6 100644 --- a/src/app/_models/mqtt-light.ts +++ b/src/app/_models/mqtt-light.ts @@ -1,9 +1,48 @@ -import { MQTTEntity } from './mqtt_base'; +import { iMQTTEntityBase, MQTTEntity } from './mqtt_base'; export class MqttLight extends MQTTEntity { - cmd_t: string = 'command/topic'; - bri_cmd_t: string = 'brightness/command/topic'; pl_on: string = '1'; pl_off: string = '0'; val_tpl: string = ''; + bri_cmd_t: string = 'brightness/command/topic'; + _cmd_t: string = 'command/topic'; + + override readonly ent_type: string = 'light'; + override attrs: Set = new Set([ + 'name', + 'uniq_id', + 'stat_t', + 'pl_on', + 'pl_off', + 'val_tpl', + 'bri_cmd_t', + 'cmd_t', + ]); + + get cmd_t() { + return this._cmd_t; + } + set cmd_t(data: string) { + this._cmd_t = data; + } + + override set name(data: string) { + super.name = data; + this.topic_updates.next('cmd_t'); + this.topic_updates.next('bri_cmd_t'); + } + + override get name() { + return this._name; + } + + override set uniq_id(data: string) { + super.uniq_id = data; + this.topic_updates.next('cmd_t'); + this.topic_updates.next('bri_cmd_t'); + } + + override get uniq_id() { + return this._uniq_id; + } } diff --git a/src/app/_models/mqtt-sensor.ts b/src/app/_models/mqtt-sensor.ts index 369ac49..bdc5080 100644 --- a/src/app/_models/mqtt-sensor.ts +++ b/src/app/_models/mqtt-sensor.ts @@ -1,5 +1,16 @@ -import { MQTTEntity } from './mqtt_base'; +import { iMQTTEntityBase, MQTTEntity } from './mqtt_base'; -export class MqttSensor extends MQTTEntity { +export class MqttSensor extends MQTTEntity implements iMqttSensor { unit_of_meas: string = 'meassure'; + override ent_type: string = 'sensor'; + override attrs: Set = new Set([ + 'name', + 'uniq_id', + 'stat_t', + 'unit_of_meas', + ]); +} + +export interface iMqttSensor extends iMQTTEntityBase { + unit_of_meas: string; } diff --git a/src/app/_models/mqtt-switch.ts b/src/app/_models/mqtt-switch.ts index 57c681a..4915ae3 100644 --- a/src/app/_models/mqtt-switch.ts +++ b/src/app/_models/mqtt-switch.ts @@ -1,8 +1,43 @@ -import { DeviceClass, MQTTEntity } from './mqtt_base'; +import { EntityService } from '../_services/entity.service'; +import { DeviceClass, iMQTTEntityBase, MQTTEntity } from './mqtt_base'; export class MqttSwitch extends MQTTEntity { + override ent_type: string = 'switch'; + override attrs: Set = new Set([ + 'name', + 'uniq_id', + 'stat_t', + 'dev_cla', + 'cmd_t', + 'pl_on', + 'pl_off', + ]); dev_cla: DeviceClass = new DeviceClass(['switch', 'outlet']); cmd_t: string = 'command/topic'; pl_on: string = '1'; pl_off: string = '0'; + + override set name(name: string) { + super.name = name; + this.topic_updates.next('cmd_t'); + } + + override set uniq_id(uniq_id: string) { + super.uniq_id = uniq_id; + this.topic_updates.next('cmd_t'); + } + + override get name() { + return this._name; + } + override get uniq_id() { + return this._uniq_id; + } +} + +export interface iMqttSwitch extends iMQTTEntityBase { + cmd_t: string; + pl_on: string; + pl_off: string; + dev_cla: DeviceClass; } diff --git a/src/app/_models/mqtt_base.ts b/src/app/_models/mqtt_base.ts index 46992a5..9a03d00 100644 --- a/src/app/_models/mqtt_base.ts +++ b/src/app/_models/mqtt_base.ts @@ -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(); + 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; + readonly ent_type: string; + topic_updates: EventEmitter; + + toJSON: () => string; +}