Skip to content

Commit 0dc4560

Browse files
committed
Work on structuring
1 parent 38c2f08 commit 0dc4560

9 files changed

Lines changed: 133 additions & 65 deletions

File tree

cSpell.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"version": "0.1",
33
"language": "en",
4-
"words": ["spdy", "multipart", "avs", "iavs"],
4+
"words": ["spdy", "multipart", "avs", "iavs", "wakeword"],
55
"ignorePaths": ["**/node_modules/**", "**/vscode-extension/**", "**/.git/**", ".vscode", "typings", "resources"]
66
}

examples/mic-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,7 @@ record
6464
.start({
6565
sampleRate: 44100,
6666
verbose: true,
67+
silence: -1,
68+
channels: 1,
6769
})
6870
.pipe(file);

examples/test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ const AVSApi = require("../dist").AVSApi;
1515

1616
const stream = record.start({
1717
verbose: true,
18+
silence: "5.0",
19+
sampleRate: 16000,
20+
channels: 1,
1821
});
1922

20-
const avs = new AVSApi(stream);
23+
const avs = new AVSApi(stream, undefined, {
24+
});
2125
console.log(avs.start());

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"lint": "tslint --project .",
1111
"prepublishOnly": "rimraf dist && npm run build",
1212
"spell": "cspell \"src/**/*.ts\"",
13-
"example": "node --expose-http2 \"examples/test.js\""
13+
"example": "node --expose-http2 \"examples/test.js\"",
14+
"mic": "node \"examples/mic-test.js\""
1415
},
1516
"repository": {
1617
"type": "git",

src/auth/token-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface IAVSOptions {
1414
clientSecret: string;
1515
}
1616

17-
export default class TokenService {
17+
export class TokenService {
1818
private token$: Observable<IAmazonTokenResponse>;
1919

2020
constructor(options: IAVSOptions) {

src/avs-api.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,52 @@
11
import {ReadStream, WriteStream} from "fs";
22
import * as http2 from "http2";
33

4+
import {IAVSOptions, TokenService} from "./auth/token-service";
45
import {BASE_URLS} from "./constants/base-urls";
56
import {API_VERSION} from "./constants/general";
67
import Directives from "./directives";
78
import SpeechRecognizer from "./speech-recognizer";
89
import System from "./system";
910

1011
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;
1217

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+
}
1732

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+
}
1941

20-
const speechRecognizer = new SpeechRecognizer(client);
21-
const directives = new Directives(client);
22-
const system = new System(client);
42+
console.log(`Access token found`);
2343

24-
const accessToken =
25-
"Atza|IwEBIFymtJ3Bb2MIW4SvBaYj7Gb9jSdrMoo1iFvfun3Ttp-oXxDXFy815mm1b2lksZZU1b3b2RWVUBslKbs9ef6R0UoEF21RcK3I0uMcmQ8yXEuXuy_wR_Z6wyHvAqcxFxPxdVzmqw145-eHPNu2gbk4nQ731JXrPYwdHs38IsLgnaeHCUHTj-DDF6Tq1AnTW7aoyNUfg0hwNIbtDKV--xQ0M5LYgMAe4cfdIz43n5dMbp1hMhe4ZPrwFeCttnkfheW04-sNmhAGjA5dgT11XSW8kAfsZ7RvMLt28DRJKcVAYxhvjjzT3h9yW-g2MOhXBrYyxezKHH1FpPmHQIBBTG_iCrPSwmOUEWfFrz7iEfjWH6rcdBVBpEyXghTaZkMI2d3ZdugQJ48-ES9QNk30QUn41q3IqOimCN5sKJ83KsRSY4aGaSqn8csuVXeZHROqvYNavHjA4vbjmtSbydrTYMarNHmBznULzb-CQGsSWIL4RYxA3mCRvDQjoQzGigAnS5rzHg0";
44+
this.init();
45+
}
2646

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);
3051
}
3152
}

src/directives.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,37 @@ import {API_VERSION} from "./constants/general";
66
export default class Directives {
77
constructor(private readonly client: http2.ClientHttp2Session) {}
88

9-
public connect(accessToken: string): void {
9+
public connect(accessToken: string): Promise<void> {
1010
const req = this.client.request({
1111
":path": `/${API_VERSION}/directives`,
1212
authorization: `Bearer ${accessToken}`,
1313
});
1414

15-
req.on("response", (headers, flags) => {
16-
// tslint:disable-next-line:forin
17-
for (const name in headers) {
18-
console.log(`${name}: ${headers[name]}`);
19-
}
20-
});
15+
return new Promise<void>((resolve) => {
16+
req.on("response", (headers, flags) => {
17+
console.log("response from directives");
2118

22-
req.setEncoding("utf8");
23-
let data = "";
24-
req.on("data", (chunk) => {
25-
data += chunk;
26-
});
27-
req.on("end", () => {
28-
console.log(`\n${data}`);
29-
});
19+
// tslint:disable-next-line:forin
20+
for (const name in headers) {
21+
console.log(`${name}: ${headers[name]}`);
22+
}
3023

31-
req.end();
24+
resolve();
25+
});
26+
27+
req.setEncoding("utf8");
28+
let data = "";
29+
req.on("data", (chunk) => {
30+
console.log("directive data");
31+
console.log(data);
32+
data += chunk;
33+
});
34+
req.on("end", () => {
35+
console.log("directive end");
36+
console.log(`\n${data}`);
37+
});
38+
39+
req.end();
40+
});
3241
}
3342
}

src/speech-recognizer.ts

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default class SpeechRecognizer {
1212
this.http2Utility = new Http2Utility();
1313
}
1414

15-
public recognize(accessToken: string, context: AVS.Context, readStream: ReadStream): void {
15+
public recognize(accessToken: string, context: AVS.Context, readStream: ReadStream): Promise<void> {
1616
const req = this.client.request({
1717
":method": "POST",
1818
":path": `/${API_VERSION}/events`,
@@ -29,25 +29,53 @@ export default class SpeechRecognizer {
2929
messageId: uuid(),
3030
},
3131
payload: {
32-
profile: "{{STRING}}",
33-
format: "{{STRING}}",
32+
profile: "NEAR_FIELD",
33+
format: "AUDIO_L16_RATE_16000_CHANNELS_1",
3434
initiator: {
35-
type: "{{STRING}}",
35+
type: "TAP",
3636
},
3737
},
3838
},
3939
});
4040

41-
req.write(metadata);
42-
console.log("sdfsd");
41+
return new Promise<void>((resolve) => {
42+
req.on("response", (headers, flags) => {
43+
console.log("response from speech");
4344

44-
readStream.on("data", (chunk: Buffer) => {
45-
console.log("data in");
46-
console.log(chunk.length);
45+
// tslint:disable-next-line:forin
46+
for (const name in headers) {
47+
console.log(`${name}: ${headers[name]}`);
48+
}
49+
});
4750

48-
const data = this.http2Utility.createBinaryAudioAttachment(chunk);
51+
req.write(metadata);
52+
console.log("speech recognizer");
4953

50-
req.write(data);
54+
readStream.on("data", (chunk: Buffer) => {
55+
console.log("data in");
56+
console.log(chunk.length);
57+
58+
const audio = this.http2Utility.createBinaryAudioAttachment(chunk);
59+
req.write(audio);
60+
});
61+
62+
req.setEncoding("utf8");
63+
let data = "";
64+
req.on("data", (chunk) => {
65+
data += chunk;
66+
});
67+
req.on("end", () => {
68+
console.log(`\n${data}`);
69+
});
70+
71+
setTimeout(() => {
72+
console.log("ending speech");
73+
readStream.removeListener("data", () => {
74+
req.write(this.http2Utility.createEnding());
75+
req.end();
76+
resolve();
77+
});
78+
}, 3000);
5179
});
5280
}
5381
}

src/system.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,14 @@ export default class System {
1111
this.http2Utility = new Http2Utility();
1212
}
1313

14-
public synchronizeState(accessToken: string, context: AVS.Context): void {
14+
public synchronizeState(accessToken: string, context: AVS.Context): Promise<void> {
1515
const req = this.client.request({
1616
":method": "POST",
1717
":path": `/${API_VERSION}/events`,
1818
authorization: `Bearer ${accessToken}`,
1919
"content-type": `multipart/form-data; boundary=${HTTP2_BOUNDARY}`,
2020
});
2121

22-
console.log("okie");
23-
24-
// req.on("response", (headers, flags) => {
25-
// for (const name in headers) {
26-
// console.log(`${name}: ${headers[name]}`);
27-
// }
28-
// });
29-
3022
const metadata = this.http2Utility.createMetadata<AVS.System.SynchronizeStateMetadata>({
3123
context: context,
3224
event: {
@@ -39,17 +31,28 @@ export default class System {
3931
},
4032
});
4133

42-
req.write(metadata);
43-
44-
req.setEncoding("utf8");
45-
let data = "";
46-
req.on("data", (chunk) => {
47-
data += chunk;
48-
});
49-
req.on("end", () => {
50-
console.log(`\n${data}`);
51-
// client.close();
34+
return new Promise<void>((resolve) => {
35+
console.log("System sync");
36+
37+
req.on("response", (headers, flags) => {
38+
// tslint:disable-next-line:forin
39+
for (const name in headers) {
40+
console.log(`${name}: ${headers[name]}`);
41+
}
42+
resolve();
43+
});
44+
45+
req.write(metadata);
46+
47+
req.setEncoding("utf8");
48+
let data = "";
49+
req.on("data", (chunk) => {
50+
data += chunk;
51+
});
52+
req.on("end", () => {
53+
console.log(`\n${data}`);
54+
});
55+
req.end();
5256
});
53-
req.end();
5457
}
5558
}

0 commit comments

Comments
 (0)