From 3d695dcbe71cdbd988b1b8c1c1e406e3a0aeabbd Mon Sep 17 00:00:00 2001 From: H Vs Date: Mon, 14 Oct 2024 15:29:38 +0800 Subject: [PATCH] =?UTF-8?q?=E8=83=8E=E5=8A=A8=E5=A4=84=E7=90=86=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E4=B8=80=E4=B8=AA=E5=B0=8F=E6=97=B6=E8=AE=A1=E7=AE=97?= =?UTF-8?q?=E4=B8=80=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HealthMonitor.Common/DateTimeUtil.cs | 17 + .../Resolver/PregnancyHeartRateResolver.cs | 71 +- HealthMonitor.WebApi/Worker.cs | 1154 ++++++----------- 3 files changed, 447 insertions(+), 795 deletions(-) diff --git a/HealthMonitor.Common/DateTimeUtil.cs b/HealthMonitor.Common/DateTimeUtil.cs index 8ad4590..3d98312 100644 --- a/HealthMonitor.Common/DateTimeUtil.cs +++ b/HealthMonitor.Common/DateTimeUtil.cs @@ -722,6 +722,23 @@ namespace HealthMonitor.Common return timeDifference.TotalSeconds; } + /// + /// 时间转换逻辑 + /// 当前时间是的0~1小时返回2小时 ,当前时间是的1 ~2小时返回3小时 ,如此类推,当前时间是的21 ~22小时返回23小时 ,当前时间是的23 ~0小时返回次日1小时 + /// + /// 当前消息 + /// + public static int GetNextHour(int currentHour) + { + // 处理 23 点特殊情况 + if (currentHour == 23) + { + return 1; // 23点到0点时,返回次日1小时 + } + + // 计算其他时间的返回值 + return (currentHour / 2 + 1) * 2; + } ///// ///// 是否为日期型字符串 diff --git a/HealthMonitor.Service/Resolver/PregnancyHeartRateResolver.cs b/HealthMonitor.Service/Resolver/PregnancyHeartRateResolver.cs index bcea08d..c81e1dc 100644 --- a/HealthMonitor.Service/Resolver/PregnancyHeartRateResolver.cs +++ b/HealthMonitor.Service/Resolver/PregnancyHeartRateResolver.cs @@ -183,36 +183,8 @@ namespace HealthMonitor.Service.Resolver var nonFreqPhr = GetNonFreqPregnancyHeartRate(phr, highFreqSampleInterval); if (nonFreqPhr.Count >= 30) { - #region 定时计算胎动数据触发器两小时间隔开始 - - /** - var fetalMovementKey = $"health_monitor/schedule_push/cal_fetal_movement/imei/{heartRate.Serialno}"; - ///// 计算 0 点秒数 - var fetalMovementLastUpdate = (DateTime)heartRate.LastUpdate!; - DateTime fmScheduleNow = DateTime.Now; - // 小于两小时 - if (fmScheduleNow > fetalMovementLastUpdate && (fmScheduleNow - fetalMovementLastUpdate).TotalHours <= 2) - { - var rand = new Random(); - var pushSec = rand.Next(59); - int pushMin = int.TryParse(heartRate.Serialno.AsSpan(heartRate.Serialno.Length - 1), out pushMin) ? pushMin : 10; - - var scheduleHourDiff = SCHEDULE_HOUR - .Where(h => h > fetalMovementLastUpdate.Hour) - .OrderBy(h => h - fetalMovementLastUpdate.Hour) - .FirstOrDefault() - fetalMovementLastUpdate.Hour; - var scheduleTime = fetalMovementLastUpdate.AddHours(scheduleHourDiff); - - DateTime nextRunTime = new(scheduleTime.Year, scheduleTime.Month, scheduleTime.Day, scheduleTime.Hour, pushMin, pushSec); - TimeSpan timeUntilNextRun = nextRunTime - fmScheduleNow; - var ttl = (long)timeUntilNextRun.TotalSeconds; - - await SetIntervalTriggerAsync(fetalMovementKey, heartRate.Serialno, ttl, heartRate); - } - */ - #endregion - #region 定时计算胎动数据触发器两小时间隔开始(奇数小时计算) + /** var fetalMovementKey = $"health_monitor/schedule_push/cal_fetal_movement/imei/{heartRate.Serialno}"; var rand = new Random(); var pushSec = rand.Next(59); @@ -254,9 +226,50 @@ namespace HealthMonitor.Service.Resolver await SetIntervalTriggerAsync(fetalMovementKey, heartRate.Serialno, ttl, heartRate); _logger.LogInformation($"{heartRate.Serialno}-{heartRate.MessageId} 创建计划统计胎动时间{scheduledDateTime.ToString("yyyy-MM-dd HH:mm:ss")}"); + */ #endregion + #region 定时计算胎动数据触发器每小时间隔开始,延时一个小时计算 + ///当前时间是的0~1小时返回2小时 , + ///当前时间是的1~2小时返回3小时 ,如此类推, + ///当前时间是的21~22小时返回23小时 + ///当前时间是的23~0小时返回次日1小时 + + var fetalMovementKey = $"health_monitor/schedule_push/cal_fetal_movement/imei/{heartRate.Serialno}"; + var rand = new Random(); + var pushSec = rand.Next(59); + int pushMin = int.TryParse(heartRate.Serialno.AsSpan(heartRate.Serialno.Length - 1), out pushMin) ? pushMin : 10; + + DateTime fmScheduleNow = DateTime.Now; + int hour = fmScheduleNow.Hour; + int selectedScheduleHour=DateTimeUtil.GetNextHour(fmScheduleNow.Hour); + + + + DateTime scheduledDateTime = new DateTime( + fmScheduleNow.Year, + fmScheduleNow.Month, + fmScheduleNow.Day, + selectedScheduleHour, + pushMin, + pushSec + ); + + // 跨天 + if (selectedScheduleHour == 1) + { + scheduledDateTime = scheduledDateTime.AddDays(1); + } + + TimeSpan timeUntilNextRun = scheduledDateTime - fmScheduleNow; + var ttl = (long)timeUntilNextRun.TotalSeconds; + + await SetIntervalTriggerAsync(fetalMovementKey, heartRate.Serialno, ttl, heartRate); + _logger.LogInformation($"{heartRate.Serialno}-{heartRate.MessageId} 创建计划统计胎动时间{scheduledDateTime.ToString("yyyy-MM-dd HH:mm:ss")}"); + + #endregion + #region 计算胎心数据(按心率时间LastUpdate) var commonPHR = await _serviceTDengine.GetLastAsync(heartRate.Serialno); diff --git a/HealthMonitor.WebApi/Worker.cs b/HealthMonitor.WebApi/Worker.cs index 21efeb1..952c4fe 100644 --- a/HealthMonitor.WebApi/Worker.cs +++ b/HealthMonitor.WebApi/Worker.cs @@ -560,52 +560,67 @@ namespace HealthMonitor.WebApi //health_monitor/schedule_push/cal_fetal_movement/imei/ else if (key.Contains("health_monitor/schedule_push/cal_fetal_movement/imei/")) { + #region 相隔2小时胎动延时计算(实时now是3小时,计算 lastupdate 0~2范围的数据,) + /** + ///实时now的hour是3小时,计算 lastupdate 0~2范围的数据, + ///只在 HOUR = new int[] { 1, 3, 5, 7, 9, 11,13, 15, 17, 19, 21, 23 };是奇数 - #region 胎动延时计算 - /** using (_logger.BeginScope(new Dictionary { ["RequestId"] = $"FM-{imeiDel}-{DateTime.Now.ToString("yyyyMMddHHmmss")}" })) { - var watchConfig = await _deviceCacheMgr.GetGpsDeviceWatchConfigCacheObjectBySerialNoAsync(imeiDel, "0067"); - _logger.LogInformation($"触发胎动计算,设备配置{JsonConvert.SerializeObject(watchConfig)}"); - var isFetalHeartEnable = watchConfig != null && (int)watchConfig["enabled"]! == 1; - if (isFetalHeartEnable) + try { - var edoc = DateTimeUtil.ToDateTime(watchConfig!["EDOC"]!.ToString()); - // 已经建模 - var commonPHR = await _serviceTDengine.GetLastAsync(imeiDel); - if (commonPHR != null) + var watchConfig = await _deviceCacheMgr.GetGpsDeviceWatchConfigCacheObjectBySerialNoAsync(imeiDel, "0067"); + _logger.LogInformation($"触发胎动计算,设备配置{JsonConvert.SerializeObject(watchConfig)}"); + var isFetalHeartEnable = watchConfig != null && (int)watchConfig["enabled"]! == 1; + var highFreqSampleInterval = (int)watchConfig!["highFreqSampleInterval"]! >= 60 ? (int)watchConfig!["highFreqSampleInterval"]! : 60; + + if (isFetalHeartEnable) { - var phr = await _serviceTDengine.GetBySerialNoAsync(imeiDel, 7); - _logger.LogInformation($"{imeiDel} 计算胎动数据 "); + var edoc = DateTimeUtil.ToDateTime(watchConfig!["EDOC"]!.ToString()); + // 已经建模 + var commonPHR = await _serviceTDengine.GetLastAsync(imeiDel); + if (commonPHR != null) + { + var phr = await _serviceTDengine.GetBySerialNoAsync(imeiDel, 7); + _logger.LogInformation($"{imeiDel} 计算胎动数据 "); - var fmNow = DateTime.Now; - // 两小时前 - var fmNowSubtract = fmNow.AddMinutes(-fmNow.Minute).AddSeconds(-fmNow.Second).AddMilliseconds(-fmNow.Millisecond); + DateTime fmNow = DateTime.Now; + int fmNowHour = fmNow.Hour; + DateTime statStartTime; + DateTime statEndTime; + + if (fmNowHour == 1) + { + // last_update 22~0 + statStartTime = fmNow.Date.AddDays(-1).AddHours(22); + statEndTime = fmNow.Date; + } + else + { + statStartTime = fmNow.Date.AddHours(fmNowHour - 3); + statEndTime = fmNow.Date.AddHours(fmNowHour - 1); + } - var fetalMovementSampleTime = DateTimeUtil.ConvertToTimeStamp(fmNowSubtract).ToString()[..10]; + _logger.LogInformation($"{imeiDel} 胎动统计周期:{statStartTime.ToString("yyyy-MM-dd HH:mm:ss")}-{statEndTime.ToString("yyyy-MM-dd HH:mm:ss")}"); - // 统计开始时间 - var statStartTime = fmNowSubtract.AddHours(-2); - // 统计结束时间 - var statEndTime = fmNowSubtract; - var isFetalMovementExisted = await _deviceCacheMgr.FetalMovementIsExistedAsync(imeiDel, fetalMovementSampleTime); - _logger.LogInformation($"{imeiDel} 胎动记录{isFetalMovementExisted},数据采样时间:{fetalMovementSampleTime}|{fmNowSubtract.ToString("yyyy-MM-dd HH:mm:ss")}, 周期:{statStartTime}-{statEndTime} 开始"); + var fetalMovementSampleTime = DateTimeUtil.ConvertToTimeStamp(statEndTime).ToString()[..10]; + var isFetalMovementExisted = await _deviceCacheMgr.FetalMovementIsExistedAsync(imeiDel, fetalMovementSampleTime); - if (!isFetalMovementExisted) - { - /// 开始计算 - var phrRange = phr.Where(i => i.LastUpdate >= statStartTime && i.LastUpdate <= statEndTime) - .OrderByDescending(i => i.LastUpdate) - .Select(i => i.LastUpdate) - .ToList(); - // 判断是否有持续佩戴 - if (phrRange.Count >= 2) + if (!isFetalMovementExisted) { - // 读取胎心数据 - GeneralParam param = new() + /// 开始计算 + var phrRange = phr.Where(i => i.LastUpdate >= statStartTime && i.LastUpdate <= statEndTime) + .OrderByDescending(i => i.LastUpdate) + .Select(i => i.LastUpdate) + .ToList(); + // 判断是否有持续佩戴 + if (phrRange.Count >= 2) { - Filters = new List + // 读取胎心数据 + GeneralParam param = new() + { + Filters = new List { new () { @@ -622,562 +637,152 @@ namespace HealthMonitor.WebApi // Operator=QueryOperatorEnum.GreaterEqual //}, }, - OrderBys = new List + OrderBys = new List { new (){ IsDesc=true, Key=nameof(HisGpsFetalHeartRate.SampleTime) } } - }; - var fetalHeartRateIsAbnormal = 0; - var fhr = await _hisFetalHeartApiClient.GetFirstAsync(param, imeiDel[^2..], null, new RequestHeader { RequestId = Guid.NewGuid().ToString("D") }); - - // 胎心数据时间与胎动时间一致 - var time = long.Parse(fhr.SampleTime.Length < 13 ? fhr.SampleTime.PadRight(13, '0') : fhr.SampleTime); - var fhrSampleTime = DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(time); + }; + var fetalHeartRateIsAbnormal = 0; + var fhr = await _hisFetalHeartApiClient.GetFirstAsync(param, imeiDel[^2..], null, new RequestHeader { RequestId = Guid.NewGuid().ToString("D") }); - // 胎心数据时间与胎动时间一致,最后一条胎心数据与胎动数据的小时差不大于2 - if ((DateTime.Now.Hour - fhrSampleTime.Hour) <= 2) - { - var duringMins = Math.Abs((phrRange.First() - phrRange.Last()).TotalMinutes); - //在餐后时间段(8:00~10:00,12:00~14:00,18:00~20:00,22:00~24:00)取中间值。其他时间段取正常起始值 - bool isInTimeRanges = IsLastUpdateInTimeRanges(phrRange.First()); + // 胎心数据时间与胎动时间一致 + var time = long.Parse(fhr.SampleTime.Length < 13 ? fhr.SampleTime.PadRight(13, '0') : fhr.SampleTime); + var fhrSampleTime = DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(time); - int pregnancyWeeks = (DateTime.Now - edoc.AddDays(-280)).Days / 7; - if (pregnancyWeeks >= 12 && pregnancyWeeks <= 50) + // 胎心数据时间与胎动时间一致,最后一条胎心数据与胎动数据的小时差不大于3 + if ((DateTime.Now.Hour - fhrSampleTime.Hour) <= 3) { - var fetalMovementMap = _mgrFetalMovementNormalValueRangeCache.GetFetalMovements(); - - var fetalMovementMapValue = isInTimeRanges ? fetalMovementMap - .Where(i => - i.PregnancyPeriod![0] <= pregnancyWeeks && - i.PregnancyPeriod[1] >= pregnancyWeeks) - .Select(i => i.MedianMovement) - .FirstOrDefault() - : - fetalMovementMap - .Where(i => - i.PregnancyPeriod![0] <= pregnancyWeeks && - i.PregnancyPeriod[1] >= pregnancyWeeks) - .Select(i => i.InitialMovement) - .FirstOrDefault() - ; - - var fetalMovementValue = (fetalMovementMapValue * duringMins * 2) / 120; - // 四舍五入 - var fetalMovement = (int)Math.Round(fetalMovementValue, 0, MidpointRounding.AwayFromZero); - // _logger.LogInformation($"{imeiDel} segmentCountFMIndex: {i} -- fetalMovementSampleTime:{fetalMovementSampleTime}|{midNight.AddHours(2 * i).ToString("yyyy-MM-dd HH:mm:ss")} -- statStartTime: {statStartTime} -- statEndTime: {statEndTime}-- isFetalMovementExisted: {isFetalMovementExisted} "); - - #region 生理健康与胎动的关系 - /// (步数)运动步数超过1500步则加1; - /// (体温)低烧则胎动减1,高烧胎动减2;低烧是37.3~38.5,38.6以上是高烧。 - /// (血压)血压收缩压大于160则加1。 - /// (心理)心理压力高加2,压力中加1; + var duringMins = Math.Abs((phrRange.First() - phrRange.Last()).TotalMinutes); + //在餐后时间段(8:00~10:00,12:00~14:00,18:00~20:00,22:00~24:00)取中间值。其他时间段取正常起始值 + bool isInTimeRanges = IsLastUpdateInTimeRanges(phrRange.First()); - var fetalMovementStepVar = 0; - var fetalMovementTempVar = 0; - var fetalMovementBpVar = 0; - var fetalMovementPpVar = 0; - var fetalMovementFhrVar = 0; - // 步数 - if (true) + int pregnancyWeeks = (DateTime.Now - edoc.AddDays(-280)).Days / 7; + if (pregnancyWeeks >= 12 && pregnancyWeeks <= 50) { - var step = await _personCacheMgr.GetStepPeriodicityAsync(imeiDel); - if (step != null) - { - if (DateTime.Now.Hour - ((DateTime)step?.LastUpdate!).Hour <= 2) - { - if (step.Steps > 1500) fetalMovementStepVar = 1; - } - else - { - _logger.LogWarning($"{imeiDel} 周期步数 时间无效"); - } - } - else - { - _logger.LogWarning($"{imeiDel} 周期步数无数据"); - } + var fetalMovementMap = _mgrFetalMovementNormalValueRangeCache.GetFetalMovements(); - } + var fetalMovementMapValue = isInTimeRanges ? fetalMovementMap + .Where(i => + i.PregnancyPeriod![0] <= pregnancyWeeks && + i.PregnancyPeriod[1] >= pregnancyWeeks) + .Select(i => i.MedianMovement) + .FirstOrDefault() + : + fetalMovementMap + .Where(i => + i.PregnancyPeriod![0] <= pregnancyWeeks && + i.PregnancyPeriod[1] >= pregnancyWeeks) + .Select(i => i.InitialMovement) + .FirstOrDefault() + ; - // 体温 - if (true) - { - var temp = await _personCacheMgr.GetTemperaturePeriodicityAsync(imeiDel); - if (temp != null) - { - if (DateTime.Now.Hour - ((DateTime)temp?.LastUpdate!).Hour <= 2) - { - // 中烧 - if (temp.Temperature >= 37.3M && temp.Temperature <= 38.5M) - { - fetalMovementTempVar = -1; - } - // 高烧 - if (temp.Temperature >= 38.6M) - { - fetalMovementTempVar = -2; - } - } - else - { - _logger.LogWarning($"{imeiDel} 周期体温 时间无效"); - } - } - else - { - _logger.LogWarning($"{imeiDel} 周期体温无数据"); - } + var fetalMovementTimeVar = (fetalMovementMapValue * duringMins * 2) / 120; + //// 四舍五入 + //var fetalMovement = (int)Math.Round(fetalMovementValue, 0, MidpointRounding.AwayFromZero); + // _logger.LogInformation($"{imeiDel} segmentCountFMIndex: {i} -- fetalMovementSampleTime:{fetalMovementSampleTime}|{midNight.AddHours(2 * i).ToString("yyyy-MM-dd HH:mm:ss")} -- statStartTime: {statStartTime} -- statEndTime: {statEndTime}-- isFetalMovementExisted: {isFetalMovementExisted} "); - } + #region 生理健康与胎动的关系 + /// (步数)运动步数超过1500步则加1; + /// (体温)低烧则胎动减1,高烧胎动减2;低烧是37.3~38.5,38.6以上是高烧。 + /// (血压)血压收缩压大于160则加1。 + /// (心理)心理压力高加2,压力中加1; - // 血压 - if (true) - { - var bp = await _personCacheMgr.GetBloodPressPeriodicityAsync(imeiDel); - if (bp != null) + var fetalMovementStepVar = 0; + var fetalMovementTempVar = 0; + var fetalMovementBpVar = 0; + var fetalMovementPpVar = 0; + var fetalMovementFhrVar = 0; + // 步数 + if (false) { - if (DateTime.Now.Hour - ((DateTime)bp?.LastUpdate!).Hour <= 2) + var step = await _personCacheMgr.GetStepPeriodicityAsync(imeiDel); + if (step != null) { - if (bp.SystolicValue > 160) + if (DateTime.Now.Hour - ((DateTime)step?.LastUpdate!).Hour <= 2) { - fetalMovementBpVar = 1; + if (step.Steps > 1500) fetalMovementStepVar = 1; + } + else + { + _logger.LogWarning($"{imeiDel} 周期步数 时间无效"); } } else { - _logger.LogWarning($"{imeiDel} 周期血压 时间无效"); - } - } - else - { - _logger.LogWarning($"{imeiDel} 周期血压无数据"); - } - - } - // 心理 - if (true) - { - //-1 不处理, - //0 正常, - //1 轻度, - //2 中度; - //3 重度; - GeneralParam psychResultParam = new() - { - Filters = new List - { - new () - { - Key=nameof(HisGpsPsychResult.Serialno), - Value=imeiDel, - ValueType=QueryValueTypeEnum.String, - Operator=QueryOperatorEnum.Equal - }, - //new () - //{ - // Key=nameof(HisGpsFetalHeartRate.SampleTime), - // Value=sampleTime, - // ValueType=QueryValueTypeEnum.String, - // Operator=QueryOperatorEnum.GreaterEqual - //}, - }, - OrderBys = new List - { - new (){ - IsDesc=true, - Key=nameof(HisGpsPsychResult.Serialno) - } - } - }; - var psych = await _hisPsychResultApiClient.GetFirstAsync(psychResultParam, imeiDel[^2..], null, new RequestHeader { RequestId = Guid.NewGuid().ToString("D") }); - if (psych != null) - { - if (psych?.StressScore == 2) - { - fetalMovementPpVar = 1; - } - - if (psych?.StressScore == 3) - { - fetalMovementPpVar = 2; + _logger.LogWarning($"{imeiDel} 周期步数无数据"); } - } - else - { - _logger.LogWarning($"{imeiDel} 周期心理压力无数据"); - } - - } - - #endregion - - #region 胎心与胎动的关系 - /// 胎心值过缓时,则胎动数量减1;胎心值过速时,则胎动也加1。 - /// 此值允许在上限值上继续增加,在下限值上继续减少,最小值为0。 - /// 告警上限阀值 - - //1表示偏高;2表示偏低 - if (true) - { - if (fhr.IsAbnormal == 2) - { - fetalMovementPpVar = -1; - } - if (fhr.IsAbnormal == 1) - { - fetalMovementPpVar = 1; - } - } - - #endregion - - _logger.LogInformation($"{imeiDel} 原始胎动值:{fetalMovementValue}, 步数参数变动值:{fetalMovementStepVar},体温参数变动值:{fetalMovementTempVar},血压参数变动值:{fetalMovementBpVar},心理压力参数变动值:{fetalMovementPpVar},胎心参数变动值:{fetalMovementFhrVar}"); - - - fetalMovementValue = fetalMovementValue + fetalMovementStepVar + fetalMovementTempVar + fetalMovementBpVar + fetalMovementPpVar + fetalMovementFhrVar; - - - _logger.LogInformation($"{imeiDel} 孕周:{pregnancyWeeks},胎动数据采样时间:{fetalMovementSampleTime}|{fmNowSubtract.ToString("yyyy-MM-dd HH:mm:ss")}, 采样周期:{statStartTime}-{statEndTime}, 原始胎动值:{fetalMovementMapValue}, 佩戴时间 :{duringMins}|{phrRange.Last()}-{phrRange.First()}, 胎动计算值:{fetalMovementValue}, 胎动最终值:{fetalMovement} 已完成."); - - // 获取胎心数据状态与胎动数据状态一致 - //etalHeartRateIsAbnormal= fhr.IsAbnormal; - var feltalMovementIsAbnormal = fetalHeartRateIsAbnormal; - - await _serviceIotApi.SetFetalMovementConfig(imeiDel, fetalMovement, fetalMovementSampleTime, feltalMovementIsAbnormal); - - // 保存到MySQL数据库 - HisGpsFetalMovement fm = new() - { - FetalMovementId = Guid.NewGuid().ToString("D"), - PersonId = commonPHR!.PersonId, - Serialno = imeiDel, - CreateTime = DateTime.Now, - IsAbnormal = feltalMovementIsAbnormal, - FetalMovementValue = fetalMovement, - SampleTime = fetalMovementSampleTime, - Method = 1, - IsDisplay = 1, - DeviceKey = commonPHR!.DeviceKey - }; - await _hisFetalMovementApiClient.AddAsync(fm).ConfigureAwait(false); - - var device = await _deviceCacheMgr.GetDeviceBySerialNoAsync(imeiDel).ConfigureAwait(false); - var fmMsgId = $"{imeiDel}-{fetalMovementSampleTime}-{Guid.NewGuid().ToString("D")[^3..]}"; - var fmMsgTime = DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(long.Parse(fetalMovementSampleTime.Length < 13 ? fetalMovementSampleTime.PadRight(13, '0') : fetalMovementSampleTime)).ToString("yyyy-MM-dd HH:mm:ss"); - // 胎动数据推送到第三方 - var topic = "topic.push.third"; - var fmThridMsg = new - { - messageId = fmMsgId, - topic = topic, - time = fmMsgTime, - data = new - { - imei = imeiDel, - value = fetalMovement, - isAbnormal = feltalMovementIsAbnormal, - type = "fetalMovement" } - }; - await _serviceMqProcess.ProcessIMEIEventMessageAsync(fmMsgId, topic, 31, fmThridMsg).ConfigureAwait(false); - - // 胎动数据推送到微信 - if (feltalMovementIsAbnormal != 0) - { - topic = "topic.push.wx"; - var fmMsg = new - { - messageId = Guid.NewGuid().ToString("D"), - topic = topic, - time = DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(long.Parse(fetalMovementSampleTime.Length < 13 ? fetalMovementSampleTime.PadRight(13, '0') : fetalMovementSampleTime)).ToString("yyyy-MM-dd HH:mm:ss"), - data = new - { - deviceId = device?.DeviceId, - imei = imeiDel, - alarmTypeId = 12, - alarmDeviceName = imeiDel, - alarmRemarks = JsonConvert.SerializeObject(new { fetalMovementValue = fetalMovement, isAbnormal = feltalMovementIsAbnormal }), - address = string.Empty, - deviceKey = device?.DeviceId - } - }; - await _serviceMqProcess.ProcessIMEIEventMessageAsync(fmMsgId, topic, fmMsg).ConfigureAwait(false); - } - // 设置入库缓存记录 - await _deviceCacheMgr.SetFetalMovementAsync(imeiDel, fetalMovementSampleTime, fm); - - - } - else - { - _logger.LogWarning($"{imeiDel} 孕周 {pregnancyWeeks},超出胎动计算范围"); - } - } - else - { - _logger.LogWarning($"{imeiDel} 最后一条胎心数据与胎动数据的小时差大于2,不计算胎动数据"); - } - } - else - { - _logger.LogInformation($"{imeiDel} 胎动记录{isFetalMovementExisted},数据采样时间:{fetalMovementSampleTime}|{fmNowSubtract.ToString("yyyy-MM-dd HH:mm:ss")}, 周期:{statStartTime}-{statEndTime} 不足两条,不能判断是否持续佩戴"); - - } - } - else - { - _logger.LogInformation($"{imeiDel} 胎动记录{isFetalMovementExisted},数据采样时间:{fetalMovementSampleTime}|{fmNowSubtract.ToString("yyyy-MM-dd HH:mm:ss")}, 周期:{statStartTime}-{statEndTime} 已处理"); - } - } - - - } - } - */ - #endregion - - #region 胎动延时计算(使用线程再延时) - /** - using (_logger.BeginScope(new Dictionary { ["RequestId"] = $"FM-{imeiDel}-{DateTime.Now.ToString("yyyyMMddHHmmss")}" })) - { - var watchConfig = await _deviceCacheMgr.GetGpsDeviceWatchConfigCacheObjectBySerialNoAsync(imeiDel, "0067"); - _logger.LogInformation($"触发胎动计算,设备配置{JsonConvert.SerializeObject(watchConfig)}"); - var isFetalHeartEnable = watchConfig != null && (int)watchConfig["enabled"]! == 1; - var highFreqSampleInterval = (int)watchConfig!["highFreqSampleInterval"]! >= 60 ? (int)watchConfig!["highFreqSampleInterval"]! : 60; - - if (isFetalHeartEnable) - { - var edoc = DateTimeUtil.ToDateTime(watchConfig!["EDOC"]!.ToString()); - // 已经建模 - var commonPHR = await _serviceTDengine.GetLastAsync(imeiDel); - if (commonPHR != null) - { - var phr = await _serviceTDengine.GetBySerialNoAsync(imeiDel, 7); - _logger.LogInformation($"{imeiDel} 计算胎动数据 "); - - var fmNow = DateTime.Now; - // 两小时前 - var fmNowSubtract = fmNow.AddMinutes(-fmNow.Minute).AddSeconds(-fmNow.Second).AddMilliseconds(-fmNow.Millisecond); - - var fetalMovementSampleTime = DateTimeUtil.ConvertToTimeStamp(fmNowSubtract).ToString()[..10]; - - // 统计开始时间 - var statStartTime = fmNowSubtract.AddHours(-2); - // 统计结束时间 - var statEndTime = fmNowSubtract; - - - Thread thread = new(async () => { - try - { - #region 休眠65分钟 - var startTime = DateTime.Now; - //var highFreqSampleInterval2 = (int)watchConfig!["highFreqSampleInterval"]!+5; - var highFreqSampleInterval2 = 65*60; - var during = TimeSpan.FromSeconds(highFreqSampleInterval2); - while (true) - { - if (DateTime.Now - startTime > during) - { - break; - } - await Task.Delay(TimeSpan.FromSeconds(1)); - } - #endregion - - - //var daysPhr = await _serviceTDengine.GetBySerialNoAsync(imeiDel,1); - //// 去除高频 - //var normalPhr = GetNonFreqPregnancyHeartRate(daysPhr, highFreqSampleInterval).OrderByDescending(i => i.LastUpdate); - //// 常规最后一条 - //var lastNormalPhr = normalPhr.First(); - //statEndTime = lastNormalPhr.LastUpdate; - PregnancyHeartRateModel lastPhr = await _serviceTDengine.GetLastAsync(imeiDel); - statEndTime = lastPhr.LastUpdate; - #region 计算 - var isFetalMovementExisted = await _deviceCacheMgr.FetalMovementIsExistedAsync(imeiDel, fetalMovementSampleTime); - _logger.LogInformation($"{imeiDel} 胎动记录{isFetalMovementExisted},数据采样时间:{fetalMovementSampleTime}|{fmNowSubtract.ToString("yyyy-MM-dd HH:mm:ss")}, 周期:{statStartTime}-{statEndTime} 开始"); - - if (!isFetalMovementExisted) - { - /// 开始计算 - var phrRange = phr.Where(i => i.LastUpdate >= statStartTime && i.LastUpdate <= statEndTime) - .OrderByDescending(i => i.LastUpdate) - .Select(i => i.LastUpdate) - .ToList(); - // 判断是否有持续佩戴 - if (phrRange.Count >= 2) - { - // 读取胎心数据 - GeneralParam param = new() - { - Filters = new List - { - new () - { - Key=nameof(HisGpsFetalHeartRate.Serialno), - Value=imeiDel, - ValueType=QueryValueTypeEnum.String, - Operator=QueryOperatorEnum.Equal - }, - //new () - //{ - // Key=nameof(HisGpsFetalHeartRate.SampleTime), - // Value=sampleTime, - // ValueType=QueryValueTypeEnum.String, - // Operator=QueryOperatorEnum.GreaterEqual - //}, - }, - OrderBys = new List - { - new (){ - IsDesc=true, - Key=nameof(HisGpsFetalHeartRate.SampleTime) - } - } - }; - var fetalHeartRateIsAbnormal = 0; - var fhr = await _hisFetalHeartApiClient.GetFirstAsync(param, imeiDel[^2..], null, new RequestHeader { RequestId = Guid.NewGuid().ToString("D") }); - - // 胎心数据时间与胎动时间一致 - var time = long.Parse(fhr.SampleTime.Length < 13 ? fhr.SampleTime.PadRight(13, '0') : fhr.SampleTime); - var fhrSampleTime = DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(time); - - // 胎心数据时间与胎动时间一致,最后一条胎心数据与胎动数据的小时差不大于2 - if ((DateTime.Now.Hour - fhrSampleTime.Hour) <= 4) - { - var duringMins = Math.Abs((phrRange.First() - phrRange.Last()).TotalMinutes); - //在餐后时间段(8:00~10:00,12:00~14:00,18:00~20:00,22:00~24:00)取中间值。其他时间段取正常起始值 - bool isInTimeRanges = IsLastUpdateInTimeRanges(phrRange.First()); - int pregnancyWeeks = (DateTime.Now - edoc.AddDays(-280)).Days / 7; - if (pregnancyWeeks >= 12 && pregnancyWeeks <= 50) + // 体温 + if (true) { - var fetalMovementMap = _mgrFetalMovementNormalValueRangeCache.GetFetalMovements(); - - var fetalMovementMapValue = isInTimeRanges ? fetalMovementMap - .Where(i => - i.PregnancyPeriod![0] <= pregnancyWeeks && - i.PregnancyPeriod[1] >= pregnancyWeeks) - .Select(i => i.MedianMovement) - .FirstOrDefault() - : - fetalMovementMap - .Where(i => - i.PregnancyPeriod![0] <= pregnancyWeeks && - i.PregnancyPeriod[1] >= pregnancyWeeks) - .Select(i => i.InitialMovement) - .FirstOrDefault() - ; - - var fetalMovementValue = (fetalMovementMapValue * duringMins * 2) / 120; - // 四舍五入 - var fetalMovement = (int)Math.Round(fetalMovementValue, 0, MidpointRounding.AwayFromZero); - // _logger.LogInformation($"{imeiDel} segmentCountFMIndex: {i} -- fetalMovementSampleTime:{fetalMovementSampleTime}|{midNight.AddHours(2 * i).ToString("yyyy-MM-dd HH:mm:ss")} -- statStartTime: {statStartTime} -- statEndTime: {statEndTime}-- isFetalMovementExisted: {isFetalMovementExisted} "); - - #region 生理健康与胎动的关系 - /// (步数)运动步数超过1500步则加1; - /// (体温)低烧则胎动减1,高烧胎动减2;低烧是37.3~38.5,38.6以上是高烧。 - /// (血压)血压收缩压大于160则加1。 - /// (心理)心理压力高加2,压力中加1; - - var fetalMovementStepVar = 0; - var fetalMovementTempVar = 0; - var fetalMovementBpVar = 0; - var fetalMovementPpVar = 0; - var fetalMovementFhrVar = 0; - // 步数 - if (true) + var temp = await _personCacheMgr.GetTemperaturePeriodicityAsync(imeiDel); + if (temp != null) { - var step = await _personCacheMgr.GetStepPeriodicityAsync(imeiDel); - if (step != null) + if (DateTime.Now.Hour - ((DateTime)temp?.LastUpdate!).Hour <= 2) { - if (DateTime.Now.Hour - ((DateTime)step?.LastUpdate!).Hour <= 2) + // 中烧 + if (temp.Temperature >= 37.3M && temp.Temperature <= 38.5M) { - if (step.Steps > 1500) fetalMovementStepVar = 1; + fetalMovementTempVar = -1; } - else + // 高烧 + if (temp.Temperature >= 38.6M) { - _logger.LogWarning($"{imeiDel} 周期步数 时间无效"); + fetalMovementTempVar = -2; } } else { - _logger.LogWarning($"{imeiDel} 周期步数无数据"); + _logger.LogWarning($"{imeiDel} 周期体温 时间无效"); } - } - - // 体温 - if (true) + else { - var temp = await _personCacheMgr.GetTemperaturePeriodicityAsync(imeiDel); - if (temp != null) - { - if (DateTime.Now.Hour - ((DateTime)temp?.LastUpdate!).Hour <= 2) - { - // 中烧 - if (temp.Temperature >= 37.3M && temp.Temperature <= 38.5M) - { - fetalMovementTempVar = -1; - } - // 高烧 - if (temp.Temperature >= 38.6M) - { - fetalMovementTempVar = -2; - } - } - else - { - _logger.LogWarning($"{imeiDel} 周期体温 时间无效"); - } - } - else - { - _logger.LogWarning($"{imeiDel} 周期体温无数据"); - } - + _logger.LogWarning($"{imeiDel} 周期体温无数据"); } - // 血压 - if (true) + } + + // 血压 + if (true) + { + var bp = await _personCacheMgr.GetBloodPressPeriodicityAsync(imeiDel); + if (bp != null) { - var bp = await _personCacheMgr.GetBloodPressPeriodicityAsync(imeiDel); - if (bp != null) + if (DateTime.Now.Hour - ((DateTime)bp?.LastUpdate!).Hour <= 2) { - if (DateTime.Now.Hour - ((DateTime)bp?.LastUpdate!).Hour <= 2) - { - if (bp.SystolicValue > 160) - { - fetalMovementBpVar = 1; - } - } - else + if (bp.SystolicValue > 160) { - _logger.LogWarning($"{imeiDel} 周期血压 时间无效"); + fetalMovementBpVar = 1; } } else { - _logger.LogWarning($"{imeiDel} 周期血压无数据"); + _logger.LogWarning($"{imeiDel} 周期血压 时间无效"); } - } - // 心理 - if (true) + else { - //-1 不处理, - //0 正常, - //1 轻度, - //2 中度; - //3 重度; - GeneralParam psychResultParam = new() - { - Filters = new List + _logger.LogWarning($"{imeiDel} 周期血压无数据"); + } + + } + // 心理 + if (false) + { + //-1 不处理, + //0 正常, + //1 轻度, + //2 中度; + //3 重度; + GeneralParam psychResultParam = new() + { + Filters = new List { new () { @@ -1194,179 +799,181 @@ namespace HealthMonitor.WebApi // Operator=QueryOperatorEnum.GreaterEqual //}, }, - OrderBys = new List + OrderBys = new List { new (){ IsDesc=true, Key=nameof(HisGpsPsychResult.Serialno) } } - }; - var psych = await _hisPsychResultApiClient.GetFirstAsync(psychResultParam, imeiDel[^2..], null, new RequestHeader { RequestId = Guid.NewGuid().ToString("D") }); - if (psych != null) + }; + var psych = await _hisPsychResultApiClient.GetFirstAsync(psychResultParam, imeiDel[^2..], null, new RequestHeader { RequestId = Guid.NewGuid().ToString("D") }); + if (psych != null) + { + if (psych?.StressScore == 2) { - if (psych?.StressScore == 2) - { - fetalMovementPpVar = 1; - } - - if (psych?.StressScore == 3) - { - fetalMovementPpVar = 2; - } + fetalMovementPpVar = 1; } - else + + if (psych?.StressScore == 3) { - _logger.LogWarning($"{imeiDel} 周期心理压力无数据"); + fetalMovementPpVar = 2; } - + } + else + { + _logger.LogWarning($"{imeiDel} 周期心理压力无数据"); } - #endregion + } - #region 胎心与胎动的关系 - /// 胎心值过缓时,则胎动数量减1;胎心值过速时,则胎动也加1。 - /// 此值允许在上限值上继续增加,在下限值上继续减少,最小值为0。 - /// 告警上限阀值 + #endregion - //1表示偏高;2表示偏低 - if (true) + #region 胎心与胎动的关系 + /// 胎心值过缓时,则胎动数量减1;胎心值过速时,则胎动也加1。 + /// 此值允许在上限值上继续增加,在下限值上继续减少,最小值为0。 + /// 告警上限阀值 + + //1表示偏高;2表示偏低 + if (true) + { + if (fhr.IsAbnormal == 2) { - if (fhr.IsAbnormal == 2) - { - fetalMovementPpVar = -1; - } + fetalMovementPpVar = -1; + } - if (fhr.IsAbnormal == 1) - { - fetalMovementPpVar = 1; - } + if (fhr.IsAbnormal == 1) + { + fetalMovementPpVar = 1; } + } - #endregion + #endregion + //var fetalMovementTimeVar = fetalMovementValue; - _logger.LogInformation($"{imeiDel} 原始胎动值:{fetalMovementValue}, 步数参数变动值:{fetalMovementStepVar},体温参数变动值:{fetalMovementTempVar},血压参数变动值:{fetalMovementBpVar},心理压力参数变动值:{fetalMovementPpVar},胎心参数变动值:{fetalMovementFhrVar}"); + _logger.LogInformation($"{imeiDel} 时间比例胎动值:{fetalMovementTimeVar}, 步数参数变动值:{fetalMovementStepVar},体温参数变动值:{fetalMovementTempVar},血压参数变动值:{fetalMovementBpVar},心理压力参数变动值:{fetalMovementPpVar},胎心参数变动值:{fetalMovementFhrVar}"); - fetalMovementValue = fetalMovementValue + fetalMovementStepVar + fetalMovementTempVar + fetalMovementBpVar + fetalMovementPpVar + fetalMovementFhrVar; + var fetalMovementValue = fetalMovementTimeVar + fetalMovementStepVar + fetalMovementTempVar + fetalMovementBpVar + fetalMovementPpVar + fetalMovementFhrVar; + // 四舍五入 + var fetalMovement = (int)Math.Round(fetalMovementValue, 0, MidpointRounding.AwayFromZero); - _logger.LogInformation($"{imeiDel} 孕周:{pregnancyWeeks},胎动数据采样时间:{fetalMovementSampleTime}|{fmNowSubtract.ToString("yyyy-MM-dd HH:mm:ss")}, 采样周期:{statStartTime}-{statEndTime}, 原始胎动值:{fetalMovementMapValue}, 佩戴时间 :{duringMins}|{phrRange.Last()}-{phrRange.First()}, 胎动计算值:{fetalMovementValue}, 胎动最终值:{fetalMovement} 已完成."); + _logger.LogInformation($"{imeiDel} 孕周:{pregnancyWeeks},胎动数据采样时间:{fetalMovementSampleTime}|{statStartTime.ToString("yyyy-MM-dd HH:mm:ss")}, 采样周期:{statStartTime}-{statEndTime}, 时间比例胎动值:{fetalMovementMapValue}, 佩戴时间 :{duringMins}|{phrRange.Last()}-{phrRange.First()}, 胎动计算值:{fetalMovementTimeVar}, 胎动四舍五入最终值:{fetalMovement} 已完成."); - // 获取胎心数据状态与胎动数据状态一致 - //etalHeartRateIsAbnormal= fhr.IsAbnormal; - var feltalMovementIsAbnormal = fetalHeartRateIsAbnormal; + // 获取胎心数据状态与胎动数据状态一致 + //etalHeartRateIsAbnormal= fhr.IsAbnormal; + var feltalMovementIsAbnormal = fetalHeartRateIsAbnormal; - await _serviceIotApi.SetFetalMovementConfig(imeiDel, fetalMovement, fetalMovementSampleTime, feltalMovementIsAbnormal); + await _serviceIotApi.SetFetalMovementConfig(imeiDel, fetalMovement, fetalMovementSampleTime, feltalMovementIsAbnormal); - // 保存到MySQL数据库 - HisGpsFetalMovement fm = new() + // 保存到MySQL数据库 + HisGpsFetalMovement fm = new() + { + FetalMovementId = Guid.NewGuid().ToString("D"), + PersonId = commonPHR!.PersonId, + Serialno = imeiDel, + CreateTime = DateTime.Now, + IsAbnormal = feltalMovementIsAbnormal, + FetalMovementValue = fetalMovement, + SampleTime = fetalMovementSampleTime, + Method = 1, + IsDisplay = 1, + DeviceKey = commonPHR!.DeviceKey + }; + await _hisFetalMovementApiClient.AddAsync(fm).ConfigureAwait(false); + + var device = await _deviceCacheMgr.GetDeviceBySerialNoAsync(imeiDel).ConfigureAwait(false); + var fmMsgId = $"{imeiDel}-{fetalMovementSampleTime}-{Guid.NewGuid().ToString("D")[^3..]}"; + var fmMsgTime = DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(long.Parse(fetalMovementSampleTime.Length < 13 ? fetalMovementSampleTime.PadRight(13, '0') : fetalMovementSampleTime)).ToString("yyyy-MM-dd HH:mm:ss"); + // 胎动数据推送到第三方 + var topic = "topic.push.third"; + var fmThridMsg = new + { + messageId = fmMsgId, + topic = topic, + time = fmMsgTime, + data = new { - FetalMovementId = Guid.NewGuid().ToString("D"), - PersonId = commonPHR!.PersonId, - Serialno = imeiDel, - CreateTime = DateTime.Now, - IsAbnormal = feltalMovementIsAbnormal, - FetalMovementValue = fetalMovement, - SampleTime = fetalMovementSampleTime, - Method = 1, - IsDisplay = 1, - DeviceKey = commonPHR!.DeviceKey - }; - await _hisFetalMovementApiClient.AddAsync(fm).ConfigureAwait(false); - - var device = await _deviceCacheMgr.GetDeviceBySerialNoAsync(imeiDel).ConfigureAwait(false); - var fmMsgId = $"{imeiDel}-{fetalMovementSampleTime}-{Guid.NewGuid().ToString("D")[^3..]}"; - var fmMsgTime = DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(long.Parse(fetalMovementSampleTime.Length < 13 ? fetalMovementSampleTime.PadRight(13, '0') : fetalMovementSampleTime)).ToString("yyyy-MM-dd HH:mm:ss"); - // 胎动数据推送到第三方 - var topic = "topic.push.third"; - var fmThridMsg = new + imei = imeiDel, + value = fetalMovement, + isAbnormal = feltalMovementIsAbnormal, + type = "fetalMovement" + } + }; + await _serviceMqProcess.ProcessIMEIEventMessageAsync(fmMsgId, topic, 31, fmThridMsg).ConfigureAwait(false); + + // 胎动数据推送到微信 + if (feltalMovementIsAbnormal != 0) + { + topic = "topic.push.wx"; + var fmMsg = new { - messageId = fmMsgId, + messageId = Guid.NewGuid().ToString("D"), topic = topic, - time = fmMsgTime, + time = DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(long.Parse(fetalMovementSampleTime.Length < 13 ? fetalMovementSampleTime.PadRight(13, '0') : fetalMovementSampleTime)).ToString("yyyy-MM-dd HH:mm:ss"), data = new { + deviceId = device?.DeviceId, imei = imeiDel, - value = fetalMovement, - isAbnormal = feltalMovementIsAbnormal, - type = "fetalMovement" + alarmTypeId = 12, + alarmDeviceName = imeiDel, + alarmRemarks = JsonConvert.SerializeObject(new { fetalMovementValue = fetalMovement, isAbnormal = feltalMovementIsAbnormal }), + address = string.Empty, + deviceKey = device?.DeviceId } }; - await _serviceMqProcess.ProcessIMEIEventMessageAsync(fmMsgId, topic, 31, fmThridMsg).ConfigureAwait(false); - - // 胎动数据推送到微信 - if (feltalMovementIsAbnormal != 0) - { - topic = "topic.push.wx"; - var fmMsg = new - { - messageId = Guid.NewGuid().ToString("D"), - topic = topic, - time = DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(long.Parse(fetalMovementSampleTime.Length < 13 ? fetalMovementSampleTime.PadRight(13, '0') : fetalMovementSampleTime)).ToString("yyyy-MM-dd HH:mm:ss"), - data = new - { - deviceId = device?.DeviceId, - imei = imeiDel, - alarmTypeId = 12, - alarmDeviceName = imeiDel, - alarmRemarks = JsonConvert.SerializeObject(new { fetalMovementValue = fetalMovement, isAbnormal = feltalMovementIsAbnormal }), - address = string.Empty, - deviceKey = device?.DeviceId - } - }; - await _serviceMqProcess.ProcessIMEIEventMessageAsync(fmMsgId, topic, fmMsg).ConfigureAwait(false); - } - // 设置入库缓存记录 - await _deviceCacheMgr.SetFetalMovementAsync(imeiDel, fetalMovementSampleTime, fm); + await _serviceMqProcess.ProcessIMEIEventMessageAsync(fmMsgId, topic, fmMsg).ConfigureAwait(false); + } + // 设置入库缓存记录 + await _deviceCacheMgr.SetFetalMovementAsync(imeiDel, fetalMovementSampleTime, fm); - } - else - { - _logger.LogWarning($"{imeiDel} 孕周 {pregnancyWeeks},超出胎动计算范围"); - } } else { - _logger.LogWarning($"{imeiDel} 最后一条胎心数据与胎动数据的小时差大于2,不计算胎动数据"); + _logger.LogWarning($"{imeiDel} 孕周 {pregnancyWeeks},超出胎动计算范围"); } } else { - _logger.LogInformation($"{imeiDel} 胎动记录{isFetalMovementExisted},数据采样时间:{fetalMovementSampleTime}|{fmNowSubtract.ToString("yyyy-MM-dd HH:mm:ss")}, 周期:{statStartTime}-{statEndTime} 不足两条,不能判断是否持续佩戴"); - + _logger.LogWarning($"{imeiDel} 最后一条胎心数据与胎动数据的小时差大于2,不计算胎动数据"); } } else { - _logger.LogInformation($"{imeiDel} 胎动记录{isFetalMovementExisted},数据采样时间:{fetalMovementSampleTime}|{fmNowSubtract.ToString("yyyy-MM-dd HH:mm:ss")}, 周期:{statStartTime}-{statEndTime} 已处理"); - } - - #endregion + _logger.LogInformation($"{imeiDel} 胎动记录{isFetalMovementExisted},数据采样时间:{fetalMovementSampleTime}|{statStartTime.ToString("yyyy-MM-dd HH:mm:ss")}, 周期:{statStartTime}-{statEndTime} 不足两条,不能判断是否持续佩戴"); + } } - catch (Exception ex) + else { - - _logger.LogError($"处理延时计算胎动数据异常:{ex.Message}, {ex.StackTrace}"); + _logger.LogInformation($"{imeiDel} 胎动记录{isFetalMovementExisted},数据采样时间:{fetalMovementSampleTime}|{statStartTime.ToString("yyyy-MM-dd HH:mm:ss")}, 周期:{statStartTime}-{statEndTime} 已处理"); } + } + else + { + _logger.LogWarning($"{imeiDel} 没有胎心建模数据"); + } - }); - - thread.Start(); } + else + { + _logger.LogWarning($"{imeiDel} 胎心监测功能没有开启"); + } + } + catch (Exception ex ) + { - + _logger.LogError($"{imeiDel} 计算胎动数据异常 {ex.Message}\n {ex.StackTrace}"); } + + } */ #endregion - #region 胎动延时计算(实时now是3小时,计算 lastupdate 0~2范围的数据,) - ///实时now的hour是3小时,计算 lastupdate 0~2范围的数据, - ///只在 HOUR = new int[] { 1, 3, 5, 7, 9, 11,13, 15, 17, 19, 21, 23 };是奇数 - + #region 相隔1小时胎动延时计算(实时now是2小时,计算 lastupdate 0~1范围的数据,) using (_logger.BeginScope(new Dictionary { ["RequestId"] = $"FM-{imeiDel}-{DateTime.Now.ToString("yyyyMMddHHmmss")}" })) { try @@ -1393,13 +1000,14 @@ namespace HealthMonitor.WebApi if (fmNowHour == 1) { - // last_update 22~0 - statStartTime = fmNow.Date.AddDays(-1).AddHours(22); + // last_update 23~0 + statStartTime = fmNow.Date.AddDays(-1).AddHours(23); statEndTime = fmNow.Date; } else { - statStartTime = fmNow.Date.AddHours(fmNowHour - 3); + // last_update 0~1->2,1~2->3,2~3->4...21~22->23 + statStartTime = fmNow.Date.AddHours(fmNowHour - 2); statEndTime = fmNow.Date.AddHours(fmNowHour - 1); } @@ -1454,8 +1062,11 @@ namespace HealthMonitor.WebApi var time = long.Parse(fhr.SampleTime.Length < 13 ? fhr.SampleTime.PadRight(13, '0') : fhr.SampleTime); var fhrSampleTime = DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(time); - // 胎心数据时间与胎动时间一致,最后一条胎心数据与胎动数据的小时差不大于3 - if ((DateTime.Now.Hour - fhrSampleTime.Hour) <= 3) + //// 胎心数据时间与胎动时间一致,最后一条胎心数据与胎动数据的小时差不大于3 + //if ((DateTime.Now.Hour - fhrSampleTime.Hour) <= 3) + + // 胎心数据时间与胎动时间一致,最后一条胎心数据与胎动数据的小时差不大于2 + if ((DateTime.Now.Hour - fhrSampleTime.Hour) <= 2) { var duringMins = Math.Abs((phrRange.First() - phrRange.Last()).TotalMinutes); //在餐后时间段(8:00~10:00,12:00~14:00,18:00~20:00,22:00~24:00)取中间值。其他时间段取正常起始值 @@ -1481,155 +1092,167 @@ namespace HealthMonitor.WebApi .FirstOrDefault() ; - var fetalMovementTimeVar = (fetalMovementMapValue * duringMins * 2) / 120; - //// 四舍五入 - //var fetalMovement = (int)Math.Round(fetalMovementValue, 0, MidpointRounding.AwayFromZero); - // _logger.LogInformation($"{imeiDel} segmentCountFMIndex: {i} -- fetalMovementSampleTime:{fetalMovementSampleTime}|{midNight.AddHours(2 * i).ToString("yyyy-MM-dd HH:mm:ss")} -- statStartTime: {statStartTime} -- statEndTime: {statEndTime}-- isFetalMovementExisted: {isFetalMovementExisted} "); + // var fetalMovementTimeVar = (fetalMovementMapValue * duringMins * 2) / 120; + var fetalMovementTimeVar = (fetalMovementMapValue * duringMins ) / 60; + + if (duringMins < 59) + { + // 取12小时胎动总数最小值 + fetalMovementMapValue = fetalMovementMap + .Where(i =>i.PregnancyPeriod![0] <= pregnancyWeeks &&i.PregnancyPeriod[1] >= pregnancyWeeks) + .First() + .TwelveHourMovementRange[0]; + //取12小时胎动总数最小值的平均值 + fetalMovementTimeVar = fetalMovementMapValue / 12; + + _logger.LogWarning($"{imeiDel} 佩戴不足1小时,12小时胎动总数最小值{fetalMovementMapValue},平均值{fetalMovementTimeVar}"); + } + #region 生理健康与胎动的关系 /// (步数)运动步数超过1500步则加1; /// (体温)低烧则胎动减1,高烧胎动减2;低烧是37.3~38.5,38.6以上是高烧。 /// (血压)血压收缩压大于160则加1。 /// (心理)心理压力高加2,压力中加1; - var fetalMovementStepVar = 0; - var fetalMovementTempVar = 0; - var fetalMovementBpVar = 0; - var fetalMovementPpVar = 0; - var fetalMovementFhrVar = 0; - // 步数 - if (false) + var fetalMovementStepVar = 0; + var fetalMovementTempVar = 0; + var fetalMovementBpVar = 0; + var fetalMovementPpVar = 0; + var fetalMovementFhrVar = 0; + // 步数 + if (false) + { + var step = await _personCacheMgr.GetStepPeriodicityAsync(imeiDel); + if (step != null) { - var step = await _personCacheMgr.GetStepPeriodicityAsync(imeiDel); - if (step != null) + if (DateTime.Now.Hour - ((DateTime)step?.LastUpdate!).Hour <= 2) { - if (DateTime.Now.Hour - ((DateTime)step?.LastUpdate!).Hour <= 2) - { - if (step.Steps > 1500) fetalMovementStepVar = 1; - } - else - { - _logger.LogWarning($"{imeiDel} 周期步数 时间无效"); - } + if (step.Steps > 1500) fetalMovementStepVar = 1; } else { - _logger.LogWarning($"{imeiDel} 周期步数无数据"); + _logger.LogWarning($"{imeiDel} 周期步数 时间无效"); } - + } + else + { + _logger.LogWarning($"{imeiDel} 周期步数无数据"); } - // 体温 - if (true) + } + + // 体温 + if (true) + { + var temp = await _personCacheMgr.GetTemperaturePeriodicityAsync(imeiDel); + if (temp != null) { - var temp = await _personCacheMgr.GetTemperaturePeriodicityAsync(imeiDel); - if (temp != null) + if (DateTime.Now.Hour - ((DateTime)temp?.LastUpdate!).Hour <= 2) { - if (DateTime.Now.Hour - ((DateTime)temp?.LastUpdate!).Hour <= 2) + // 中烧 + if (temp.Temperature >= 37.3M && temp.Temperature <= 38.5M) { - // 中烧 - if (temp.Temperature >= 37.3M && temp.Temperature <= 38.5M) - { - fetalMovementTempVar = -1; - } - // 高烧 - if (temp.Temperature >= 38.6M) - { - fetalMovementTempVar = -2; - } + fetalMovementTempVar = -1; } - else + // 高烧 + if (temp.Temperature >= 38.6M) { - _logger.LogWarning($"{imeiDel} 周期体温 时间无效"); + fetalMovementTempVar = -2; } } else { - _logger.LogWarning($"{imeiDel} 周期体温无数据"); + _logger.LogWarning($"{imeiDel} 周期体温 时间无效"); } - + } + else + { + _logger.LogWarning($"{imeiDel} 周期体温无数据"); } - // 血压 - if (true) + } + + // 血压 + if (true) + { + var bp = await _personCacheMgr.GetBloodPressPeriodicityAsync(imeiDel); + if (bp != null) { - var bp = await _personCacheMgr.GetBloodPressPeriodicityAsync(imeiDel); - if (bp != null) + if (DateTime.Now.Hour - ((DateTime)bp?.LastUpdate!).Hour <= 2) { - if (DateTime.Now.Hour - ((DateTime)bp?.LastUpdate!).Hour <= 2) - { - if (bp.SystolicValue > 160) - { - fetalMovementBpVar = 1; - } - } - else + if (bp.SystolicValue > 160) { - _logger.LogWarning($"{imeiDel} 周期血压 时间无效"); + fetalMovementBpVar = 1; } } else { - _logger.LogWarning($"{imeiDel} 周期血压无数据"); + _logger.LogWarning($"{imeiDel} 周期血压 时间无效"); } - } - // 心理 - if (false) + else { - //-1 不处理, - //0 正常, - //1 轻度, - //2 中度; - //3 重度; - GeneralParam psychResultParam = new() - { - Filters = new List + _logger.LogWarning($"{imeiDel} 周期血压无数据"); + } + + } + // 心理 + if (false) + { + //-1 不处理, + //0 正常, + //1 轻度, + //2 中度; + //3 重度; + GeneralParam psychResultParam = new() { - new () - { - Key=nameof(HisGpsPsychResult.Serialno), - Value=imeiDel, - ValueType=QueryValueTypeEnum.String, - Operator=QueryOperatorEnum.Equal - }, - //new () - //{ - // Key=nameof(HisGpsFetalHeartRate.SampleTime), - // Value=sampleTime, - // ValueType=QueryValueTypeEnum.String, - // Operator=QueryOperatorEnum.GreaterEqual - //}, - }, - OrderBys = new List + Filters = new List + { + new () { - new (){ - IsDesc=true, - Key=nameof(HisGpsPsychResult.Serialno) - } + Key=nameof(HisGpsPsychResult.Serialno), + Value=imeiDel, + ValueType=QueryValueTypeEnum.String, + Operator=QueryOperatorEnum.Equal + }, + //new () + //{ + // Key=nameof(HisGpsFetalHeartRate.SampleTime), + // Value=sampleTime, + // ValueType=QueryValueTypeEnum.String, + // Operator=QueryOperatorEnum.GreaterEqual + //}, + }, + OrderBys = new List + { + new (){ + IsDesc=true, + Key=nameof(HisGpsPsychResult.Serialno) } - }; - var psych = await _hisPsychResultApiClient.GetFirstAsync(psychResultParam, imeiDel[^2..], null, new RequestHeader { RequestId = Guid.NewGuid().ToString("D") }); - if (psych != null) + } + }; + var psych = await _hisPsychResultApiClient.GetFirstAsync(psychResultParam, imeiDel[^2..], null, new RequestHeader { RequestId = Guid.NewGuid().ToString("D") }); + if (psych != null) + { + if (psych?.StressScore == 2) { - if (psych?.StressScore == 2) - { - fetalMovementPpVar = 1; - } - - if (psych?.StressScore == 3) - { - fetalMovementPpVar = 2; - } + fetalMovementPpVar = 1; } - else + + if (psych?.StressScore == 3) { - _logger.LogWarning($"{imeiDel} 周期心理压力无数据"); + fetalMovementPpVar = 2; } - + } + else + { + _logger.LogWarning($"{imeiDel} 周期心理压力无数据"); } - #endregion + } + + #endregion #region 胎心与胎动的关系 /// 胎心值过缓时,则胎动数量减1;胎心值过速时,则胎动也加1。 @@ -1651,12 +1274,11 @@ namespace HealthMonitor.WebApi } #endregion - //var fetalMovementTimeVar = fetalMovementValue; _logger.LogInformation($"{imeiDel} 时间比例胎动值:{fetalMovementTimeVar}, 步数参数变动值:{fetalMovementStepVar},体温参数变动值:{fetalMovementTempVar},血压参数变动值:{fetalMovementBpVar},心理压力参数变动值:{fetalMovementPpVar},胎心参数变动值:{fetalMovementFhrVar}"); - var fetalMovementValue = fetalMovementTimeVar + fetalMovementStepVar + fetalMovementTempVar + fetalMovementBpVar + fetalMovementPpVar + fetalMovementFhrVar; + var fetalMovementValue = fetalMovementTimeVar + fetalMovementStepVar + fetalMovementTempVar + fetalMovementBpVar + fetalMovementPpVar + fetalMovementFhrVar; // 四舍五入 var fetalMovement = (int)Math.Round(fetalMovementValue, 0, MidpointRounding.AwayFromZero); @@ -1759,18 +1381,18 @@ namespace HealthMonitor.WebApi } } - else + else { _logger.LogWarning($"{imeiDel} 胎心监测功能没有开启"); } } - catch (Exception ex ) + catch (Exception ex) { _logger.LogError($"{imeiDel} 计算胎动数据异常 {ex.Message}\n {ex.StackTrace}"); } - + } #endregion