protected void bSaveAndClose_Click(object sender, EventArgs e)
        {
            // TODO: a try catch.. for failure to import

            // TODO: security check ...
            // TODO: need to keep running totals for the user.
            // here we  add the nike+ data to the users workouts
            IList<Workout> workouts = atiStream.DataSource.Cast<Workout>().ToList<Workout>();

            // Now we know "workouts" that the user wants to import.  So we need to try to get the extended data for each workout.

            aqufitEntities entities = new aqufitEntities();
            AccountType accountType = entities.AccountType.FirstOrDefault(at => at.Name == hiddenAccountType.Value);
            ThirdPartyAccounts account = entities.ThirdPartyAccounts.Include("UserSettings").Include("AccountType").FirstOrDefault(a => a.UserSettings.UserKey == this.UserId && a.UserSettings.PortalKey == this.PortalId && a.AccountType.Id == accountType.Id);
            WorkoutConnectorFactory connectorFactory = new WorkoutConnectorFactory(); // TODO: Initialize to an appropriate value
            IWorkoutConnector connector = connectorFactory.GetWorkoutConnector(account);
            if (!connector.Login(account))
            {
                // TODO: error handle
                throw new Exception("Error: login third party data sync failed.");
            }

            workouts = workouts.OrderByDescending(w => w.Date).ToList();     // we want to insert from past to present

            // make some vaiables for totals
            double totalDistance = 0.0;
            long totalTime = 0;
            // So here is the deal with the next bit of code.  If someone syncs more runs then "3"? We don't want to flood the
            // friends streams with all those runs.  So we sync all the runs to the owner steam and NOT friends (publish setting 1)
            // Then we sync a notive to ONLY the friends stream that a bunch of runs were synced (publish setting 2)
            double absTotalDist = 0.0;
            double absTotalCal = 0.0;
            long absTotalTime = 0;
            UserSettings settings = entities.UserSettings.FirstOrDefault(us => us.UserKey == this.UserId && us.PortalKey == this.PortalId);
            Affine.Data.Managers.IDataManager dataManager = Affine.Data.Managers.LINQ.DataManager.Instance;
            if (workouts.Count > 3)
            {
                foreach (Workout w in workouts)
                {
                    totalDistance += Convert.ToDouble(w.Distance);
                    absTotalDist += totalDistance;
                    totalTime += Convert.ToInt64(w.Duration);
                    absTotalTime += totalTime;
                    absTotalCal += Convert.ToDouble(w.Calories);
                    w.PublishSettings = 1; // TODO: no magic number (1) is to publish for yourself but NOT your friends
                    w.Id = 0;       // make sure our third party id does not show up in the id
                    w.UserSetting = settings;
                    WorkoutExtended ext = new WorkoutExtended();
                    w.WorkoutExtendeds.Add(ext);
                   // entities.AddToUserStreamSet(w);

                    try
                    {
                        IList<WorkoutSample> samples = connector.SyncDeviceSamplesForWorkout(ext);
                        samples.ToList().ForEach(s => entities.AddToWorkoutSamples(s));
                    }
                    catch (NoExtendedDataException)
                    {
                        // change the workout src so we dont expect extended data with this one.
                        w.DataSrc = (int)Utils.WorkoutUtil.DataSrc.MANUAL_NO_MAP;
                    }
                    dataManager.SaveWorkout(entities, w);
                    // We got all the data that we require so save the workout
                }

                Utils.UnitsUtil.MeasureUnit distUnit = Affine.Utils.UnitsUtil.ToUnit(settings.DistanceUnits.Value);
                TimeSpan duration = new TimeSpan(0, 0, 0, 0, Convert.ToInt32(totalTime));
                TimeSpan pace = new TimeSpan(0, 0, 0, 0, totalDistance > 0 ? Convert.ToInt32(totalTime / totalDistance) : 0);
                Notification workoutNotification = new Notification()
                {
                    PortalKey = this.PortalId,
                    UserSetting = settings,
                    Date = DateTime.Now.ToUniversalTime(),
                    Title = "Notification <em>"+account.AccountType.Name+" Sync</em>",
                    Description = "Nike+ sync of " + workouts.Count + " workouts.<br />Total Distance: "
                                                + String.Format("{0:0.0}", Affine.Utils.UnitsUtil.systemDefaultToUnits(totalDistance, distUnit)) + " " + Affine.Utils.UnitsUtil.unitToStringName(distUnit) + "<br />"
                                                + "Total Time: " + String.Format("{0:00}", duration.Hours) + ":" + String.Format("{0:00}", duration.Minutes) + ":" + String.Format("{0:00}", duration.Seconds) + "<br />"
                                                + "Avg. Pace: " + String.Format("{0:0}", pace.Minutes) + ":" + String.Format("{0:00}", pace.Seconds),
                    TimeStamp = DateTime.Now.ToUniversalTime(),
                    NotificationType = (int)Affine.Utils.ConstsUtil.NotificationTypes.NIKE_PLUS,
                    PublishSettings = (int)Affine.Utils.ConstsUtil.PublishSettings.FRIEND_ONLY
                };
                entities.AddToUserStreamSet(workoutNotification);
                // Adjust the totals now
                /*
                WorkoutTotal totals = entities.WorkoutTotals.FirstOrDefault(wt => wt.UserKey == this.UserId && wt.PortalKey == this.PortalId && wt.WorkoutTypeKey == 0);   // When workout type key == 0 This is the "Entire" totals for all workout types
                if (totals == null)
                {
                    totals = new WorkoutTotal();
                    totals.PortalKey = this.PortalId;
                    totals.UserKey = this.UserId;
                    entities.AddToWorkoutTotals(totals);
                }
                totals.Distance += absTotalDist;
                totals.Calories += absTotalCal;
                totals.Time += absTotalTime;
                totals.Count+=workouts.Count;
                entities.SaveChanges();
                 */
            }
            else  // just add the 3 or less runs normaly
            {
                foreach (Workout w in workouts)
                {
                    totalDistance += Convert.ToDouble(w.Distance);
                    absTotalDist += totalDistance;
                    totalTime += Convert.ToInt64(w.Duration);
                    absTotalTime += totalTime;
                    absTotalCal += Convert.ToDouble(w.Calories);
                    w.Id = 0;       // make sure our third party id does not show up in the id
                    w.UserSetting = settings;
                    WorkoutExtended ext = new WorkoutExtended();
                    w.WorkoutExtendeds.Add(ext);
                   // entities.AddToUserStreamSet(w);
                    try
                    {
                        IList<WorkoutSample> samples = connector.SyncDeviceSamplesForWorkout(ext);
                        samples.ToList().ForEach(s => entities.AddToWorkoutSamples(s));
                    }
                    catch (NoExtendedDataException)
                    {
                        // TODO: log this... change the workout src so we dont expect extended data with this one.
                        w.DataSrc = (int)Utils.WorkoutUtil.DataSrc.MANUAL_NO_MAP;
                    }
                    dataManager.SaveWorkout(entities, w);
                }
            }
            RadAjaxManager1.ResponseScripts.Add("self.close();  top.location.href = '" + ResolveUrl("~") + UserSettings.UserName + "'; ");
        }
Exemple #2
0
    /*
    private IList<WorkoutSample> DeviceSamplesForWorkout(WorkoutExtended workoutExted)
    {
            IList<WorkoutSample> workoutSamples = new List<WorkoutSample>();
            int sequenceNum = 0;
            double minLat = double.MaxValue;
            double maxLat = double.MinValue;
            double minLng = double.MaxValue;
            double maxLng = double.MinValue;
            foreach (XElement sample in samples)
            {
                WorkoutSample workoutSample = DeviceSampleToWorkoutSampleAdaptor(sample);
                if (workoutSample.Lat != null && workoutSample.Lng != null)
                {
                    double lat = Convert.ToDouble(workoutSample.Lat);
                    double lng = Convert.ToDouble(workoutSample.Lng);
                    if (lat < minLat)
                    {
                        minLat = lat;
                    }
                    if (lat > maxLat)
                    {
                        maxLat = lat;
                    }
                    if (lng < minLng)
                    {
                        minLng = lng;
                    }
                    if (lng > maxLng)
                    {
                        maxLng = lng;
                    }
                }
                workoutSample.SampleNumber = sequenceNum++;
                workoutSample.WorkoutExtended = workoutExted;
                workoutSamples.Add(workoutSample);
            }
            workoutExted.LatMin = minLat;
            workoutExted.LatMax = maxLat;
            workoutExted.LngMin = minLng;
            workoutExted.LngMax = maxLng;

            return workoutSamples;

    }
     */
    private void ParseFitnessHistoryDetail(string xml)
    {
        XElement doc = XElement.Parse(xml);
        XElement activity = doc.Descendants().FirstOrDefault(n => n.Name.LocalName == "Activity");

        aqufitEntities entities = new aqufitEntities();
        User user = entities.UserSettings.OfType<User>().FirstOrDefault(u => u.UserKey == PortalSettings.UserId && u.PortalKey == PortalSettings.PortalId);

        Affine.Utils.WorkoutUtil.WorkoutType workoutType = Affine.Utils.WorkoutUtil.WorkoutType.RUNNING;
        string atype = (activity.Attribute("Sport").Value).ToLower();
        string title = string.Empty;
        switch (atype)
        {
            case "walking":
                workoutType = Affine.Utils.WorkoutUtil.WorkoutType.WALKING;
                title = "Walking ";
                break;
            case "cycling":
                workoutType = Affine.Utils.WorkoutUtil.WorkoutType.CYCLING;
                title = "Cycling ";
                break;
            case "swimming":
                workoutType = Affine.Utils.WorkoutUtil.WorkoutType.SWIMMING;
                title = "Swimming ";
                break;
            case "running":
            default:
                title = "Running ";
                workoutType = Affine.Utils.WorkoutUtil.WorkoutType.RUNNING;
                break;
        }
        string activityId = activity.Descendants().FirstOrDefault( n => n.Name.LocalName == "Id" ).Value;
        long thirdParyId = GetNumberFromStr(activityId);

        IEnumerable<XElement> laps = activity.Descendants().Where(n => n.Name.LocalName == "Lap");
        if (laps.Count() > 0)
        {   // we must have a lap
            // get the start time.
            XElement lap1 = laps.ElementAt(0);
            DateTime startTime = Convert.ToDateTime(lap1.Attribute("StartTime").Value);

            // total time
            double[] timesInSec = activity.Descendants().Where(n => n.Name.LocalName == "TotalTimeSeconds").Select(n => Convert.ToDouble( n.Value )).ToArray();
            double totalSec = 0.0;
            foreach (double t in timesInSec)
            {
                totalSec += t;
            }
            long duration = (long)Affine.Utils.UnitsUtil.unitsToSystemDefualt(totalSec, Affine.Utils.UnitsUtil.MeasureUnit.UNIT_SEC);

            // total cal
            double[] calArray = activity.Descendants().Where(n => n.Name.LocalName == "Calories").Select(n => Convert.ToDouble( n.Value)).ToArray();
            double totalCal = 0.0;
            foreach (double c in calArray)
            {
                totalCal += c;
            }

            // total dist
            double[] distArray = activity.Descendants().Where(n => n.Name.LocalName == "DistanceMeters").Where(n => n.Parent.Name.LocalName == "Lap").Select(n => Convert.ToDouble(n.Value)).ToArray();
            double totalDist = 0.0;
            foreach (double d in distArray)
            {
                totalDist += d;
            }

            Workout workout = new Workout()
            {
                Date = startTime.ToUniversalTime(),
                TimeStamp = DateTime.Now,
                Calories = totalCal,
                AccoutType = (short)Affine.Services.ThirdParty.AccountTypes.GARMIN,
                Description = "",
                Distance = Affine.Utils.UnitsUtil.unitsToSystemDefualt(totalDist, Affine.Utils.UnitsUtil.MeasureUnit.UNIT_M),
                Duration = duration,
                Emotion = null,
                PortalKey = (int)PortalSettings.PortalId,
                UserSetting = user,
                Terrain = null,
                ThirdPartyId = thirdParyId,
                Title = title,
                Weather = null,
                DataSrc = (int)Affine.Utils.WorkoutUtil.DataSrc.GARMIN,
                WorkoutTypeKey = (long)workoutType
            };
            // now we need to add the extended data ...

            // get all the track points
            XElement[] trackpoints = activity.Descendants().Where(n => n.Name.LocalName == "Trackpoint").ToArray();
          //  IList<WorkoutSample> workoutSamples = new List<WorkoutSample>();
            WorkoutExtended extended = new WorkoutExtended()
            {
                UserStream = workout
            };
            entities.AddToWorkoutExtendeds(extended);
            int sequenceNum = 0;
            double minLat = double.MaxValue;
            double maxLat = double.MinValue;
            double minLng = double.MaxValue;
            double maxLng = double.MinValue;
            foreach (XElement point in trackpoints)
            {
                try
                {
                    double lat = Convert.ToDouble(point.Descendants().FirstOrDefault(n => n.Name.LocalName == "LatitudeDegrees").Value);
                    double lng = Convert.ToDouble(point.Descendants().FirstOrDefault(n => n.Name.LocalName == "LongitudeDegrees").Value);
                    DateTime time = Convert.ToDateTime(point.Descendants().FirstOrDefault(n => n.Name.LocalName == "Time").Value);
                    double distance = Convert.ToDouble(point.Descendants().FirstOrDefault(n => n.Name.LocalName == "DistanceMeters").Value);
                    distance = Affine.Utils.UnitsUtil.unitsToSystemDefualt(distance, Affine.Utils.UnitsUtil.MeasureUnit.UNIT_M);
                    XElement el = point.Descendants().FirstOrDefault(n => n.Name.LocalName == "AltitudeMeters");
                    double? elivation = null;
                    if (el != null)
                    {
                        elivation = Convert.ToDouble(el.Value);
                        elivation = Affine.Utils.UnitsUtil.unitsToSystemDefualt(elivation.Value, Affine.Utils.UnitsUtil.MeasureUnit.UNIT_M);
                    }
                    if (lat < minLat)
                    {
                        minLat = lat;
                    }
                    if (lat > maxLat)
                    {
                        maxLat = lat;
                    }
                    if (lng < minLng)
                    {
                        minLng = lng;
                    }
                    if (lng > maxLng)
                    {
                        maxLng = lng;
                    }
                    WorkoutSample sample = new WorkoutSample()
                    {
                        Lat = lat,
                        Lng = lng,
                        SampleNumber = sequenceNum++,
                        WorkoutExtended = extended,
                        Date = time,
                        Elevation = elivation,
                        Distance = distance
                    };
                    entities.AddToWorkoutSamples(sample);
                }
                catch (NullReferenceException ex) { }   // just goto the next full sample
            }
            extended.LatMax = maxLat;
            extended.LatMin = minLat;
            extended.LngMax = maxLng;
            extended.LngMin = minLng;
            Affine.Data.Managers.IDataManager dataMan = Affine.Data.Managers.LINQ.DataManager.Instance;
            dataMan.SaveWorkout(entities, workout);
        }
    }
        protected void bSaveAndClose_Click(object sender, EventArgs e)
        {
            // TODO: a try catch.. for failure to import

            // TODO: security check ...
            // TODO: need to keep running totals for the user.
            // here we  add the nike+ data to the users workouts
            IList <Workout> workouts = atiStream.DataSource.Cast <Workout>().ToList <Workout>();

            // Now we know "workouts" that the user wants to import.  So we need to try to get the extended data for each workout.

            aqufitEntities          entities         = new aqufitEntities();
            AccountType             accountType      = entities.AccountType.FirstOrDefault(at => at.Name == hiddenAccountType.Value);
            ThirdPartyAccounts      account          = entities.ThirdPartyAccounts.Include("UserSettings").Include("AccountType").FirstOrDefault(a => a.UserSettings.UserKey == this.UserId && a.UserSettings.PortalKey == this.PortalId && a.AccountType.Id == accountType.Id);
            WorkoutConnectorFactory connectorFactory = new WorkoutConnectorFactory(); // TODO: Initialize to an appropriate value
            IWorkoutConnector       connector        = connectorFactory.GetWorkoutConnector(account);

            if (!connector.Login(account))
            {
                // TODO: error handle
                throw new Exception("Error: login third party data sync failed.");
            }

            workouts = workouts.OrderByDescending(w => w.Date).ToList();     // we want to insert from past to present

            // make some vaiables for totals
            double totalDistance = 0.0;
            long   totalTime     = 0;
            // So here is the deal with the next bit of code.  If someone syncs more runs then "3"? We don't want to flood the
            // friends streams with all those runs.  So we sync all the runs to the owner steam and NOT friends (publish setting 1)
            // Then we sync a notive to ONLY the friends stream that a bunch of runs were synced (publish setting 2)
            double       absTotalDist = 0.0;
            double       absTotalCal  = 0.0;
            long         absTotalTime = 0;
            UserSettings settings     = entities.UserSettings.FirstOrDefault(us => us.UserKey == this.UserId && us.PortalKey == this.PortalId);

            Affine.Data.Managers.IDataManager dataManager = Affine.Data.Managers.LINQ.DataManager.Instance;
            if (workouts.Count > 3)
            {
                foreach (Workout w in workouts)
                {
                    totalDistance    += Convert.ToDouble(w.Distance);
                    absTotalDist     += totalDistance;
                    totalTime        += Convert.ToInt64(w.Duration);
                    absTotalTime     += totalTime;
                    absTotalCal      += Convert.ToDouble(w.Calories);
                    w.PublishSettings = 1; // TODO: no magic number (1) is to publish for yourself but NOT your friends
                    w.Id          = 0;     // make sure our third party id does not show up in the id
                    w.UserSetting = settings;
                    WorkoutExtended ext = new WorkoutExtended();
                    w.WorkoutExtendeds.Add(ext);
                    // entities.AddToUserStreamSet(w);

                    try
                    {
                        IList <WorkoutSample> samples = connector.SyncDeviceSamplesForWorkout(ext);
                        samples.ToList().ForEach(s => entities.AddToWorkoutSamples(s));
                    }
                    catch (NoExtendedDataException)
                    {
                        // change the workout src so we dont expect extended data with this one.
                        w.DataSrc = (int)Utils.WorkoutUtil.DataSrc.MANUAL_NO_MAP;
                    }
                    dataManager.SaveWorkout(entities, w);
                    // We got all the data that we require so save the workout
                }

                Utils.UnitsUtil.MeasureUnit distUnit = Affine.Utils.UnitsUtil.ToUnit(settings.DistanceUnits.Value);
                TimeSpan     duration            = new TimeSpan(0, 0, 0, 0, Convert.ToInt32(totalTime));
                TimeSpan     pace                = new TimeSpan(0, 0, 0, 0, totalDistance > 0 ? Convert.ToInt32(totalTime / totalDistance) : 0);
                Notification workoutNotification = new Notification()
                {
                    PortalKey   = this.PortalId,
                    UserSetting = settings,
                    Date        = DateTime.Now.ToUniversalTime(),
                    Title       = "Notification <em>" + account.AccountType.Name + " Sync</em>",
                    Description = "Nike+ sync of " + workouts.Count + " workouts.<br />Total Distance: "
                                  + String.Format("{0:0.0}", Affine.Utils.UnitsUtil.systemDefaultToUnits(totalDistance, distUnit)) + " " + Affine.Utils.UnitsUtil.unitToStringName(distUnit) + "<br />"
                                  + "Total Time: " + String.Format("{0:00}", duration.Hours) + ":" + String.Format("{0:00}", duration.Minutes) + ":" + String.Format("{0:00}", duration.Seconds) + "<br />"
                                  + "Avg. Pace: " + String.Format("{0:0}", pace.Minutes) + ":" + String.Format("{0:00}", pace.Seconds),
                    TimeStamp        = DateTime.Now.ToUniversalTime(),
                    NotificationType = (int)Affine.Utils.ConstsUtil.NotificationTypes.NIKE_PLUS,
                    PublishSettings  = (int)Affine.Utils.ConstsUtil.PublishSettings.FRIEND_ONLY
                };
                entities.AddToUserStreamSet(workoutNotification);
                // Adjust the totals now

                /*
                 * WorkoutTotal totals = entities.WorkoutTotals.FirstOrDefault(wt => wt.UserKey == this.UserId && wt.PortalKey == this.PortalId && wt.WorkoutTypeKey == 0);   // When workout type key == 0 This is the "Entire" totals for all workout types
                 * if (totals == null)
                 * {
                 *  totals = new WorkoutTotal();
                 *  totals.PortalKey = this.PortalId;
                 *  totals.UserKey = this.UserId;
                 *  entities.AddToWorkoutTotals(totals);
                 * }
                 * totals.Distance += absTotalDist;
                 * totals.Calories += absTotalCal;
                 * totals.Time += absTotalTime;
                 * totals.Count+=workouts.Count;
                 * entities.SaveChanges();
                 */
            }
            else  // just add the 3 or less runs normaly
            {
                foreach (Workout w in workouts)
                {
                    totalDistance += Convert.ToDouble(w.Distance);
                    absTotalDist  += totalDistance;
                    totalTime     += Convert.ToInt64(w.Duration);
                    absTotalTime  += totalTime;
                    absTotalCal   += Convert.ToDouble(w.Calories);
                    w.Id           = 0; // make sure our third party id does not show up in the id
                    w.UserSetting  = settings;
                    WorkoutExtended ext = new WorkoutExtended();
                    w.WorkoutExtendeds.Add(ext);
                    // entities.AddToUserStreamSet(w);
                    try
                    {
                        IList <WorkoutSample> samples = connector.SyncDeviceSamplesForWorkout(ext);
                        samples.ToList().ForEach(s => entities.AddToWorkoutSamples(s));
                    }
                    catch (NoExtendedDataException)
                    {
                        // TODO: log this... change the workout src so we dont expect extended data with this one.
                        w.DataSrc = (int)Utils.WorkoutUtil.DataSrc.MANUAL_NO_MAP;
                    }
                    dataManager.SaveWorkout(entities, w);
                }
            }
            RadAjaxManager1.ResponseScripts.Add("self.close();  top.location.href = '" + ResolveUrl("~") + UserSettings.UserName + "'; ");
        }
Exemple #4
0
    /*
     * private IList<WorkoutSample> DeviceSamplesForWorkout(WorkoutExtended workoutExted)
     * {
     *      IList<WorkoutSample> workoutSamples = new List<WorkoutSample>();
     *      int sequenceNum = 0;
     *      double minLat = double.MaxValue;
     *      double maxLat = double.MinValue;
     *      double minLng = double.MaxValue;
     *      double maxLng = double.MinValue;
     *      foreach (XElement sample in samples)
     *      {
     *          WorkoutSample workoutSample = DeviceSampleToWorkoutSampleAdaptor(sample);
     *          if (workoutSample.Lat != null && workoutSample.Lng != null)
     *          {
     *              double lat = Convert.ToDouble(workoutSample.Lat);
     *              double lng = Convert.ToDouble(workoutSample.Lng);
     *              if (lat < minLat)
     *              {
     *                  minLat = lat;
     *              }
     *              if (lat > maxLat)
     *              {
     *                  maxLat = lat;
     *              }
     *              if (lng < minLng)
     *              {
     *                  minLng = lng;
     *              }
     *              if (lng > maxLng)
     *              {
     *                  maxLng = lng;
     *              }
     *          }
     *          workoutSample.SampleNumber = sequenceNum++;
     *          workoutSample.WorkoutExtended = workoutExted;
     *          workoutSamples.Add(workoutSample);
     *      }
     *      workoutExted.LatMin = minLat;
     *      workoutExted.LatMax = maxLat;
     *      workoutExted.LngMin = minLng;
     *      workoutExted.LngMax = maxLng;
     *
     *      return workoutSamples;
     *
     * }
     */

    private void ParseFitnessHistoryDetail(string xml)
    {
        XElement doc      = XElement.Parse(xml);
        XElement activity = doc.Descendants().FirstOrDefault(n => n.Name.LocalName == "Activity");

        aqufitEntities entities = new aqufitEntities();
        User           user     = entities.UserSettings.OfType <User>().FirstOrDefault(u => u.UserKey == PortalSettings.UserId && u.PortalKey == PortalSettings.PortalId);

        Affine.Utils.WorkoutUtil.WorkoutType workoutType = Affine.Utils.WorkoutUtil.WorkoutType.RUNNING;
        string atype = (activity.Attribute("Sport").Value).ToLower();
        string title = string.Empty;

        switch (atype)
        {
        case "walking":
            workoutType = Affine.Utils.WorkoutUtil.WorkoutType.WALKING;
            title       = "Walking ";
            break;

        case "cycling":
            workoutType = Affine.Utils.WorkoutUtil.WorkoutType.CYCLING;
            title       = "Cycling ";
            break;

        case "swimming":
            workoutType = Affine.Utils.WorkoutUtil.WorkoutType.SWIMMING;
            title       = "Swimming ";
            break;

        case "running":
        default:
            title       = "Running ";
            workoutType = Affine.Utils.WorkoutUtil.WorkoutType.RUNNING;
            break;
        }
        string activityId  = activity.Descendants().FirstOrDefault(n => n.Name.LocalName == "Id").Value;
        long   thirdParyId = GetNumberFromStr(activityId);

        IEnumerable <XElement> laps = activity.Descendants().Where(n => n.Name.LocalName == "Lap");

        if (laps.Count() > 0)
        {   // we must have a lap
            // get the start time.
            XElement lap1      = laps.ElementAt(0);
            DateTime startTime = Convert.ToDateTime(lap1.Attribute("StartTime").Value);

            // total time
            double[] timesInSec = activity.Descendants().Where(n => n.Name.LocalName == "TotalTimeSeconds").Select(n => Convert.ToDouble(n.Value)).ToArray();
            double   totalSec   = 0.0;
            foreach (double t in timesInSec)
            {
                totalSec += t;
            }
            long duration = (long)Affine.Utils.UnitsUtil.unitsToSystemDefualt(totalSec, Affine.Utils.UnitsUtil.MeasureUnit.UNIT_SEC);

            // total cal
            double[] calArray = activity.Descendants().Where(n => n.Name.LocalName == "Calories").Select(n => Convert.ToDouble(n.Value)).ToArray();
            double   totalCal = 0.0;
            foreach (double c in calArray)
            {
                totalCal += c;
            }

            // total dist
            double[] distArray = activity.Descendants().Where(n => n.Name.LocalName == "DistanceMeters").Where(n => n.Parent.Name.LocalName == "Lap").Select(n => Convert.ToDouble(n.Value)).ToArray();
            double   totalDist = 0.0;
            foreach (double d in distArray)
            {
                totalDist += d;
            }

            Workout workout = new Workout()
            {
                Date           = startTime.ToUniversalTime(),
                TimeStamp      = DateTime.Now,
                Calories       = totalCal,
                AccoutType     = (short)Affine.Services.ThirdParty.AccountTypes.GARMIN,
                Description    = "",
                Distance       = Affine.Utils.UnitsUtil.unitsToSystemDefualt(totalDist, Affine.Utils.UnitsUtil.MeasureUnit.UNIT_M),
                Duration       = duration,
                Emotion        = null,
                PortalKey      = (int)PortalSettings.PortalId,
                UserSetting    = user,
                Terrain        = null,
                ThirdPartyId   = thirdParyId,
                Title          = title,
                Weather        = null,
                DataSrc        = (int)Affine.Utils.WorkoutUtil.DataSrc.GARMIN,
                WorkoutTypeKey = (long)workoutType
            };
            // now we need to add the extended data ...

            // get all the track points
            XElement[] trackpoints = activity.Descendants().Where(n => n.Name.LocalName == "Trackpoint").ToArray();
            //  IList<WorkoutSample> workoutSamples = new List<WorkoutSample>();
            WorkoutExtended extended = new WorkoutExtended()
            {
                UserStream = workout
            };
            entities.AddToWorkoutExtendeds(extended);
            int    sequenceNum = 0;
            double minLat      = double.MaxValue;
            double maxLat      = double.MinValue;
            double minLng      = double.MaxValue;
            double maxLng      = double.MinValue;
            foreach (XElement point in trackpoints)
            {
                try
                {
                    double   lat      = Convert.ToDouble(point.Descendants().FirstOrDefault(n => n.Name.LocalName == "LatitudeDegrees").Value);
                    double   lng      = Convert.ToDouble(point.Descendants().FirstOrDefault(n => n.Name.LocalName == "LongitudeDegrees").Value);
                    DateTime time     = Convert.ToDateTime(point.Descendants().FirstOrDefault(n => n.Name.LocalName == "Time").Value);
                    double   distance = Convert.ToDouble(point.Descendants().FirstOrDefault(n => n.Name.LocalName == "DistanceMeters").Value);
                    distance = Affine.Utils.UnitsUtil.unitsToSystemDefualt(distance, Affine.Utils.UnitsUtil.MeasureUnit.UNIT_M);
                    XElement el        = point.Descendants().FirstOrDefault(n => n.Name.LocalName == "AltitudeMeters");
                    double?  elivation = null;
                    if (el != null)
                    {
                        elivation = Convert.ToDouble(el.Value);
                        elivation = Affine.Utils.UnitsUtil.unitsToSystemDefualt(elivation.Value, Affine.Utils.UnitsUtil.MeasureUnit.UNIT_M);
                    }
                    if (lat < minLat)
                    {
                        minLat = lat;
                    }
                    if (lat > maxLat)
                    {
                        maxLat = lat;
                    }
                    if (lng < minLng)
                    {
                        minLng = lng;
                    }
                    if (lng > maxLng)
                    {
                        maxLng = lng;
                    }
                    WorkoutSample sample = new WorkoutSample()
                    {
                        Lat             = lat,
                        Lng             = lng,
                        SampleNumber    = sequenceNum++,
                        WorkoutExtended = extended,
                        Date            = time,
                        Elevation       = elivation,
                        Distance        = distance
                    };
                    entities.AddToWorkoutSamples(sample);
                }
                catch (NullReferenceException ex) { }   // just goto the next full sample
            }
            extended.LatMax = maxLat;
            extended.LatMin = minLat;
            extended.LngMax = maxLng;
            extended.LngMin = minLng;
            Affine.Data.Managers.IDataManager dataMan = Affine.Data.Managers.LINQ.DataManager.Instance;
            dataMan.SaveWorkout(entities, workout);
        }
    }
Exemple #5
0
 protected void Page_Load(object sender, EventArgs e)
 {
     if (!string.IsNullOrEmpty(Request["w"]))
     {
         aqufitEntities entities = new aqufitEntities();
         long           wid      = Convert.ToInt64(Request["w"]);
         // TODO: need security
         Workout workout = entities.UserStreamSet.Include("WorkoutExtendeds").Include("UserSetting").OfType <Workout>().FirstOrDefault(w => w.Id == wid);
         if (workout != null)
         {
             Affine.Data.json.Workout json = new Affine.Data.json.Workout()
             {
                 Calories    = workout.Calories,
                 Date        = workout.Date.ToShortDateString(),
                 Description = workout.Description,
                 Distance    = workout.Distance,
                 Duration    = workout.Duration,
                 Emotion     = workout.Emotion,
                 Id          = workout.Id,
                 Title       = workout.Title,
                 PortalKey   = workout.PortalKey,
                 UserKey     = workout.UserSetting.UserKey,
                 Weather     = workout.Weather,
             };
             if (workout.WorkoutExtendeds.FirstOrDefault() != null)
             {
                 WorkoutExtended ext = workout.WorkoutExtendeds.First();
                 Affine.Data.json.WorkoutExtended jsonWorkoutExtended = new Affine.Data.json.WorkoutExtended()
                 {
                     Id     = ext.Id,
                     LatMax = ext.LatMax,
                     LatMin = ext.LatMin,
                     LngMax = ext.LngMax,
                     LngMin = ext.LngMin
                 };
                 Affine.Data.json.WorkoutSample[] samples = entities.WorkoutSamples.Where(s => s.WorkoutExtended.Id == ext.Id).OrderBy(s => s.SampleNumber)
                                                            .Select(s => new Affine.Data.json.WorkoutSample()
                 {
                     Date         = s.Date,
                     Time         = 0,
                     Distance     = s.Distance,
                     Elevation    = s.Elevation,
                     HeartRate    = s.HeartRate,
                     Id           = s.Id,
                     Lat          = s.Lat,
                     Lng          = s.Lng,
                     SampleNumber = s.SampleNumber
                 }).ToArray();
                 foreach (Affine.Data.json.WorkoutSample s in samples)
                 {
                     s.Time = s.Date.Ticks;
                 }
                 jsonWorkoutExtended.WorkoutSamples = samples;
                 json.WorkoutExtended = jsonWorkoutExtended;
             }
             JavaScriptSerializer jserializer = new JavaScriptSerializer();
             Response.Write(jserializer.Serialize(json));
         }
     }
     else if (Request["ed"] != null && Request["u"] != null && Request["sd"] != null)
     {
         long     uid       = Convert.ToInt64(Request["u"]);
         DateTime endDate   = DateTime.Parse(Request["ed"]);
         DateTime startDate = DateTime.Parse(Request["sd"]).ToUniversalTime();
         endDate = endDate.AddDays(1).ToUniversalTime();
         aqufitEntities entities = new aqufitEntities();
         IList <Affine.Data.json.Workout> ret = new List <Affine.Data.json.Workout>();
         IList <Workout> workoutList          = entities.UserStreamSet.Include("UserSetting").OfType <Workout>().
                                                Where(w => w.UserSetting.UserKey == uid && w.Date.CompareTo(endDate) <= 0 && w.Date.CompareTo(startDate) >= 0).OrderByDescending(w => w.Date).ToList();
         workoutList = workoutList.Reverse().ToList();
         foreach (Workout workout in workoutList)
         {
             Affine.Data.json.Workout json = new Affine.Data.json.Workout()
             {
                 Calories    = workout.Calories,
                 TimeStamp   = workout.TimeStamp,
                 Date        = workout.Date.ToShortDateString(),
                 Description = workout.Description,
                 Distance    = workout.Distance,
                 Duration    = workout.Duration,
                 Emotion     = workout.Emotion,
                 Id          = workout.Id,
                 Title       = workout.Title,
                 PortalKey   = workout.PortalKey,
                 UserKey     = workout.UserSetting.UserKey,
                 Weather     = workout.Weather,
                 DataSrc     = workout.DataSrc
             };
             ret.Add(json);
         }
         // TODO: need a case for empty data
         JavaScriptSerializer jserializer = new JavaScriptSerializer();
         Response.Write(jserializer.Serialize(ret.ToArray()));
     }
     else
     {
         Response.Write("{ERROR:'invalid request'}");
     }
 }