Skip to content

Commit

Permalink
feat: loop argument for basic functions
Browse files Browse the repository at this point in the history
  • Loading branch information
marnixah committed Jan 21, 2022
1 parent fee68a9 commit ab64e98
Showing 1 changed file with 39 additions and 33 deletions.
72 changes: 39 additions & 33 deletions src/classes/tv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,78 +9,80 @@ export default class TV {
this.backendURL = process.env.SONY_BACKEND_URL || 'http://localhost:3000';
}

sendIRCC(command: string): Promise<Response> {
sendIRCC(command: string, times?: number): Promise<Response[]> {
const url = `${this.backendURL}/tv/${this.ip}`;
return fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ command: command }),
});
}

left(): Promise<Response> {
return this.sendIRCC('Left');
}

right(): Promise<Response> {
const promises: Promise<Response>[] = [];
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<Response[]> {
return this.sendIRCC('Left', times);
}

right(times?: number): Promise<Response[]> {
return this.sendIRCC('Right');
}

up(): Promise<Response> {
up(times?: number): Promise<Response[]> {
return this.sendIRCC('Up');
}

down(): Promise<Response> {
down(times?: number): Promise<Response[]> {
return this.sendIRCC('Down');
}

ok(): Promise<Response> {
ok(times?: number): Promise<Response[]> {
return this.sendIRCC('Confirm');
}

off(): Promise<Response> {
off(times?: number): Promise<Response[]> {
return this.sendIRCC('PowerOff');
}

on(): Promise<Response> {
on(times?: number): Promise<Response[]> {
return this.sendIRCC('WakeUp');
}

sleep(): Promise<Response> {
sleep(times?: number): Promise<Response[]> {
return this.sendIRCC('Sleep');
}

volumeUp(): Promise<Response> {
volumeUp(times?: number): Promise<Response[]> {
return this.sendIRCC('VolumeUp');
}

volumeDown(): Promise<Response> {
volumeDown(times?: number): Promise<Response[]> {
return this.sendIRCC('VolumeDown');
}

mute(): Promise<Response> {
mute(): Promise<Response[]> {
return this.sendIRCC('Mute');
}

home(): Promise<Response> {
home(): Promise<Response[]> {
return this.sendIRCC('Home');
}

applicationLauncher(): Promise<Response> {
applicationLauncher(): Promise<Response[]> {
return this.sendIRCC('ApplicationLauncher');
}

async resetApplicationLauncher(): Promise<void> {
// 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<void> {
Expand All @@ -97,4 +99,8 @@ export default class TV {
await this.down();
await this.ok();
}

topOfScreen(): Promise<Response[]> {
return this.up(30);
}
}

0 comments on commit ab64e98

Please sign in to comment.