Example #1
0
        private async Task <PlayResult> InternalPlayAsync(string controlID, List <CallMedia> play)
        {
            await API.API.SetupAsync();

            PlayResult resultPlay = new PlayResult();
            TaskCompletionSource <bool> tcsCompletion = new TaskCompletionSource <bool>();

            // Hook callbacks temporarily to catch required events
            PlayFinishedCallback finishedCallback = (a, c, e, p) =>
            {
                resultPlay.Event = new Event(e.EventType, JObject.FromObject(p));
                tcsCompletion.SetResult(true);
            };
            PlayErrorCallback errorCallback = (a, c, e, p) =>
            {
                resultPlay.Event = new Event(e.EventType, JObject.FromObject(p));
                tcsCompletion.SetResult(false);
            };

            OnPlayFinished += finishedCallback;
            OnPlayError    += errorCallback;

            try
            {
                Task <LL_PlayResult> taskLLPlay = mAPI.LL_PlayAsync(new LL_PlayParams()
                {
                    NodeID    = mNodeID,
                    CallID    = mID,
                    ControlID = controlID,
                    Play      = play,
                });

                // The use of await rethrows exceptions from the task
                LL_PlayResult resultLLPlay = await taskLLPlay;
                if (resultLLPlay.Code == "200")
                {
                    mLogger.LogDebug("Play {0} for call {1} waiting for completion events", controlID, ID);

                    resultPlay.Successful = await tcsCompletion.Task;

                    mLogger.LogDebug("Play {0} for call {1} {2}", controlID, ID, resultPlay.Successful ? "successful" : "unsuccessful");
                }
            }
            catch (Exception exc)
            {
                mLogger.LogError(exc, "Play {0} for call {1} exception", controlID, ID);
            }

            // Unhook temporary callbacks
            OnPlayFinished -= finishedCallback;
            OnPlayError    -= errorCallback;

            return(resultPlay);
        }
Example #2
0
        private async Task <PromptResult> InternalPromptAsync(string controlID, List <CallMedia> play, CallCollect collect)
        {
            await API.API.SetupAsync();

            PromptResult resultPrompt = new PromptResult();
            TaskCompletionSource <bool> tcsCompletion = new TaskCompletionSource <bool>();

            // Hook callbacks temporarily to catch required events
            PlayErrorCallback errorCallback = (a, c, e, p) =>
            {
                resultPrompt.Event = new Event(e.EventType, JObject.FromObject(p));
                tcsCompletion.SetResult(false);
            };
            PromptCallback promptCallback = (a, c, e, p) =>
            {
                resultPrompt.Event = new Event(e.EventType, JObject.FromObject(p));
                resultPrompt.Type  = p.Result.Type;

                switch (resultPrompt.Type)
                {
                case CallCollectType.digit:
                {
                    CallingEventParams.CollectParams.ResultParams.DigitParams digitParams = p.Result.ParametersAs <CallingEventParams.CollectParams.ResultParams.DigitParams>();
                    resultPrompt.Result     = digitParams.Digits;
                    resultPrompt.Terminator = digitParams.Terminator;

                    tcsCompletion.SetResult(true);
                    break;
                }

                case CallCollectType.speech:
                {
                    CallingEventParams.CollectParams.ResultParams.SpeechParams speechParams = p.Result.ParametersAs <CallingEventParams.CollectParams.ResultParams.SpeechParams>();
                    resultPrompt.Result     = speechParams.Text;
                    resultPrompt.Confidence = speechParams.Confidence;

                    tcsCompletion.SetResult(true);
                    break;
                }

                default:
                {
                    tcsCompletion.SetResult(false);
                    break;
                }
                }
            };

            OnPlayError += errorCallback;
            OnPrompt    += promptCallback;

            try
            {
                Task <LL_PlayAndCollectResult> taskLLPlayAndCollect = mAPI.LL_PlayAndCollectAsync(new LL_PlayAndCollectParams()
                {
                    NodeID    = mNodeID,
                    CallID    = mID,
                    ControlID = controlID,
                    Play      = play,
                    Collect   = collect,
                });

                // The use of await rethrows exceptions from the task
                LL_PlayAndCollectResult resultLLPlayAndCollect = await taskLLPlayAndCollect;
                if (resultLLPlayAndCollect.Code == "200")
                {
                    mLogger.LogDebug("Prompt {0} for call {1} waiting for completion events", controlID, ID);

                    resultPrompt.Successful = await tcsCompletion.Task;

                    mLogger.LogDebug("Prompt {0} for call {1} {2}", controlID, ID, resultPrompt.Successful ? "successful" : "unsuccessful");
                }
            }
            catch (Exception exc)
            {
                mLogger.LogError(exc, "Prompt {0} for call {1} exception", controlID, ID);
            }

            // Unhook temporary callbacks
            OnPlayError -= errorCallback;
            OnPrompt    -= promptCallback;

            return(resultPrompt);
        }