Ver código fonte

取消使用缓存接口,改用代码实现

datasub12_previous
H Vs 11 meses atrás
pai
commit
6a85b3f9d6
3 arquivos alterados com 166 adições e 69 exclusões
  1. +89
    -38
      HealthMonitor.Service/Biz/IotWebApiService.cs
  2. +75
    -29
      HealthMonitor.Service/Cache/PersonCacheManager.cs
  3. +2
    -2
      HealthMonitor.Service/Resolver/BloodpressResolver.cs

+ 89
- 38
HealthMonitor.Service/Biz/IotWebApiService.cs Ver arquivo

@@ -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<IotWebApiService> _logger;
private readonly PersonCacheManager _personCacheMgr;

private readonly HttpHelper _httpHelper = default!;

private readonly GpsCardAccessorClient<GpsPerson> _gpsPersonApiClient;

public IotWebApiService(ILogger<IotWebApiService> logger, HttpHelper httpHelper, GpsCardAccessorClient<GpsPerson> gpsPersonApiClient, IOptions<ServiceConfig> optConfigService)
public IotWebApiService(ILogger<IotWebApiService> logger, HttpHelper httpHelper, GpsCardAccessorClient<GpsPerson> gpsPersonApiClient, IOptions<ServiceConfig> optConfigService, PersonCacheManager personCacheMgr)
{
_configService = optConfigService.Value;
_httpHelper=httpHelper;
_logger = logger;
_logger = logger;
_personCacheMgr = personCacheMgr;
_gpsPersonApiClient = gpsPersonApiClient;
}
/// <summary>
@@ -80,7 +82,7 @@ namespace HealthMonitor.Service.Biz

}

/** 取消
/// <summary>
/// 更新 gps_person remark和缓存
/// </summary>
@@ -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<KeyValuePair<string, string>> headers = new()
// {
// new KeyValuePair<string, string>("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<KeyValuePair<string, string>> 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<KeyValuePair<string, string>> headers = new()
{
new KeyValuePair<string, string>("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;
}

*/
/// <summary>
/// 初次开通更新 gps_person remark和对应的缓存
/// </summary>
@@ -193,15 +223,36 @@ namespace HealthMonitor.Service.Biz
_logger.LogInformation($"更新Person remarks字段|{person.Remarks}");

// 更新缓存
var url = $"{_configService.IotWebApiUrl}Device/UpdatePersonInfoCache?imei={imei}";
List<KeyValuePair<string, string>> headers = new()
var personCache = await _personCacheMgr.GetDeviceGpsPersonCacheObjectBySerialNoAsync(new Guid().ToString(), imei).ConfigureAwait(false);
if (personCache != null)
{
new KeyValuePair<string, string>("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<KeyValuePair<string, string>> headers = new()
//{
// new KeyValuePair<string, string>("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)


+ 75
- 29
HealthMonitor.Service/Cache/PersonCacheManager.cs Ver arquivo

@@ -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<GpsPerson> _personApiClient;
private readonly ILogger<PersonCacheManager> _logger;
private readonly ILogger<PersonCacheManager> _logger;


public PersonCacheManager(
GpsCardAccessorClient<GpsPerson> personApiClient,
ILogger<PersonCacheManager> logger)
{
ILogger<PersonCacheManager> logger)
{
_personApiClient = personApiClient;
_logger = logger;
}
_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)
@@ -76,17 +77,17 @@ namespace HealthMonitor.Service.Cache
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}");
catch (Exception ex)
{
_logger.LogWarning($"Redis发生异常,将直接读取MySQL数据库,构造新实例:{ex.Message}, {ex.StackTrace}");
var param = new GeneralParam
{
Filters = new List<QueryFilterCondition>
@@ -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
}
};
}


}
/// <summary>
/// 获取gps_person缓存
/// </summary>
/// <param name="messageId"></param>
/// <param name="sn"></param>
/// <returns></returns>
public async Task<JToken?> 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;
}
/// <summary>
/// 更新gps_person缓存
/// </summary>
/// <param name="person"></param>
/// <param name="sn"></param>
/// <returns></returns>
public async Task<bool> 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;

}
}
}

+ 2
- 2
HealthMonitor.Service/Resolver/BloodpressResolver.cs Ver arquivo

@@ -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)
{


Carregando…
Cancelar
Salvar