private void GlucoseUnit_Changed(object sender, EventArgs e) { var isMmol = this.btnUnitsMMOL.Checked; if (isMmol) { if (this.numUrgentHigh.Value >= 36) { this.numUrgentHigh.Value = GlucoseStatus.ToMmol(this.numUrgentHigh.Value); } if (this.numHigh.Value >= 36) { this.numHigh.Value = GlucoseStatus.ToMmol(this.numHigh.Value); } if (this.numLow.Value >= 36) { this.numLow.Value = GlucoseStatus.ToMmol(this.numLow.Value); } if (this.numUrgentLow.Value >= 36) { this.numUrgentLow.Value = GlucoseStatus.ToMmol(this.numUrgentLow.Value); } } else { if (this.numUrgentHigh.Value < 36) { this.numUrgentHigh.Value = GlucoseStatus.ToMgdl(this.numUrgentHigh.Value); } if (this.numHigh.Value < 36) { this.numHigh.Value = GlucoseStatus.ToMgdl(this.numHigh.Value); } if (this.numLow.Value < 36) { this.numLow.Value = GlucoseStatus.ToMgdl(this.numLow.Value); } if (this.numUrgentLow.Value < 36) { this.numUrgentLow.Value = GlucoseStatus.ToMgdl(this.numUrgentLow.Value); } } }
// // Main loop. This will be called each 60s and also when the settings are reloaded // private async void LoadGlucoseValue() { IDataSourcePlugin data = null; var alarmManger = SoundAlarm.Instance; var now = DateTime.Now; var alarmsPostponed = alarmManger.GetPostponedUntil(); //cleanup context menu if (alarmsPostponed != null && alarmsPostponed < now) { this.postponedUntilFooToolStripMenuItem.Visible = this.reenableAlarmsToolStripMenuItem.Visible = false; } try { WriteDebug("Trying to refresh data"); var endpoint = PluginLoader.Instance.GetActivePlugin(); var name = endpoint.DataSourceShortName; endpoint.VerifyConfig(Default); data = await endpoint.GetDataSourceDataAsync(this.datasourceLocation); var glucoseDate = data.LocalDate; this.lblLastUpdate.Text = glucoseDate.ToTimeAgo(); // // even if we have glucose data, don't display them if it's considered stale // if (Default.EnableAlarms) { var urgentTime = now.AddMinutes(-Default.AlarmStaleDataUrgent); var warningTime = now.AddMinutes(-Default.AlarmStaleDataWarning); var isUrgent = glucoseDate <= urgentTime; var isWarning = glucoseDate <= warningTime; if (isUrgent || isWarning) { this.lblGlucoseValue.Text = "Stale"; this.lblDelta.Text = "data"; this.notifyIcon1.Text = "Stale data"; alarmManger.PlayStaleAlarm(); if (isUrgent) { setLabelsColor(Color.Red); } else { setLabelsColor(Color.Yellow); } return; } } string arrow = data.DirectionArrow; //mgdl values are always reported in whole numbers this.lblGlucoseValue.Text = Default.GlucoseUnits == "mmol" ? $"{data.Glucose:N1} {arrow}" : $"{data.Glucose:N0} {arrow}"; this.notifyIcon1.Text = "BG: " + this.lblGlucoseValue.Text; var status = GlucoseStatus.GetGlucoseStatus((decimal)data.Glucose); this.lblDelta.Text = data.FormattedDelta + " " + (Default.GlucoseUnits == "mmol" ? "mmol/L" : "mg/dL"); if (Default.EnableRawGlucoseDisplay) { this.lblRawBG.Text = $"{data.RawGlucose:N1}"; } this.SetSuccessState(); switch (status) { case GlucoseStatusEnum.UrgentHigh: case GlucoseStatusEnum.UrgentLow: setLabelsColor(Color.Red); alarmManger.PlayGlucoseAlarm(); break; case GlucoseStatusEnum.Low: case GlucoseStatusEnum.High: setLabelsColor(Color.Yellow); alarmManger.PlayGlucoseAlarm(); break; case GlucoseStatusEnum.Unknown: case GlucoseStatusEnum.Normal: default: alarmManger.StopAlarm(); setLabelsColor(Color.Green); break; } } catch (FileNotFoundException ex) { //will only happen during debugging, when the allow file:/// scheme is set this.showErrorMessage($"Could not find file '{ex.FileName}'!"); this.SetErrorState(ex); return; } catch (IOException ex) { this.SetErrorState(ex); } catch (HttpRequestException ex) { this.SetErrorState(ex); } catch (JsonReaderException ex) { this.SetErrorState(ex); } catch (MissingDataException ex) { //typically happens during azure site restarts this.SetErrorState(ex); } catch (JsonSerializationException ex) { //typically happens during azure site restarts this.SetErrorState(ex); } catch (InvalidJsonDataException ex) { this.SetErrorState(ex); this.showErrorMessage(ex.Message); AppShared.SettingsFormShouldFocusAdvancedSettings = true; this.settingsForm.Visible = false; this.settingsForm.ShowDialogIfNonVisible(); } catch (NoSuchPluginException ex) { var msg = "A datasource plugin was chosen that is no longer available, please choose another datasource: " + ex.Message; this.showErrorMessage(msg); this.settingsForm.ShowDialogIfNonVisible(); } catch (ConfigValidationError ex) { this.showErrorMessage(ex.Message); this.settingsForm.ShowDialogIfNonVisible(); } /* catch (Exception ex) * { * var msg = "An unknown error occured of type " + ex.GetType().ToString() + ": " + ex.Message; * this.showErrorMessage(msg); * Application.Exit(); * }*/ try { if (Default.EnableRawGlucoseDisplay && data != null) { this.lblRawDelta.Text = data.FormattedRawDelta; } } catch (InvalidJsonDataException) { // No data available. // This can happen even if raw glucose is enabled // as it required two data points to be available this.lblRawDelta.Text = "-"; } //these are just for layout tests //this.lblGlucoseValue.Text = "+500.0"; //this.lblRawBG.Text = "+489.5"; //this.lblRawDelta.Text = "+50.0"; //this.lblDelta.Text = "-50.0"; }