Ejemplo n.º 1
0
        /// <summary>
        /// <para>Process the Activity items into Sql Tables</para>
        /// </summary>
        /// <param name="processActivity">The activity to be processed</param>
        /// <param name="garminToSqlEntities">The entity framework to process activities into</param>
        protected static void GarminToSqlProcessActivity(FickleFrostbite.TCX.Activity processActivity, FickleFrostbite.SQL.GarminToSqlEntities garminToSqlEntities)
        {
            /* check if there is a duplicate to be processed */
            var isDuplicate = (from a in garminToSqlEntities.Activities
                               where a.TcxActivityId == processActivity.Id
                               select a).Any();

            if (isDuplicate)
            {
                System.Console.WriteLine();
                System.Console.WriteLine("Activity " + processActivity.Id + " is a duplicate entry.");
            }
            else
            {
                /* process activity into SQL */
                System.Console.Write("   Processing " + processActivity.RowCount + " trackpoints: ");

                var processedItemCount = 0;
                var writeActivity      = new FickleFrostbite.SQL.Activity();
                writeActivity.Sport         = processActivity.Sport;
                writeActivity.TcxActivityId = processActivity.Id;
                garminToSqlEntities.Set <FickleFrostbite.SQL.Activity>().Add(writeActivity);
                foreach (var processLap in processActivity.Laps)
                {
                    var writeLap = new FickleFrostbite.SQL.Lap();
                    if (processLap.AverageHeartRateBpm != null)
                    {
                        writeLap.AverageHeartRateBpm = processLap.AverageHeartRateBpm.Value;
                    }
                    writeLap.Calories       = processLap.Calories;
                    writeLap.DistanceMeters = processLap.DistanceMeters;
                    if (processLap.MaximumHeartRateBpm != null)
                    {
                        writeLap.MaximumHeartRateBpm = processLap.MaximumHeartRateBpm.Value;
                    }
                    writeLap.StartTime        = processLap.StartTime;
                    writeLap.TotalTimeSeconds = processLap.TotalTimeSeconds;
                    writeLap.Activity         = writeActivity;
                    garminToSqlEntities.Set <FickleFrostbite.SQL.Lap>().Add(writeLap);
                    foreach (var processTrackpoint in processLap.Track)
                    {
                        var writeTrackpoint = new FickleFrostbite.SQL.Trackpoint();
                        writeTrackpoint.AltitudeMeters = processTrackpoint.AltitudeMeters;
                        writeTrackpoint.DistanceMeters = processTrackpoint.DistanceMeters;
                        if (processTrackpoint.HeartRateBpm != null)
                        {
                            writeTrackpoint.HeartRateBpm = processTrackpoint.HeartRateBpm.Value;
                        }
                        if (processTrackpoint.Position != null)
                        {
                            writeTrackpoint.LatitudeDegrees = processTrackpoint.Position.LatitudeDegrees;
                        }
                        if (processTrackpoint.Position != null)
                        {
                            writeTrackpoint.LongitudeDegrees = processTrackpoint.Position.LongitudeDegrees;
                        }
                        writeTrackpoint.Time = processTrackpoint.Time;
                        writeTrackpoint.Lap  = writeLap;
                        garminToSqlEntities.Set <FickleFrostbite.SQL.Trackpoint>().Add(writeTrackpoint);
                        processedItemCount++;
                        if (processedItemCount % 500 == 0)
                        {
                            System.Console.Write(processedItemCount + "..");
                        }
                    }
                }
                garminToSqlEntities.SaveChanges();
                System.Console.WriteLine();
            }
        }
        /// <summary>
        /// <para>Process the Activity items into Sql Tables</para>
        /// </summary>
        /// <param name="processActivity">The activity to be processed</param>
        /// <param name="garminToSqlEntities">The entity framework to process activities into</param>
        protected static void GarminToSqlProcessActivity(FickleFrostbite.TCX.Activity processActivity, FickleFrostbite.SQL.GarminToSqlEntities garminToSqlEntities)
        {
            /* check if there is a duplicate to be processed */
            var isDuplicate = (from a in garminToSqlEntities.Activities
                               where a.TcxActivityId == processActivity.Id
                               select a).Any();
            if (isDuplicate)
            {
                System.Console.WriteLine();
                System.Console.WriteLine("Activity " + processActivity.Id + " is a duplicate entry.");
            }
            else
            {
                /* process activity into SQL */
                System.Console.Write("   Processing " + processActivity.RowCount + " trackpoints: ");

                var processedItemCount = 0;
                var writeActivity = new FickleFrostbite.SQL.Activity();
                writeActivity.Sport = processActivity.Sport;
                writeActivity.TcxActivityId = processActivity.Id;
                garminToSqlEntities.Set<FickleFrostbite.SQL.Activity>().Add(writeActivity);
                foreach (var processLap in processActivity.Laps)
                {
                    var writeLap = new FickleFrostbite.SQL.Lap();
                    if (processLap.AverageHeartRateBpm != null) { writeLap.AverageHeartRateBpm = processLap.AverageHeartRateBpm.Value; }
                    writeLap.Calories = processLap.Calories;
                    writeLap.DistanceMeters = processLap.DistanceMeters;
                    if (processLap.MaximumHeartRateBpm != null) { writeLap.MaximumHeartRateBpm = processLap.MaximumHeartRateBpm.Value; }
                    writeLap.StartTime = processLap.StartTime;
                    writeLap.TotalTimeSeconds = processLap.TotalTimeSeconds;
                    writeLap.Activity = writeActivity;
                    garminToSqlEntities.Set<FickleFrostbite.SQL.Lap>().Add(writeLap);
                    foreach (var processTrackpoint in processLap.Track)
                    {
                        var writeTrackpoint = new FickleFrostbite.SQL.Trackpoint();
                        writeTrackpoint.AltitudeMeters = processTrackpoint.AltitudeMeters;
                        writeTrackpoint.DistanceMeters = processTrackpoint.DistanceMeters;
                        if (processTrackpoint.HeartRateBpm != null) { writeTrackpoint.HeartRateBpm = processTrackpoint.HeartRateBpm.Value; }
                        if (processTrackpoint.Position != null) { writeTrackpoint.LatitudeDegrees = processTrackpoint.Position.LatitudeDegrees; }
                        if (processTrackpoint.Position != null) { writeTrackpoint.LongitudeDegrees = processTrackpoint.Position.LongitudeDegrees; }
                        writeTrackpoint.Time = processTrackpoint.Time;
                        writeTrackpoint.Lap = writeLap;
                        garminToSqlEntities.Set<FickleFrostbite.SQL.Trackpoint>().Add(writeTrackpoint);
                        processedItemCount++;
                        if (processedItemCount % 500 == 0) { System.Console.Write(processedItemCount + ".."); }
                    }
                }
                garminToSqlEntities.SaveChanges();
                System.Console.WriteLine();
            }
        }