Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

PersonCacheManager.cs 8.4KB

1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
3 meses atrás
3 meses atrás
3 meses atrás
3 meses atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using CSRedis;
  2. using HealthMonitor.Model.Cache;
  3. using HealthMonitor.Model.Config;
  4. using Microsoft.Extensions.Logging;
  5. using Microsoft.Extensions.Options;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Net.NetworkInformation;
  11. using System.Threading.Tasks;
  12. using TelpoDataService.Util;
  13. using TelpoDataService.Util.Clients;
  14. using TelpoDataService.Util.Entities.GpsCard;
  15. using TelpoDataService.Util.Entities.GpsLocationHistory;
  16. using TelpoDataService.Util.Models;
  17. using TelpoDataService.Util.QueryObjects;
  18. namespace HealthMonitor.Service.Cache
  19. {
  20. public class PersonCacheManager
  21. {
  22. //注意要改用 redis DB7 数据库 ,Prefix=TELPO
  23. private const string CACHE_HASH_KEY_GPSDEVICEPERSON = "#GPSDEVICE_PERSON_HASH";
  24. #region db10
  25. // 周期性体温
  26. private const string CACHE_HASH_KEY_TEMP_PERIODICITY = "#HM_PERIODICITY_TEMP_HASH";
  27. // 周期性步数
  28. private const string CACHE_HASH_KEY_STEP_PERIODICITY = "#HM_PERIODICITY_STEP_HASH";
  29. // 周期性血压
  30. private const string CACHE_HASH_KEY_BLOODPRESS_PERIODICITY = "#HM_PERIODICITY_BLOODPRESS_HASH";
  31. #endregion
  32. private readonly GpsCardAccessorClient<GpsPerson> _personApiClient;
  33. private readonly ILogger<PersonCacheManager> _logger;
  34. public PersonCacheManager(
  35. GpsCardAccessorClient<GpsPerson> personApiClient,
  36. ILogger<PersonCacheManager> logger)
  37. {
  38. _personApiClient = personApiClient;
  39. _logger = logger;
  40. }
  41. //public async Task<GpsPerson?> GetPersonBySerialNoAsync(string messageId, string sn)
  42. //{
  43. // if (string.IsNullOrWhiteSpace(sn)) return null;
  44. // // 切换redis DB7 数据库和前缀 "TELPO"
  45. // // 增加容错,防止Redis宕机造成业务中断
  46. // try
  47. // {
  48. // return await csRedisDb7.HGetAsync<GpsPerson>(CACHE_HASH_KEY_GPSDEVICEPERSON, sn);
  49. // }
  50. // catch (Exception ex)
  51. // {
  52. // _logger.LogWarning($"Redis发生异常,将直接读取MySQL数据库,构造新实例:{ex.Message}, {ex.StackTrace}");
  53. // var param = new GeneralParam
  54. // {
  55. // Filters = new List<QueryFilterCondition>
  56. // {
  57. // new QueryFilterCondition
  58. // {
  59. // Key=nameof(GpsPerson.Serialno),
  60. // Value=sn,
  61. // ValueType=QueryValueTypeEnum.String,
  62. // Operator=QueryOperatorEnum.Equal
  63. // }
  64. // }
  65. // }!;
  66. // return await _personApiClient.GetFirstAsync(param, new RequestHeader { RequestId = messageId }).ConfigureAwait(false);
  67. // }
  68. //}
  69. /// <summary>
  70. /// 读取个人信息(血压使用)注意要改用 redis DB7
  71. /// </summary>
  72. /// <param name="messageId"></param>
  73. /// <param name="sn"></param>
  74. /// <returns></returns>
  75. public async Task<GpsDevicePerson?> GetDeviceGpsPersonCacheBySerialNoAsync(string messageId, string sn)
  76. {
  77. if (string.IsNullOrWhiteSpace(sn)) return null;
  78. // 增加容错,防止Redis宕机造成业务中断
  79. try
  80. {
  81. var person = await RedisHelperDb7.HGetAsync<GpsDevicePerson>(CACHE_HASH_KEY_GPSDEVICEPERSON, sn).ConfigureAwait(false);
  82. return person;
  83. }
  84. catch (Exception ex)
  85. {
  86. _logger.LogWarning($"Redis发生异常,将直接读取MySQL数据库,构造新实例:{ex.Message}, {ex.StackTrace}");
  87. var param = new GeneralParam
  88. {
  89. Filters = new List<QueryFilterCondition>
  90. {
  91. new QueryFilterCondition
  92. {
  93. Key=nameof(GpsPerson.Serialno),
  94. Value=sn,
  95. ValueType=QueryValueTypeEnum.String,
  96. Operator=QueryOperatorEnum.Equal
  97. }
  98. }
  99. }!;
  100. var person = await _personApiClient.GetFirstAsync(param, new RequestHeader { RequestId = messageId }).ConfigureAwait(false);
  101. // 读取数据库,构造新实例
  102. return new GpsDevicePerson {
  103. Time = DateTime.Now,
  104. Person = new Person {
  105. DeviceId = person.DeviceId,
  106. PersonId = person.PersonId,
  107. Remarks = person.Remarks,
  108. SerialNo = person.Serialno
  109. }
  110. };
  111. }
  112. }
  113. /// <summary>
  114. /// 获取gps_person缓存
  115. /// </summary>
  116. /// <param name="messageId"></param>
  117. /// <param name="sn"></param>
  118. /// <returns></returns>
  119. public async Task<JObject?> GetDeviceGpsPersonCacheObjectBySerialNoAsync(string messageId, string sn)
  120. {
  121. if (string.IsNullOrWhiteSpace(sn)) return null;
  122. try
  123. {
  124. var person = await RedisHelperDb7.HGetAsync(CACHE_HASH_KEY_GPSDEVICEPERSON, sn).ConfigureAwait(false);
  125. return (JObject?)JsonConvert.DeserializeObject(person);
  126. }
  127. catch (Exception ex)
  128. {
  129. _logger.LogWarning($"Redis发生异常:{ex.Message}, {ex.StackTrace}");
  130. }
  131. return null;
  132. }
  133. /// <summary>
  134. /// 更新gps_person缓存
  135. /// </summary>
  136. /// <param name="person"></param>
  137. /// <param name="sn"></param>
  138. /// <returns></returns>
  139. public async Task<bool> UpdateDeviceGpsPersonCacheObjectBySerialNoAsync(JObject person, string sn)
  140. {
  141. var flag = false;
  142. if (string.IsNullOrWhiteSpace(sn)) return flag;
  143. try
  144. {
  145. // 覆盖 返回false
  146. await RedisHelperDb7.HSetAsync(CACHE_HASH_KEY_GPSDEVICEPERSON, sn, person).ConfigureAwait(false);
  147. flag = true;
  148. }
  149. catch (Exception ex)
  150. {
  151. _logger.LogWarning($"Redis发生异常:{ex.Message}, {ex.StackTrace}");
  152. }
  153. return flag;
  154. }
  155. #region 生理健康(步数、周期性体温、周期性血压) Db10
  156. // 周期性体温
  157. public async Task<HisGpsTemperature?> GetTemperaturePeriodicityAsync(string sn)
  158. {
  159. try
  160. {
  161. var res=await RedisHelperDb10.HGetAsync(CACHE_HASH_KEY_TEMP_PERIODICITY, sn);
  162. if (!string.IsNullOrEmpty(res))
  163. {
  164. return JsonConvert.DeserializeObject<HisGpsTemperature>(res)!;
  165. }
  166. }
  167. catch (Exception ex)
  168. {
  169. _logger.LogError($"Redis发生异常:{ex.Message}, {ex.StackTrace}");
  170. }
  171. return null;
  172. }
  173. // 周期性步数
  174. public async Task<HisGpsStep?> GetStepPeriodicityAsync(string sn )
  175. {
  176. try
  177. {
  178. var res = await RedisHelperDb10.HGetAsync(CACHE_HASH_KEY_STEP_PERIODICITY, sn);
  179. if (!string.IsNullOrEmpty(res))
  180. {
  181. return JsonConvert.DeserializeObject<HisGpsStep>(res)!;
  182. }
  183. }
  184. catch (Exception ex)
  185. {
  186. _logger.LogError($"Redis发生异常:{ex.Message}, {ex.StackTrace}");
  187. }
  188. return null;
  189. }
  190. // 周期性血压
  191. public async Task<HisGpsBloodPress?> GetBloodPressPeriodicityAsync(string sn)
  192. {
  193. try
  194. {
  195. var res = await RedisHelperDb10.HGetAsync(CACHE_HASH_KEY_BLOODPRESS_PERIODICITY, sn);
  196. if (!string.IsNullOrEmpty(res))
  197. {
  198. return JsonConvert.DeserializeObject<HisGpsBloodPress>(res)!;
  199. }
  200. }
  201. catch (Exception ex)
  202. {
  203. _logger.LogError($"Redis发生异常:{ex.Message}, {ex.StackTrace}");
  204. }
  205. return null;
  206. }
  207. #endregion
  208. }
  209. }