private void GlucoseUnit_Changed(object sender, EventArgs e) { var isMmol = this.btnUnitsMMOL.Checked; if (isMmol) { if (this.numUrgentHigh.Value >= 36) { this.numUrgentHigh.Value = GlucoseMath.ToMmol(this.numUrgentHigh.Value); } if (this.numHigh.Value >= 36) { this.numHigh.Value = GlucoseMath.ToMmol(this.numHigh.Value); } if (this.numLow.Value >= 36) { this.numLow.Value = GlucoseMath.ToMmol(this.numLow.Value); } if (this.numUrgentLow.Value >= 36) { this.numUrgentLow.Value = GlucoseMath.ToMmol(this.numUrgentLow.Value); } } else { if (this.numUrgentHigh.Value < 36) { this.numUrgentHigh.Value = GlucoseMath.ToMgdl(this.numUrgentHigh.Value); } if (this.numHigh.Value < 36) { this.numHigh.Value = GlucoseMath.ToMgdl(this.numHigh.Value); } if (this.numLow.Value < 36) { this.numLow.Value = GlucoseMath.ToMgdl(this.numLow.Value); } if (this.numUrgentLow.Value < 36) { this.numUrgentLow.Value = GlucoseMath.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("Start Trying to refresh data"); var endpoint = PluginLoader.Instance.GetActivePlugin(); var name = endpoint.DataSourceShortName; WriteDebug($"Data will be fetched via plugin: {name}"); if (AppShared.IsShowingSettings) { //avoid further loading of glucose values if the user has settings view open // the user will probably select new settings anyway.. WriteDebug("Could not refresh data, settingsform is modally open..!"); return; } 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); } WriteDebug("Refreshed, but got stale data"); return; } } string arrow = data.DirectionArrow(); if (endpoint.PluginHandlesFormatting) { var result = endpoint.HandleFormatting(); this.lblGlucoseValue.Text = result[0]; this.lblLastUpdate.Text = result[1]; this.lblDelta.Text = result[2]; this.notifyIcon1.Text = result[3]; } else { //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 = GlucoseMath.GetGlucoseAlarmStatus((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 GlucoseAlarmStatusEnum.UrgentHigh: case GlucoseAlarmStatusEnum.UrgentLow: setLabelsColor(Color.Red); alarmManger.PlayGlucoseAlarm(); break; case GlucoseAlarmStatusEnum.Low: case GlucoseAlarmStatusEnum.High: setLabelsColor(Color.Yellow); alarmManger.PlayGlucoseAlarm(); break; case GlucoseAlarmStatusEnum.Unknown: case GlucoseAlarmStatusEnum.Normal: default: alarmManger.StopAlarm(); setLabelsColor(Color.Green); break; } //this.BackgroundImage = this.renderGlucoseChart(Color.Black, Color.DarkCyan); } } catch (FileNotFoundException ex) { this.SetErrorState(ex); //will only happen during debugging, when the allow file:/// scheme is set this.showErrorMessage($"Could not find file '{ex.FileName}'!"); return; } catch (IOException ex) { this.SetErrorState(ex); } catch (System.Threading.Tasks.TaskCanceledException ex) { // This might happen when a datasource attempts to get data from a remote location, // but for some reason that fails 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 (InvalidOperationException ex) { //might happen if json structure is correectly formed, but is missing data elements 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 (NoPluginChosenException) { //this will happen on first run, as there is no default set plugin anymore this.WriteDebug("No plugin is chosen"); 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 (ConfigValidationException ex) { this.SetErrorState(ex); this.showErrorMessage(ex.Message); this.settingsForm.ShowDialogIfNonVisible(); } /* catch (Exception ex) * { * var msg = "An unknown error occurred 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"; WriteDebug("End Trying to refresh data"); }