diff --git a/HealthMonitor.Service/Biz/db/TDengineService.cs b/HealthMonitor.Service/Biz/db/TDengineService.cs index d2c968e..cccdb38 100644 --- a/HealthMonitor.Service/Biz/db/TDengineService.cs +++ b/HealthMonitor.Service/Biz/db/TDengineService.cs @@ -430,9 +430,19 @@ namespace HealthMonitor.Service.Biz.db var result = await GernalRestSqlResTextAsync(sql); var res = JsonConvert.DeserializeObject(result!); + if(res?.Data?.Count==0) return null; List data = res?.Data!; return data[0] as JArray; } + + public async Task GetCount(string tbName, string? condition) + { + var sql = $"SELECT count(ts) FROM {_configTDengineService.DB}.{tbName} WHERE {condition}"; + var result = await GernalRestSqlResTextAsync(sql); + var res = JsonConvert.DeserializeObject(result!); + List data = res?.Data!; + return SafeType.SafeInt(data[0][0]); + } #endregion } diff --git a/HealthMonitor.WebApi/Controllers/HealthMonitor/HmBloodPressConfigManualCalibrationController.cs b/HealthMonitor.WebApi/Controllers/HealthMonitor/HmBloodPressConfigManualCalibrationController.cs index ce8f943..0aefb96 100644 --- a/HealthMonitor.WebApi/Controllers/HealthMonitor/HmBloodPressConfigManualCalibrationController.cs +++ b/HealthMonitor.WebApi/Controllers/HealthMonitor/HmBloodPressConfigManualCalibrationController.cs @@ -15,6 +15,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.EntityFrameworkCore.Metadata; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; @@ -29,26 +30,34 @@ namespace HealthMonitor.WebApi.Controllers.HealthMonitor private readonly ILogger _logger; private readonly PersonCacheManager _personCacheMgr; private readonly TDengineService _serviceTDengine; + private readonly HttpHelper _httpHelper = default!; public HmBloodPressConfigManualCalibrationController ( TDengineService serviceDengine, - PersonCacheManager personCacheMgr, + PersonCacheManager personCacheMgr, HttpHelper httpHelper, + ILogger logger ) { _serviceTDengine = serviceDengine; _logger = logger; + _httpHelper = httpHelper; _personCacheMgr = personCacheMgr; } [HttpPost] - public async Task Put([FromBody] BloodPressManualCalibration model, [FromHeader] string requestId) + public async Task Put([FromBody] BloodPressManualCalibration model, [FromHeader] string requestId) { + /** + 没有血压数据时增量值为0 + */ + - // 保存新的标定值 + + // 保存新的标定值 // 使用新的标定值计算新的增量值 @@ -56,6 +65,9 @@ namespace HealthMonitor.WebApi.Controllers.HealthMonitor // 计算增量值 var systolicRefValue = model.ManualSystolicRefValue;//? var diastolicRefValue = model.ManualDiastolicRefValue;//? + // 没有血压数据时增量值为0 + var systolicInc = 0; + var diastolicInc = 0; //int duration = 7; long duration = 7 * 24 * 3600 * 1000; TimeSpan ts = TimeSpan.FromMilliseconds(duration); @@ -70,79 +82,137 @@ namespace HealthMonitor.WebApi.Controllers.HealthMonitor startTime = newTs; } } - + var lastBP = await _serviceTDengine.GetLastAsync("stb_hm_bloodpress", $"serialno='{imei}' order by ts desc"); - // - //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}'"); + //var total = await _serviceTDengine.GetCount("stb_hm_bloodpress", $"serialno='{imei}' order by ts desc"); + //if (total >= 4) + if(lastBP?.Count !=0 ) + { + //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}'"); + + //// 最大值 + //var systolicMax = systolicAggregate.Max; + //var diastolicMax = diastolicAggregate.Max; + //// 最小值 + //var systolicMin = systolicAggregate.Min; + //var diastolicMin = diastolicAggregate.Min; + + + // 计算去除最大值和最小值和异常值的平均值 + var systolicAvg = await _serviceTDengine.GetAvgExceptMaxMinValueAsync("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}' and systolic_value < {systolicRefValue} "); + var diastolicAvg = await _serviceTDengine.GetAvgExceptMaxMinValueAsync("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}' and diastolic_value < {diastolicRefValue}"); + + if (systolicAvg.Equals(0) || diastolicAvg.Equals(0)) + { + _logger.LogWarning("平均值为0不保存"); + } + // 偏移参数 + var avgOffset = 0.25M; - //// 最大值 - //var systolicMax = systolicAggregate.Max; - //var diastolicMax = diastolicAggregate.Max; - //// 最小值 - //var systolicMin = systolicAggregate.Min; - //var diastolicMin = diastolicAggregate.Min; + var systolicAvgOffset = avgOffset; + var diastolicAvgOffset = avgOffset; - // 计算去除最大值和最小值和异常值的平均值 - var systolicAvg = await _serviceTDengine.GetAvgExceptMaxMinValueAsync("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}' and systolic_value < {systolicRefValue} "); - var diastolicAvg = await _serviceTDengine.GetAvgExceptMaxMinValueAsync("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}' and diastolic_value < {diastolicRefValue}"); + // 增量值=(标定值-平均值)* 0.25 + systolicInc = systolicAvg.Equals(0M) ? 0 : (int)((systolicRefValue - systolicAvg) * systolicAvgOffset)!; + diastolicInc = diastolicAvg.Equals(0M) ? 0 : (int)((diastolicRefValue - diastolicAvg) * diastolicAvgOffset)!; - if (systolicAvg.Equals(0) || diastolicAvg.Equals(0)) + } + + var bpData = new + { + imei = imei, + //systolicCalibrationValue = last?[5], //收缩压标定值,值为0 表示不生效 + //diastolicCalibrationValue = last?[12], //舒张压标定值,值为0表示不生效 + //systolicIncValue = last?[10], //收缩压显示增量,值为0 表示不生效 + //diastolicIncValue = last?[17] //舒张压显示增量,值为0 表示不生效 + + systolicCalibrationValue = systolicRefValue, //收缩压标定值,值为0 表示不生效 + diastolicCalibrationValue = diastolicRefValue, //舒张压标定值,值为0表示不生效 + systolicIncValue = systolicInc, //收缩压显示增量,值为0 表示不生效 + diastolicIncValue = diastolicInc //舒张压显示增量,值为0 表示不生效 + }; + var str = JsonConvert.SerializeObject(bpData); + var url = $"http://id.ssjlai.com/webapi/api/Command/SetBloodPressCalibrationConfig"; + List> headers = new() + { + new KeyValuePair("AuthKey", "key1") + }; +#if DEBUG + var flag = true; +#else + var res = await _httpHelper.HttpToPostAsync(url, bpData, headers).ConfigureAwait(false); + _logger.LogInformation($"向{imei}下发增量值数据:{str},响应:{res}"); + var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken; + var flag = resJToken?["message"]?.ToString().Equals("ok") ?? false; +#endif + + object? response; + if (flag) { - _logger.LogWarning("平均值为0不保存"); + #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 + + // 返回结果 + response = new + { + //Timestamp= DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), + data = new + { + imei = model.Imei, + systolicCalibrationValue = systolicRefValue, //收缩压标定值,值为0 表示不生效 + diastolicCalibrationValue = diastolicRefValue, //舒张压标定值,值为0表示不生效 + systolicIncValue = systolicInc, //收缩压显示增量,值为0 表示不生效 + diastolicIncValue = diastolicInc //舒张压显示增量,值为0 表示不生效 + }, + result = new + { + status = "succeed", + code = 200, + message = "请求成功!" + } + }; } - // 偏移参数 - var avgOffset = 0.25M; - - var systolicAvgOffset = avgOffset; - var diastolicAvgOffset = avgOffset; - - - // 增量值=(标定值-平均值)* 0.25 - 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 + else { - //Timestamp= DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), - data =new - { - imei = model.Imei, - systolicCalibrationValue = systolicRefValue, //收缩压标定值,值为0 表示不生效 - diastolicCalibrationValue = diastolicRefValue, //舒张压标定值,值为0表示不生效 - systolicIncValue = systolicInc, //收缩压显示增量,值为0 表示不生效 - diastolicIncValue = diastolicInc //舒张压显示增量,值为0 表示不生效 - }, - result=new + // 返回结果 + response = new { - status= "succeed", - code=200, - message="请求成功!" - } - }; - - return Ok(res); + //Timestamp= DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), + data = new + { + imei = model.Imei, + systolicCalibrationValue = -1, //收缩压标定值,值为0 表示不生效 + diastolicCalibrationValue = -1, //舒张压标定值,值为0表示不生效 + systolicIncValue = -1, //收缩压显示增量,值为0 表示不生效 + diastolicIncValue = -1 //舒张压显示增量,值为0 表示不生效 + }, + result = new + { + status = "failed", + code = 500, + message = "业务出错!下发指令失败" + } + }; + } + return Ok(response); } } }