/// <summary>
        /// Gets api response for 7 days weather forecast and converts it into JSON object
        /// </summary>
        private async Task Get7DaysForecast()
        {
            string uri = "https://api.openweathermap.org/data/2.5/onecall?lat=" + _currentForecast.coord.lat + "&lon=" + _currentForecast.coord.lon +
                         "&exclude=current,minutely,hourly&units=metric&appid=" + Credentials.OpenWeatherMapApiKey;

            try
            {
                _7DaysApiResponse = await _httpClient.GetStringAsync(uri);

                _sevenDaysForecast = JsonConvert.DeserializeObject <SevenDaysForecast>(_7DaysApiResponse);
            }
            catch (HttpRequestException e)
            {
                await new MessageDialog(e.Message).ShowAsync();
            }
        }
示例#2
0
        public static async void calcvdAsync(bool showaqi)
        {
            //https://icon-library.net/icon/air-quality-icon-22.html (the air quality icon are from this free website)
            //以下獲取個人記錄
            if (!CrossConnectivity.Current.IsConnected)
            {
                while (true)
                {
                    bool v = await new ContentPage().DisplayAlert("Warning", "You are not connected to the Internet, records cannot be updated", "retry", "cancel");
                    if (!v)
                    {
                        return;
                    }
                    if (CrossConnectivity.Current.IsConnected)
                    {
                        break;
                    }
                }
            }
            try
            {
                double   cvd_idx = double.NaN;
                DB_pdata c;

                using (SQLiteConnection dbc = new SQLiteConnection(DB_persondata))
                {
                    dbc.CreateTable <DB_pdata>();
                    if (dbc.Table <DB_pdata>().ToList().Count == 0)
                    {
                        return;
                    }
                    c = dbc.Table <DB_pdata>().ToList().First();
                }

                //以下假期因素
                bool isholiday = holiday[DateTime.Now.Month - 1].Contains(DateTime.Now.Day);
                //以下空氣污染物
                string res   = await new HttpClient().GetStringAsync("http://www.smg.gov.mo/smg/airQuality/latestAirConcentration.json");
                var    stuff = JsonConvert.DeserializeObject <AirQualityResponse>(res);
                var    aq    = new weatherkey()
                {
                    date        = DateTime.Parse(stuff.datetime),
                    CO_level    = getwwmean(stuff.pohopolu.HE_CO, stuff.enhopolu.HE_CO, stuff.tchopolu.HE_CO, stuff.tghopolu.HE_CO, stuff.cdhopolu.HE_CO, stuff.khhopolu.HE_CO),
                    NO2_level   = getwwmean(stuff.pohopolu.HE_NO2, stuff.enhopolu.HE_NO2, stuff.tchopolu.HE_NO2, stuff.tghopolu.HE_NO2, stuff.cdhopolu.HE_NO2, stuff.khhopolu.HE_NO2),
                    PM_10_level = getwwmean(stuff.pohopolu.HE_PM10, stuff.enhopolu.HE_PM10, stuff.tchopolu.HE_PM10, stuff.tghopolu.HE_PM10, stuff.cdhopolu.HE_PM10, stuff.khhopolu.HE_PM10),
                    PM2_5_level = getwwmean(stuff.pohopolu.HE_PM2_5, stuff.enhopolu.HE_PM2_5, stuff.tchopolu.HE_PM2_5, stuff.tghopolu.HE_PM2_5, stuff.cdhopolu.HE_PM2_5, stuff.khhopolu.HE_PM2_5),
                    O3_level    = getwwmean(stuff.pohopolu.HE_O3, stuff.enhopolu.HE_O3, stuff.tchopolu.HE_O3, stuff.tghopolu.HE_O3, stuff.cdhopolu.HE_O3, stuff.khhopolu.HE_O3),
                    SO2_level   = getwwmean(stuff.pohopolu.HE_SO2, stuff.enhopolu.HE_SO2, stuff.tchopolu.HE_SO2, stuff.tghopolu.HE_SO2, stuff.cdhopolu.HE_SO2, stuff.khhopolu.HE_SO2)
                };
                //以下溫度
                var               tmp         = await new HttpClient().GetStreamAsync("http://www.smg.gov.mo/smg/latestWeather/e_7daysforecast.xml");
                XmlSerializer     serializer  = new XmlSerializer(typeof(SevenDaysForecast));
                SevenDaysForecast items       = (SevenDaysForecast)serializer.Deserialize(tmp);
                var               temps       = items.WeatherItem.WeatherForecast[0].Temperature;
                var               futuretemps = items.WeatherItem.WeatherForecast[1].Temperature;
                double            todaytemp   = (double.Parse(temps.First(x => x.Type == "1").Value) + double.Parse(temps.First(x => x.Type == "2").Value)) / 2;
                double            futuretemp  = (double.Parse(futuretemps.First(x => x.Type == "1").Value) + double.Parse(futuretemps.First(x => x.Type == "2").Value)) / 2;
                //以下計算cvd
                cvd_idx = calcvd(c, todaytemp, aq, isholiday);
                CrossLocalNotifications.Current.Show("CVD Calculated", "Your CVD Index for today is " + (Math.Round(cvd_idx * 10000) / 100).ToString() + "%", 100000);
                //以下求空氣質量指數
                var    web     = new HttpClient();
                string aqilink = await web.GetStringAsync("http://www.smg.gov.mo/smg/airQuality/ho_api_history.json");

                JObject  aqi    = JObject.Parse(aqilink);
                string[] places = new string[] { "en", "tc", "tg", "kh", "po", "cd" };
                var      aqi0   = aqi["sysdate"].ToObject <DateTime>();
                //澳門高密度
                var aqi1 = aqi["en"].Children().ToList().Last().ToObject <AQIVal>().value;
                //澳門路邊
                var aqi2 = aqi["po"].Children().ToList().Last().ToObject <AQIVal>().value;
                //氹仔高密度
                var aqi3 = aqi["tc"].Children().ToList().Last().ToObject <AQIVal>().value;
                //氹仔一般性
                var aqi4 = aqi["tg"].Children().ToList().Last().ToObject <AQIVal>().value;
                //九澳路邊
                var aqi5 = aqi["kh"].Children().ToList().Last().ToObject <AQIVal>().value;
                //路環一般性
                var    aqi6 = aqi["cd"].Children().ToList().Last().ToObject <AQIVal>().value;
                double aqia = getwwmean(aqi1, aqi2, aqi3, aqi4, aqi5, aqi6);
                // if(showaqi)
                CrossLocalNotifications.Current.Show("Air Quality Notice", "The AQI for today is " + aqia.ToString(), 100001);
                using (SQLiteConnection savedt = new SQLiteConnection(everydayinfo))
                {
                    savedt.CreateTable <weatherkey>();
                    savedt.Insert(new weatherkey()
                    {
                        date = DateTime.Now, CVD_idx = cvd_idx, AQI_idx = aqia, todaytemp = todaytemp, futuretemp = futuretemp
                    });
                }
            }catch (Exception EX) {
#if DEBUG
                CrossLocalNotifications.Current.Show("error", EX.Message + " " + EX.Source);
#endif
            }
        }