Пример #1
0
        public async Task ImportTrack(string file)
        {
            RecurringJob.RemoveIfExists("ProcessPings");

            XmlDocument gpxDoc = new XmlDocument();

            gpxDoc.Load(file);

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(gpxDoc.NameTable);

            nsmgr.AddNamespace("x", "http://www.topografix.com/GPX/1/1");
            XmlNodeList trksegments = gpxDoc.SelectNodes("//x:trkseg", nsmgr);

            var resetFrom = DateTime.Now;

            foreach (XmlNode trkseg in trksegments)
            {
                var pings = trkseg.ChildNodes;
                if (pings.Count <= 1)
                {
                    continue;
                }

                var firstPing = GetDateFromGpx(pings[0]["time"].InnerText);
                var lastPing  = GetDateFromGpx(pings[pings.Count - 1]["time"].InnerText);
                if (firstPing < resetFrom)
                {
                    resetFrom = firstPing;
                }

                await pingRepository.DeleteBetweenDates(firstPing, lastPing);

                foreach (XmlNode pingData in pings)
                {
                    var ping = new Ping()
                    {
                        Latitude  = double.Parse(pingData.Attributes["lat"].Value),
                        Longitude = double.Parse(pingData.Attributes["lon"].Value),
                        Accuracy  = 5,
                        Time      = GetDateFromGpx(pingData["time"].InnerText)
                    };

                    pingRepository.Insert(ping);
                }

                await pingRepository.SaveAsync();
            }
            cache.ClearCache();
            await Reset(resetFrom, DateTime.Now);

            // BackgroundJob.Enqueue<ProcessPings>(x => x.Process());
        }