|
1 | 1 | import {ReadStream, WriteStream} from "fs"; |
2 | 2 | import * as http2 from "http2"; |
3 | 3 |
|
| 4 | +import {IAVSOptions, TokenService} from "./auth/token-service"; |
4 | 5 | import {BASE_URLS} from "./constants/base-urls"; |
5 | 6 | import {API_VERSION} from "./constants/general"; |
6 | 7 | import Directives from "./directives"; |
7 | 8 | import SpeechRecognizer from "./speech-recognizer"; |
8 | 9 | import System from "./system"; |
9 | 10 |
|
10 | 11 | export class AVSApi { |
11 | | - constructor(private readonly readStream: ReadStream, private readonly writeStream: WriteStream) {} |
| 12 | + private accessToken: string; |
| 13 | + private readonly speechRecognizer: SpeechRecognizer; |
| 14 | + private readonly directives: Directives; |
| 15 | + private readonly system: System; |
| 16 | + private readonly context: AVS.Context; |
12 | 17 |
|
13 | | - public start(): void { |
14 | | - const client = http2.connect(`${BASE_URLS.europe}`); |
15 | | - client.on("error", (err) => console.error(err)); |
16 | | - client.on("socketError", (err) => console.error(err)); |
| 18 | + constructor(private readonly readStream: ReadStream, private readonly writeStream: WriteStream, options: IAVSOptions) { |
| 19 | + const client = http2 |
| 20 | + .connect(`${BASE_URLS.europe}`) |
| 21 | + .on("error", (err) => console.error(err)) |
| 22 | + .on("socketError", (err) => console.error(err)); |
| 23 | + |
| 24 | + this.context = []; |
| 25 | + this.speechRecognizer = new SpeechRecognizer(client); |
| 26 | + this.directives = new Directives(client); |
| 27 | + this.system = new System(client); |
| 28 | + |
| 29 | + const tokenService = new TokenService(options); |
| 30 | + tokenService.Token$.subscribe((res) => (this.accessToken = res.access_token)); |
| 31 | + } |
17 | 32 |
|
18 | | - const context: AVS.Context = []; |
| 33 | + public start(): void { |
| 34 | + if (!this.accessToken) { |
| 35 | + setTimeout(() => { |
| 36 | + console.log("Access token not present. Re-trying in 1000"); |
| 37 | + this.start(); |
| 38 | + }, 1000); |
| 39 | + return; |
| 40 | + } |
19 | 41 |
|
20 | | - const speechRecognizer = new SpeechRecognizer(client); |
21 | | - const directives = new Directives(client); |
22 | | - const system = new System(client); |
| 42 | + console.log(`Access token found`); |
23 | 43 |
|
24 | | - const accessToken = |
25 | | - "Atza|IwEBIFymtJ3Bb2MIW4SvBaYj7Gb9jSdrMoo1iFvfun3Ttp-oXxDXFy815mm1b2lksZZU1b3b2RWVUBslKbs9ef6R0UoEF21RcK3I0uMcmQ8yXEuXuy_wR_Z6wyHvAqcxFxPxdVzmqw145-eHPNu2gbk4nQ731JXrPYwdHs38IsLgnaeHCUHTj-DDF6Tq1AnTW7aoyNUfg0hwNIbtDKV--xQ0M5LYgMAe4cfdIz43n5dMbp1hMhe4ZPrwFeCttnkfheW04-sNmhAGjA5dgT11XSW8kAfsZ7RvMLt28DRJKcVAYxhvjjzT3h9yW-g2MOhXBrYyxezKHH1FpPmHQIBBTG_iCrPSwmOUEWfFrz7iEfjWH6rcdBVBpEyXghTaZkMI2d3ZdugQJ48-ES9QNk30QUn41q3IqOimCN5sKJ83KsRSY4aGaSqn8csuVXeZHROqvYNavHjA4vbjmtSbydrTYMarNHmBznULzb-CQGsSWIL4RYxA3mCRvDQjoQzGigAnS5rzHg0"; |
| 44 | + this.init(); |
| 45 | + } |
26 | 46 |
|
27 | | - directives.connect(accessToken); |
28 | | - system.synchronizeState(accessToken, context); |
29 | | - speechRecognizer.recognize(accessToken, context, this.readStream); |
| 47 | + private async init(): Promise<void> { |
| 48 | + await this.directives.connect(this.accessToken); |
| 49 | + await this.system.synchronizeState(this.accessToken, this.context); |
| 50 | + await this.speechRecognizer.recognize(this.accessToken, this.context, this.readStream); |
30 | 51 | } |
31 | 52 | } |
0 commit comments