Exemple #1
0
        public Models.CurrentConditionsWeatherResponse GetCurrentWeather(string city, string state)
        {
            var cachedFile = new FileInfo(CacheFolder + "\\CurrentWeather.json");

            Models.CurrentConditionsWeatherResponse weather = null;
            if (cachedFile.Exists && (DateTime.Now - cachedFile.LastWriteTime).TotalSeconds < UpdateInterval)
            {
                weather = Newtonsoft.Json.JsonConvert.DeserializeObject <Models.CurrentConditionsWeatherResponse>(File.ReadAllText(cachedFile.FullName), JsonSerializerSettings());
            }
            else
            {
                var client = new System.Net.WebClient {
                    Proxy = { Credentials = CredentialCache.DefaultCredentials }
                };
                var weatherJson = client.DownloadString($"{ApiUrl}conditions/q/{state}/{city}.json");
                weather = Newtonsoft.Json.JsonConvert.DeserializeObject <Models.CurrentConditionsWeatherResponse>(weatherJson, JsonSerializerSettings());
                if (weather.current_observation != null)
                {
                    CacheCurrentWeather(weather);
                }
                else
                {
                    MessageBox.Show($"Weather returned null, please check your API key, city, or state in the Config.json file. Current API Key: {AppConfig.ApiKey}");
                }
            }

            return(weather);
        }
Exemple #2
0
        public void CacheCurrentWeather(Models.CurrentConditionsWeatherResponse weather)
        {
            var file = CacheFolder + "\\CurrentWeather.json";

            System.IO.File.WriteAllText(file, Newtonsoft.Json.JsonConvert.SerializeObject(weather));
        }