@constructorfleet/ultimate-govee
Version:
Library for interacting with Govee devices written in Typescript.
77 lines • 3.53 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 BlePublishCommandHandler_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BlePublishCommandHandler = void 0;
const cqrs_1 = require("@nestjs/cqrs");
const commands_1 = require("../commands");
const common_1 = require("@nestjs/common");
const ble_channel_service_1 = require("../ble-channel.service");
const cqrs_2 = require("../../../devices/cqrs");
const rxjs_1 = require("rxjs");
const cqrs_3 = require("@nestjs/cqrs");
let BlePublishCommandHandler = BlePublishCommandHandler_1 = class BlePublishCommandHandler {
constructor(eventBus, service) {
this.eventBus = eventBus;
this.service = service;
this.logger = new common_1.Logger(BlePublishCommandHandler_1.name);
this.expiredIds = [];
}
handle(event) {
this.expiredIds.push(event.commandId);
(0, rxjs_1.timer)(5000)
.pipe((0, rxjs_1.map)(() => event.commandId))
.subscribe((id) => this.expiredIds.splice(this.expiredIds.indexOf(id), 1));
}
async execute(command) {
const results$ = new rxjs_1.Subject();
const subscriptions = [];
const unsubscripe = () => {
subscriptions.forEach((sub) => {
sub.unsubscribe();
});
};
subscriptions.push((0, rxjs_1.timer)(10000).subscribe(() => {
unsubscripe();
}), results$
.pipe((0, rxjs_1.reduce)((acc, opCode) => {
return [...acc, opCode];
}, []), (0, rxjs_1.map)((commands) => ({
id: command.id,
cmd: 'status',
model: '',
pactCode: 0,
pactType: 1,
state: {},
op: {
command: commands,
},
})), (0, rxjs_1.map)((status) => {
command.debug && this.logger.debug(status);
return new cqrs_2.DeviceStatusReceivedEvent(status);
}))
.subscribe((event) => {
unsubscripe();
this.logger.debug(`Publishing ${event.constructor.name}`);
this.eventBus.publish(event);
}));
command.debug && this.logger.debug(command.commands);
await this.service.sendCommand(command.commandId, command.id, command.bleAddress, command.commands, results$);
}
};
exports.BlePublishCommandHandler = BlePublishCommandHandler;
exports.BlePublishCommandHandler = BlePublishCommandHandler = BlePublishCommandHandler_1 = __decorate([
(0, cqrs_1.CommandHandler)(commands_1.BlePublishCommand),
(0, cqrs_1.EventsHandler)(cqrs_2.CommandExpiredEvent),
__metadata("design:paramtypes", [cqrs_3.EventBus,
ble_channel_service_1.BleChannelService])
], BlePublishCommandHandler);
//# sourceMappingURL=ble-publish.handler.js.map