UNPKG

@constructorfleet/ultimate-govee

Version:

Library for interacting with Govee devices written in Typescript.

96 lines 4.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deviceMatches = void 0; const decoder_constants_1 = require("./decoder.constants"); const deviceMatches = (device, conditions, operand) => { if (conditions.length === 0) { return true; } if (conditions.includes(decoder_constants_1.Or)) { return conditions .reduce((acc, cond) => { if (cond === decoder_constants_1.Or) { acc.push([]); } else { acc[acc.length - 1].push(cond); } return acc; }, [[]]) .some((conditionGroup) => (0, exports.deviceMatches)(device, conditionGroup)); } // logger.debug({ // device, // condition: conditions[0], // condition1: conditions[1], // operand, // }); switch (conditions[0]) { case decoder_constants_1.ServiceData: return (0, exports.deviceMatches)(device, conditions.slice(1), device.serviceData); case decoder_constants_1.ManufacturerData: return (0, exports.deviceMatches)(device, conditions.slice(1), device.manufacturerData); case decoder_constants_1.NoManufacturerData: return (device.manufacturerData.length === 0 && (0, exports.deviceMatches)(device, conditions.slice(1))); case decoder_constants_1.Name: return (0, exports.deviceMatches)(device, conditions.slice(1), (operand ?? device).name); case decoder_constants_1.UUID: return (0, exports.deviceMatches)(device, conditions.slice(1), (operand ?? device).uuid); case decoder_constants_1.MacAtIndex: return (0, exports.deviceMatches)(device, conditions.slice(2), device.macAddress[conditions[1]]); case decoder_constants_1.ReverseMacAtIndex: return (0, exports.deviceMatches)(device, conditions.slice(2), device.macAddress.split('').reverse().join('')[conditions[1]]); case decoder_constants_1.And: return (0, exports.deviceMatches)(device, conditions.slice(1)); case decoder_constants_1.Or: return true; case decoder_constants_1.Inverse: return !(0, exports.deviceMatches)(device, conditions.slice(1), operand); case decoder_constants_1.Index: return (0, exports.deviceMatches)(device, conditions.slice(2), operand.slice(typeof conditions[1] === 'string' ? Number.parseInt(conditions[1], 10) : conditions[1])); case '>=': return ((typeof operand === 'number' ? operand : operand.length) >= conditions[1] && (0, exports.deviceMatches)(device, conditions.slice(2), operand)); case '<=': return ((typeof operand === 'number' ? operand : operand.length) <= conditions[1] && (0, exports.deviceMatches)(device, conditions.slice(2), operand)); case '>': return ((typeof operand === 'number' ? operand : operand.length) > conditions[1] && (0, exports.deviceMatches)(device, conditions.slice(2), operand)); case '<': return ((typeof operand === 'number' ? operand : operand.length) < conditions[1] && (0, exports.deviceMatches)(device, conditions.slice(2), operand)); case '=': if (typeof conditions[1] === 'string') { return (operand.toString().startsWith(conditions[1]) && (0, exports.deviceMatches)(device, conditions.slice(2), operand)); } if (typeof operand === 'string') { return (operand.length === conditions[1] && (0, exports.deviceMatches)(device, conditions.slice(2), operand)); } return (conditions[1] === operand && (0, exports.deviceMatches)(device, conditions.slice(2), operand)); default: if (typeof conditions[0] === 'string') { return (operand.toString().startsWith(conditions[0]) && (0, exports.deviceMatches)(device, conditions.slice(1), operand)); } if (typeof operand === 'string') { return (operand.length === conditions[0] && (0, exports.deviceMatches)(device, conditions.slice(1), operand)); } return (conditions[0] === operand && (0, exports.deviceMatches)(device, conditions.slice(1), operand)); return false; } }; exports.deviceMatches = deviceMatches; //# sourceMappingURL=device.condition.js.map