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.

305 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. }
  108. catch (Exception ex)
  109. {
  110. _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}");
  111. }
  112. }
  113. public async Task SetFetalHeartRateAsync(string serialNo, string sampleTime)
  114. {
  115. try
  116. {
  117. var key = $"{CACHE_KEY_FETALHEARTRATE}_{serialNo}_{sampleTime}";
  118. var time = long.Parse(sampleTime.Length < 13 ? sampleTime.PadRight(13, '0') : sampleTime);
  119. var value = DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(time).ToString("yyyy-MM-dd HH:mm:ss");
  120. await RedisHelper.SetAsync(key, value, 7200).ConfigureAwait(false);
  121. }
  122. catch (Exception ex)
  123. {
  124. _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}");
  125. }
  126. }
  127. public async Task<bool> FetalHeartRateIsExistedAsync(string serialNo, string sampleTime)
  128. {
  129. try
  130. {
  131. var key = $"{CACHE_KEY_FETALHEARTRATE}_{serialNo}_{sampleTime}";
  132. var res = await RedisHelper.GetAsync(key).ConfigureAwait(false);
  133. if (string.IsNullOrEmpty(res))
  134. {
  135. GeneralParam param = new()
  136. {
  137. Filters = new List<QueryFilterCondition>
  138. {
  139. new ()
  140. {
  141. Key=nameof(HisGpsFetalHeartRate.Serialno),
  142. Value=serialNo,
  143. ValueType=QueryValueTypeEnum.String,
  144. Operator=QueryOperatorEnum.Equal
  145. },
  146. new ()
  147. {
  148. Key=nameof(HisGpsFetalHeartRate.SampleTime),
  149. Value=sampleTime,
  150. ValueType=QueryValueTypeEnum.String,
  151. Operator=QueryOperatorEnum.Equal
  152. },
  153. }
  154. };
  155. var isFetalMovementExist = await _hisFetalHeartRateApiClient.GetFirstAsync(param, serialNo[^2..], null, new RequestHeader { RequestId = Guid.NewGuid().ToString("D") });
  156. if (isFetalMovementExist != null)
  157. {
  158. await SetFetalHeartRateAsync(serialNo, sampleTime);
  159. return true;
  160. }
  161. }
  162. else
  163. {
  164. return true;
  165. }
  166. }
  167. catch (Exception ex)
  168. {
  169. _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}");
  170. }
  171. return false;
  172. }
  173. #endregion
  174. #region 胎动相关
  175. public async Task SetFetalMovementAsync(string serialNo, string sampleTime, HisGpsFetalMovement fm)
  176. {
  177. try
  178. {
  179. var key = $"{CACHE_KEY_FETALMOVEMENT}_{serialNo}_{sampleTime}";
  180. var time = long.Parse(sampleTime.Length < 13 ? sampleTime.PadRight(13, '0') : sampleTime);
  181. var value = DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(time).ToString("yyyy-MM-dd HH:mm:ss");
  182. fm.SampleTime = value;
  183. await RedisHelper.SetAsync(key, JsonConvert.SerializeObject(fm), 7200).ConfigureAwait(false);
  184. }
  185. catch (Exception ex)
  186. {
  187. _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}");
  188. }
  189. }
  190. public async Task<bool> FetalMovementIsExistedAsync(string serialNo, string sampleTime)
  191. {
  192. try
  193. {
  194. var key = $"{CACHE_KEY_FETALMOVEMENT}_{serialNo}_{sampleTime}";
  195. var res = await RedisHelper.GetAsync(key).ConfigureAwait(false);
  196. if (string.IsNullOrEmpty(res))
  197. {
  198. GeneralParam param = new()
  199. {
  200. Filters = new List<QueryFilterCondition>
  201. {
  202. new ()
  203. {
  204. Key=nameof(HisGpsFetalMovement.Serialno),
  205. Value=serialNo,
  206. ValueType=QueryValueTypeEnum.String,
  207. Operator=QueryOperatorEnum.Equal
  208. },
  209. new ()
  210. {
  211. Key=nameof(HisGpsFetalMovement.SampleTime),
  212. Value=sampleTime,
  213. ValueType=QueryValueTypeEnum.String,
  214. Operator=QueryOperatorEnum.Equal
  215. },
  216. }
  217. };
  218. var isFetalMovementExist = await _hisFetalMovementApiClient.GetFirstAsync(param, serialNo[^2..], null, new RequestHeader { RequestId = Guid.NewGuid().ToString("D") });
  219. if (isFetalMovementExist != null)
  220. {
  221. await SetFetalMovementAsync(serialNo, sampleTime, isFetalMovementExist);
  222. return true;
  223. }
  224. }
  225. else
  226. {
  227. return true;
  228. }
  229. }
  230. catch (Exception ex)
  231. {
  232. _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}");
  233. }
  234. return false;
  235. }
  236. #endregion
  237. #region 业务间隔
  238. public async Task<string?> GetBizIntervalAsync(string sn, string biz)
  239. {
  240. try
  241. {
  242. var key = biz + "-"+sn;
  243. var status = await RedisHelper.GetAsync(key).ConfigureAwait(false);
  244. return status;
  245. }
  246. catch (Exception ex)
  247. {
  248. _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}");
  249. }
  250. return null;
  251. }
  252. /// <summary>
  253. /// 业务间隔(默认15分钟)
  254. /// </summary>
  255. /// <param name="sn"></param>
  256. /// <param name="biz"></param>
  257. /// <param name="ttl"></param>
  258. /// <returns></returns>
  259. public async Task SetBizIntervalAsync(string sn, string biz, int ttl=60*15)
  260. {
  261. try
  262. {
  263. var key = biz + "-" + sn;
  264. await RedisHelper.SetAsync(key, biz, ttl).ConfigureAwait(false);
  265. }
  266. catch (Exception ex)
  267. {
  268. _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}");
  269. }
  270. }
  271. #endregion
  272. }
  273. }