|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- 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<DeviceCacheManager> _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<HisGpsFetalMovement> _hisFetalMovementApiClient;
- private readonly GpsLocationHistoryAccessorClient<HisGpsFetalHeartRate> _hisFetalHeartRateApiClient;
-
-
-
-
- public DeviceCacheManager(ILogger<DeviceCacheManager> logger
- , GpsLocationHistoryAccessorClient<HisGpsFetalMovement> hisFetalMovementApiClient
- , GpsLocationHistoryAccessorClient<HisGpsFetalHeartRate> hisFetalHeartRateApiClient)
- {
- _logger = logger;
- _hisFetalMovementApiClient = hisFetalMovementApiClient;
- _hisFetalHeartRateApiClient = hisFetalHeartRateApiClient;
- }
-
- /// <summary>
- /// 获取device_id
- /// </summary>
- /// <param name="sn"></param>
- /// <returns></returns>
- public async Task<GpsDevice?> GetDeviceBySerialNoAsync(string sn)
- {
- string key = CACHE_KEY_DEVICE + sn;
- var device = await RedisHelperDb10.GetAsync<GpsDevice>(key).ConfigureAwait(false);
- return device;
- }
-
- /// <summary>
- ///
- /// </summary>
- /// <param name="sn"></param>
- /// <param name="bizCode">
- /// 业务码
- /// 0067 胎心启动配置
- /// </param>
- /// <returns></returns>
- public async Task<JObject?> 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 心率相关
- /// <summary>
- /// 读取高频状态
- /// </summary>
- /// <param name="sn"></param>
- /// <returns></returns>
- public async Task<PregnancyHeartRateModel?> 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<PregnancyHeartRateModel>(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);
- _logger.LogInformation($"{sn} 结束高频状态");
- }
- 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<bool> 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<QueryFilterCondition>
- {
- 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<bool> 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<QueryFilterCondition>
- {
- 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
-
- #region 业务间隔
- public async Task<string?> GetBizIntervalAsync(string sn, string biz)
- {
- try
- {
- var key = biz + "-"+sn;
- var status = await RedisHelper.GetAsync(key).ConfigureAwait(false);
- return status;
- }
- catch (Exception ex)
- {
- _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}");
- }
- return null;
- }
- /// <summary>
- /// 业务间隔(默认15分钟)
- /// </summary>
- /// <param name="sn"></param>
- /// <param name="biz"></param>
- /// <param name="ttl"></param>
- /// <returns></returns>
- public async Task SetBizIntervalAsync(string sn, string biz, int ttl=60*15)
- {
- try
- {
- var key = biz + "-" + sn;
- await RedisHelper.SetAsync(key, biz, ttl).ConfigureAwait(false);
- }
- catch (Exception ex)
- {
- _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}");
- }
- }
-
- #endregion
- }
- }
|