コード例 #1
0
        public List <WeatherData> GetWeatherForcast(Location location, WeatherUnits unit)
        {
            string url = BaseURL + "/data/2.5/forecast?";

            if (location is CityLocation)
            {
                CityLocation loc = (CityLocation)location;
                url += "q=" + loc.cityName;
                if (!System.String.IsNullOrEmpty(loc.countryCode))
                {
                    url += "," + loc.countryCode;
                }
            }
            else if (location is CoordinateLocation)
            {
                CoordinateLocation loc = (CoordinateLocation)location;
                url += "lat=" + loc.lat + "&lon=" + loc.lon;
            }
            else
            {
                throw new WeatherDataServiceException("unsupported location type");
            }
            if (unit == WeatherUnits.Celsius)
            {
                url += "&units=metric";
            }
            else if (unit == WeatherUnits.Fahrenheit)
            {
                url += "&units=imperial";
            }
            else if (unit == WeatherUnits.Kelvin)
            {
            }
            else
            {
                throw new WeatherDataServiceException("unsupported unit type");
            }
            url += "&appid=" + APIKey;

            DataContractJsonSerializer parser = new DataContractJsonSerializer(typeof(WeatherListJson));

            WeatherListJson    json = (WeatherListJson)parser.ReadObject((MakeWebRequest(url)));
            List <WeatherData> data = new List <WeatherData>();

            foreach (WeatherJson j in json.list)
            {
                data.Add(FromJsonToData(j, location, unit));
            }
            return(data);
        }
コード例 #2
0
        public WeatherData GetWeatherData(Location location, WeatherUnits unit)
        {
            string url = BaseURL + "/data/2.5/weather?";

            if (location is CityLocation)
            {
                CityLocation loc = (CityLocation)location;
                url += "q=" + loc.cityName;
                if (!System.String.IsNullOrEmpty(loc.countryCode))
                {
                    url += "," + loc.countryCode;
                }
            }
            else if (location is Ziplocation)
            {
                Ziplocation loc = (Ziplocation)location;
                url += "zip=" + loc.zipCode + "," + loc.countryCode;
            }
            else if (location is CoordinateLocation)
            {
                CoordinateLocation loc = (CoordinateLocation)location;
                url += "lat=" + loc.lat + "&lon=" + loc.lon;
            }
            else
            {
                throw new WeatherDataServiceException("unsupported location type");
            }
            if (unit == WeatherUnits.Celsius)
            {
                url += "&units=metric";
            }
            else if (unit == WeatherUnits.Fahrenheit)
            {
                url += "&units=imperial";
            }
            else if (unit == WeatherUnits.Kelvin)
            {
            }
            else
            {
                throw new WeatherDataServiceException("unsupported unit type");
            }
            url += "&appid=" + APIKey;

            DataContractJsonSerializer parser = new DataContractJsonSerializer(typeof(WeatherJson));
            WeatherJson json = (WeatherJson)parser.ReadObject(MakeWebRequest(url));

            return(FromJsonToData(json, location, unit));
        }
コード例 #3
0
        private WeatherData FromJsonToData(WeatherJson json, Location location, WeatherUnits unit)
        {
            WeatherData data = new WeatherData();

            data.date        = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime().AddSeconds(json.dt);
            data.unit        = unit;
            data.location    = location;
            data.humidity    = json.main.humidity;
            data.temp        = json.main.temp;
            data.windSpeed   = json.wind.speed;
            data.clouds      = json.clouds.all;
            data.status      = json.weather.First().main;
            data.description = json.weather.First().description;
            return(data);
        }
コード例 #4
0
 public async Task <WeatherData> GetWeatherDataByCityIdAsync(string appId, int cityId, WeatherUnits units = WeatherUnits.Standard)
 {
     try
     {
         string url = GetWeatherDataByCityIdUrl(appId, cityId, units);
         return(await service.GetAsync(url));
     }
     catch
     {
         return(null);
     }
 }
コード例 #5
0
 public async Task <WeatherData> GetWeatherDataByCityNameAsync(string appId, string cityName, string countryCode = "", WeatherUnits units = WeatherUnits.Standard)
 {
     try
     {
         string url = GetWeatherDataByCityNameUrl(appId, cityName, countryCode, units);
         return(await service.GetAsync(url));
     }
     catch (Exception ex)
     {
         return(null);
     }
 }
コード例 #6
0
 public async Task <WeatherData> GetWeatherDataByZipAsync(string appId, string zipCode, string countryCode = "us", WeatherUnits units = WeatherUnits.Standard)
 {
     try
     {
         string url = GetWeatherDataByZipUrl(appId, zipCode, countryCode, units);
         return(await service.GetAsync(url));
     }
     catch
     {
         return(null);
     }
 }
コード例 #7
0
 private string GetWeatherDataByCityIdUrl(string appId, int cityId, WeatherUnits units)
 {
     return($"http://api.openweathermap.org/data/2.5/weather?Id={cityId}&appid={appId}&units={units.ToString()}");
 }
コード例 #8
0
 private string GetWeatherDataByCityNameUrl(string appId, string cityName, string countryCode, WeatherUnits units)
 {
     if (!string.IsNullOrEmpty(countryCode))
     {
         return($"http://api.openweathermap.org/data/2.5/weather?q={cityName},{countryCode}&appid={appId}&units={units.ToString()}");
     }
     else
     {
         return(GetWeatherDataByCityNameUrl(appId, cityName, units));
     }
 }
コード例 #9
0
 private string GetWeatherDataByZipUrl(string appId, string zipCode, string countryCode, WeatherUnits units)
 {
     return($"http://api.openweathermap.org/data/2.5/weather?zip={zipCode},{countryCode}&appid={appId}&units={units.ToString()}");
 }
コード例 #10
0
ファイル: AsyncService.cs プロジェクト: devanegas/AsyncApp
        public async Task <WeatherData> GetWeatherAsync(string zipCode, string countryCode, WeatherUnits units)
        {
            try
            {
                var json = await new HttpClient().GetStringAsync($"http://api.openweathermap.org/data/2.5/weather?zip={zipCode},{countryCode}&APPID=49b6fa96350f8c676a1b763f2005960c&units=metric");
                data = Newtonsoft.Json.JsonConvert.DeserializeObject <WeatherData>(json);
                //data = await forecast.GetWeatherDataByZipAsync(appId, zipCode, countryCode,units);
            }
            catch (Exception e)
            {
                throw;
            }

            return(data);
        }
コード例 #11
0
 [DllImport("VantageProDll242\\VantagePro.dll")] public static extern short SetUnits_V(WeatherUnits units);