Browse Source

初始化remark

datasub12_previous
H Vs 1 year ago
parent
commit
4fcb052e13
2 changed files with 170 additions and 13 deletions
  1. +64
    -0
      HealthMonitor.Service/Biz/IotWebApiService.cs
  2. +106
    -13
      HealthMonitor.Service/Resolver/BloodpressResolver.cs

+ 64
- 0
HealthMonitor.Service/Biz/IotWebApiService.cs View File

@@ -163,8 +163,72 @@ namespace HealthMonitor.Service.Biz
_logger.LogError($"{nameof(UpdatePersonRemarksAsync)} 更新个人信息异常:{ex.Message}, {ex.StackTrace}");
}
return flag;
}

/// <summary>
/// 初次开通
/// </summary>
/// <param name="imei"></param>
/// <param name="systolicRefValue"></param>
/// <param name="diastolicRefValue"></param>
/// <param name="systolicIncValue"></param>
/// <param name="diastolicIncValue"></param>
/// <returns></returns>
public async Task<bool> UpdatePersonRemarksAsync(string imei, int systolicRefValue, int diastolicRefValue,int systolicIncValue,int diastolicIncValue)
{
var flag = false;
try
{
GeneralParam condition = new()
{
Filters = new List<QueryFilterCondition> {
new QueryFilterCondition {
Key=nameof(GpsDevice.Serialno),
Value=imei,
Operator= QueryOperatorEnum.Equal,
ValueType=QueryValueTypeEnum.String
}
},
OrderBys = new List<OrderByCondition> { new OrderByCondition { Key = "serialno", IsDesc = true } }

};
var person = await _gpsPersonApiClient.GetFirstAsync(condition, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
// 若remark为空,更新person remark字段
if (string.IsNullOrWhiteSpace(person?.Remarks))
{
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 表示不生效
}
};
person!.Remarks = $"is_blood_press:{JsonConvert.SerializeObject(newRemarkData)}|";
await _gpsPersonApiClient.UpdateAsync(person, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
_logger.LogInformation($"更新Person remarks字段|{person.Remarks}");

// 更新缓存
var url = $"{_configService.IotWebApiUrl}Device/UpdatePersonInfoCache?imei={imei}";
List<KeyValuePair<string, string>> headers = new()
{
new KeyValuePair<string, string>("AuthKey", "key1")
};
var res = await _httpHelper.HttpToGetAsync(url, headers).ConfigureAwait(false);
_logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonInfoCacheAsync)},响应:{res}");
var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
flag = resJToken?["message"]?.ToString().Equals("ok") ?? false;
}
}
catch (Exception ex)
{
_logger.LogError($"{nameof(UpdatePersonRemarksAsync)} 更新个人信息异常:{ex.Message}, {ex.StackTrace}");
}
return flag;
}
}
}

+ 106
- 13
HealthMonitor.Service/Resolver/BloodpressResolver.cs View File

@@ -18,7 +18,11 @@ using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using TDengineTMQ;
using TelpoDataService.Util.Entities.GpsCard;
using TelpoDataService.Util;
using TelpoDataService.Util.Entities.GpsLocationHistory;
using HealthMonitor.Service.Biz;
using HealthMonitor.Model.Service;

namespace HealthMonitor.Service.Resolver
{
@@ -32,6 +36,9 @@ namespace HealthMonitor.Service.Resolver

private readonly HttpHelper _httpHelper = default!;

private readonly GpsCardAccessorClient<GpsPerson> _gpsPersonApiClient;
private readonly IotWebApiService _serviceIotWebApi;

private readonly AsyncLocal<string> _messageId = new();
private readonly AsyncLocal<HisGpsBloodPress> _msgData = new();

@@ -41,15 +48,21 @@ namespace HealthMonitor.Service.Resolver
TDengineService serviceDengine,
BloodPressReferenceValueCacheManager bpRefValCacheManager,
PersonCacheManager personCacheMgr, HttpHelper httpHelper,
GpsCardAccessorClient<GpsPerson> gpsPersonApiClient,
IotWebApiService iotWebApiService,
EtcdService serviceEtcd,

ILogger<BloodpressResolver> logger)
{
_httpHelper = httpHelper;
_serviceTDengine = serviceDengine;
_bpRefValCacheManager = bpRefValCacheManager;
_gpsPersonApiClient = gpsPersonApiClient;
_serviceIotWebApi = iotWebApiService;
_logger = logger;
_personCacheMgr = personCacheMgr;
_serviceEtcd = serviceEtcd;

}

public void SetResolveInfo(PackageMsgModel msg)
@@ -113,6 +126,12 @@ namespace HealthMonitor.Service.Resolver
var diastolicRefValue = bpRef?.Diastolic;//?
long duration = 7 * 24 * 3600 * 1000;

int systolicInc;
int diastolicInc;
string sql = string.Empty;

var remarkFlag = false;

// 曾经有下发记录
if (last?.Count!=0)
{
@@ -153,6 +172,13 @@ namespace HealthMonitor.Service.Resolver
var diastolicMin = diastolicAggregate.Min;


// 偏移参数
var avgOffset = 0.25M;

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='{bp.Serialno}' 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='{bp.Serialno}' and diastolic_value < {diastolicRefValue}");
@@ -160,6 +186,47 @@ namespace HealthMonitor.Service.Resolver
if (systolicAvg.Equals(0) || diastolicAvg.Equals(0))
{
_logger.LogWarning("平均值为0不保存");
systolicAvg = bp.SystolicValue;
diastolicAvg = bp.DiastolicValue;
systolicInc = (int)((systolicRefValue - systolicAvg) * systolicAvgOffset)! > 0? (int)((systolicRefValue - systolicAvg) * systolicAvgOffset)!:0;
diastolicInc = (int)((diastolicRefValue - diastolicAvg) * diastolicAvgOffset)! >0? (int)((diastolicRefValue - diastolicAvg) * diastolicAvgOffset)!:0;
// 初始化 remark
remarkFlag = await _serviceIotWebApi.UpdatePersonRemarksAsync(bp.Serialno, (int)systolicRefValue!, (int)diastolicRefValue!, systolicInc, diastolicInc).ConfigureAwait(false);
if (remarkFlag)
{
// 下推
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)
{
#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(" +
$"'{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}'," +
$"'{bp.Serialno}'," +
$"{systolicRefValue}," +
$"{diastolicRefValue}," +
$"{systolicInc}," +
$"{diastolicInc}," +
$"{true})";

_serviceTDengine.ExecuteInsertSQL(sql);
#endregion
}
}

return;
}

@@ -169,27 +236,18 @@ namespace HealthMonitor.Service.Resolver



// 偏移参数
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)!;
systolicInc = systolicAvg.Equals(0M) ? 0 : (int)((systolicRefValue - systolicAvg) * systolicAvgOffset)!;
diastolicInc = diastolicAvg.Equals(0M) ? 0 : (int)((diastolicRefValue - diastolicAvg) * diastolicAvgOffset)!;
#endregion






#region 插入BP增量值 hm_bloodpress_stats_inc
// 自动建表
var sql = $"INSERT INTO health_monitor.hm_bp_stats_inc_{bp.Serialno.Substring(bp.Serialno.Length - 2)} " +
sql = $"INSERT INTO health_monitor.hm_bp_stats_inc_{bp.Serialno.Substring(bp.Serialno.Length - 2)} " +
$"USING health_monitor.stb_hm_bloodpress_stats_inc " +
$"TAGS ('{bp.Serialno.Substring(bp.Serialno.Length - 2)}') " +
$"VALUES(" +
@@ -226,6 +284,41 @@ namespace HealthMonitor.Service.Resolver
// 发送到 设置设备血压标定参数

#endregion
// 初始化 remark
remarkFlag = await _serviceIotWebApi.UpdatePersonRemarksAsync(bp.Serialno, (int)systolicRefValue!, (int)diastolicRefValue!, systolicInc, diastolicInc).ConfigureAwait(false);
if (remarkFlag)
{
// 下推
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)
{
#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(" +
$"'{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}'," +
$"'{bp.Serialno}'," +
$"{systolicRefValue}," +
$"{diastolicRefValue}," +
$"{systolicInc}," +
$"{diastolicInc}," +
$"{true})";

_serviceTDengine.ExecuteInsertSQL(sql);
#endregion
}
}

// 注册定时下发事件
// 获取当前时间
@@ -266,7 +359,7 @@ namespace HealthMonitor.Service.Resolver

//var result = JsonConvert.SerializeObject(data);
//var result = bp.Serialno;
var key = $"health_moniter/schedule_push/imei/{bp.Serialno}";
var schedule_push = await _serviceEtcd.GetValAsync(key).ConfigureAwait(false);
if (string.IsNullOrWhiteSpace(schedule_push))


Loading…
Cancel
Save