@constructorfleet/ultimate-govee
Version:
Library for interacting with Govee devices written in Typescript.
134 lines • 6.54 kB
JavaScript
"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;
};
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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IoTChannelService = void 0;
const common_1 = require("@nestjs/common");
const cqrs_1 = require("@nestjs/cqrs");
const json_stringify_safe_1 = __importDefault(require("json-stringify-safe"));
const rxjs_1 = require("rxjs");
const uuid_1 = require("uuid");
const _ultimate_govee_data_1 = require("../../../data");
const device_refresh_event_1 = require("../../devices/cqrs/events/device-refresh.event");
const device_state_command_event_1 = require("../../devices/cqrs/events/device-state-command.event");
const device_status_received_event_1 = require("../../devices/cqrs/events/device-status-received.event");
const channel_service_1 = require("../channel.service");
const iot_channel_providers_1 = require("./iot-channel.providers");
const command_expired_event_1 = require("../../devices/cqrs/events/command-expired.event");
let IoTChannelService = class IoTChannelService extends channel_service_1.ChannelService {
constructor(enabled, iot, eventBus) {
super(eventBus, enabled);
this.iot = iot;
this.togglable = true;
this.name = 'iot';
this.expiredCommands = [];
this.refreshDevice$ = new rxjs_1.Subject();
this.subscriptions.push((0, rxjs_1.combineLatest)([this.onConfigChanged$, this.onEnabledChanged$]).subscribe(async ([iotData, enabled]) => enabled ? await this.connect(iotData) : await this.disconnect()), this.refreshDevice$
.pipe((0, rxjs_1.skipWhile)(() => !this.isEnabled), (0, rxjs_1.filter)((event) => event.addresses.iotTopic !== undefined &&
typeof event.addresses.iotTopic !== 'string'))
.subscribe((event) => this.publishMessage((0, uuid_1.v4)(), event.addresses.iotTopic, {
topic: event.addresses.iotTopic,
msg: {
accountTopic: this.getConfig()?.topic,
cmd: 'status',
cmdVersion: 0,
transaction: `u_${Date.now()}`,
type: 0,
},
}, event.debug)));
}
async handle(event) {
if (!this.isEnabled) {
return;
}
if (event instanceof command_expired_event_1.CommandExpiredEvent) {
this.expiredCommands.push(event.commandId);
if (this.expiredCommands.length > 100) {
const overCount = this.expiredCommands.length - 100;
this.expiredCommands.splice(0, overCount);
}
return;
}
if (event.addresses.iotTopic === undefined ||
typeof event.addresses.iotTopic !== 'string') {
return;
}
if (event instanceof device_refresh_event_1.DeviceRefeshEvent) {
this.refreshDevice$.next(event);
this.publishMessage((0, uuid_1.v4)(), event.addresses.iotTopic, {
topic: event.addresses.iotTopic,
msg: {
accountTopic: this.getConfig()?.topic,
cmd: 'status',
cmdVersion: 0,
transaction: `u_${Date.now()}`,
type: 0,
},
}, event.debug);
return;
}
return await this.publishMessage(event.command.commandId, event.addresses.iotTopic, {
topic: event.addresses.iotTopic,
msg: {
accountTopic: this.getConfig()?.topic,
cmd: event.command.command ?? 'ptReal',
cmdVersion: event.command.cmdVersion ?? 0,
data: event.command.data,
transaction: `u_${Date.now()}`,
type: event.command.type ?? 1,
},
}, event.debug);
}
async disconnect() {
await this.iot.disconnect();
}
async connect(iotData, deviceIds) {
await this.iot.connect(iotData, (message) => {
if (deviceIds === undefined || deviceIds?.includes(message.id) === true) {
this.eventBus.publish(new device_status_received_event_1.DeviceStatusReceivedEvent(message));
}
});
this.iot.subscribe(iotData.topic);
}
async publishMessage(commandId, topic, payload, debug) {
if (commandId in this.expiredCommands || !this.isEnabled) {
return;
}
if ('msg' in payload && !!payload.msg && typeof payload.msg === 'object') {
if ('accountTopic' in payload.msg && !payload.msg.accountTopic) {
payload.msg.accountTopic = this.getConfig()?.topic;
}
}
if (debug === true) {
this.logger.debug(topic, payload);
}
this.eventBus.publish(new command_expired_event_1.CommandExpiredEvent(commandId));
return await this.iot.send(topic, (0, json_stringify_safe_1.default)(payload));
}
async onModuleDestroy() {
await this.disconnect();
super.closeSubscriptions();
}
};
exports.IoTChannelService = IoTChannelService;
exports.IoTChannelService = IoTChannelService = __decorate([
(0, cqrs_1.EventsHandler)(device_refresh_event_1.DeviceRefeshEvent, device_state_command_event_1.DeviceStateCommandEvent, command_expired_event_1.CommandExpiredEvent),
(0, common_1.Injectable)(),
__param(0, iot_channel_providers_1.InjectEnabled),
__metadata("design:paramtypes", [Boolean, _ultimate_govee_data_1.IoTService,
cqrs_1.EventBus])
], IoTChannelService);
//# sourceMappingURL=iot-channel.service.js.map