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.

374 lines
19KB

  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. /// <param name="isInitRemakers">是否初始化,即清空remakers</param>
  250. /// <returns></returns>
  251. public async Task<bool> UpdatePersonRemarksAsync(string imei, int systolicRefValue, int diastolicRefValue, int systolicIncValue, int diastolicIncValue, bool isInitRemakers=false)
  252. {
  253. var flag = false;
  254. try
  255. {
  256. // 保证实时性,先更新缓存,再更新数据库
  257. var personCache = await _personCacheMgr.GetDeviceGpsPersonCacheObjectBySerialNoAsync(new Guid().ToString(), imei).ConfigureAwait(false);
  258. if (personCache == null)
  259. {
  260. _logger.LogInformation($"{imei} -- Person remarks数据异常,检查缓存和数据库");
  261. }
  262. else if (string.IsNullOrWhiteSpace(personCache["person"]!["remarks"]!.ToString()))
  263. {
  264. var newRemarkData = new
  265. {
  266. imei,
  267. time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  268. commandValue = new
  269. {
  270. systolicCalibrationValue = systolicRefValue, //收缩压标定值,值为0 表示不生效
  271. diastolicCalibrationValue = diastolicRefValue, //舒张压标定值,值为0表示不生效
  272. systolicIncValue, //收缩压显示增量,值为0 表示不生效
  273. diastolicIncValue //舒张压显示增量,值为0 表示不生效
  274. }
  275. };
  276. var newRemarkStr = isInitRemakers ? string.Empty:$"is_blood_press:{JsonConvert.SerializeObject(newRemarkData)}|";
  277. personCache["person"]!["remarks"] = newRemarkStr;
  278. bool cacheFlag = await _personCacheMgr.UpdateDeviceGpsPersonCacheObjectBySerialNoAsync(personCache, imei);
  279. if (cacheFlag)
  280. {
  281. GeneralParam condition = new()
  282. {
  283. Filters = new List<QueryFilterCondition> {
  284. new QueryFilterCondition {
  285. Key=nameof(GpsDevice.Serialno),
  286. Value=imei,
  287. Operator= QueryOperatorEnum.Equal,
  288. ValueType=QueryValueTypeEnum.String
  289. }
  290. },
  291. OrderBys = new List<OrderByCondition> { new OrderByCondition { Key = "serialno", IsDesc = true } }
  292. };
  293. _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonRemarksAsync)}成功,{JsonConvert.SerializeObject(personCache)}");
  294. // 读取数据库
  295. var person = await _gpsPersonApiClient.GetFirstAsync(condition, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
  296. // 更新字段
  297. person!.Remarks = newRemarkStr;
  298. await _gpsPersonApiClient.UpdateAsync(person, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
  299. _logger.LogInformation($"{imei} 更新Person remarks字段|{person.Remarks}");
  300. }
  301. else
  302. {
  303. _logger.LogInformation($"{imei} 更新缓存和数据库{nameof(UpdatePersonRemarksAsync)}失败,{JsonConvert.SerializeObject(personCache)}");
  304. }
  305. flag = cacheFlag;
  306. }
  307. }
  308. catch (Exception ex)
  309. {
  310. _logger.LogError($"{nameof(UpdatePersonRemarksAsync)} {imei}--更新个人信息异常:{ex.Message}, {ex.StackTrace}");
  311. }
  312. return flag;
  313. }
  314. /** 取消
  315. public async Task<bool> UpdatePersonInfoCacheAsync(string imei)
  316. {
  317. var flag = false;
  318. try
  319. {
  320. var url = $"{_configService.IotWebApiUrl}Device/UpdatePersonInfoCache?imei={imei}";
  321. List<KeyValuePair<string, string>> headers = new()
  322. {
  323. new KeyValuePair<string, string>("AuthKey", "key1")
  324. };
  325. var res = await _httpHelper.HttpToGetAsync(url, headers).ConfigureAwait(false);
  326. _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonInfoCacheAsync)},响应:{res}");
  327. var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
  328. flag = resJToken?["message"]?.ToString().Equals("ok") ?? false;
  329. }
  330. catch (Exception ex)
  331. {
  332. _logger.LogError($"{nameof(UpdatePersonInfoCacheAsync)} 更新缓存异常:{ex.Message}, {ex.StackTrace}");
  333. }
  334. return flag;
  335. }
  336. */
  337. }
  338. }