CHANGE: automatic topic generation in class
This commit is contained in:
@@ -1,6 +1,16 @@
|
|||||||
import { DeviceClass, MQTTEntity } from './mqtt_base';
|
import { DeviceClass, iMQTTEntityBase, MQTTEntity } from './mqtt_base';
|
||||||
|
|
||||||
export class MqttBinary extends MQTTEntity {
|
export class MqttBinary extends MQTTEntity {
|
||||||
|
override ent_type: string = 'binary';
|
||||||
|
override attrs: Set<string> = new Set([
|
||||||
|
'name',
|
||||||
|
'uniq_id',
|
||||||
|
'stat_t',
|
||||||
|
'pl_on',
|
||||||
|
'pl_off',
|
||||||
|
'dev_cla',
|
||||||
|
]);
|
||||||
|
|
||||||
dev_cla: DeviceClass = new DeviceClass([
|
dev_cla: DeviceClass = new DeviceClass([
|
||||||
'battery',
|
'battery',
|
||||||
'battery_charging',
|
'battery_charging',
|
||||||
|
|||||||
@@ -1,9 +1,48 @@
|
|||||||
import { MQTTEntity } from './mqtt_base';
|
import { iMQTTEntityBase, MQTTEntity } from './mqtt_base';
|
||||||
|
|
||||||
export class MqttLight extends MQTTEntity {
|
export class MqttLight extends MQTTEntity {
|
||||||
cmd_t: string = 'command/topic';
|
|
||||||
bri_cmd_t: string = 'brightness/command/topic';
|
|
||||||
pl_on: string = '1';
|
pl_on: string = '1';
|
||||||
pl_off: string = '0';
|
pl_off: string = '0';
|
||||||
val_tpl: string = '';
|
val_tpl: string = '';
|
||||||
|
bri_cmd_t: string = 'brightness/command/topic';
|
||||||
|
_cmd_t: string = 'command/topic';
|
||||||
|
|
||||||
|
override readonly ent_type: string = 'light';
|
||||||
|
override attrs: Set<string> = 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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';
|
unit_of_meas: string = 'meassure';
|
||||||
|
override ent_type: string = 'sensor';
|
||||||
|
override attrs: Set<string> = new Set([
|
||||||
|
'name',
|
||||||
|
'uniq_id',
|
||||||
|
'stat_t',
|
||||||
|
'unit_of_meas',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface iMqttSensor extends iMQTTEntityBase {
|
||||||
|
unit_of_meas: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 {
|
export class MqttSwitch extends MQTTEntity {
|
||||||
|
override ent_type: string = 'switch';
|
||||||
|
override attrs: Set<string> = new Set([
|
||||||
|
'name',
|
||||||
|
'uniq_id',
|
||||||
|
'stat_t',
|
||||||
|
'dev_cla',
|
||||||
|
'cmd_t',
|
||||||
|
'pl_on',
|
||||||
|
'pl_off',
|
||||||
|
]);
|
||||||
dev_cla: DeviceClass = new DeviceClass(['switch', 'outlet']);
|
dev_cla: DeviceClass = new DeviceClass(['switch', 'outlet']);
|
||||||
cmd_t: string = 'command/topic';
|
cmd_t: string = 'command/topic';
|
||||||
pl_on: string = '1';
|
pl_on: string = '1';
|
||||||
pl_off: string = '0';
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,49 @@
|
|||||||
export class MQTTEntity {
|
import { EventEmitter, Injectable } from '@angular/core';
|
||||||
name: string = '';
|
import { EntityService } from '../_services/entity.service';
|
||||||
stat_t: string = '';
|
|
||||||
uniq_id: string = '';
|
|
||||||
// dev: MQTTDevice | null = null;
|
|
||||||
// entity_type: ENTITY_TYPE = 0;
|
|
||||||
|
|
||||||
setProperty(name: unknown, value: any): void {
|
@Injectable()
|
||||||
if (!this.hasOwnProperty(String(name))) return;
|
export class MQTTEntity implements iMQTTEntityBase {
|
||||||
this[name as keyof this] = value;
|
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 {
|
set name(name: string) {
|
||||||
if (!this.hasOwnProperty(name)) return;
|
this._name = name;
|
||||||
return this[name as keyof this];
|
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 {
|
toJSON(): any {
|
||||||
@@ -24,14 +55,15 @@ export class MQTTEntity {
|
|||||||
}
|
}
|
||||||
return jsonObject;
|
return jsonObject;
|
||||||
}
|
}
|
||||||
|
getProperty(name: string): any {
|
||||||
|
if (!this.attrs.has(name)) return;
|
||||||
|
return this[name as keyof this];
|
||||||
|
}
|
||||||
|
|
||||||
// toJSON(): any {
|
setProperty(name: string, value: any) {
|
||||||
// return {
|
if (!this.attrs.has(name)) return;
|
||||||
// name: this.name,
|
this[name as keyof this] = value;
|
||||||
// stat_t: this.stat_t,
|
}
|
||||||
// uniq_id: this.uniq_id,
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class MQTTDevice {
|
export class MQTTDevice {
|
||||||
@@ -53,3 +85,16 @@ export class DeviceClass {
|
|||||||
return this.choices[this.value];
|
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;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user