UNPKG

mqtt-melcloud

Version:

MQTT integration for Mitsubishi Melcloud devices

75 lines (65 loc) 2.41 kB
const operationMode = [ { key: 1, value: 'heat' }, { key: 2, value: 'dry' }, { key: 3, value: 'cool' }, { key: 7, value: 'fan' }, { key: 8, value: 'auto' }, ]; const vaneVertical = [ { key: 0, value: 'auto' }, { key: 1, value: '0' }, { key: 2, value: '30' }, { key: 3, value: '45' }, { key: 4, value: '60' }, { key: 5, value: '90' }, { key: 7, value: 'swing' }, ]; const vaneHorizontal = [ { key: 0, value: 'auto' }, { key: 1, value: '0' }, { key: 2, value: '45' }, { key: 3, value: '90' }, { key: 4, value: '135' }, { key: 5, value: '180' }, { key: 12, value: 'swing' }, ]; const effectiveFlags = { Power: 1, OperationMode: 2, SetTemperature: 4, SetFanSpeed: 8, VaneVertical: 16, VaneHorizontal: 256, }; const findKey = (v, arr) => { const result = arr.find(({ value }) => value === v); return result ? result.key : undefined; }; // const findValue = (k, arr) => { // const result = arr.find(({ key }) => key === k); // return result ? result.value : undefined; // }; const prepareUpdate = (update) => ({ Power: update.power !== undefined ? !!update.power : undefined, SetTemperature: update.target && Math.round(update.target * 2) / 2, SetFanSpeed: update.fan === 'auto' ? 0 : update.fan, OperationMode: findKey(update.mode, operationMode), VaneHorizontal: findKey(update.horizontal, vaneHorizontal), VaneVertical: findKey(update.vertical, vaneVertical), }); console.log([findKey('dry', operationMode)]); console.log(JSON.stringify(prepareUpdate({}))); console.log(JSON.stringify(prepareUpdate({})) === '{}'); console.log(JSON.stringify(prepareUpdate({ power: true })) === '{"Power":true}'); console.log(JSON.stringify(prepareUpdate({ power: false })) === '{"Power":false}'); console.log(JSON.stringify(prepareUpdate({ target: 11.7 })) === '{"SetTemperature":11.5}'); console.log(JSON.stringify(prepareUpdate({ fan: 'auto' })) === '{"SetFanSpeed":0}'); console.log(JSON.stringify(prepareUpdate({ fan: 2 })) === '{"SetFanSpeed":2}'); console.log(JSON.stringify(prepareUpdate({ mode: 'auto' })) === '{"OperationMode":8}'); console.log(JSON.stringify(prepareUpdate({ mode: 'dry' })) === '{"OperationMode":2}'); const change = prepareUpdate({ power: true }); const e = Object.keys(change) .map((k) => (change[k] === undefined ? 0 : effectiveFlags[k])) .reduce((r, a) => r + a, 0); console.log(Object.values(effectiveFlags).reduce((r, a) => r + a, 0)); console.log(e);