From 3af32b9d7145de178302671cce7875f117572bd4 Mon Sep 17 00:00:00 2001 From: H Vs Date: Wed, 16 Oct 2024 16:29:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BD=93=E5=89=8D=E8=AE=A1=E7=AE=97=E8=83=8E?= =?UTF-8?q?=E5=8A=A8=E6=97=B6=E9=97=B4=E4=B8=8E=E5=BF=83=E7=8E=87=E6=97=B6?= =?UTF-8?q?=E9=97=B42=E5=B0=8F=E6=97=B6=E7=9A=84=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Resolver/PregnancyHeartRateResolver.cs | 14 ++- HealthMonitor.WebApi/Worker.cs | 97 +++++++++++++------ 2 files changed, 76 insertions(+), 35 deletions(-) diff --git a/HealthMonitor.Service/Resolver/PregnancyHeartRateResolver.cs b/HealthMonitor.Service/Resolver/PregnancyHeartRateResolver.cs index c071792..43780cb 100644 --- a/HealthMonitor.Service/Resolver/PregnancyHeartRateResolver.cs +++ b/HealthMonitor.Service/Resolver/PregnancyHeartRateResolver.cs @@ -293,10 +293,16 @@ namespace HealthMonitor.Service.Resolver .AddMinutes(pushMin) .AddSeconds(pushSec); - // 当前时间与lastUpdate的时间差小于2小时,sample_time采集时间与计划计算时间最长是2小时 - if (scheduledDateTime.Subtract((DateTime)heartRate.LastUpdate!).TotalHours <= 2) + // 当前时间与lastUpdate的时间差小于2小时,0~1->2,0~1的数据是1点的sample_time,2点计算 + if (DateTime.Now.Subtract((DateTime)heartRate.LastUpdate!).TotalHours <= 2) { - + selectedScheduleHour = DateTimeUtil.GetNextHour(((DateTime)heartRate.LastUpdate).Hour); + scheduledDateTime = DateTime.Now.Date + .AddHours(selectedScheduleHour) + .AddMinutes(pushMin) + .AddSeconds(pushSec); + + if (((DateTime)heartRate.LastUpdate).Hour % 2 == 0) { // lastUpdate偶数小时 @@ -311,7 +317,7 @@ namespace HealthMonitor.Service.Resolver // sample_time采集时间与计划计算时间最长是2小时,次日上报引起 else { - _logger.LogWarning($"iot 上报延迟,sample_time采集时间与计划计算时间超过2小时"); + _logger.LogWarning($"iot 上报延迟,last_update与计划计算时间超过2小时"); } diff --git a/HealthMonitor.WebApi/Worker.cs b/HealthMonitor.WebApi/Worker.cs index 69191d1..fd94186 100644 --- a/HealthMonitor.WebApi/Worker.cs +++ b/HealthMonitor.WebApi/Worker.cs @@ -23,6 +23,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Reflection; +using System.Text.RegularExpressions; using System.Threading.Channels; using TDengineDriver; using TDengineTMQ; @@ -157,7 +158,13 @@ namespace HealthMonitor.WebApi // var key = $"health_monitor/schedule_push/imei/{bp.Serialno}"; var key = e.Kv.Key.ToStringUtf8(); - var imeiDel = key.Split('/')[^1]; + //var imeiDel = key.Split('/')[^1]; + var imeiDel = ExtractImei(key); + if (string.IsNullOrEmpty(imeiDel)) + { + _logger.LogWarning("定时器不能抽取imei"); + return; + } var schedule_push = await _serviceEtcd.GetValAsync(key).ConfigureAwait(false); if (string.IsNullOrWhiteSpace(schedule_push)) { @@ -1426,7 +1433,7 @@ namespace HealthMonitor.WebApi #region 相隔1小时胎动延时计算(实时now是2小时,计算 lastupdate 0~1范围的数据,) /** - 0~1->2 + 0~1->2,0~1 的lastupdate数据,sample_time 是1点,计算时间2点 1~2->3 2~3->4 3~4->5 @@ -1455,7 +1462,7 @@ namespace HealthMonitor.WebApi if (!string.IsNullOrEmpty(trigger)) { var triggerHeartRate = JsonConvert.DeserializeObject(trigger); - using (_logger.BeginScope(new Dictionary { ["RequestId"] = $"FM-{imeiDel}-{DateTime.Now.ToString("yyyyMMddHHmmss")}" })) + using (_logger.BeginScope(new Dictionary { ["RequestId"] = $"FM-{imeiDel}-{triggerHeartRate?.MessageId}" })) { try { @@ -2715,53 +2722,64 @@ namespace HealthMonitor.WebApi var daysPhr = await _serviceTDengine.GetBySerialNoAsync(heartRate.Serialno, 7); - //var start = ""; - //var end = ""; + DateTime fmNow = DateTime.Now; int fmNowHour = fmNow.Hour; DateTime startHour; DateTime endHour; - // 跨天 - if (fmNowHour == 1) + if (fmNow.Subtract((DateTime)heartRate.LastUpdate!).Hours>2) { - // last_update 23~0 - startHour = fmNow.Date.AddDays(-1).AddHours(((DateTime)heartRate.LastUpdate!).Hour); - endHour = fmNow.Date; + _logger.LogWarning($"{heartRate.Serialno} 当前计算时间与lastUpdate 时差超过2小时,将从当前触发心率到当天的所有心率计算对应的胎动"); + startHour = ((DateTime)heartRate.LastUpdate!).Date.AddHours(((DateTime)heartRate.LastUpdate!).Hour); + endHour = ((DateTime)heartRate.LastUpdate!).Date.AddDays(1); } else { - // last_update 0~1->2,1~2->3,2~3->4...21~22->23 - startHour = fmNow.Date.AddHours(((DateTime)heartRate.LastUpdate!).Hour); - endHour = fmNow.Date.AddHours(fmNowHour - 1); + // 跨天 + if (fmNowHour == 1) + { + // last_update 23~0 + startHour = fmNow.Date.AddDays(-1).AddHours(((DateTime)heartRate.LastUpdate!).Hour); + endHour = fmNow.Date; + } + else + { + // last_update 0~1->2,1~2->3,2~3->4...21~22->23 + startHour = fmNow.Date.AddHours(((DateTime)heartRate.LastUpdate!).Hour); + endHour = fmNow.Date.AddHours(fmNowHour - 1); + } } - var filterPhr = daysPhr - .Where(i => i.LastUpdate >= startHour && i.LastUpdate<= endHour) + try + { + + + var filterPhr = daysPhr + .Where(i => i.LastUpdate >= startHour && i.LastUpdate <= endHour) .OrderBy(i => i.LastUpdate).ToList(); - var startPhr = filterPhr.First(); - var endPhr = filterPhr.Last(); + if (filterPhr.Count < 2) + { + _logger.LogWarning($"{heartRate.Serialno} 胎动统计数据少于2条,不统计,{string.Join('-', filterPhr.Select(i=>i.LastUpdate))}"); + return; + } + var startPhr = filterPhr.First(); + var endPhr = filterPhr.Last(); - // 数据统计边界 - var boundaryStatStartTime = startPhr.LastUpdate.Date.AddHours(startPhr.LastUpdate.Hour); - var boundaryStatEndTime = endPhr.LastUpdate.Date.AddHours(endPhr.LastUpdate.Hour + 1); - _logger.LogInformation($"{heartRate.Serialno} 胎动统计边界{boundaryStatStartTime}-{boundaryStatEndTime}"); + + // 数据统计边界 + var boundaryStatStartTime = startPhr.LastUpdate.Date.AddHours(startPhr.LastUpdate.Hour); + var boundaryStatEndTime = endPhr.LastUpdate.Date.AddHours(endPhr.LastUpdate.Hour + 1); + + _logger.LogInformation($"{heartRate.Serialno} 胎动统计边界{boundaryStatStartTime}-{boundaryStatEndTime}"); - try - { - if (filterPhr.Count<2) - { - _logger.LogWarning($"{heartRate.Serialno} 胎动统计边界{boundaryStatStartTime}-{boundaryStatEndTime},数据少于2条,不统计"); - return; - } - var c = 0; while (true) { @@ -2795,8 +2813,8 @@ namespace HealthMonitor.WebApi } _logger.LogInformation($"{heartRate.Serialno} 当前胎动统计周期{statStartTime.ToString("yyyy-MM-dd HH:mm:ss")}-{statEndTime.ToString("yyyy-MM-dd HH:mm:ss")},对应的常规心率ID{string.Join(",", segmentPhr.Select(i => i.MessageId))}"); - - var fetalMovementSampleTime = DateTimeUtil.ConvertToTimeStamp(statStartTime).ToString()[..10]; + // 0~1 点的lastUpdate数据sample_time是1点,计算是2点 + var fetalMovementSampleTime = DateTimeUtil.ConvertToTimeStamp(statEndTime).ToString()[..10]; var isFetalMovementExisted = await _deviceCacheMgr.FetalMovementIsExistedAsync(heartRate.Serialno, fetalMovementSampleTime); if (!isFetalMovementExisted) @@ -3197,5 +3215,22 @@ namespace HealthMonitor.WebApi // 返回当前时间刻度下的 DateTime,保留小时和新计算的分钟,秒和毫秒设为0 return new DateTime(lastUpdate.Year, lastUpdate.Month, lastUpdate.Day, lastUpdate.Hour, intervalStartMinute, 0, 0); } + + public static string ExtractImei(string input) + { + // 提取 'imei/' 后的数字 + string pattern = @"imei/(\d+)"; + + Match match = Regex.Match(input, pattern); + + if (match.Success) + { + return match.Groups[1].Value; + } + else + { + return string.Empty; + } + } } }