Ejemplo n.º 1
0
        public SettingsWindow()
        {
            InitializeComponent();

            combobox_loglevel.ItemsSource      = Enum.GetNames <LogEventLevel>();
            combobox_dexcom_server.ItemsSource = typeof(DexcomServerLocation).GetFields().Select(x => (DescriptionAttribute[])x.GetCustomAttributes(typeof(DescriptionAttribute), false)).SelectMany(x => x).Select(x => x.Description);

            if (File.Exists(Program.SettingsFile))
            {
                try
                {
                    var model = FileService <GlucoseTraySettings> .ReadModelFromFile(Program.SettingsFile);

                    if (model is null)
                    {
                        MessageBox.Show("Unable to load existing settings due to a bad file.");
                    }
                    else
                    {
                        txt_dexcom_password.Password  = model.DexcomPassword;
                        txt_nightscout_token.Password = model.AccessToken;
                        if (model.FetchMethod == FetchMethod.DexcomShare)
                        {
                            radio_source_dexcom.IsChecked = true;
                        }
                        else
                        {
                            radio_source_nightscout.IsChecked = true;
                        }

                        if (model.GlucoseUnit == GlucoseUnitType.MG)
                        {
                            radio_unit_mg.IsChecked = true;
                        }
                        else
                        {
                            radio_unit_mmol.IsChecked = true;
                        }

                        combobox_loglevel.SelectedIndex      = (int)model.LogLevel;
                        combobox_dexcom_server.SelectedIndex = (int)model.DexcomServer;

                        Settings = model;
                    }
                }
                catch (Exception e) // Catch serialization errors due to a bad file
                {
                    MessageBox.Show("Unable to load existing settings due to a bad file.  " + e.Message + e.InnerException?.Message);
                }
            }

            DataContext = Settings;
        }
Ejemplo n.º 2
0
        private static string ValidateNightScout(GlucoseTraySettings model)
        {
            if (string.IsNullOrWhiteSpace(model.NightscoutUrl))
            {
                return("Nightscout Url is missing");
            }
            if (!model.NightscoutUrl.StartsWith("https://", StringComparison.OrdinalIgnoreCase) && !model.NightscoutUrl.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
            {
                return("Nightscout URL must start with http:// or https://");
            }
            if (!Uri.TryCreate(model.NightscoutUrl, UriKind.Absolute, out _))
            {
                return("Invalid Nightscout URL");
            }

            try
            {
                var url = $"{model.NightscoutUrl}/api/v1/status.json";
                url += !string.IsNullOrWhiteSpace(model.AccessToken) ? $"?token={model.AccessToken}" : string.Empty;

                using var httpClient = new HttpClient();
                var request = new HttpRequestMessage(HttpMethod.Get, new Uri(url));
                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                var response = httpClient.SendAsync(request).Result;

                if (!response.IsSuccessStatusCode)
                {
                    return("Invalid Nightscout url - Error : " + response.ReasonPhrase);
                }

                var result = response.Content.ReadAsStringAsync().Result;

                try
                {
                    var status = JsonSerializer.Deserialize <Models.NightScoutStatus>(result);
                    return(string.Equals(status.Status, "ok", StringComparison.CurrentCultureIgnoreCase) ? null : "Nightscout status is " + status.Status);
                }
                catch (JsonException)
                {
                    return("Nightscout URL returned invalid staus " + result);
                }
            }
            catch (Exception ex)
            {
                return("Can not access Nightscout : Error " + ex.Message);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// If model is null, will validate from stored settings file.
        /// </summary>
        /// <param name="model"></param>
        public static List <string> ValidateSettings(GlucoseTraySettings model = null)
        {
            var errors = new List <string>();

            if (model is null)
            {
                model = FileService <GlucoseTraySettings> .ReadModelFromFile(Program.SettingsFile);

                if (model is null)
                {
                    errors.Add("File is Invalid");
                    return(errors);
                }
            }

            if (model.FetchMethod == FetchMethod.DexcomShare)
            {
                if (string.IsNullOrWhiteSpace(model.DexcomUsername))
                {
                    errors.Add("Dexcom Username is missing");
                }
                if (string.IsNullOrWhiteSpace(model.DexcomPassword))
                {
                    errors.Add("Dexcom Password is missing");
                }
            }
            else
            {
                string nightScoutError = ValidateNightScout(model);
                if (!String.IsNullOrWhiteSpace(nightScoutError))
                {
                    errors.Add(nightScoutError);
                }
            }
            if (string.IsNullOrWhiteSpace(model.DatabaseLocation))
            {
                errors.Add("Database Location is missing");
            }

            if (!(model.HighBg > model.WarningHighBg && model.WarningHighBg > model.WarningLowBg && model.WarningLowBg > model.LowBg && model.LowBg > model.CriticalLowBg))
            {
                errors.Add("Thresholds overlap ");
            }

            return(errors);
        }
Ejemplo n.º 4
0
 public GlucoseFetchService(IOptions <GlucoseTraySettings> options, ILogger <IGlucoseFetchService> logger, IHttpClientFactory httpClientFactory)
 {
     _logger            = logger;
     _options           = options.Value;
     _httpClientFactory = httpClientFactory;
 }
Ejemplo n.º 5
0
 public IconService(ILogger <IconService> logger, IOptions <GlucoseTraySettings> options)
 {
     _logger  = logger;
     _options = options.Value;
 }