Esempio n. 1
0
        private List <DaysWeatherModel> GetListDaysWeather(ForecastWeather forecastWeather)
        {
            List <DaysWeatherModel> days = new List <DaysWeatherModel>();

            List <WeatherInfo> templateList = forecastWeather.List;
            var daysWeathers = templateList.GroupBy(x => x.Dt_txt.Date);

            foreach (var group in daysWeathers)
            {
                DaysWeatherModel day = new DaysWeatherModel();
                day.WeatherInfos = new List <PeriodWeatherModel>();

                day.Date = group.Key;
                day.WeatherInfos.Add(group.Where(item => item.Dt_txt.Hour <= 6).Select(period => GetPeriodWeatherModel(PartsOfDay.Night, period)).FirstOrDefault());
                day.WeatherInfos.Add(group.Where(item => item.Dt_txt.Hour >= 7 && item.Dt_txt.Hour <= 11).Select(period => GetPeriodWeatherModel(PartsOfDay.Morning, period)).FirstOrDefault());
                day.WeatherInfos.Add(group.Where(item => item.Dt_txt.Hour >= 12 && item.Dt_txt.Hour <= 18).Select(period => GetPeriodWeatherModel(PartsOfDay.Day, period)).FirstOrDefault());
                day.WeatherInfos.Add(group.Where(item => item.Dt_txt.Hour >= 19 && item.Dt_txt.Hour <= 24).Select(period => GetPeriodWeatherModel(PartsOfDay.Evening, period)).FirstOrDefault());

                for (int i = 3; i >= 0; i--)
                {
                    if (day.WeatherInfos[i] == null)
                    {
                        day.WeatherInfos.Remove(day.WeatherInfos[i]);
                    }
                }
                days.Add(day);
            }

            return(days);
        }
Esempio n. 2
0
 public static ForecastWeatherDto ToDto(this ForecastWeather source, string imageUrlTemplate, string type)
 {
     return(new ForecastWeatherDto
     {
         ConsolidatedWeather = source.ConsolidatedWeather.ToDtos(imageUrlTemplate, type),
         RetrievedAt = $"{source.Time.ToLongTimeString()} on {source.Time.ToShortDateString()}",
         Title = source.Title,
     });
 }
        public ForecastWeather ProcessForecast(dynamic apiData)
        {
            DateTime currentDT = apiData.list[0].dt_txt;
            DateTime targetDT  = currentDT.AddDays(1);
            int      counter   = 0;

            while (currentDT.Date.Day != targetDT.Date.Day)
            {
                currentDT = currentDT.AddHours(3);
                counter++;
            }
            counter = counter + 4;//Starting Index for forecast
            ForecastWeather forecast = new ForecastWeather();

            forecast.Temperature     = new decimal[5];
            forecast.Min_Temperature = new decimal[5];
            forecast.Max_Temperature = new decimal[5];
            forecast.Humidity        = new decimal[5];
            forecast.Pressure        = new decimal[5];
            forecast.WindSpeed       = new decimal[5];
            forecast.Description     = new string[5];
            forecast.Icon            = new string[5];
            forecast.DateStamp       = new DateTime[5];
            forecast.DayOfTheWeek    = new string[5];
            for (int i = 0; i < 5; i++)
            {
                if (i == 4 && counter > 39)
                {
                    //reset counter to take last instance
                    counter = 39;
                }
                forecast.Temperature[i]     = apiData.list[counter].main.temp - 273.15;     //Convert to C;
                forecast.Min_Temperature[i] = apiData.list[counter].main.temp_min - 273.15; //Convert to C;
                forecast.Max_Temperature[i] = apiData.list[counter].main.temp_max - 273.15; //Convert to C;
                forecast.Humidity[i]        = apiData.list[counter].main.humidity;
                forecast.Pressure[i]        = apiData.list[counter].main.pressure;
                forecast.WindSpeed[i]       = apiData.list[counter].wind.speed;
                forecast.Description[i]     = apiData.list[counter].weather[0].description;
                forecast.Icon[i]            = apiData.list[counter].weather[0].icon;
                forecast.DateStamp[i]       = apiData.list[counter].dt_txt;
                forecast.DayOfTheWeek[i]    = forecast.DateStamp[i].DayOfWeek.ToString();
                counter = counter + 8;
            }
            return(forecast);
        }
Esempio n. 4
0
        //private async void SaveDbAsync<T>(List<T> results, CancellationToken token)
        //{
        //    using(var session = dbContext.SessionFactory.OpenStatelessSession())
        //    using(var transaction = session.BeginTransaction())
        //    {


        //        foreach (T responseWeather in results)
        //        {
        //            await session.InsertAsync(responseWeather, token);
        //        }
        //        await transaction.CommitAsync(token);
        //    }

        //}

        private IEnumerable <ForecastWeather> ConvertForecastWether(int siteId, ResponseForecast responseForecast)
        {
            foreach (ResponseBase responseWeather in responseForecast.list)
            {
                ForecastWeather currentweather = new ForecastWeather();
                currentweather.Clouds = responseWeather.clouds?.all;

                if (responseWeather.weather.Count > 0)
                {
                    Weather weather = responseWeather.weather[0];
                    currentweather.Code        = weather.id;
                    currentweather.Description = weather.description;
                    currentweather.Main        = weather.main;
                }
                if (responseWeather.wind != null)
                {
                    currentweather.Winddeg   = responseWeather.wind.deg;
                    currentweather.Windspeed = responseWeather.wind.speed;
                }
                if (responseWeather.rain != null)
                {
                    currentweather.Rain1h = responseWeather.rain.Rain1h;
                    currentweather.Rain3h = responseWeather.rain.Rain3h;
                }
                if (responseWeather.snow != null)
                {
                    currentweather.Snow1h = responseWeather.snow.Snow1h;
                    currentweather.Snow3h = responseWeather.snow.Snow3h;
                }

                currentweather.Temperature     = responseWeather.main.temp - 273.15f; //섭씨계산
                currentweather.Lowtemperature  = responseWeather.main.temp_min - 273.15f;
                currentweather.Hightemperature = responseWeather.main.temp_max - 273.15f;
                currentweather.Pressure        = responseWeather.main.pressure;
                currentweather.Humidity        = responseWeather.main.humidity;
                currentweather.Createts        = ToDateTime(responseWeather.dt);
                currentweather.Siteid          = siteId;
                yield return(currentweather);
            }
        }
Esempio n. 5
0
        public async Task <List <DaysWeatherModel> > GetForecastWeatherByCity(int id)
        {
            try
            {
                string templateUrl = $"{Constants.ApiUrl}/forecast?id={id}&appid={Constants.ApiKey}";
                var    degreeType  = await SecureStorage.GetAsync("DegreeType");

                if (degreeType == DegreeType.Celsius.ToString())
                {
                    templateUrl = $"{templateUrl}&units=metric";
                }
                ForecastWeather response = await _httpClientHelper.PerformGet <ForecastWeather>(templateUrl);

                List <DaysWeatherModel> listDays = GetListDaysWeather(response);
                return(listDays);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(null);
            }
        }
Esempio n. 6
0
 /// <summary>
 /// ToString override
 /// </summary>
 /// <returns>string</returns>
 public override string ToString()
 {
     return(ForecastLastupdate.ToString() + "\n\n" + ForecastLocation.ToString() + "\n" + ForecastWeather.ToString() + "\n" + ForecastSunRise.ToString() + "\n" + ForecastTemperature.ToString() + "\n" + ForecastHumidity + "\n" +
            ForecastPressure.ToString() + "\n" + ForecastWindSpeed.ToString() + "\n" + ForecastWindDirection.ToString() + "\n" + ForecastClouds.ToString() + "\n" + ForecastPrecipitation.ToString());
 }
Esempio n. 7
0
        public async Task <IActionResult> Index(string state, string municipality, string station)
        {
            try
            {
                // Set the parameters
                ViewBag.s       = state ?? string.Empty;
                ViewBag.m       = municipality ?? string.Empty;
                ViewBag.w       = station ?? string.Empty;
                ViewBag.Section = SectionSite.Climate;

                // Setting data
                SetWS();

                // Searching the weather station, if the parameters don't come, it will redirect a default weather station
                if (string.IsNullOrEmpty(state) || string.IsNullOrEmpty(municipality) || string.IsNullOrEmpty(station))
                {
                    WeatherStationFull wsDefault = DefaultWeatherStation();
                    return(RedirectToAction("Index", new { wsDefault.State, wsDefault.Municipality, wsDefault.Station }));
                }
                WeatherStationFull ws = SearchWS(state, municipality, station);
                ViewBag.ws = ws;

                // Getting the forecast weather information
                RepositoryForecastWeather rFW      = new RepositoryForecastWeather(Root);
                ForecastWeather           forecast = await rFW.SearchAsync(ws.Id);

                // Sending the climate data to the view
                ViewBag.climate_data = forecast.Climate.First().Data;
                // Processing Scenarios data
                List <ForecastScenario> scenario = forecast.Scenario.ToList();
                var             scenario_name    = scenario.Select(p => p.Name).Distinct();
                var             measures         = scenario.SelectMany(p => p.Monthly_Data).SelectMany(p => p.Data).Select(p => p.Measure).Distinct();
                List <Scenario> scenario_list    = new List <Scenario>();
                foreach (var s in scenario)
                {
                    foreach (var md in s.Monthly_Data)
                    {
                        foreach (var da in md.Data)
                        {
                            Scenario i = scenario_list.SingleOrDefault(p => p.Measure == da.Measure && p.Year == s.Year && p.Month == md.Month);
                            if (i == null)
                            {
                                scenario_list.Add(new Scenario()
                                {
                                    Measure = da.Measure,
                                    Year    = s.Year,
                                    Month   = md.Month,
                                    Avg     = s.Name == "avg" ? da.Value : 0,
                                    Max     = s.Name == "max" ? da.Value : 0,
                                    Min     = s.Name == "min" ? da.Value : 0
                                });
                            }
                            else
                            {
                                if (s.Name == "avg")
                                {
                                    i.Avg = da.Value;
                                }
                                else if (s.Name == "max")
                                {
                                    i.Max = da.Value;
                                }
                                else if (s.Name == "min")
                                {
                                    i.Min = da.Value;
                                }
                            }
                        }
                    }
                }
                ViewBag.scenario = scenario_list;

                // Getting de historical climate
                RepositoryHistoricalClimate rHC = new RepositoryHistoricalClimate(Root);
                ViewBag.historical = await rHC.SearchAsync(ws.Id);

                // Getting climatology
                RepositoryHistoricalClimatology rHCy = new RepositoryHistoricalClimatology(Root);
                ViewBag.climatology = (await rHCy.SearchAsync(ws.Id)).FirstOrDefault();

                // Settings variables
                ViewBag.weather_vars = new string[] { "prec", "sol_rad", "t_max", "t_min" };

                return(View());
            }
            catch (Exception ex)
            {
                return(View("Error"));
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Method that gets the forecast information
        /// </summary>
        /// <param name="ws">Weather station ID</param>
        /// <returns></returns>
        public async Task <ForecastWeather> SearchAsync(string ws)
        {
            ForecastWeather answer = await Client.GetForecastWeatherAsync(ws);

            return(answer);
        }