Пример #1
0
        /*
         * @param exp_sleep_start_time - The time when the user pressed to start sleep tracking. The time in DateTime format
         * @after: liveIdCredentials.Last_Sleep_Segment field updated
         */
        public static async Task getLastSleepActivity(object sender, EventArgs e, DateTime exp_sleep_start_time)
        {
            //create URL to send
            UriBuilder uri   = new UriBuilder("https://api.microsofthealth.net/v1/me/Activities/");
            var        query = new StringBuilder();

            query.AppendFormat("startTime={0}", Uri.EscapeDataString(exp_sleep_start_time.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'")));
            //query.AppendFormat("&activityTypes={0}", Uri.EscapeDataString("sleep"));
            query.AppendFormat("&ActivityIncludes={0}", Uri.EscapeDataString("Details"));
            uri.Query = query.ToString();

            //sending URL and waiting for response code
            var httpClient         = new HttpClient();
            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, uri.Uri);

            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", LiveIdCredentials.AccessToken);
            var httpResponse = httpClient.SendAsync(httpRequestMessage);

            //converting the response to JSON format
            var stringResponse = httpResponse.Result.Content.ReadAsStringAsync().Result;

            if (stringResponse != null)
            {
                ActivityResponse activityResponse = JsonConvert.DeserializeObject <ActivityResponse>(stringResponse);
                if (activityResponse != null && activityResponse.sleepActivities != null)
                {
                    LiveIdCredentials.Last_Sleep_Segment = activityResponse.sleepActivities.Last().activitySegments.Last();
                    LiveIdCredentials.SleepStart         = activityResponse.sleepActivities.Last().fallAsleepTime;
                }
            }
        }
Пример #2
0
        public static async Task getSleepStatsOfUser(object sender, EventArgs e)
        {
            //create URL to send
            UriBuilder uri   = new UriBuilder("https://api.microsofthealth.net/v1/me/Activities/");
            var        query = new StringBuilder();

            query.AppendFormat("startTime={0}", Uri.EscapeDataString((new DateTime(1990, 1, 1)).ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'")));
            query.AppendFormat("&activityTypes={0}", Uri.EscapeDataString("sleep"));
            uri.Query = query.ToString();

            //sending URL and waiting for response code
            var httpClient         = new HttpClient();
            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, uri.Uri);

            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", LiveIdCredentials.AccessToken);
            var httpResponse = httpClient.SendAsync(httpRequestMessage);

            //converting the response to JSON format
            var stringResponse = httpResponse.Result.Content.ReadAsStringAsync().Result;

            if (stringResponse != null)
            {
                ActivityResponse activityResponse = JsonConvert.DeserializeObject <ActivityResponse>(stringResponse);
                if (activityResponse != null && activityResponse.sleepActivities != null)
                {
                    int count  = 0;
                    int sumEff = 0;
                    int sumWA  = 0;
                    foreach (SleepActivity sleepActivity in activityResponse.sleepActivities)
                    {
                        sumEff += sleepActivity.sleepEfficiencyPercentage;
                        StatisticsPage.sleepEfficiencyAcrossTime.Add(sleepActivity.sleepEfficiencyPercentage);
                        sumWA += sleepActivity.numberOfWakeups;
                        StatisticsPage.wakeUpsAcrossTime.Add(sleepActivity.numberOfWakeups);
                        count++;
                    }
                    if (count > 0)//prevent div by zero
                    {
                        LiveIdCredentials.mean_sleep_efficience = sumEff / count;
                        LiveIdCredentials.mean_num_of_wakeups   = sumWA / count;
                    }
                }
            }
        }
Пример #3
0
        public static async Task getSleepSegmentsStats(object sender, EventArgs e, DateTime last_updated_time_of_segments_stats)
        {
            //create URL to send
            UriBuilder uri   = new UriBuilder("https://api.microsofthealth.net/v1/me/Activities/");
            var        query = new StringBuilder();

            query.AppendFormat("startTime={0}", Uri.EscapeDataString(last_updated_time_of_segments_stats.AddDays(1).ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'")));
            query.AppendFormat("&activityTypes={0}", Uri.EscapeDataString("sleep"));
            query.AppendFormat("&ActivityIncludes={0}", Uri.EscapeDataString("Details"));
            uri.Query = query.ToString();

            //sending URL and waiting for response code
            var httpClient         = new HttpClient();
            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, uri.Uri);

            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", LiveIdCredentials.AccessToken);
            var httpResponse = httpClient.SendAsync(httpRequestMessage);

            //converting the response to JSON format
            var stringResponse = httpResponse.Result.Content.ReadAsStringAsync().Result;

            if (stringResponse != null)
            {
                ActivityResponse activityResponse = JsonConvert.DeserializeObject <ActivityResponse>(stringResponse);
                if (activityResponse != null && activityResponse.sleepActivities != null)
                {
                    foreach (SleepActivity sleepActivity in activityResponse.sleepActivities)
                    {
                        if (minFromDur(sleepActivity.duration) > 30)//statistics from real sleep activities - 30+ minutes
                        {
                            for (int i = 0; i < sleepActivity.activitySegments.Count; i++)
                            {
                                int curStage  = 0;
                                int nextStage = 0;
                                if (sleepActivity.activitySegments[i].segmentType == "Awake")
                                {
                                    curStage = (int)SegmentStage.Awake;
                                }
                                if (sleepActivity.activitySegments[i].segmentType == "Doze")
                                {
                                    curStage = (int)SegmentStage.Doze;
                                }
                                if (sleepActivity.activitySegments[i].segmentType == "Snooze")
                                {
                                    curStage = (int)SegmentStage.Snooze;
                                }
                                if (sleepActivity.activitySegments[i].segmentType == "Sleep")
                                {
                                    if (sleepActivity.activitySegments[i].segmentType == "Sleep")
                                    {
                                        if (sleepActivity.activitySegments[i].sleepType == "RestlessSleep")
                                        {
                                            curStage = (int)SegmentStage.RestlessSleep;
                                        }
                                        if (sleepActivity.activitySegments[i].sleepType == "RestfulSleep")
                                        {
                                            curStage = (int)SegmentStage.RestfulSleep;
                                        }
                                    }
                                }
                                if (i < (sleepActivity.activitySegments.Count - 1))
                                {
                                    if (sleepActivity.activitySegments[i + 1].segmentType == "Awake")
                                    {
                                        nextStage = (int)SegmentStage.Awake;
                                    }
                                    if (sleepActivity.activitySegments[i + 1].segmentType == "Doze")
                                    {
                                        nextStage = (int)SegmentStage.Doze;
                                    }
                                    if (sleepActivity.activitySegments[i + 1].segmentType == "Snooze")
                                    {
                                        nextStage = (int)SegmentStage.Snooze;
                                    }
                                    if (sleepActivity.activitySegments[i + 1].segmentType == "Sleep")
                                    {
                                        if (sleepActivity.activitySegments[i + 1].segmentType == "Sleep")
                                        {
                                            if (sleepActivity.activitySegments[i + 1].sleepType == "RestlessSleep")
                                            {
                                                nextStage = (int)SegmentStage.RestlessSleep;
                                            }
                                            if (sleepActivity.activitySegments[i + 1].sleepType == "RestfulSleep")
                                            {
                                                nextStage = (int)SegmentStage.RestfulSleep;
                                            }
                                        }
                                    }
                                }
                                switch (curStage)
                                {
                                case (int)SegmentStage.Awake:
                                    SegmentSummaryTable.Awake.countTimes++;
                                    SegmentSummaryTable.Awake.totalDuration += minFromDur(sleepActivity.activitySegments[i].duration);
                                    increaseNextSegmentCounter(curStage, nextStage);
                                    break;

                                case (int)SegmentStage.Doze:
                                    SegmentSummaryTable.Doze.countTimes++;
                                    SegmentSummaryTable.Doze.totalDuration += minFromDur(sleepActivity.activitySegments[i].duration);
                                    increaseNextSegmentCounter(curStage, nextStage);
                                    break;

                                case (int)SegmentStage.Snooze:
                                    SegmentSummaryTable.Snooze.countTimes++;
                                    SegmentSummaryTable.Snooze.totalDuration += minFromDur(sleepActivity.activitySegments[i].duration);
                                    increaseNextSegmentCounter(curStage, nextStage);
                                    break;

                                case (int)SegmentStage.RestlessSleep:
                                    int isREM = 0;
                                    //for REM sleep:
                                    //the segmented started later then after 90 minutes from falling asleep
                                    if (DateTime.Compare(sleepActivity.activitySegments[i].startTime, sleepActivity.fallAsleepTime.AddMinutes(90)) >= 0)
                                    {
                                        //if the segment is longer then 40 minutes - good possibility to REM sleep
                                        if (minFromDur(sleepActivity.activitySegments[i].duration) > 40)
                                        {
                                            isREM = 1;
                                            SegmentSummaryTable.RestlessSleep.countTimes++;
                                            SegmentSummaryTable.RestlessSleep.totalDuration += 40;
                                            SegmentSummaryTable.RestlessSleep.timesToREM++;
                                            SegmentSummaryTable.REMSleep.countTimes++;
                                            SegmentSummaryTable.REMSleep.totalDuration += minFromDur(sleepActivity.activitySegments[i].duration);
                                            increaseNextSegmentCounter((int)SegmentStage.REM, nextStage);
                                        }
                                    }
                                    if (isREM == 0)
                                    {
                                        SegmentSummaryTable.RestlessSleep.countTimes++;
                                        SegmentSummaryTable.RestlessSleep.totalDuration += minFromDur(sleepActivity.activitySegments[i].duration);
                                        increaseNextSegmentCounter(curStage, nextStage);
                                    }
                                    break;

                                case (int)SegmentStage.RestfulSleep:
                                    SegmentSummaryTable.RestfulSleep.countTimes++;
                                    SegmentSummaryTable.RestfulSleep.totalDuration += minFromDur(sleepActivity.activitySegments[i].duration);
                                    increaseNextSegmentCounter(curStage, nextStage);
                                    break;
                                }
                            }
                        }
                    }
                    SegmentSummaryTable.userID      = LiveIdCredentials.userId;
                    SegmentSummaryTable.lastUpdated = activityResponse.sleepActivities.Last().dayId;
                }
            }
        }