/// <summary>
        /// Method for simulating a PhoneCall. this method choose randomly an answer call and retrieve the information.
        /// </summary>
        /// <param name="e">Object: CallSimulatorRequest with Call data</param>
        /// <returns>Object: CallSimulatorResponse with Hangout Call data</returns>
        private CallSimulatorResponse Phonecall(CallSimulatorRequest e)
        {
            try
            {
                var response = new CallSimulatorResponse();
                Dictionary <string, int> answer = new Dictionary <string, int>();
                int h = 0;
                answer.Add("Accepted", 0);
                answer.Add("Rejected", 1);
                answer.Add("On_Hold", 2);
                answer.Add("UnReach", 3);

                Random c = new Random();
                h = c.Next(answer.Min(a => a.Value), answer.Max(a => a.Value));
                var endc = e.startCall.AddMinutes(c.Next(1, 20));
                switch (h)
                {
                case 0:
                    response.startCall  = e.startCall;
                    response.endCall    = endc;
                    response.answerType = h;
                    response.answerDesc = answer.FirstOrDefault(k => k.Value == h).Key;
                    break;

                case 1:
                    response.startCall  = e.startCall;
                    response.endCall    = e.startCall;
                    response.answerType = h;
                    response.answerDesc = answer.FirstOrDefault(k => k.Value == h).Key;
                    break;

                case 2:
                    response.startCall  = e.startCall;
                    response.endCall    = endc;
                    response.answerType = h;
                    response.answerDesc = answer.FirstOrDefault(k => k.Value == h).Key;
                    break;

                case 3:
                    response.startCall  = e.startCall;
                    response.endCall    = e.startCall;
                    response.answerType = h;
                    response.answerDesc = answer.FirstOrDefault(k => k.Value == h).Key;
                    break;
                }
                return(response);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 /// <summary>
 /// Call simulator Method
 /// </summary>
 /// <param name="call">Object: CallSimulatorRequest with Call data</param>
 /// <returns>Object: CallSimulatorResponse with Hangout Call data</returns>
 public CallSimulatorResponse  StartPhoneCall(CallSimulatorRequest call)
 {
     try
     {
         var response = new CallSimulatorResponse();
         response = Phonecall(call);
         return(response);
     }
     catch (Exception ex)
     {
         throw new Exception("Cannot connect the phone call with the destination phone number. Exception: " + ex.Message);
     }
 }
 public CallSimulatorResponse startcall(CallSimulatorRequest e)
 {
     try
     {
         Phonecall(e);
         var response = new CallSimulatorResponse();
         response = Phonecall(e);
         return(response);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Execute an incoming Call Request. Calculating:
        /// 1.Call Duration.
        /// 2.Call Cost.
        /// 3.Minutes Used.
        /// 4.Minutes Balance.
        /// </summary>
        /// <param name="data">Stream with Json Data; Object: Call Entity</param>
        /// <returns>Stream with Json Data; Object: Response Entity</returns>
        public Stream startPhoneCall(Stream data)
        {
            try
            {
                decimal callLast = 0;
                call = SerializationHelpers.DeserializeJson <Call>(data);

                cus = dataAccess.getCustomerPerPhone(call.PhoneNumber);

                cusPhone = dataAccess.getBalance(cus.Id, cus.PhoneNumber);
                if (cusPhone != null)
                {
                    if (!transValidations.validateMinLeft(cusPhone.MinuteBalance))
                    {
                        resp.idResponse = 0;
                        resp.response   = "Customer does not have minutes for making this call.";
                        resp.exception  = null;
                        return(SerializationHelpers.GenerateStreamFromString(SerializationHelpers.SerializeJson <Response>(resp)));
                    }
                }
                else
                {
                    resp.idResponse = 0;
                    resp.response   = "Customer does not have minutes for making this call.";
                    resp.exception  = null;
                    return(SerializationHelpers.GenerateStreamFromString(SerializationHelpers.SerializeJson <Response>(resp)));
                }

                Price p = dataAccess.getPrice(Convert.ToInt32(2));

                //Call simulation
                var makecall = new CallSimulatorMock();
                var callreq  = new CallSimulatorRequest {
                    toPhoneNumber = cus.PhoneNumber, fromPhoneNumber = call.DestinationNumber, startCall = DateTime.Now, minutesLet = cusPhone.MinuteBalance
                };
                var callresp = new CallSimulatorResponse();
                callresp = makecall.StartPhoneCall(callreq);

                TimeSpan duCall = BussinessOps.GetDurationCall(callresp);
                if (Convert.ToDecimal(duCall.TotalSeconds) >= (cusPhone.MinuteBalance * 60))
                {
                    callLast = cusPhone.MinuteBalance;
                    cusPhone.MinuteBalance = 0;
                    cusPhone.MinutesUsed   = callLast;
                    dataAccess.updBalance(cusPhone);
                }
                else
                {
                    callLast = Convert.ToDecimal(duCall.TotalSeconds);
                    cusPhone.MinuteBalance -= callLast / 60;
                    cusPhone.MinutesUsed   += callLast / 60;
                    dataAccess.updBalance(cusPhone);
                }

                var callCost = BussinessOps.GetCallCost(p.Prices, callLast);
                var c        = new Call {
                    Id = cus.Id, PhoneNumber = cus.PhoneNumber, DestinationNumber = call.DestinationNumber, InitialDatetime = callresp.startCall, FinalDatetime = callresp.endCall, Duration = callLast, Cost = callCost, State = callresp.answerType
                };
                var res = dataAccess.initPhoneCall(c);

                resp.idResponse = 0;
                resp.response   = "From Phone Number: " + cus.PhoneNumber + " \nTo from Phone Number: " + call.DestinationNumber + " \nStart Call DateTime: " + c.InitialDatetime.ToString() + " \nEndCall DateTime : " + c.FinalDatetime.ToString() +
                                  " \nCall Duration: " + c.Duration.ToString() + "sec. \n Call Cost: " + c.Cost + " \n Call State: " + callresp.answerDesc;
                resp.exception = null;
                return(SerializationHelpers.GenerateStreamFromString(SerializationHelpers.SerializeJson <Response>(resp)));
            }
            catch (Exception ex)
            {
                resp.idResponse = 400;
                resp.response   = "Transaction: PhoneCall; Customer does not Exist.";
                resp.exception  = ex.Message;
                return(SerializationHelpers.GenerateStreamFromString(SerializationHelpers.SerializeJson <Response>(resp)));
            }
        }
 /// <summary>
 /// Calculate the duration of the Phone Call
 /// </summary>
 /// <param name="callresp">Call Information</param>
 /// <returns>Duration Call</returns>
 public static TimeSpan GetDurationCall(CallSimulatorResponse callresp)
 {
     return(callresp.endCall - callresp.startCall);
 }