From ab64e98a8521631bb881efb95db86021b3312abe Mon Sep 17 00:00:00 2001 From: marnixah Date: Fri, 21 Jan 2022 11:59:08 +0100 Subject: [PATCH] feat: loop argument for basic functions --- src/classes/tv.ts | 72 +++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/src/classes/tv.ts b/src/classes/tv.ts index acfe3ea..1182e3d 100644 --- a/src/classes/tv.ts +++ b/src/classes/tv.ts @@ -9,78 +9,80 @@ export default class TV { this.backendURL = process.env.SONY_BACKEND_URL || 'http://localhost:3000'; } - sendIRCC(command: string): Promise { + sendIRCC(command: string, times?: number): Promise { const url = `${this.backendURL}/tv/${this.ip}`; - return fetch(url, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ command: command }), - }); - } - - left(): Promise { - return this.sendIRCC('Left'); - } - - right(): Promise { + const promises: Promise[] = []; + if (!times) times = 1; + for (let i = 0; i < times; i++) { + promises.push( + fetch(url, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ command: command }), + }) + ); + } + return Promise.all(promises); + } + + left(times?: number): Promise { + return this.sendIRCC('Left', times); + } + + right(times?: number): Promise { return this.sendIRCC('Right'); } - up(): Promise { + up(times?: number): Promise { return this.sendIRCC('Up'); } - down(): Promise { + down(times?: number): Promise { return this.sendIRCC('Down'); } - ok(): Promise { + ok(times?: number): Promise { return this.sendIRCC('Confirm'); } - off(): Promise { + off(times?: number): Promise { return this.sendIRCC('PowerOff'); } - on(): Promise { + on(times?: number): Promise { return this.sendIRCC('WakeUp'); } - sleep(): Promise { + sleep(times?: number): Promise { return this.sendIRCC('Sleep'); } - volumeUp(): Promise { + volumeUp(times?: number): Promise { return this.sendIRCC('VolumeUp'); } - volumeDown(): Promise { + volumeDown(times?: number): Promise { return this.sendIRCC('VolumeDown'); } - mute(): Promise { + mute(): Promise { return this.sendIRCC('Mute'); } - home(): Promise { + home(): Promise { return this.sendIRCC('Home'); } - applicationLauncher(): Promise { + applicationLauncher(): Promise { return this.sendIRCC('ApplicationLauncher'); } async resetApplicationLauncher(): Promise { // Go left 5 times and up 2 times - await this.left(); - await this.left(); - await this.left(); - await this.left(); - await this.left(); - await this.up(); - await this.up(); + await this.left(5); + await this.up(2); } async browser(): Promise { @@ -97,4 +99,8 @@ export default class TV { await this.down(); await this.ok(); } + + topOfScreen(): Promise { + return this.up(30); + } }