From 6a85b3f9d6bb6ca2c66a6c499612304f687a9050 Mon Sep 17 00:00:00 2001 From: H Vs Date: Tue, 21 Nov 2023 15:35:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=96=E6=B6=88=E4=BD=BF=E7=94=A8=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=8E=A5=E5=8F=A3=EF=BC=8C=E6=94=B9=E7=94=A8=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HealthMonitor.Service/Biz/IotWebApiService.cs | 127 ++++++++++++------ .../Cache/PersonCacheManager.cs | 104 ++++++++++---- .../Resolver/BloodpressResolver.cs | 4 +- 3 files changed, 166 insertions(+), 69 deletions(-) diff --git a/HealthMonitor.Service/Biz/IotWebApiService.cs b/HealthMonitor.Service/Biz/IotWebApiService.cs index 0a8f5ee..842541b 100644 --- a/HealthMonitor.Service/Biz/IotWebApiService.cs +++ b/HealthMonitor.Service/Biz/IotWebApiService.cs @@ -16,6 +16,7 @@ using TelpoDataService.Util; using TelpoDataService.Util.Clients; using TelpoDataService.Util.Models; using TelpoDataService.Util.QueryObjects; +using HealthMonitor.Service.Cache; namespace HealthMonitor.Service.Biz { @@ -23,17 +24,18 @@ namespace HealthMonitor.Service.Biz { private readonly ServiceConfig _configService; private readonly ILogger _logger; - + private readonly PersonCacheManager _personCacheMgr; private readonly HttpHelper _httpHelper = default!; private readonly GpsCardAccessorClient _gpsPersonApiClient; - public IotWebApiService(ILogger logger, HttpHelper httpHelper, GpsCardAccessorClient gpsPersonApiClient, IOptions optConfigService) + public IotWebApiService(ILogger logger, HttpHelper httpHelper, GpsCardAccessorClient gpsPersonApiClient, IOptions optConfigService, PersonCacheManager personCacheMgr) { _configService = optConfigService.Value; _httpHelper=httpHelper; - _logger = logger; + _logger = logger; + _personCacheMgr = personCacheMgr; _gpsPersonApiClient = gpsPersonApiClient; } /// @@ -80,7 +82,7 @@ namespace HealthMonitor.Service.Biz } - + /** 取消 /// /// 更新 gps_person remark和缓存 /// @@ -107,36 +109,63 @@ namespace HealthMonitor.Service.Biz }; var person = await _gpsPersonApiClient.GetFirstAsync(condition, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false); - // 若remark为空,更新person remark字段 - if (string.IsNullOrWhiteSpace(person?.Remarks)) + //// 若remark为空,更新person remark字段 + //if (string.IsNullOrWhiteSpace(person?.Remarks)) + //{ + // var newRemarkData = new + // { + // imei, + // time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), + // commandValue = new + // { + // systolicCalibrationValue = systolicRefValue, //收缩压标定值,值为0 表示不生效 + // diastolicCalibrationValue = diastolicRefValue, //舒张压标定值,值为0表示不生效 + // systolicIncValue = 0, //收缩压显示增量,值为0 表示不生效 + // diastolicIncValue = 0 //舒张压显示增量,值为0 表示不生效 + // } + // }; + // person!.Remarks = $"is_blood_press:{JsonConvert.SerializeObject(newRemarkData)}|"; + // await _gpsPersonApiClient.UpdateAsync(person, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false); + // _logger.LogInformation($"更新Person remarks字段|{person.Remarks}"); + + // // 更新缓存 + // var url = $"{_configService.IotWebApiUrl}Device/UpdatePersonInfoCache?imei={imei}"; + // List> headers = new() + // { + // new KeyValuePair("AuthKey", "key1") + // }; + // var res = await _httpHelper.HttpToGetAsync(url, headers).ConfigureAwait(false); + // _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonRemarksAsync)},响应:{res}"); + // var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken; + // flag = resJToken?["message"]?.ToString().Equals("ok") ?? false; + //} + + var newRemarkData = new { - var newRemarkData = new + imei, + time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), + commandValue = new { - imei, - time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), - commandValue = new - { - systolicCalibrationValue = systolicRefValue, //收缩压标定值,值为0 表示不生效 - diastolicCalibrationValue = diastolicRefValue, //舒张压标定值,值为0表示不生效 - systolicIncValue = 0, //收缩压显示增量,值为0 表示不生效 - diastolicIncValue = 0 //舒张压显示增量,值为0 表示不生效 - } - }; - person!.Remarks = $"is_blood_press:{JsonConvert.SerializeObject(newRemarkData)}|"; - await _gpsPersonApiClient.UpdateAsync(person, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false); - _logger.LogInformation($"更新Person remarks字段|{person.Remarks}"); - - // 更新缓存 - var url = $"{_configService.IotWebApiUrl}Device/UpdatePersonInfoCache?imei={imei}"; - List> headers = new() + systolicCalibrationValue = systolicRefValue, //收缩压标定值,值为0 表示不生效 + diastolicCalibrationValue = diastolicRefValue, //舒张压标定值,值为0表示不生效 + systolicIncValue = 0, //收缩压显示增量,值为0 表示不生效 + diastolicIncValue = 0 //舒张压显示增量,值为0 表示不生效 + } + }; + person!.Remarks = $"is_blood_press:{JsonConvert.SerializeObject(newRemarkData)}|"; + await _gpsPersonApiClient.UpdateAsync(person, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false); + _logger.LogInformation($"更新Person remarks字段|{person.Remarks}"); + + // 更新缓存 + var url = $"{_configService.IotWebApiUrl}Device/UpdatePersonInfoCache?imei={imei}"; + List> headers = new() { new KeyValuePair("AuthKey", "key1") }; - var res = await _httpHelper.HttpToGetAsync(url, headers).ConfigureAwait(false); - _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonRemarksAsync)},响应:{res}"); - var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken; - flag = resJToken?["message"]?.ToString().Equals("ok") ?? false; - } + var res = await _httpHelper.HttpToGetAsync(url, headers).ConfigureAwait(false); + _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonRemarksAsync)},响应:{res}"); + var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken; + flag = resJToken?["message"]?.ToString().Equals("ok") ?? false; } catch (Exception ex) { @@ -144,7 +173,8 @@ namespace HealthMonitor.Service.Biz } return flag; } - + */ + /// /// 初次开通更新 gps_person remark和对应的缓存 /// @@ -193,15 +223,36 @@ namespace HealthMonitor.Service.Biz _logger.LogInformation($"更新Person remarks字段|{person.Remarks}"); // 更新缓存 - var url = $"{_configService.IotWebApiUrl}Device/UpdatePersonInfoCache?imei={imei}"; - List> headers = new() + var personCache = await _personCacheMgr.GetDeviceGpsPersonCacheObjectBySerialNoAsync(new Guid().ToString(), imei).ConfigureAwait(false); + if (personCache != null) { - new KeyValuePair("AuthKey", "key1") - }; - var res = await _httpHelper.HttpToGetAsync(url, headers).ConfigureAwait(false); - _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonRemarksAsync)},响应:{res}"); - var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken; - flag = resJToken?["message"]?.ToString().Equals("ok") ?? false; + //personCache.Person.Remarks = person!.Remarks; + personCache["person"]!["remarks"] = person!.Remarks; + bool cacheFlag= await _personCacheMgr.UpdateDeviceGpsPersonCacheObjectBySerialNoAsync(personCache, imei); + + // flag = true; + if (cacheFlag) + { + flag = true; + _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonRemarksAsync)}成功,{JsonConvert.SerializeObject(personCache)}"); + } + else + { + flag = false; + _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonRemarksAsync)}失败,{JsonConvert.SerializeObject(personCache)}"); + } + } + + + //var url = $"{_configService.IotWebApiUrl}Device/UpdatePersonInfoCache?imei={imei}"; + //List> headers = new() + //{ + // new KeyValuePair("AuthKey", "key1") + //}; + //var res = await _httpHelper.HttpToGetAsync(url, headers).ConfigureAwait(false); + //_logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonRemarksAsync)},响应:{res}"); + //var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken; + //flag = resJToken?["message"]?.ToString().Equals("ok") ?? false; } } catch (Exception ex) diff --git a/HealthMonitor.Service/Cache/PersonCacheManager.cs b/HealthMonitor.Service/Cache/PersonCacheManager.cs index 71fe6b9..e176481 100644 --- a/HealthMonitor.Service/Cache/PersonCacheManager.cs +++ b/HealthMonitor.Service/Cache/PersonCacheManager.cs @@ -3,6 +3,7 @@ using HealthMonitor.Model.Cache; using HealthMonitor.Model.Config; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Net.NetworkInformation; @@ -15,9 +16,9 @@ using TelpoDataService.Util.QueryObjects; namespace HealthMonitor.Service.Cache { - + public class PersonCacheManager - { + { //注意要改用 redis DB7 数据库 ,Prefix=TELPO private const string CACHE_HASH_KEY_GPSDEVICEPERSON = "#GPSDEVICE_PERSON_HASH"; @@ -25,26 +26,26 @@ namespace HealthMonitor.Service.Cache private readonly GpsCardAccessorClient _personApiClient; - private readonly ILogger _logger; + private readonly ILogger _logger; public PersonCacheManager( GpsCardAccessorClient personApiClient, - ILogger logger) - { + ILogger logger) + { _personApiClient = personApiClient; - _logger = logger; - } + _logger = logger; + } //public async Task GetPersonBySerialNoAsync(string messageId, string sn) //{ // if (string.IsNullOrWhiteSpace(sn)) return null; // // 切换redis DB7 数据库和前缀 "TELPO" - + // // 增加容错,防止Redis宕机造成业务中断 // try // { - + // return await csRedisDb7.HGetAsync(CACHE_HASH_KEY_GPSDEVICEPERSON, sn); // } // catch (Exception ex) @@ -76,17 +77,17 @@ namespace HealthMonitor.Service.Cache public async Task GetDeviceGpsPersonCacheBySerialNoAsync(string messageId, string sn) { if (string.IsNullOrWhiteSpace(sn)) return null; - + // 增加容错,防止Redis宕机造成业务中断 try - { + { var person = await RedisHelperDb7.HGetAsync(CACHE_HASH_KEY_GPSDEVICEPERSON, sn).ConfigureAwait(false); return person; } - catch(Exception ex) - { - _logger.LogWarning($"Redis发生异常,将直接读取MySQL数据库,构造新实例:{ex.Message}, {ex.StackTrace}"); + catch (Exception ex) + { + _logger.LogWarning($"Redis发生异常,将直接读取MySQL数据库,构造新实例:{ex.Message}, {ex.StackTrace}"); var param = new GeneralParam { Filters = new List @@ -100,21 +101,66 @@ namespace HealthMonitor.Service.Cache } } }!; - var person = await _personApiClient.GetFirstAsync(param, new RequestHeader { RequestId = messageId }).ConfigureAwait(false); - // 读取数据库,构造新实例 - return new GpsDevicePerson { - Time = DateTime.Now, - Person=new Person { - DeviceId=person.DeviceId, - PersonId=person.PersonId, - Remarks=person.Remarks, - SerialNo=person.Serialno - } - }; - } - - + var person = await _personApiClient.GetFirstAsync(param, new RequestHeader { RequestId = messageId }).ConfigureAwait(false); + // 读取数据库,构造新实例 + return new GpsDevicePerson { + Time = DateTime.Now, + Person = new Person { + DeviceId = person.DeviceId, + PersonId = person.PersonId, + Remarks = person.Remarks, + SerialNo = person.Serialno + } + }; + } + + + } + + /// + /// 获取gps_person缓存 + /// + /// + /// + /// + public async Task GetDeviceGpsPersonCacheObjectBySerialNoAsync(string messageId, string sn) + { + if (string.IsNullOrWhiteSpace(sn)) return null; + + try + { + var person = await RedisHelperDb7.HGetAsync(CACHE_HASH_KEY_GPSDEVICEPERSON, sn).ConfigureAwait(false); + return person; + } + catch (Exception ex) + { + _logger.LogWarning($"Redis发生异常:{ex.Message}, {ex.StackTrace}"); + + } + + return null; + } + /// + /// 更新gps_person缓存 + /// + /// + /// + /// + public async Task UpdateDeviceGpsPersonCacheObjectBySerialNoAsync(JToken person, string sn) + { + var flag = false; + if (string.IsNullOrWhiteSpace(sn)) return flag; + try + { + return await RedisHelperDb7.HSetAsync(CACHE_HASH_KEY_GPSDEVICEPERSON, sn, person).ConfigureAwait(false); + + } + catch (Exception ex) + { + _logger.LogWarning($"Redis发生异常:{ex.Message}, {ex.StackTrace}"); + } + return flag; + } - } } diff --git a/HealthMonitor.Service/Resolver/BloodpressResolver.cs b/HealthMonitor.Service/Resolver/BloodpressResolver.cs index fa6fe30..7f6481f 100644 --- a/HealthMonitor.Service/Resolver/BloodpressResolver.cs +++ b/HealthMonitor.Service/Resolver/BloodpressResolver.cs @@ -292,8 +292,8 @@ namespace HealthMonitor.Service.Resolver #endregion - #region 初始化 gps_persoon remarks - // 初始化 remark + #region 更新 gps_persoon remarks + // 更新 remarkFlag = await _serviceIotWebApi.UpdatePersonRemarksAsync(bp.Serialno, (int)systolicRefValue!, (int)diastolicRefValue!, systolicInc, diastolicInc).ConfigureAwait(false); if (remarkFlag) {