示例#1
0
 public Either <(ActivityHeader activity, string gpx), OMError> ToGpx(ActivityHeader activity, Stream omdStream)
 {
     if (activity != null)
     {
         using (omdStream)
         {
             OMDParser parser = new OMDParser();
             try
             {
                 var datas = parser.Parse(activity, omdStream);
                 if (datas.IsRight)
                 {
                     return(datas.IfLeft(() => new OMError(activity, "no error")));
                 }
                 if (datas.IsLeft && datas.Any())
                 {
                     string gpx = GpxSerializer.Serialize(datas.IfRight(() =>
                     {
                         return(new List <WayPoint>());
                     }));
                     return(activity, gpx);
                 }
             }
             catch (Exception e)
             {
                 return(new OMError(activity, $"ERROR on activity {activity} : {e.Message}"));
             }
         }
     }
     return(new OMError(activity, "no activity provided"));
 }
示例#2
0
        public Either <Unit, OMError> ExtractActivity(ActivityHeader activity, string outputDirectory = null)
        {
            if (activity != null)
            {
                string gpxFileName = activity.GpxFileName;

                using (var stream = File.Open(Path.Combine(DataRoot, $"{activity.Name}.OMD"), FileMode.Open,
                                              FileAccess.Read))
                {
                    var result = ToGpx(activity, stream);


                    if (result.IsLeft)
                    {
                        var    res      = result.IfRight(x => (activity, "empty"));
                        string filename = Path.Combine(outputDirectory ?? OutputDirectory, gpxFileName);
                        File.WriteAllText(filename, res.gpx);
                        return(new Unit());
                    }
                    else
                    {
                        var res = result.IfLeft(() => new OMError(activity, "no error."));
                        return(res);
                    }
                }
            }
            return(new OMError(activity, "no activity provided."));
        }
示例#3
0
 public ExtractionSummary(ActivityHeader activity, string gpx)
 {
     Ok       = true;
     Summary  = activity.ToString();
     Name     = activity.Name;
     Activity = activity;
     Gpx      = gpx;
 }
示例#4
0
        public ActivityHeader GetHeader(string name, Stream stream)
        {
            Dictionary <string, object> omh;

            using (stream)
            {
                omh = OnMov200Schemas.OMH.Read(stream);
            }

            var header = new ActivityHeader(omh, name.Replace($".{HeaderExtension}", ""));

            return(header);
        }
示例#5
0
        public ActivityHeaderModel(ActivityHeader header, List <IDispersionSpan> dispersion)
        {
            Name             = header.Name;
            Dispersion       = dispersion;
            LastPointTime    = header.LastPointTime;
            Duration         = header.Duration;
            DistanceInMeters = header.DistanceInMeters;
            MaximumSpeed     = header.MaximumSpeed;

            DisplayableStartTime    = LastPointTime.ToLocalTime().ToSmartShortDate();
            DisplayableDistance     = (DistanceInMeters / 1000f).ToString("0.00");
            DisplayableTimeSpan     = $"{Duration:h\\:mm}";
            DisplayableAverageSpeed = $"{((DistanceInMeters / 1000f) / Duration.TotalHours):0.0}";
        }
示例#6
0
        public Either <List <WayPoint>, OMError> Parse(ActivityHeader activity, Stream stream)
        {
            DateTime startTime  = activity.DateTime;
            long     length     = stream.Length;
            long     frameCount = length / CHUNK_SIZE;
            long     remainder  = length % CHUNK_SIZE;

            if (remainder != 0)
            {
                return(new OMError(activity, "corrupted activity file : bad length"));
            }

            List <WayPoint> points = new List <WayPoint>();

            for (int i = 0; i < frameCount; i++)
            {
                long dataIdPosition  = i * CHUNK_SIZE + (CHUNK_SIZE - 1);
                long currentPosition = stream.Position;
                stream.Position = dataIdPosition;
                int dataId = stream.ReadByte();
                stream.Position = currentPosition;
                // get schema
                if (!Schemas.ContainsKey(dataId))
                {
                    return(new OMError(activity, $"bad data id for chunk #{i}"));
                }
                Schema schema = Schemas[dataId];
                // read schema
                var data = schema.Read(stream);
                if (dataId == GPS_DATA_ID)
                {
                    var wp = new WayPoint(data);
                    points.Add(wp);
                }
                else
                {
                    int hr1 = (int)data["hr"];
                    int hr2 = (int)data["hr2"];
                    int sw1 = (int)data["stopwatch"];
                    int sw2 = (int)data["stopwatch2"];

                    // todo set HR to last 2 points
                    points.Last().HR   = hr2;
                    DateTime time2     = startTime.AddSeconds(sw2);
                    points.Last().Time = time2;

                    DateTime time1 = startTime.AddSeconds(sw1);
                    points[^ 2].Time = time1;
示例#7
0
        public ActivityHeader GetHeader(FileInfo file)
        {
            ActivityHeader header = null;

            using (var stream = File.Open(file.FullName, FileMode.Open, FileAccess.Read))
            {
                header = GetHeader(file.Name, stream);
            }

            return(header);
            // Dictionary<string, object> omh;
            // using (var stream = File.Open(file.FullName, FileMode.Open, FileAccess.Read))
            // {
            //     omh = OnMov200Schemas.OMH.Read(stream);
            // }
            //
            // var header = new ActivityHeader(omh, file.Name.Replace(".OMH", ""));
            // return header;
        }
示例#8
0
        public ActivityHeaderViewModel(ActivityHeader header, List <IDispersionSpan> dispersion)
        {
            AthleteName      = header.AthleteName;
            Sport            = header.Sport;
            Dispersion       = dispersion;
            LastPointTime    = header.LastPointTime;
            Duration         = header.Duration;
            DistanceInMeters = header.DistanceInMeters;
            CaloriesBurnt    = header.CaloriesBurnt;
            AverageHeartRate = header.AverageHeartRate;
            MaximumHeartRate = header.MaximumHeartRate;
            MaximumSpeed     = header.MaximumSpeed;

            DisplayableCaloriesBurnt    = CaloriesBurnt == 0 ? AppResources.NoValue : CaloriesBurnt.ToString();
            DisplayableStartTime        = LastPointTime.ToLocalTime().ToSmartShortDate();
            DisplayableDistance         = (DistanceInMeters / 1000f).ToString("0.00");
            DisplayableTimeSpan         = $"{Duration:h\\:mm}";
            DisplayableAverageSpeed     = $"{((DistanceInMeters / 1000f) / Duration.TotalHours):0.0}";
            DisplayableAverageHeartRate =
                AverageHeartRate == null ? AppResources.NoValue : AverageHeartRate.Value.ToString();
        }
示例#9
0
        private List <ExtractionSummary> ProcessActivities(List <IFormFile> files)
        {
            List <ExtractionSummary> summary = new List <ExtractionSummary>();

            var onmov      = new OnMov200();
            var validFiles = files.Where(f =>
                                         f.FileName.EndsWith(OnMov200.HeaderExtension) || f.FileName.EndsWith(OnMov200.DataExtension));

            if (validFiles.Count() % 2 != 0)
            {
                throw new Exception("even file number expected");
            }

            var groupedActivitiesFiles = validFiles.GroupBy(f => f.NameWithoutExtension()).ToList();

            int countOk   = 0;
            var extracted = new Dictionary <ActivityHeader, string>();
            int countKo   = 0;
            var errors    = new Dictionary <string, string>();

            foreach (var activityFiles in groupedActivitiesFiles)
            {
                var headerFile = activityFiles.FirstOrDefault(f => f.Extension() == $".{OnMov200.HeaderExtension}");
                if (headerFile != null)
                {
                    string         name   = headerFile.NameWithoutExtension();
                    ActivityHeader header = null;
                    using (var stream = headerFile.OpenReadStream())
                    {
                        header = onmov.GetHeader(name, stream);
                    }

                    if (header != null)
                    {
                        var dataFile = activityFiles.FirstOrDefault(f => f.Extension() == $".{OnMov200.DataExtension}");
                        if (dataFile != null)
                        {
                            using (var stream = dataFile.OpenReadStream())
                            {
                                var result = onmov.ToGpx(header, stream);
                                if (result.IsLeft)
                                {
                                    var res = result.IfRight(() => (header, null));
                                    extracted[res.activity] = res.gpx;
                                    var sum = new ExtractionSummary(res.activity, res.gpx);
                                    summary.Add(sum);
                                    countOk++;
                                }
                                else
                                {
                                    countKo++;
                                    var error = result.IfLeft(() => new OMError(header, "no error"));

                                    var sum = new ExtractionSummary(header.Name, error.ErrorMessage);
                                    summary.Add(sum);
                                }
                            }
                        }
                        else
                        {
                            countKo++;

                            var sum = new ExtractionSummary(header.Name, "unable to find data file.");
                            summary.Add(sum);
                        }
                    }
                    else
                    {
                        countKo++;

                        var sum = new ExtractionSummary(header.Name, " bad header file.");
                        summary.Add(sum);
                    }
                }
                else
                {
                    countKo++;
                    var sum = new ExtractionSummary(activityFiles.Key, " unable to find header file.");
                }
            }



            return(summary);
        }
示例#10
0
 // Used for explore in activity mode
 public Activity(int startTime, string name)
 {
     Serial         = -1;
     Header         = new ActivityHeader();
     PlayerServices = new PlayerServices(startTime, name);
 }
示例#11
0
 public ActivityModel(ActivityHeader activity)
 {
     Activity = activity;
 }
示例#12
0
 public OMError(ActivityHeader activity, string message)
 {
     this.activity = activity;
     this.message  = message;
 }