Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

535 rindas
28KB

  1. using HealthMonitor.Common;
  2. using HealthMonitor.Common.helper;
  3. using HealthMonitor.Service.Biz.db;
  4. using HealthMonitor.Service.Cache;
  5. using HealthMonitor.Service.Etcd;
  6. using HealthMonitor.Service.Resolver.Interface;
  7. using HealthMonitor.Service.Sub;
  8. using HealthMonitor.Service.Sub.Topic.Model;
  9. using Microsoft.EntityFrameworkCore.Metadata;
  10. using Microsoft.Extensions.Logging;
  11. using Newtonsoft.Json;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Data.Common;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Text.Json.Serialization;
  18. using System.Threading.Tasks;
  19. using TDengineTMQ;
  20. using TelpoDataService.Util.Entities.GpsCard;
  21. using TelpoDataService.Util;
  22. using TelpoDataService.Util.Entities.GpsLocationHistory;
  23. using HealthMonitor.Service.Biz;
  24. using HealthMonitor.Model.Service;
  25. using Microsoft.Extensions.Options;
  26. using HealthMonitor.Model.Config;
  27. using HealthMonitor.Model.Service.Mapper;
  28. using Mvccpb;
  29. namespace HealthMonitor.Service.Resolver
  30. {
  31. public class BloodpressResolver: IResolver
  32. {
  33. private readonly ILogger<BloodpressResolver> _logger;
  34. private readonly BoodPressResolverConfig _configBoodPressResolver;
  35. private readonly PersonCacheManager _personCacheMgr;
  36. private readonly TDengineService _serviceTDengine;
  37. private readonly BloodPressReferenceValueCacheManager _bpRefValCacheManager;
  38. private readonly HttpHelper _httpHelper = default!;
  39. private readonly GpsCardAccessorClient<GpsPerson> _gpsPersonApiClient;
  40. private readonly IotWebApiService _serviceIotWebApi;
  41. private readonly AsyncLocal<string> _messageId = new();
  42. private readonly AsyncLocal<HisGpsBloodPress> _msgData = new();
  43. private readonly EtcdService _serviceEtcd;
  44. public BloodpressResolver(
  45. TDengineService serviceDengine,
  46. BloodPressReferenceValueCacheManager bpRefValCacheManager,
  47. PersonCacheManager personCacheMgr, HttpHelper httpHelper,
  48. GpsCardAccessorClient<GpsPerson> gpsPersonApiClient,
  49. IotWebApiService iotWebApiService,
  50. EtcdService serviceEtcd,
  51. IOptions<BoodPressResolverConfig> optionBoodPressResolver,
  52. ILogger<BloodpressResolver> logger)
  53. {
  54. _httpHelper = httpHelper;
  55. _serviceTDengine = serviceDengine;
  56. _bpRefValCacheManager = bpRefValCacheManager;
  57. _gpsPersonApiClient = gpsPersonApiClient;
  58. _serviceIotWebApi = iotWebApiService;
  59. _logger = logger;
  60. _personCacheMgr = personCacheMgr;
  61. _serviceEtcd = serviceEtcd;
  62. _configBoodPressResolver= optionBoodPressResolver.Value;
  63. }
  64. public void SetResolveInfo(PackageMsgModel msg)
  65. {
  66. var topicHmBloodPress = JsonConvert.DeserializeObject<TopicHmBloodPress>(msg.DetailData.ToString()!);
  67. _messageId.Value = msg.MessageId;
  68. _msgData.Value = new HisGpsBloodPress()
  69. {
  70. BloodPressId = topicHmBloodPress!.BloodPressId,
  71. MessageId = topicHmBloodPress!.MessageId,
  72. Serialno= topicHmBloodPress!.Serialno,
  73. SystolicValue = topicHmBloodPress!.SystolicValue,
  74. DiastolicValue= topicHmBloodPress!.DiastolicValue,
  75. LastUpdate= DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(SafeType.SafeInt64(topicHmBloodPress.LastUpdate) / 1000000),
  76. CreateTime= DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(SafeType.SafeInt64(topicHmBloodPress.CreateTime) / 1000000),
  77. Method= topicHmBloodPress!.Method,
  78. IsDisplay=topicHmBloodPress!.IsDisplay ? 1 : 0
  79. };
  80. }
  81. public override string ToString()
  82. {
  83. return $"{nameof(BloodpressResolver)}[{_messageId.Value}]";
  84. }
  85. public async Task ExecuteMessageAsync()
  86. {
  87. try
  88. {
  89. var messageId = _messageId.Value;
  90. var bp = _msgData.Value!;
  91. int systolicRefValue;
  92. int diastolicRefValue;
  93. int systolicInc;
  94. int diastolicInc;
  95. decimal systolicAvg;
  96. decimal diastolicAvg;
  97. int systolicMax = 0;
  98. int diastolicMax = 0;
  99. // 最小值
  100. int systolicMin = 0;
  101. int diastolicMin = 0;
  102. // 偏移参数
  103. var avgOffset = 0.25M;
  104. var systolicAvgOffset = avgOffset;
  105. var diastolicAvgOffset = avgOffset;
  106. // 统计时间
  107. DateTime endTime = DateTime.Now; //测试
  108. DateTime startTime = DateTime.Now;
  109. // 最后一次下发值
  110. //int lastPushSystolicInc = 0;
  111. //int lastPushDiastolicInc = 0;
  112. bool remarkFlag = false;
  113. //long duration = 7 * 24 * 3600 * 1000;
  114. string sql = string.Empty;
  115. #region 获取个人信息
  116. var person = await _personCacheMgr.GetDeviceGpsPersonCacheBySerialNoAsync(bp.MessageId, bp.Serialno).ConfigureAwait(false);
  117. //验证这个信息是否存在
  118. if (person == null || person?.Person.BornDate == null)
  119. {
  120. _logger.LogWarning($"{bp.Serialno}--{bp.MessageId} 验证个人信息,找不到个人信息,跳过此消息");
  121. return;
  122. }
  123. // 验证年龄是否在范围 (2 - 120)
  124. var age = SafeType.SafeInt(DateTime.Today.Year - person?.Person.BornDate!.Value.Year!);
  125. if (age < 2 || age > 120)
  126. {
  127. _logger.LogWarning($"{bp.Serialno}--{bp.MessageId} 验证年龄,不在范围 (2 - 120)岁,跳过此消息");
  128. return;
  129. }
  130. var gender = person?.Person.Gender == true ? 1 : 2;
  131. var isHypertension = SafeType.SafeBool(person?.Person.Ishypertension!);
  132. var height = SafeType.SafeDouble(person?.Person.Height!);
  133. var weight = SafeType.SafeDouble(person?.Person.Weight!);
  134. #endregion
  135. #region 初始化常规血压标定值标定值
  136. var bpRef = await _bpRefValCacheManager.GetBloodPressReferenceValueAsync(age, gender, isHypertension);
  137. systolicRefValue = bpRef!.Systolic;//?
  138. diastolicRefValue = bpRef!.Diastolic;//?
  139. #endregion
  140. _logger.LogInformation($"{bp.Serialno} -- Person 值:{JsonConvert.SerializeObject(person)}");
  141. _logger.LogInformation($"{bp.Serialno} -- Person Remarks 值:{person?.Person.Remarks}");
  142. if (string.IsNullOrWhiteSpace(person?.Person.Remarks))
  143. {
  144. _logger.LogInformation($"{bp.Serialno},设备解绑后绑定,首次手工测量了血压值,下发年龄标定值和增量值(测量值=平均值计算得出的)");
  145. #region 初始化计算增量值(个人血压信息)
  146. // 测量值当作平均值
  147. systolicAvg = bp.SystolicValue;
  148. diastolicAvg = bp.DiastolicValue;
  149. systolicInc = (int)((systolicRefValue - systolicAvg) * systolicAvgOffset)!;
  150. diastolicInc = (int)((diastolicRefValue - diastolicAvg) * diastolicAvgOffset)!;
  151. #region 更新 gps_persoon remarks 下发增量值到iot
  152. // 更新
  153. remarkFlag = await _serviceIotWebApi.UpdatePersonRemarksAsync(bp.Serialno, (int)systolicRefValue!, (int)diastolicRefValue!, systolicInc, diastolicInc).ConfigureAwait(false);
  154. if (remarkFlag)
  155. {
  156. _logger.LogInformation($"{nameof(BloodpressResolver)} 开启血压标定值下发: {_configBoodPressResolver.EnableBPRefPush}");
  157. // 启血压标定值下发开关
  158. if (_configBoodPressResolver.EnableBPRefPush)
  159. {
  160. // 下推
  161. BloodPressCalibrationConfigModel bpIncData = new()
  162. {
  163. Imei = bp.Serialno,
  164. SystolicRefValue = (int)systolicRefValue!, //收缩压标定值,值为0 表示不生效
  165. DiastolicRefValue = (int)diastolicRefValue!, //舒张压标定值,值为0表示不生效
  166. SystolicIncValue = systolicInc, //收缩压显示增量,值为0 表示不生效
  167. DiastolicIncValue = diastolicInc //舒张压显示增量,值为0 表示不生效
  168. };
  169. // 下发 IOT 增量值
  170. //var flagIot = await _serviceIotWebApi.SetBloodPressCalibrationConfigAsync(bpIncData).ConfigureAwait(false);
  171. var response = await _serviceIotWebApi.SetBloodPressCalibrationConfig2Async(bpIncData).ConfigureAwait(false);
  172. var flagIot = response.Flag;
  173. if (flagIot)
  174. {
  175. startTime = (DateTime)bp.LastUpdate!;
  176. endTime = DateTime.Now;
  177. #region 保存下推记录 stb_hm_bp_push_ref_inc_value
  178. sql = $"INSERT INTO health_monitor.hm_bp_push_ref_inc_value_{bp.Serialno.Substring(bp.Serialno.Length - 2)} " +
  179. $"USING health_monitor.stb_hm_bp_push_ref_inc_value " +
  180. $"TAGS ('{bp.Serialno.Substring(bp.Serialno.Length - 2)}') " +
  181. $"VALUES(" +
  182. $"'{endTime:yyyy-MM-dd HH:mm:ss.fff}'," +
  183. $"'{bp.Serialno}'," +
  184. $"{systolicRefValue}," +
  185. $"{diastolicRefValue}," +
  186. $"{systolicInc}," +
  187. $"{diastolicInc}," +
  188. $"{true}," +
  189. $"{systolicAvg}," +
  190. $"{diastolicAvg}," +
  191. $"{systolicAvgOffset}," +
  192. $"{diastolicAvgOffset}," +
  193. $"'{startTime:yyyy-MM-dd HH:mm:ss.fff}'," +
  194. $"'{endTime:yyyy-MM-dd HH:mm:ss.fff}'" +
  195. $")";
  196. _serviceTDengine.ExecuteInsertSQL(sql);
  197. #endregion
  198. }
  199. }
  200. }
  201. #endregion
  202. #endregion
  203. }
  204. else if (person.Person.Remarks.Contains("lastPushRefValue"))
  205. {
  206. _logger.LogInformation($"{bp.Serialno},有新标定值(lastPushRefValue),使用最近一次下推的标定值和增量值(测量值=平均值计算得出的)");
  207. var lastPushResponse = await _serviceTDengine.ExecuteSelectRestResponseAsync("stb_hm_bp_push_ref_inc_value", $"serialno='{bp.Serialno}' order by ts desc", "last_row(*)");
  208. var lastPushParser = JsonConvert.DeserializeObject<ParseTDengineRestResponse<BloodPressurePushRefIncModel>>(lastPushResponse!);
  209. var lastPush = lastPushParser!.Select().FirstOrDefault();
  210. var lastPushSystolicRefValue = lastPush!.SystolicRefValue;
  211. var lastPushDiastolicRefValue = lastPush!.DiastolicRefValue;
  212. //使用最后一次设备下发校准值作为新的标定值
  213. systolicRefValue = lastPushSystolicRefValue;
  214. diastolicRefValue = lastPushDiastolicRefValue;
  215. // 测量值当作平均值
  216. systolicAvg = bp.SystolicValue;
  217. diastolicAvg = bp.DiastolicValue;
  218. systolicInc = (int)((systolicRefValue - systolicAvg) * systolicAvgOffset)!;
  219. diastolicInc = (int)((diastolicRefValue - diastolicAvg) * diastolicAvgOffset)!;
  220. #region 更新 gps_persoon remarks 下发增量值到iot
  221. // 更新
  222. remarkFlag = await _serviceIotWebApi.UpdatePersonRemarksAsync(bp.Serialno, (int)systolicRefValue!, (int)diastolicRefValue!, systolicInc, diastolicInc).ConfigureAwait(false);
  223. if (remarkFlag)
  224. {
  225. _logger.LogInformation($"{nameof(BloodpressResolver)} 开启血压标定值下发: {_configBoodPressResolver.EnableBPRefPush}");
  226. // 启血压标定值下发开关
  227. if (_configBoodPressResolver.EnableBPRefPush)
  228. {
  229. // 下推
  230. BloodPressCalibrationConfigModel bpIncData = new()
  231. {
  232. Imei = bp.Serialno,
  233. SystolicRefValue = (int)systolicRefValue!, //收缩压标定值,值为0 表示不生效
  234. DiastolicRefValue = (int)diastolicRefValue!, //舒张压标定值,值为0表示不生效
  235. SystolicIncValue = systolicInc, //收缩压显示增量,值为0 表示不生效
  236. DiastolicIncValue = diastolicInc //舒张压显示增量,值为0 表示不生效
  237. };
  238. // 下发 IOT 增量值
  239. //var flagIot = await _serviceIotWebApi.SetBloodPressCalibrationConfigAsync(bpIncData).ConfigureAwait(false);
  240. var response = await _serviceIotWebApi.SetBloodPressCalibrationConfig2Async(bpIncData).ConfigureAwait(false);
  241. var flagIot = response.Flag;
  242. if (flagIot)
  243. {
  244. startTime = (DateTime)bp.LastUpdate!;
  245. endTime = DateTime.Now;
  246. #region 保存下推记录 stb_hm_bp_push_ref_inc_value
  247. sql = $"INSERT INTO health_monitor.hm_bp_push_ref_inc_value_{bp.Serialno.Substring(bp.Serialno.Length - 2)} " +
  248. $"USING health_monitor.stb_hm_bp_push_ref_inc_value " +
  249. $"TAGS ('{bp.Serialno.Substring(bp.Serialno.Length - 2)}') " +
  250. $"VALUES(" +
  251. $"'{endTime:yyyy-MM-dd HH:mm:ss.fff}'," +
  252. $"'{bp.Serialno}'," +
  253. $"{systolicRefValue}," +
  254. $"{diastolicRefValue}," +
  255. $"{systolicInc}," +
  256. $"{diastolicInc}," +
  257. $"{true}," +
  258. $"{systolicAvg}," +
  259. $"{diastolicAvg}," +
  260. $"{systolicAvgOffset}," +
  261. $"{diastolicAvgOffset}," +
  262. $"'{startTime:yyyy-MM-dd HH:mm:ss.fff}'," +
  263. $"'{endTime:yyyy-MM-dd HH:mm:ss.fff}'" +
  264. $")";
  265. _serviceTDengine.ExecuteInsertSQL(sql);
  266. #endregion
  267. }
  268. }
  269. }
  270. #endregion
  271. }
  272. else
  273. {
  274. #region (暂时取消)正常计算增量值
  275. /**
  276. // var lastPush = await _serviceTDengine.GetLastAsync("stb_hm_bp_push_ref_inc_value", $"serialno='{bp.Serialno}' order by ts desc");
  277. var lastPushResponse = await _serviceTDengine.ExecuteSelectRestResponseAsync("stb_hm_bp_push_ref_inc_value", $"serialno='{bp.Serialno}' order by ts desc", "last_row(*)");
  278. if (lastPushResponse == null)
  279. {
  280. return;
  281. }
  282. var lastPushParser = JsonConvert.DeserializeObject<ParseTDengineRestResponse<BloodPressurePushRefIncModel>>(lastPushResponse);
  283. var lastPush = lastPushParser!.Select().FirstOrDefault();
  284. //var ts = last?[0];
  285. // 曾经有下发记录
  286. if (lastPushParser?.Rows != 0)
  287. {
  288. // 重置设备,取正常值标定值
  289. if (
  290. lastPush!.SystolicRefValue == 0
  291. && lastPush!.DiastolicRefValue == 0
  292. && lastPush!.SystolicIncValue == 0
  293. && lastPush!.DiastolicIncValue == 0
  294. )
  295. {
  296. systolicRefValue = bpRef!.Systolic;//?
  297. diastolicRefValue = bpRef!.Diastolic;//?
  298. }
  299. // 取最后一条下推的标定值
  300. else
  301. {
  302. systolicRefValue = lastPush!.SystolicRefValue;
  303. diastolicRefValue = lastPush!.DiastolicRefValue;
  304. }
  305. duration = SafeType.SafeInt64(((DateTime)bp.LastUpdate! - lastPush!.Timestamp).TotalMilliseconds);
  306. lastPushSystolicInc = lastPush!.SystolicIncValue;
  307. lastPushDiastolicInc = lastPush!.DiastolicIncValue;
  308. }
  309. TimeSpan ts = TimeSpan.FromMilliseconds(duration);
  310. // 获取历史数据
  311. ////DateTime now = DateTime.Now;
  312. //DateTime now = (DateTime)bp.LastUpdate!; //测试
  313. //DateTime startTime = now.AddDays(-duration);
  314. //DateTime endTime = now;
  315. endTime = (DateTime)bp.LastUpdate!; //测试
  316. startTime = endTime - ts;
  317. var condition = $"ts between '{startTime:yyyy-MM-dd HH:mm:ss.fff}' and '{endTime:yyyy-MM-dd HH:mm:ss.fff}' and serialno='{bp.Serialno}'";
  318. var hmBpResponse = await _serviceTDengine.ExecuteSelectRestResponseAsync("stb_hm_bloodpress", condition);
  319. var hmBpParser = JsonConvert.DeserializeObject<ParseTDengineRestResponse<BloodPressureModel>>(hmBpResponse!);
  320. var hmBp = hmBpParser?.Select();
  321. if (hmBp?.ToList().Count < 2)
  322. {
  323. _logger.LogInformation($"{bp.Serialno} 数据值不足");
  324. return;
  325. }
  326. // 最大值
  327. systolicMax = (int)hmBpParser?.Select(i => i.SystolicValue).Max()!;
  328. diastolicMax = (int)hmBpParser?.Select(i => i.DiastolicValue).Max()!;
  329. // 最小值
  330. systolicMin = (int)hmBpParser?.Select(i => i.SystolicValue).Min()!;
  331. diastolicMin = (int)hmBpParser?.Select(i => i.DiastolicValue).Min()!;
  332. // 计算去除最大值和最小值和异常值的平均值
  333. //var systolicAvg = await _serviceTDengine.GetAvgExceptMaxMinValueAsync("systolic_value", "stb_hm_bloodpress", $"ts>='{startTime:yyyy-MM-dd HH:mm:ss.fff}' and ts <='{endTime:yyyy-MM-dd HH:mm:ss.fff}' and serialno='{bp.Serialno}' and systolic_value < {systolicRefValue} ");
  334. //var diastolicAvg = await _serviceTDengine.GetAvgExceptMaxMinValueAsync("diastolic_value", "stb_hm_bloodpress", $"ts>='{startTime:yyyy-MM-dd HH:mm:ss.fff}' and ts <='{endTime:yyyy-MM-dd HH:mm:ss.fff}' and serialno='{bp.Serialno}' and diastolic_value < {diastolicRefValue}");
  335. systolicAvg = (int)(hmBpParser?.AverageAfterRemovingOneMinMaxRef(i => i.SystolicValue, SafeType.SafeInt(systolicRefValue!)))!;
  336. diastolicAvg = (int)(hmBpParser?.AverageAfterRemovingOneMinMaxRef(i => i.DiastolicValue, SafeType.SafeInt(diastolicRefValue!)))!;
  337. if (systolicAvg.Equals(0) || diastolicAvg.Equals(0))
  338. {
  339. _logger.LogWarning($"{bp.Serialno} 历史数据{startTime}---{endTime}除最大值和最小值和异常值的平均值为0,使用测试量当做平均值");
  340. systolicAvg = bp.SystolicValue;
  341. diastolicAvg = bp.DiastolicValue;
  342. }
  343. //var systolicAvg = _serviceTDengine.GetAvgExceptMaxMinValue("systolic_value", "hm_bloodpress", $"ts>='{startTime:yyyy-MM-dd HH:mm:ss.fff}' and ts <='{endTime:yyyy-MM-dd HH:mm:ss.fff}' and serialno='{bp.Serialno}' and systolic_value < {systolicRefValue} ");
  344. //var diastolicAvg = _serviceTDengine.GetAvgExceptMaxMinValue("diastolic_value", "hm_bloodpress", $"ts>='{startTime:yyyy-MM-dd HH:mm:ss.fff}' and ts <='{endTime:yyyy-MM-dd HH:mm:ss.fff}' and serialno='{bp.Serialno}' and diastolic_value < {diastolicRefValue}");
  345. // 增量值=(标定值-平均值)* 0.25
  346. var currentSystolicInc = (int)((systolicRefValue - systolicAvg) * systolicAvgOffset)!;
  347. var currentDiastolicInc = (int)((diastolicRefValue - diastolicAvg) * diastolicAvgOffset)!;
  348. // 累计增量
  349. systolicInc = currentSystolicInc + lastPushSystolicInc;
  350. diastolicInc = currentDiastolicInc + lastPushDiastolicInc;
  351. */
  352. #endregion
  353. }
  354. #region (暂时取消)插入BP增量值 hm_bloodpress_stats_inc
  355. // 自动建表
  356. /** sql = $"INSERT INTO health_monitor.hm_bp_stats_inc_{bp.Serialno.Substring(bp.Serialno.Length - 2)} " +
  357. $"USING health_monitor.stb_hm_bloodpress_stats_inc " +
  358. $"TAGS ('{bp.Serialno.Substring(bp.Serialno.Length - 2)}') " +
  359. $"VALUES(" +
  360. $"'{bp.LastUpdate:yyyy-MM-dd HH:mm:ss.fff}'," +
  361. $"'{bp.BloodPressId}'," +
  362. $"'{bp.MessageId}'," +
  363. $"'{bp.Serialno}'," +
  364. $"{bp.SystolicValue}," +
  365. $"{systolicRefValue}," +
  366. $"{systolicAvg}," +
  367. $"{systolicMax}," +
  368. $"{systolicMin}," +
  369. $"{systolicAvgOffset}," +
  370. $"{systolicInc}," +
  371. $"{bp.DiastolicValue}," +
  372. $"{diastolicRefValue}," +
  373. $"{diastolicAvg}," +
  374. $"{diastolicMax}," +
  375. $"{diastolicMin}," +
  376. $"{diastolicAvgOffset}," +
  377. $"{diastolicInc}," +
  378. $"{gender}," +
  379. $"{age}," +
  380. $"{height}," +
  381. $"{weight}," +
  382. $"'{bp.LastUpdate:yyyy-MM-dd HH:mm:ss.fff}'," +
  383. $"{duration}," +
  384. $"'{startTime:yyyy-MM-dd HH:mm:ss.fff}'," +
  385. $"'{endTime:yyyy-MM-dd HH:mm:ss.fff}'," +
  386. $"'{string.Empty}'," +
  387. $"{isHypertension})";
  388. _serviceTDengine.ExecuteInsertSQL(sql);
  389. */
  390. // 发送到 设置设备血压标定参数
  391. #endregion
  392. #region 定时下发触发器
  393. var key = $"health_moniter/schedule_push/imei/{bp.Serialno}";
  394. var schedule_push = await _serviceEtcd.GetValAsync(key).ConfigureAwait(false);
  395. if (string.IsNullOrWhiteSpace(schedule_push))
  396. {
  397. // 注册首次下推
  398. #if DEBUG
  399. // await _serviceEtcd.PutValAsync(key, result, 60*1, false).ConfigureAwait(false);
  400. var interval = 0;
  401. // 获取当前时间
  402. DateTime now = DateTime.Now;
  403. // 计算距离下一个$interval天后的8点的时间间隔
  404. DateTime nextRunTime = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute + 1, 58).AddDays(interval);
  405. TimeSpan timeUntilNextRun = nextRunTime - now;
  406. // 如果当前时间已经超过了8点,将等待到明天后的8点
  407. if (timeUntilNextRun < TimeSpan.Zero)
  408. {
  409. timeUntilNextRun = timeUntilNextRun.Add(TimeSpan.FromMinutes(1));
  410. nextRunTime += timeUntilNextRun;
  411. }
  412. var ttl = (long)timeUntilNextRun.TotalSeconds;
  413. var data = new
  414. {
  415. imei = bp.Serialno,
  416. create_time = now.ToString("yyyy-MM-dd HH:mm:ss"),
  417. ttl,
  418. next_run_time = nextRunTime.ToString("yyyy-MM-dd HH:mm:ss")
  419. };
  420. var result = JsonConvert.SerializeObject(data);
  421. await _serviceEtcd.PutValAsync(key, result, ttl, false).ConfigureAwait(false);
  422. #else
  423. //DateTime sNow = DateTime.Now;
  424. //// 计算距离19:59:55点的时间间隔
  425. //TimeSpan timeUntil = new DateTime(sNow.Year, sNow.Month, sNow.Day, 19, 59, 55) - sNow;
  426. //// 如果当前时间已经超过了12点,将等待到明天
  427. //if (timeUntil < TimeSpan.Zero)
  428. //{
  429. // timeUntil = timeUntil.Add(TimeSpan.FromHours(24));
  430. //}
  431. //var ttl = (long)timeUntil.TotalSeconds;
  432. var interval = 0;
  433. // 获取当前时间
  434. DateTime now = DateTime.Now;
  435. // 计算距离下一个$interval天后的8点的时间间隔
  436. DateTime nextRunTime = new DateTime(now.Year, now.Month, now.Day, 19, 59, 58).AddDays(interval);
  437. TimeSpan timeUntilNextRun = nextRunTime - now;
  438. // 如果当前时间已经超过了8点,将等待到明天后的8点
  439. if (timeUntilNextRun < TimeSpan.Zero)
  440. {
  441. timeUntilNextRun = timeUntilNextRun.Add(TimeSpan.FromDays(1));
  442. // nextRunTime += timeUntilNextRun;
  443. nextRunTime += TimeSpan.FromDays(1);
  444. }
  445. var ttl =(long)timeUntilNextRun.TotalSeconds;
  446. var data = new
  447. {
  448. imei = bp.Serialno,
  449. create_time = now.ToString("yyyy-MM-dd HH:mm:ss"),
  450. ttl,
  451. next_run_time = nextRunTime.ToString("yyyy-MM-dd HH:mm:ss")
  452. };
  453. var result = JsonConvert.SerializeObject(data);
  454. await _serviceEtcd.PutValAsync(key, result,ttl, false).ConfigureAwait(false);
  455. #endif
  456. }
  457. #endregion
  458. }
  459. catch (Exception ex)
  460. {
  461. _logger.LogError($"解析血压出错, {ex.Message}\n{ex.StackTrace}");
  462. }
  463. }
  464. }
  465. }