Example #1
0
 private async Task ImportTempBasal(TempBasal tempBasal, SQLiteConnection conn)
 {
     await ExecuteNonQuery("INSERT INTO tempbasal(time,duration,absolute,percentage) VALUES(@t, @d, @u, @p)",
                           new []
     {
         GetParameter("t", tempBasal.Time),
         GetParameter("d", tempBasal.Duration),
         GetParameter("u", tempBasal.AbsoluteRate),
         GetParameter("p", tempBasal.Percentage)
     }, conn);
 }
Example #2
0
 private void AddTempbasalToTimeline(IntervalCollection <TempBasal> timeline, TempBasal tempBasal)
 {
     if (tempBasal.Duration == 0)
     {
         timeline.Crop(tempBasal.Time);
     }
     else
     {
         timeline.Add(tempBasal.Time, tempBasal.Time.AddMinutes(tempBasal.Duration), tempBasal);
     }
 }
Example #3
0
        public async Task OnGetAsync()
        {
            try
            {
                if (!this.Request.Query.TryGetValue("Hostname", out StringValues hostnameValues) ||
                    hostnameValues.Count != 1 ||
                    !Uri.TryCreate(
                        "https://" + hostnameValues[0],
                        UriKind.Absolute,
                        out Uri baseUri))
                {
                    throw new ArgumentException(
                              "Query parameter 'Hostname' is missing or invalid.",
                              "Hostname");
                }
                ;

                if (!this.Request.Query.TryGetValue("Day", out StringValues dayValues) ||
                    dayValues.Count != 1 ||
                    dayValues[0].Length != 10 ||
                    !DateTimeOffset.TryParseExact(
                        dayValues[0],
                        format: "yyyy-MM-dd",
                        formatProvider: CultureInfo.InvariantCulture,
                        styles: DateTimeStyles.AssumeUniversal,
                        result: out DateTimeOffset parsedDay))
                {
                    throw new ArgumentException(
                              "Query parameter 'Day' is missing or invalid. It must have the format 'yyyy-MM-dd'",
                              "Day");
                }
                ;

                if (!this.Request.Query.TryGetValue("Token", out StringValues tokenValues) ||
                    tokenValues.Count != 1 ||
                    String.IsNullOrWhiteSpace(tokenValues[0]))
                {
                    throw new ArgumentException(
                              "Query parameter 'Token' is missing.",
                              "Token");
                }
                ;

                this.DayQueryParameter = parsedDay.ToString("yyyy-MM-dd");

                for (int i = 0; i < 12 * 24; i++)
                {
                    this.timeLineLabels5M[i] = String.Concat("'", parsedDay.AddMinutes(5 * i).ToString("HH:mm"), "'");
                    this.sgvs[i]             = "NaN";
                    this.mbgvs[i]            = "NaN";
                }

                using (var client = new NightScoutClient(baseUri, tokenValues[0], this.logger))
                {
                    Profile profile = await client.GetDefaultProfileAsync(CancellationToken.None);

                    //TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(profile.)


                    Task <IList <Entry> > entriesTask = client.GetEntriesAsync(
                        CancellationToken.None,
                        from: parsedDay,
                        to: parsedDay.AddDays(1).AddMilliseconds(-1),
                        maxCount: 300);

                    Task <IList <Treatment> > treatmentsTask = client.GetTreatmentsAsync(
                        CancellationToken.None,
                        id: null,
                        from: parsedDay,
                        to: parsedDay.AddDays(1).AddMilliseconds(-1),
                        maxCount: 300);

                    await Task.WhenAll(entriesTask, treatmentsTask);

                    IList <Entry>     entries    = entriesTask.Result;
                    IList <Treatment> treatments = treatmentsTask.Result;

                    decimal suggestedMaxGlucoseValue = 180;

                    SetBloodGlucoseLevels(
                        parsedDay,
                        entries,
                        entryType: "sgv",
                        getGlucoseValue: new Func <Entry, decimal?>(e => e.Sgv),
                        values:  this.sgvs,
                        suggestedMaxGlucoseValue: ref suggestedMaxGlucoseValue);

                    SetBloodGlucoseLevels(
                        parsedDay,
                        entries,
                        entryType: "mbg",
                        getGlucoseValue: new Func <Entry, decimal?>(e => e.Mbg),
                        values: this.mbgvs,
                        suggestedMaxGlucoseValue: ref suggestedMaxGlucoseValue);

                    suggestedMaxGlucoseValue = ((int)((suggestedMaxGlucoseValue * 1.1m) / 10)) * 10 + 10;

                    this.WarningHigh              = Math.Min(240, suggestedMaxGlucoseValue).ToString("###.##");
                    this.CriticalHigh             = suggestedMaxGlucoseValue.ToString("###.##");
                    this.SuggestedMaxGlucoseValue = suggestedMaxGlucoseValue;

                    this.SerumGlucoseValues      = String.Join(',', this.sgvs);
                    this.MeterBloodGlucoseValues = String.Join(',', this.mbgvs);
                    this.TimeLineLabels5M        = new HtmlString(String.Join(',', this.timeLineLabels5M));

                    for (int i = 0; i < 60 * 24; i++)
                    {
                        this.timeLineLabels1M[i] = String.Concat("'", parsedDay.AddMinutes(i).ToString("HH:mm"), "'");
                    }

                    var orderedBasalRates = new List <ProfileDefinition>(profile.BasalRate.OrderBy(br => br.TimeAsSeconds));
                    for (int i = 0; i < orderedBasalRates.Count; i++)
                    {
                        int left = (int)orderedBasalRates[i].TimeAsSeconds / 60;
                        int right;
                        if (i < orderedBasalRates.Count - 1)
                        {
                            right = (int)orderedBasalRates[i + 1].TimeAsSeconds / 60;
                        }
                        else
                        {
                            right = 60 * 24;
                        }

                        for (int n = left; n < right; n++)
                        {
                            this.targetBasalRateValues[n] = orderedBasalRates[i].Value.ToString("##0.##", CultureInfo.InvariantCulture);
                        }
                    }

                    var temporaryBasalRateTreatments = new List <Treatment>(
                        treatments.Where(t => t.EventType == "Temp Basal").OrderBy(t => t.CreatedAt));

                    var tempBasalRates = new List <TempBasal>();

                    for (int i = 0; i < temporaryBasalRateTreatments.Count; i++)
                    {
                        Treatment current = temporaryBasalRateTreatments[i];
                        if (current.Percent == null)
                        {
                            continue;
                        }

                        int start = (int)(DateTimeOffset.Parse(current.CreatedAt, CultureInfo.InvariantCulture) - parsedDay).TotalMinutes;
                        var tbr   = new TempBasal
                        {
                            Start  = start,
                            End    = start + current.Duration.Value,
                            Factor = temporaryBasalRateTreatments[i].Percent.Value / 100,
                        };

                        if (i < temporaryBasalRateTreatments.Count - 1)
                        {
                            Treatment next      = temporaryBasalRateTreatments[i + 1];
                            int       nextStart = (int)(DateTimeOffset.Parse(next.CreatedAt, CultureInfo.InvariantCulture) - parsedDay).TotalMinutes;

                            tbr.End = Math.Min(tbr.End, nextStart);
                        }

                        tempBasalRates.Add(tbr);
                    }

                    for (int i = 0; i < this.targetBasalRateValues.Length; i++)
                    {
                        this.actualBasalRateValues[i] = this.targetBasalRateValues[i];
                    }

                    for (int i = 0; i < tempBasalRates.Count; i++)
                    {
                        for (int n = tempBasalRates[i].Start; n < tempBasalRates[i].End; n++)
                        {
                            decimal targetRate   = Decimal.Parse(this.actualBasalRateValues[n], CultureInfo.InvariantCulture);
                            decimal adjustedRate = targetRate * (1 + tempBasalRates[i].Factor);
                            this.actualBasalRateValues[n] = adjustedRate.ToString("##0.##", CultureInfo.InvariantCulture);
                        }
                    }

                    this.TimeLineLabels1M = new HtmlString(String.Join(',', this.timeLineLabels1M));
                    this.TargetBasalRates = new HtmlString(String.Join(',', this.targetBasalRateValues));
                    this.ActualBasalRates = new HtmlString(String.Join(',', this.actualBasalRateValues));

                    var eventTypes = new HashSet <string>(
                        treatments.Select(t => t.EventType));

                    Treatment dailySummaryTreatment = treatments
                                                      .Where(t => t.EventType == "NightScoutReporterDailySummary")
                                                      .OrderBy(t => t.CreatedAt)
                                                      .LastOrDefault();

                    if (dailySummaryTreatment != null &&
                        !String.IsNullOrWhiteSpace(dailySummaryTreatment.Notes))
                    {
                        this.DailySummary = new HtmlString(String.Join(',', eventTypes));
                    }
                    else
                    {
                        this.DailySummary = new HtmlString("Keine");
                    }

                    List <MealBolusTreatment> mealBoulsTreatments = ToMealBolusTreatments(treatments);
                    var sb = new StringBuilder();

                    if (mealBoulsTreatments.Count > 0)
                    {
                        sb.Append("<table>")
                        .Append("<tr>")
                        .Append("<th width='15%' align='left'>Uhrzeit</th>")
                        .Append("<th width='15%' align='right'>BG (in mg/dl)</th>")
                        .Append("<th width='15%' align='right'>KH (in g)</th>")
                        .Append("<th width='25%' align='right'>IE (+/- Korrektur)</th>")
                        .Append("<th width='15%' align='right'>COB (in g)</th>")
                        .Append("<th width='15%' align='right'>IOB (in IE)</th>")
                        .Append("</tr>");

                        foreach (MealBolusTreatment t in mealBoulsTreatments)
                        {
                            string insulinText = String.Empty;

                            if (t.Insulin != null)
                            {
                                if (t.InsulinCorrection != null)
                                {
                                    insulinText = String.Format(
                                        CultureInfo.InvariantCulture,
                                        "{0} [{1}]",
                                        t.Insulin.Value.ToString("###0.00"),
                                        t.InsulinCorrection.Value.ToString("###0.00"));
                                }
                                else
                                {
                                    insulinText = t.Insulin.Value.ToString("###0.00");
                                }
                            }

                            sb
                            .Append("<tr>")
                            .Append("<td align='left' style='font-weight:bold'>").Append(t.CreatedAt.Substring(11, 8)).Append("</td>")
                            .Append("<td align='right'>")
                            .Append(t.BG != null ? t.BG.Value.ToString("###0") : String.Empty).Append("</td>")
                            .Append("<td align='right'>")
                            .Append(t.Carbs != null ?
                                    t.Carbs.Value.ToString("###0") :
                                    String.Empty).Append("</td>")
                            .Append("<td align='right'>").Append(insulinText).Append("</td>")
                            .Append("<td align='right'>")
                            .Append(t.CarbsOnBoard != null ?
                                    t.CarbsOnBoard.Value.ToString("###0") :
                                    String.Empty).Append("</td>")
                            .Append("<td align='right'>")
                            .Append(t.InsulinOnBoard != null ?
                                    t.InsulinOnBoard.Value.ToString("###0.00") :
                                    String.Empty).Append("</td>")
                            .Append("</tr>");
                        }

                        sb.Append("</table>");
                    }

                    this.Meals = new HtmlString(sb.ToString());
                }
            }
            catch (Exception error)
            {
                this.logger.LogError(error, "Error in OnGet: {0}", error.ToString());
                throw;
            }
        }