CHANGE: seperated mqtt device

This commit is contained in:
2024-08-02 07:09:46 +02:00
parent f6fb4f6b61
commit e6a49d55e2
2 changed files with 59 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
import { MqttDevice } from './mqtt-device';
describe('MqttDevice', () => {
it('should create an instance', () => {
expect(new MqttDevice()).toBeTruthy();
});
});

View File

@@ -0,0 +1,52 @@
import { EventEmitter } from "@angular/core";
import { hash } from "./mqtt_base";
export class MQTTDevice {
private _name: string = '';
private _identifier: string = '';
private _serial_number: string = '';
// private configuration_url: string = '';
topic_updates = new EventEmitter<string>();
get name() {
return this._name;
}
set name(name: string) {
this._name = name;
if (name == '') this._serial_number = '';
else this._serial_number = hash(name);
this.topic_updates.next('stat_t');
this.topic_updates.next('cmd_t');
this.topic_updates.next('bri_cmd_t');
}
get serial_number() {
return this._serial_number;
}
set serial_number(serial_number: string) {
this._serial_number = serial_number;
}
get indetifier() {
this._identifier =
this._name == '' && this._serial_number == ''
? ''
: [this._name, this._serial_number].join('_');
return this._identifier;
}
toJSON(full: boolean = false): { [key: string]: string } {
if (full)
return {
...this.toJSON(),
name: this._name,
serial_number: this._serial_number,
};
return {
ids: this._identifier,
};
}
}