From 30ba44f1146f210850fc80a07b8ea835778fdfdc Mon Sep 17 00:00:00 2001 From: tom Date: Thu, 27 Jun 2024 21:08:16 +0100 Subject: [PATCH] ADD: new deviceclass and toJSON Method --- src/app/_models/mqtt_base.ts | 78 +++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/src/app/_models/mqtt_base.ts b/src/app/_models/mqtt_base.ts index 72c8024..46992a5 100644 --- a/src/app/_models/mqtt_base.ts +++ b/src/app/_models/mqtt_base.ts @@ -1,43 +1,55 @@ export class MQTTEntity { - name: string = ""; - stat_t: string = "state/topic"; - uniq_id: string = "unique_id"; - dev: MQTTDevice | null = null; - // entity_type: ENTITY_TYPE = 0; + name: string = ''; + stat_t: string = ''; + uniq_id: string = ''; + // dev: MQTTDevice | null = null; + // entity_type: ENTITY_TYPE = 0; - setProperty(name: unknown, value: any): void { - if(!this.hasOwnProperty(String(name))) return - this[name as keyof this] = value; - } + setProperty(name: unknown, value: any): void { + if (!this.hasOwnProperty(String(name))) return; + this[name as keyof this] = value; + } - createString() : string{ - let string: string = ""; - for(let property of Object.getOwnPropertyNames(this)){ - if(this[property as keyof this] == null) continue; - string += `"${property}": "${this[property as keyof this]}",` - } - console.log(string); - return string; + getProperty(name: string): any { + if (!this.hasOwnProperty(name)) return; + return this[name as keyof this]; + } + + toJSON(): any { + let jsonObject: { [key: string]: string } = {}; + for (let prop_name of Object.getOwnPropertyNames(this)) { + let property = this[prop_name as keyof this]; + if (property == '') continue; + jsonObject[prop_name] = String(property); } + return jsonObject; + } + + // toJSON(): any { + // return { + // name: this.name, + // stat_t: this.stat_t, + // uniq_id: this.uniq_id, + // }; + // } } export class MQTTDevice { - name: string = ""; - identifiers: string[] = ["MQTT"]; - serial_number: string = ""; - configuration_url: string = ""; + name: string = ''; + identifiers: string[] = ['MQTT']; + serial_number: string = ''; + configuration_url: string = ''; } -// export enum ENTITY_TYPE { -// light = 0, -// switch = 1, -// sensor = 2, -// binary_sensor = 3, -// button = 4, -// } +export class DeviceClass { + value: number = 0; + choices: string[] = ['None']; -export enum DEVICE_CLASS { - motion = 0, - movement = 1, - outlet = 2 - } \ No newline at end of file + constructor(choices: string[]) { + this.choices = choices; + } + + toString(): string { + return this.choices[this.value]; + } +}