Exemplo n.º 1
0
 public DataSourceInfo(IDataSourcePlugin plugin)
 {
     this.Type                = plugin.GetType();
     this.Instance            = plugin;
     this.DataSourceShortName = this.Instance.DataSourceShortName;
     this.FullName            = this.Type.FullName;
 }
Exemplo n.º 2
0
 public DataSourceInfo(Type plugin)
 {
     this.Type                = plugin;
     this.Instance            = (IDataSourcePlugin)Activator.CreateInstance(plugin);
     this.DataSourceShortName = this.Instance.DataSourceShortName;
     this.FullName            = plugin.FullName;
 }
 private ListBox GetListBox(IDataSourcePlugin plugin)
 {
     TabPage tabPage = (from TabPage tp in tabControl.TabPages where tp.Tag.ToString() == plugin.Name select tp).FirstOrDefault<TabPage>();
     if (tabPage != null)
     {
         Control listBox = (from Control control in tabPage.Controls where control.GetType() == typeof(ListBox) select control).FirstOrDefault<Control>();
         return (ListBox)listBox;
     }
     return null;
 }
 private void BuildUI(IDataSourcePlugin x)
 {
     TabPage tabPage = new TabPage(x.Name) { Tag = x.Name };
     tabControl.TabPages.Add(tabPage);
     ListBox listBox = new ListBox();
     tabPage.Controls.Add(listBox);
     listBox.Parent = tabPage;
     x.DataSource.Add(string.Format("Hello everyone from {0}...", x.Name));
     listBox.DataSource = x.DataSource;
     listBox.Dock = DockStyle.Fill;
 }
Exemplo n.º 5
0
 public IDataSourcePlugin SetActivePlugin(string dataSourceFullName)
 {
     try
     {
         this.plugin = this.GetAllPlugins().Where((x) => x.FullName == dataSourceFullName).First().Instance;
     }
     catch (InvalidOperationException)
     {
         throw new NoSuchPluginException("Invalid plugin: " + dataSourceFullName);
     }
     return(this.plugin);
 }
        public static string DirectionArrow(this IDataSourcePlugin plugin)
        {
            switch (plugin.Direction)
            {
            case "DoubleUp":
                return("⇈");

            case "SingleUp":
                return("↑");

            case "FortyFiveUp":
                return("↗");

            case "Flat":
                return("→");

            case "FortyFiveDown":
                return("↘");

            case "SingleDown":
                return("↓");

            case "DoubleDown":
                return("⇊");

            case "NOT COMPUTABLE":
                return("-");

            case "OUT OF RANGE":
                return("⇕");

            case "":
                return("");

            default:
            case "None":
                return("⇼");
            }
        }
Exemplo n.º 7
0
        //
        // 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");
        }
        public static void WriteDebug(this IDataSourcePlugin plugin, string line)
        {
            var now = DateTime.Now.ToUniversalTime();

            Debug.WriteLine(now + ":" + line);
        }
 public static string FormattedRawDelta(this IDataSourcePlugin plugin)
 {
     return($"{(plugin.RoundedRawDelta() >= 0.0 ? "+" : "")}{plugin.RoundedRawDelta():N1}");
 }
 public static bool UserWantsMmolUnits(this IDataSourcePlugin plugin) => Default.GlucoseUnits == "mmol";
Exemplo n.º 11
0
        //
        // 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";
        }