Beispiel #1
0
        private async Task <ResponseForecast> RequestForecastWeather(double lat, double lon)
        {
            string apiResponse = "";
            string url         = $"http://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&appid={OpenWeatherMapAppId}&lang=kr";

            try
            {
                /*Calling API http://openweathermap.org/api */
                HttpWebRequest apiRequest =
                    WebRequest.Create(url) as HttpWebRequest;


                using (HttpWebResponse response = apiRequest.GetResponse() as HttpWebResponse)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream());

                    apiResponse = await reader.ReadToEndAsync();

                    //Logging(apiResponse);
                }
                /*End*/

                /*http://json2csharp.com*/
                ResponseForecast rootObject = JsonConvert.DeserializeObject <ResponseForecast>(apiResponse);
                return(rootObject);
            }
            catch (Exception ex)
            {
                Logging(ex);
                Logging("url: " + url);
                Logging("apiResponse: \n" + apiResponse);
                return(null);
            }
        }
Beispiel #2
0
        public async Task UploadingForecastWeather(CancellationToken stoppingToken)
        {
            try
            {
                Logging("Starting collects forecast weather...");

                using (var session = dataAccessor.SessionFactory.OpenStatelessSession())
                    using (var Transaction = session.BeginTransaction())
                    {
                        Logging("Truncate forecast weather on before works");
                        await Clear(session, stoppingToken);

                        Logging("Working to collect forecast weather...(5 days / 3 hours)");
                        var allOfSites = await session.CreateCriteria <Vwcontractorsite>().ListAsync <Vwcontractorsite>();

                        List <ForecastWeather> results = new List <ForecastWeather>();
                        foreach (Vwcontractorsite site in allOfSites)
                        {
                            Logging($"Request Forecast weather at {site.Siteid} ({site.Lng} / {site.Lat})");
                            ResponseForecast weather = await RequestForecastWeather(site.Lng, site.Lat);

                            if (weather == null)
                            {
                                Logging($"Failed to request forecast weather");
                                continue;
                            }
                            IEnumerable <ForecastWeather> currentweathers = ConvertForecastWether(site.Siteid, weather);
                            await SaveOrUpdate(currentweathers, session);

                            stoppingToken.ThrowIfCancellationRequested();
                            if (stoppingToken.IsCancellationRequested)
                            {
                                break;
                            }
                        }
                        await Transaction.CommitAsync();
                    }
            }
            catch (Exception ex)
            {
                Logging(ex);
            }
            finally
            {
                //NextRetriveTime = DateTime.Now.Add(RefreshRate);
                Logging("Completed works.");
            }
        }
Beispiel #3
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);
            }
        }