/// <summary> /// takes the current stored item and saves it to the database, if empty nothing will happen. /// </summary> /// <returns>Current Object</returns> public void Save() { if (_currObject != null && _currObject.weather.Count != 0) { //write _currObject to the database. WeatherInfo newRecord = new WeatherInfo { Timestamp = DateTime.Now, Locale = _currObject.name, Country = _currObject.sys.country, Temperature = _currObject.main.temp.ToString(), Temperature_Min = _currObject.main.temp_min.ToString(), Temperature_Max = _currObject.main.temp_max.ToString() }; _weather = _currObject.weather.FirstOrDefault(); if (_weather != null) { newRecord.Description = _weather.description; newRecord.MainInfo = _weather.main; } db.WeatherInfoes.Add(newRecord); try { db.SaveChanges(); //in this case only crashes if the database is unreachable or if data is corrupted } catch (Exception e) { _currObject = null; //do this so that updatescreen doesnt die on error data } } }
public WeatherAPIControl(string apiKey, string locale) { //_currObject = new ApiResponseObject(); string resourceUrl = string.Format("data/2.5/weather?q={0}&units=metric", locale); var response = RestHelper.Get("http://api.openweathermap.org", resourceUrl, apiKey); _currObject = RestHelper.ConvertJsonToObject <ApiResponseObject>(response.Content); }