Example #1
0
 // c'tor
 private WeatherData(WeatherData obj)
 {
     this.clouds = obj.clouds;
     this.cod = obj.cod;
     this.coord = obj.coord;
     this.dt = obj.dt;
     this.id = obj.id;
     this.main = obj.main;
     this.name = obj.name;
     this.rain = obj.rain;
     this.sys = obj.sys;
     this.weather = obj.weather;
     this.wind = obj.wind;
 }
Example #2
0
 public static WeatherData GetInstance(WeatherData obj)
 {
     if (instance == null)
     {
         instance = obj == null ? new WeatherData() : new WeatherData(obj);
     }
     return instance;
 }
Example #3
0
        // get Weather Data by Location (Long, Lat)
        public WeatherData GetWeatherData(WeatherDataEnum platform)
        {
            var json_data = string.Empty;

            if (platform != WeatherDataEnum.OPEN_WEATHER_MAP)
            {
                throw new WeatherDataServiceException("Unrecognized Enum Type. Try to use OPEN_WEATHER_MAP");
            }

            using (var w = new WebClient())
            {
                // attempt to download JSON data as a string
                try
                {
                    string url = "http://api.openweathermap.org/data/2.5/weather?lat=" + this.location.latitude.ToString() + "&lon=" + this.location.longitude.ToString();
                    Console.WriteLine(url);
                    json_data = w.DownloadString(url);
                    // if string with JSON data is not empty, deserialize it to class and return its instance
                    return(!string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject <WeatherData>(json_data) : WeatherData.GetInstance(null));
                    //return this.GetWeather();
                }
                catch (WeatherDataServiceException e)
                {
                    Console.WriteLine("Error : " + e.StackTrace);
                }
            }

            return(null);
        }