Esempio n. 1
0
        public override IEnumerable <DailyIcon> GetDailyIcons()
        {
            try
            {
                JObject iconData = JsonFromUrl(DailyIconURL);
                if (iconData == null)
                {
                    return(null);
                }
                // request to the same "result" to get a collection of DailyIcon objects
                var dailyResult = from day in (JArray)iconData["forecast"]["simpleforecast"]["forecastday"]
                                  where DateTimeFromUnixTimestampSeconds((long)day["date"]["epoch"]) <= DateTime.Today.AddDays(5)
                                  select new DailyIcon
                {
                    forecastFor = DateTimeFromUnixTimestampSeconds((long)day["date"]["epoch"]),
                    source      = this,
                    description = WeatherDescription.GetWeatherDescription(day["conditions"].ToString().ToLower())
                };

                return(dailyResult);
            }
            catch (Exception ex)
            {
                Logger.Log.Error(String.Format("An error occurred while trying to data processing from the source: {0}; in the method : {1}; by the address {2}", Name, "GetDailyIcon", DailyIconURL), ex);
                return(null);
            }
        }
Esempio n. 2
0
        public override IEnumerable <DailyIcon> GetDailyIcons()
        {
            try
            {
                JObject iconData = JsonFromUrl(DailyIconURL);
                if (iconData == null)
                {
                    return(null);
                }

                // request to the same "result of the request to the resource" to get a collection of DailyIcon objects
                var dailyResult = from day in (JArray)iconData["forecast"]["forecastday"]
                                  select new DailyIcon
                {
                    forecastFor = DateTime.ParseExact(day["date"].ToString(), "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    source      = this,
                    description = WeatherDescription.GetWeatherDescription(day["condition"]["text"].ToString().ToLower()),
                };

                return(dailyResult);
            }
            catch (Exception ex)
            {
                Logger.Log.Error(String.Format("An error occurred while trying to data processing from the source: {0}; in the method : {1}; by the address {2}", Name, "GetDailyIcon", DailyIconURL), ex);
                return(null);
            }
        }
Esempio n. 3
0
        public override IEnumerable <DailyIcon> GetDailyIcons()
        {
            try
            {
                JObject iconData = JsonFromUrl(DailyIconURL);

                if (iconData == null)
                {
                    return(null);
                }

                var dailyResult = from forecast in (JArray)iconData["data"]
                                  where DateTime.ParseExact(forecast["datetime"].ToString(), "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture) <= DateTime.Today.AddDays(5)
                                  select new DailyIcon
                {
                    forecastFor = DateTime.ParseExact(forecast["datetime"].ToString(), "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    source      = this,
                    description = WeatherDescription.GetWeatherDescription(forecast["weather"]["description"].ToString().ToLower()),
                };

                return(dailyResult);
            }
            catch (Exception ex)
            {
                Logger.Log.Error(String.Format("An error occurred while trying to data processing from the source: {0}; in the method : {1}; by the address {2}", Name, "GetDailyIcon", DailyIconURL), ex);
                return(null);
            }
        }
Esempio n. 4
0
        public override WeatherCondition GetCurrentWeather()
        {
            dynamic data = JsonFromUrl(CurrentWeatherURL);

            if (data == null)
            {
                return(null);
            }

            WeatherCondition weather = new WeatherCondition();

            try
            {
                weather.airTemperature = data["data"][0]["temp"].Value;
                weather.isDay          = data["data"][0]["pod"].ToString() == "d" ? true : false;
                weather.description    = WeatherDescription.GetWeatherDescription(data["data"][0]["weather"]["description"].Value.ToString().ToLower());
                weather.pressure       = data["data"][0]["slp"].Value * WeatherCondition.mBarInMMGH;
                weather.humidity       = data["data"][0]["rh"].Value;
                weather.windSpeed      = data["data"][0]["wind_spd"].Value;
                weather.windDirection  = new WindDirection(data["data"][0]["wind_dir"].Value, data["data"][0]["wind_cdir"].Value);
                weather.rain           = data["data"][0]["precip"].Value == null ? 0 : data["data"][0]["precip"].Value;
                ((Newtonsoft.Json.Linq.JObject)(data["data"][0])).Values("snow");
                weather.snow  = (data["data"][0]["snow"] != null && data["data"][0]["snow"].Value != null) ? data["data"][0]["snow"].Value : 0;
                weather.cloud = int.Parse(data["data"][0]["clouds"].ToString());
                weather.date  = WeatherSource.DateTimeFromUnixTimestampSeconds((long)data["data"][0]["ts"].Value);

                return(weather);
            }
            catch (Exception ex)
            {
                Logger.Log.Error(String.Format("An error occurred while trying to get current weather from the source: {0}; in the method : {1}; by the address {2}", Name, "GetCurrentWeather", CurrentWeatherURL), ex);
                return(null);
            }
        }
Esempio n. 5
0
        public override WeatherCondition GetCurrentWeather()
        {
            dynamic data = JsonFromUrl(CurrentWeatherURL);

            if (data == null)
            {
                return(null);
            }

            WeatherCondition weather = new WeatherCondition();

            try
            {
                weather.airTemperature = data["current"]["temp_c"].Value;
                weather.isDay          = data["current"]["is_day"].Value == 1 ? true : false;
                weather.description    = WeatherDescription.GetWeatherDescription(data["current"]["condition"]["text"].ToString().ToLower());
                weather.pressure       = data["current"]["pressure_mb"].Value * WeatherCondition.mBarInMMGH;
                weather.humidity       = data["current"]["humidity"].Value;
                weather.windSpeed      = data["current"]["wind_kph"].Value * WeatherCondition.metersPerSecondInKPH;
                weather.windDirection  = new WindDirection(data["current"]["wind_degree"].Value, data["current"]["wind_dir"].Value);
                weather.rain           = data["current"]["precip_mm"].Value;
                weather.cloud          = int.Parse(data["current"]["cloud"].ToString());
                weather.date           = WeatherSource.DateTimeFromUnixTimestampSeconds((long)data["current"]["last_updated_epoch"].Value);

                return(weather);
            }
            catch (Exception ex)
            {
                Logger.Log.Error(String.Format("An error occurred while trying to get current weather from the source: {0}; in the method : {1}; by the address {2}", Name, "GetCurrentWeather", CurrentWeatherURL), ex);
                return(null);
            }
        }
Esempio n. 6
0
        public override IEnumerable <DailyIcon> GetDailyIcons()
        {
            try
            {
                JObject iconData = JsonFromUrl(DailyIconURL);

                if (iconData == null)
                {
                    return(null);
                }
                var dailyResult = from day in (JArray)iconData["list"]
                                  select new DailyIcon
                {
                    forecastFor = DateTimeFromUnixTimestampSeconds(Convert.ToInt64(day["dt"].ToString())),
                    source      = this,
                    description = WeatherDescription.GetWeatherDescription(day["weather"][0]["description"].ToString().ToLower()),
                };

                return(dailyResult);
            }
            catch (Exception ex)
            {
                Logger.Log.Error(String.Format("An error occurred while trying to data processing from the source: {0}; in the method : {1}; by the address {2}", Name, "GetDailyIcon", DailyIconURL), ex);
                return(null);
            }
        }
Esempio n. 7
0
        public override WeatherCondition GetCurrentWeather()
        {
            dynamic data = JsonFromUrl(CurrentWeatherURL);//JObject.Parse(source);

            if (data == null)
            {
                return(null);
            }

            WeatherCondition weather = new WeatherCondition();

            try
            {
                weather.airTemperature = data["main"]["temp"].Value;
                weather.description    = WeatherDescription.GetWeatherDescription(data["weather"][0]["description"].Value.ToString().ToLower());
                weather.isDay          = data["weather"][0]["icon"].ToString().IndexOf("d") >= 0 ? true : false;
                weather.pressure       = data["main"]["pressure"].Value * WeatherCondition.mBarInMMGH;
                weather.humidity       = data["main"]["humidity"].Value;
                weather.windSpeed      = data["wind"]["speed"].Value;
                weather.windDirection  = data["wind"]["deg"] == null? new WindDirection(): new WindDirection(data["wind"]["deg"].Value);
                weather.rain           = data.SelectToken("rain") == null ? 0.0 : data["rain"].HasValues ? data["rain"]["3h"].Value : 0.0;
                weather.snow           = data.SelectToken("snow") == null ? 0.0 : data["snow"].HasValues ? data["snow"]["3h"].Value : 0.0;
                weather.cloud          = int.Parse(data["clouds"]["all"].ToString());
                weather.date           = WeatherSource.DateTimeFromUnixTimestampSeconds((long)data["dt"].Value);

                return(weather);
            }
            catch (Exception ex)
            {
                Logger.Log.Error(String.Format("An error occurred while trying to get current weather from the source: {0}; in the method : {1}; by the address {2}", Name, "GetCurrentWeather", CurrentWeatherURL), ex);
                return(null);
            }
        }
Esempio n. 8
0
        public override IEnumerable <ForecastFromSource> GetForecasts(DateTime deadline, ref List <DailyIcon> icons)
        {
            IEnumerable <DailyIcon> localIcons = null;

            try
            {
                localIcons = GetDailyIcons();
                JObject data = JsonFromUrl(ForecastURL);//JObject.Parse(source);
                if (data == null || localIcons == null)
                {
                    if (deadline > DateTime.Now)
                    {
                        Thread.Sleep(1000 * 60 * 3);// 3 min
                        return(GetForecasts(deadline, ref icons));
                    }
                    Logger.Log.Debug("In Wunderground deadline " + deadline + " at" + DateTime.Now);
                    return(null);
                }
                ;

                // determine the first time point, which has not already passed. according time zones   2,5,8,11,14,17,20,23
                int hours = DateTime.Now.ToUniversalTime().Hour;//UTC
                if (hours % 3 > 0)
                {
                    hours = hours - hours % 3; // hours are already passed.
                }
                hours += 3;                    // UTC & Nex is +3 hour

                //as this source offers an hourly forecast,  Define an array of dates with time for selecting the forecasts we need.
                var dates = Enumerable.Range(0, CountOfForecasts).Select(x => x * 3).Select(dd => UnixTimestampSecondsFromUTC(DateTime.Now.ToUniversalTime().Date.AddHours(dd + hours))).ToArray();//2017-09-20 00:00

                var threeHourResult = from hour in (data["hourly_forecast"])
                                      where dates.Contains((long)hour["FCTTIME"]["epoch"])
                                      select new ForecastFromSource()
                {
                    forecastCreating = DateTime.Today.AddHours(DateTime.Now.Hour),
                    source           = this,
                    date             = DateTimeFromUnixTimestampSeconds((long)hour["FCTTIME"]["epoch"]),
                    airTemperature   = Convert.ToDouble(hour["temp"]["metric"].ToString()),
                    description      = WeatherDescription.GetWeatherDescription(hour["condition"].ToString().ToLower()),
                    isDay            = hour["icon_url"].ToString().Contains(signOfTheDay) ? false : true,
                    pressure         = Convert.ToDouble(hour["mslp"]["metric"].ToString()) * WeatherCondition.mBarInMMGH,
                    humidity         = Convert.ToDouble(hour["humidity"].ToString()),
                    windSpeed        = Convert.ToDouble(hour["wspd"]["metric"].ToString()) * WeatherCondition.metersPerSecondInKPH,
                    windDirection    = new WindDirection(Convert.ToDouble(hour["wdir"]["degrees"].ToString()), hour["wdir"]["dir"].ToString()),
                    rain             = Convert.ToDouble(hour["qpf"]["metric"].ToString()),             //precipitation in 3 hours
                    snow             = Convert.ToDouble(hour["snow"]["metric"].ToString()),            //precipitation in 3 hours
                    cloud            = Convert.ToInt32(hour["sky"].ToString())
                };

                if (threeHourResult.Count() == CountOfForecasts)
                {
                    icons.AddRange(localIcons);
                    return(threeHourResult);
                }
                else
                {
                    Logger.Log.Debug("In Wunderground " + threeHourResult.Count() + " forecasts.");
                    if (deadline > DateTime.Now)
                    {
                        Thread.Sleep(1000 * 60 * 3);// 3 min
                        return(GetForecasts(deadline, ref icons));
                    }
                    Logger.Log.Debug("In Wunderground deadline " + deadline + " at " + DateTime.Now);
                    return(null);
                }
            }
            catch (Exception ex)
            {
                Logger.Log.Debug("In Wunderground ", ex);

                if (deadline > DateTime.Now)
                {
                    Thread.Sleep(1000 * 60 * 3);// 3 min
                    return(GetForecasts(deadline, ref icons));
                }
                Logger.Log.Error(String.Format("An error occurred while trying to data processing from the source: {0}; in the method : {1}; by the address {2}", Name, "GetForecasts", ForecastURL), ex);
                return(null);
            }
        }
Esempio n. 9
0
        public override IEnumerable <ForecastFromSource> GetForecasts(DateTime deadline, ref List <DailyIcon> icons)
        {
            IEnumerable <DailyIcon> localIcons = null;

            try
            {
                localIcons = GetDailyIcons();
                JObject data = JsonFromUrl(ForecastURL);

                if (data == null || icons == null)
                {
                    if (deadline > DateTime.Now)
                    {
                        Thread.Sleep(1000 * 60 * 3);// 3 min
                        return(GetForecasts(deadline, ref icons));
                    }

                    return(null);
                }
                ;
                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                var threeHourResult = from forecast in (JArray)data["data"]
                                      select new ForecastFromSource()
                {
                    forecastCreating = DateTime.Today.AddHours(DateTime.Now.Hour),
                    source           = this,
                    date             = WeatherSource.DateTimeFromUnixTimestampSeconds((long)forecast["ts"]),
                    airTemperature   = Convert.ToDouble(forecast["temp"].ToString()),
                    description      = WeatherDescription.GetWeatherDescription(forecast["weather"]["description"].ToString().ToLower()),
                    isDay            = forecast["pod"].ToString() == "d" ? true : false,
                    pressure         = Convert.ToDouble(forecast["slp"].ToString()) * WeatherCondition.mBarInMMGH,
                    humidity         = Convert.ToDouble(forecast["rh"].ToString()),
                    windSpeed        = Convert.ToDouble(forecast["wind_spd"].ToString()),
                    windDirection    = new WindDirection(Convert.ToDouble(forecast["wind_dir"].ToString()), forecast["wind_cdir"].ToString()),
                    rain             = Convert.ToDouble(forecast["precip"].ToString()),           //precipitation in 3 hours
                    snow             = Convert.ToDouble(forecast["snow"].ToString()),             //precipitation in 3 hours
                    cloud            = Convert.ToInt32(forecast["clouds"].ToString())
                };

                if (threeHourResult.Count() == CountOfForecasts)
                {
                    icons.AddRange(localIcons);
                    return(threeHourResult);
                }
                else
                {
                    if (deadline > DateTime.Now)
                    {
                        Thread.Sleep(1000 * 60 * 3);// 3 min
                        return(GetForecasts(deadline, ref icons));
                    }
                    return(null);
                }
            }
            catch (Exception ex)
            {
                Logger.Log.Error(String.Format("An error occurred while trying to data processing from the source: {0}; in the method : {1}; by the address {2}", Name, "GetForecasts", ForecastURL), ex);

                if (deadline > DateTime.Now)
                {
                    Thread.Sleep(1000 * 60 * 3);// 3 min
                    return(GetForecasts(deadline, ref icons));
                }
                return(null);
            }
        }
Esempio n. 10
0
        public override IEnumerable <ForecastFromSource> GetForecasts(DateTime deadline, ref List <DailyIcon> icons)
        {
            IEnumerable <DailyIcon> localIcons = null;

            try
            {
                localIcons = GetDailyIcons();
                JObject data = JsonFromUrl(ForecastURL);
                if (data == null || icons == null)
                {
                    if (deadline > DateTime.Now)
                    {
                        Thread.Sleep(1000 * 60 * 3);// 3 min
                        return(GetForecasts(deadline, ref icons));
                    }
                    return(null);
                }

                // determine the first time point, which has already passed. according time zones  3,6,9,12,15,18,21,00
                int hours = DateTime.Now.Hour;
                if (hours % 3 > 0)
                {
                    hours = hours - hours % 3;
                }


                //as this source offers an hourly forecast
                //let's define an array of dates with time for selecting the forecasts we need
                var dates = Enumerable.Range(0, CountOfForecasts).Select(x => x * 3).Select(dd => DateTime.Today.AddHours(dd + hours).ToString("yyyy-MM-dd HH:mm")).ToArray();//2017-09-20 00:00

                //make a request to the object - the result of the request to the resource
                //inside the query, create and populate a collection of Forecast objects for a three-hour forecast
                var threeHourResult = from day in (JArray)data["forecast"]["forecastday"]
                                      from hour in (JArray)day["hour"]
                                      where dates.Contains(((JValue)hour["time"]).Value.ToString())
                                      select new ForecastFromSource()
                {
                    forecastCreating = DateTime.Now,
                    source           = this,
                    date             = DateTime.ParseExact(hour["time"].ToString(), "yyyy-MM-dd HH:mm", System.Globalization.CultureInfo.InvariantCulture),
                    airTemperature   = Convert.ToDouble(hour["temp_c"].ToString()),
                    description      = WeatherDescription.GetWeatherDescription(hour["condition"]["text"].ToString().ToLower()),
                    isDay            = hour["is_day"].ToString() == "1" ? true : false,
                    pressure         = Convert.ToDouble(hour["pressure_mb"].ToString()) * WeatherCondition.mBarInMMGH,
                    humidity         = Convert.ToDouble(hour["humidity"].ToString()),
                    windSpeed        = Convert.ToDouble(hour["wind_kph"].ToString()) * WeatherCondition.metersPerSecondInKPH,
                    windDirection    = new WindDirection(Convert.ToDouble(hour["wind_degree"].ToString()), hour["wind_dir"].ToString()),
                    rain             = Convert.ToDouble(hour["precip_mm"].ToString()),             //precipitation in 3 hours
                    cloud            = Convert.ToInt32(hour["cloud"].ToString())
                };
                if (threeHourResult.Count() == CountOfForecasts)
                {
                    icons.AddRange(localIcons);
                    return(threeHourResult);
                }
                return(null);
            }
            catch (Exception ex)
            {
                Logger.Log.Error(String.Format("An error occurred while trying to data processing from the source: {0}; in the method : {1}; by the address {2}", Name, "GetForecasts", ForecastURL), ex);
                return(null);
            }
        }
Esempio n. 11
0
        public override IEnumerable <ForecastFromSource> GetForecasts(DateTime deadline, ref List <DailyIcon> icons)
        {
            IEnumerable <DailyIcon> localIcons = null;

            try
            {
                localIcons = GetDailyIcons();
                JObject data = JsonFromUrl(ForecastURL);
                if (data == null || icons == null)
                {
                    if (deadline > DateTime.Now)
                    {
                        Thread.Sleep(1000 * 60 * 3);// 3 min
                        return(GetForecasts(deadline, ref icons));
                    }
                    return(null);
                }

                var threeHourResult = from day in (JArray)data["list"]
                                      select new ForecastFromSource()
                {
                    forecastCreating = DateTime.Today.AddHours(DateTime.Now.Hour),
                    source           = this,
                    date             = WeatherSource.DateTimeFromUnixTimestampSeconds((long)day["dt"]),
                    airTemperature   = Convert.ToDouble(day["main"]["temp"].ToString()),
                    description      = WeatherDescription.GetWeatherDescription(day["weather"][0]["description"].ToString().ToLower()),
                    isDay            = day["sys"]["pod"].ToString() == "d" ? true : false,
                    pressure         = Convert.ToDouble(day["main"]["sea_level"].ToString()) * WeatherCondition.mBarInMMGH,
                    humidity         = Convert.ToDouble(day["main"]["humidity"].ToString()),
                    windSpeed        = Convert.ToDouble(day["wind"]["speed"].ToString()),
                    windDirection    = new WindDirection(Convert.ToDouble(day["wind"]["deg"].ToString())),
                    rain             = day.SelectToken("rain") == null ? 0.0 : day["rain"].HasValues? Convert.ToDouble(day["rain"]["3h"].ToString()): 0.0,
                    snow             = day.SelectToken("snow") == null ? 0.0 : day["snow"].HasValues ? Convert.ToDouble(day["snow"]["3h"].ToString()) : 0.0,
                    cloud            = Convert.ToInt32(day["clouds"]["all"].ToString())
                };
                if (threeHourResult.Count() == CountOfForecasts)
                {
                    icons.AddRange(localIcons);
                    return(threeHourResult);
                }
                else
                {
                    if (threeHourResult.Count() >= CountOfForecasts - 5)
                    {
                        icons.AddRange(localIcons);
                        return(threeHourResult);
                    }
                    else
                    {
                        if (deadline > DateTime.Now)
                        {
                            Thread.Sleep(1000 * 60 * 3);// 3 min
                            return(GetForecasts(deadline, ref icons));
                        }
                        Logger.Log.Debug("the number of forecasts is less than normal(40-5) and equal  " + threeHourResult.Count());
                        return(null);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log.Error(String.Format("An error occurred while trying to data processing from the source: {0}; in the method : {1}; by the address {2}", Name, "GetForecasts", ForecastURL), ex);

                if (deadline > DateTime.Now)
                {
                    Thread.Sleep(1000 * 60 * 3);    // 3 min
                    return(GetForecasts(deadline, ref icons));
                }
                return(null);
            }
        }