Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
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);
            }
        }
Ejemplo n.º 4
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);
            }
        }
Ejemplo n.º 5
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);
            }
        }