/// <summary> /// Gets number of steps for current day /// </summary> /// <returns><c>true</c> if steps were successfully fetched, <c>false</c> otherwise</returns> private async Task <bool> GetStepsAsync() { StepCounter stepCounter = null; try { stepCounter = await StepCounter.GetDefaultAsync(); _steps = await stepCounter.GetStepCountForRangeAsync( DateTime.Now.Date, DateTime.Now - DateTime.Now.Date); } catch (Exception e) { _lastError = SenseHelper.GetSenseError(e.HResult); return(false); } finally { if (stepCounter != null) { stepCounter.Dispose(); } } return(true); }
/// <summary> /// Gets number of steps for current day /// </summary> /// <returns><c>true</c> if steps were successfully fetched, <c>false</c> otherwise</returns> private async Task <bool> GetStepsAsync() { // First try the pedometer try { var readings = await Pedometer.GetSystemHistoryAsync(DateTime.Now.Date, DateTime.Now - DateTime.Now.Date); _steps = StepCountData.FromPedometerReadings(readings); return(true); } catch (Exception) { // Continue to the fallback } // Fall back to using Lumia Sensor Core. StepCounter stepCounter = null; try { stepCounter = await StepCounter.GetDefaultAsync(); StepCount count = await stepCounter.GetStepCountForRangeAsync( DateTime.Now.Date, DateTime.Now - DateTime.Now.Date); _steps = StepCountData.FromLumiaStepCount(count); } catch (Exception e) { _lastError = SenseHelper.GetSenseError(e.HResult); return(false); } finally { if (stepCounter != null) { stepCounter.Dispose(); } } return(true); }
/// <summary> /// Loads steps for the given day /// </summary> /// <param name="fetchDate">Date to fetch steps for</param> /// <returns>List of step counts per data point interval for the given day</returns> private async Task <Dictionary <DateTime, StepCount> > LoadDaySteps(DateTime fetchDate) { Dictionary <DateTime, StepCount> daySteps = new Dictionary <DateTime, StepCount>(); try { int totalMinutesPerDay = 24 * 60; int dataPoints = totalMinutesPerDay / DataPointInterval; for (int i = 0; i <= dataPoints; i++) { DateTime fetchFrom = fetchDate.Date + TimeSpan.FromMinutes(i * DataPointInterval); if (fetchFrom > DateTime.Now) { break; } TimeSpan fetchRange = TimeSpan.FromMinutes(DataPointInterval); if ((fetchFrom + fetchRange) > DateTime.Now) { fetchRange = DateTime.Now - fetchFrom; } StepCount stepCount = await _stepCounter.GetStepCountForRangeAsync( fetchFrom, fetchRange ); if (stepCount != null) { daySteps.Add(fetchFrom, stepCount); } } } catch (Exception) { } return(daySteps); }
/// <summary> /// Updates visualization /// </summary> private async Task UpdateScreenAsync() { // clear visual ActivityPanel.Children.Clear(); TimePanel.Children.Clear(); //update labels ZoomLabel.Text = "" + ZOOM_LEVELS[_currentZoomLevel] + " pixel(s)/minute"; DateLabel.Text = _iCurrentDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture); //update date selected DateTime endDate = _iCurrentDate.AddDays(1); if (_iCurrentDate.Date == DateTime.Now.Date) { endDate = DateTime.Now; } // Add time labels and steps(walking+running) count for each hour for (int i = 0; i < 24; i++) { Grid timeBlock = new Grid(); StepCount stepCount = null; // getting steps count try { DateTime fromDate = _iCurrentDate + TimeSpan.FromHours(i); TimeSpan queryLength = TimeSpan.FromHours(1); if ((fromDate + queryLength) > endDate) { queryLength = endDate - fromDate; } stepCount = await _stepCounter.GetStepCountForRangeAsync(fromDate, queryLength); } catch (Exception) { } //updating steps count for each hour in visualizer TextBlock label = new TextBlock(); label.Height = ZOOM_LEVELS[_currentZoomLevel] * 60.0 * BASE_DRAW_SCALE; label.FontSize = 14.0; if (stepCount != null) { label.Text = String.Format( "{0:00}:00\n{1}/{2}", i, stepCount.WalkingStepCount, stepCount.RunningStepCount ); } else { label.Text = String.Format("{0:00}:00", i); } label.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top; timeBlock.Children.Add(label); //creating time(hour) intervel blocks Rectangle divider = new Rectangle(); divider.Width = 200.0; divider.Height = 0.5; divider.Fill = new SolidColorBrush(Colors.Gray); divider.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top; timeBlock.Children.Add(divider); TimePanel.Children.Add(timeBlock); } // Add activities for each hour in a day IList <ActivityMonitorReading> activities = null; if (await CallSenseApiAsync(async() => { activities = await _activityMonitor.GetActivityHistoryAsync(_iCurrentDate, endDate - _iCurrentDate); })) { if (activities.Count >= 2) { ActivityMonitorReading previousReading = activities[0]; // if first activity started few minutes after the day started then Add filler if needed if (previousReading.Timestamp > _iCurrentDate) { AppendActivityBarBlock(Colors.Transparent, previousReading.Timestamp - _iCurrentDate); } // Add activities for (int i = 1; i < activities.Count; i++) { ActivityMonitorReading reading = activities[i]; TimeSpan activityLength = reading.Timestamp - previousReading.Timestamp; // if first activity started before the day started then cut off any excess if (previousReading.Timestamp < _iCurrentDate) { activityLength -= (_iCurrentDate - previousReading.Timestamp); } AppendActivityBarBlock(ACTIVITY_COLORS[previousReading.Mode], activityLength); previousReading = reading; } // Show also current activity AppendActivityBarBlock(ACTIVITY_COLORS[previousReading.Mode], endDate - previousReading.Timestamp); } // Scroll to present/current time ActivityScroller.UpdateLayout(); double scrollTo = (ZOOM_LEVELS[_currentZoomLevel] * (endDate - _iCurrentDate).TotalMinutes * BASE_DRAW_SCALE) - 400.0; if (scrollTo < 0.0) { scrollTo = 0.0; } ActivityScroller.ChangeView(null, scrollTo, null); } else { MessageDialog dlg = new MessageDialog("Failed to fetch activities"); await dlg.ShowAsync(); } }