major refactoring

This commit is contained in:
Dave Horton
2020-01-25 11:47:33 -05:00
parent 621ea8c0f5
commit 4a1ea4e091
25 changed files with 947 additions and 933 deletions

View File

@@ -9,7 +9,7 @@ class TaskGather extends Task {
this.preconditions = TaskPreconditions.Endpoint;
[
'action', 'finishOnKey', 'hints', 'input', 'language', 'method', 'numDigits',
'action', 'finishOnKey', 'hints', 'input', 'method', 'numDigits',
'partialResultCallback', 'partialResultCallbackMethod', 'profanityFilter',
'speechTimeout', 'timeout', 'say'
].forEach((k) => this[k] = this.data[k]);
@@ -17,13 +17,17 @@ class TaskGather extends Task {
this.partialResultCallbackMethod = this.partialResultCallbackMethod || 'POST';
this.method = this.method || 'POST';
this.timeout = (this.timeout || 5) * 1000;
this.language = this.language || 'en-US';
this.digitBuffer = '';
//this._earlyMedia = this.data.earlyMedia === true;
if (this.say) {
this.sayTask = makeTask(this.logger, {say: this.say});
this.interim = this.partialResultCallback;
if (this.data.recognizer) {
this.language = this.data.recognizer.language || 'en-US';
this.vendor = this.data.recognizer.vendor;
}
this.digitBuffer = '';
this._earlyMedia = this.data.earlyMedia === true;
if (this.say) this.sayTask = makeTask(this.logger, {say: this.say}, this);
}
get name() { return TaskName.Gather; }
@@ -34,15 +38,14 @@ class TaskGather extends Task {
}
async exec(cs, ep) {
super.exec(cs);
this.ep = ep;
this.actionHook = cs.actionHook;
this.taskInProgress = true;
try {
if (this.sayTask) {
this.sayTask.exec(cs, ep); // kicked off, _not_ waiting for it to complete
this.sayTask.on('playDone', (err) => {
if (this.taskInProgress) this._startTimer();
if (!this.killed) this._startTimer();
});
}
else this._startTimer();
@@ -56,11 +59,10 @@ class TaskGather extends Task {
ep.on('dtmf', this._onDtmf.bind(this, ep));
}
await this._waitForCompletion();
await this.awaitTaskDone();
} catch (err) {
this.logger.error(err, 'TaskGather:exec error');
}
this.taskInProgress = false;
ep.removeCustomEventListener(TranscriptionEvents.Transcription);
ep.removeCustomEventListener(TranscriptionEvents.EndOfUtterance);
}
@@ -71,10 +73,6 @@ class TaskGather extends Task {
this._resolve('killed');
}
async _waitForCompletion() {
return new Promise((resolve) => this.resolver = resolve);
}
_onDtmf(ep, evt) {
this.logger.debug(evt, 'TaskGather:_onDtmf');
if (evt.dtmf === this.finishOnKey) this._resolve('dtmf-terminator-key');
@@ -89,7 +87,7 @@ class TaskGather extends Task {
const opts = {
GOOGLE_SPEECH_USE_ENHANCED: true,
GOOGLE_SPEECH_SINGLE_UTTERANCE: true,
GOOGLE_SPEECH_MODEL: 'phone_call'
GOOGLE_SPEECH_MODEL: 'command_and_search'
};
if (this.hints) {
Object.assign(opts, {'GOOGLE_SPEECH_HINTS': this.hints.join(',')});
@@ -107,7 +105,7 @@ class TaskGather extends Task {
_startTranscribing(ep) {
ep.startTranscription({
interim: this.partialResultCallback ? true : false,
language: this.language
language: this.language || this.callSession.speechRecognizerLanguage
}).catch((err) => this.logger.error(err, 'TaskGather:_startTranscribing error'));
}
@@ -124,46 +122,30 @@ class TaskGather extends Task {
}
_killAudio() {
if (this.sayTask) {
this.sayTask.kill();
this.sayTask = null;
}
this.sayTask.kill();
}
_onTranscription(ep, evt) {
this.logger.debug(evt, 'TaskGather:_onTranscription');
if (evt.is_final) {
ep.removeCustomEventListener(TranscriptionEvents.Transcription);
ep.removeCustomEventListener(TranscriptionEvents.EndOfUtterance);
this._resolve('speech', evt);
}
else if (this.partialResultCallback) {
this.actionHook(this.partialResultCallback, 'POST', {
Speech: evt
});
}
if (evt.is_final) this._resolve('speech', evt);
else if (this.partialResultCallback) this.notifyHook(this.partialResultCallback, 'POST', null, {speech: evt});
}
_onEndOfUtterance(ep, evt) {
this.logger.info(evt, 'TaskGather:_onEndOfUtterance');
this._startTranscribing(ep);
}
_resolve(reason, evt) {
async _resolve(reason, evt) {
this.logger.debug(`TaskGather:resolve with reason ${reason}`);
assert(this.resolver);
if (reason.startsWith('dtmf')) {
this.actionHook(this.action, this.method, {
Digits: this.digitBuffer
});
this.performAction(this.method, null, {digits: this.digitBuffer});
}
else if (reason.startsWith('speech')) {
this.actionHook(this.action, this.method, {
Speech: evt
});
this.performAction(this.method, null, {speech: evt});
}
this._clearTimer();
this.resolver();
this.notifyTaskDone();
}
}