private void FB_StatusHandler(FeedBackResponse fbr)
        {
            //1. get messages list from FeedBack Responce
            List <messages> sms_fbr = new List <messages>();

            sms_fbr.AddRange(fbr.messages);
            int[] clientIdarr = sms_fbr.Select(x => x.clientId).ToArray();

            //2. Update records in database
            List <SmsLog> slxx = new List <SmsLog>();

            try
            {
                using (DB_A4A060_csEntities context = new DB_A4A060_csEntities())
                {
                    slxx = context.SmsLogs.Where(x => clientIdarr.Contains(x.id)).ToList();
                    //Update status and smscid from FeedBack Response;
                    foreach (var item in slxx)
                    {
                        int client_Id = sms_fbr.Find(x => x.clientId == item.id).clientId;
                        if (item.id == client_Id)
                        {
                            item.status = sms_fbr.Find(x => x.clientId == item.id).status;
                            item.smscId = sms_fbr.Find(x => x.clientId == item.id).smscId;
                        }
                    }
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                string x = ex.Message;
            }
        }
 // Write SMS log records
 public void WriteSmsLogs(List <SmsLog> smsLogs)
 {
     try
     {
         using (DB_A4A060_csEntities context = new DB_A4A060_csEntities())
         {
             context.SmsLogs.AddRange(smsLogs);
             context.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         throw;
     }
 }
        //FeedBack response after sending------------------------------------------
        public void FB_ResponseHandler(FeedBackResponse fbr)
        {
            //1. get messages list from FeedBack Responce
            List <messages> sms_fbr = new List <messages>();

            sms_fbr = fbr.messages;
            //sms_fbr.AddRange(fbr.messages);

            //2. Update records in database
            List <SmsLog> slxx = new List <SmsLog>();

            try
            {
                using (DB_A4A060_csEntities context = new DB_A4A060_csEntities())
                {
                    slxx = context.SmsLogs.Where(x => x.status == "prepared to sending" && x.smscId == "0").ToList();
                    //Update status and smscid from FeedBack Response;
                    foreach (var item in slxx)
                    {
                        int client_Id = (sms_fbr.Count > 1) ? sms_fbr.Where(x => x.clientId == item.id).SingleOrDefault().clientId : sms_fbr[0].clientId;
                        if (item.id == client_Id)
                        {
                            item.status = (sms_fbr.Count > 1) ? sms_fbr.Where(x => x.clientId == item.id).SingleOrDefault().status : sms_fbr[0].status;
                            item.smscId = (sms_fbr.Count > 1) ? sms_fbr.Where(x => x.clientId == item.id).SingleOrDefault().smscId : sms_fbr[0].smscId;
                        }
                    }
                    foreach (var item in slxx)
                    {
                        if (item.smscId.Trim() == "0")
                        {
                            item.status = slxx.Where(x => x.phonenumber == item.phonenumber && x.smscId.Trim() != "0").SingleOrDefault().status;
                            item.smscId = slxx.Where(x => x.phonenumber == item.phonenumber && x.smscId.Trim() != "0").SingleOrDefault().smscId.ToString();
                        }
                    }
                    //context.SmsLogs.AddRange(sl);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                string x = ex.Message;
            }
        }
        public bool WriteRecord(Log item)
        {
            bool result = false;

            try
            {
                using (DB_A4A060_csEntities context = new DB_A4A060_csEntities())
                {
                    item.datestamp = GetTorontoLocalDateTime();// DateTime.Now;
                    context.Logs.Add(item);
                    // executes the commands to implement the changes to the database
                    context.SaveChanges();
                    result = true;
                }
            }
            catch (Exception ex)
            {
                SendEmail("*****@*****.**", "*****@*****.**", ex.Message + ex.InnerException.Message, "LogWriter");
            }
            return(result);
        }
        private static List <SmsLog> SaveSMSinDatabase(List <SmsLog> alerts_ruled)
        {
            List <SmsLog> sl = new List <SmsLog>();

            foreach (var item in alerts_ruled)
            {
                SmsLog slitem = new SmsLog();
                slitem.@event        = item.@event;
                slitem.message       = item.message;
                slitem.phonenumber   = item.phonenumber;
                slitem.status        = "prepared to sending";
                slitem.datemark      = item.datemark;
                slitem.smscId        = "0";
                slitem.AspNetUser_Id = item.AspNetUser_Id;
                sl.Add(slitem);
                slitem = null;
            }
            //update database
            try
            {
                using (DB_A4A060_csEntities context = new DB_A4A060_csEntities())
                {
                    context.SmsLogs.AddRange(sl);
                    context.SaveChanges();
                    int rowsnumber = sl.Count;
                    sl.Clear();
                    sl = context.SmsLogs.OrderByDescending(x => x.id).Where(x => x.status == "prepared to sending" && x.smscId == "0").Take(rowsnumber).ToList();
                }
            }
            catch (Exception ex)
            {
                var x = ex.Message;
                sl = null;
            }
            return(sl);
        }