From 5a9cc1603c1f004e6fa31c6a6a4a9e36b3179f5d Mon Sep 17 00:00:00 2001 From: Kevin Van den Abeele Date: Mon, 1 Jun 2020 16:33:17 +0200 Subject: [PATCH] Implementing & testing functions setting mode, v-swing & h-swing working --- resources/wideq/example.py | 30 +++++++- src/lg/wideq-adapter.ts | 148 ++++++++++++++++++++++++++++++++++++- src/utils/python-utils.ts | 6 +- 3 files changed, 174 insertions(+), 10 deletions(-) diff --git a/resources/wideq/example.py b/resources/wideq/example.py index 9014a39..38e57d2 100755 --- a/resources/wideq/example.py +++ b/resources/wideq/example.py @@ -134,7 +134,27 @@ def set_temp(client, device_id, temp): """Set the configured temperature for an AC device.""" ac = wideq.ACDevice(client, _force_device(client, device_id)) - ac.set_fahrenheit(int(temp)) + ac.set_celsius(int(temp)) + + +def set_mode(client, device_id, mode): + ac = wideq.ACDevice(client, _force_device(client, device_id)) + ac.set_mode(wideq.ACMode[mode]) + + +def set_fan_speed(client, device_id, speed): + ac = wideq.ACDevice(client, _force_device(client, device_id)) + ac.set_fan_speed(wideq.ACFanSpeed[speed]) + + +def set_swing_mode_v(client, device_id, swing_mode_v): + ac = wideq.ACDevice(client, _force_device(client, device_id)) + ac.set_vert_swing(wideq.ACVSwingMode[swing_mode_v]) + + +def set_swing_mode_h(client, device_id, swing_mode_h): + ac= wideq.ACDevice(client, _force_device(client, device_id)) + ac.set_horz_swing(wideq.ACHSwingMode[swing_mode_h]) def turn(client, device_id, on_off): @@ -162,9 +182,13 @@ def ac_config(client, device_id): 'ls': ls, 'mon': mon, 'ac-mon': ac_mon, - 'set-temp': set_temp, - 'turn': turn, 'ac-config': ac_config, + 'turn': turn, + 'set-temp': set_temp, + 'set-mode': set_mode, + 'set-speed': set_fan_speed, + 'set-swing-v': set_swing_mode_v, + 'set-swing-h': set_swing_mode_h } diff --git a/src/lg/wideq-adapter.ts b/src/lg/wideq-adapter.ts index ae8b6f2..9d91601 100644 --- a/src/lg/wideq-adapter.ts +++ b/src/lg/wideq-adapter.ts @@ -25,7 +25,7 @@ export class WideqAdapter { } public async listAirCoolers(): Promise> { - const data: string = await this.pythonUtils.executePython3(this.wideqScriptFile,['ls', '-c ' + this.country, '-l ' + this.language, '-v']); + const data: string = await this.pythonUtils.executePython3(this.wideqScriptFile, ['-c ' + this.country, '-l ' + this.language, '-v', 'ls']); const devices = data.split('\n'); @@ -44,16 +44,156 @@ export class WideqAdapter { return processedDevices; } + + public async getStatus(): Promise { + return null; + } + + public async setPowerOnOff(deviceId: string, poweredOn: boolean): Promise { + return false; + } + + public async setTargetTemperature(deviceId: string, temperatureInCelcius: number): Promise { + return false; + } + + public async setMode(deviceId: string, mode: Mode): Promise { + try { + const data: string = await this.pythonUtils.executePython3(this.wideqScriptFile, ['-c ' + this.country, '-l ' + this.language, '-v', 'set-mode ' + deviceId + ' ' + mode]); + console.log(data); + return true; + } catch (error) { + console.error(error); + return false; + } + } + + public async setFanSpeed(deviceId: string, fanSpeed: FanSpeed): Promise { + return false; + } + + public async setSwingModeV(deviceId: string, swingModeV: VSwingMode): Promise { + try { + const data: string = await this.pythonUtils.executePython3(this.wideqScriptFile, ['-c ' + this.country, '-l ' + this.language, '-v', 'set-swing-v ' + deviceId + ' ' + swingModeV]); + console.log(data); + return true; + } catch (error) { + console.error(error); + return false; + } + } + + public async setSwingModeH(deviceId: string, swingModeH: HSwingMode): Promise { + try { + const data: string = await this.pythonUtils.executePython3(this.wideqScriptFile, ['-c ' + this.country, '-l ' + this.language, '-v', 'set-swing-h ' + deviceId + ' ' + swingModeH]); + console.log(data); + return true; + } catch (error) { + console.error(error); + return false; + } + } } //TODO: Testing only! setTimeout(async () => { - const test = new WideqAdapter('BE', 'en-UK'); - const result = await test.listAirCoolers(); - console.log(result); + const testAdapter = new WideqAdapter('BE', 'en-UK'); + const airCoolers: AirCooler[] = await testAdapter.listAirCoolers(); + const airCooler: AirCooler = airCoolers[0]; + console.log(airCooler); + const status: AirCoolerStatus = await testAdapter.getStatus(); + console.log(status); + + const sleep = (ms: number) => { + return new Promise(resolve => setTimeout(resolve, ms)); + }; + + let success: boolean = false; + //success = await testAdapter.setMode(airCooler.deviceId, Mode.COOL); + success = await testAdapter.setSwingModeV(airCooler.deviceId, VSwingMode.ONE); + console.log(success); + await sleep(2500); + success = await testAdapter.setSwingModeH(airCooler.deviceId, HSwingMode.ONE); + console.log(success); }); export interface AirCooler { deviceId: string; deviceType: string; +} + +export interface AirCoolerStatus { + +} + +/* +* WIDEQ AC related enums +* Please check ac.py for updated values & documentation +**/ + +export enum HSwingMode { + OFF = "OFF", + ONE = "ONE", + TWO = "TWO", + THREE = "THREE", + FOUR = "FOUR", + FIVE = "FIVE", + LEFT_HALF = "LEFT_HALF", + RIGHT_HALF = "RIGHT_HALF", + ALL = "ALL", +} + +export enum VSwingMode { + OFF = "OFF", + ONE = "ONE", + TWO = "TWO", + THREE = "THREE", + FOUR = "FOUR", + FIVE = "FIVE", + SIX = "SIX", + ALL = "ALL" +} + +export enum Mode { + COOL = "COOL", + DRY = "DRY", + FAN = "FAN", + AI = "AI", + HEAT = "HEAT", + AIRCLEAN = "AIRCLEAN", + ACO = "ACO", + AROMA = "AROMA", + ENERGY_SAVING = "ENERGY_SAVING", + ENERGY_SAVER = "ENERGY_SAVER", +} + +export enum FanSpeed { + SLOW = 'SLOW', + SLOW_LOW = 'SLOW_LOW', + LOW = 'LOW', + LOW_MID = 'LOW_MID', + MID = 'MID', + MID_HIGH = 'MID_HIGH', + HIGH = 'HIGH', + POWER = 'POWER', + AUTO = 'AUTO', + NATURE = 'NATURE', + R_LOW = 'R_LOW', + R_MID = 'R_MID', + R_HIGH = 'R_HIGH', + L_LOW = 'L_LOW', + L_MID = 'L_MID', + L_HIGH = 'L_HIGH', + L_LOWR_LOW = 'L_LOWR_LOW', + L_LOWR_MID = 'L_LOWR_MID', + L_LOWR_HIGH = 'L_LOWR_HIGH', + L_MIDR_LOW = 'L_MIDR_LOW', + L_MIDR_MID = 'L_MIDR_MID', + L_MIDR_HIGH = 'L_MIDR_HIGH', + L_HIGHR_LOW = 'L_HIGHR_LOW', + L_HIGHR_MID = 'L_HIGHR_MID', + L_HIGHR_HIGH = 'L_HIGHR_HIGH', + AUTO_2 = 'AUTO_2', + POWER_2 = 'POWER_2', + LONGPOWER = 'LONGPOWER' } \ No newline at end of file diff --git a/src/utils/python-utils.ts b/src/utils/python-utils.ts index 9839b66..ec9a1fd 100644 --- a/src/utils/python-utils.ts +++ b/src/utils/python-utils.ts @@ -2,10 +2,10 @@ const exec = require('child_process').exec; export class PythonUtils { - private readonly workingdDir: string; + private readonly workingDir: string; constructor(workingDir: string) { - this.workingdDir = workingDir; + this.workingDir = workingDir; } public async executePython3(scriptName: string, args: string[], forceCloseProcessAfterOutput: boolean = false): Promise { @@ -18,7 +18,7 @@ export class PythonUtils { let data: string[] = []; console.log('python3 -u example.py ' + pythonArgs.join(' ')); - const process = exec('python3 -u example.py ' + pythonArgs.join(' '), {cwd: this.workingdDir}, (error: any) => { + const process = exec('python3 -u example.py ' + pythonArgs.join(' '), {cwd: this.workingDir}, (error: any) => { if (error) { console.error(error); reject(error);