Exemplo n.º 1
0
        private void DeviceWptGPXCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            _success = false;
            switch (e.Action)
            {
            case NotifyCollectionChangedAction.Add:
            {
                int       newIndex  = e.NewStartingIndex;
                DeviceGPX newWPTGPX = DeviceGPXCollection[newIndex];
                if (DeviceWaypointGPXes.Add(newWPTGPX))
                {
                    CurrentEntity = newWPTGPX;
                    _success      = true;
                }
            }
            break;

            case NotifyCollectionChangedAction.Remove:
            {
                List <DeviceGPX> tempListOfRemovedItems = e.OldItems.OfType <DeviceGPX>().ToList();
                DeviceWaypointGPXes.Delete(tempListOfRemovedItems[0].RowID);
            }
            break;

            case NotifyCollectionChangedAction.Replace:
            {
                List <DeviceGPX> tempList = e.NewItems.OfType <DeviceGPX>().ToList();
                _success = DeviceWaypointGPXes.Update(tempList[0]);               // As the IDs are unique, only one row will be effected hence first index only
            }
            break;
            }
        }
        public bool Update1(DeviceGPX gpx)
        {
            bool success = false;

            using (OleDbConnection conn = new OleDbConnection(Global.ConnectionString))
            {
                conn.Open();
                var sql = $@"Update device_gpx set
                                DeviceID= '{gpx.GPS.DeviceID}',
                                gpx_xml = '{gpx.GPX}',
                                FileName = '{gpx.Filename}',
                                md5='{gpx.MD5}',
                                DateModified='{DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss")}',
                                gpx_type = '{gpx.GPXType}',
                                time_range_start = '{gpx.TimeRangeStart}',
                                time_range_end = '{gpx.TimeRangeEnd}',
                                TimerInterval = {gpx.TimerInterval}
                            WHERE RowID = {gpx.RowID}";
                using (OleDbCommand update = new OleDbCommand(sql, conn))
                {
                    try
                    {
                        success = update.ExecuteNonQuery() > 0;
                    }
                    catch (OleDbException dbex)
                    {
                    }
                    catch (Exception ex)
                    {
                        Logger.Log(ex);
                    }
                }
            }
            return(success);
        }
        public bool Add(DeviceGPX gpx)
        {
            bool success = false;

            using (OleDbConnection conn = new OleDbConnection(Global.ConnectionString))
            {
                conn.Open();
                var sql = $@"Insert into device_gpx (DeviceID,FileName,gpx_xml,RowID,md5,DateAdded,DateModified,gpx_type,time_range_start,time_range_end,TimerInterval)
                           Values (
                                    '{gpx.GPS.DeviceID}',
                                    '{gpx.Filename}', 
                                    '{gpx.GPX}',
                                     {gpx.RowID},
                                    '{gpx.MD5}',
                                    '{DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss")}',
                                    '{DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss")}',
                                    '{gpx.GPXType}',
                                    '{gpx.TimeRangeStart}',
                                    '{gpx.TimeRangeEnd}',
                                     {gpx.TimerInterval}
                                  )";
                using (OleDbCommand update = new OleDbCommand(sql, conn))
                {
                    success = update.ExecuteNonQuery() > 0;
                }
            }
            return(success);
        }
Exemplo n.º 4
0
 public DeviceGPX GetDeviceGPX(DeviceGPX deviceGPX)
 {
     return(DeviceGPXCollection
            .Where(t => t.GPS.DeviceID == deviceGPX.GPS.DeviceID)
            .Where(t => t.Filename == deviceGPX.Filename)
            .FirstOrDefault());
 }
Exemplo n.º 5
0
        public GPXFile ConvertToGPXFile(DeviceGPX deviceGPX)
        {
            GPXFile gpxFile = new GPXFile(deviceGPX.Filename);

            gpxFile.GPS = deviceGPX.GPS;
            gpxFile.ComputeStats(deviceGPX);
            gpxFile.XML = deviceGPX.GPX;
            return(gpxFile);
        }
Exemplo n.º 6
0
        public bool AddRecordToRepo(DeviceGPX gpx)
        {
            int oldCount = DeviceGPXCollection.Count;

            if (gpx == null)
            {
                throw new ArgumentNullException("Error: The argument is Null");
            }
            DeviceGPXCollection.Add(gpx);
            AddToDictionary(gpx.GPS, Entities.GPXFileViewModel.GetFile(gpx.GPS, gpx.Filename));
            return(DeviceGPXCollection.Count > oldCount);
        }
Exemplo n.º 7
0
        public DeviceGPX GetDeviceGPX(DeviceGPX deviceGPX)
        {
            var gpx = DeviceGPXCollection
                .Where(t => t.GPS.DeviceID == deviceGPX.GPS.DeviceID)
                .Where(t => t.Filename == deviceGPX.Filename)
                .FirstOrDefault();

            if (gpx.GPX == "")
            {
                gpx.GPX = DeviceWaypointGPXes.getXML(gpx.RowID);
            }
            return gpx;
        }
Exemplo n.º 8
0
 public GPXFile ConvertToGPXFile(DeviceGPX deviceGPX)
 {
     if (deviceGPX != null)
     {
         GPXFile gpxFile = new GPXFile(deviceGPX.Filename);
         gpxFile.RowID = deviceGPX.RowID;
         gpxFile.GPS   = deviceGPX.GPS;
         gpxFile.ComputeStats(deviceGPX);
         gpxFile.XML = deviceGPX.GPX;
         return(gpxFile);
     }
     return(null);
 }
        public bool Update(DeviceGPX gpx)
        {
            bool success = false;

            using (OleDbConnection conn = new OleDbConnection(Global.ConnectionString))
            {
                conn.Open();


                using (OleDbCommand update = conn.CreateCommand())
                {
                    update.Parameters.Add("@deviceID", OleDbType.VarChar).Value     = gpx.GPS.DeviceID;
                    update.Parameters.Add("@xml", OleDbType.LongVarWChar).Value     = gpx.GPX;
                    update.Parameters.Add("@fileName", OleDbType.VarChar).Value     = gpx.Filename;
                    update.Parameters.Add("@md5", OleDbType.VarChar).Value          = gpx.MD5;
                    update.Parameters.Add("@dateModified", OleDbType.Date).Value    = DateTime.Now;
                    update.Parameters.Add("@gpxType", OleDbType.VarChar).Value      = gpx.GPXType;
                    update.Parameters.Add("@timeStart", OleDbType.Date).Value       = gpx.TimeRangeStart;
                    update.Parameters.Add("@timeEnd", OleDbType.Date).Value         = gpx.TimeRangeEnd;
                    update.Parameters.Add("@timeInterval", OleDbType.Integer).Value = gpx.TimerInterval;
                    update.Parameters.Add("@extracted", OleDbType.Boolean).Value    = gpx.TrackIsExtracted;
                    update.Parameters.Add("@rowID", OleDbType.Integer).Value        = gpx.RowID;

                    update.CommandText = @"Update device_gpx set
                                DeviceID= @deviveID,
                                gpx_xml = @xml,
                                FileName = @fileName,
                                md5 = @md5,
                                DateModified = @dateModified,
                                gpx_type = @gpxType,
                                time_range_start = @timeStart,
                                time_range_end = @timeEnd,
                                TimerInterval = @timeInterval,
                                TrackIsExtracted = @extracted    
                            WHERE RowID = @rowID";

                    try
                    {
                        success = update.ExecuteNonQuery() > 0;
                    }
                    catch (OleDbException dbex)
                    {
                    }
                    catch (Exception ex)
                    {
                        Logger.Log(ex);
                    }
                }
            }
            return(success);
        }
Exemplo n.º 10
0
        public List <Track> ReadTracksFromXML(DeviceGPX deviceGPX)
        {
            string xml       = deviceGPX.GPX;
            GPS    gps       = deviceGPX.GPS;
            string fileName  = deviceGPX.Filename;
            var    trackList = new List <Track>();

            using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
            {
                try
                {
                    while (reader.Read())
                    {
                        if (reader.IsStartElement())
                        {
                            if (reader.Name == "trk")
                            {
                                Track trk = new Track();
                                trk.GPS      = gps;
                                trk.FileName = Path.GetFileName(fileName);
                                trk.IsRoute  = false;
                                trk.Read(reader);

                                if (!Tracks.ContainsKey(gps))
                                {
                                    Tracks.Add(gps, new List <Track>());
                                    Tracks[gps].Add(trk);
                                }
                                else
                                {
                                    if (Tracks[gps].Where(t => t.Name == trk.Name).FirstOrDefault() == null)
                                    {
                                        Tracks[gps].Add(trk);
                                    }
                                }

                                trackList.Add(trk);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Log(ex);
                    return(null);
                }
            }
            return(trackList);
        }
Exemplo n.º 11
0
        public bool UpdateRecordInRepo(DeviceGPX gpx, bool updateExtractedStatus=false)
        {
            _updateExtractedStatus = updateExtractedStatus;
            if (gpx.RowID == 0)
                throw new Exception("Error: ID cannot be null");

            int index = 0;
            while (index < DeviceGPXCollection.Count)
            {
                if (DeviceGPXCollection[index].RowID == gpx.RowID)
                {
                    DeviceGPXCollection[index] = gpx;
                    break;
                }
                index++;
            }
            return _editSuccess;
        }
Exemplo n.º 12
0
        public bool UpdateRecordInRepo(DeviceGPX gpx)
        {
            if (gpx.RowID == 0)
            {
                throw new Exception("Error: ID cannot be null");
            }

            int index = 0;

            while (index < DeviceGPXCollection.Count)
            {
                if (DeviceGPXCollection[index].RowID == gpx.RowID)
                {
                    DeviceGPXCollection[index] = gpx;
                    break;
                }
                index++;
            }
            return(_success);
        }
Exemplo n.º 13
0
        public bool AddRecordToRepo(DeviceGPX gpx)
        {
            int oldCount = DeviceGPXCollection.Count;
            if (gpx == null)
                throw new ArgumentNullException("Error: The argument is Null");
            DeviceGPXCollection.Add(gpx);
            if (_editSuccess)
            {
                GPXFile gpxFile = Entities.GPXFileViewModel.GetFile(gpx.GPS, gpx.Filename);
                if (gpxFile == null)
                {
                    gpxFile = new GPXFile(gpx.Filename) { GPS = gpx.GPS, XML = gpx.GPX };
                    gpxFile.ComputeStats(gpx);


                    Entities.GPXFileViewModel.Add(gpxFile);
                }
                AddToDictionary(gpx.GPS, gpxFile);
            }
            return DeviceGPXCollection.Count > oldCount;
        }
Exemplo n.º 14
0
        public List <DeviceGPX> getDeviceGPXes()
        {
            var thisList = new List <DeviceGPX>();
            var dt       = new DataTable();

            using (var conection = new OleDbConnection(Global.ConnectionString))
            {
                try
                {
                    conection.Open();
                    string query = $"Select * from device_gpx";


                    var adapter = new OleDbDataAdapter(query, conection);
                    adapter.Fill(dt);
                    if (dt.Rows.Count > 0)
                    {
                        thisList.Clear();
                        foreach (DataRow dr in dt.Rows)
                        {
                            DeviceGPX gpx = new DeviceGPX();
                            gpx.Filename       = dr["FileName"].ToString();
                            gpx.GPS            = Entities.GPSViewModel.GetGPSEx(dr["DeviceID"].ToString());
                            gpx.RowID          = int.Parse(dr["RowID"].ToString());
                            gpx.GPX            = dr["gpx_xml"].ToString();
                            gpx.MD5            = dr["md5"].ToString();
                            gpx.GPXType        = dr["gpx_type"].ToString();;
                            gpx.TimeRangeStart = (DateTime)dr["time_range_start"];
                            gpx.TimeRangeEnd   = (DateTime)dr["time_range_end"];
                            thisList.Add(gpx);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Log(ex);
                }
            }
            return(thisList);
        }
Exemplo n.º 15
0
        public void GetFilesFromDevice(DetectedDevice device)
        {
            if (!_gpsFinishedReadingFiles.Contains(device.GPS))
            {
                var gpxFolder = $"{device.Disks[0].Caption }\\{ device.GPS.Folder}";
                if (Directory.Exists(gpxFolder))
                {
                    List <FileInfo> myFiles = new DirectoryInfo(gpxFolder)
                                              .EnumerateFiles()
                                              .Where(f => f.Extension == ".gpx").ToList();


                    foreach (var file in myFiles)
                    {
                        string    xml = File.OpenText(file.FullName).ReadToEnd();
                        DeviceGPX dg  = new DeviceGPX {
                            GPX = xml, Filename = file.Name, GPS = device.GPS
                        };
                        Entities.WaypointViewModel.ReadWaypointsFromFile(dg);
                        GPXFile gf = new GPXFile(file)
                        {
                            GPS       = device.GPS,
                            DriveName = device.Disks[0].Caption,
                            XML       = xml
                        };


                        if (!Contains(gf))
                        {
                            Add(gf);
                            gf.ComputeStats();
                        }
                    }
                }
                _gpsFinishedReadingFiles.Add(device.GPS);
            }
        }
        public List <DeviceGPX> getDeviceGPXes()
        {
            var thisList = new List <DeviceGPX>();
            var dt       = new DataTable();

            using (var conection = new OleDbConnection(Global.ConnectionString))
            {
                try
                {
                    conection.Open();
                    string query = $"Select * from device_gpx";
                    //string query = @"Select FileName, DeviceID, RowID, md5, gpx_type,
                    //                        time_range_start, time_range_end,
                    //                        TimerInterval  from device_gpx";

                    var adapter = new OleDbDataAdapter(query, conection);
                    adapter.Fill(dt);
                    if (dt.Rows.Count > 0)
                    {
                        thisList.Clear();
                        foreach (DataRow dr in dt.Rows)
                        {
                            DeviceGPX gpx = new DeviceGPX();
                            gpx.Filename = dr["FileName"].ToString();
                            gpx.GPS      = Entities.GPSViewModel.GetGPSEx(dr["DeviceID"].ToString());
                            gpx.RowID    = int.Parse(dr["RowID"].ToString());
                            gpx.GPX      = dr["gpx_xml"].ToString();
                            //gpx.GPX = "";
                            gpx.MD5            = dr["md5"].ToString();
                            gpx.GPXType        = dr["gpx_type"].ToString();;
                            gpx.TimeRangeStart = (DateTime)dr["time_range_start"];
                            gpx.TimeRangeEnd   = (DateTime)dr["time_range_end"];
                            if (dr["TimerInterval"] != DBNull.Value)
                            {
                                gpx.TimerInterval = (int)dr["TimerInterval"];
                            }
                            gpx.TrackIsExtracted = (bool)dr["TrackIsExtracted"];
                            thisList.Add(gpx);
                        }
                    }
                }
                catch (OleDbException dbex)
                {
                }
                catch (Exception ex)
                {
                    switch (ex.HResult)
                    {
                    case -2147024809:
                        var    arr       = ex.Message.Split(new char[] { ' ', '\'' });
                        string fieldName = arr[2];
                        if (AddField(fieldName))
                        {
                            return(getDeviceGPXes());
                        }
                        break;

                    default:
                        Logger.Log(ex);
                        break;
                    }
                }
            }
            return(thisList);
        }
Exemplo n.º 17
0
        public bool ComputeStats(DeviceGPX deviceGPX = null)
        {
            DateTime?trkDateStart = null;
            DateTime?trkDateEnd   = null;
            DateTime?wptDateStart = null;
            DateTime?wptDateEnd   = null;


            List <Track> tracks;

            if (deviceGPX != null && deviceGPX.GPX.Length > 0)
            {
                tracks = Entities.TrackViewModel.ReadTracksFromXML(deviceGPX);
                if (tracks == null)
                {
                    return(false);
                }
            }
            else
            {
                tracks = Entities.TrackViewModel.ReadTracksFromFile($"{FileInfo.FullName}", GPS);
            }
            TrackCount = tracks.Count;
            Tracks     = tracks;

            if (TrackCount > 0)
            {
                trkDateStart = tracks[0].Waypoints[0].Time.AddHours(Global.Settings.HoursOffsetGMT);
                trkDateEnd   = tracks[TrackCount - 1].Waypoints[tracks[TrackCount - 1].Waypoints.Count - 1].Time.AddHours(Global.Settings.HoursOffsetGMT);

                int count = 1;
                foreach (var trk in tracks)
                {
                    if (trk.Statistics == null)
                    {
                        return(false);
                    }
                    TrackPointsCount += trk.Waypoints.Count;
                    TrackLength      += trk.Statistics.Length;
                    TimeInMotion     += trk.Statistics.TimeInMotion;

                    foreach (var pt in trk.Waypoints)
                    {
                        pt.Name = count++.ToString();
                        TrackWaypoinsInLocalTime.Add(new WaypointLocalTime(pt));
                    }
                }
                GPSTimerInterval = tracks[0].Statistics.TrackingInterval;
            }


            if (deviceGPX != null || (deviceGPX == null && XML != null))
            {
                List <Waypoint> waypoints = null;
                if (deviceGPX != null)
                {
                    waypoints = Entities.WaypointViewModel.ReadWaypointsFromFile(deviceGPX, true);
                }
                else if (XML != null & XML.Length > 0)
                {
                    waypoints = Entities.WaypointViewModel.ReadWaypointFromXML(XML, FileName);
                }
                if (waypoints == null)
                {
                    WaypointCount = 0;
                }
                else
                {
                    WaypointCount = waypoints.Count;
                }
                if (WaypointCount > 0)
                {
                    wptDateStart = waypoints[0].Time.AddHours(Global.Settings.HoursOffsetGMT);
                    wptDateEnd   = waypoints[WaypointCount - 1].Time.AddHours(Global.Settings.HoursOffsetGMT);

                    foreach (var pt in waypoints)
                    {
                        NamedWaypointsInLocalTime.Add(new WaypointLocalTime(pt));
                    }
                }
            }
            else
            {
                if (Entities.WaypointViewModel.Waypoints.Count > 0 && Entities.WaypointViewModel.Waypoints.ContainsKey(GPS))
                {
                    var waypoints = Entities.WaypointViewModel.Waypoints[GPS]
                                    .Where(t => t.FileName == FileInfo.Name)
                                    .FirstOrDefault();
                    if (waypoints == null)
                    {
                        WaypointCount = 0;
                    }
                    else
                    {
                        WaypointCount = waypoints.Waypoints.Count;
                    }

                    if (WaypointCount > 0)
                    {
                        wptDateStart = waypoints.Waypoints[0].Time.AddHours(Global.Settings.HoursOffsetGMT);
                        wptDateEnd   = waypoints.Waypoints[WaypointCount - 1].Time.AddHours(Global.Settings.HoursOffsetGMT);

                        foreach (var pt in waypoints.Waypoints)
                        {
                            NamedWaypointsInLocalTime.Add(new WaypointLocalTime(pt));
                        }
                    }
                }
            }


            if (trkDateStart != null && wptDateStart != null)
            {
                DateRangeStart = (DateTime)trkDateStart > (DateTime)wptDateStart ? (DateTime)wptDateStart : (DateTime)trkDateStart;
            }
            else if (trkDateStart != null)
            {
                DateRangeStart = (DateTime)trkDateStart;
            }
            else
            {
                if (wptDateStart == null)
                {
                    return(false);
                }
                DateRangeStart = (DateTime)wptDateStart;
            }

            if (trkDateEnd != null && wptDateEnd != null)
            {
                DateRangeEnd = (DateTime)trkDateEnd > (DateTime)wptDateEnd ? (DateTime)trkDateEnd : (DateTime)wptDateEnd;
            }
            else if (trkDateEnd != null)
            {
                DateRangeEnd = (DateTime)trkDateEnd;
            }
            else
            {
                DateRangeEnd = (DateTime)wptDateEnd;
            }

            //if (DateRangeEnd.Date == DateRangeStart.Date)
            //{
            //    DateRange = $"{DateRangeStart.ToString("MMM-dd-yyyy HH:mm")}";
            //}
            //else
            //{
            //    DateRange = $"{DateRangeStart.ToString("MMM-dd-yyyy HH:mm")} - {DateRangeEnd.ToString("MMM-dd-yyyy HH:mm")}";
            //}
            DateRange = $"{DateRangeStart.ToString("MMM-dd-yyyy HH:mm")} - {DateRangeEnd.ToString("MMM-dd-yyyy HH:mm")}";

            if (trkDateStart != null && trkDateEnd != null)
            {
                TimeSpan = trkDateEnd - trkDateStart;
                var h = (int)(((TimeSpan)TimeSpan).TotalMinutes / 60);
                var m = ((TimeSpan)TimeSpan).TotalMinutes % 60;
                TimeSpanHourMinute = $"{h.ToString("000")}:{m.ToString("N1")}";
            }

            return(true);
        }
Exemplo n.º 18
0
        public int ImportGPX(string folder, GPS in_gps = null, bool first = false)
        {
            if (first)
            {
                _count = 0;
            }
            GPS gps         = null;
            GPS current_gps = null;
            var files       = Directory.GetFiles(folder).Select(s => new FileInfo(s));

            if (files.Any())
            {
                foreach (var file in files)
                {
                    if (file.Extension.ToLower() == ".gpx")
                    {
                        var folderName = Path.GetFileName(folder);

                        gps = Entities.GPSViewModel.GetGPSByName(folderName);

                        if (gps != null)
                        {
                            current_gps = gps;
                        }
                        else if (gps == null && in_gps != null)
                        {
                            current_gps = in_gps;
                        }

                        if (current_gps != null)
                        {
                            GPXFile g = new GPXFile(file);
                            g.GPS = current_gps;
                            g.ComputeStats();
                            DeviceGPX d = new DeviceGPX
                            {
                                Filename       = file.Name,
                                GPS            = current_gps,
                                GPX            = g.XML,
                                GPXType        = g.GPXFileType == GPXFileType.Track ? "track" : "waypoint",
                                RowID          = NextRecordNumber,
                                MD5            = CreateMD5(g.XML),
                                TimeRangeStart = g.DateRangeStart,
                                TimeRangeEnd   = g.DateRangeEnd
                            };

                            string fileProcessed = $@"{current_gps.DeviceName}:{file.FullName}";

                            DeviceGPX saved = GetDeviceGPX(d);
                            if (saved == null)
                            {
                                if (AddRecordToRepo(d))
                                {
                                    _count++;
                                    fileProcessed += "  (ADDED)";
                                }
                            }
                            else
                            {
                                if (saved.MD5 != d.MD5 && d.TimeRangeEnd > saved.TimeRangeEnd)
                                {
                                    UpdateRecordInRepo(d);
                                    fileProcessed += " (MODIFIED ADDED)";
                                }
                                else
                                {
                                    fileProcessed += "  (DUPLICATE)";
                                }
                            }
                            Console.WriteLine(fileProcessed);
                        }
                    }
                }
            }

            foreach (var dir in Directory.GetDirectories(folder))
            {
                ImportGPX(dir, current_gps);
            }
            return(_count);
        }
Exemplo n.º 19
0
 public List <Waypoint> ReadWaypointFromDeviceGPX(DeviceGPX deviceGPX)
 {
     return(ReadWaypointFromXML(deviceGPX.GPX, deviceGPX.Filename));
 }
Exemplo n.º 20
0
        private static int ImportGPX(string folder, GPS in_gps = null, bool first = false, int startYear = 2019)
        {
            if (first)
            {
                _count = 0;
            }
            GPS gps         = null;
            GPS current_gps = null;
            var files       = Directory.GetFiles(folder).Select(s => new FileInfo(s));

            if (files.Any())
            {
                foreach (var file in files)
                {
                    if (file.Extension.ToLower() == ".gpx")
                    {
                        var folderName = System.IO.Path.GetFileName(folder);
                        if (ImportGPSData.GPSNameStart.Length > 0)
                        {
                            gps = Entities.GPSViewModel.GetGPS($"{ImportGPSData.GPSNameStart} {GetNumericPartOfFolderName(folderName)}");
                        }
                        else
                        {
                            gps = Entities.GPSViewModel.GetGPSByName(folderName);
                        }

                        if (gps != null)
                        {
                            current_gps = gps;
                        }
                        else if (gps == null && in_gps != null)
                        {
                            current_gps = in_gps;
                        }

                        if (current_gps != null)
                        {
                            GPXFile g = new GPXFile(file);
                            g.GPS = current_gps;
                            if (g.ComputeStats())
                            {
                                if (g.DateRangeStart.Year >= startYear)
                                {
                                    DeviceGPX d = new DeviceGPX
                                    {
                                        Filename       = file.Name,
                                        GPS            = current_gps,
                                        GPX            = g.XML,
                                        GPXType        = g.GPXFileType == GPXFileType.Track ? "track" : "waypoint",
                                        RowID          = Entities.DeviceGPXViewModel.NextRecordNumber,
                                        MD5            = CreateMD5(g.XML),
                                        TimeRangeStart = g.DateRangeStart,
                                        TimeRangeEnd   = g.DateRangeEnd
                                    };

                                    string fileProcessed = $@"{current_gps.DeviceName}:{file.FullName}";

                                    DeviceGPX saved = Entities.DeviceGPXViewModel.GetDeviceGPX(d);
                                    if (saved == null)
                                    {
                                        if (Entities.DeviceGPXViewModel.AddRecordToRepo(d))
                                        {
                                            _count++;
                                            fileProcessed += "  (ADDED)";
                                        }
                                    }
                                    else
                                    {
                                        if (saved.MD5 != d.MD5 && d.TimeRangeEnd > saved.TimeRangeEnd)
                                        {
                                            Entities.DeviceGPXViewModel.UpdateRecordInRepo(d);
                                            fileProcessed += " (MODIFIED ADDED)";
                                        }
                                        else
                                        {
                                            fileProcessed += "  (DUPLICATE)";
                                        }
                                    }
                                    Console.WriteLine(fileProcessed);
                                }
                                else
                                {
                                    Logger.Log($"GPX year for {file.FullName} is less than {startYear}");
                                }
                            }
                            else
                            {
                                Logger.Log($"Error in computing stats for {file.FullName}");
                            }
                        }
                    }
                }
            }

            foreach (var dir in Directory.GetDirectories(folder))
            {
                ImportGPX(dir, current_gps);
            }
            return(_count);
        }
Exemplo n.º 21
0
        private static int ImportGPXByFolder(string folder, GPS in_gps = null, bool?first = false, int yearStartProcess = 2019)
        {
            if ((bool)first)
            {
                Logger.LogType = LogType.ImportGPXfFromFolder;
                _count         = 0;
                _gpxCount      = 0;
            }

            GPS gps         = null;
            GPS current_gps = null;

            if (in_gps != null)
            {
                gps         = in_gps;
                current_gps = in_gps;
            }
            //Logger.Log($"processing folder: {folder}");

            var folderName = System.IO.Path.GetFileName(folder);
            //if (ImportGPSData.GPSNameStart.Length > 0)
            //{

            string result = GetNumericPartOfFolderName(folderName);

            if (result.Length > 0)
            {
                int numericPart = int.Parse(result);
                if (numericPart >= ImportGPSData.StartGPSNumbering && numericPart <= ImportGPSData.EndGPSNumbering)
                {
                    gps = Entities.GPSViewModel.GetGPS($"{ImportGPSData.GPSNameStart} {GetNumericPartOfFolderName(folderName)}");
                }
                else
                {
                    return(0);
                }
            }
            //int numericPart = int.Parse(GetNumericPartOfFolderName(folderName));

            //}
            //else
            //{
            //    gps = Entities.GPSViewModel.GetGPSByName(folderName);
            //}

            if (gps != null)
            {
                current_gps = gps;
            }
            else if (gps == null && in_gps != null)
            {
                current_gps = in_gps;
            }


            var files = Directory.GetFiles(folder).Select(s => new FileInfo(s));

            if (files.Any())
            {
                foreach (var file in files)
                {
                    if (file.Extension.ToLower() == ".gpx")
                    {
                        //var folderName = System.IO.Path.GetFileName(folder);
                        //if(ImportGPSData.GPSNameStart.Length>0)
                        //{

                        //    string result = GetNumericPartOfFolderName(folderName);
                        //    if (result.Length > 0)
                        //    {
                        //        int numericPart = int.Parse(result);
                        //        if (numericPart >= ImportGPSData.StartGPSNumbering && numericPart <= ImportGPSData.EndGPSNumbering)
                        //        {
                        //            gps = Entities.GPSViewModel.GetGPS($"{ImportGPSData.GPSNameStart} {GetNumericPartOfFolderName(folderName)}");
                        //        }
                        //        else
                        //        {
                        //            return 0;
                        //        }
                        //    }
                        //    //int numericPart = int.Parse(GetNumericPartOfFolderName(folderName));

                        //}
                        //else {
                        //    gps = Entities.GPSViewModel.GetGPSByName(folderName);
                        //}

                        //if (gps != null)
                        //{
                        //    current_gps = gps;
                        //}
                        //else if (gps == null && in_gps != null)
                        //{
                        //    current_gps = in_gps;
                        //}

                        if (current_gps != null)
                        {
                            _gpxCount++;
                            GPXFile g = new GPXFile(file);
                            g.GPS = current_gps;
                            if (g.ComputeStats())
                            {
                                if (g.DateRangeStart.Year >= yearStartProcess)
                                {
                                    DeviceGPX d = new DeviceGPX
                                    {
                                        Filename       = file.Name,
                                        GPS            = current_gps,
                                        GPX            = g.XML,
                                        GPXType        = g.GPXFileType == GPXFileType.Track ? "track" : "waypoint",
                                        RowID          = Entities.DeviceGPXViewModel.NextRecordNumber,
                                        MD5            = CreateMD5(g.XML),
                                        TimeRangeStart = g.DateRangeStart,
                                        TimeRangeEnd   = g.DateRangeEnd
                                    };

                                    string fileProcessed = $@"{current_gps.DeviceName}:{file.FullName}";


                                    DeviceGPX saved = Entities.DeviceGPXViewModel.GetDeviceGPX(d);
                                    if (saved == null)
                                    {
                                        if (Entities.DeviceGPXViewModel.AddRecordToRepo(d))
                                        {
                                            _count++;
                                            fileProcessed += "  (ADDED)";

                                            EventHandler <ImportGPXEventArg> importEvent = ImportGPXEvent;
                                            if (importEvent != null)
                                            {
                                                ImportGPXEventArg e = new ImportGPXEventArg
                                                {
                                                    Intent        = "gpx saved",
                                                    GPS           = current_gps,
                                                    ImportedCount = _count,
                                                    GPXFileName   = file.Name
                                                };

                                                importEvent(null, e);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (saved.MD5 != d.MD5 && d.TimeRangeEnd > saved.TimeRangeEnd)
                                        {
                                            if (Entities.DeviceGPXViewModel.UpdateRecordInRepo(d))
                                            {
                                                EventHandler <ImportGPXEventArg> importEvent = ImportGPXEvent;
                                                if (importEvent != null)
                                                {
                                                    ImportGPXEventArg e = new ImportGPXEventArg
                                                    {
                                                        Intent        = "gpx file modified",
                                                        GPS           = current_gps,
                                                        ImportedCount = _count,
                                                        GPXFileName   = file.Name
                                                    };

                                                    importEvent(null, e);
                                                }
                                            }
                                            fileProcessed += " (MODIFIED ADDED)";
                                        }
                                        else
                                        {
                                            EventHandler <ImportGPXEventArg> importEvent = ImportGPXEvent;
                                            if (importEvent != null)
                                            {
                                                ImportGPXEventArg e = new ImportGPXEventArg
                                                {
                                                    Intent        = "gpx file duplicate",
                                                    GPS           = current_gps,
                                                    ImportedCount = _count,
                                                    GPXFileName   = file.Name
                                                };

                                                importEvent(null, e);
                                            }
                                            fileProcessed += "  (DUPLICATE)";
                                        }
                                    }
                                    //Console.WriteLine(fileProcessed);
                                    Logger.Log(fileProcessed);
                                }
                                else
                                {
                                    Logger.Log($"GPX file {file.FullName} time is {g.DateRangeEnd.ToString("MMM-dd-yyyy")} and is not saved");
                                }
                            }
                            else
                            {
                                Logger.Log($"Error computing stats for GPX file {file.FullName} and is not saved");
                            }
                        }
                    }
                }
            }

            foreach (var dir in Directory.GetDirectories(folder))
            {
                ImportGPXByFolder(dir, current_gps);
            }
            return(_count);
        }
Exemplo n.º 22
0
        //public List<Waypoint>ReadWaypointFromDeviceGPX1(DeviceGPX deviceGPX)
        //{
        //    List<Waypoint> wpts = new List<Waypoint>();
        //    using (XmlReader reader = XmlReader.Create(new StringReader(deviceGPX.GPX)))
        //    {
        //        while (reader.Read())
        //        {
        //            if (reader.IsStartElement())
        //            {
        //                if (reader.Name == "wpt")
        //                {
        //                    Waypoint namedWaypoint = new Waypoint();
        //                    namedWaypoint.Read(reader);
        //                    namedWaypoint.GPXFileName = Path.GetFileName(deviceGPX.Filename);
        //                    wpts.Add(namedWaypoint);
        //                }
        //            }
        //        }
        //    }
        //    return wpts;
        //}

        public List <Waypoint> ReadWaypointsFromFile(DeviceGPX deviceGPX = null, bool waypointsOnly = false)
        {
            GPS             gps      = deviceGPX.GPS;
            string          filename = deviceGPX.Filename;
            List <Waypoint> wpts     = new List <Waypoint>();

            using (XmlReader reader = XmlReader.Create(new StringReader(deviceGPX.GPX)))
            {
                while (reader.Read())
                {
                    if (reader.IsStartElement())
                    {
                        if (reader.Name == "wpt")
                        {
                            Waypoint namedWaypoint = new Waypoint();
                            namedWaypoint.Read(reader);
                            namedWaypoint.GPXFileName = Path.GetFileName(deviceGPX.Filename);
                            wpts.Add(namedWaypoint);
                        }
                    }
                }
            }

            if (!waypointsOnly)
            {
                if (wpts.Count > 0)
                {
                    GPSWaypointSet gws = new GPSWaypointSet
                    {
                        GPS          = gps,
                        FullFileName = filename,
                        Waypoints    = wpts
                    };


                    if (Waypoints.Count == 0)
                    {
                        var list = new List <GPSWaypointSet>();
                        list.Add(gws);
                        Waypoints.Add(gps, list);
                    }
                    else
                    {
                        if (Waypoints.ContainsKey(gps))
                        {
                            if (Waypoints[gps].Where(t => t.FileName == gws.FileName).FirstOrDefault() == null)
                            {
                                Waypoints[gps].Add(gws);
                            }
                        }
                        else
                        {
                            var list = new List <GPSWaypointSet>();
                            list.Add(gws);
                            Waypoints.Add(gps, list);
                        }
                    }
                }
            }
            return(wpts);
        }