Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

235 rindas
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. public async Task<bool> SetBloodPressCalibrationConfigAsync(BloodPressCalibrationConfigModel bpsCalibrationConfig)
  35. {
  36. #if DEBUG
  37. var flag = true;
  38. #else
  39. //systolicCalibrationValue = 0, //收缩压标定值,值为0 表示不生效
  40. //diastolicCalibrationValue 0, //舒张压标定值,值为0表示不生效
  41. //systolicIncValue = 0, //收缩压显示增量,值为0 表示不生效
  42. //diastolicIncValue = 0 //舒张压显示增量,值为0 表示不生效
  43. var flag = false;
  44. try
  45. {
  46. var url = $"{_configService.IotWebApiUrl}Command/SetBloodPressCalibrationConfig";
  47. List<KeyValuePair<string, string>> headers = new()
  48. {
  49. new KeyValuePair<string, string>("AuthKey", "key1")
  50. };
  51. var res = await _httpHelper.HttpToPostAsync(url, bpsCalibrationConfig, headers).ConfigureAwait(false);
  52. _logger.LogInformation($"向{bpsCalibrationConfig.Imei}下发增量值数据:{JsonConvert.SerializeObject(bpsCalibrationConfig)},响应:{res}");
  53. var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
  54. flag= resJToken?["message"]?.ToString().Equals("ok") ?? false;
  55. }
  56. catch (Exception ex)
  57. {
  58. _logger.LogError($"{nameof(SetBloodPressCalibrationConfigAsync)} 下发血压增量值异常:{ex.Message}, {ex.StackTrace}");
  59. }
  60. #endif
  61. return flag;
  62. }
  63. public async Task<bool> UpdatePersonInfoCacheAsync(string imei)
  64. {
  65. var flag = false;
  66. try
  67. {
  68. var url = $"{_configService.IotWebApiUrl}Device/UpdatePersonInfoCache?imei={imei}";
  69. List<KeyValuePair<string, string>> headers = new()
  70. {
  71. new KeyValuePair<string, string>("AuthKey", "key1")
  72. };
  73. var res = await _httpHelper.HttpToGetAsync(url, headers).ConfigureAwait(false);
  74. _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonInfoCacheAsync)},响应:{res}");
  75. var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
  76. flag = resJToken?["message"]?.ToString().Equals("ok") ?? false;
  77. }
  78. catch (Exception ex)
  79. {
  80. _logger.LogError($"{nameof(UpdatePersonInfoCacheAsync)} 更新缓存异常:{ex.Message}, {ex.StackTrace}");
  81. }
  82. return flag;
  83. }
  84. /// <summary>
  85. /// 更新 gps_person remark和缓存
  86. /// </summary>
  87. /// <param name="imei"></param>
  88. /// <param name="systolicRefValue"></param>
  89. /// <param name="diastolicRefValue"></param>
  90. /// <returns></returns>
  91. public async Task<bool> UpdatePersonRemarksAsync(string imei,int systolicRefValue,int diastolicRefValue)
  92. {
  93. var flag = false;
  94. try
  95. {
  96. GeneralParam condition = new ()
  97. {
  98. Filters = new List<QueryFilterCondition> {
  99. new QueryFilterCondition {
  100. Key=nameof(GpsDevice.Serialno),
  101. Value=imei,
  102. Operator= QueryOperatorEnum.Equal,
  103. ValueType=QueryValueTypeEnum.String
  104. }
  105. },
  106. OrderBys = new List<OrderByCondition> { new OrderByCondition { Key = "serialno", IsDesc = true } }
  107. };
  108. var person = await _gpsPersonApiClient.GetFirstAsync(condition, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
  109. // 若remark为空,更新person remark字段
  110. if (string.IsNullOrWhiteSpace(person?.Remarks))
  111. {
  112. var newRemarkData = new
  113. {
  114. imei,
  115. time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  116. commandValue = new
  117. {
  118. systolicCalibrationValue = systolicRefValue, //收缩压标定值,值为0 表示不生效
  119. diastolicCalibrationValue = diastolicRefValue, //舒张压标定值,值为0表示不生效
  120. systolicIncValue = 0, //收缩压显示增量,值为0 表示不生效
  121. diastolicIncValue = 0 //舒张压显示增量,值为0 表示不生效
  122. }
  123. };
  124. person!.Remarks = $"is_blood_press:{JsonConvert.SerializeObject(newRemarkData)}|";
  125. await _gpsPersonApiClient.UpdateAsync(person, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
  126. _logger.LogInformation($"更新Person remarks字段|{person.Remarks}");
  127. // 更新缓存
  128. var url = $"{_configService.IotWebApiUrl}Device/UpdatePersonInfoCache?imei={imei}";
  129. List<KeyValuePair<string, string>> headers = new()
  130. {
  131. new KeyValuePair<string, string>("AuthKey", "key1")
  132. };
  133. var res = await _httpHelper.HttpToGetAsync(url, headers).ConfigureAwait(false);
  134. _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonInfoCacheAsync)},响应:{res}");
  135. var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
  136. flag = resJToken?["message"]?.ToString().Equals("ok") ?? false;
  137. }
  138. }
  139. catch (Exception ex)
  140. {
  141. _logger.LogError($"{nameof(UpdatePersonRemarksAsync)} 更新个人信息异常:{ex.Message}, {ex.StackTrace}");
  142. }
  143. return flag;
  144. }
  145. /// <summary>
  146. /// 初次开通
  147. /// </summary>
  148. /// <param name="imei"></param>
  149. /// <param name="systolicRefValue"></param>
  150. /// <param name="diastolicRefValue"></param>
  151. /// <param name="systolicIncValue"></param>
  152. /// <param name="diastolicIncValue"></param>
  153. /// <returns></returns>
  154. public async Task<bool> UpdatePersonRemarksAsync(string imei, int systolicRefValue, int diastolicRefValue,int systolicIncValue,int diastolicIncValue)
  155. {
  156. var flag = false;
  157. try
  158. {
  159. GeneralParam condition = new()
  160. {
  161. Filters = new List<QueryFilterCondition> {
  162. new QueryFilterCondition {
  163. Key=nameof(GpsDevice.Serialno),
  164. Value=imei,
  165. Operator= QueryOperatorEnum.Equal,
  166. ValueType=QueryValueTypeEnum.String
  167. }
  168. },
  169. OrderBys = new List<OrderByCondition> { new OrderByCondition { Key = "serialno", IsDesc = true } }
  170. };
  171. var person = await _gpsPersonApiClient.GetFirstAsync(condition, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
  172. // 若remark为空,更新person remark字段
  173. if (string.IsNullOrWhiteSpace(person?.Remarks))
  174. {
  175. var newRemarkData = new
  176. {
  177. imei,
  178. time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  179. commandValue = new
  180. {
  181. systolicCalibrationValue = systolicRefValue, //收缩压标定值,值为0 表示不生效
  182. diastolicCalibrationValue = diastolicRefValue, //舒张压标定值,值为0表示不生效
  183. systolicIncValue, //收缩压显示增量,值为0 表示不生效
  184. diastolicIncValue //舒张压显示增量,值为0 表示不生效
  185. }
  186. };
  187. person!.Remarks = $"is_blood_press:{JsonConvert.SerializeObject(newRemarkData)}|";
  188. await _gpsPersonApiClient.UpdateAsync(person, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
  189. _logger.LogInformation($"更新Person remarks字段|{person.Remarks}");
  190. // 更新缓存
  191. var url = $"{_configService.IotWebApiUrl}Device/UpdatePersonInfoCache?imei={imei}";
  192. List<KeyValuePair<string, string>> headers = new()
  193. {
  194. new KeyValuePair<string, string>("AuthKey", "key1")
  195. };
  196. var res = await _httpHelper.HttpToGetAsync(url, headers).ConfigureAwait(false);
  197. _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonInfoCacheAsync)},响应:{res}");
  198. var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
  199. flag = resJToken?["message"]?.ToString().Equals("ok") ?? false;
  200. }
  201. }
  202. catch (Exception ex)
  203. {
  204. _logger.LogError($"{nameof(UpdatePersonRemarksAsync)} 更新个人信息异常:{ex.Message}, {ex.StackTrace}");
  205. }
  206. return flag;
  207. }
  208. }
  209. }