@constructorfleet/ultimate-govee
Version:
Library for interacting with Govee devices written in Typescript.
187 lines • 8.07 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PurifierActiveMode = exports.CustomModeState = exports.CustomModeStateName = exports.ManualModeState = exports.ManualModeStateName = void 0;
const _ultimate_govee_common_1 = require("../../../../../common");
const states_1 = require("../../../states");
const humidifier_modes_1 = require("../humidifier/humidifier.modes");
var PurifierModes;
(function (PurifierModes) {
PurifierModes[PurifierModes["MANUAL"] = 1] = "MANUAL";
PurifierModes[PurifierModes["PROGRAM"] = 2] = "PROGRAM";
PurifierModes[PurifierModes["AUTO"] = 3] = "AUTO";
})(PurifierModes || (PurifierModes = {}));
exports.ManualModeStateName = 'manualMode';
class ManualModeState extends states_1.DeviceOpState {
constructor(device, opType = _ultimate_govee_common_1.OpType.REPORT, identifier = [0x05, PurifierModes.MANUAL]) {
super({ opType, identifier }, device, exports.ManualModeStateName, undefined);
this.speedIndex = 0;
this.stateToCommand = (nextState) => {
if (nextState === undefined) {
this.logger.warn('Fan speed not specified, ignoring command');
return;
}
const filler = (0, _ultimate_govee_common_1.ArrayRange)(Math.max(this.speedIndex, 1))
.fill(0)
.slice(0, this.speedIndex);
return {
command: {
data: {
command: [
(0, _ultimate_govee_common_1.asOpCode)(_ultimate_govee_common_1.OpType.COMMAND, this.identifier, filler, nextState),
],
},
},
status: {
op: {
command: [[0, nextState]],
},
},
};
};
}
parseOpCommand(opCommand) {
if (opCommand[0] !== PurifierModes.MANUAL) {
return;
}
const command = opCommand.slice(1);
this.speedIndex = command.indexOf(0x00) - 1;
this.stateValue.next(command[this.speedIndex]);
}
}
exports.ManualModeState = ManualModeState;
exports.CustomModeStateName = 'customMode';
class CustomModeState extends states_1.DeviceOpState {
constructor(device, opType = _ultimate_govee_common_1.OpType.REPORT, identifier = [0x05, PurifierModes.PROGRAM]) {
super({ opType, identifier }, device, exports.CustomModeStateName, undefined);
this.customModes = {};
this.stateToCommand = (nextState) => {
if (nextState === undefined || nextState?.fanSpeed === undefined) {
this.logger.warn('Fan speed not specified, ignoring command');
return;
}
const newProgram = {
id: nextState.id ?? this.customModes.currentProgram?.id ?? 0,
duration: nextState?.duration ?? this.customModes.currentProgram?.duration ?? 100,
remaining: nextState?.remaining ??
this.customModes.currentProgram?.duration ??
100,
fanSpeed: nextState?.fanSpeed ?? this.customModes?.currentProgram?.fanSpeed ?? 1,
};
const newPrograms = {
...(this.customModes.programs ?? {
0: {
id: 0,
duration: 100,
remaining: newProgram.id > 0 ? 0 : 100,
fanSpeed: 0,
},
1: {
id: 1,
duration: 100,
remaining: newProgram.id > 1 ? 0 : 100,
fanSpeed: 0,
},
2: {
id: 2,
duration: 32640,
remaining: 32640,
fanSpeed: 0,
},
}),
[newProgram.id]: newProgram,
};
return {
command: {
data: {
command: [
(0, _ultimate_govee_common_1.asOpCode)(_ultimate_govee_common_1.OpType.COMMAND, this.identifier, newProgram.id, ...[0, 1, 2].reduce((commands, program) => {
commands.push(...[
newPrograms[program].fanSpeed,
Math.floor(newPrograms[program].duration / 255),
newPrograms[program].duration % 255,
Math.floor(newPrograms[program].remaining / 255),
newPrograms[program].remaining % 255,
]);
return commands;
}, [])),
],
},
},
status: {
op: {
command: [
[
newProgram.id,
...[0, 1, 2].reduce((commands, program) => {
commands.push(...[
newPrograms[program].fanSpeed,
Math.floor(newPrograms[program].duration / 255),
newPrograms[program].duration % 255,
undefined,
undefined,
]);
return commands;
}, []),
],
],
},
},
};
};
}
parseOpCommand(opCommand) {
const command = opCommand.slice(1);
const value = {
currentProgramId: command[0],
programs: [0, 1, 2].map((i) => ({
id: i,
fanSpeed: command[1 + 5 * i],
duration: (0, _ultimate_govee_common_1.total)([command[2 + 5 * i], command[3 + 5 * i]]),
remaining: (0, _ultimate_govee_common_1.total)([command[4 + 5 * i], command[5 + 5 * i]]),
})),
};
this.customModes = {
...value,
currentProgram: value.currentProgramId !== undefined
? value.programs[value.currentProgramId]
: undefined,
};
this.stateValue.next(this.customModes.currentProgram);
}
}
exports.CustomModeState = CustomModeState;
class PurifierActiveMode extends states_1.ModeState {
constructor(device, states) {
super(device, states.filter((state) => state !== undefined));
this.stateToCommand = () => {
return undefined;
};
this.activeIdentifier.subscribe((identifier) => {
if (identifier === undefined) {
return;
}
switch (identifier[0]) {
case PurifierModes.MANUAL:
this.stateValue.next(this.modes.find((mode) => mode.name === exports.ManualModeStateName));
break;
case PurifierModes.PROGRAM:
this.stateValue.next(this.modes.find((mode) => mode.name === exports.CustomModeStateName));
break;
case PurifierModes.AUTO:
this.stateValue.next(this.modes.find((mode) => mode.name === humidifier_modes_1.AutoModeStateName));
break;
default:
break;
}
});
}
setState(nextState) {
if (nextState === undefined) {
this.logger.warn('Next state is not specified, ignoring command');
return [];
}
return nextState.setState(nextState.value);
}
}
exports.PurifierActiveMode = PurifierActiveMode;
//# sourceMappingURL=purifier.modes.js.map