Example #1
0
        private static void CallSetup(
            string tag,
            string toNumber,
            bool shouldRun,
            ManualResetEventSlim waitHandle,
            Func <CallingEventParams.DetectParams, bool> isDetectValid,
            Action setSuccessfulDetection,
            CallDetect detect)
        {
            if (!shouldRun)
            {
                waitHandle.Set();
                sDetect = null;
                return;
            }

            Logger.LogInformation("[{0}] Beginning setup for call to {1}", tag, toNumber);
            PhoneCall call = sCallingAPI.NewPhoneCall(toNumber, sCallFromNumber);

            Logger.LogInformation("[{0}] Call created, associating events", tag);
            call.OnDetectUpdate += (CallingAPI api, Call detectedCall, CallingEventParams detectEventParams, CallingEventParams.DetectParams detectParams) =>
            {
                if (detectParams.Detect.Parameters.Event == "READY")
                {
                    return;
                }
                if (detectParams.Detect.Parameters.Event == "finished")
                {
                    if (isDetectValid(detectParams))
                    {
                        Logger.LogInformation("[{0}] Completed successfully", tag);
                        setSuccessfulDetection();
                    }
                    // Not necessarily unsuccessful in the else case, e.g.
                    // Human detection gives an event of HUMAN under machine detection, but it can't be "finished" and "HUMAN" at the same time
                    sDetect = null;
                    return;
                }

                Logger.LogInformation("[{0}] OnDetect with ID: {1}, {2} for {3}", tag, detectedCall.ID, detectParams.Detect.Type, detectParams.ControlID);
                if (isDetectValid(detectParams))
                {
                    setSuccessfulDetection();
                    Task.Run(() =>
                    {
                        sDetect.Stop();
                        sDetect = null;
                    });
                    Logger.LogInformation("[{0}] Completed successfully", tag);
                }
                else
                {
                    Logger.LogError("[{0}] Unsuccessful", tag);
                }
            };
            Logger.LogInformation("[{0}] OnDetect associated", tag);
            call.OnEnded += (CallingAPI api, Call endedCall, CallingEventParams stateEventParams, CallingEventParams.StateParams stateParams) =>
            {
                Logger.LogInformation("[{0}] OnEnded with ID: {1}", tag, endedCall.ID);
                sDetect = null;
                waitHandle.Set();
                Logger.LogInformation("[{0}] OnEnded complete", tag);
            };
            Logger.LogInformation("[{0}] OnEnded associated", tag);
            call.OnAnswered += (CallingAPI api, Call answeredCall, CallingEventParams answerEventParams, CallingEventParams.StateParams stateParams) =>
            {
                Logger.LogInformation("[{0}] OnAnswered with ID: {1}", tag, answeredCall.ID);

                Task.Run(() =>
                {
                    try
                    {
                        Logger.LogInformation("[{0}] Performing detect", tag);
                        sDetect = answeredCall.DetectAsync(detect);
                        Logger.LogInformation("[{0}] Detect performed", tag);
                    }
                    catch (Exception exc)
                    {
                        Logger.LogError(exc, $"[{tag}] call.Detect failed");
                        waitHandle.Set();
                        sDetect = null;
                        return;
                    }
                });
            };
            Logger.LogInformation("[{0}] OnAnswered associated", tag);
            call.OnConnectStateChange += (CallingAPI api, Call connectStateChangeCall, CallingEventParams connectStateChangeEventParams, CallingEventParams.ConnectParams connectStateChangeParams) =>
            {
                Logger.LogInformation("[{0}] OnConnectStateChange: {1}", tag, connectStateChangeParams.State);
            };
            Logger.LogInformation("[{0}] OnConnectStateChange associated", tag);
            call.OnReceiveStateChange += (CallingAPI api, Call receiveStateChangeCall, CallingEventParams receiveStateChangeEventParams, CallingEventParams.ReceiveParams receiveStateChangeParams) =>
            {
                Logger.LogInformation("[{0}] OnReceiveStateChange: {1}", tag, receiveStateChangeParams.CallState);
            };
            Logger.LogInformation("[{0}] OnReceiveStateChange associated", tag);
            call.OnStateChange += (CallingAPI api, Call stateChangeCall, CallingEventParams stateChangeEventParams, CallingEventParams.StateParams stateChangeParams) =>
            {
                Logger.LogInformation("[{0}] OnStateChange: {1}", tag, stateChangeParams.CallState);
            };
            Logger.LogInformation("[{0}] OnStateChange associated", tag);

            try
            {
                Logger.LogInformation("[{0}] Executing call", tag);
                var dialAction = call.Dial();
                Logger.LogInformation("[{0}] Call executed", tag);
            }
            catch (Exception exc)
            {
                Logger.LogError(exc, $"[{tag}] call.DialAsync failed");
                waitHandle.Set();
                sDetect = null;
                return;
            }
        }
Example #2
0
        private static void CallMachineReady(string toNumber)
        {
            const string tag = "Machine Ready";

            Logger.LogInformation("[{0}] Beginning setup for call to {1}", tag, toNumber);
            PhoneCall call = sCallingAPI.NewPhoneCall(toNumber, sCallFromNumber);

            Logger.LogInformation("[{0}] Call created, associating events", tag);
            call.OnDetectUpdate += (CallingAPI api, Call detectedCall, CallingEventParams detectEventParams, CallingEventParams.DetectParams detectParams) =>
            {
                if (detectParams.Detect.Parameters.Event == "READY")
                {
                    Logger.LogInformation("[{0}] Completed successfully", tag);
                    sMachineReadySuccessful = true;
                    sMachineReadyCompleted.Set();
                    return;
                }
                if (detectParams.Detect.Parameters.Event == "MACHINE")
                {
                    // We do expect one of these since usually MACHINE occurs before READY
                    return;
                }

                Logger.LogInformation("[{0}] OnDetect with ID: {1}, {2} for {3}", tag, detectedCall.ID, detectParams.Detect.Type, detectParams.ControlID);
                Logger.LogError("[{0}] Unsuccessful", tag);
            };
            Logger.LogInformation("[{0}] OnDetect associated", tag);

            call.OnEnded += (CallingAPI api, Call endedCall, CallingEventParams stateEventParams, CallingEventParams.StateParams stateParams) =>
            {
                Logger.LogInformation("[{0}] OnEnded with ID: {1}", tag, endedCall.ID);
                sDetect = null;
                sMachineReadyCompleted.Set();
                Logger.LogInformation("[{0}] OnEnded complete", tag);
            };
            Logger.LogInformation("[{0}] OnEnded associated", tag);

            call.OnAnswered += (CallingAPI api, Call answeredCall, CallingEventParams answerEventParams, CallingEventParams.StateParams stateParams) =>
            {
                Logger.LogInformation("[{0}] OnAnswered with ID: {1}", tag, answeredCall.ID);

                Task.Run(() =>
                {
                    try
                    {
                        Logger.LogInformation("[{0}] Performing detect", tag);
                        var detectResult = answeredCall.DetectAnsweringMachine(waitForBeep: true);
                        Logger.LogInformation("[{0}] Detect performed", tag);
                    }
                    catch (Exception exc)
                    {
                        Logger.LogError(exc, $"[{tag}] call.Detect failed");
                        sMachineReadyCompleted.Set();
                        sDetect = null;
                        return;
                    }
                });
            };
            Logger.LogInformation("[{0}] OnAnswered associated", tag);

            call.OnConnectStateChange += (CallingAPI api, Call connectStateChangeCall, CallingEventParams connectStateChangeEventParams, CallingEventParams.ConnectParams connectStateChangeParams) =>
            {
                Logger.LogInformation("[{0}] OnConnectStateChange: {1}", tag, connectStateChangeParams.State);
            };
            Logger.LogInformation("[{0}] OnConnectStateChange associated", tag);

            call.OnReceiveStateChange += (CallingAPI api, Call receiveStateChangeCall, CallingEventParams receiveStateChangeEventParams, CallingEventParams.ReceiveParams receiveStateChangeParams) =>
            {
                Logger.LogInformation("[{0}] OnReceiveStateChange: {1}", tag, receiveStateChangeParams.CallState);
            };
            Logger.LogInformation("[{0}] OnReceiveStateChange associated", tag);

            call.OnStateChange += (CallingAPI api, Call stateChangeCall, CallingEventParams stateChangeEventParams, CallingEventParams.StateParams stateChangeParams) =>
            {
                Logger.LogInformation("[{0}] OnStateChange: {1}", tag, stateChangeParams.CallState);
            };
            Logger.LogInformation("[{0}] OnStateChange associated", tag);

            try
            {
                Logger.LogInformation("[{0}] Executing call", tag);
                var dialAction = call.Dial();
                Logger.LogInformation("[{0}] Call executed", tag);
            }
            catch (Exception exc)
            {
                Logger.LogError(exc, $"[{tag}] call.DialAsync failed");
                sMachineReadyCompleted.Set();
                sDetect = null;
                return;
            }
        }