UNPKG

lifxlan

Version:

TypeScript library for controlling LIFX products over LAN

161 lines 6.84 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.GetColorZonesCommand = GetColorZonesCommand; exports.SetColorZonesCommand = SetColorZonesCommand; exports.GetMultiZoneEffectCommand = GetMultiZoneEffectCommand; exports.SetMultiZoneEffectCommand = SetMultiZoneEffectCommand; exports.GetExtendedColorZonesCommand = GetExtendedColorZonesCommand; exports.SetExtendedColorZonesCommand = SetExtendedColorZonesCommand; const Encoding = __importStar(require("../encoding.js")); const index_js_1 = require("../constants/index.js"); function GetColorZonesCommand(startIndex, endIndex, onResponse) { const expectedZones = new Set(); for (let i = startIndex; i <= endIndex; i++) { expectedZones.add(i); } const responses = []; const decode = (bytes, offsetRef, continuation, responseType) => { let response; if (responseType === index_js_1.Type.StateZone) { response = Encoding.decodeStateZone(bytes, offsetRef); expectedZones.delete(response.zone_index); } else if (responseType === index_js_1.Type.StateMultiZone) { response = Encoding.decodeStateMultiZone(bytes, offsetRef); // Remove all zones covered by this response for (let i = 0; i < response.colors.length; i++) { expectedZones.delete(response.zone_index + i); } } // Update continuation to indicate if more responses are expected if (continuation) { if (response) { responses.push(response); // Call user callback if provided let shouldContinue = true; if (onResponse) { const result = onResponse(response); shouldContinue = result !== false; // false = stop early } continuation.expectMore = shouldContinue && expectedZones.size > 0; } else { // Unknown response type - still expect more responses continuation.expectMore = expectedZones.size > 0; } } return responses; // Always return the accumulated array }; return { type: index_js_1.Type.GetColorZones, payload: Encoding.encodeGetColorZones(startIndex, endIndex), decode, defaultResponseMode: 'response', }; } function SetColorZonesCommand(startIndex, endIndex, hue, saturation, brightness, kelvin, duration, apply) { return { type: index_js_1.Type.SetColorZones, payload: Encoding.encodeSetColorZones(startIndex, endIndex, hue, saturation, brightness, kelvin, duration, apply), decode: Encoding.decodeStateMultiZone, defaultResponseMode: 'ack-only', }; } function GetMultiZoneEffectCommand() { return { type: index_js_1.Type.GetMultiZoneEffect, decode: Encoding.decodeStateMultiZoneEffect, defaultResponseMode: 'response', }; } function SetMultiZoneEffectCommand(instanceid, effectType, speed, duration, parameters) { return { type: index_js_1.Type.SetMultiZoneEffect, payload: Encoding.encodeSetMultiZoneEffect(instanceid, effectType, speed, duration, parameters), decode: Encoding.decodeStateMultiZoneEffect, defaultResponseMode: 'ack-only', }; } function GetExtendedColorZonesCommand(onResponse) { const expectedZoneIndexes = new Set(); let firstResponse = true; const responses = []; const decode = (bytes, offsetRef, continuation, responseType) => { let response; if (responseType === index_js_1.Type.StateExtendedColorZones) { response = Encoding.decodeStateExtendedColorZones(bytes, offsetRef); // On first response, calculate expected zone indexes based on total zones if (firstResponse && response.zones_count > 82) { firstResponse = false; // Each response can contain up to 82 zones for (let i = 0; i < response.zones_count; i += 82) { expectedZoneIndexes.add(i); } } expectedZoneIndexes.delete(response.zone_index); } // Update continuation to indicate if more responses are expected if (continuation) { if (response) { responses.push(response); // Call user callback if provided let shouldContinue = true; if (onResponse) { const result = onResponse(response); shouldContinue = result !== false; // false = stop early } continuation.expectMore = shouldContinue && expectedZoneIndexes.size > 0; } else { // Unknown response type - still expect more responses continuation.expectMore = expectedZoneIndexes.size > 0; } } return responses; // Always return the accumulated array }; return { type: index_js_1.Type.GetExtendedColorZones, decode, }; } function SetExtendedColorZonesCommand(duration, apply, zoneIndex, colorsCount, colors) { return { type: index_js_1.Type.SetExtendedColorZones, payload: Encoding.encodeSetExtendedColorZones(duration, apply, zoneIndex, colorsCount, colors), decode: Encoding.decodeStateExtendedColorZones, }; } //# sourceMappingURL=multizone.js.map