diff --git a/HealthMonitor.Service/Cache/DeviceCacheManager.cs b/HealthMonitor.Service/Cache/DeviceCacheManager.cs index 857a1b5..d7291d5 100644 --- a/HealthMonitor.Service/Cache/DeviceCacheManager.cs +++ b/HealthMonitor.Service/Cache/DeviceCacheManager.cs @@ -2,7 +2,12 @@ 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 { @@ -11,11 +16,22 @@ namespace HealthMonitor.Service.Cache 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 readonly GpsLocationHistoryAccessorClient _hisFetalMovementApiClient; + private readonly GpsLocationHistoryAccessorClient _hisFetalHeartRateApiClient; - public DeviceCacheManager(ILogger logger) + + + + public DeviceCacheManager(ILogger logger + , GpsLocationHistoryAccessorClient hisFetalMovementApiClient + , GpsLocationHistoryAccessorClient hisFetalHeartRateApiClient) { _logger = logger; + _hisFetalMovementApiClient = hisFetalMovementApiClient; + _hisFetalHeartRateApiClient = hisFetalHeartRateApiClient; } @@ -55,7 +71,7 @@ namespace HealthMonitor.Service.Cache } - #region 心率状态 + #region 心率相关 /// /// 读取高频状态 /// @@ -104,6 +120,142 @@ namespace HealthMonitor.Service.Cache } } + + 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 } } diff --git a/HealthMonitor.Service/Cache/FetalHeartRateCacheManager.cs b/HealthMonitor.Service/Cache/FetalHeartRateCacheManager.cs deleted file mode 100644 index a0286ee..0000000 --- a/HealthMonitor.Service/Cache/FetalHeartRateCacheManager.cs +++ /dev/null @@ -1,94 +0,0 @@ -using HealthMonitor.Common; -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TelpoDataService.Util.Clients; -using TelpoDataService.Util.Entities.GpsLocationHistory; -using TelpoDataService.Util.Models; -using TelpoDataService.Util.QueryObjects; - -namespace HealthMonitor.Service.Cache -{ - public class FetalHeartRateCacheManager - { - private readonly ILogger _logger; - - private readonly GpsLocationHistoryAccessorClient _hisFetalHeartRateApiClient; - - private const string CACHE_KEY_FETALHEARTRATE = "FHR_"; - - public FetalHeartRateCacheManager(ILogger logger, GpsLocationHistoryAccessorClient hisFetalHeartRateApiClient) - { - _logger = logger; - _hisFetalHeartRateApiClient = hisFetalHeartRateApiClient; - } - - 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; - } - } -} diff --git a/HealthMonitor.Service/Cache/FetalMovementCacheManager.cs b/HealthMonitor.Service/Cache/FetalMovementCacheManager.cs deleted file mode 100644 index ed84e69..0000000 --- a/HealthMonitor.Service/Cache/FetalMovementCacheManager.cs +++ /dev/null @@ -1,98 +0,0 @@ -using HealthMonitor.Common; -using HealthMonitor.Model.Service.Mapper; -using Microsoft.EntityFrameworkCore.Metadata.Internal; -using Microsoft.Extensions.Logging; -using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TelpoDataService.Util.Clients; -using TelpoDataService.Util.Entities.GpsLocationHistory; -using TelpoDataService.Util.Models; -using TelpoDataService.Util.QueryObjects; - -namespace HealthMonitor.Service.Cache -{ - public class FetalMovementCacheManager - { - private readonly ILogger _logger; - private readonly GpsLocationHistoryAccessorClient _hisFetalMovementApiClient; - - private const string CACHE_KEY_FETALMOVEMENT = "FM_"; - - public FetalMovementCacheManager(ILogger logger, GpsLocationHistoryAccessorClient hisFetalMovementApiClient) - { - _logger = logger; - _hisFetalMovementApiClient = hisFetalMovementApiClient; - } - 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; - } - } -} diff --git a/HealthMonitor.Service/Resolver/PregnancyHeartRateResolver.cs b/HealthMonitor.Service/Resolver/PregnancyHeartRateResolver.cs index 92f4cf6..9d0e85b 100644 --- a/HealthMonitor.Service/Resolver/PregnancyHeartRateResolver.cs +++ b/HealthMonitor.Service/Resolver/PregnancyHeartRateResolver.cs @@ -44,15 +44,13 @@ namespace HealthMonitor.Service.Resolver private readonly FetalMovementNormalValueRangeCacheManager _mgrFetalMovementNormalValueRangeCache; - private readonly FetalMovementCacheManager _mgrFetalMovement; - private readonly FetalHeartRateCacheManager _mgrFetalHeartRateCache; + public PregnancyHeartRateResolver(ILogger logger, HttpHelper httpHelper, EtcdService serviceEtcd, DeviceCacheManager deviceCacheMgr, IotApiService iotApiService, TDengineService serviceDengine, FetalMovementNormalValueRangeCacheManager fetalMovementNormalValueRangeCacheMgr, GpsLocationHistoryAccessorClient hisFetalHeartApiClient, - FetalMovementCacheManager fetalMovementCacheManager, FetalHeartRateCacheManager fetalHeartRateCacheManager, GpsLocationHistoryAccessorClient hisFetalMovementApiClient ) { @@ -65,8 +63,6 @@ namespace HealthMonitor.Service.Resolver _hisFetalHeartApiClient = hisFetalHeartApiClient; _hisFetalMovementApiClient = hisFetalMovementApiClient; _mgrFetalMovementNormalValueRangeCache = fetalMovementNormalValueRangeCacheMgr; - _mgrFetalMovement = fetalMovementCacheManager; - _mgrFetalHeartRateCache = fetalHeartRateCacheManager; } public void SetResolveInfo(PackageMsgModel msg) @@ -597,9 +593,8 @@ namespace HealthMonitor.Service.Resolver // 统计结束时间 var statEndTime = midNight.AddHours(2 * (i+1)); - //var isFetalMovementExist = await _hisFetalMovementApiClient.GetFirstAsync(param, heartRate.Serialno[^2..], null, new RequestHeader { RequestId = Guid.NewGuid().ToString("D") }); - var isFetalMovementExisted = await _mgrFetalMovement.FetalMovementIsExistedAsync(heartRate.Serialno, fetalMovementSampleTime); - //_logger.LogInformation($"{heartRate.Serialno} 胎动记录{isFetalMovementExisted},数据采样时间:{fetalMovementSampleTime}|{midNight.AddHours(2 * i).ToString("yyyy-MM-dd HH:mm:ss")}, 周期:{statStartTime}-{statEndTime}, "); + var isFetalMovementExisted = await _deviceCacheMgr.FetalMovementIsExistedAsync(heartRate.Serialno, fetalMovementSampleTime); + _logger.LogInformation($"{heartRate.Serialno} 胎动记录{isFetalMovementExisted},数据采样时间:{fetalMovementSampleTime}|{midNight.AddHours(2 * i).ToString("yyyy-MM-dd HH:mm:ss")}, 周期:{statStartTime}-{statEndTime} 开始"); if (!isFetalMovementExisted) { @@ -640,7 +635,7 @@ namespace HealthMonitor.Service.Resolver var fetalMovement = (int)Math.Round(fetalMovementValue, 0, MidpointRounding.AwayFromZero); // _logger.LogInformation($"{heartRate.Serialno} segmentCountFMIndex: {i} -- fetalMovementSampleTime:{fetalMovementSampleTime}|{midNight.AddHours(2 * i).ToString("yyyy-MM-dd HH:mm:ss")} -- statStartTime: {statStartTime} -- statEndTime: {statEndTime}-- isFetalMovementExisted: {isFetalMovementExisted} "); - _logger.LogInformation($"{heartRate.Serialno} 胎动数据采样时间:{fetalMovementSampleTime}|{midNight.AddHours(2 * i).ToString("yyyy-MM-dd HH:mm:ss")}, 采样周期:{statStartTime}-{statEndTime}, 原始胎动值:{fetalMovementMapValue}, 佩戴时间 :{duringMins}|{statStartTime}-{phrRange.First()}, 胎动计算值:{fetalMovementValue}, 胎动最终值:{fetalMovement}"); + _logger.LogInformation($"{heartRate.Serialno} 胎动数据采样时间:{fetalMovementSampleTime}|{midNight.AddHours(2 * i).ToString("yyyy-MM-dd HH:mm:ss")}, 采样周期:{statStartTime}-{statEndTime}, 原始胎动值:{fetalMovementMapValue}, 佩戴时间 :{duringMins}|{statStartTime}-{phrRange.First()}, 胎动计算值:{fetalMovementValue}, 胎动最终值:{fetalMovement} 已完成."); // 获取胎心数据状态与胎动数据状态一致 var feltalMovementIsAbnormal = fetalHeartRateIsAbnormal; @@ -666,18 +661,22 @@ namespace HealthMonitor.Service.Resolver await _hisFetalMovementApiClient.AddAsync(fm).ConfigureAwait(false); // 设置入库缓存记录 - await _mgrFetalMovement.SetFetalMovementAsync(heartRate.Serialno, fetalMovementSampleTime,fm); + await _deviceCacheMgr.SetFetalMovementAsync(heartRate.Serialno, fetalMovementSampleTime,fm); } else { _logger.LogWarning($"{heartRate.Serialno} 孕周 {pregnancyWeeks},超出胎动计算范围"); } } + else + { + _logger.LogInformation($"{heartRate.Serialno} 胎动记录{isFetalMovementExisted},数据采样时间:{fetalMovementSampleTime}|{midNight.AddHours(2 * i).ToString("yyyy-MM-dd HH:mm:ss")}, 周期:{statStartTime}-{statEndTime} 不足两条,不能判断是否持续佩戴"); + + } } else { _logger.LogInformation($"{heartRate.Serialno} 胎动记录{isFetalMovementExisted},数据采样时间:{fetalMovementSampleTime}|{midNight.AddHours(2 * i).ToString("yyyy-MM-dd HH:mm:ss")}, 周期:{statStartTime}-{statEndTime} 已处理"); - } } diff --git a/HealthMonitor.WebApi/Program.cs b/HealthMonitor.WebApi/Program.cs index d081961..31f20f3 100644 --- a/HealthMonitor.WebApi/Program.cs +++ b/HealthMonitor.WebApi/Program.cs @@ -173,8 +173,6 @@ namespace HealthMonitor.WebApi builder.Services .AddSingleton() .AddSingleton() - .AddSingleton() - .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton();