From e916cb83db5d1c2aec55c349ef0fe27dd9905b08 Mon Sep 17 00:00:00 2001 From: H Vs Date: Wed, 6 Dec 2023 21:53:36 +0800 Subject: [PATCH] =?UTF-8?q?remark=E5=A2=9E=E5=8A=A0lastPushRefValue?= =?UTF-8?q?=E5=88=A4=E5=AE=9A=E6=98=AF=E5=90=A6=E4=BD=BF=E7=94=A8=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E6=A0=87=E5=AE=9A=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HealthMonitor.Service/Biz/IotWebApiService.cs | 82 ++++++++++++++++++- .../Resolver/BloodpressResolver.cs | 71 +++++++++++++++- ...dPressConfigManualCalibrationController.cs | 59 +++++++------ 3 files changed, 179 insertions(+), 33 deletions(-) diff --git a/HealthMonitor.Service/Biz/IotWebApiService.cs b/HealthMonitor.Service/Biz/IotWebApiService.cs index 3dabfd3..fc835f2 100644 --- a/HealthMonitor.Service/Biz/IotWebApiService.cs +++ b/HealthMonitor.Service/Biz/IotWebApiService.cs @@ -264,7 +264,7 @@ namespace HealthMonitor.Service.Biz // } // return flag; //} - + /** /// /// 更新 gps_person remark缓存和数据库 /// @@ -342,6 +342,86 @@ namespace HealthMonitor.Service.Biz } return flag; } + */ + /// + /// 更新 gps_person remark缓存和数据库 + /// + /// + /// + /// + /// + /// + /// + /// + public async Task UpdatePersonRemarksAsync(string imei, int systolicRefValue, int diastolicRefValue, int systolicIncValue, int diastolicIncValue, string remarks= "is_blood_press") + { + var flag = false; + try + { + + // 保证实时性,先更新缓存,再更新数据库 + var personCache = await _personCacheMgr.GetDeviceGpsPersonCacheObjectBySerialNoAsync(new Guid().ToString(), imei).ConfigureAwait(false); + + if (personCache == null) + { + _logger.LogInformation($"{imei} -- Person remarks数据异常,检查缓存和数据库"); + } + else if (string.IsNullOrWhiteSpace(personCache["person"]!["remarks"]!.ToString())) + { + var newRemarkData = new + { + imei, + time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), + commandValue = new + { + systolicCalibrationValue = systolicRefValue, //收缩压标定值,值为0 表示不生效 + diastolicCalibrationValue = diastolicRefValue, //舒张压标定值,值为0表示不生效 + systolicIncValue, //收缩压显示增量,值为0 表示不生效 + diastolicIncValue //舒张压显示增量,值为0 表示不生效 + } + }; + var newRemarkStr = $"{remarks}:{JsonConvert.SerializeObject(newRemarkData)}|"; + + personCache["person"]!["remarks"] = newRemarkStr; + bool cacheFlag = await _personCacheMgr.UpdateDeviceGpsPersonCacheObjectBySerialNoAsync(personCache, imei); + if (cacheFlag) + { + GeneralParam condition = new() + { + Filters = new List { + new QueryFilterCondition { + Key=nameof(GpsDevice.Serialno), + Value=imei, + Operator= QueryOperatorEnum.Equal, + ValueType=QueryValueTypeEnum.String + } + }, + OrderBys = new List { new OrderByCondition { Key = "serialno", IsDesc = true } } + + }; + _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonRemarksAsync)}成功,{JsonConvert.SerializeObject(personCache)}"); + // 读取数据库 + var person = await _gpsPersonApiClient.GetFirstAsync(condition, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false); + // 更新字段 + person!.Remarks = newRemarkStr; + await _gpsPersonApiClient.UpdateAsync(person, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false); + _logger.LogInformation($"{imei} 更新Person remarks字段|{person.Remarks}"); + + } + else + { + _logger.LogInformation($"{imei} 更新缓存和数据库{nameof(UpdatePersonRemarksAsync)}失败,{JsonConvert.SerializeObject(personCache)}"); + } + flag = cacheFlag; + } + } + catch (Exception ex) + { + _logger.LogError($"{nameof(UpdatePersonRemarksAsync)} {imei}--更新个人信息异常:{ex.Message}, {ex.StackTrace}"); + } + return flag; + } + /** 取消 public async Task UpdatePersonInfoCacheAsync(string imei) { diff --git a/HealthMonitor.Service/Resolver/BloodpressResolver.cs b/HealthMonitor.Service/Resolver/BloodpressResolver.cs index 086da2e..fd70290 100644 --- a/HealthMonitor.Service/Resolver/BloodpressResolver.cs +++ b/HealthMonitor.Service/Resolver/BloodpressResolver.cs @@ -105,8 +105,8 @@ namespace HealthMonitor.Service.Resolver int diastolicRefValue; int systolicInc; int diastolicInc; - int systolicAvg; - int diastolicAvg; + decimal systolicAvg; + decimal diastolicAvg; int systolicMax = 0; int diastolicMax = 0; @@ -225,6 +225,73 @@ namespace HealthMonitor.Service.Resolver #endregion } + else if (person.Person.Remarks.Contains("lastPushRefValue")) + { + _logger.LogInformation($"{bp.Serialno},有新标定值(lastPushRefValue),使用最近一次下推的标定值和增量值(测量值=平均值计算得出的)"); + var lastPushResponse = await _serviceTDengine.ExecuteSelectRestResponseAsync("stb_hm_bp_push_ref_inc_value", $"serialno='{bp.Serialno}' order by ts desc", "last_row(*)"); + var lastPushParser = JsonConvert.DeserializeObject>(lastPushResponse!); + var lastPush = lastPushParser!.Select().FirstOrDefault(); + + var lastPushSystolicRefValue = lastPush!.SystolicRefValue; + var lastPushDiastolicRefValue = lastPush!.SystolicRefValue; + // 测量值当作平均值 + systolicAvg = bp.SystolicValue; + diastolicAvg = bp.DiastolicValue; + systolicInc = (int)((systolicRefValue - systolicAvg) * systolicAvgOffset)!; + diastolicInc = (int)((diastolicRefValue - diastolicAvg) * diastolicAvgOffset)!; + #region 更新 gps_persoon remarks 下发增量值到iot + // 更新 + remarkFlag = await _serviceIotWebApi.UpdatePersonRemarksAsync(bp.Serialno, (int)systolicRefValue!, (int)diastolicRefValue!, systolicInc, diastolicInc).ConfigureAwait(false); + if (remarkFlag) + { + _logger.LogInformation($"{nameof(BloodpressResolver)} 开启血压标定值下发: {_configBoodPressResolver.EnableBPRefPush}"); + // 启血压标定值下发开关 + if (_configBoodPressResolver.EnableBPRefPush) + { + // 下推 + BloodPressCalibrationConfigModel bpIncData = new() + { + Imei = bp.Serialno, + SystolicRefValue = (int)systolicRefValue!, //收缩压标定值,值为0 表示不生效 + DiastolicRefValue = (int)diastolicRefValue!, //舒张压标定值,值为0表示不生效 + SystolicIncValue = systolicInc, //收缩压显示增量,值为0 表示不生效 + DiastolicIncValue = diastolicInc //舒张压显示增量,值为0 表示不生效 + }; + // 下发 IOT 增量值 + var flagIot = await _serviceIotWebApi.SetBloodPressCalibrationConfigAsync(bpIncData).ConfigureAwait(false); + if (flagIot) + { + startTime = (DateTime)bp.LastUpdate!; + endTime = DateTime.Now; + #region 保存下推记录 stb_hm_bp_push_ref_inc_value + sql = $"INSERT INTO health_monitor.hm_bp_push_ref_inc_value_{bp.Serialno.Substring(bp.Serialno.Length - 2)} " + + $"USING health_monitor.stb_hm_bp_push_ref_inc_value " + + $"TAGS ('{bp.Serialno.Substring(bp.Serialno.Length - 2)}') " + + $"VALUES(" + + $"'{endTime:yyyy-MM-dd HH:mm:ss.fff}'," + + $"'{bp.Serialno}'," + + $"{systolicRefValue}," + + $"{diastolicRefValue}," + + $"{systolicInc}," + + $"{diastolicInc}," + + $"{true}," + + $"{systolicAvg}," + + $"{diastolicAvg}," + + $"{systolicAvgOffset}," + + $"{diastolicAvgOffset}," + + $"'{startTime:yyyy-MM-dd HH:mm:ss.fff}'," + + $"'{endTime:yyyy-MM-dd HH:mm:ss.fff}'" + + $")"; + + _serviceTDengine.ExecuteInsertSQL(sql); + #endregion + } + } + + } + + #endregion + } else { #region (暂时取消)正常计算增量值 diff --git a/HealthMonitor.WebApi/Controllers/HealthMonitor/HmBloodPressConfigManualCalibrationController.cs b/HealthMonitor.WebApi/Controllers/HealthMonitor/HmBloodPressConfigManualCalibrationController.cs index f9139e3..fcf8c51 100644 --- a/HealthMonitor.WebApi/Controllers/HealthMonitor/HmBloodPressConfigManualCalibrationController.cs +++ b/HealthMonitor.WebApi/Controllers/HealthMonitor/HmBloodPressConfigManualCalibrationController.cs @@ -190,8 +190,6 @@ namespace HealthMonitor.WebApi.Controllers.HealthMonitor var hmBpParser = JsonConvert.DeserializeObject>(hmBpResponse!); if (hmBpParser!.Rows >= 5) { - - //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!); @@ -201,23 +199,24 @@ namespace HealthMonitor.WebApi.Controllers.HealthMonitor // 数据不足等同进行初始化,只下发标定值,增量值为0;remarks恢复到未校准状态,需要基于第一个测量值生成增量值。 if (systolicAvg.Equals(0)) { - _logger.LogInformation($"测量数据样本不足,将进行标定值初始,只下发标定值,增量值为0,不更新remark"); + _logger.LogInformation($"测量数据样本不足,将进行标定值初始,只下发标定值,增量值为0,更新remark (lastPushRefValue)"); // 重置设备 - var statNow = DateTime.Now; - await IotSetBloodPressCalibrationConfigResponseAsync(imei, 0, 0, 0, 0, 0, 0, 0, 0, statNow, statNow).ConfigureAwait(false); - _logger.LogInformation($"1.测量数据样本不足,向IOT重置设备发送0,不更新remark"); - - //var initRemarksFlag = await _serviceIotWebApi.UpdatePersonRemarksAsync(imei, 0, 0, 0, 0, true).ConfigureAwait(false); - //if (initRemarksFlag) - //{ - // _logger.LogInformation($"2.测量数据样本不足,remarks恢复到未校准状态(remarks设置为空),成功"); - //} - //else - //{ - // _logger.LogInformation($"2.测量数据样本不足,remarks恢复到未校准状态(remarks设置为空),失败"); - //} + // var statNow = DateTime.Now; + //await IotSetBloodPressCalibrationConfigResponseAsync(imei, 0, 0, 0, 0, 0, 0, 0, 0, statNow, statNow).ConfigureAwait(false); + _logger.LogInformation($"1.测量数据样本不足,更新remark (lastPushRefValue)"); + + var initRemarksFlag = await _serviceIotWebApi.UpdatePersonRemarksAsync(imei, 0, 0, 0, 0, "lastPushRefValue").ConfigureAwait(false); + if (initRemarksFlag) + { + _logger.LogInformation($"2.测量数据样本不足,remarks恢复到未校准状态(remarks设置为空),成功"); + } + else + { + _logger.LogInformation($"2.测量数据样本不足,remarks恢复到未校准状态(remarks设置为空),失败"); + } _logger.LogInformation($"2.测量数据样本不足,只下发手动标定值systolicRefValue:{systolicRefValue} -- diastolicRefValue:{diastolicRefValue},增量值为0"); - statNow = statNow.AddSeconds(3); + //statNow = statNow.AddSeconds(3); + var statNow = DateTime.Now; return await IotSetBloodPressCalibrationConfigResponseAsync(imei, systolicRefValue, diastolicRefValue, 0, 0, 0, 0, 0, 0, statNow, statNow).ConfigureAwait(false); } @@ -236,21 +235,21 @@ namespace HealthMonitor.WebApi.Controllers.HealthMonitor } else { - _logger.LogInformation($"没有符合条件的测试量数据,将进行标定值初始,只下发标定值,增量值为0;不更新remark"); + _logger.LogInformation($"没有符合条件的测试量数据(少于5条),将进行标定值初始,只下发标定值,增量值为0;更新remark (lastPushRefValue)"); var statNow = DateTime.Now; - await IotSetBloodPressCalibrationConfigResponseAsync(imei, 0, 0, 0, 0, 0, 0, 0, 0, statNow, statNow).ConfigureAwait(false); - _logger.LogInformation($"1.没有符合条件的测试量数据,向IOT重置设备发送0,不更新remark"); + // await IotSetBloodPressCalibrationConfigResponseAsync(imei, 0, 0, 0, 0, 0, 0, 0, 0, statNow, statNow).ConfigureAwait(false); + _logger.LogInformation($"1.没有符合条件的测试量数据(少于5条),更新remark (lastPushRefValue)"); - //var initRemarksFlag = await _serviceIotWebApi.UpdatePersonRemarksAsync(imei, 0, 0, 0, 0, true).ConfigureAwait(false); - //if (initRemarksFlag) - //{ - // _logger.LogInformation($"2.没有符合条件的测试量数据,remarks恢复到未校准状态(remarks设置为空),成功"); - //} - //else - //{ - // _logger.LogInformation($"2.没有符合条件的测试量数据,remarks恢复到未校准状态(remarks设置为空),失败"); - //} - _logger.LogInformation($"2.没有符合条件的测试量数据,只下发手动标定值systolicRefValue:{systolicRefValue} -- diastolicRefValue:{diastolicRefValue},增量值为0"); + var initRemarksFlag = await _serviceIotWebApi.UpdatePersonRemarksAsync(imei, 0, 0, 0, 0, "lastPushRefValue").ConfigureAwait(false); + if (initRemarksFlag) + { + _logger.LogInformation($"2.没有符合条件的测试量数据(少于5条),remarks恢复到未校准状态(remarks设置为空),成功"); + } + else + { + _logger.LogInformation($"2.没有符合条件的测试量数据(少于5条),remarks恢复到未校准状态(remarks设置为空),失败"); + } + _logger.LogInformation($"2.没有符合条件的测试量数据(少于5条),只下发手动标定值systolicRefValue:{systolicRefValue} -- diastolicRefValue:{diastolicRefValue},增量值为0"); statNow = statNow.AddSeconds(3); return await IotSetBloodPressCalibrationConfigResponseAsync(imei, systolicRefValue, diastolicRefValue, 0, 0, 0, 0, 0, 0, statNow, statNow).ConfigureAwait(false);