diff --git a/HealthMonitor.Service/Biz/db/TDengineService.cs b/HealthMonitor.Service/Biz/db/TDengineService.cs index 81cedd9..2b080d9 100644 --- a/HealthMonitor.Service/Biz/db/TDengineService.cs +++ b/HealthMonitor.Service/Biz/db/TDengineService.cs @@ -1,6 +1,7 @@ using HealthMonitor.Common; using HealthMonitor.Common.helper; using HealthMonitor.Model.Config; +using HealthMonitor.Model.Service.Mapper; using HealthMonitor.Service.Biz.db.Dto; using HealthMonitor.Util.Models; using Microsoft.EntityFrameworkCore.Metadata.Internal; @@ -520,5 +521,42 @@ namespace HealthMonitor.Service.Biz.db //return values.Average(); } + /// + /// + /// + /// + /// + /// + public decimal[] AverageAfterRemovingOneMinMaxRef(int systolicRefValue, ParseTDengineRestResponse? hmBpParser) + { + var sortedList = hmBpParser?.Select(i => i) + .Where(i => i.IsDisplay.Equals(true)) + .OrderByDescending(i => i.SystolicValue) + .ThenByDescending(i => i.DiastolicValue) + .ToList(); + _logger.LogInformation($"计算时间段排列数据集:{JsonConvert.SerializeObject(sortedList)}"); + // 去除最大值和最小值各一个(列表的头和尾) + var trimmedList = sortedList? + .Skip(1) + .Take(sortedList.Count - 2) + .ToList(); + + _logger.LogInformation($"计算去除最大值和最小值各一个数据集:{JsonConvert.SerializeObject(trimmedList)}"); + // 去除异常值 + var filteredList = trimmedList?.Where(bp => bp.SystolicValue < SafeType.SafeInt(systolicRefValue!)).ToList(); + + _logger.LogInformation($"计算除异常值个数据集:{JsonConvert.SerializeObject(filteredList)}"); + + if (filteredList?.Count < 2) + { + // throw new ArgumentException("数据不够不能计算"); + // 平均值为0,说明数据不足,不能计算增量值 + return new decimal[] { 0M, 0M }; + } + var systolicAvg = filteredList?.Select(bp => bp.SystolicValue).Average(); + var diastolicAvg = filteredList?.Select(bp => bp.DiastolicValue).Average(); + + return new decimal[] { (decimal)systolicAvg!, (decimal)diastolicAvg! }; + } } } diff --git a/HealthMonitor.WebApi/Controllers/HealthMonitor/HmBloodPressConfigManualCalibrationController.cs b/HealthMonitor.WebApi/Controllers/HealthMonitor/HmBloodPressConfigManualCalibrationController.cs index c7b3b3f..bf1e768 100644 --- a/HealthMonitor.WebApi/Controllers/HealthMonitor/HmBloodPressConfigManualCalibrationController.cs +++ b/HealthMonitor.WebApi/Controllers/HealthMonitor/HmBloodPressConfigManualCalibrationController.cs @@ -188,14 +188,18 @@ namespace HealthMonitor.WebApi.Controllers.HealthMonitor $" and is_display = true"; var hmBpResponse = await _serviceTDengine.ExecuteSelectRestResponseAsync("stb_hm_bloodpress", condition); var hmBpParser = JsonConvert.DeserializeObject>(hmBpResponse!); - if (hmBpParser!.Rows > 5) + if (hmBpParser!.Rows >= 5) { - systolicAvg = (int)(hmBpParser?.AverageAfterRemovingOneMinMaxRef(i => i.SystolicValue, SafeType.SafeInt(systolicRefValue!)))!; - diastolicAvg = (int)(hmBpParser?.AverageAfterRemovingOneMinMaxRef(i => i.DiastolicValue, SafeType.SafeInt(diastolicRefValue!)))!; + + //systolicAvg = (int)(hmBpParser?.AverageAfterRemovingOneMinMaxRef(i => i.SystolicValue, SafeType.SafeInt(systolicRefValue!)))!; + //diastolicAvg = (int)(hmBpParser?.AverageAfterRemovingOneMinMaxRef(i => i.DiastolicValue, SafeType.SafeInt(diastolicRefValue!)))!; + var avgs = _serviceTDengine.AverageAfterRemovingOneMinMaxRef(SafeType.SafeInt(systolicRefValue!), hmBpParser!); + systolicAvg = (int)avgs[0]; + diastolicAvg = (int)avgs[1]; // 平均值为0,说明数据不足,不能计算增量值 // 数据不足等同进行初始化,只下发标定值,增量值为0;remarks恢复到未校准状态,需要基于第一个测量值生成增量值。 - if (systolicAvg.Equals(0) || diastolicAvg.Equals(0)) + if (systolicAvg.Equals(0)) { _logger.LogInformation($"测量数据样本不足,将进行标定值初始,只下发标定值,增量值为0;remarks恢复到未校准状态。"); // 重置设备 diff --git a/HealthMonitor.WebApi/Worker.cs b/HealthMonitor.WebApi/Worker.cs index 4b74a09..5e763f6 100644 --- a/HealthMonitor.WebApi/Worker.cs +++ b/HealthMonitor.WebApi/Worker.cs @@ -140,7 +140,7 @@ namespace HealthMonitor.WebApi private void WatchEvents(WatchResponse response) { - response.Events.ToList().ForEach(async e => + response.Events.ToList().ForEach(async e => { switch (e.Type.ToString()) @@ -210,7 +210,7 @@ namespace HealthMonitor.WebApi if (lastHmBp?.Timestamp.AddDays(7) > DateTime.Now) { // 计算增量值 - condition= $"serialno='{imeiDel}' order by ts desc"; + condition = $"serialno='{imeiDel}' order by ts desc"; var lastPushResponse = await _serviceTDengine.ExecuteSelectRestResponseAsync("stb_hm_bp_push_ref_inc_value", condition, field); if (lastPushResponse == null) { @@ -233,26 +233,32 @@ namespace HealthMonitor.WebApi var hmBp = hmBpParser?.Select(); //if (hmBp?.ToList().Count < 2) // 1. 判断数据样本数量 - if(hmBpParser!.Rows < 5) + if (hmBpParser!.Rows < 5) { _logger.LogInformation($"{imeiDel} -- {nameof(Worker)} --{nameof(WatchEvents)} -- 统计定时下发,计算增量值的数据条目不足:{hmBpParser!.Rows} < 5"); _logger.LogInformation($"{imeiDel} -- {nameof(Worker)} --{nameof(WatchEvents)} 没有足够的数据样本,不会定时下发"); break; } - + // NewMethod(systolicRefValue, hmBpParser); // 最大值 - systolicMax = (int)hmBpParser?.Select(i => i.SystolicValue).Max()!; - diastolicMax = (int)hmBpParser?.Select(i => i.DiastolicValue).Max()!; - // 最小值 - systolicMin = (int)hmBpParser?.Select(i => i.SystolicValue).Min()!; - diastolicMin = (int)hmBpParser?.Select(i => i.DiastolicValue).Min()!; - - systolicAvg = (int)(hmBpParser?.AverageAfterRemovingOneMinMaxRef(i => i.SystolicValue, SafeType.SafeInt(systolicRefValue!)))!; - diastolicAvg = (int)(hmBpParser?.AverageAfterRemovingOneMinMaxRef(i => i.DiastolicValue, SafeType.SafeInt(diastolicRefValue!)))!; + //systolicMax = (int)hmBpParser?.Select(i => i.SystolicValue).Max()!; + //diastolicMax = (int)hmBpParser?.Select(i => i.DiastolicValue).Max()!; + //// 最小值 + //systolicMin = (int)hmBpParser?.Select(i => i.SystolicValue).Min()!; + //diastolicMin = (int)hmBpParser?.Select(i => i.DiastolicValue).Min()!; + + //systolicAvg = (int)(hmBpParser?.AverageAfterRemovingOneMinMaxRef(i => i.SystolicValue, SafeType.SafeInt(systolicRefValue!)))!; + //diastolicAvg = (int)(hmBpParser?.AverageAfterRemovingOneMinMaxRef(i => i.DiastolicValue, SafeType.SafeInt(diastolicRefValue!)))!; + + var avgs = _serviceTDengine.AverageAfterRemovingOneMinMaxRef(SafeType.SafeInt(systolicRefValue!), hmBpParser!); + systolicAvg = (int)avgs[0]; + diastolicAvg = (int)avgs[1]; + + // 2. 判断能否计算增量值 - if (systolicAvg.Equals(0) || diastolicAvg.Equals(0)) + if (systolicAvg.Equals(0)) { _logger.LogInformation($"{imeiDel}--{nameof(Worker)}--计算平均值" + $"\n currentSystolicAvg:{systolicAvg} -- lastPushSystolicInc:{lastPushSystolicInc}" + @@ -265,7 +271,7 @@ namespace HealthMonitor.WebApi var currentSystolicInc = (int)((systolicRefValue - systolicAvg) * systolicAvgOffset)!; var currentDiastolicInc = (int)((diastolicRefValue - diastolicAvg) * diastolicAvgOffset)!; - + // 累计增量 systolicInc = currentSystolicInc + lastPushSystolicInc; diastolicInc = currentDiastolicInc + lastPushDiastolicInc; @@ -407,6 +413,26 @@ namespace HealthMonitor.WebApi }); } + private static void NewMethod(int systolicRefValue, ParseTDengineRestResponse? hmBpParser) + { + var sortedList = hmBpParser?.Select(i => i) + .Where(i => i.IsDisplay.Equals(true)) + .OrderByDescending(i => i.SystolicValue) + .ThenByDescending(i => i.DiastolicValue) + .ToList(); + // 去除最大值和最小值各一个(列表的头和尾) + var trimmedList = sortedList? + .Skip(1) + .Take(sortedList.Count - 2) + .ToList(); + + // 去除异常值 + var filteredList = trimmedList?.Where(bp => bp.SystolicValue < SafeType.SafeInt(systolicRefValue!)).ToList(); + + var systolicAvg1 = filteredList?.Select(bp => bp.SystolicValue).Average(); + var diastolicAvg1 = filteredList?.Select(bp => bp.DiastolicValue).Average(); + } + // private void WatchEvents(WatchResponse response) // {