private void timerInsertDB_Tick(object sender, EventArgs e)
        {
            try
            {
                //string alltext = System.IO.File.ReadAllText(jsonFilePath);
                List <FlightPos> ObjOrderList = new List <FlightPos>();
                using (RedisClient client1 = new RedisClient("localhost", 6379))
                {
                    IRedisTypedClient <FlightPos> pos = client1.As <FlightPos>();
                    ObjOrderList = pos.Lists[redis_flight_time_filtered].GetAll();

                    var allPos = new List <FlightPos>();

                    var listICAO = ObjOrderList.GroupBy(x => x.ICAO).Select(x => x.First()).ToList();
                    foreach (var item in listICAO)
                    {
                        var flightPosByICAO         = ObjOrderList.Where(m => m.ICAO == item.ICAO).ToList();
                        var flightPosByICAOFiltered = GetListFlightByTime(flightPosByICAO);
                        allPos.AddRange(flightPosByICAOFiltered);
                    }
                    using (var db = new FlightDetailEntities())
                    {
                        db.FlightPos.AddRange(allPos);
                        db.SaveChanges();
                        pos.Lists[redis_flight_time_filtered].RemoveAll();
                    }
                }
            }
            catch
            {
            }
        }
 public void InsertOrUpdateCallsign(string icao, string callsign)
 {
     using (var db = new FlightDetailEntities())
     {
         var temp = db.FlightIdentities.Where(m => m.ICAO == icao).FirstOrDefault();
         if (temp != null)
         {
             if (!String.IsNullOrEmpty(temp.Callsign))
             {
                 temp.Callsign = callsign;
                 db.SaveChanges();
             }
         }
         else
         {
             var ident = new FlightIdentity();
             ident.Callsign = callsign;
             ident.ICAO     = icao;
             db.FlightIdentities.Add(ident);
             db.SaveChanges();
         }
     }
 }
        private void timer1_Tick(object sender, EventArgs e)
        {
            FlightPos currentObject = null;
            var       listPosObject = new List <FlightPos>();
            var       fileNameInput = Directory.GetFiles(txtSourceFolder.Text).Select(System.IO.Path.GetFullPath).OrderBy(d => new FileInfo(d).CreationTime).ToList();

            if (fileNameInput != null && fileNameInput.Count > 0)
            {
                foreach (var currentItem in fileNameInput)
                {
                    try
                    {
                        if (count == fileNameInput.Count - 1)
                        {
                            count = 0;
                            break;
                        }

                        listObject = excel.ProcessTxtFile(currentItem);

                        if (listObject != null)
                        {
                            foreach (var item in listObject)
                            {
                                try
                                {
                                    var trans = item.transmissionType;
                                    if (trans == "1")
                                    {
                                        currentObject = null;
                                        if (!String.IsNullOrEmpty(item.callsign))
                                        {
                                            InsertOrUpdateCallsign(item.ICAO, item.callsign);
                                        }
                                    }
                                    else if (trans == "3")
                                    {
                                        currentObject                  = new FlightPos();
                                        currentObject.MessageType      = item.messageType;
                                        currentObject.TransmissionType = item.transmissionType;
                                        currentObject.ICAO             = item.ICAO;
                                        currentObject.DateGenerate     = item.dategenerate;
                                        currentObject.DateLog          = item.datelog;
                                        currentObject.Altitude         = item.altitude;
                                        currentObject.Latitude         = item.latitude;
                                        currentObject.Longitude        = item.longitude;
                                        //currentTransmissionType = trans;
                                    }
                                    else if (trans == "4")
                                    {
                                        if (currentObject != null)
                                        {
                                            currentObject.Speed        = item.speed;
                                            currentObject.Track        = item.track;
                                            currentObject.VerticalRate = item.verticalrate;

                                            if (!String.IsNullOrEmpty(currentObject.Altitude))
                                            {
                                                double altitude;
                                                altitude = Convert.ToDouble(currentObject.Altitude) * 0.3048;
                                                currentObject.Altitude = Math.Round(altitude, 2).ToString();
                                            }

                                            if (!String.IsNullOrEmpty(currentObject.Altitude))
                                            {
                                                double speed;
                                                speed = Convert.ToDouble(currentObject.Speed) * 1.6093;
                                                currentObject.Speed = Math.Round(speed, 2).ToString();
                                            }

                                            if (!String.IsNullOrEmpty(currentObject.Altitude))
                                            {
                                                double verticalrate;
                                                verticalrate = Convert.ToDouble(currentObject.VerticalRate) * 0.3048;
                                                currentObject.VerticalRate = Math.Round(verticalrate, 2).ToString();
                                            }

                                            listPosObject.Add(currentObject);

                                            currentObject = null;
                                        }
                                    }
                                    // transmissionType = 5
                                    else
                                    {
                                        currentObject = null;
                                        if (!String.IsNullOrEmpty(item.callsign))
                                        {
                                            InsertOrUpdateCallsign(item.ICAO, item.callsign);
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.Message);
                                }
                            }
                        }

                        count++;
                    }
                    catch
                    {
                    }
                }
            }

            try
            {
                if (listPosObject.Count > 0)
                {
                    var tempList = listPosObject.OrderByDescending(m => m.DateGenerate).GroupBy(x => x.ICAO).Select(x => x.First()).ToList();
                    var allPos   = new List <FlightPos>();

                    var listICAO = tempList.GroupBy(x => x.ICAO).Select(x => x.First()).ToList();
                    foreach (var item in listICAO)
                    {
                        var flightPosByICAO         = tempList.Where(m => m.ICAO == item.ICAO).ToList();
                        var flightPosByICAOFiltered = GetListFlightByTime(flightPosByICAO);
                        allPos.AddRange(flightPosByICAOFiltered);
                    }
                    using (var db = new FlightDetailEntities())
                    {
                        db.FlightPos.AddRange(allPos);
                        db.SaveChanges();
                        //pos.Lists[redis_flight_time_filtered].RemoveAll();
                    }
                }
            }
            catch
            {
            }



            foreach (var currentItem in fileNameInput)
            {
                if (System.IO.File.Exists(currentItem))
                {
                    try
                    {
                        System.IO.File.Delete(currentItem);
                        //System.IO.File.Move(currentItem, desFile);
                    }
                    catch
                    {
                    }
                }
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();
        }