Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

BloodpressResolver.cs 7.9KB

pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. 
  2. using HealthMonitor.Common;
  3. using HealthMonitor.Service.Biz.db;
  4. using HealthMonitor.Service.Cache;
  5. using HealthMonitor.Service.Resolver.Interface;
  6. using HealthMonitor.Service.Sub;
  7. using HealthMonitor.Service.Sub.Topic.Model;
  8. using Microsoft.Extensions.Logging;
  9. using Newtonsoft.Json;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Data.Common;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Text.Json.Serialization;
  16. using System.Threading.Tasks;
  17. using TDengineTMQ;
  18. using TelpoDataService.Util.Entities.GpsLocationHistory;
  19. namespace HealthMonitor.Service.Resolver
  20. {
  21. public class BloodpressResolver: IResolver
  22. {
  23. private readonly ILogger<BloodpressResolver> _logger;
  24. private readonly PersonCacheManager _personCacheMgr;
  25. private readonly TDengineService _serviceTDengine;
  26. private readonly BloodPressReferenceValueCacheManager _bpRefValCacheManager;
  27. private AsyncLocal<string> _messageId = new AsyncLocal<string>();
  28. private AsyncLocal<HisGpsBloodPress> _msgData = new AsyncLocal<HisGpsBloodPress>();
  29. public BloodpressResolver(
  30. TDengineService serviceDengine,
  31. BloodPressReferenceValueCacheManager bpRefValCacheManager,
  32. PersonCacheManager personCacheMgr,
  33. ILogger<BloodpressResolver> logger)
  34. {
  35. _serviceTDengine = serviceDengine;
  36. _bpRefValCacheManager = bpRefValCacheManager;
  37. _logger = logger;
  38. _personCacheMgr = personCacheMgr;
  39. }
  40. public void SetResolveInfo(PackageMsgModel msg)
  41. {
  42. var topicHmBloodPress = JsonConvert.DeserializeObject<TopicHmBloodPress>(msg.DetailData.ToString()!);
  43. _messageId.Value = msg.MessageId;
  44. _msgData.Value = new HisGpsBloodPress()
  45. {
  46. BloodPressId = topicHmBloodPress!.BloodPressId,
  47. MessageId = topicHmBloodPress!.MessageId,
  48. Serialno= topicHmBloodPress!.Serialno,
  49. SystolicValue = topicHmBloodPress!.SystolicValue,
  50. DiastolicValue= topicHmBloodPress!.DiastolicValue,
  51. LastUpdate= DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(SafeType.SafeInt64(topicHmBloodPress.LastUpdate) / 1000000),
  52. CreateTime= DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(SafeType.SafeInt64(topicHmBloodPress.CreateTime) / 1000000),
  53. Method= topicHmBloodPress!.Method,
  54. IsDisplay=topicHmBloodPress!.IsDisplay ? 1 : 0
  55. };
  56. Console.WriteLine($"SetResolveInfo" + _messageId.Value);
  57. }
  58. public override string ToString()
  59. {
  60. return $"{nameof(BloodpressResolver)}[{_messageId.Value}]";
  61. }
  62. public async Task ExecuteMessageAsync()
  63. {
  64. var messageId = _messageId.Value;
  65. Console.WriteLine($"ExecuteMessageAsync" + messageId);
  66. var bp = _msgData.Value!;
  67. //#region 获取个人信息
  68. var person = await _personCacheMgr.GetDeviceGpsPersonCacheBySerialNoAsync(bp.MessageId, bp.Serialno).ConfigureAwait(false);
  69. Console.WriteLine(person?.Person.PersonName);
  70. // 验证这个信息是否存在
  71. //if (person == null || person?.Person.BornDate == null)
  72. //{
  73. // Console.WriteLine("验证这个信息是否存在");
  74. // return;
  75. //}
  76. //// 验证年龄是否在范围 (2 - 120)
  77. //var age = SafeType.SafeInt(DateTime.Today.Year - person?.Person.BornDate!.Value.Year!);
  78. //if (age < 1 || age > 120)
  79. //{
  80. // Console.WriteLine("验证年龄是否在范围 (2 - 120)");
  81. //}
  82. //#endregion
  83. //var gender = person?.Person.Gender == true ? 1 : 2;
  84. //var isHypertension = SafeType.SafeBool(person?.Person.Ishypertension!);
  85. //var height = SafeType.SafeDouble(person?.Person.Height!);
  86. //var weight = SafeType.SafeDouble(person?.Person.Weight!);
  87. //#region 计算增量值
  88. //var bpRef = await _bpRefValCacheManager.GetBloodPressReferenceValueAsync(age, gender, isHypertension);
  89. //var systolicRefValue = bpRef?.Systolic;//?
  90. //var diastolicRefValue = bpRef?.Diastolic;//?
  91. //int duration = 30;
  92. //// 获取历史数据
  93. ////DateTime now = DateTime.Now;
  94. //DateTime now = (DateTime)bp.LastUpdate!; //测试
  95. //DateTime startTime = now.AddDays(-duration);
  96. //DateTime endTime = now;
  97. ////
  98. //var systolicAggregate = await _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}'");
  99. //var diastolicAggregate = await _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}'");
  100. //// 最大值
  101. //var systolicMax = systolicAggregate.Max;
  102. //var diastolicMax = diastolicAggregate.Max;
  103. //// 最小值
  104. //var systolicMin = systolicAggregate.Min;
  105. //var diastolicMin = diastolicAggregate.Min;
  106. //// 计算去除最大值和最小值和异常值的平均值
  107. //var systolicAvg = await _serviceTDengine.GetAvgExceptMaxMinValue("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}' and systolic_value < {systolicRefValue} ");
  108. //var diastolicAvg = await _serviceTDengine.GetAvgExceptMaxMinValue("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}' and diastolic_value < {diastolicRefValue}");
  109. //// 偏移参数
  110. //var avgOffset = 0.25M;
  111. //var systolicAvgOffset = avgOffset;
  112. //var diastolicAvgOffset = avgOffset;
  113. //// 增量值=(标定值-平均值)* 0.25
  114. //var systolicInc = systolicAvg.Equals(0M) ? 0 : (int)((systolicRefValue - systolicAvg) * systolicAvgOffset)!;
  115. //var diastolicInc = diastolicAvg.Equals(0M) ? 0 : (int)((diastolicRefValue - diastolicAvg) * diastolicAvgOffset)!;
  116. //#endregion
  117. //#region 插入BP增量值 hm_bloodpress_stats_inc
  118. //var sql = $"INSERT INTO health_monitor.hm_bloodpress_stats_inc VALUES(" +
  119. // $"'{bp.LastUpdate:yyyy-MM-ddTHH:mm:ss.fffZ}'," +
  120. // $"'{bp.BloodPressId}'," +
  121. // $"'{bp.MessageId}'," +
  122. // $"'{bp.Serialno}'," +
  123. // $"{bp.SystolicValue}," +
  124. // $"{systolicRefValue}," +
  125. // $"{systolicAvg}," +
  126. // $"{systolicMax}," +
  127. // $"{systolicMin}," +
  128. // $"{systolicAvgOffset}," +
  129. // $"{systolicInc}," +
  130. // $"{bp.DiastolicValue}," +
  131. // $"{diastolicRefValue}," +
  132. // $"{diastolicAvg}," +
  133. // $"{diastolicMax}," +
  134. // $"{diastolicMin}," +
  135. // $"{diastolicAvgOffset}," +
  136. // $"{diastolicInc}," +
  137. // $"{gender}," +
  138. // $"{age}," +
  139. // $"{height}," +
  140. // $"{weight}," +
  141. // $"'{bp.LastUpdate:yyyy-MM-ddTHH:mm:ss.fffZ}'," +
  142. // $"{duration}," +
  143. // $"'{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}'," +
  144. // $"'{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}'," +
  145. // $"'{string.Empty}')";
  146. //var res = await _serviceTDengine.GernalRestSql(sql);
  147. //#endregion
  148. }
  149. }
  150. }