UNPKG

@constructorfleet/ultimate-govee

Version:

Library for interacting with Govee devices written in Typescript.

146 lines 5.87 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MqttClient = void 0; const common_1 = require("@nestjs/common"); const reactive_enum_1 = require("@ngbites/reactive-enum"); const mqtt_1 = require("mqtt"); var ClientState; (function (ClientState) { ClientState[ClientState["uninitialized"] = 0] = "uninitialized"; ClientState[ClientState["initialized"] = 1] = "initialized"; ClientState[ClientState["connecting"] = 2] = "connecting"; ClientState[ClientState["connected"] = 3] = "connected"; ClientState[ClientState["reconnecting"] = 4] = "reconnecting"; ClientState[ClientState["disconnected"] = 5] = "disconnected"; ClientState[ClientState["offline"] = 6] = "offline"; ClientState[ClientState["ended"] = 7] = "ended"; ClientState[ClientState["closed"] = 8] = "closed"; })(ClientState || (ClientState = {})); let MqttClient = class MqttClient { constructor() { this.logger = new common_1.Logger('MqttClient'); this.state = (0, reactive_enum_1.reactiveEnum)(ClientState, { initialValue: ClientState.uninitialized, }); } async quit() { await this.delegate?.endAsync(); } async connect(connection, messagehandler) { try { this.messageHander = messagehandler; this.delegate = await (0, mqtt_1.connectAsync)(connection.brokerUrl, { username: connection.username, password: connection.password, clientId: connection.clientId, }); this.delegate.on('connect', (packet) => { this.connectionHandler(packet); }); this.delegate.on('reconnect', () => { this.reconnectHandler(); }); this.delegate.on('close', () => { this.closeHandler(); }); this.delegate.on('disconnect', (packet) => { this.disconnectHandler(packet); }); this.delegate.on('offline', () => { this.offlineHandler(); }); this.delegate.on('error', (error) => { this.errorHandler(error); }); this.delegate.on('end', () => { this.endHandler(); }); this.delegate.on('message', async (topic, message, packet) => { if (this.messageHander === undefined) { this.logger.warn('Received message but mo message handler asssigned'); return; } await this.onMessage(topic, message, packet); }); } catch (error) { this.logger.error('Error connecting to MQTT broker', error); } } async publish(topic, message, options) { try { if (this.delegate === undefined) { throw new Error('Unable to publish message to uninitiated MQTT client.'); } if (this.state.value() !== ClientState.connected) { throw new Error('Unable to publish message to unconnected MQTT cleint.'); } await this.delegate.publishAsync(topic, message, options); } catch (error) { this.logger.error(`Error publishing to MQTT topic ${topic}`, error); } } async subscribe(topic, options) { try { if (this.delegate === undefined) { throw new Error('Unable to subscribe to topic to uninitiated MQTT client.'); } if (this.state.value() !== ClientState.connected) { throw new Error('Unable to subscribe to topic to unconnected MQTT cleint.'); } return await this.delegate.subscribeAsync(topic, options); } catch (error) { this.logger.error(`Error subscribing to MQTT topic ${topic}`, error); } } connectionHandler(_) { // TODO : connAct = true and clean is `false - rely on stored messages instead of subscribing this.state.set(ClientState.connected); } reconnectHandler() { this.state.set(ClientState.reconnecting); } closeHandler() { this.state.set(ClientState.closed); } disconnectHandler(_) { this.state.set(ClientState.disconnected); } offlineHandler() { this.state.set(ClientState.offline); } errorHandler(error) { this.logger.error('Error during MQTT operations', error); } endHandler() { this.state.set(ClientState.ended); this.messageHander = undefined; this.delegate = undefined; } async onMessage(topic, message, packet) { try { if (this.messageHander === undefined) { throw new Error(`No message handler defined to process message from ${topic}`); } await this.messageHander.handleMessage(topic, message, packet); } catch (error) { this.logger.error('Unable to processes message', error); } } }; exports.MqttClient = MqttClient; exports.MqttClient = MqttClient = __decorate([ (0, common_1.Injectable)({ scope: common_1.Scope.TRANSIENT, }) ], MqttClient); //# sourceMappingURL=mqtt.client.js.map