// /weather/getlastten public async Task <ViewResult> GetLastTen() { List <WeatherMeasurement> weatherMeasurementList = new List <WeatherMeasurement>(); WeatherMeasurement weatherMeasurement = null; using (var client = new HttpClient()) { for (int i = 9; i >= 0; i--) { DateTime upperBound = DateTime.UtcNow.AddHours(-1 * i); string dateTimeUrl = upperBound.ToString("yyyy-MM-ddTHH"); var jsonResponse = await client.GetStringAsync("http://weatherapi2dashboard.azurewebsites.net/api/WeatherMeasurement/" + dateTimeUrl); if (jsonResponse != null) { weatherMeasurement = JsonConvert.DeserializeObject <WeatherMeasurement>(jsonResponse); weatherMeasurementList.Add(weatherMeasurement); } } string result = ""; foreach (WeatherMeasurement wm in weatherMeasurementList) { result += wm.Temperature + " "; } //return result; ViewBag.One = weatherMeasurementList[0]; return(View("Index")); } }
private void StoreMeasurementToOfflineStorage(WeatherMeasurement measurement) { var ser = new BinaryFormatter(); using (var memStream = new MemoryStream()) { ser.Serialize(memStream, measurement); var array = memStream.ToArray(); var path = System.Configuration.ConfigurationManager.AppSettings["OfflineStoragePath"]; if (Directory.Exists(path) == false) { Directory.CreateDirectory(path); } File.WriteAllBytes($"{path}\\{measurement.Timestamp.Timestamp2String(true, true)}", array); } }
// /weather/getall?time=2018-03-06T14 public async Task <string> Get(string time) { WeatherMeasurement weatherMeasurement = null; using (var client = new HttpClient()) { var jsonResponse = await client.GetStringAsync("http://weatherapi2dashboard.azurewebsites.net/api/WeatherMeasurement/" + time); if (jsonResponse != null) { weatherMeasurement = JsonConvert.DeserializeObject <WeatherMeasurement>(jsonResponse); } return(weatherMeasurement.Temperature.ToString()); //return time; } }
private WeatherInfo extractDayFromList(List <WeatherMeasurement> list) { int min = 999; foreach (var hour in list) { if (hour.MinTemp < min) { min = Convert.ToInt32(hour.MinTemp); } } int minTemp = (min - 273); int max = 0; foreach (var hour in list) { if (hour.MaxTemp > max) { max = Convert.ToInt32(hour.MaxTemp); } } int maxTemp = (max - 273); WeatherMeasurement wm = new WeatherMeasurement(); wm.MinTemp = minTemp; wm.MaxTemp = maxTemp; wm.Temp = (minTemp + maxTemp) / 2; wm.Pressure = list[4].Pressure; wm.Humidity = list[4].Humidity; wm.Status = list[4].Status; wm.Description = list[4].Description; wm.Time = list[0].Time; WeatherInfo wi = new WeatherInfo(wm); return(wi); }
protected override async void DoWork() { try { var wsp = new WeatherStatsPoller(this.City, this.Country); while (false == this.ClosingDown) { var now = DateTime.Now; var pollTime10MinPrecision = now.ToTenMinutePrecision(); var ts = new TimeSpan(0, 0, 10, 0) - (now - pollTime10MinPrecision); Thread.Sleep(Convert.ToInt32(ts.TotalMilliseconds)); var measuredDoubleValue = await wsp.PollDataFromWeb(); var measurement = new WeatherMeasurement(pollTime10MinPrecision, measuredDoubleValue); try { this.Database.WeatherMeasurement.Add(measurement); this.Database.SaveChanges(); Console.WriteLine($"Web says that in {wsp.Country},{wsp.City} the temperature is {measurement.TemperatureMeasurement} measured at: {measurement.Timestamp.Timestamp2String(false)}"); } catch (Exception e) { // database is not avilable, store the data in a file StoreMeasurementToOfflineStorage(measurement); } } this.RanToEnd = true; } catch (ThreadAbortException) { // if the thead is being aborted } catch (Exception e) { Debug.Assert(false, e.Message); } }
public void QueryWeatherApi(int id) { this.WeatherList = new ObservableCollection <WeatherMeasurement>(WeatherMeasurement.GetWeatherMeasurements(id)); return; }