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.

IotApiService.cs 25KB

pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
pirms 4 mēnešiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. using Etcdserverpb;
  22. namespace HealthMonitor.Service.Biz
  23. {
  24. public class IotApiService
  25. {
  26. private readonly ServiceConfig _configService;
  27. private readonly ILogger<IotApiService> _logger;
  28. private readonly PersonCacheManager _personCacheMgr;
  29. private readonly DeviceCacheManager _deviceCacheMgr;
  30. private readonly HttpHelper _httpHelper = default!;
  31. private readonly GpsCardAccessorClient<GpsPerson> _gpsPersonApiClient;
  32. public IotApiService(ILogger<IotApiService> logger, HttpHelper httpHelper, DeviceCacheManager deviceCacheMgr, GpsCardAccessorClient<GpsPerson> gpsPersonApiClient, IOptions<ServiceConfig> optConfigService, PersonCacheManager personCacheMgr)
  33. {
  34. _configService = optConfigService.Value;
  35. _httpHelper=httpHelper;
  36. _logger = logger;
  37. _personCacheMgr = personCacheMgr;
  38. _gpsPersonApiClient = gpsPersonApiClient;
  39. _deviceCacheMgr = deviceCacheMgr;
  40. }
  41. #region 平台下发血压标定参数
  42. /// <summary>
  43. /// 平台下发血压标定参数
  44. /// </summary>
  45. /// <param name="bpsCalibrationConfig"></param>
  46. /// <returns></returns>
  47. public async Task<bool> SetBloodPressCalibrationConfigAsync(BloodPressCalibrationConfigModel bpsCalibrationConfig)
  48. {
  49. #if DEBUG
  50. var flag = true;
  51. #else
  52. //systolicCalibrationValue = 0, //收缩压标定值,值为0 表示不生效
  53. //diastolicCalibrationValue 0, //舒张压标定值,值为0表示不生效
  54. //systolicIncValue = 0, //收缩压显示增量,值为0 表示不生效
  55. //diastolicIncValue = 0 //舒张压显示增量,值为0 表示不生效
  56. var flag = false;
  57. try
  58. {
  59. var url = $"{_configService.IotWebApiUrl}Command/SetBloodPressCalibrationConfig";
  60. List<KeyValuePair<string, string>> headers = new()
  61. {
  62. new KeyValuePair<string, string>("AuthKey", "key1")
  63. };
  64. var res = await _httpHelper.HttpToPostAsync(url, bpsCalibrationConfig, headers).ConfigureAwait(false);
  65. _logger.LogInformation($"向{bpsCalibrationConfig.Imei}下发增量值数据:{JsonConvert.SerializeObject(bpsCalibrationConfig)},响应:{res}");
  66. var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
  67. flag= resJToken?["message"]?.ToString().Equals("ok") ?? false;
  68. }
  69. catch (Exception ex)
  70. {
  71. _logger.LogError($"{nameof(SetBloodPressCalibrationConfigAsync)} 下发血压增量值异常:{ex.Message}, {ex.StackTrace}");
  72. }
  73. #endif
  74. return flag;
  75. }
  76. public async Task<BloodPressCalibrationConfigModelReponse> SetBloodPressCalibrationConfig2Async(BloodPressCalibrationConfigModel bpsCalibrationConfig)
  77. {
  78. BloodPressCalibrationConfigModelReponse response = new BloodPressCalibrationConfigModelReponse();
  79. response.Flag = false;
  80. response.Message = string.Empty;
  81. #if DEBUG
  82. //var flag = true;
  83. response.Flag=true;
  84. #else
  85. //systolicCalibrationValue = 0, //收缩压标定值,值为0 表示不生效
  86. //diastolicCalibrationValue 0, //舒张压标定值,值为0表示不生效
  87. //systolicIncValue = 0, //收缩压显示增量,值为0 表示不生效
  88. //diastolicIncValue = 0 //舒张压显示增量,值为0 表示不生效
  89. // var flag = false;
  90. try
  91. {
  92. var url = $"{_configService.IotWebApiUrl}Command/SetBloodPressCalibrationConfig";
  93. List<KeyValuePair<string, string>> headers = new()
  94. {
  95. new KeyValuePair<string, string>("AuthKey", "key1")
  96. };
  97. var res = await _httpHelper.HttpToPostAsync(url, bpsCalibrationConfig, headers).ConfigureAwait(false);
  98. _logger.LogInformation($"向{bpsCalibrationConfig.Imei}下发增量值数据:{JsonConvert.SerializeObject(bpsCalibrationConfig)},响应:{res}");
  99. var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
  100. //response.Flag= resJToken?["message"]?.ToString().Equals("ok") ?? false;
  101. response.Flag = Convert.ToBoolean(resJToken?["succeed"]?.ToString());
  102. if (!response.Flag)
  103. {
  104. response.Message = resJToken?["message"]?.ToString()!;
  105. }
  106. }
  107. catch (Exception ex)
  108. {
  109. _logger.LogError($"{nameof(SetBloodPressCalibrationConfigAsync)} 下发血压增量值异常:{ex.Message}, {ex.StackTrace}");
  110. }
  111. #endif
  112. return response;
  113. }
  114. public async Task<bool> UpdatePersonRemarksAsync(string imei, int systolicRefValue, int diastolicRefValue, int systolicIncValue, int diastolicIncValue, string remarks = "is_blood_press")
  115. {
  116. var flag = false;
  117. try
  118. {
  119. // 保证实时性,先更新缓存,再更新数据库
  120. var personCache = await _personCacheMgr.GetDeviceGpsPersonCacheObjectBySerialNoAsync(new Guid().ToString(), imei).ConfigureAwait(false);
  121. if (personCache == null)
  122. {
  123. _logger.LogInformation($"{imei} -- Person remarks数据异常,检查缓存和数据库");
  124. }
  125. else
  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 表示不生效
  136. diastolicIncValue //舒张压显示增量,值为0 表示不生效
  137. }
  138. };
  139. var newRemarkStr = $"{remarks}:{JsonConvert.SerializeObject(newRemarkData)}|";
  140. personCache["person"]!["remarks"] = newRemarkStr;
  141. bool cacheFlag = await _personCacheMgr.UpdateDeviceGpsPersonCacheObjectBySerialNoAsync(personCache, imei);
  142. if (cacheFlag)
  143. {
  144. GeneralParam condition = new()
  145. {
  146. Filters = new List<QueryFilterCondition> {
  147. new QueryFilterCondition {
  148. Key=nameof(GpsDevice.Serialno),
  149. Value=imei,
  150. Operator= QueryOperatorEnum.Equal,
  151. ValueType=QueryValueTypeEnum.String
  152. }
  153. },
  154. OrderBys = new List<OrderByCondition> { new OrderByCondition { Key = "serialno", IsDesc = true } }
  155. };
  156. _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonRemarksAsync)}成功,{JsonConvert.SerializeObject(personCache)}");
  157. // 读取数据库
  158. var person = await _gpsPersonApiClient.GetFirstAsync(condition, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
  159. // 更新字段
  160. person!.Remarks = newRemarkStr;
  161. await _gpsPersonApiClient.UpdateAsync(person, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
  162. _logger.LogInformation($"{imei} 更新Person remarks字段|{person.Remarks}");
  163. }
  164. else
  165. {
  166. _logger.LogInformation($"{imei} 更新缓存和数据库{nameof(UpdatePersonRemarksAsync)}失败,{JsonConvert.SerializeObject(personCache)}");
  167. }
  168. flag = cacheFlag;
  169. }
  170. // else if (string.IsNullOrWhiteSpace(personCache["person"]!["remarks"]!.ToString()))
  171. //else if (personCache?["person"]!["remarks"]!.ToString()!=null)
  172. //{
  173. // var newRemarkData = new
  174. // {
  175. // imei,
  176. // time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  177. // commandValue = new
  178. // {
  179. // systolicCalibrationValue = systolicRefValue, //收缩压标定值,值为0 表示不生效
  180. // diastolicCalibrationValue = diastolicRefValue, //舒张压标定值,值为0表示不生效
  181. // systolicIncValue, //收缩压显示增量,值为0 表示不生效
  182. // diastolicIncValue //舒张压显示增量,值为0 表示不生效
  183. // }
  184. // };
  185. // var newRemarkStr = $"{remarks}:{JsonConvert.SerializeObject(newRemarkData)}|";
  186. // personCache["person"]!["remarks"] = newRemarkStr;
  187. // bool cacheFlag = await _personCacheMgr.UpdateDeviceGpsPersonCacheObjectBySerialNoAsync(personCache, imei);
  188. // if (cacheFlag)
  189. // {
  190. // GeneralParam condition = new()
  191. // {
  192. // Filters = new List<QueryFilterCondition> {
  193. // new QueryFilterCondition {
  194. // Key=nameof(GpsDevice.Serialno),
  195. // Value=imei,
  196. // Operator= QueryOperatorEnum.Equal,
  197. // ValueType=QueryValueTypeEnum.String
  198. // }
  199. // },
  200. // OrderBys = new List<OrderByCondition> { new OrderByCondition { Key = "serialno", IsDesc = true } }
  201. // };
  202. // _logger.LogInformation($"{imei} 更新缓存{nameof(UpdatePersonRemarksAsync)}成功,{JsonConvert.SerializeObject(personCache)}");
  203. // // 读取数据库
  204. // var person = await _gpsPersonApiClient.GetFirstAsync(condition, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
  205. // // 更新字段
  206. // person!.Remarks = newRemarkStr;
  207. // await _gpsPersonApiClient.UpdateAsync(person, new RequestHeader() { RequestId = $"{imei}" }).ConfigureAwait(false);
  208. // _logger.LogInformation($"{imei} 更新Person remarks字段|{person.Remarks}");
  209. // }
  210. // else
  211. // {
  212. // _logger.LogInformation($"{imei} 更新缓存和数据库{nameof(UpdatePersonRemarksAsync)}失败,{JsonConvert.SerializeObject(personCache)}");
  213. // }
  214. // flag = cacheFlag;
  215. //}
  216. }
  217. catch (Exception ex)
  218. {
  219. _logger.LogError($"{nameof(UpdatePersonRemarksAsync)} {imei}--更新个人信息异常:{ex.Message}, {ex.StackTrace}");
  220. }
  221. return flag;
  222. }
  223. #endregion
  224. #region 平台下发胎心监测参数
  225. /// <summary>
  226. /// 胎心设置
  227. /// </summary>
  228. /// <param name="serialno"></param>
  229. /// <param name="modeStatus"></param>
  230. /// <param name="maxValue"></param>
  231. /// <param name="minValue"></param>
  232. /// <returns></returns>
  233. public async Task<bool> SetFetalConfig(string serialno, int modeStatus=0, int maxValue=0, int minValue = 0)
  234. {
  235. try
  236. {
  237. #region 读取缓存
  238. // db7.HashGet("TELPO#GPSDEVICE_WATCH_CONFIG_HASH","861281060086083_0067")
  239. var watchConfig = await _deviceCacheMgr.GetGpsDeviceWatchConfigCacheObjectBySerialNoAsync(serialno, "0067");
  240. if (watchConfig == null)
  241. {
  242. return false;
  243. }
  244. #endregion
  245. #region 获取B端 Token
  246. string tokenAuthData = await getAccessToken2(serialno).ConfigureAwait(false);
  247. if (tokenAuthData == null)
  248. {
  249. return false;
  250. }
  251. #endregion
  252. _logger.LogInformation($" SetFetalConfig Token TelpoManufactorId: {tokenAuthData}");
  253. #region 发送到B端
  254. List<KeyValuePair<string, string>> headers = new()
  255. {
  256. new KeyValuePair<string, string>("AccessToken", tokenAuthData)
  257. };
  258. //var data = new
  259. //{
  260. // imeis = new string[] { serialno },
  261. // enabled = (int)watchConfig["enabled"]!,
  262. // interval= (int)watchConfig["interval"]!,
  263. // triggerHighFreqHigh = maxValue == 0 ? (int)watchConfig["triggerHighFreqHigh"]! : maxValue,
  264. // triggerHighFreqLow = minValue == 0 ? (int)watchConfig["triggerHighFreqLow"]! : minValue,
  265. // highFreqSampleTimes = (int)watchConfig["highFreqSampleTimes"]!,
  266. // highFreqSampleInterval = (int)watchConfig["highFreqSampleInterval"]!,
  267. // stopHighFreqSampleCount = (int)watchConfig["stopHighFreqSampleCount"]!,
  268. // mode = modeStatus,
  269. // edoc = watchConfig["EDOC"]!,
  270. // vibrateEnabled = (int)watchConfig["vibrateEnabled"]!,
  271. // lcdEnabled = (int)watchConfig["lcdEnabled"]!,
  272. // upperAlarmThreshold = (int)watchConfig["upperAlarmThreshold"]!,
  273. // lowerAlarmThreshold = (int)watchConfig["lowerAlarmThreshold"]!
  274. //};
  275. watchConfig["mode"] = modeStatus;
  276. watchConfig["triggerHighFreqHigh"] = maxValue == 0 ? (int)watchConfig["triggerHighFreqHigh"]! : maxValue;
  277. watchConfig["triggerHighFreqLow"] = minValue == 0 ? (int)watchConfig["triggerHighFreqLow"]! : minValue;
  278. var data = watchConfig;
  279. var setUrl = $"{_configService.IotCore}/SetFetalConfig";
  280. _logger.LogInformation($"{setUrl} 请求 {JsonConvert.SerializeObject(JsonConvert.SerializeObject(data))}");
  281. var res = await _httpHelper.HttpToPostAsync(setUrl, data, headers).ConfigureAwait(false);
  282. _logger.LogInformation($"{setUrl} 响应 {res}");
  283. var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
  284. return resJToken?["message"]?.ToString().Equals("ok") ?? false;
  285. //response.Flag = resJToken?["message"]?.ToString().Equals("ok") ?? false;
  286. //response.Flag = Convert.ToBoolean(resJToken?["succeed"]?.ToString());
  287. //if (!response.Flag)
  288. //{
  289. // response.Message = resJToken?["message"]?.ToString()!;
  290. //}
  291. #endregion
  292. }
  293. catch (Exception ex)
  294. {
  295. _logger.LogError($"{nameof(SetFetalConfig)} 下发胎心检测参数异常:{ex.Message}, {ex.StackTrace}");
  296. return false;
  297. }
  298. }
  299. public async Task<bool> SetFetalConfigMockAsync(string serialno, int modeStatus = 0, int triggerHighFreqHigh = 0, int triggerHighFreqLow = 0,
  300. int highFreqSampleTimes=0,int highFreqSampleInterval=0,int stopHighFreqSampleCount=0)
  301. {
  302. try
  303. {
  304. #region 读取缓存
  305. // db7.HashGet("TELPO#GPSDEVICE_WATCH_CONFIG_HASH","861281060086083_0067")
  306. var watchConfig = await _deviceCacheMgr.GetGpsDeviceWatchConfigCacheObjectBySerialNoAsync(serialno, "0067");
  307. if (watchConfig == null)
  308. {
  309. return false;
  310. }
  311. #endregion
  312. #region 获取B端 Token
  313. string tokenAuthData = await getAccessToken2(serialno).ConfigureAwait(false);
  314. if (tokenAuthData == null)
  315. {
  316. return false;
  317. }
  318. #endregion
  319. _logger.LogInformation($" SetFetalConfig Token TelpoManufactorId: {tokenAuthData}");
  320. #region 发送到B端
  321. List<KeyValuePair<string, string>> headers = new()
  322. {
  323. new KeyValuePair<string, string>("AccessToken", tokenAuthData)
  324. };
  325. watchConfig["mode"] = modeStatus;
  326. watchConfig["highFreqSampleTimes"]= highFreqSampleTimes == 0 ? (int)watchConfig["highFreqSampleTimes"]! : highFreqSampleTimes;
  327. watchConfig["highFreqSampleInterval"] = highFreqSampleInterval == 0 ? (int)watchConfig["highFreqSampleInterval"]! : highFreqSampleInterval;
  328. watchConfig["stopHighFreqSampleCount"] = stopHighFreqSampleCount == 0 ? (int)watchConfig["stopHighFreqSampleCount"]! : stopHighFreqSampleCount;
  329. watchConfig["triggerHighFreqHigh"] = triggerHighFreqHigh == 0 ? (int)watchConfig["triggerHighFreqHigh"]! : triggerHighFreqHigh;
  330. watchConfig["triggerHighFreqLow"] = triggerHighFreqLow == 0 ? (int)watchConfig["triggerHighFreqLow"]! : triggerHighFreqLow;
  331. var data = watchConfig;
  332. var setUrl = $"{_configService.IotCore}/SetFetalConfig";
  333. _logger.LogInformation($"{setUrl} 请求 {JsonConvert.SerializeObject(JsonConvert.SerializeObject(data))}");
  334. var res = await _httpHelper.HttpToPostAsync(setUrl, data, headers).ConfigureAwait(false);
  335. _logger.LogInformation($"{setUrl} 响应 {res}");
  336. var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
  337. return resJToken?["message"]?.ToString().Equals("ok") ?? false;
  338. //response.Flag = resJToken?["message"]?.ToString().Equals("ok") ?? false;
  339. //response.Flag = Convert.ToBoolean(resJToken?["succeed"]?.ToString());
  340. //if (!response.Flag)
  341. //{
  342. // response.Message = resJToken?["message"]?.ToString()!;
  343. //}
  344. #endregion
  345. }
  346. catch (Exception ex)
  347. {
  348. _logger.LogError($"{nameof(SetFetalConfig)} 下发胎心检测参数异常:{ex.Message}, {ex.StackTrace}");
  349. return false;
  350. }
  351. }
  352. /// <summary>
  353. /// 下发胎心数据
  354. /// </summary>
  355. /// <param name="serialno"></param>
  356. /// <param name="fhr">心率值</param>
  357. /// <param name="sampleTime">检测时间(时间戳)</param>
  358. /// <param name="isAbnormal">0 //是否异常 0 表示正常;1表示偏高;2表示偏低</param>
  359. /// <returns></returns>
  360. public async Task<bool> SetFetalHeartRateConfig(string serialno, int fhr, string sampleTime, int isAbnormal)
  361. {
  362. try
  363. {
  364. #region 读取缓存
  365. // db7.HashGet("TELPO#GPSDEVICE_WATCH_CONFIG_HASH","861281060086083_0067")
  366. var watchConfig = await _deviceCacheMgr.GetGpsDeviceWatchConfigCacheObjectBySerialNoAsync(serialno, "0067");
  367. if (watchConfig == null)
  368. {
  369. return false;
  370. }
  371. #endregion
  372. #region 获取B端 Token
  373. string tokenAuthData = await getAccessToken2(serialno).ConfigureAwait(false);
  374. if (tokenAuthData == null)
  375. {
  376. return false;
  377. }
  378. #endregion
  379. #region 下发数据
  380. List<KeyValuePair<string, string>> headers = new()
  381. {
  382. new KeyValuePair<string, string>("AccessToken", tokenAuthData)
  383. };
  384. _logger.LogInformation($" SetFetalHeartRateConfig Token TelpoManufactorId: {tokenAuthData}");
  385. var data = new
  386. {
  387. imei = serialno,
  388. heartValue = fhr,
  389. sampleTime = sampleTime.Length > 10 ? sampleTime.Substring(0, 10) : sampleTime,
  390. isAbnormal
  391. };
  392. var setUrl = $"{_configService.IotCore}/SetFetalHeartRateConfig";
  393. _logger.LogInformation($"{setUrl} 请求 {JsonConvert.SerializeObject(JsonConvert.SerializeObject(data))}");
  394. var res = await _httpHelper.HttpToPostAsync(setUrl, data, headers).ConfigureAwait(false);
  395. _logger.LogInformation($"{setUrl} 响应 {res}");
  396. var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
  397. return resJToken?["message"]?.ToString().Equals("ok") ?? false;
  398. #endregion
  399. }
  400. catch (Exception ex)
  401. {
  402. _logger.LogError($"{nameof(SetFetalHeartRateConfig)} 下发胎心数据异常:{ex.Message}, {ex.StackTrace}");
  403. return false;
  404. }
  405. }
  406. public async Task<bool> SetFetalMovementConfig(string serialno,int fm, string sampleTime, int isAbnormal)
  407. {
  408. try
  409. {
  410. #region 读取缓存
  411. // db7.HashGet("TELPO#GPSDEVICE_WATCH_CONFIG_HASH","861281060086083_0067")
  412. var watchConfig = await _deviceCacheMgr.GetGpsDeviceWatchConfigCacheObjectBySerialNoAsync(serialno, "0067");
  413. if (watchConfig == null)
  414. {
  415. return false;
  416. }
  417. #endregion
  418. #region 获取B端 Token
  419. string tokenAuthData = await getAccessToken2(serialno).ConfigureAwait(false);
  420. if (tokenAuthData == null)
  421. {
  422. return false;
  423. }
  424. #endregion
  425. #region 下发数据
  426. List<KeyValuePair<string, string>> headers = new()
  427. {
  428. new KeyValuePair<string, string>("AccessToken", tokenAuthData)
  429. };
  430. _logger.LogInformation($" SetFetalHeartRateConfig Token TelpoManufactorId: {tokenAuthData}");
  431. var data = new
  432. {
  433. imei = serialno,
  434. movementValue = fm,
  435. sampleTime = sampleTime.Length > 10 ? sampleTime.Substring(0, 10) : sampleTime,
  436. isAbnormal
  437. };
  438. var setUrl = $"{_configService.IotCore}/SetFetalMovementConfig";
  439. _logger.LogInformation($"{setUrl} 请求 {JsonConvert.SerializeObject(JsonConvert.SerializeObject(data))}");
  440. var res = await _httpHelper.HttpToPostAsync(setUrl, data, headers).ConfigureAwait(false);
  441. _logger.LogInformation($"{setUrl} 响应 {res}");
  442. var resJToken = JsonConvert.DeserializeObject(res ?? string.Empty) as JToken;
  443. return resJToken?["message"]?.ToString().Equals("ok") ?? false;
  444. #endregion
  445. }
  446. catch (Exception ex)
  447. {
  448. _logger.LogError($"{nameof(SetFetalMovementConfig)} 下发胎动数据异常:{ex.Message}, {ex.StackTrace}");
  449. return false;
  450. }
  451. }
  452. private async Task<string> getAccessToken2(string serialno)
  453. {
  454. var getTokenUrl = $"{_configService.IotAuth}/getAccessToken2";
  455. var tokenReq = new
  456. {
  457. manufactorId = "7c7c38cb-d045-41d8-b3d0-fcaaa84a8f02",
  458. imei = serialno
  459. };
  460. _logger.LogInformation($"{getTokenUrl} 请求 {JsonConvert.SerializeObject(tokenReq)}");
  461. var resToken = await _httpHelper.HttpToPostAsync(getTokenUrl, tokenReq).ConfigureAwait(false);
  462. _logger.LogInformation($"{getTokenUrl} 响应 {resToken}");
  463. var tokenAuth = JsonConvert.DeserializeObject(resToken ?? string.Empty) as JToken;
  464. var tokenAuthData = tokenAuth?["data"]?.ToString() ?? string.Empty;
  465. return tokenAuthData;
  466. }
  467. #endregion
  468. }
  469. }