// Syntax SIML public string Evaluate(Context parameter) { string cityname = ""; string cdeget = ""; string value = ""; string valvar = ""; bool flagok = true; if (parameter.Element.Attribute("City") != null) { cityname = parameter.Element.Attribute("City").Value; } if (parameter.Element.Attribute("Value") != null) { value = parameter.Element.Attribute("Value").Value; } // Get API Key in Bot User Settings string key = parameter.Bot.Settings["Weatherundergound_APIkey"].Value; // Get Bot User language String querylanguage = parameter.User.Settings[botlanguage_variable].Value.ToUpper(); WeatherClient client = new WeatherClient(key); client.GetForecast(QueryType.GlobalCity, new QueryOptions() { Country = "France", City = cityname, Language = querylanguage }); return(valvar); }
public async Task <ActionResult> GetWeather(LocationViewModel location) { var forecast = await _weatherClient.GetForecast(location.Latitude, location.Longitude); return(View(forecast)); }
static private string GetForecastWunder(string param) { List <Param> Params = Param.decryptParam(param); if (Params != null) { //Check if there is an automation setting if (Params.Where(m => m.Name.ToLower() == paramAutomateAll).Any()) { WunderSettings.AutomateAll = Param.CheckConvertBool(Params, paramAutomateAll); } } StringBuilder sb = new StringBuilder(); try { WeatherClient client = new WeatherClient(WunderSettings.Key); sb.Append(BuildHeader()); WunderSettings.NeedToSprinkle = false; for (int i = 0; i < WunderSettings.Stations.Length; i++) { var forecast = client.GetForecast(QueryType.PWSId, new QueryOptions() { PWSId = WunderSettings.Stations[i] }); var history = client.GetHistory(QueryType.PWSId, new QueryOptions() { PWSId = WunderSettings.Stations[i], Date = DateTime.Now.AddDays(-1) }); sb.Append($"<p>Full forecast for this station: <a href=\"https://www.wunderground.com/cgi-bin/findweather/getForecast?query=pws:{WunderSettings.Stations[i]}\">{WunderSettings.Stations[i]}</a><br>"); //Console.WriteLine("Magic recommendation:"); sb.Append("Magic recommendation:<br>"); //check temperature // so far, threshold are manual // Min temp is 15, don't sprinkler below 15 and 10C is 1 minute // More thabn 30, then sprinkler the maximum time var maxtmp = history.History.Dailysummary[0].MaxTempMetric; if (maxtmp == null) { maxtmp = 0; } // Adjust the simple proportion with min/max thresholds if ((maxtmp > 0)) { if (maxtmp >= WunderSettings.MaxTemp) { WunderSettings.PercentageCorrection = 1; } else if (maxtmp <= WunderSettings.MinTemp) { WunderSettings.PercentageCorrection = 0; } else { WunderSettings.PercentageCorrection = ((float)maxtmp - WunderSettings.MinTemp) / (WunderSettings.MaxTemp - WunderSettings.MinTemp); } } else { WunderSettings.PercentageCorrection = 1; } // Case we just want to use the simple proportion if (Fuzzy == null) { if (history.History.Dailysummary[0].PrecipitationMetric > WunderSettings.PrecipitationThresholdActuals) { //Console.Write($"It rained and more than 5mm ({history.History.DailySummaries[0].PrecipitationMetric}mm), no need to sprinkle."); sb.Append($"It rained and more than {WunderSettings.PrecipitationThresholdActuals} mm ({history.History.Dailysummary[0].PrecipitationMetric}mm), <b>no need to sprinkle</b>.<br>"); WunderSettings.NeedToSprinkle = false; } else if (forecast.Forecast.SimpleForecast.ForecastDay[0].Qpf_AllDay.Mm > WunderSettings.PrecipitationThresholdForecast) { //Console.WriteLine($"Forecast is for rain, more than 3mm ({forecast.Forecast.SimpleForecast.ForecastDay[0].Qpf_AllDay.Mm}mm), checking the confidence index."); sb.Append($"Forecast is for rain, more than {WunderSettings.PrecipitationThresholdForecast} mm ({forecast.Forecast.SimpleForecast.ForecastDay[0].Qpf_AllDay.Mm} mm), checking the confidence index.<br>"); if (forecast.Forecast.SimpleForecast.ForecastDay[0].Pop > WunderSettings.PrecipitationPercentForecast) { //Console.WriteLine($"Considence index if more than 60% ({forecast.Forecast.SimpleForecast.ForecastDay[0].Pop}%), so no need to sprinkle."); sb.Append($"Considence index is more than {WunderSettings.PrecipitationPercentForecast} % ({forecast.Forecast.SimpleForecast.ForecastDay[0].Pop} %), so <b>no need to sprinkle</b>.<br>"); WunderSettings.NeedToSprinkle = false; } else { if (WunderSettings.PercentageCorrection > 0) { WunderSettings.NeedToSprinkle = true; sb.Append($"Confidence index is less than {WunderSettings.PrecipitationPercentForecast} % ({forecast.Forecast.SimpleForecast.ForecastDay[0].Pop} %), "); sb.Append($"sprinkling will be adjusted by {(WunderSettings.PercentageCorrection * 100).ToString("0")} %, so yes, <b>plan to sprinkle!</b><br>"); } else { sb.Append($"Temperature is too low {maxtmp}C, so we will not sprinkler at all. Please use manual program if you still want to sprinkler."); WunderSettings.NeedToSprinkle = false; } } } else { if (WunderSettings.PercentageCorrection > 0) { WunderSettings.NeedToSprinkle = true; sb.Append($"Forecast is for rain, but less than {WunderSettings.PrecipitationThresholdActuals} mm ({forecast.Forecast.SimpleForecast.ForecastDay[0].Qpf_AllDay.Mm} mm), "); sb.Append($"sprinkling will be adjusted by {(WunderSettings.PercentageCorrection * 100).ToString("0")} %, so yes, <b>plan to sprinkle!</b><br>"); } else { sb.Append($"Temperature is too low {maxtmp}C, so we will not sprinkler at all. Please use manual program if you still want to sprinkler."); WunderSettings.NeedToSprinkle = false; } } } else { // Use of Fuzzy logic foreach (var objective in Fuzzy) { //Found the righ range if ((objective.TempMin >= maxtmp) && (objective.TempMax < maxtmp)) { // How much rain? if (history.History.Dailysummary[0].PrecipitationMetric > objective.RainMax) { sb.Append($"It rained and more than {objective.RainMax} mm ({history.History.Dailysummary[0].PrecipitationMetric}mm), <b>no need to sprinkle</b>.<br>"); WunderSettings.NeedToSprinkle = false; } else if (forecast.Forecast.SimpleForecast.ForecastDay[0].Qpf_AllDay.Mm > WunderSettings.PrecipitationThresholdForecast) { sb.Append($"Considence index is more than {WunderSettings.PrecipitationPercentForecast} % ({forecast.Forecast.SimpleForecast.ForecastDay[0].Pop} %), so <b>no need to sprinkle</b>.<br>"); WunderSettings.NeedToSprinkle = false; } else { // so we need to sprinkler. Make the math how long with the correction factor // first calculate proportion of time vs the theoritical maximum if (history.History.Dailysummary[0].PrecipitationMetric != null) { WunderSettings.PercentageCorrection = (float)(((objective.RainMax - history.History.Dailysummary[0].PrecipitationMetric.Value) / objective.RainMax) * objective.SprinklingMax / 100.0); } else { WunderSettings.PercentageCorrection = (float)(objective.SprinklingMax / 100.0); } } } } } if (WunderSettings.NeedToSprinkle) { //Console.WriteLine("I will use the typical programs. Please adjust manually if needed.<br>"); sb.Append($"I will use the typical programs. Please adjust manually if needed.<br>"); } sb.Append("<br>Forecast:<br><table><tr><th>Date</th><th>Rain</th><th>Chances</th><th></th><th>Conditions</th><tr>"); //Console.WriteLine("Forecast"); foreach (var itm in forecast.Forecast.SimpleForecast.ForecastDay) { //Console.WriteLine($"{itm.Date.Pretty} rain: {itm.Qpf_AllDay.Mm}, chances: {itm.Pop}%"); sb.Append($"<tr><td>{itm.Date.Pretty}</td><td>{itm.Qpf_AllDay.Mm} mm</td><td>{itm.Pop}%</td><td><img src=\"{itm.Icon_Url}\"></td><td>{itm.Conditions}</td></tr>"); } sb.Append("</table><br>"); //Console.WriteLine("Observation"); sb.Append("Observations:"); sb.Append("<br><table><tr><th>Date</th><th>Rain</th><th>Humidity</th><th>Pressure</th><th>Temperature</th><th>Wind Direction</th><tr>"); sb.Append($"<tr><td>{history.History.Dailysummary[0].Date.Pretty}</td><td>{history.History.Dailysummary[0].PrecipitationMetric} mm</td><td>{history.History.Dailysummary[0].Humidity} %</td><td>{history.History.Dailysummary[0].MeanPressureMetric}</td><td>{history.History.Dailysummary[0].MeanTempMetric} °C</td><td>{history.History.Dailysummary[0].MeanWindSpeedMetric}</td></tr>"); foreach (var itm in history.History.Observations) { //Console.WriteLine($"{itm.Date.Hour}:{itm.Date.Min} rain: {itm.PrecipitationTotalMetric}"); sb.Append($"<tr><td>{itm.Date.Pretty}</td><td>{itm.PrecipitationTotalMetric} mm</td><td>{itm.Humidity} %</td><td>{itm.PressureMetric}</td><td>{itm.TempCelcius} °C</td><td>{itm.WindDirection}</td></tr>"); } sb.Append("</table><br></p>"); //Console.WriteLine($"Total day rain: {history.History.DailySummaries[0].PrecipitationMetric}"); //call the sendmail } } catch (Exception ex) { sb.Append($"<p>ERROR:<br>{ex.Message}"); } sb.Append("<p><a href='/typic.aspx" + Param.ParamStart + securityKey + Param.ParamSeparator + paramClk + Param.ParamEqual + "1'>Create typical program</a><br>"); sb.Append("<a href='/" + paramPageSprinkler + Param.ParamStart + securityKey + "'>Return to main page</a>"); sb.Append("</p></BODY></HTML>"); return(sb.ToString()); }