UNPKG

@constructorfleet/ultimate-govee

Version:

Library for interacting with Govee devices written in Typescript.

83 lines 5.54 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; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var BleChannelSagas_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.BleChannelSagas = void 0; const common_1 = require("@nestjs/common"); const cqrs_1 = require("@nestjs/cqrs"); const equal_lib_1 = require("@santi100/equal-lib"); const rxjs_1 = require("rxjs"); const uuid_1 = require("uuid"); const _ultimate_govee_common_1 = require("../../../common"); const cqrs_2 = require("../../devices/cqrs"); 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 ble_channel_service_1 = require("./ble-channel.service"); const commands_1 = require("./commands"); const configure_ble_channel_command_1 = require("./commands/configure-ble-channel.command"); const events_1 = require("./events"); let BleChannelSagas = BleChannelSagas_1 = class BleChannelSagas { constructor(service, eventBus) { this.service = service; this.eventBus = eventBus; this.logger = new common_1.Logger(BleChannelSagas_1.name); this.configureBleChannelFlow = (events$) => events$.pipe((0, cqrs_1.ofType)(events_1.BleChannelConfigReceivedEvent), (0, rxjs_1.map)((event) => new configure_ble_channel_command_1.ConfigureBleChannelCommand(false, event.config))); this.configurationChangedFlow = (events$) => events$.pipe((0, cqrs_1.ofType)(events_1.BleChannelChangedEvent), (0, rxjs_1.map)((event) => new configure_ble_channel_command_1.ConfigureBleChannelCommand(event.enabled, event.config))); this.deviceDiscoveredFlow = (events$) => events$.pipe((0, cqrs_1.ofType)(cqrs_2.DeviceDiscoveredEvent), (0, rxjs_1.map)((event) => new commands_1.BleRecordDeviceCommand(event.device))); this.refreshDeviceFlow = (events$) => events$.pipe((0, cqrs_1.ofType)(device_refresh_event_1.DeviceRefeshEvent), (0, rxjs_1.filter)((event) => event.addresses.bleAddress !== undefined), // && event.addresses.iotTopic === undefined), (0, rxjs_1.map)((event) => { if (event.opIdentifiers === undefined) { return new device_refresh_event_1.DeviceRefeshEvent(event.deviceId, event.model, event.goodsType, event.addresses, [[0x01]]); } return event; }), (0, rxjs_1.groupBy)((event) => event.deviceId), (0, rxjs_1.mergeMap)((eventGroup$) => eventGroup$.pipe((0, rxjs_1.throttleTime)(5000, undefined, { leading: true, trailing: false }))), (0, rxjs_1.map)((event) => new commands_1.BlePublishCommand((0, uuid_1.v4)(), event.deviceId, event.addresses.bleAddress, event .opIdentifiers.filter((identifiers, index, arr) => index === 0 || arr .slice(0, index - 1) .find((i) => (0, equal_lib_1.arrayEquality)(i, identifiers)) === undefined) .map((op) => (0, _ultimate_govee_common_1.asOpCode)(0xaa, ...op))))); this.publishBleCommand = (events$) => events$.pipe((0, cqrs_1.ofType)(device_state_command_event_1.DeviceStateCommandEvent), (0, rxjs_1.filter)((event) => event.addresses.bleAddress !== undefined && event.command.command !== undefined && (event.command.command === 'turn' || event.command.data.command !== undefined)), (0, rxjs_1.map)((event) => new commands_1.BlePublishCommand(event.command.commandId, event.id, event.addresses.bleAddress, event.command.command === 'turn' ? [(0, _ultimate_govee_common_1.asOpCode)(_ultimate_govee_common_1.OpType.COMMAND, 0x01, event.command.data.value?.toString() === '1' ? 0x01 : 0x00)] : event.command.data.command.map((value) => typeof value === 'string' ? (0, _ultimate_govee_common_1.base64ToHex)(value) : value)))); } }; exports.BleChannelSagas = BleChannelSagas; __decorate([ (0, cqrs_1.Saga)(), __metadata("design:type", Object) ], BleChannelSagas.prototype, "configureBleChannelFlow", void 0); __decorate([ (0, cqrs_1.Saga)(), __metadata("design:type", Object) ], BleChannelSagas.prototype, "configurationChangedFlow", void 0); __decorate([ (0, cqrs_1.Saga)(), __metadata("design:type", Object) ], BleChannelSagas.prototype, "deviceDiscoveredFlow", void 0); __decorate([ (0, cqrs_1.Saga)(), __metadata("design:type", Object) ], BleChannelSagas.prototype, "refreshDeviceFlow", void 0); __decorate([ (0, cqrs_1.Saga)(), __metadata("design:type", Object) ], BleChannelSagas.prototype, "publishBleCommand", void 0); exports.BleChannelSagas = BleChannelSagas = BleChannelSagas_1 = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [ble_channel_service_1.BleChannelService, cqrs_1.EventBus]) ], BleChannelSagas); //# sourceMappingURL=ble-channel.sagas.js.map