|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- using CSRedis;
- using HealthMonitor.Model.Cache;
- using HealthMonitor.Model.Config;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.Net.NetworkInformation;
- using System.Threading.Tasks;
- using TelpoDataService.Util;
- using TelpoDataService.Util.Clients;
- using TelpoDataService.Util.Entities.GpsCard;
- using TelpoDataService.Util.Entities.GpsLocationHistory;
- using TelpoDataService.Util.Models;
- 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";
-
- #region db10
- // 周期性体温
- private const string CACHE_HASH_KEY_TEMP_PERIODICITY = "#HM_PERIODICITY_TEMP_HASH";
-
- // 周期性步数
- private const string CACHE_HASH_KEY_STEP_PERIODICITY = "#HM_PERIODICITY_STEP_HASH";
-
- // 周期性血压
- private const string CACHE_HASH_KEY_BLOODPRESS_PERIODICITY = "#HM_PERIODICITY_BLOODPRESS_HASH";
- #endregion
-
- private readonly GpsCardAccessorClient<GpsPerson> _personApiClient;
- private readonly ILogger<PersonCacheManager> _logger;
-
-
- public PersonCacheManager(
- GpsCardAccessorClient<GpsPerson> personApiClient,
- ILogger<PersonCacheManager> logger)
- {
- _personApiClient = personApiClient;
- _logger = logger;
- }
- //public async Task<GpsPerson?> GetPersonBySerialNoAsync(string messageId, string sn)
- //{
- // if (string.IsNullOrWhiteSpace(sn)) return null;
- // // 切换redis DB7 数据库和前缀 "TELPO"
-
-
- // // 增加容错,防止Redis宕机造成业务中断
- // try
- // {
-
- // return await csRedisDb7.HGetAsync<GpsPerson>(CACHE_HASH_KEY_GPSDEVICEPERSON, sn);
- // }
- // catch (Exception ex)
- // {
- // _logger.LogWarning($"Redis发生异常,将直接读取MySQL数据库,构造新实例:{ex.Message}, {ex.StackTrace}");
- // var param = new GeneralParam
- // {
- // Filters = new List<QueryFilterCondition>
- // {
- // new QueryFilterCondition
- // {
- // Key=nameof(GpsPerson.Serialno),
- // Value=sn,
- // ValueType=QueryValueTypeEnum.String,
- // Operator=QueryOperatorEnum.Equal
- // }
- // }
- // }!;
- // return await _personApiClient.GetFirstAsync(param, new RequestHeader { RequestId = messageId }).ConfigureAwait(false);
- // }
- //}
-
- /// <summary>
- /// 读取个人信息(血压使用)注意要改用 redis DB7
- /// </summary>
- /// <param name="messageId"></param>
- /// <param name="sn"></param>
- /// <returns></returns>
- public async Task<GpsDevicePerson?> GetDeviceGpsPersonCacheBySerialNoAsync(string messageId, string sn)
- {
- if (string.IsNullOrWhiteSpace(sn)) return null;
-
-
- // 增加容错,防止Redis宕机造成业务中断
- try
- {
- var person = await RedisHelperDb7.HGetAsync<GpsDevicePerson>(CACHE_HASH_KEY_GPSDEVICEPERSON, sn).ConfigureAwait(false);
- return person;
- }
- catch (Exception ex)
- {
- _logger.LogWarning($"Redis发生异常,将直接读取MySQL数据库,构造新实例:{ex.Message}, {ex.StackTrace}");
- var param = new GeneralParam
- {
- Filters = new List<QueryFilterCondition>
- {
- new QueryFilterCondition
- {
- Key=nameof(GpsPerson.Serialno),
- Value=sn,
- ValueType=QueryValueTypeEnum.String,
- Operator=QueryOperatorEnum.Equal
- }
- }
- }!;
- 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
- }
- };
- }
-
-
- }
-
- /// <summary>
- /// 获取gps_person缓存
- /// </summary>
- /// <param name="messageId"></param>
- /// <param name="sn"></param>
- /// <returns></returns>
- public async Task<JObject?> 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 (JObject?)JsonConvert.DeserializeObject(person);
- }
- catch (Exception ex)
- {
- _logger.LogWarning($"Redis发生异常:{ex.Message}, {ex.StackTrace}");
-
- }
-
- return null;
- }
- /// <summary>
- /// 更新gps_person缓存
- /// </summary>
- /// <param name="person"></param>
- /// <param name="sn"></param>
- /// <returns></returns>
- public async Task<bool> UpdateDeviceGpsPersonCacheObjectBySerialNoAsync(JObject person, string sn)
- {
- var flag = false;
- if (string.IsNullOrWhiteSpace(sn)) return flag;
- try
- {
- // 覆盖 返回false
- await RedisHelperDb7.HSetAsync(CACHE_HASH_KEY_GPSDEVICEPERSON, sn, person).ConfigureAwait(false);
- flag = true;
-
- }
- catch (Exception ex)
- {
- _logger.LogWarning($"Redis发生异常:{ex.Message}, {ex.StackTrace}");
- }
- return flag;
-
- }
-
-
- #region 生理健康(步数、周期性体温、周期性血压) Db10
- // 周期性体温
- public async Task<HisGpsTemperature?> GetTemperaturePeriodicityAsync(string sn)
- {
- try
- {
- var res=await RedisHelperDb10.HGetAsync(CACHE_HASH_KEY_TEMP_PERIODICITY, sn);
- if (!string.IsNullOrEmpty(res))
- {
- return JsonConvert.DeserializeObject<HisGpsTemperature>(res)!;
- }
-
- }
- catch (Exception ex)
- {
-
- _logger.LogError($"Redis发生异常:{ex.Message}, {ex.StackTrace}");
- }
- return null;
-
- }
-
- // 周期性步数
- public async Task<HisGpsStep?> GetStepPeriodicityAsync(string sn )
- {
- try
- {
- var res = await RedisHelperDb10.HGetAsync(CACHE_HASH_KEY_STEP_PERIODICITY, sn);
- if (!string.IsNullOrEmpty(res))
- {
- return JsonConvert.DeserializeObject<HisGpsStep>(res)!;
- }
- }
- catch (Exception ex)
- {
-
- _logger.LogError($"Redis发生异常:{ex.Message}, {ex.StackTrace}");
- }
- return null;
- }
-
- // 周期性血压
- public async Task<HisGpsBloodPress?> GetBloodPressPeriodicityAsync(string sn)
- {
- try
- {
- var res = await RedisHelperDb10.HGetAsync(CACHE_HASH_KEY_BLOODPRESS_PERIODICITY, sn);
- if (!string.IsNullOrEmpty(res))
- {
- return JsonConvert.DeserializeObject<HisGpsBloodPress>(res)!;
- }
- }
- catch (Exception ex)
- {
-
- _logger.LogError($"Redis发生异常:{ex.Message}, {ex.StackTrace}");
- }
- return null;
- }
-
- #endregion
-
- }
- }
|