|
- using HealthMonitor.Common.helper;
- using HealthMonitor.Model.Config;
- using HealthMonitor.Service.Resolver;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using Newtonsoft.Json.Linq;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using HealthMonitor.Model.Service;
- using TelpoDataService.Util.Entities.GpsCard;
- using TelpoDataService.Util;
- using TelpoDataService.Util.Clients;
- using TelpoDataService.Util.Models;
- using TelpoDataService.Util.QueryObjects;
- using HealthMonitor.Service.Cache;
- using HealthMonitor.Model.Cache;
-
- namespace HealthMonitor.Service.Biz
- {
- public class IotWebApiService
- {
- 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, PersonCacheManager personCacheMgr)
- {
- _configService = optConfigService.Value;
- _httpHelper=httpHelper;
- _logger = logger;
- _personCacheMgr = personCacheMgr;
- _gpsPersonApiClient = gpsPersonApiClient;
- }
- /// <summary>
- /// 平台下发血压标定参数
- /// </summary>
- /// <param name="bpsCalibrationConfig"></param>
- /// <returns></returns>
- public async Task<bool> SetBloodPressCalibrationConfigAsync(BloodPressCalibrationConfigModel bpsCalibrationConfig)
- {
-
- #if DEBUG
- var flag = true;
- #else
- //systolicCalibrationValue = 0, //收缩压标定值,值为0 表示不生效
- //diastolicCalibrationValue 0, //舒张压标定值,值为0表示不生效
- //systolicIncValue = 0, //收缩压显示增量,值为0 表示不生效
- //diastolicIncValue = 0 //舒张压显示增量,值为0 表示不生效
-
- var flag = false;
- try
- {
- var url = $"{_configService.IotWebApiUrl}Command/SetBloodPressCalibrationConfig";
- List<KeyValuePair<string, string>> headers = new()
- {
- new KeyValuePair<string, string>("AuthKey", "key1")
- };
- var res = await _httpHelper.HttpToPostAsync(url, bpsCalibrationConfig, headers).ConfigureAwait(false);
- _logger.LogInformation($"向{bpsCalibrationConfig.Imei}下发增量值数据:{JsonConvert.SerializeObject(bpsCalibrationConfig)},响应:{res}");
- var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
- flag= resJToken?["message"]?.ToString().Equals("ok") ?? false;
-
- }
- catch (Exception ex)
- {
- _logger.LogError($"{nameof(SetBloodPressCalibrationConfigAsync)} 下发血压增量值异常:{ex.Message}, {ex.StackTrace}");
-
- }
-
-
- #endif
-
- return flag;
-
-
- }
-
- /** 取消
- /// <summary>
- /// 更新 gps_person remark和缓存
- /// </summary>
- /// <param name="imei"></param>
- /// <param name="systolicRefValue"></param>
- /// <param name="diastolicRefValue"></param>
- /// <returns></returns>
- public async Task<bool> UpdatePersonRemarksAsync(string imei,int systolicRefValue,int diastolicRefValue)
- {
- var flag = false;
- try
- {
- GeneralParam condition = new ()
- {
- Filters = new List<QueryFilterCondition> {
- new QueryFilterCondition {
- Key=nameof(GpsDevice.Serialno),
- Value=imei,
- Operator= QueryOperatorEnum.Equal,
- ValueType=QueryValueTypeEnum.String
- }
- },
- OrderBys = new List<OrderByCondition> { new OrderByCondition { Key = "serialno", IsDesc = true } }
-
- };
- var person = await _gpsPersonApiClient.GetFirstAsync(condition, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
- //// 若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
- {
- 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;
- }
- catch (Exception ex)
- {
- _logger.LogError($"{nameof(UpdatePersonRemarksAsync)} 更新个人信息异常:{ex.Message}, {ex.StackTrace}");
- }
- return flag;
- }
- */
-
- ///// <summary>
- ///// 初次开通更新 gps_person remark和对应的缓存
- ///// </summary>
- ///// <param name="imei"></param>
- ///// <param name="systolicRefValue"></param>
- ///// <param name="diastolicRefValue"></param>
- ///// <param name="systolicIncValue"></param>
- ///// <param name="diastolicIncValue"></param>
- ///// <returns></returns>
- //public async Task<bool> UpdatePersonRemarksAsync0(string imei, int systolicRefValue, int diastolicRefValue,int systolicIncValue,int diastolicIncValue)
- //{
- // var flag = false;
- // try
- // {
- // GeneralParam condition = new()
- // {
- // Filters = new List<QueryFilterCondition> {
- // new QueryFilterCondition {
- // Key=nameof(GpsDevice.Serialno),
- // Value=imei,
- // Operator= QueryOperatorEnum.Equal,
- // ValueType=QueryValueTypeEnum.String
- // }
- // },
- // OrderBys = new List<OrderByCondition> { new OrderByCondition { Key = "serialno", IsDesc = true } }
-
- // };
- // var person = await _gpsPersonApiClient.GetFirstAsync(condition, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
- // // 若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 表示不生效
- // diastolicIncValue //舒张压显示增量,值为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 personCache = await _personCacheMgr.GetDeviceGpsPersonCacheObjectBySerialNoAsync(new Guid().ToString(), imei).ConfigureAwait(false);
-
- // if (personCache != null)
- // {
-
- // //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)
- // {
- // _logger.LogError($"{nameof(UpdatePersonRemarksAsync)} 更新个人信息异常:{ex.Message}, {ex.StackTrace}");
- // }
- // return flag;
- //}
-
- /// <summary>
- /// 更新 gps_person remark缓存和数据库
- /// </summary>
- /// <param name="imei"></param>
- /// <param name="systolicRefValue"></param>
- /// <param name="diastolicRefValue"></param>
- /// <param name="systolicIncValue"></param>
- /// <param name="diastolicIncValue"></param>
- /// <param name="isInitRemakers">是否初始化,即清空remakers</param>
- /// <returns></returns>
- public async Task<bool> UpdatePersonRemarksAsync(string imei, int systolicRefValue, int diastolicRefValue, int systolicIncValue, int diastolicIncValue, bool isInitRemakers=false)
- {
- var flag = false;
- try
- {
-
- // 保证实时性,先更新缓存,再更新数据库
- var personCache = await _personCacheMgr.GetDeviceGpsPersonCacheObjectBySerialNoAsync(new Guid().ToString(), imei).ConfigureAwait(false);
-
- if (personCache == null)
- {
- _logger.LogInformation($"{imei} -- Person remarks数据异常,检查缓存和数据库");
- }
- else if (string.IsNullOrWhiteSpace(personCache["person"]!["remarks"]!.ToString()))
- {
- var newRemarkData = new
- {
- imei,
- time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
- commandValue = new
- {
- systolicCalibrationValue = systolicRefValue, //收缩压标定值,值为0 表示不生效
- diastolicCalibrationValue = diastolicRefValue, //舒张压标定值,值为0表示不生效
- systolicIncValue, //收缩压显示增量,值为0 表示不生效
- diastolicIncValue //舒张压显示增量,值为0 表示不生效
- }
- };
- var newRemarkStr = isInitRemakers ? string.Empty:$"is_blood_press:{JsonConvert.SerializeObject(newRemarkData)}|";
- personCache["person"]!["remarks"] = newRemarkStr;
- bool cacheFlag = await _personCacheMgr.UpdateDeviceGpsPersonCacheObjectBySerialNoAsync(personCache, imei);
- if (cacheFlag)
- {
- GeneralParam condition = new()
- {
- Filters = new List<QueryFilterCondition> {
- new QueryFilterCondition {
- Key=nameof(GpsDevice.Serialno),
- Value=imei,
- Operator= QueryOperatorEnum.Equal,
- ValueType=QueryValueTypeEnum.String
- }
- },
- OrderBys = new List<OrderByCondition> { new OrderByCondition { Key = "serialno", IsDesc = true } }
-
- };
- _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonRemarksAsync)}成功,{JsonConvert.SerializeObject(personCache)}");
- // 读取数据库
- var person = await _gpsPersonApiClient.GetFirstAsync(condition, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
- // 更新字段
- person!.Remarks = newRemarkStr;
- await _gpsPersonApiClient.UpdateAsync(person, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
- _logger.LogInformation($"{imei} 更新Person remarks字段|{person.Remarks}");
-
- }
- else
- {
- _logger.LogInformation($"{imei} 更新缓存和数据库{nameof(UpdatePersonRemarksAsync)}失败,{JsonConvert.SerializeObject(personCache)}");
- }
- flag = cacheFlag;
- }
- }
- catch (Exception ex)
- {
- _logger.LogError($"{nameof(UpdatePersonRemarksAsync)} {imei}--更新个人信息异常:{ex.Message}, {ex.StackTrace}");
- }
- return flag;
- }
- /** 取消
- public async Task<bool> UpdatePersonInfoCacheAsync(string imei)
- {
- var flag = false;
- try
- {
- 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(UpdatePersonInfoCacheAsync)},响应:{res}");
- var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
- flag = resJToken?["message"]?.ToString().Equals("ok") ?? false;
- }
- catch (Exception ex)
- {
- _logger.LogError($"{nameof(UpdatePersonInfoCacheAsync)} 更新缓存异常:{ex.Message}, {ex.StackTrace}");
-
- }
- return flag;
-
-
- }
- */
-
- }
- }
|