lifxlan
Version:
TypeScript library for controlling LIFX products over LAN
120 lines • 4.96 kB
JavaScript
import * as Encoding from '../encoding.js';
import { Type } from '../constants/index.js';
export 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 === Type.StateZone) {
response = Encoding.decodeStateZone(bytes, offsetRef);
expectedZones.delete(response.zone_index);
}
else if (responseType === 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: Type.GetColorZones,
payload: Encoding.encodeGetColorZones(startIndex, endIndex),
decode,
defaultResponseMode: 'response',
};
}
export function SetColorZonesCommand(startIndex, endIndex, hue, saturation, brightness, kelvin, duration, apply) {
return {
type: Type.SetColorZones,
payload: Encoding.encodeSetColorZones(startIndex, endIndex, hue, saturation, brightness, kelvin, duration, apply),
decode: Encoding.decodeStateMultiZone,
defaultResponseMode: 'ack-only',
};
}
export function GetMultiZoneEffectCommand() {
return {
type: Type.GetMultiZoneEffect,
decode: Encoding.decodeStateMultiZoneEffect,
defaultResponseMode: 'response',
};
}
export function SetMultiZoneEffectCommand(instanceid, effectType, speed, duration, parameters) {
return {
type: Type.SetMultiZoneEffect,
payload: Encoding.encodeSetMultiZoneEffect(instanceid, effectType, speed, duration, parameters),
decode: Encoding.decodeStateMultiZoneEffect,
defaultResponseMode: 'ack-only',
};
}
export function GetExtendedColorZonesCommand(onResponse) {
const expectedZoneIndexes = new Set();
let firstResponse = true;
const responses = [];
const decode = (bytes, offsetRef, continuation, responseType) => {
let response;
if (responseType === 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: Type.GetExtendedColorZones,
decode,
};
}
export function SetExtendedColorZonesCommand(duration, apply, zoneIndex, colorsCount, colors) {
return {
type: Type.SetExtendedColorZones,
payload: Encoding.encodeSetExtendedColorZones(duration, apply, zoneIndex, colorsCount, colors),
decode: Encoding.decodeStateExtendedColorZones,
};
}
//# sourceMappingURL=multizone.js.map