示例#1
0
        public override bool Import()
        {
            bool result = false;

            try
            {
                if (device.Open())
                {
                    IList <GlobalsatPacket.TrackFileHeader> headers = ((GlobalsatProtocol2)device).ReadTrackHeaders(monitor);
                    List <GlobalsatPacket.TrackFileHeader>  fetch   = new List <GlobalsatPacket.TrackFileHeader>();

                    if (device.FitnessDevice.configInfo.ImportOnlyNew && Plugin.Instance.Application != null && Plugin.Instance.Application.Logbook != null)
                    {
                        IDictionary <DateTime, IList <GlobalsatPacket.TrackFileHeader> > headersByStart = new Dictionary <DateTime, IList <GlobalsatPacket.TrackFileHeader> >();
                        foreach (GlobalsatPacket.TrackFileHeader header in headers)
                        {
                            //Adjust time in headers
                            DateTime start = header.StartTime;
                            if (!headersByStart.ContainsKey(start))
                            {
                                headersByStart.Add(start, new List <GlobalsatPacket.TrackFileHeader>());
                            }
                            headersByStart[start].Add(header);
                        }
                        DateTime now = DateTime.UtcNow;
                        foreach (IActivity activity in Plugin.Instance.Application.Logbook.Activities)
                        {
                            DateTime findTime = activity.StartTime;
                            if (headersByStart.ContainsKey(findTime) && (now - findTime).TotalSeconds > device.FitnessDevice.configInfo.SecondsAlwaysImport &&
                                //always import "bad" data
                                findTime - device.FitnessDevice.NoGpsDate < TimeSpan.FromDays(14))
                            {
                                headersByStart.Remove(findTime);
                            }
                        }
                        foreach (IList <GlobalsatPacket.TrackFileHeader> dateHeaders in headersByStart.Values)
                        {
                            fetch.AddRange(dateHeaders);
                        }
                    }
                    else
                    {
                        fetch.AddRange(headers);
                    }

                    //Read the complete activities from the device
                    IList <GlobalsatPacket.Train> trains = ((GlobalsatProtocol2)device).ReadTracks(fetch, monitor);

                    //popup if short remaining time
                    GlobalsatSystemConfiguration2 systemInfo = ((GlobalsatProtocol2)device).GetGlobalsatSystemConfiguration2();
                    TimeSpan remainTime = device.RemainingTime(headers, systemInfo);
                    if (remainTime < TimeSpan.FromHours(5))
                    {
                        if (remainTime < TimeSpan.Zero)
                        {
                            remainTime = TimeSpan.Zero;
                        }
                        string msg = string.Format("Remaining recording time about {0}", remainTime.ToString());
                        MessageDialog.Show(msg, "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                    }

                    for (int i = 0; i < trains.Count; i++)
                    {
                        //Adjust "no gps fix" time so they are easier to find...
                        //Guess assumes no-GPS matches a real activity and that activities are in order
                        GlobalsatPacket.Train train = trains[i];
                        if (i > 20)
                        {
                        }
                        IActivity logActivity = this.getMatchingLogActivity(i, trains.Count);
                        if (train.StartTime - device.FitnessDevice.NoGpsDate < TimeSpan.FromDays(14) && logActivity != null)
                        {
                            train.Comment += "Original activity date: " + train.StartTime + ", index: " + i;
                            if (DateTime.UtcNow - logActivity.StartTime < TimeSpan.FromDays(7))
                            {
                                //fairly recent, assume this is a duplicate
                                train.StartTime = logActivity.StartTime;
                            }
                            else
                            {
                                //older, set older date
                                train.StartTime = DateTime.UtcNow - TimeSpan.FromDays(7 + trains.Count - i);
                            }
                        }
                    }
                    AddActivities(importResults, trains, device.FitnessDevice.configInfo.ImportSpeedDistanceTrack, device.FitnessDevice.configInfo.DetectPauses, device.FitnessDevice.configInfo.Verbose);
                    result = true;
                }
            }
            catch (Exception e)
            {
                //if (device.DataRecieved)
                {
                    monitor.ErrorText = e.Message;
                    //throw e;
                }
            }
            finally
            {
                device.Close();
            }
            if (!device.DataRecieved)
            {
                //override other possible errors
                device.NoCommunicationError(monitor);
            }
            return(result);
        }
示例#2
0
        public virtual IList <GlobalsatPacket.Train> ReadTracks(IList <GlobalsatPacket.TrackFileHeader> tracks, IJobMonitor monitor)
        {
            if (tracks.Count == 0)
            {
                return(new GlobalsatPacket.Train[0]);
            }

            int           totalPoints  = 0;
            IList <Int16> trackIndexes = new List <Int16>();

            foreach (GlobalsatPacket.TrackFileHeader header in tracks)
            {
                totalPoints += header.TrackPointCount;
                //track number, less than 100
                trackIndexes.Add((Int16)header.TrackPointIndex);
            }
            int totalPointsRead = 0;

            GlobalsatPacket  getFilesPacket = PacketFactory.GetTrackFileSections(trackIndexes);
            GlobalsatPacket2 response       = (GlobalsatPacket2)SendPacket(getFilesPacket);

            monitor.PercentComplete = 0;

            IList <GlobalsatPacket.Train> trains = new List <GlobalsatPacket.Train>();
            ReadMode readMode        = ReadMode.Header;
            int      trainLapsToRead = 0;
            int      pointsToRead    = 0;

            while (response.CommandId != GlobalsatPacket2.CommandId_FINISH && !monitor.Cancelled)
            {
                //Check that previous mode was finished, especially at corruptions there can be out of sync
                if (readMode != ReadMode.Header)
                {
                    byte readMode2 = response.GetTrainContent();
                    if (readMode2 == GlobalsatPacket.HeaderTypeTrackPoints)
                    {
                        if (readMode != ReadMode.Points)
                        {
                            //TODO: Handle error
                        }
                        readMode = ReadMode.Points;
                    }
                    else if (readMode2 == GlobalsatPacket.HeaderTypeLaps)
                    {
                        if (readMode != ReadMode.Laps)
                        {
                            //TODO: Handle error
                        }
                        readMode = ReadMode.Laps;
                    }
                    else
                    {
                        if (readMode != ReadMode.Header)
                        {
                            //TODO: Handle error
                            if (trains.Count > 0)
                            {
                                trains.RemoveAt(trains.Count - 1);
                            }
                        }
                        readMode = ReadMode.Header;
                    }
                }
                if (response.CommandId == GlobalsatPacket2.CommandId_FINISH)
                {
                    break;
                }
                switch (readMode)
                {
                case ReadMode.Header:
                {
                    GlobalsatPacket.Train train = response.UnpackTrainHeader();
                    if (train != null)
                    {
                        trainLapsToRead = train.LapCount;
                        pointsToRead    = train.TrackPointCount;
                        trains.Add(train);
                    }
                    readMode = ReadMode.Laps;
                    break;
                }

                case ReadMode.Laps:
                {
                    GlobalsatPacket.Train       currentTrain = trains[trains.Count - 1];
                    IList <GlobalsatPacket.Lap> laps         = response.UnpackLaps();
                    foreach (GlobalsatPacket.Lap lap in laps)
                    {
                        currentTrain.Laps.Add(lap);
                    }
                    trainLapsToRead -= laps.Count;
                    if (trainLapsToRead <= 0)
                    {
                        readMode = ReadMode.Points;
                    }
                    break;
                }

                case ReadMode.Points:
                {
                    GlobalsatPacket.Train currentTrain        = trains[trains.Count - 1];
                    IList <GlobalsatPacket.TrackPoint> points = response.UnpackTrackPoints();
                    foreach (GlobalsatPacket.TrackPoint point in points)
                    {
                        currentTrain.TrackPoints.Add(point);
                    }
                    pointsToRead    -= points.Count;
                    totalPointsRead += points.Count;
                    DateTime startTime      = currentTrain.StartTime.ToLocalTime();
                    string   statusProgress = startTime.ToShortDateString() + " " + startTime.ToShortTimeString();
                    monitor.StatusText      = String.Format(CommonResources.Text.Devices.ImportJob_Status_Reading, statusProgress);
                    monitor.PercentComplete = (float)totalPointsRead / (float)totalPoints;
                    if (pointsToRead <= 0)
                    {
                        readMode = ReadMode.Header;
                    }
                    break;
                }
                }
                //All requests are the same
                response = (GlobalsatPacket2)SendPacket(PacketFactory.GetNextTrackSection());
            }

            monitor.PercentComplete = 1;
            monitor.StatusText      = CommonResources.Text.Devices.ImportJob_Status_ImportComplete;
            return(trains);
        }