You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 satır
11KB

  1. using HealthMonitor.Common.helper;
  2. using HealthMonitor.Model.Config;
  3. using HealthMonitor.Service.Resolver;
  4. using Microsoft.Extensions.Logging;
  5. using Microsoft.Extensions.Options;
  6. using Newtonsoft.Json.Linq;
  7. using Newtonsoft.Json;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using HealthMonitor.Model.Service;
  14. using TelpoDataService.Util.Entities.GpsCard;
  15. using TelpoDataService.Util;
  16. using TelpoDataService.Util.Clients;
  17. using TelpoDataService.Util.Models;
  18. using TelpoDataService.Util.QueryObjects;
  19. namespace HealthMonitor.Service.Biz
  20. {
  21. public class IotWebApiService
  22. {
  23. private readonly ServiceConfig _configService;
  24. private readonly ILogger<IotWebApiService> _logger;
  25. private readonly HttpHelper _httpHelper = default!;
  26. private readonly GpsCardAccessorClient<GpsPerson> _gpsPersonApiClient;
  27. public IotWebApiService(ILogger<IotWebApiService> logger, HttpHelper httpHelper, GpsCardAccessorClient<GpsPerson> gpsPersonApiClient, IOptions<ServiceConfig> optConfigService)
  28. {
  29. _configService = optConfigService.Value;
  30. _httpHelper=httpHelper;
  31. _logger = logger;
  32. _gpsPersonApiClient = gpsPersonApiClient;
  33. }
  34. /// <summary>
  35. /// 平台下发血压标定参数
  36. /// </summary>
  37. /// <param name="bpsCalibrationConfig"></param>
  38. /// <returns></returns>
  39. public async Task<bool> SetBloodPressCalibrationConfigAsync(BloodPressCalibrationConfigModel bpsCalibrationConfig)
  40. {
  41. #if DEBUG
  42. var flag = true;
  43. #else
  44. //systolicCalibrationValue = 0, //收缩压标定值,值为0 表示不生效
  45. //diastolicCalibrationValue 0, //舒张压标定值,值为0表示不生效
  46. //systolicIncValue = 0, //收缩压显示增量,值为0 表示不生效
  47. //diastolicIncValue = 0 //舒张压显示增量,值为0 表示不生效
  48. var flag = false;
  49. try
  50. {
  51. var url = $"{_configService.IotWebApiUrl}Command/SetBloodPressCalibrationConfig";
  52. List<KeyValuePair<string, string>> headers = new()
  53. {
  54. new KeyValuePair<string, string>("AuthKey", "key1")
  55. };
  56. var res = await _httpHelper.HttpToPostAsync(url, bpsCalibrationConfig, headers).ConfigureAwait(false);
  57. _logger.LogInformation($"向{bpsCalibrationConfig.Imei}下发增量值数据:{JsonConvert.SerializeObject(bpsCalibrationConfig)},响应:{res}");
  58. var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
  59. flag= resJToken?["message"]?.ToString().Equals("ok") ?? false;
  60. }
  61. catch (Exception ex)
  62. {
  63. _logger.LogError($"{nameof(SetBloodPressCalibrationConfigAsync)} 下发血压增量值异常:{ex.Message}, {ex.StackTrace}");
  64. }
  65. #endif
  66. return flag;
  67. }
  68. /// <summary>
  69. /// 更新 gps_person remark和缓存
  70. /// </summary>
  71. /// <param name="imei"></param>
  72. /// <param name="systolicRefValue"></param>
  73. /// <param name="diastolicRefValue"></param>
  74. /// <returns></returns>
  75. public async Task<bool> UpdatePersonRemarksAsync(string imei,int systolicRefValue,int diastolicRefValue)
  76. {
  77. var flag = false;
  78. try
  79. {
  80. GeneralParam condition = new ()
  81. {
  82. Filters = new List<QueryFilterCondition> {
  83. new QueryFilterCondition {
  84. Key=nameof(GpsDevice.Serialno),
  85. Value=imei,
  86. Operator= QueryOperatorEnum.Equal,
  87. ValueType=QueryValueTypeEnum.String
  88. }
  89. },
  90. OrderBys = new List<OrderByCondition> { new OrderByCondition { Key = "serialno", IsDesc = true } }
  91. };
  92. var person = await _gpsPersonApiClient.GetFirstAsync(condition, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
  93. // 若remark为空,更新person remark字段
  94. if (string.IsNullOrWhiteSpace(person?.Remarks))
  95. {
  96. var newRemarkData = new
  97. {
  98. imei,
  99. time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  100. commandValue = new
  101. {
  102. systolicCalibrationValue = systolicRefValue, //收缩压标定值,值为0 表示不生效
  103. diastolicCalibrationValue = diastolicRefValue, //舒张压标定值,值为0表示不生效
  104. systolicIncValue = 0, //收缩压显示增量,值为0 表示不生效
  105. diastolicIncValue = 0 //舒张压显示增量,值为0 表示不生效
  106. }
  107. };
  108. person!.Remarks = $"is_blood_press:{JsonConvert.SerializeObject(newRemarkData)}|";
  109. await _gpsPersonApiClient.UpdateAsync(person, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
  110. _logger.LogInformation($"更新Person remarks字段|{person.Remarks}");
  111. // 更新缓存
  112. var url = $"{_configService.IotWebApiUrl}Device/UpdatePersonInfoCache?imei={imei}";
  113. List<KeyValuePair<string, string>> headers = new()
  114. {
  115. new KeyValuePair<string, string>("AuthKey", "key1")
  116. };
  117. var res = await _httpHelper.HttpToGetAsync(url, headers).ConfigureAwait(false);
  118. _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonRemarksAsync)},响应:{res}");
  119. var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
  120. flag = resJToken?["message"]?.ToString().Equals("ok") ?? false;
  121. }
  122. }
  123. catch (Exception ex)
  124. {
  125. _logger.LogError($"{nameof(UpdatePersonRemarksAsync)} 更新个人信息异常:{ex.Message}, {ex.StackTrace}");
  126. }
  127. return flag;
  128. }
  129. /// <summary>
  130. /// 初次开通更新 gps_person remark和对应的缓存
  131. /// </summary>
  132. /// <param name="imei"></param>
  133. /// <param name="systolicRefValue"></param>
  134. /// <param name="diastolicRefValue"></param>
  135. /// <param name="systolicIncValue"></param>
  136. /// <param name="diastolicIncValue"></param>
  137. /// <returns></returns>
  138. public async Task<bool> UpdatePersonRemarksAsync(string imei, int systolicRefValue, int diastolicRefValue,int systolicIncValue,int diastolicIncValue)
  139. {
  140. var flag = false;
  141. try
  142. {
  143. GeneralParam condition = new()
  144. {
  145. Filters = new List<QueryFilterCondition> {
  146. new QueryFilterCondition {
  147. Key=nameof(GpsDevice.Serialno),
  148. Value=imei,
  149. Operator= QueryOperatorEnum.Equal,
  150. ValueType=QueryValueTypeEnum.String
  151. }
  152. },
  153. OrderBys = new List<OrderByCondition> { new OrderByCondition { Key = "serialno", IsDesc = true } }
  154. };
  155. var person = await _gpsPersonApiClient.GetFirstAsync(condition, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
  156. // 若remark为空,更新person remark字段
  157. if (string.IsNullOrWhiteSpace(person?.Remarks))
  158. {
  159. var newRemarkData = new
  160. {
  161. imei,
  162. time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  163. commandValue = new
  164. {
  165. systolicCalibrationValue = systolicRefValue, //收缩压标定值,值为0 表示不生效
  166. diastolicCalibrationValue = diastolicRefValue, //舒张压标定值,值为0表示不生效
  167. systolicIncValue, //收缩压显示增量,值为0 表示不生效
  168. diastolicIncValue //舒张压显示增量,值为0 表示不生效
  169. }
  170. };
  171. person!.Remarks = $"is_blood_press:{JsonConvert.SerializeObject(newRemarkData)}|";
  172. await _gpsPersonApiClient.UpdateAsync(person, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
  173. _logger.LogInformation($"更新Person remarks字段|{person.Remarks}");
  174. // 更新缓存
  175. var url = $"{_configService.IotWebApiUrl}Device/UpdatePersonInfoCache?imei={imei}";
  176. List<KeyValuePair<string, string>> headers = new()
  177. {
  178. new KeyValuePair<string, string>("AuthKey", "key1")
  179. };
  180. var res = await _httpHelper.HttpToGetAsync(url, headers).ConfigureAwait(false);
  181. _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonRemarksAsync)},响应:{res}");
  182. var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
  183. flag = resJToken?["message"]?.ToString().Equals("ok") ?? false;
  184. }
  185. }
  186. catch (Exception ex)
  187. {
  188. _logger.LogError($"{nameof(UpdatePersonRemarksAsync)} 更新个人信息异常:{ex.Message}, {ex.StackTrace}");
  189. }
  190. return flag;
  191. }
  192. /** 取消
  193. public async Task<bool> UpdatePersonInfoCacheAsync(string imei)
  194. {
  195. var flag = false;
  196. try
  197. {
  198. var url = $"{_configService.IotWebApiUrl}Device/UpdatePersonInfoCache?imei={imei}";
  199. List<KeyValuePair<string, string>> headers = new()
  200. {
  201. new KeyValuePair<string, string>("AuthKey", "key1")
  202. };
  203. var res = await _httpHelper.HttpToGetAsync(url, headers).ConfigureAwait(false);
  204. _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonInfoCacheAsync)},响应:{res}");
  205. var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
  206. flag = resJToken?["message"]?.ToString().Equals("ok") ?? false;
  207. }
  208. catch (Exception ex)
  209. {
  210. _logger.LogError($"{nameof(UpdatePersonInfoCacheAsync)} 更新缓存异常:{ex.Message}, {ex.StackTrace}");
  211. }
  212. return flag;
  213. }
  214. */
  215. }
  216. }