@constructorfleet/ultimate-govee
Version:
Library for interacting with Govee devices written in Typescript.
140 lines • 6.54 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DetectionSettingsState = exports.DetectionSettingsStateName = exports.EnablePresenceState = exports.EnablePresenceStateName = exports.BiologicalPresenceState = exports.MMWavePresenceState = exports.BiologicalPresenceStateName = exports.MMWavePresenceStateName = void 0;
const device_state_1 = require("../../../states/device.state");
const presence_state_1 = require("../../../states/presence.state");
const _ultimate_govee_common_1 = require("../../../../../common");
const moment_1 = __importDefault(require("moment"));
exports.MMWavePresenceStateName = 'presence-mmWave';
exports.BiologicalPresenceStateName = 'presence-biological';
class MMWavePresenceState extends presence_state_1.PresenceState {
constructor(device) {
super(device, 'mmWave', _ultimate_govee_common_1.OpType.REPORT, 1);
}
}
exports.MMWavePresenceState = MMWavePresenceState;
class BiologicalPresenceState extends presence_state_1.PresenceState {
constructor(device) {
super(device, 'biological', _ultimate_govee_common_1.OpType.REPORT, 1, -1, -1, -1);
}
}
exports.BiologicalPresenceState = BiologicalPresenceState;
exports.EnablePresenceStateName = 'enablePresence';
class EnablePresenceState extends device_state_1.DeviceOpState {
constructor(device, defaultState = {}) {
super({ opType: _ultimate_govee_common_1.OpType.REPORT, identifier: [31] }, device, exports.EnablePresenceStateName, defaultState);
this.stateToCommand = (state) => {
if ((state.biologicalEnabled ?? this.value.biologicalEnabled) === undefined) {
this.logger.warn('Missing biologicalEnabled, skipping command');
return undefined;
}
if ((state.mmWaveEnabled ?? this.value.mmWaveEnabled) === undefined) {
this.logger.warn('Missing mmWaveEnabled, skipping command');
return undefined;
}
return {
command: {
command: 'multiSync',
data: {
command: [
(0, _ultimate_govee_common_1.asOpCode)(_ultimate_govee_common_1.OpType.COMMAND, ...this.identifier, (state.biologicalEnabled ?? this.value.biologicalEnabled) === true
? 0x01
: 0x00, (state.mmWaveEnabled ?? this.value.mmWaveEnabled) === true
? 0x01
: 0x00),
],
},
},
status: {
op: {
command: [
[
(state.biologicalEnabled ?? this.value.biologicalEnabled) === true
? 0x01
: 0x00,
(state.mmWaveEnabled ?? this.value.mmWaveEnabled) === true
? 0x01
: 0x00,
],
],
},
},
};
};
}
parseOpCommand(opCommand) {
this.stateValue.next({
biologicalEnabled: opCommand[0] === 0x01,
mmWaveEnabled: opCommand[1] === 0x01,
});
}
}
exports.EnablePresenceState = EnablePresenceState;
exports.DetectionSettingsStateName = 'detectionSettings';
class DetectionSettingsState extends device_state_1.DeviceOpState {
constructor(device) {
super({
opType: _ultimate_govee_common_1.OpType.REPORT,
identifier: [0x05, 0x01],
}, device, exports.DetectionSettingsStateName, {});
this.stateToCommand = (state) => {
const distance = state.detectionDistance ?? this.value.detectionDistance;
const absence = state.absenceDuration ?? this.value.absenceDuration;
const report = state.reportDetection ?? this.value.reportDetection;
if (distance?.value === undefined) {
this.logger.warn('Missing detection distance value, ignoring command.');
return;
}
if (absence?.value === undefined) {
this.logger.warn('Missing absence duration, ignoring command');
return;
}
if (report?.value === undefined) {
this.logger.warn('Missing report detection, ignoring command');
return;
}
absence.value = moment_1.default.duration(absence.value, absence.unit).asSeconds();
report.value = moment_1.default.duration(report.value, report.unit).asSeconds();
// TODO: Convert distance
const distanceValue = [distance.value >> 8, distance.value % 256];
const absenceValue = [absence.value >> 8, absence.value % 256];
const reportValue = [report.value >> 8, report.value % 256];
return {
command: {
data: {
command: [
(0, _ultimate_govee_common_1.asOpCode)(_ultimate_govee_common_1.OpType.COMMAND, 0x05, 0x00, 0x01),
(0, _ultimate_govee_common_1.asOpCode)(_ultimate_govee_common_1.OpType.COMMAND, ...this.identifier, ...distanceValue, ...absenceValue, ...reportValue),
],
},
},
status: {
op: {
command: [[...distanceValue, ...absenceValue, ...reportValue]],
},
},
};
};
}
parseOpCommand(opCommand) {
this.stateValue.next({
detectionDistance: {
value: (0, _ultimate_govee_common_1.total)(opCommand.slice(0, 2)),
unit: 'cm',
},
absenceDuration: {
value: (0, _ultimate_govee_common_1.total)(opCommand.slice(2, 4)),
unit: 's',
},
reportDetection: {
value: (0, _ultimate_govee_common_1.total)(opCommand.slice(4, 6)),
unit: 's',
},
});
}
}
exports.DetectionSettingsState = DetectionSettingsState;
//# sourceMappingURL=presence.states.js.map