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 _logger; private readonly PersonCacheManager _personCacheMgr; private readonly HttpHelper _httpHelper = default!; private readonly GpsCardAccessorClient _gpsPersonApiClient; public IotWebApiService(ILogger logger, HttpHelper httpHelper, GpsCardAccessorClient gpsPersonApiClient, IOptions optConfigService, PersonCacheManager personCacheMgr) { _configService = optConfigService.Value; _httpHelper=httpHelper; _logger = logger; _personCacheMgr = personCacheMgr; _gpsPersonApiClient = gpsPersonApiClient; } /// /// 平台下发血压标定参数 /// /// /// public async Task 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> headers = new() { new KeyValuePair("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; } /** 取消 /// /// 更新 gps_person remark和缓存 /// /// /// /// /// public async Task UpdatePersonRemarksAsync(string imei,int systolicRefValue,int diastolicRefValue) { var flag = false; try { GeneralParam condition = new () { Filters = new List { new QueryFilterCondition { Key=nameof(GpsDevice.Serialno), Value=imei, Operator= QueryOperatorEnum.Equal, ValueType=QueryValueTypeEnum.String } }, OrderBys = new List { 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> 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 { 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; } catch (Exception ex) { _logger.LogError($"{nameof(UpdatePersonRemarksAsync)} 更新个人信息异常:{ex.Message}, {ex.StackTrace}"); } return flag; } */ ///// ///// 初次开通更新 gps_person remark和对应的缓存 ///// ///// ///// ///// ///// ///// ///// //public async Task UpdatePersonRemarksAsync0(string imei, int systolicRefValue, int diastolicRefValue,int systolicIncValue,int diastolicIncValue) //{ // var flag = false; // try // { // GeneralParam condition = new() // { // Filters = new List { // new QueryFilterCondition { // Key=nameof(GpsDevice.Serialno), // Value=imei, // Operator= QueryOperatorEnum.Equal, // ValueType=QueryValueTypeEnum.String // } // }, // OrderBys = new List { 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> 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) // { // _logger.LogError($"{nameof(UpdatePersonRemarksAsync)} 更新个人信息异常:{ex.Message}, {ex.StackTrace}"); // } // return flag; //} /// /// 更新 gps_person remark缓存和数据库 /// /// /// /// /// /// /// 是否初始化,即清空remakers /// public async Task 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 { new QueryFilterCondition { Key=nameof(GpsDevice.Serialno), Value=imei, Operator= QueryOperatorEnum.Equal, ValueType=QueryValueTypeEnum.String } }, OrderBys = new List { 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 UpdatePersonInfoCacheAsync(string imei) { var flag = false; try { 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(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; } */ } }