CHANGE: added publish topic functions
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { iMQTTEntityBase, MQTTEntity } from './mqtt_base';
|
||||
import { MQTTEntity } from './mqtt_base';
|
||||
|
||||
export class MqttLight extends MQTTEntity {
|
||||
pl_on: string = '1';
|
||||
@@ -45,4 +45,12 @@ export class MqttLight extends MQTTEntity {
|
||||
override get uniq_id() {
|
||||
return this._uniq_id;
|
||||
}
|
||||
|
||||
override publish_topics(index: number = 1): string[] {
|
||||
return [
|
||||
`String stat_topic_${index} = "${this.stat_t}";`,
|
||||
`String cmd_topic_${index} = "${this.cmd_t}";`,
|
||||
`String bri_cmd_topic_${index} = "${this.bri_cmd_t}";`,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { EntityService } from '../_services/entity.service';
|
||||
import { DeviceClass, iMQTTEntityBase, MQTTEntity } from './mqtt_base';
|
||||
|
||||
export class MqttSwitch extends MQTTEntity {
|
||||
@@ -41,6 +40,13 @@ export class MqttSwitch extends MQTTEntity {
|
||||
override get uniq_id() {
|
||||
return this._uniq_id;
|
||||
}
|
||||
|
||||
override publish_topics(index: number = 1): string[] {
|
||||
return [
|
||||
`String stat_topic_${index} = "${this.stat_t}";`,
|
||||
`String cmd_topic_${index} = "${this.cmd_t}";`,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
export interface iMqttSwitch extends iMQTTEntityBase {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { EventEmitter, Injectable } from '@angular/core';
|
||||
import { EntityService } from '../_services/entity.service';
|
||||
|
||||
@Injectable()
|
||||
export class MQTTEntity implements iMQTTEntityBase {
|
||||
protected _name: string = '';
|
||||
protected _stat_t: string = 'state/topic';
|
||||
protected _uniq_id: string = '';
|
||||
private _size: number = 0;
|
||||
|
||||
attrs = new Set(['name', 'stat_t', 'uniq_id']);
|
||||
topic_updates = new EventEmitter<string>();
|
||||
@@ -18,7 +18,7 @@ export class MQTTEntity implements iMQTTEntityBase {
|
||||
set name(name: string) {
|
||||
this._name = name;
|
||||
if (name == '') return;
|
||||
this._uniq_id = hash(name);
|
||||
this._uniq_id = name + '_' + hash(name);
|
||||
this.topic_updates.next('stat_t');
|
||||
}
|
||||
|
||||
@@ -40,16 +40,19 @@ export class MQTTEntity implements iMQTTEntityBase {
|
||||
}
|
||||
|
||||
get display_name() {
|
||||
if (this._name != '' && this._uniq_id != '') {
|
||||
return this._name + '_' + this._uniq_id;
|
||||
} else {
|
||||
if (this._uniq_id != '') {
|
||||
return this._uniq_id;
|
||||
}
|
||||
return this._name;
|
||||
}
|
||||
|
||||
publish_topics(index: number = 1): string[] {
|
||||
return [`String stat_topic_${index} = "${this.stat_t}";`];
|
||||
}
|
||||
|
||||
toJSON(): any {
|
||||
let jsonObject: { [key: string]: string } = {};
|
||||
for (let prop_name of Object.getOwnPropertyNames(this)) {
|
||||
toJSON(): { [key: string]: string | {} } {
|
||||
let jsonObject: { [key: string]: string | {} } = {};
|
||||
for (let prop_name of this.attrs.values()) {
|
||||
let property = this[prop_name as keyof this];
|
||||
if (property == '') continue;
|
||||
jsonObject[prop_name] = String(property);
|
||||
@@ -63,7 +66,7 @@ export class MQTTEntity implements iMQTTEntityBase {
|
||||
}
|
||||
|
||||
getProperty(name: string): any {
|
||||
if (!this.attrs.has(name)) return;
|
||||
if (!this.attrs.has(name)) return '';
|
||||
return this[name as keyof this];
|
||||
}
|
||||
|
||||
@@ -73,13 +76,6 @@ export class MQTTEntity implements iMQTTEntityBase {
|
||||
}
|
||||
}
|
||||
|
||||
export class MQTTDevice {
|
||||
name: string = '';
|
||||
identifiers: string[] = ['MQTT'];
|
||||
serial_number: string = '';
|
||||
configuration_url: string = '';
|
||||
}
|
||||
|
||||
export class DeviceClass {
|
||||
value: number = 0;
|
||||
choices: string[] = ['None'];
|
||||
@@ -94,22 +90,23 @@ export class DeviceClass {
|
||||
}
|
||||
|
||||
export interface iMQTTEntityBase {
|
||||
// name: string;
|
||||
// stat_t: string;
|
||||
// uniq_id: string;
|
||||
// display_name: string;
|
||||
name: string;
|
||||
stat_t: string;
|
||||
uniq_id: string;
|
||||
display_name: string;
|
||||
|
||||
attrs: Set<string>;
|
||||
readonly ent_type: string;
|
||||
topic_updates: EventEmitter<string>;
|
||||
|
||||
toJSON: () => string;
|
||||
publish_topics: (index: number) => string[];
|
||||
toJSON: () => { [key: string]: string | {} };
|
||||
}
|
||||
|
||||
function hash(str: string): string {
|
||||
export function hash(str: string, digits: number = 5): string {
|
||||
let seed = cyrb128(str);
|
||||
let rand = sfc32(seed[0], seed[1], seed[2], seed[3]);
|
||||
let rand_num = Math.floor(rand() * 100000).toString();
|
||||
let rand_num = Math.floor(rand() * 10 ** digits).toString();
|
||||
return rand_num;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user