UNPKG

@felixgeelhaar/govee-api-client

Version:

Enterprise-grade TypeScript client library for the Govee Developer REST API

119 lines 3.56 kB
import { ColorRgb, ColorTemperature, Brightness } from '../value-objects'; export class Command { } export class PowerOnCommand extends Command { constructor() { super(...arguments); this.name = 'turn'; this.value = 'on'; } toObject() { return { name: this.name, value: this.value }; } } export class PowerOffCommand extends Command { constructor() { super(...arguments); this.name = 'turn'; this.value = 'off'; } toObject() { return { name: this.name, value: this.value }; } } export class BrightnessCommand extends Command { constructor(brightness) { super(); this.name = 'brightness'; this._brightness = brightness; } get value() { return this._brightness.level; } get brightness() { return this._brightness; } toObject() { return { name: this.name, value: this.value }; } } export class ColorCommand extends Command { constructor(color) { super(); this.name = 'color'; this._color = color; } get value() { return this._color.toObject(); } get color() { return this._color; } toObject() { return { name: this.name, value: this.value }; } } export class ColorTemperatureCommand extends Command { constructor(colorTemperature) { super(); this.name = 'colorTem'; this._colorTemperature = colorTemperature; } get value() { return this._colorTemperature.kelvin; } get colorTemperature() { return this._colorTemperature; } toObject() { return { name: this.name, value: this.value }; } } export class CommandFactory { static powerOn() { return new PowerOnCommand(); } static powerOff() { return new PowerOffCommand(); } static brightness(brightness) { return new BrightnessCommand(brightness); } static color(color) { return new ColorCommand(color); } static colorTemperature(colorTemperature) { return new ColorTemperatureCommand(colorTemperature); } static fromObject(obj) { switch (obj.name) { case 'turn': if (obj.value === 'on') { return new PowerOnCommand(); } else if (obj.value === 'off') { return new PowerOffCommand(); } throw new Error(`Invalid power command value: ${obj.value}`); case 'brightness': if (typeof obj.value === 'number') { return new BrightnessCommand(new Brightness(obj.value)); } throw new Error(`Invalid brightness command value: ${obj.value}`); case 'color': if (typeof obj.value === 'object' && obj.value !== null) { const colorValue = obj.value; return new ColorCommand(ColorRgb.fromObject(colorValue)); } throw new Error(`Invalid color command value: ${obj.value}`); case 'colorTem': if (typeof obj.value === 'number') { return new ColorTemperatureCommand(new ColorTemperature(obj.value)); } throw new Error(`Invalid color temperature command value: ${obj.value}`); default: throw new Error(`Unknown command name: ${obj.name}`); } } } //# sourceMappingURL=Command.js.map