using HealthMonitor.Model.Service.Mapper; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using TelpoDataService.Util.Clients; using TelpoDataService.Util.Entities.GpsCard; using TelpoDataService.Util.Entities.GpsLocationHistory; using TelpoDataService.Util.Models; using TelpoDataService.Util.QueryObjects; using HealthMonitor.Common; namespace HealthMonitor.Service.Cache { public class DeviceCacheManager { private readonly ILogger _logger; private const string CACHE_KEY_DEVICE = "Device_"; private const string CACHE_KEY_GPSDEVICE_WATCH_CONFIG = "#GPSDEVICE_WATCH_CONFIG_HASH"; private const string CACHE_KEY_FETALMOVEMENT = "FM_"; private const string CACHE_KEY_FETALHEARTRATE = "FHR_"; private const string CACHE_KEY_PHRFREQSTATUS = "PhrFreqStatus_"; private readonly GpsLocationHistoryAccessorClient _hisFetalMovementApiClient; private readonly GpsLocationHistoryAccessorClient _hisFetalHeartRateApiClient; public DeviceCacheManager(ILogger logger , GpsLocationHistoryAccessorClient hisFetalMovementApiClient , GpsLocationHistoryAccessorClient hisFetalHeartRateApiClient) { _logger = logger; _hisFetalMovementApiClient = hisFetalMovementApiClient; _hisFetalHeartRateApiClient = hisFetalHeartRateApiClient; } /// /// 获取device_id /// /// /// public async Task GetDeviceBySerialNoAsync(string sn) { string key = CACHE_KEY_DEVICE + sn; var device = await RedisHelperDb10.GetAsync(key).ConfigureAwait(false); return device; } /// /// /// /// /// /// 业务码 /// 0067 胎心启动配置 /// /// public async Task GetGpsDeviceWatchConfigCacheObjectBySerialNoAsync(string sn, string bizCode) { if (string.IsNullOrWhiteSpace(sn)) return null; try { var config = await RedisHelperDb7.HGetAsync(CACHE_KEY_GPSDEVICE_WATCH_CONFIG, $"{sn}_{bizCode}").ConfigureAwait(false); if (config == null) return null; _logger.LogInformation($"{sn} 配置信息 {config}"); return (JObject)JsonConvert.DeserializeObject(config)!; } catch (Exception ex) { _logger.LogWarning($"Redis DB7发生异常:{ex.Message}, {ex.StackTrace}"); } return null; } #region 心率相关 /// /// 读取高频状态 /// /// /// public async Task GetPregnancyHeartRateFreqStatusAsync(string sn) { try { var key = CACHE_KEY_PHRFREQSTATUS+sn; var status = await RedisHelper.GetAsync(key).ConfigureAwait(false); if (string.IsNullOrEmpty(status)) return null; return JsonConvert.DeserializeObject(status); } catch (Exception ex) { _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}"); } return null; } public async Task SetPregnancyHeartRateFreqStatusAsync(string sn, PregnancyHeartRateModel status,int expire=-1) { try { var key = CACHE_KEY_PHRFREQSTATUS + sn; var data=JsonConvert.SerializeObject(status); await RedisHelper.SetAsync(key, data, expire).ConfigureAwait(false); } catch (Exception ex) { _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}"); } } public async Task DelPregnancyHeartRateFreqStatusAsync(string sn) { try { var key = CACHE_KEY_PHRFREQSTATUS + sn; await RedisHelper.DelAsync(key).ConfigureAwait(false); } catch (Exception ex) { _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}"); } } public async Task SetFetalHeartRateAsync(string serialNo, string sampleTime) { try { var key = $"{CACHE_KEY_FETALHEARTRATE}_{serialNo}_{sampleTime}"; var time = long.Parse(sampleTime.Length < 13 ? sampleTime.PadRight(13, '0') : sampleTime); var value = DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(time).ToString("yyyy-MM-dd HH:mm:ss"); await RedisHelper.SetAsync(key, value, 7200).ConfigureAwait(false); } catch (Exception ex) { _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}"); } } public async Task FetalHeartRateIsExistedAsync(string serialNo, string sampleTime) { try { var key = $"{CACHE_KEY_FETALHEARTRATE}_{serialNo}_{sampleTime}"; var res = await RedisHelper.GetAsync(key).ConfigureAwait(false); if (string.IsNullOrEmpty(res)) { GeneralParam param = new() { Filters = new List { new () { Key=nameof(HisGpsFetalHeartRate.Serialno), Value=serialNo, ValueType=QueryValueTypeEnum.String, Operator=QueryOperatorEnum.Equal }, new () { Key=nameof(HisGpsFetalHeartRate.SampleTime), Value=sampleTime, ValueType=QueryValueTypeEnum.String, Operator=QueryOperatorEnum.Equal }, } }; var isFetalMovementExist = await _hisFetalHeartRateApiClient.GetFirstAsync(param, serialNo[^2..], null, new RequestHeader { RequestId = Guid.NewGuid().ToString("D") }); if (isFetalMovementExist != null) { await SetFetalHeartRateAsync(serialNo, sampleTime); return true; } } else { return true; } } catch (Exception ex) { _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}"); } return false; } #endregion #region 胎动相关 public async Task SetFetalMovementAsync(string serialNo, string sampleTime, HisGpsFetalMovement fm) { try { var key = $"{CACHE_KEY_FETALMOVEMENT}_{serialNo}_{sampleTime}"; var time = long.Parse(sampleTime.Length < 13 ? sampleTime.PadRight(13, '0') : sampleTime); var value = DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(time).ToString("yyyy-MM-dd HH:mm:ss"); fm.SampleTime = value; await RedisHelper.SetAsync(key, JsonConvert.SerializeObject(fm), 7200).ConfigureAwait(false); } catch (Exception ex) { _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}"); } } public async Task FetalMovementIsExistedAsync(string serialNo, string sampleTime) { try { var key = $"{CACHE_KEY_FETALMOVEMENT}_{serialNo}_{sampleTime}"; var res = await RedisHelper.GetAsync(key).ConfigureAwait(false); if (string.IsNullOrEmpty(res)) { GeneralParam param = new() { Filters = new List { new () { Key=nameof(HisGpsFetalMovement.Serialno), Value=serialNo, ValueType=QueryValueTypeEnum.String, Operator=QueryOperatorEnum.Equal }, new () { Key=nameof(HisGpsFetalMovement.SampleTime), Value=sampleTime, ValueType=QueryValueTypeEnum.String, Operator=QueryOperatorEnum.Equal }, } }; var isFetalMovementExist = await _hisFetalMovementApiClient.GetFirstAsync(param, serialNo[^2..], null, new RequestHeader { RequestId = Guid.NewGuid().ToString("D") }); if (isFetalMovementExist != null) { await SetFetalMovementAsync(serialNo, sampleTime, isFetalMovementExist); return true; } } else { return true; } } catch (Exception ex) { _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}"); } return false; } #endregion } }