@constructorfleet/ultimate-govee
Version:
Library for interacting with Govee devices written in Typescript.
84 lines • 3.84 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 UltimateGoveeService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UltimateGoveeService = void 0;
const common_1 = require("@nestjs/common");
const cqrs_1 = require("@nestjs/cqrs");
const auth_1 = require("./domain/auth");
const rxjs_1 = require("rxjs");
const ultimate_govee_config_1 = require("./ultimate-govee.config");
const cqrs_2 = require("./domain/devices/cqrs");
const domain_1 = require("./domain");
const core_1 = require("@nestjs/core");
const devices_service_1 = require("./domain/devices/devices.service");
let UltimateGoveeService = UltimateGoveeService_1 = class UltimateGoveeService {
constructor(config, deviceService, moduleRef, commandBus, eventBus) {
this.config = config;
this.deviceService = deviceService;
this.commandBus = commandBus;
this.eventBus = eventBus;
this.logger = new common_1.Logger(UltimateGoveeService_1.name);
this.subscriptions = [];
this.channels = {
ble: moduleRef.get(domain_1.BleChannelService, { strict: false }),
iot: moduleRef.get(domain_1.IoTChannelService, { strict: false }),
openapi: moduleRef.get(domain_1.OpenApiChannelService, { strict: false }),
};
this.subscriptions.push((0, rxjs_1.interval)(5000).subscribe(() => common_1.Logger.flush()));
}
channel(name) {
return this.channels[name];
}
get deviceDiscovered() {
return this.eventBus.pipe((0, cqrs_1.ofType)(cqrs_2.DeviceDiscoveredEvent));
}
constructDevice(deviceModel) {
return this.deviceService.createDeviceFromModel(deviceModel);
}
async connect(username, password) {
this.logger.log('Issuing authenticate command');
await this.commandBus.execute(new auth_1.SetCredentialsCommand({
username,
password,
}));
}
closeSubscriptions() {
this.subscriptions.map((sub) => sub.unsubscribe());
this.commandBus.subject$.complete();
this.eventBus.subject$.complete();
try {
this.channels.ble.closeSubscriptions();
this.channels.iot.closeSubscriptions();
this.channels.openapi.closeSubscriptions();
}
catch (err) {
this.logger.warn('Unable to close susbscriptions to control channels.');
}
}
onModuleDestroy() {
this.closeSubscriptions();
common_1.Logger.flush();
}
};
exports.UltimateGoveeService = UltimateGoveeService;
exports.UltimateGoveeService = UltimateGoveeService = UltimateGoveeService_1 = __decorate([
(0, common_1.Injectable)(),
__param(0, ultimate_govee_config_1.InjectGoveeConfig),
__metadata("design:paramtypes", [Object, devices_service_1.DevicesService,
core_1.ModuleRef,
cqrs_1.CommandBus,
cqrs_1.EventBus])
], UltimateGoveeService);
//# sourceMappingURL=ultimate-govee.service.js.map