diff --git a/HealthMonitor.Service/Biz/db/TDengineService.cs b/HealthMonitor.Service/Biz/db/TDengineService.cs index a44babd..d2c968e 100644 --- a/HealthMonitor.Service/Biz/db/TDengineService.cs +++ b/HealthMonitor.Service/Biz/db/TDengineService.cs @@ -371,7 +371,13 @@ namespace HealthMonitor.Service.Biz.db var result = await _httpHelper.HttpToPostAsync(url, sql, headers).ConfigureAwait(false); return result; } - + /// + /// 最大值,最小值聚合 + /// + /// + /// + /// + /// public async Task GetAggregateValueAsync(string field,string tbName,string? condition) { var sql = $"SELECT MAX({field}), MIN({field}) FROM {_configTDengineService.DB}.{tbName} WHERE {condition}"; @@ -387,7 +393,13 @@ namespace HealthMonitor.Service.Biz.db Min = data.Count.Equals(0) ? 0 : data[0][1], }; } - + /// + /// 去除最大值和最小值后的平均值 + /// + /// + /// + /// + /// public async Task GetAvgExceptMaxMinValueAsync(string field, string tbName, string? condition) { @@ -405,6 +417,12 @@ namespace HealthMonitor.Service.Biz.db return data.Count.Equals(0)?0:(int)data[0][0]; } + /// + /// 获取最后的记录 + /// + /// + /// + /// public async Task GetLastAsync(string tbName, string? condition) { var sql = $"SELECT last_row(*) FROM {_configTDengineService.DB}.{tbName} WHERE {condition}"; diff --git a/HealthMonitor.Service/Resolver/BloodpressResolver.cs b/HealthMonitor.Service/Resolver/BloodpressResolver.cs index a27376b..7177df2 100644 --- a/HealthMonitor.Service/Resolver/BloodpressResolver.cs +++ b/HealthMonitor.Service/Resolver/BloodpressResolver.cs @@ -7,6 +7,7 @@ using HealthMonitor.Service.Etcd; using HealthMonitor.Service.Resolver.Interface; using HealthMonitor.Service.Sub; using HealthMonitor.Service.Sub.Topic.Model; +using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System; @@ -103,11 +104,29 @@ namespace HealthMonitor.Service.Resolver var weight = SafeType.SafeDouble(person?.Person.Weight!); #region 计算增量值 + var bpRef = await _bpRefValCacheManager.GetBloodPressReferenceValueAsync(age, gender, isHypertension); + var last = await _serviceTDengine.GetLastAsync("stb_hm_bp_push_ref_inc_value", $"serialno='{bp.Serialno}' order by ts desc"); + //var ts = last?[0]; var systolicRefValue = bpRef?.Systolic;//? var diastolicRefValue = bpRef?.Diastolic;//? - int duration = 7; + long duration = 7 * 24 * 3600 * 1000; + + // 曾经有下发记录 + if (last?.Count!=0) + { + if (DateTime.TryParse(last?[0]!.ToString(), out DateTime newTs)) + { + systolicRefValue = (int)last?[2]!; + diastolicRefValue = (int)last?[3]!; + duration = SafeType.SafeInt64(((DateTime)bp.LastUpdate! - newTs).TotalMilliseconds); + } + + } + + + TimeSpan ts= TimeSpan.FromMilliseconds(duration); // 获取历史数据 ////DateTime now = DateTime.Now; //DateTime now = (DateTime)bp.LastUpdate!; //测试 @@ -115,8 +134,10 @@ namespace HealthMonitor.Service.Resolver //DateTime endTime = now; DateTime endTime = (DateTime)bp.LastUpdate!; //测试 - DateTime startTime = endTime.AddDays(-duration); - + DateTime startTime = endTime-ts; + + + // 如果hm_bp_config_manual_calibration存在数据,使用最新数据 // var systolicAggregate = await _serviceTDengine.GetAggregateValueAsync("systolic_value", "stb_hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}'"); diff --git a/HealthMonitor.WebApi/Controllers/HealthMonitor/HmBloodPressConfigManualCalibrationController.cs b/HealthMonitor.WebApi/Controllers/HealthMonitor/HmBloodPressConfigManualCalibrationController.cs index c7c8c8c..ce8f943 100644 --- a/HealthMonitor.WebApi/Controllers/HealthMonitor/HmBloodPressConfigManualCalibrationController.cs +++ b/HealthMonitor.WebApi/Controllers/HealthMonitor/HmBloodPressConfigManualCalibrationController.cs @@ -1,5 +1,6 @@ using Google.Protobuf.WellKnownTypes; using Grpc.Core; +using HealthMonitor.Common; using HealthMonitor.Common.helper; using HealthMonitor.Core.Dal; using HealthMonitor.Service.Biz.db; @@ -45,19 +46,36 @@ namespace HealthMonitor.WebApi.Controllers.HealthMonitor [HttpPost] public async Task Put([FromBody] BloodPressManualCalibration model, [FromHeader] string requestId) { + + // 保存新的标定值 + + // 使用新的标定值计算新的增量值 + + var imei = model.Imei; // 计算增量值 var systolicRefValue = model.ManualSystolicRefValue;//? var diastolicRefValue = model.ManualDiastolicRefValue;//? - int duration = 7; - + //int duration = 7; + long duration = 7 * 24 * 3600 * 1000; + TimeSpan ts = TimeSpan.FromMilliseconds(duration); DateTime endTime = DateTime.Now; //测试 - DateTime startTime = endTime.AddDays(-duration); + DateTime startTime = endTime - ts; + + var last = await _serviceTDengine.GetLastAsync("stb_hm_bp_push_ref_inc_value", $"serialno='{imei}' order by ts desc"); + if (last?.Count != 0) + { + if (DateTime.TryParse(last?[0]!.ToString(), out DateTime newTs)) + { + startTime = newTs; + } + } + // - var systolicAggregate = await _serviceTDengine.GetAggregateValueAsync("systolic_value", "stb_hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{imei}'"); - var diastolicAggregate = await _serviceTDengine.GetAggregateValueAsync("diastolic_value", "stb_hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{imei}'"); + //var systolicAggregate = await _serviceTDengine.GetAggregateValueAsync("systolic_value", "stb_hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{imei}'"); + //var diastolicAggregate = await _serviceTDengine.GetAggregateValueAsync("diastolic_value", "stb_hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{imei}'"); //var systolicAggregate = _serviceTDengine.GetAggregateValue("systolic_value", "hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}'"); //var diastolicAggregate = _serviceTDengine.GetAggregateValue("diastolic_value", "hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}'"); @@ -88,6 +106,21 @@ namespace HealthMonitor.WebApi.Controllers.HealthMonitor var systolicInc = systolicAvg.Equals(0M) ? 0 : (int)((systolicRefValue - systolicAvg) * systolicAvgOffset)!; var diastolicInc = diastolicAvg.Equals(0M) ? 0 : (int)((diastolicRefValue - diastolicAvg) * diastolicAvgOffset)!; + #region 保存下推记录 stb_hm_bp_push_ref_inc_value + var sql = $"INSERT INTO health_monitor.hm_bp_push_ref_inc_value_{imei.Substring(imei.Length - 2)} " + + $"USING health_monitor.stb_hm_bp_push_ref_inc_value " + + $"TAGS ('{imei.Substring(imei.Length - 2)}') " + + $"VALUES(" + + $"'{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}'," + + $"'{imei}'," + + $"{systolicRefValue}," + + $"{diastolicRefValue}," + + $"{systolicInc}," + + $"{diastolicInc}," + + $"{true})"; + + _serviceTDengine.ExecuteInsertSQL(sql); + #endregion // 返回结果 var res = new diff --git a/HealthMonitor.WebApi/Worker.cs b/HealthMonitor.WebApi/Worker.cs index 4140fcf..2cfea16 100644 --- a/HealthMonitor.WebApi/Worker.cs +++ b/HealthMonitor.WebApi/Worker.cs @@ -1,14 +1,17 @@ using dotnet_etcd; using Etcdserverpb; +using Google.Protobuf.WellKnownTypes; using HealthMonitor.Common; using HealthMonitor.Common.helper; using HealthMonitor.Core.Common.Extensions; using HealthMonitor.Service.Biz.db; using HealthMonitor.Service.Etcd; using HealthMonitor.Service.Sub; +using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.EntityFrameworkCore.Metadata.Internal; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using System.Reflection; using System.Threading.Channels; using TDengineDriver; using TDengineTMQ; @@ -162,13 +165,23 @@ namespace HealthMonitor.WebApi if (newTs.AddDays(7) > DateTime.Now) { Console.WriteLine(ts); + var systolic_ref_value = last?[5]; + var diastolic_ref_value = last?[12]; + var systolic_inc_value = last?[10]; + var diastolic_inc_value = last?[17]; + var bpData = new { imei = imeiDel, - systolicCalibrationValue = last?[5], //收缩压标定值,值为0 表示不生效 - diastolicCalibrationValue = last?[12], //舒张压标定值,值为0表示不生效 - systolicIncValue = last?[10], //收缩压显示增量,值为0 表示不生效 - diastolicIncValue = last?[17] //舒张压显示增量,值为0 表示不生效 + //systolicCalibrationValue = last?[5], //收缩压标定值,值为0 表示不生效 + //diastolicCalibrationValue = last?[12], //舒张压标定值,值为0表示不生效 + //systolicIncValue = last?[10], //收缩压显示增量,值为0 表示不生效 + //diastolicIncValue = last?[17] //舒张压显示增量,值为0 表示不生效 + + systolicCalibrationValue = systolic_ref_value, //收缩压标定值,值为0 表示不生效 + diastolicCalibrationValue = diastolic_ref_value, //舒张压标定值,值为0表示不生效 + systolicIncValue = systolic_inc_value, //收缩压显示增量,值为0 表示不生效 + diastolicIncValue = diastolic_inc_value //舒张压显示增量,值为0 表示不生效 }; var str = JsonConvert.SerializeObject(bpData); var url = $"http://id.ssjlai.com/webapi/api/Command/SetBloodPressCalibrationConfig"; @@ -181,6 +194,23 @@ namespace HealthMonitor.WebApi var resJToken = JsonConvert.DeserializeObject(res!) as JToken; if (resJToken!["message"]!.ToString().Equals("ok")) { + + #region 保存下推记录 stb_hm_bp_push_ref_inc_value + var sql = $"INSERT INTO health_monitor.hm_bp_push_ref_inc_value_{imeiDel.Substring(imeiDel.Length - 2)} " + + $"USING health_monitor.stb_hm_bp_push_ref_inc_value " + + $"TAGS ('{imeiDel.Substring(imeiDel.Length - 2)}') " + + $"VALUES(" + + $"'{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}'," + + $"'{imeiDel}'," + + $"{systolic_ref_value}," + + $"{diastolic_ref_value}," + + $"{systolic_inc_value}," + + $"{diastolic_inc_value}," + + $"{false})"; + + _serviceTDengine.ExecuteInsertSQL(sql); + #endregion + // 注册下次下推 var endTime = DateTime.Now; #if DEBUG