public static async Task <PebbleData> GetNightscoutPebbleDataAsync(string url)
        {
            var    client      = new HttpClient();
            var    pebbleData  = new PebbleData();
            string urlContents = await client.GetStringAsync(url);

            Bg bgs = null;

            //urlContents = "{ \"status\":[{\"now\":1471947452808}],\"bgs\":[],\"cals\":[]";
            //urlContents = "{}"
            var parsed =
                pebbleData.NsData = JsonConvert.DeserializeObject <GeneratedNsData>(urlContents);

            try
            {
                bgs = parsed.bgs.First();
                pebbleData.Direction = bgs.direction;
                pebbleData.Glucose   = Double.Parse(bgs.sgv, NumberStyles.Any, PebbleData.Culture);
                pebbleData.Date      = DateTimeOffset.FromUnixTimeMilliseconds(bgs.datetime).DateTime;
                pebbleData.Delta     = Double.Parse(bgs.bgdelta, NumberStyles.Any, PebbleData.Culture);
            }
            catch (InvalidOperationException ex)
            {
                //this exception might be hit when the nightscout installation is brand new or contains no recent data;
                throw new MissingDataException("No data");
            }

            return(pebbleData);
        }
Exemple #2
0
        public static async Task <PebbleData> GetNightscoutPebbleDataAsync(string uri)
        {
            var    client     = new HttpClient();
            var    pebbleData = new PebbleData();
            string urlContents;

            if (Default.AllowFileURIScheme && uri.ToLower().StartsWith("file:///"))
            {
                using (var reader = File.OpenText(new Uri(uri).LocalPath))
                {
                    urlContents = await reader.ReadToEndAsync();
                }
            }
            else
            {
                urlContents = await client.GetStringAsync(uri);
            }


            Bg bgs = null;

            //urlContents = "{ \"status\":[{\"now\":1471947452808}],\"bgs\":[],\"cals\":[]";
            //urlContents = "{}"
            var parsed =
                pebbleData.NsData = JsonConvert.DeserializeObject <GeneratedNsData>(urlContents);

            try
            {
                bgs = parsed.bgs.First();
                pebbleData.Direction = bgs.direction;
                pebbleData.Glucose   = Double.Parse(bgs.sgv, NumberStyles.Any, PebbleData.Culture);
                pebbleData.Date      = DateTimeOffset.FromUnixTimeMilliseconds(bgs.datetime).DateTime;
                pebbleData.Delta     = Double.Parse(bgs.bgdelta, NumberStyles.Any, PebbleData.Culture);
            }
            catch (InvalidOperationException ex)
            {
                //this exception might be hit when the nightscout installation is brand new or contains no recent data;
                throw new MissingDataException("No data");
            }

            return(pebbleData);
        }