No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

306 líneas
11KB

  1. using HealthMonitor.Model.Service.Mapper;
  2. using Microsoft.Extensions.Logging;
  3. using Newtonsoft.Json;
  4. using Newtonsoft.Json.Linq;
  5. using TelpoDataService.Util.Clients;
  6. using TelpoDataService.Util.Entities.GpsCard;
  7. using TelpoDataService.Util.Entities.GpsLocationHistory;
  8. using TelpoDataService.Util.Models;
  9. using TelpoDataService.Util.QueryObjects;
  10. using HealthMonitor.Common;
  11. namespace HealthMonitor.Service.Cache
  12. {
  13. public class DeviceCacheManager
  14. {
  15. private readonly ILogger<DeviceCacheManager> _logger;
  16. private const string CACHE_KEY_DEVICE = "Device_";
  17. private const string CACHE_KEY_GPSDEVICE_WATCH_CONFIG = "#GPSDEVICE_WATCH_CONFIG_HASH";
  18. private const string CACHE_KEY_FETALMOVEMENT = "FM_";
  19. private const string CACHE_KEY_FETALHEARTRATE = "FHR_";
  20. private const string CACHE_KEY_PHRFREQSTATUS = "PhrFreqStatus_";
  21. private readonly GpsLocationHistoryAccessorClient<HisGpsFetalMovement> _hisFetalMovementApiClient;
  22. private readonly GpsLocationHistoryAccessorClient<HisGpsFetalHeartRate> _hisFetalHeartRateApiClient;
  23. public DeviceCacheManager(ILogger<DeviceCacheManager> logger
  24. , GpsLocationHistoryAccessorClient<HisGpsFetalMovement> hisFetalMovementApiClient
  25. , GpsLocationHistoryAccessorClient<HisGpsFetalHeartRate> hisFetalHeartRateApiClient)
  26. {
  27. _logger = logger;
  28. _hisFetalMovementApiClient = hisFetalMovementApiClient;
  29. _hisFetalHeartRateApiClient = hisFetalHeartRateApiClient;
  30. }
  31. /// <summary>
  32. /// 获取device_id
  33. /// </summary>
  34. /// <param name="sn"></param>
  35. /// <returns></returns>
  36. public async Task<GpsDevice?> GetDeviceBySerialNoAsync(string sn)
  37. {
  38. string key = CACHE_KEY_DEVICE + sn;
  39. var device = await RedisHelperDb10.GetAsync<GpsDevice>(key).ConfigureAwait(false);
  40. return device;
  41. }
  42. /// <summary>
  43. ///
  44. /// </summary>
  45. /// <param name="sn"></param>
  46. /// <param name="bizCode">
  47. /// 业务码
  48. /// 0067 胎心启动配置
  49. /// </param>
  50. /// <returns></returns>
  51. public async Task<JObject?> GetGpsDeviceWatchConfigCacheObjectBySerialNoAsync(string sn, string bizCode)
  52. {
  53. if (string.IsNullOrWhiteSpace(sn)) return null;
  54. try
  55. {
  56. var config = await RedisHelperDb7.HGetAsync(CACHE_KEY_GPSDEVICE_WATCH_CONFIG, $"{sn}_{bizCode}").ConfigureAwait(false);
  57. if (config == null) return null;
  58. _logger.LogInformation($"{sn} 配置信息 {config}");
  59. return (JObject)JsonConvert.DeserializeObject(config)!;
  60. }
  61. catch (Exception ex)
  62. {
  63. _logger.LogWarning($"Redis DB7发生异常:{ex.Message}, {ex.StackTrace}");
  64. }
  65. return null;
  66. }
  67. #region 心率相关
  68. /// <summary>
  69. /// 读取高频状态
  70. /// </summary>
  71. /// <param name="sn"></param>
  72. /// <returns></returns>
  73. public async Task<PregnancyHeartRateModel?> GetPregnancyHeartRateFreqStatusAsync(string sn)
  74. {
  75. try
  76. {
  77. var key = CACHE_KEY_PHRFREQSTATUS+sn;
  78. var status = await RedisHelper.GetAsync(key).ConfigureAwait(false);
  79. if (string.IsNullOrEmpty(status)) return null;
  80. return JsonConvert.DeserializeObject<PregnancyHeartRateModel>(status);
  81. }
  82. catch (Exception ex)
  83. {
  84. _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}");
  85. }
  86. return null;
  87. }
  88. public async Task SetPregnancyHeartRateFreqStatusAsync(string sn, PregnancyHeartRateModel status,int expire=-1)
  89. {
  90. try
  91. {
  92. var key = CACHE_KEY_PHRFREQSTATUS + sn;
  93. var data=JsonConvert.SerializeObject(status);
  94. await RedisHelper.SetAsync(key, data, expire).ConfigureAwait(false);
  95. }
  96. catch (Exception ex)
  97. {
  98. _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}");
  99. }
  100. }
  101. public async Task DelPregnancyHeartRateFreqStatusAsync(string sn)
  102. {
  103. try
  104. {
  105. var key = CACHE_KEY_PHRFREQSTATUS + sn;
  106. await RedisHelper.DelAsync(key).ConfigureAwait(false);
  107. _logger.LogInformation($"{sn} 结束高频状态");
  108. }
  109. catch (Exception ex)
  110. {
  111. _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}");
  112. }
  113. }
  114. public async Task SetFetalHeartRateAsync(string serialNo, string sampleTime)
  115. {
  116. try
  117. {
  118. var key = $"{CACHE_KEY_FETALHEARTRATE}_{serialNo}_{sampleTime}";
  119. var time = long.Parse(sampleTime.Length < 13 ? sampleTime.PadRight(13, '0') : sampleTime);
  120. var value = DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(time).ToString("yyyy-MM-dd HH:mm:ss");
  121. await RedisHelper.SetAsync(key, value, 7200).ConfigureAwait(false);
  122. }
  123. catch (Exception ex)
  124. {
  125. _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}");
  126. }
  127. }
  128. public async Task<bool> FetalHeartRateIsExistedAsync(string serialNo, string sampleTime)
  129. {
  130. try
  131. {
  132. var key = $"{CACHE_KEY_FETALHEARTRATE}_{serialNo}_{sampleTime}";
  133. var res = await RedisHelper.GetAsync(key).ConfigureAwait(false);
  134. if (string.IsNullOrEmpty(res))
  135. {
  136. GeneralParam param = new()
  137. {
  138. Filters = new List<QueryFilterCondition>
  139. {
  140. new ()
  141. {
  142. Key=nameof(HisGpsFetalHeartRate.Serialno),
  143. Value=serialNo,
  144. ValueType=QueryValueTypeEnum.String,
  145. Operator=QueryOperatorEnum.Equal
  146. },
  147. new ()
  148. {
  149. Key=nameof(HisGpsFetalHeartRate.SampleTime),
  150. Value=sampleTime,
  151. ValueType=QueryValueTypeEnum.String,
  152. Operator=QueryOperatorEnum.Equal
  153. },
  154. }
  155. };
  156. var isFetalMovementExist = await _hisFetalHeartRateApiClient.GetFirstAsync(param, serialNo[^2..], null, new RequestHeader { RequestId = Guid.NewGuid().ToString("D") });
  157. if (isFetalMovementExist != null)
  158. {
  159. await SetFetalHeartRateAsync(serialNo, sampleTime);
  160. return true;
  161. }
  162. }
  163. else
  164. {
  165. return true;
  166. }
  167. }
  168. catch (Exception ex)
  169. {
  170. _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}");
  171. }
  172. return false;
  173. }
  174. #endregion
  175. #region 胎动相关
  176. public async Task SetFetalMovementAsync(string serialNo, string sampleTime, HisGpsFetalMovement fm)
  177. {
  178. try
  179. {
  180. var key = $"{CACHE_KEY_FETALMOVEMENT}_{serialNo}_{sampleTime}";
  181. var time = long.Parse(sampleTime.Length < 13 ? sampleTime.PadRight(13, '0') : sampleTime);
  182. var value = DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(time).ToString("yyyy-MM-dd HH:mm:ss");
  183. fm.SampleTime = value;
  184. await RedisHelper.SetAsync(key, JsonConvert.SerializeObject(fm), 7200).ConfigureAwait(false);
  185. }
  186. catch (Exception ex)
  187. {
  188. _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}");
  189. }
  190. }
  191. public async Task<bool> FetalMovementIsExistedAsync(string serialNo, string sampleTime)
  192. {
  193. try
  194. {
  195. var key = $"{CACHE_KEY_FETALMOVEMENT}_{serialNo}_{sampleTime}";
  196. var res = await RedisHelper.GetAsync(key).ConfigureAwait(false);
  197. if (string.IsNullOrEmpty(res))
  198. {
  199. GeneralParam param = new()
  200. {
  201. Filters = new List<QueryFilterCondition>
  202. {
  203. new ()
  204. {
  205. Key=nameof(HisGpsFetalMovement.Serialno),
  206. Value=serialNo,
  207. ValueType=QueryValueTypeEnum.String,
  208. Operator=QueryOperatorEnum.Equal
  209. },
  210. new ()
  211. {
  212. Key=nameof(HisGpsFetalMovement.SampleTime),
  213. Value=sampleTime,
  214. ValueType=QueryValueTypeEnum.String,
  215. Operator=QueryOperatorEnum.Equal
  216. },
  217. }
  218. };
  219. var isFetalMovementExist = await _hisFetalMovementApiClient.GetFirstAsync(param, serialNo[^2..], null, new RequestHeader { RequestId = Guid.NewGuid().ToString("D") });
  220. if (isFetalMovementExist != null)
  221. {
  222. await SetFetalMovementAsync(serialNo, sampleTime, isFetalMovementExist);
  223. return true;
  224. }
  225. }
  226. else
  227. {
  228. return true;
  229. }
  230. }
  231. catch (Exception ex)
  232. {
  233. _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}");
  234. }
  235. return false;
  236. }
  237. #endregion
  238. #region 业务间隔
  239. public async Task<string?> GetBizIntervalAsync(string sn, string biz)
  240. {
  241. try
  242. {
  243. var key = biz + "-"+sn;
  244. var status = await RedisHelper.GetAsync(key).ConfigureAwait(false);
  245. return status;
  246. }
  247. catch (Exception ex)
  248. {
  249. _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}");
  250. }
  251. return null;
  252. }
  253. /// <summary>
  254. /// 业务间隔(默认15分钟)
  255. /// </summary>
  256. /// <param name="sn"></param>
  257. /// <param name="biz"></param>
  258. /// <param name="ttl"></param>
  259. /// <returns></returns>
  260. public async Task SetBizIntervalAsync(string sn, string biz, int ttl=60*15)
  261. {
  262. try
  263. {
  264. var key = biz + "-" + sn;
  265. await RedisHelper.SetAsync(key, biz, ttl).ConfigureAwait(false);
  266. }
  267. catch (Exception ex)
  268. {
  269. _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}");
  270. }
  271. }
  272. #endregion
  273. }
  274. }