From d16fb914002966def1816ab6bcb4a782b40a59cc Mon Sep 17 00:00:00 2001 From: H Vs Date: Thu, 15 Aug 2024 10:27:54 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HealthMonitor.WebApi/Worker.cs | 195 +++++++++++---------------------- 1 file changed, 62 insertions(+), 133 deletions(-) diff --git a/HealthMonitor.WebApi/Worker.cs b/HealthMonitor.WebApi/Worker.cs index b4cb2ee..f78dcb7 100644 --- a/HealthMonitor.WebApi/Worker.cs +++ b/HealthMonitor.WebApi/Worker.cs @@ -17,6 +17,7 @@ using HealthMonitor.Service.Sub; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.Extensions.Options; +using NetTaste; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; @@ -185,7 +186,7 @@ namespace HealthMonitor.WebApi } - + #region 注册定时下发 var startTime = DateTime.Now; @@ -213,7 +214,7 @@ namespace HealthMonitor.WebApi nextRunTime += timeUntilNextRun; } - //long ttl = timeUntilNextRun.Milliseconds/1000; + //long ttl = timeUntilNextRun.Milliseconds/1000; long ttl = (long)((timeUntilNextRun.TotalMilliseconds - (endTime - startTime).TotalMilliseconds) / 1000); var data = new { @@ -264,149 +265,77 @@ namespace HealthMonitor.WebApi // health_monitor/schedule_push/cal_fetal_heart_rate/imei/ else if (key.Contains("health_monitor/schedule_push/cal_fetal_heart_rate/imei/")) { - /** - var watchConfig = await _deviceCacheMgr.GetGpsDeviceWatchConfigCacheObjectBySerialNoAsync(imeiDel, "0067"); - var isFetalHeartEnable = watchConfig != null && (int)watchConfig["enabled"]! == 1; - - if (isFetalHeartEnable) - { - // 处理胎心计算业务,计算一般心率获取胎心系数 - var commonPHR = await _serviceTDengine.InitPregnancyCommonHeartRateModeAsync(imeiDel); - if (commonPHR != null) - { - # region 计算上刻钟的胎心数据 - // 告警上限阀值 - var upperAlarmThreshold = (int)watchConfig!["upperAlarmThreshold"]!; - // 告警下限阀值 - var lowerAlarmThreshold = (int)watchConfig["lowerAlarmThreshold"]!; - - //var lastPhr = await _serviceTDengine.GetLastAsync(); - // 计算胎心=孕妇心率*系数 - //var fetalHeartRate = SafeType.SafeInt(lastPhr.PregnancyHeartRate * commonPHR?.StatModeAvgFprCoefficient!); - - // 上15分钟的数据 - // 获取当前时间 - DateTime now = DateTime.Now; - - // 计算当前时间到上一刻钟的分钟数 - int minutesToSubtract = now.Minute % 15; - - // 计算上一刻钟的时间 - DateTime previousQuarter = now.AddMinutes(-minutesToSubtract).AddSeconds(-now.Second).AddMilliseconds(-now.Millisecond); - - var lastQuarterPhr = await _serviceTDengine.GetBySerialNoAsync(imeiDel, 1); - - var filteredPhr = lastQuarterPhr - .Where(i => i.LastUpdate <= previousQuarter && i.LastUpdate >= previousQuarter.AddMinutes(-15)) - .ToList(); - - var lastQuarterPhrValue = filteredPhr.Count == 1 - ? filteredPhr.First().PregnancyHeartRate - : filteredPhr.Average(i => i.PregnancyHeartRate); - - var fetalHeartRate = SafeType.SafeInt(lastQuarterPhrValue * commonPHR?.StatModeAvgFprCoefficient!); - var sampleTime = DateTimeUtil.ConvertToTimeStamp(DateTime.Now).ToString(); - - var isAbnormal = fetalHeartRate > upperAlarmThreshold ? 1 : (fetalHeartRate < lowerAlarmThreshold ? 2 : 0); - - HisGpsFetalHeartRate gpsFetalHeartRate = new() - { - FetalHeartRateId = Guid.NewGuid().ToString("D"), - PersonId = commonPHR!.PersonId, - Serialno = imeiDel, - HeartRate = fetalHeartRate, - SampleTime = sampleTime, - IsAbnormal = isAbnormal, - StatStartTime = filteredPhr.OrderByDescending(i=>i.LastUpdate).First().LastUpdate, - StatEndTime = filteredPhr.OrderByDescending(i => i.LastUpdate).Last().LastUpdate, - CreateTime = DateTime.Now, - Method = 1, - IsDisplay = 1, - DeviceKey = commonPHR!.DeviceKey - }; - // 保存到 数据服务 MySQL 数据库 - await _hisFetalHeartApiClient.AddAsync(gpsFetalHeartRate).ConfigureAwait(false); - - // 推送到api/v1/open/OpenIot/SetFetalHeartRateConfig - await _serviceIotApi.SetFetalHeartRateConfig(imeiDel, fetalHeartRate, sampleTime, isAbnormal); - #endregion - - #region 注册定时计算胎心数据触发器 {interval} 秒后 - //await SetIntervalTriggerAsync(key, imeiDel, 60 * 15); - #endregion - } - } - */ - - var watchConfig = await _deviceCacheMgr.GetGpsDeviceWatchConfigCacheObjectBySerialNoAsync(imeiDel, "0067"); - _logger.LogInformation($"触发平常心率计算胎心,设备配置{JsonConvert.SerializeObject(watchConfig)}"); - var isFetalHeartEnable = watchConfig != null && (int)watchConfig["enabled"]! == 1; - - if (isFetalHeartEnable) + var triggerValue= (JObject)JsonConvert.DeserializeObject(e.Kv.Value.ToStringUtf8())!; + var trigger = triggerValue["trigger"]?.ToString(); + if (!string.IsNullOrEmpty(trigger)) { - // 告警上限阀值 - var upperAlarmThreshold = (int)watchConfig!["upperAlarmThreshold"]!; - // 告警下限阀值 - var lowerAlarmThreshold = (int)watchConfig["lowerAlarmThreshold"]!; - - var commonPHR = await _serviceTDengine.GetLastAsync(imeiDel); - - // 最后一条孕妇心率 - var lastPhr= await _serviceTDengine.GetLastAsync(imeiDel); - var triggerValue= (JObject)JsonConvert.SerializeObject(e.Kv.Value.ToStringUtf8()); - var trigger = triggerValue["trigger"]?.ToString(); - if (!string.IsNullOrEmpty(trigger)) + var triggerHeartRate= JsonConvert.DeserializeObject(trigger); + using (_logger.BeginScope(new Dictionary { ["RequestId"] = triggerHeartRate?.MessageId! })) { - var triggerHeartRate= JsonConvert.DeserializeObject(trigger); - var isNormalHeartRate = triggerHeartRate?.MessageId == lastPhr.MessageId; - // 判断最后一条孕妇心率与解析器的触发心率是否一致 - if (isNormalHeartRate) - { - HisGpsHeartRate heartRate = new() - { - CreateTime = lastPhr.CreateTime, - DeviceKey = lastPhr.DeviceKey, - HeartRate = lastPhr.PregnancyHeartRate, - HeartRateId = lastPhr.PregnancyHeartRateId, - IsDisplay = lastPhr.IsDisplay ? 1 : 0, - MessageId = lastPhr.MessageId, - LastUpdate = lastPhr.LastUpdate, - Method = lastPhr.Method, - PersonId = lastPhr.PersonId, - Serialno = lastPhr.SerialNumber - }; - var intervalFHR = 15; - await CalculateNormalFetalHeartRateAsync(heartRate, upperAlarmThreshold, lowerAlarmThreshold, intervalFHR, commonPHR); - } - // 不一致,触发心率是高频心率的首条 - else + var watchConfig = await _deviceCacheMgr.GetGpsDeviceWatchConfigCacheObjectBySerialNoAsync(imeiDel, "0067"); + _logger.LogInformation($"触发平常心率计算胎心"); + var isFetalHeartEnable = watchConfig != null && (int)watchConfig["enabled"]! == 1; + + if (isFetalHeartEnable) { - var phr = await _serviceTDengine.GetBySerialNoAsync(imeiDel, 7); - var freqFirstPhr= phr.Where(p=>p.MessageId== triggerHeartRate?.MessageId).First(); - // 修改缓存中高频状态触发心率 - var phrFreqstatus = await _deviceCacheMgr.GetPregnancyHeartRateFreqStatusAsync(imeiDel); - if (phrFreqstatus!=null) + // 告警上限阀值 + var upperAlarmThreshold = (int)watchConfig!["upperAlarmThreshold"]!; + // 告警下限阀值 + var lowerAlarmThreshold = (int)watchConfig["lowerAlarmThreshold"]!; + + var commonPHR = await _serviceTDengine.GetLastAsync(imeiDel); + + // 最后一条孕妇心率 + var lastPhr = await _serviceTDengine.GetLastAsync(imeiDel); + var isNormalHeartRate = triggerHeartRate?.MessageId == lastPhr.MessageId; + // 判断最后一条孕妇心率与解析器的触发心率是否一致 + if (isNormalHeartRate) { - await _deviceCacheMgr.SetPregnancyHeartRateFreqStatusAsync(imeiDel, freqFirstPhr); - _logger.LogInformation($"{imeiDel} 设置高频状态,修正触发的孕妇心率记录"); + HisGpsHeartRate heartRate = new() + { + CreateTime = lastPhr.CreateTime, + DeviceKey = lastPhr.DeviceKey, + HeartRate = lastPhr.PregnancyHeartRate, + HeartRateId = lastPhr.PregnancyHeartRateId, + IsDisplay = lastPhr.IsDisplay ? 1 : 0, + MessageId = lastPhr.MessageId, + LastUpdate = lastPhr.LastUpdate, + Method = lastPhr.Method, + PersonId = lastPhr.PersonId, + Serialno = lastPhr.SerialNumber + }; + var intervalFHR = 15; + await CalculateNormalFetalHeartRateAsync(heartRate, upperAlarmThreshold, lowerAlarmThreshold, intervalFHR, commonPHR); } + // 不一致,触发心率是高频心率的首条 else { - _logger.LogWarning($"{imeiDel} 不在高频状态状态,但触发孕妇心率与最后一条孕妇心率不一致"); + var phr = await _serviceTDengine.GetBySerialNoAsync(imeiDel, 7); + var freqFirstPhr = phr.Where(p => p.MessageId == triggerHeartRate?.MessageId).First(); + // 修改缓存中高频状态触发心率 + var phrFreqstatus = await _deviceCacheMgr.GetPregnancyHeartRateFreqStatusAsync(imeiDel); + if (phrFreqstatus != null) + { + await _deviceCacheMgr.SetPregnancyHeartRateFreqStatusAsync(imeiDel, freqFirstPhr); + _logger.LogInformation($"{imeiDel} 设置高频状态,修正触发的孕妇心率记录"); + } + else + { + _logger.LogWarning($"{imeiDel} 不在高频状态状态,但触发孕妇心率与最后一条孕妇心率不一致"); + } } - } - - } - else - { - _logger.LogWarning("trigger is not set"); + else + { + _logger.LogWarning($"{imeiDel} 胎心监测功能没有开启"); + } } - - + } + else + { + _logger.LogWarning($"{imeiDel} trigger is not set"); } } - //health_monitor/schedule_push/cal_fetal_movement/imei/ else if (key.Contains("health_monitor/schedule_push/cal_fetal_movement/imei/")) {