/// <summary> /// check inpyt values ranges if are real /// </summary> /// <param name="weatherImperial"></param> /// <param name="checkErrorDescription"></param> /// <returns></returns> public static bool CheckRange(this WeatherImperial weatherImperial, ref string checkErrorDescription) { List <bool> results = new List <bool>(); List <string> errors = new List <string>(); //temperatures results.Add(weatherImperial.Tempf.IsWithin(nameof(weatherImperial.Tempf), Constants.RangeTempF, ref errors)); results.Add(weatherImperial.Indoortempf.IsWithin(nameof(weatherImperial.Indoortempf), Constants.RangeTempF, ref errors)); results.Add(weatherImperial.Dewptf.IsWithin(nameof(weatherImperial.Dewptf), Constants.RangeTempF, ref errors)); //humidity results.Add(((decimal?)weatherImperial.Humidity).IsWithin(nameof(weatherImperial.Humidity), Constants.RangeHumi, ref errors)); results.Add(((decimal?)weatherImperial.Indoorhumidity).IsWithin(nameof(weatherImperial.Indoorhumidity), Constants.RangeHumi, ref errors)); //baro results.Add(((decimal?)weatherImperial.Baromin).IsWithin(nameof(weatherImperial.Baromin), Constants.RangeBaroIn, ref errors)); //wind dir results.Add(((decimal?)weatherImperial.Winddir).IsWithin(nameof(weatherImperial.Winddir), Constants.RangeWindDirDeg, ref errors)); results.Add(((decimal?)weatherImperial.Windgustdir).IsWithin(nameof(weatherImperial.Windgustdir), Constants.RangeWindDirDeg, ref errors)); //wind speed results.Add(((decimal?)weatherImperial.Windspeedmph).IsWithin(nameof(weatherImperial.Windspeedmph), Constants.RangeWindSpeedMpH, ref errors)); results.Add(((decimal?)weatherImperial.Windgustmph).IsWithin(nameof(weatherImperial.Windgustmph), Constants.RangeWindSpeedMpH, ref errors)); //uv results.Add(((decimal?)weatherImperial.Uv).IsWithin(nameof(weatherImperial.Uv), Constants.RangeUV, ref errors)); //rain results.Add(((decimal?)weatherImperial.Rainin).IsWithin(nameof(weatherImperial.Rainin), Constants.RangeRainIn, ref errors)); results.Add(((decimal?)weatherImperial.Dailyrainin).IsWithin(nameof(weatherImperial.Dailyrainin), Constants.RangeRainIn, ref errors)); checkErrorDescription = JsonSerializer.Serialize(errors); return(results.All(r => r == true)); }
public async Task <IActionResult> PostWeather(string id, string pwd, [FromBody] WeatherImperial weatherImperial) { DateTime now = DateTime.UtcNow; //get pws and they last requet from cache - chache timeout in Constants.PWSTimeout, after this period is chache refreshed, and same request can by writed CacheWeather pwsCache = await new CacheWeatherLogic(_memoryCache, _context, _serviceFactory).GetPWSAsync(id, pwd); //no PWS foud by pwsData.PWSId if (pwsCache == null) { return(Unauthorized(Constants.NoPWS)); } string checkRangeError = string.Empty; if (!weatherImperial.CheckRange(ref checkRangeError)) { return(ValidationProblem(checkRangeError)); } Weather weather = weatherImperial.ToWeather(); weather.IdPws = pwsCache.IdPws; weather.Dateutc = now; pwsCache.lastWeatherSet.Enqueue(weather); return(Ok()); }
/// <summary> /// convert input protocol class as TDO model to weather entity, round to 1 decimal point /// </summary> /// <param name="weatherImperial"></param> /// <returns></returns> public static Weather ToWeather(this WeatherImperial weatherImperial) { return(new Weather { Baromhpa = ImperialToMetric.MercInchToHpa(weatherImperial.Baromin), Dailyrainmm = ImperialToMetric.InchesToMilimeters(weatherImperial.Dailyrainin), Dewptc = ImperialToMetric.FarenheitToCelsius(weatherImperial.Dewptf), Humidity = weatherImperial.Humidity, //Id = null, //IdPws Indoorhumidity = weatherImperial.Indoorhumidity, Indoortempc = ImperialToMetric.FarenheitToCelsius(weatherImperial.Indoortempf), Rainmm = ImperialToMetric.InchesToMilimeters(weatherImperial.Rainin), Tempc = ImperialToMetric.FarenheitToCelsius(weatherImperial.Tempf), Uv = weatherImperial.Uv, Winddir = weatherImperial.Winddir, Windgustkmh = ImperialToMetric.MphToKmh(weatherImperial.Windgustmph), Windspeedkmh = ImperialToMetric.MphToKmh(weatherImperial.Windspeedmph) }); }