@constructorfleet/ultimate-govee
Version:
Library for interacting with Govee devices written in Typescript.
109 lines • 4.39 kB
JavaScript
;
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;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var MqttService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MqttService = void 0;
const common_1 = require("@nestjs/common");
const mqtt_providers_1 = require("./mqtt.providers");
const isValidConnection = (connection) => {
const matches = connection.brokerUrl?.match(/^(?<protocol>(?:wss?)|(?:mqtts?)):\/\/(?<host>[^:]+):?(?:port[0-9+])?\/.+/);
if (!matches || !matches.groups) {
return false;
}
if (!matches.groups['protocol'] && !matches['host']) {
return false;
}
return true;
};
let MqttService = MqttService_1 = class MqttService {
constructor(deserializer, broker, factory) {
this.deserializer = deserializer;
this.broker = broker;
this.factory = factory;
this.logger = new common_1.Logger(MqttService_1.name);
this.subscriptions = [];
this.subscriptionGrants = [];
}
deserializeMessage(message) {
return this.deserializer(message);
}
async handleMessage(topic, payload) {
if (this.messageHandler === undefined) {
return;
}
const message = this.deserializeMessage(payload);
await this.messageHandler?.handle(message);
}
async publish(topic, payload) {
if (!this.client) {
this.logger.warn(`Cannot publish payload to ${topic}, not connected to broker.`);
return;
}
await this.client.publish(topic, payload);
}
async connect(credentials, messageHandler) {
const connection = {
brokerUrl: this.broker,
...credentials,
};
if (!isValidConnection(connection)) {
throw new Error('The connection provided is invalid please check your configuration.');
}
this.messageHandler = messageHandler;
if (this.client !== undefined) {
this.subscriptions.forEach((s) => s.unsubscribe());
await this.client.quit();
}
await this.createClientAndConnect(connection);
}
async disconnect() {
if (this.client === undefined) {
return;
}
await this.client.quit();
}
async createClientAndConnect(connection) {
this.client = await this.factory(connection);
this.subscriptions.push(this.client.state.connected$.subscribe(() => this.doSubscribe([connection.topic])));
}
async doSubscribe(topics) {
if (this.client === undefined) {
return;
}
await Promise.all(topics
.filter((t) => t !== undefined)
.map(async (topic) => await this.subscribe(topic)));
}
async subscribe(topic) {
if (this.client === undefined || topic === undefined) {
return;
}
const grant = await this.client
?.subscribe(topic)
?.then((sub) => sub?.forEach((s) => s !== undefined && this.subscriptionGrants.push(s)));
if (grant === undefined) {
return;
}
this.subscriptionGrants.push(grant);
}
};
exports.MqttService = MqttService;
exports.MqttService = MqttService = MqttService_1 = __decorate([
(0, common_1.Injectable)(),
__param(0, mqtt_providers_1.InjectMqttDeserializer),
__param(1, mqtt_providers_1.InjectMqttBroker),
__param(2, mqtt_providers_1.InjectMQTTFactory),
__metadata("design:paramtypes", [Function, String, Function])
], MqttService);
//# sourceMappingURL=mqtt.service.js.map