public bool CheckIfWeatherWillBeGood(int hours, out string weatherAheadDescription) { weatherAheadDescription = "Weather will be fine."; Forecast forecast = Smhi.GetForecast(); ForecastTimeSerie currentWeather = Smhi.GetCurrentWeather(); int i = 0; foreach (ForecastTimeSerie timeSerie in forecast.timeseries .Where(ts => ts.validTime >= currentWeather.validTime) .OrderBy(ts => ts.validTime)) { // Kolla om det kommer att regna för mycket decimal precipitation = Math.Max(timeSerie.PrecipitationMax, timeSerie.PrecipitationMin); if (precipitation > (decimal)MaxHourlyPrecipitaionMillimeter) { weatherAheadDescription = "Expecting rain as a maximum of " + precipitation.ToString("0.0") + " mm/h at " + timeSerie.ValidTimeLocal.ToShortTimeString() + "."; return(false); } // Kolla om det är för hög sannolikhet för åska if (timeSerie.ThunderProbability > MaxHourlyThunderPercent) { weatherAheadDescription = "Thunder warning of " + timeSerie.ThunderProbability + "% at " + timeSerie.ValidTimeLocal.ToShortTimeString() + "."; return(false); } // Kolla om det kommer att bli för hög luftfuktighet if (timeSerie.RelativeHumidity > MaxRelativeHumidityPercent) { weatherAheadDescription = "Expecting a relative humidity of " + timeSerie.RelativeHumidity + "% at " + timeSerie.ValidTimeLocal.ToShortTimeString() + "."; return(false); } i++; if (i > hours) { break; } } return(true); }
private async Task StartAsync() { while (true) { // First, add a new weather time serie to the list of historic weather. ForecastTimeSerie currentWeather = _smhi.GetCurrentWeather(); var timeSerieNow = _weatherTimeSeries.OrderByDescending(ts => ts.validTime).FirstOrDefault(ts => ts.validTime == currentWeather.validTime); if (timeSerieNow != null) { for (int i = 0; i < _weatherTimeSeries.Count; i++) { if (timeSerieNow.validTime.ToString("yyyy-MM-dd HH:mm") == _weatherTimeSeries[i].validTime.ToString("yyyy-MM-dd HH:mm")) { _weatherTimeSeries[i] = timeSerieNow; } } } else { _weatherTimeSeries.Add(currentWeather); } decimal currentPrecipitation = Math.Max(currentWeather.PrecipitationMax, currentWeather.PrecipitationMin); // Calculate the wetness int wetness = Convert.ToInt32(currentPrecipitation * 100); foreach (var timeSerie in _weatherTimeSeries.Where(ts => ts.validTime > _systemTime.Now.ToUniversalTime().AddDays(-1)).OrderBy(ts => ts.validTime)) { decimal precipitation = Math.Max(timeSerie.PrecipitationMin, timeSerie.PrecipitationMax); // If it's raining, increase the wetness wetness += Convert.ToInt32(precipitation * 100); if (wetness > 100) { wetness = 100; } // If it's not raining, decrease wetness in relation to relative humidity if (precipitation == 0) { // Increase wetness if relative humidity is high and grass is not so wet if (timeSerie.RelativeHumidity >= 80 && wetness < 75) { wetness += (timeSerie.RelativeHumidity - 80); } // Lower wetness since grass is drying up wetness -= (100 - timeSerie.RelativeHumidity) / 2; } if (wetness < 0) { wetness = 0; } } Wetness = wetness; IsWet = currentPrecipitation > 0 || Wetness > 10; // Wait for 15 minutes await Task.Delay(15 * 60 * 1000); } }