@constructorfleet/ultimate-govee
Version:
Library for interacting with Govee devices written in Typescript.
101 lines • 4.35 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); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BleChannelService = void 0;
const common_1 = require("@nestjs/common");
const cqrs_1 = require("@nestjs/cqrs");
const rxjs_1 = require("rxjs");
const _ultimate_govee_common_1 = require("../../../common");
const _ultimate_govee_data_1 = require("../../../data");
const cqrs_2 = require("../../devices/cqrs");
const channel_service_1 = require("../channel.service");
const ble_channel_providers_1 = require("./ble-channel.providers");
let BleChannelService = class BleChannelService extends channel_service_1.ChannelService {
constructor(bleClient, eventBus, enabled, deviceIds) {
super(eventBus, enabled, { devices: deviceIds });
this.bleClient = bleClient;
this.togglable = true;
this.name = 'ble';
this.devices = {};
this.peripherals = {};
this.subscriptions.push((0, rxjs_1.combineLatest)([this.onConfigChanged$, this.onEnabledChanged$])
.subscribe(() => {
this.bleClient.enabled.next(this.isEnabled);
}), bleClient.peripheralDecoded.subscribe((peripheral) => {
const device = this.devices[peripheral.address];
if (!device) {
return;
}
this.peripherals[peripheral.id] = peripheral;
const event = new cqrs_2.DeviceStatusReceivedEvent({
id: device.id,
model: device.model,
pactCode: device.pactCode,
pactType: device.pactType,
cmd: 'status',
state: peripheral.state,
});
this.eventBus.publish(event);
}));
this.bleClient.enabled.next(this.isEnabled);
}
allowedDevice(device) {
return this.getConfig()?.devices?.includes(device.id) ?? true;
}
handle(event) {
this.bleClient.cancelCommand(event.commandId);
}
toPeripheralId(deviceId) {
return deviceId.replace(/:/g, '').toLowerCase();
}
toDeviceId(peripheralId) {
return (0, _ultimate_govee_common_1.chunk)(peripheralId.toUpperCase().split(''), 2).join(':');
}
recordDevice(device) {
if (!this.allowedDevice || device.bleAddress === undefined) {
return;
}
this.devices[device.bleAddress] = device;
}
isPeripheralKnown(deviceId) {
return this.peripherals[this.toPeripheralId(deviceId)] !== undefined;
}
sendCommand(commandId, id, bleAddress, commands, results$) {
if (!this.isEnabled) {
return;
}
const device = this.devices[bleAddress];
if (!device) {
this.logger.error(`Device ${id} not known`);
return results$.complete();
}
this.bleClient.commandQueue.next({
commandId,
id: id,
address: bleAddress,
commands,
results$,
});
}
};
exports.BleChannelService = BleChannelService;
exports.BleChannelService = BleChannelService = __decorate([
(0, common_1.Injectable)(),
(0, cqrs_1.EventsHandler)(cqrs_2.CommandExpiredEvent),
__param(2, ble_channel_providers_1.InjectEnabled),
__param(3, ble_channel_providers_1.InjectDeviceIds),
__metadata("design:paramtypes", [_ultimate_govee_data_1.BleClient,
cqrs_1.EventBus, Boolean, Array])
], BleChannelService);
//# sourceMappingURL=ble-channel.service.js.map