Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

373 lines
18KB

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