Beispiel #1
0
 /// <summary>
 /// Update Current Activity that's cached.
 /// </summary>
 /// <param name="args">Current acitivity value</param>
 public void UpdateCurrentActivity(object args)
 {
     ActivityData <Lumia.Sense.Activity> .Instance().CurrentActivity = (Lumia.Sense.Activity)args;
 }
Beispiel #2
0
        /// <summary>
        /// Update Summary.
        /// </summary>
        /// <returns>Asynchronous task/returns>
        /// <param name="DayOffset">Day Offset</param>
        public async Task UpdateSummaryAsync(uint DayOffset)
        {
            // Read current activity
            ActivityMonitorReading reading = null;

            await CallSensorCoreApiAsync(async() =>
            {
                reading = await _activityMonitor.GetCurrentReadingAsync();
            });

            if (reading != null)
            {
                ActivityData <Lumia.Sense.Activity> .Instance().CurrentActivity = reading.Mode;
            }

            // Fetch activity history for the day
            DateTime startDate = DateTime.Today.Subtract(TimeSpan.FromDays(DayOffset));
            DateTime endDate   = startDate + TimeSpan.FromDays(1);
            IList <ActivityMonitorReading> history = null;

            await CallSensorCoreApiAsync(async() =>
            {
                history = await _activityMonitor.GetActivityHistoryAsync(startDate, TimeSpan.FromDays(1));
            });

            // Create a dictionary to store data
            Dictionary <Activity, TimeSpan> activitySummary = new Dictionary <Activity, TimeSpan>();

            // Initialize timespan for all entries
            var activityTypes = Enum.GetValues(typeof(Activity));

            foreach (var type in activityTypes)
            {
                activitySummary[(Activity)type] = TimeSpan.Zero;
            }

            // Update the timespan for all activities in the dictionary
            if (history.Count > 0)
            {
                Activity currentActivity = history[0].Mode;
                DateTime currentDate     = history[0].Timestamp.DateTime;
                foreach (var item in history)
                {
                    if (item.Timestamp >= startDate)
                    {
                        TimeSpan duration = TimeSpan.Zero;
                        if (currentDate < startDate)
                        {
                            // If first activity of the day started already yesterday, set start time to midnight.
                            currentDate = startDate;
                        }
                        if (item.Timestamp > endDate)
                        {
                            // If last activity extends over to next day, set end time to midnight.
                            duration = endDate - currentDate;
                            break;
                        }
                        else
                        {
                            duration = item.Timestamp - currentDate;
                        }
                        activitySummary[currentActivity] += duration;
                    }
                    currentActivity = item.Mode;
                    currentDate     = item.Timestamp.DateTime;
                }
            }

            // Prepare the summary to add it to data source
            List <ActivityDuration <Lumia.Sense.Activity> > historyList = new List <ActivityDuration <Lumia.Sense.Activity> >();

            foreach (var activityType in activityTypes)
            {
                // For each entry in the summary add the type and duration to data source
                historyList.Add(new ActivityDuration <Lumia.Sense.Activity>((Activity)activityType, activitySummary[(Activity)activityType]));
            }

            // Update the singleton instance of the data source
            ActivityData <Lumia.Sense.Activity> .Instance().History = historyList;

            ActivityData <Lumia.Sense.Activity> .Instance().Date = startDate;
        }
Beispiel #3
0
 /// <summary>
 /// Get singleton instance of ActivityData<Lumia.Sense.Activity>.
 /// </summary>
 public object GetActivityDataInstance()
 {
     return(ActivityData <Lumia.Sense.Activity> .Instance());
 }
Beispiel #4
0
 /// <summary>
 /// Update the current activity that's displayed.
 /// </summary>
 /// <param name="args">Event arguments</param>
 public void UpdateCurrentActivity(object args)
 {
     ActivityData <Windows.Devices.Sensors.ActivityType> .Instance().CurrentActivity = (Windows.Devices.Sensors.ActivityType)args;
 }
Beispiel #5
0
        /// <summary>
        /// Updates the summary in the screen.
        /// </summary>
        /// <returns>Asynchronous task/returns>
        /// <param name="DayOffset">Day offset</param>
        /// <returns>Asyncrhonous Task</returns>
        public async Task UpdateSummaryAsync(uint DayOffset)
        {
            // Read current activity
            ActivitySensorReading reading = await _sensor.GetCurrentReadingAsync();

            if (reading != null)
            {
                ActivityData <Windows.Devices.Sensors.ActivityType> .Instance().CurrentActivity = reading.Activity;
            }

            // Fetch activity history for the day
            DateTime startDate = DateTime.Today.Subtract(TimeSpan.FromDays(DayOffset));
            DateTime endDate   = startDate + TimeSpan.FromDays(1);

            var history = await ActivitySensor.GetSystemHistoryAsync(startDate, TimeSpan.FromDays(1));

            // Create a dictionary to store data
            Dictionary <Windows.Devices.Sensors.ActivityType, TimeSpan> activitySummary = new Dictionary <Windows.Devices.Sensors.ActivityType, TimeSpan>();

            // Initialize timespan for all entries
            var activityTypes = Enum.GetValues(typeof(Windows.Devices.Sensors.ActivityType));

            foreach (var type in activityTypes)
            {
                activitySummary[(Windows.Devices.Sensors.ActivityType)type] = TimeSpan.Zero;
            }

            if (history.Count == 0 || history[0].Timestamp > startDate)
            {
                ActivitySensorReading firstReading = await GetActivityAtAsync(startDate);

                if (firstReading != null)
                {
                    List <ActivitySensorReading> finalHistory = new List <ActivitySensorReading>(history);
                    finalHistory.Insert(0, firstReading);
                    history = finalHistory.AsReadOnly();
                }
            }

            // Update the timespan for all activities in the dictionary
            if (history.Count > 0)
            {
                Windows.Devices.Sensors.ActivityType currentActivity = history[0].Activity;
                DateTime currentDate = history[0].Timestamp.DateTime;
                foreach (var item in history)
                {
                    if (item.Timestamp >= startDate)
                    {
                        TimeSpan duration = TimeSpan.Zero;
                        if (currentDate < startDate)
                        {
                            // If first activity of the day started already yesterday, set start time to midnight.
                            currentDate = startDate;
                        }
                        if (item.Timestamp > endDate)
                        {
                            // If last activity extends over to next day, set end time to midnight.
                            duration = endDate - currentDate;
                            break;
                        }
                        else
                        {
                            duration = item.Timestamp - currentDate;
                        }
                        activitySummary[currentActivity] += duration;
                    }
                    currentActivity = item.Activity;
                    currentDate     = item.Timestamp.DateTime;
                }
            }

            // Prepare the summary to add it to data source
            List <ActivityDuration <Windows.Devices.Sensors.ActivityType> > historyList = new List <ActivityDuration <Windows.Devices.Sensors.ActivityType> >();

            foreach (var activityType in activityTypes)
            {
                // For each entry in the summary add the type and duration to data source
                historyList.Add(new ActivityDuration <Windows.Devices.Sensors.ActivityType>((Windows.Devices.Sensors.ActivityType)activityType, activitySummary[(Windows.Devices.Sensors.ActivityType)activityType]));
            }

            // Update the singleton instance of the data source
            ActivityData <Windows.Devices.Sensors.ActivityType> .Instance().History = historyList;

            ActivityData <Windows.Devices.Sensors.ActivityType> .Instance().Date = startDate;
        }
Beispiel #6
0
 /// <summary>
 /// Get the singleton instance of ActivityData<Windows.Devices.Sensors.ActivityType>.
 /// </summary>
 /// <returns>ActivityData/returns>
 public object GetActivityDataInstance()
 {
     return(ActivityData <Windows.Devices.Sensors.ActivityType> .Instance());
 }