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.

294 rindas
14KB

  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. using HealthMonitor.Service.Cache;
  20. namespace HealthMonitor.Service.Biz
  21. {
  22. public class IotWebApiService
  23. {
  24. private readonly ServiceConfig _configService;
  25. private readonly ILogger<IotWebApiService> _logger;
  26. private readonly PersonCacheManager _personCacheMgr;
  27. private readonly HttpHelper _httpHelper = default!;
  28. private readonly GpsCardAccessorClient<GpsPerson> _gpsPersonApiClient;
  29. public IotWebApiService(ILogger<IotWebApiService> logger, HttpHelper httpHelper, GpsCardAccessorClient<GpsPerson> gpsPersonApiClient, IOptions<ServiceConfig> optConfigService, PersonCacheManager personCacheMgr)
  30. {
  31. _configService = optConfigService.Value;
  32. _httpHelper=httpHelper;
  33. _logger = logger;
  34. _personCacheMgr = personCacheMgr;
  35. _gpsPersonApiClient = gpsPersonApiClient;
  36. }
  37. /// <summary>
  38. /// 平台下发血压标定参数
  39. /// </summary>
  40. /// <param name="bpsCalibrationConfig"></param>
  41. /// <returns></returns>
  42. public async Task<bool> SetBloodPressCalibrationConfigAsync(BloodPressCalibrationConfigModel bpsCalibrationConfig)
  43. {
  44. #if DEBUG
  45. var flag = true;
  46. #else
  47. //systolicCalibrationValue = 0, //收缩压标定值,值为0 表示不生效
  48. //diastolicCalibrationValue 0, //舒张压标定值,值为0表示不生效
  49. //systolicIncValue = 0, //收缩压显示增量,值为0 表示不生效
  50. //diastolicIncValue = 0 //舒张压显示增量,值为0 表示不生效
  51. var flag = false;
  52. try
  53. {
  54. var url = $"{_configService.IotWebApiUrl}Command/SetBloodPressCalibrationConfig";
  55. List<KeyValuePair<string, string>> headers = new()
  56. {
  57. new KeyValuePair<string, string>("AuthKey", "key1")
  58. };
  59. var res = await _httpHelper.HttpToPostAsync(url, bpsCalibrationConfig, headers).ConfigureAwait(false);
  60. _logger.LogInformation($"向{bpsCalibrationConfig.Imei}下发增量值数据:{JsonConvert.SerializeObject(bpsCalibrationConfig)},响应:{res}");
  61. var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
  62. flag= resJToken?["message"]?.ToString().Equals("ok") ?? false;
  63. }
  64. catch (Exception ex)
  65. {
  66. _logger.LogError($"{nameof(SetBloodPressCalibrationConfigAsync)} 下发血压增量值异常:{ex.Message}, {ex.StackTrace}");
  67. }
  68. #endif
  69. return flag;
  70. }
  71. /** 取消
  72. /// <summary>
  73. /// 更新 gps_person remark和缓存
  74. /// </summary>
  75. /// <param name="imei"></param>
  76. /// <param name="systolicRefValue"></param>
  77. /// <param name="diastolicRefValue"></param>
  78. /// <returns></returns>
  79. public async Task<bool> UpdatePersonRemarksAsync(string imei,int systolicRefValue,int diastolicRefValue)
  80. {
  81. var flag = false;
  82. try
  83. {
  84. GeneralParam condition = new ()
  85. {
  86. Filters = new List<QueryFilterCondition> {
  87. new QueryFilterCondition {
  88. Key=nameof(GpsDevice.Serialno),
  89. Value=imei,
  90. Operator= QueryOperatorEnum.Equal,
  91. ValueType=QueryValueTypeEnum.String
  92. }
  93. },
  94. OrderBys = new List<OrderByCondition> { new OrderByCondition { Key = "serialno", IsDesc = true } }
  95. };
  96. var person = await _gpsPersonApiClient.GetFirstAsync(condition, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
  97. //// 若remark为空,更新person remark字段
  98. //if (string.IsNullOrWhiteSpace(person?.Remarks))
  99. //{
  100. // var newRemarkData = new
  101. // {
  102. // imei,
  103. // time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  104. // commandValue = new
  105. // {
  106. // systolicCalibrationValue = systolicRefValue, //收缩压标定值,值为0 表示不生效
  107. // diastolicCalibrationValue = diastolicRefValue, //舒张压标定值,值为0表示不生效
  108. // systolicIncValue = 0, //收缩压显示增量,值为0 表示不生效
  109. // diastolicIncValue = 0 //舒张压显示增量,值为0 表示不生效
  110. // }
  111. // };
  112. // person!.Remarks = $"is_blood_press:{JsonConvert.SerializeObject(newRemarkData)}|";
  113. // await _gpsPersonApiClient.UpdateAsync(person, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
  114. // _logger.LogInformation($"更新Person remarks字段|{person.Remarks}");
  115. // // 更新缓存
  116. // var url = $"{_configService.IotWebApiUrl}Device/UpdatePersonInfoCache?imei={imei}";
  117. // List<KeyValuePair<string, string>> headers = new()
  118. // {
  119. // new KeyValuePair<string, string>("AuthKey", "key1")
  120. // };
  121. // var res = await _httpHelper.HttpToGetAsync(url, headers).ConfigureAwait(false);
  122. // _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonRemarksAsync)},响应:{res}");
  123. // var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
  124. // flag = resJToken?["message"]?.ToString().Equals("ok") ?? false;
  125. //}
  126. var newRemarkData = new
  127. {
  128. imei,
  129. time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  130. commandValue = new
  131. {
  132. systolicCalibrationValue = systolicRefValue, //收缩压标定值,值为0 表示不生效
  133. diastolicCalibrationValue = diastolicRefValue, //舒张压标定值,值为0表示不生效
  134. systolicIncValue = 0, //收缩压显示增量,值为0 表示不生效
  135. diastolicIncValue = 0 //舒张压显示增量,值为0 表示不生效
  136. }
  137. };
  138. person!.Remarks = $"is_blood_press:{JsonConvert.SerializeObject(newRemarkData)}|";
  139. await _gpsPersonApiClient.UpdateAsync(person, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
  140. _logger.LogInformation($"更新Person remarks字段|{person.Remarks}");
  141. // 更新缓存
  142. var url = $"{_configService.IotWebApiUrl}Device/UpdatePersonInfoCache?imei={imei}";
  143. List<KeyValuePair<string, string>> headers = new()
  144. {
  145. new KeyValuePair<string, string>("AuthKey", "key1")
  146. };
  147. var res = await _httpHelper.HttpToGetAsync(url, headers).ConfigureAwait(false);
  148. _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonRemarksAsync)},响应:{res}");
  149. var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
  150. flag = resJToken?["message"]?.ToString().Equals("ok") ?? false;
  151. }
  152. catch (Exception ex)
  153. {
  154. _logger.LogError($"{nameof(UpdatePersonRemarksAsync)} 更新个人信息异常:{ex.Message}, {ex.StackTrace}");
  155. }
  156. return flag;
  157. }
  158. */
  159. /// <summary>
  160. /// 初次开通更新 gps_person remark和对应的缓存
  161. /// </summary>
  162. /// <param name="imei"></param>
  163. /// <param name="systolicRefValue"></param>
  164. /// <param name="diastolicRefValue"></param>
  165. /// <param name="systolicIncValue"></param>
  166. /// <param name="diastolicIncValue"></param>
  167. /// <returns></returns>
  168. public async Task<bool> UpdatePersonRemarksAsync(string imei, int systolicRefValue, int diastolicRefValue,int systolicIncValue,int diastolicIncValue)
  169. {
  170. var flag = false;
  171. try
  172. {
  173. GeneralParam condition = new()
  174. {
  175. Filters = new List<QueryFilterCondition> {
  176. new QueryFilterCondition {
  177. Key=nameof(GpsDevice.Serialno),
  178. Value=imei,
  179. Operator= QueryOperatorEnum.Equal,
  180. ValueType=QueryValueTypeEnum.String
  181. }
  182. },
  183. OrderBys = new List<OrderByCondition> { new OrderByCondition { Key = "serialno", IsDesc = true } }
  184. };
  185. var person = await _gpsPersonApiClient.GetFirstAsync(condition, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
  186. // 若remark为空,更新person remark字段
  187. if (string.IsNullOrWhiteSpace(person?.Remarks))
  188. {
  189. var newRemarkData = new
  190. {
  191. imei,
  192. time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  193. commandValue = new
  194. {
  195. systolicCalibrationValue = systolicRefValue, //收缩压标定值,值为0 表示不生效
  196. diastolicCalibrationValue = diastolicRefValue, //舒张压标定值,值为0表示不生效
  197. systolicIncValue, //收缩压显示增量,值为0 表示不生效
  198. diastolicIncValue //舒张压显示增量,值为0 表示不生效
  199. }
  200. };
  201. person!.Remarks = $"is_blood_press:{JsonConvert.SerializeObject(newRemarkData)}|";
  202. await _gpsPersonApiClient.UpdateAsync(person, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
  203. _logger.LogInformation($"更新Person remarks字段|{person.Remarks}");
  204. // 更新缓存
  205. var personCache = await _personCacheMgr.GetDeviceGpsPersonCacheObjectBySerialNoAsync(new Guid().ToString(), imei).ConfigureAwait(false);
  206. if (personCache != null)
  207. {
  208. //personCache.Person.Remarks = person!.Remarks;
  209. personCache["person"]!["remarks"] = person!.Remarks;
  210. bool cacheFlag= await _personCacheMgr.UpdateDeviceGpsPersonCacheObjectBySerialNoAsync(personCache, imei);
  211. // flag = true;
  212. if (cacheFlag)
  213. {
  214. flag = true;
  215. _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonRemarksAsync)}成功,{JsonConvert.SerializeObject(personCache)}");
  216. }
  217. else
  218. {
  219. flag = false;
  220. _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonRemarksAsync)}失败,{JsonConvert.SerializeObject(personCache)}");
  221. }
  222. }
  223. //var url = $"{_configService.IotWebApiUrl}Device/UpdatePersonInfoCache?imei={imei}";
  224. //List<KeyValuePair<string, string>> headers = new()
  225. //{
  226. // new KeyValuePair<string, string>("AuthKey", "key1")
  227. //};
  228. //var res = await _httpHelper.HttpToGetAsync(url, headers).ConfigureAwait(false);
  229. //_logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonRemarksAsync)},响应:{res}");
  230. //var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
  231. //flag = resJToken?["message"]?.ToString().Equals("ok") ?? false;
  232. }
  233. }
  234. catch (Exception ex)
  235. {
  236. _logger.LogError($"{nameof(UpdatePersonRemarksAsync)} 更新个人信息异常:{ex.Message}, {ex.StackTrace}");
  237. }
  238. return flag;
  239. }
  240. /** 取消
  241. public async Task<bool> UpdatePersonInfoCacheAsync(string imei)
  242. {
  243. var flag = false;
  244. try
  245. {
  246. var url = $"{_configService.IotWebApiUrl}Device/UpdatePersonInfoCache?imei={imei}";
  247. List<KeyValuePair<string, string>> headers = new()
  248. {
  249. new KeyValuePair<string, string>("AuthKey", "key1")
  250. };
  251. var res = await _httpHelper.HttpToGetAsync(url, headers).ConfigureAwait(false);
  252. _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonInfoCacheAsync)},响应:{res}");
  253. var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
  254. flag = resJToken?["message"]?.ToString().Equals("ok") ?? false;
  255. }
  256. catch (Exception ex)
  257. {
  258. _logger.LogError($"{nameof(UpdatePersonInfoCacheAsync)} 更新缓存异常:{ex.Message}, {ex.StackTrace}");
  259. }
  260. return flag;
  261. }
  262. */
  263. }
  264. }