UNPKG

nope-js-node

Version:

NoPE Runtime for Nodejs. For Browser-Support please use nope-browser

92 lines (91 loc) 4.03 kB
/** * @author Martin Karkowski * @email m.karkowski@zema.de * @create date 2021-08-03 17:32:16 * @modify date 2021-08-03 21:14:12 * @desc [description] */ import { ILogger } from "js-logger"; import { MqttClient } from "mqtt"; import { ValidLoggerDefinition } from "../../logger/getLogger"; import { NopeObservable } from "../../observables/nopeObservable"; import { EventnameToEventType, ICommunicationInterface } from "../../types/nope"; /** * Default implementation of an {@link ICommunicationInterface}. * * This layer will use mqtt to connect and transport messages. * * Defaultly all messages will be subscribed on the following topics: * - `+/nope/<eventname>` * * Defaultly all messages will be published on the following topics: * - `<preTopic>/nope/<eventname>` * - `preTopic` is set to the hostname. * * The Layer is able to forward data, events etc to default ports. * Asume data is emitted using the `dataChanged` emit. If the flag * `forwardToCustomTopics` is set to true, the path of the data will * directly forward to mqtt. */ export declare class MQTTLayer implements ICommunicationInterface { uri: string; preTopic: string; qos: 0 | 1 | 2; forwardToCustomTopics: boolean; protected _client: MqttClient; protected _cbs: Map<string, Set<(...args: any[]) => void>>; protected _logger: ILogger; connected: NopeObservable<boolean>; considerConnection: boolean; /** * See {@link ICommunicationInterface.id} */ id: string; /** * Creates an instance of MQTTLayer. * @param {string} uri Uri of the Broker. e.g. `mqtt://localhost:1883` or `ws://localhost:9000`. * @param {ValidLoggerDefinition} [logger="info"] Logger level * @param {string} [preTopic=hostname()] Defaultly all messages will be published on the following topics: `<preTopic>/nope/<eventname>`. `preTopic` is defaultly set to the hostname of the node in which `NoPE` is running. * @param {(0 | 1 | 2)} [qos=2] The QOS of mqtt. see https://www.hivemq.com/blog/mqtt-essentials-part-6-mqtt-quality-of-service-levels/ for more details. Default = Exactly once. Otherwise there might be an issue. * @param {boolean} [forwardToCustomTopics=true] The Layer is able to forward data, events etc to default ports. This flag enables this behavior * @memberof MQTTLayer */ constructor(uri: string, logger?: ValidLoggerDefinition, preTopic?: string, qos?: 0 | 1 | 2, forwardToCustomTopics?: boolean); /** * See {@link ICommunicationInterface.on} */ on<T extends keyof EventnameToEventType>(eventname: T, cb: (data: EventnameToEventType[T]) => void): Promise<void>; /** * See {@link ICommunicationInterface.emit} */ emit<T extends keyof EventnameToEventType>(eventname: T, data: EventnameToEventType[T]): Promise<void>; readonly receivesOwnMessages: boolean; protected _adaptTopic(topic: string): string; /** * Internal Function to subscribe to a topic using a specific callback * @param topic the topic, which should be subscribed to * @param callback the callback to call * @returns */ protected _on(topic: string, callback: (...args: any[]) => void): Promise<void>; /** * Internal function to remove a susbcription from a topic. * To be precise, we only remove the callback. * @param topic the topic, which should be unsubscribed * @param callback the callback to unsubscribe * @returns */ protected _off(topic: string, callback: (...args: any[]) => void): Promise<void>; /** * Internal function to publish data on the given topic * @param topic The topic to publish the data on * @param data The data to publish * @returns */ protected _emit(topic: string, data: any): Promise<void>; /** * Function to dispose the Interface. * @returns nothing */ dispose(): Promise<void>; }