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; namespace HealthMonitor.Service.Biz { public class IotWebApiService { private readonly ServiceConfig _configService; private readonly ILogger _logger; private readonly HttpHelper _httpHelper = default!; private readonly GpsCardAccessorClient _gpsPersonApiClient; public IotWebApiService(ILogger logger, HttpHelper httpHelper, GpsCardAccessorClient gpsPersonApiClient, IOptions optConfigService) { _configService = optConfigService.Value; _httpHelper=httpHelper; _logger = logger; _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; } 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; } /// /// 更新 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(UpdatePersonInfoCacheAsync)},响应:{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; } } }