import * as logger from './log' import { mqttHandler } from './MqttDispatcher' import { ExportType } from './Export' import * as Events from 'events' export interface HasStateTopic { getStateTopic() : string } export interface HasStateAndFeedbackTopic extends HasStateTopic { getStateFeedbackTopic() : string } export interface HasStateAndFeedbackTopicAndLabel extends HasFeedbackTopic { getLabel() : string } export interface HasStateAndFeedbackTopicAndLabelAndRoom extends HasStateAndFeedbackTopic { getLabel() : string getRoom() : string } export interface HasFeedbackTopic { getStateFeedbackTopic() : string } export interface HasInTopic { getInTopic() : string } export abstract class AItem extends Events.EventEmitter { protected topicFirstPart: string protected itemId: string protected label: string protected item: string protected room: string protected floor: string protected subscribeTopics: string[] constructor(floor: string, room: string, item: string, label: string = '') { super() this.floor = floor this.room = room this.item = item this.itemId = `${this.floor}_${this.room}_${this.item}` if (label == '') { this.label = this.itemId } else { this.label = label } this.topicFirstPart = `dispatcher_ng/items/${this.floor}/${this.room}/${this.item}` } abstract processMessage(topic: string, payload: string) : void startFunc() : void { } exportItem() : ExportType|null { return null } start() : void { mqttHandler.register(this.subscribeTopics, (topic: string, payload: string) : void => { // logger.info(`item ${this.itemId}: ${topic}, ${payload}`) this.processMessage(topic, payload) }, () => { this.startFunc() }) } }