public async Task <ActionResult <WeatherLog> > PostWeatherLog(WeatherLogDto weatherLogDto) { var place = await _context.Places.FindAsync(weatherLogDto.LogPlaceId); if (place == null) { return(NotFound()); } WeatherLog weatherLog = new WeatherLog() { LogTime = weatherLogDto.LogTime, LogPlace = place, Temperature = weatherLogDto.Temperature, Humidity = weatherLogDto.Humidity, AirPressure = weatherLogDto.AirPressure }; _context.WeatherLogs.Add(weatherLog); await _context.SaveChangesAsync(); //================== SignalR =================== var placeid = weatherLog.LogPlace.PlaceId; var jsonmsg = JsonConvert.SerializeObject(weatherLog); await _weatherHub.Clients.All.SendAsync("WeatherUpdate", jsonmsg); return(CreatedAtAction("GetWeatherLog", new { id = weatherLog.LogId }, weatherLog)); }
public void Start() { HttpClient client = new HttpClient(); string json = client.GetStringAsync("http://api.openweathermap.org/data/2.5/weather?id=146286&units=metric").Result; Rootobject obj = Newtonsoft.Json.JsonConvert.DeserializeObject <Rootobject>(json); WeatherLogClient logClient = new WeatherLogClient(); WeatherLog log = new WeatherLog(DateTime.Now) { Clouds = obj.clouds.all, Description = obj.weather.FirstOrDefault().description, Humidty = obj.main.humidity, Pressure = obj.main.pressure, Rain = obj.rain._3h, Sunrise = obj.sys.sunrise, Sunset = obj.sys.sunset, Temperature = obj.main.temp, WindDegree = obj.wind.deg, WindSpeed = obj.wind.speed, CityId = obj.id }; logClient.InsertOrReplace(log); }
public void SeedWeatherLog(ApplicationDbContext context) { var exists = context.WeatherLogs.SingleOrDefault(d => d.LogId == 1); if (exists != null) { } else { var places = context.Places.ToList(); foreach (var place in places) { if (place.PlaceId == 1) { DateTime date1 = new DateTime(2021, 10, 8, 8, 00, 00); WeatherLog log1 = new WeatherLog() { LogTime = date1, LogPlace = place, Temperature = 24, Humidity = 80, AirPressure = 50 }; context.WeatherLogs.Add(log1); context.SaveChanges(); DateTime date2 = new DateTime(2021, 10, 9, 8, 00, 00); WeatherLog log2 = new WeatherLog() { LogTime = date2, LogPlace = place, Temperature = 20, Humidity = 80, AirPressure = 50 }; context.WeatherLogs.Add(log2); context.SaveChanges(); DateTime date3 = new DateTime(2021, 10, 9, 12, 30, 00); WeatherLog log3 = new WeatherLog() { LogTime = date3, LogPlace = place, Temperature = 21, Humidity = 80, AirPressure = 50 }; context.WeatherLogs.Add(log3); context.SaveChanges(); DateTime date4 = new DateTime(2021, 10, 9, 16, 40, 00); WeatherLog log4 = new WeatherLog() { LogTime = date4, LogPlace = place, Temperature = 22, Humidity = 80, AirPressure = 50 }; context.WeatherLogs.Add(log4); context.SaveChanges(); DateTime date5 = new DateTime(2021, 10, 10, 18, 20, 00); WeatherLog log5 = new WeatherLog() { LogTime = date5, LogPlace = place, Temperature = 23, Humidity = 80, AirPressure = 50 }; context.WeatherLogs.Add(log5); context.SaveChanges(); } if (place.PlaceId == 2) { WeatherLog log1 = new WeatherLog() { LogTime = Convert.ToDateTime("10-10-2021"), LogPlace = place, Temperature = 18, Humidity = 80, AirPressure = 50 }; context.WeatherLogs.Add(log1); context.SaveChanges(); WeatherLog log2 = new WeatherLog() { LogTime = Convert.ToDateTime("11-10-2021"), LogPlace = place, Temperature = 16, Humidity = 80, AirPressure = 50 }; context.WeatherLogs.Add(log2); context.SaveChanges(); } if (place.PlaceId == 3) { WeatherLog log1 = new WeatherLog() { LogTime = Convert.ToDateTime("12-10-2021"), LogPlace = place, Temperature = 28, Humidity = 80, AirPressure = 50 }; context.WeatherLogs.Add(log1); context.SaveChanges(); WeatherLog log2 = new WeatherLog() { LogTime = Convert.ToDateTime("13-10-2021"), LogPlace = place, Temperature = 24, Humidity = 80, AirPressure = 50 }; context.WeatherLogs.Add(log2); context.SaveChanges(); } } //WeatherLog secondlog = new WeatherLog() //{ // LogTime = Convert.ToDateTime("10-10-2021"), // LogPlace = place2, // Temperature = 24, // Humidity = 80, // AirPressure = 50 //}; //context.WeatherLogs.Add(secondlog); //context.SaveChanges(); //WeatherLog thirdlog = new WeatherLog() //{ // LogTime = Convert.ToDateTime("10-10-2021"), // LogPlace = place3, // Temperature = 24, // Humidity = 80, // AirPressure = 50 //}; //context.WeatherLogs.Add(thirdlog); //context.SaveChanges(); } }