public HttpResponseMessage GetVal(long id, bool?isCorrect = null) { WeatherValue wv = db.WeatherValues.Find(id); wv.IsCorrect = isCorrect; if (isCorrect == true || isCorrect == null) { wv.ActualIcon = null; } var vals = db.WeatherValues.Where(v => v.DateTime == wv.DateTime); foreach (WeatherValue v in vals) { if (wv.PgoIconId == v.PgoIconId) { if (isCorrect == true || isCorrect == null) { v.ActualIcon = null; v.IsCorrect = isCorrect; } else { v.IsCorrect = isCorrect; } } } db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); }
public static void SaveWeather(WeatherObject wo, WeatherTranslation wt) { using (PokeEntities db = new PokeEntities()) { WeatherValue wv = new WeatherValue() { DateTime = wo.DateTime.ToUniversalTime(), WeatherIcon = wo.WeatherIcon, IconPhrase = wo.IconPhrase, IsDaylight = wo.IsDaylight, TempValue = wo.Temperature.Value, TempUnit = wo.Temperature.Unit, WindSpeed = wo.Wind.Speed.Value, WindUnit = wo.Wind.Speed.Unit, GustSpeed = wo.WindGust.Speed.Value, GustUnit = wo.WindGust.Speed.Unit, PrecipitationProbability = wo.PrecipitationProbability, RainProbability = wo.RainProbability, SnowProbability = wo.SnowProbability, IceProbability = wo.IceProbability, CloudCover = wo.CloudCover, RainAmt = wo.Rain.Value, SnowAmt = wo.Snow.Value, IceAmt = wo.Ice.Value, PgoIconId = wt.PgoWeather.Id, DateAdded = DateTime.Now.ToUniversalTime() }; db.WeatherValues.Add(wv); db.SaveChanges(); } }
public ConfigSetting PostConfig(ConfigSetting c) { var cfg = db.ConfigSettings.Find(c.Id); if (cfg != null) { db.Entry(cfg).CurrentValues.SetValues(c); db.SaveChanges(); return(cfg); } return(c); }
public HttpResponseMessage PostTranslation(WeatherTranslation w) { var wt = db.WeatherTranslations.Find(w.Id); if (wt == null) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "not found")); } db.Entry(wt).CurrentValues.SetValues(w); db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, "updated")); }
public static void RemoveStale() { using (PokeEntities db = new PokeEntities()) { var oldDate = DateTime.Now.AddDays(-3).Date; var oldValues = db.WeatherValues.Where(v => v.DateAdded < oldDate); var oldEntries = db.WeatherEntries.Where(e => e.DateCreated < oldDate); db.WeatherValues.RemoveRange(oldValues); db.WeatherEntries.RemoveRange(oldEntries); db.SaveChanges(); } }
public HttpResponseMessage GetWeather() { // get forecast from AccuWeather and save in DB // this needs to get called hourly at about 5 past the hour (or 35 past if your time zone is a half hour off) using (WebClient client = new WebClient()) { var resp = client.DownloadString("http://dataservice.accuweather.com/forecasts/v1/hourly/12hour/" + Resources.LocationId + "?apikey=" + Resources.ApiKey + "&metric=true&details=true"); var now = DateTime.UtcNow; DateTime thisTime = DateTime.Now; string tzId = Resources.TimeZoneId; TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById(tzId); bool isDaylight = tst.IsDaylightSavingTime(thisTime); WeatherObject[] response = null; try { response = JsonConvert.DeserializeObject <WeatherObject[]>(resp); } catch (Exception ex) { Trace.Write(ex); } var LindsayTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(now, tst.Id); List <WeatherValue> wvs = new List <WeatherValue>(); for (var i = 0; i < response.Length; i++) { var w = response[i]; //w.PokeWeather = new PgoWeather(); WeatherTranslation wt = db.WeatherTranslations.Find(w.WeatherIcon); int weatherId; if (wt.CanWindy == true && (w.Wind.Speed.Value > 29 || w.WindGust.Speed.Value > 31)) // (OR IS IT SUM OF THESE ? 55? This works for me, anyway) { w.PokeWeather = db.PgoWeathers.Find(6); weatherId = 6; } else { weatherId = wt.PgoWeather.Id; } WeatherValue wv = new WeatherValue() { DateTime = w.DateTime, WeatherIcon = w.WeatherIcon, IconPhrase = w.IconPhrase, IsDaylight = w.IsDaylight, TempUnit = w.Temperature.Unit, TempValue = w.Temperature.Value, WindSpeed = w.Wind.Speed.Value, WindUnit = w.Wind.Speed.Unit, GustSpeed = w.WindGust.Speed.Value, GustUnit = w.WindGust.Speed.Unit, PrecipitationProbability = w.PrecipitationProbability, RainProbability = w.RainProbability, RainAmt = w.Rain.Value, RainUnit = w.Rain.Unit, SnowProbability = w.SnowProbability, SnowAmt = w.Snow.Value, SnowUnit = w.Snow.Unit, IceProbability = w.IceProbability, IceAmt = w.Ice.Value, IceUnit = w.Ice.Unit, CloudCover = w.CloudCover, PgoIconId = weatherId, DateAdded = now }; wvs.Add(wv); } WeatherEntry entry = new WeatherEntry() { DateCreated = LindsayTime, Date = LindsayTime, Hour = LindsayTime.Hour, WeatherValues = wvs }; db.WeatherEntries.Add(entry); db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, "got it")); } }