您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

84 行
3.6KB

  1. using GpsCardGatewayPosition.Model.Cache;
  2. using GpsCardGatewayPosition.Model.Config;
  3. using GpsCardGatewayPosition.Model.GatewayEntity;
  4. using GpsCardGatewayPosition.Service.Biz.Iot;
  5. using GpsCardGatewayPosition.Service.Cache;
  6. using Microsoft.Extensions.Logging;
  7. using Microsoft.Extensions.Options;
  8. using Newtonsoft.Json;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace GpsCardGatewayPosition.Service.Biz
  15. {
  16. public class GlobalService
  17. {
  18. private readonly ServiceAccessConfig _configServiceAccess;
  19. private readonly DeviceCacheManager _deviceCacheMgr;
  20. private readonly DeviceIotOpenService _serviceDeviceIot;
  21. private readonly ILogger<GlobalService> _logger;
  22. public GlobalService(IOptions<ServiceAccessConfig> optConfigServiceAccess,
  23. DeviceCacheManager deviceCacheMgr, DeviceIotOpenService serviceDeviceIot, ILogger<GlobalService> logger)
  24. {
  25. _configServiceAccess = optConfigServiceAccess.Value;
  26. _deviceCacheMgr = deviceCacheMgr;
  27. _serviceDeviceIot = serviceDeviceIot;
  28. _logger = logger;
  29. }
  30. #region 推送WIFI LBS转成GPS的信息到IOT平台(设备)
  31. public async Task PushLocationDataAsync(string sn, decimal lat, decimal lng)
  32. {
  33. if (!_configServiceAccess.EnableIotService) return;
  34. //locationDownload指定只能1小时下发一次
  35. var status = await _deviceCacheMgr.GetPositionStatusCacheAsync(sn);
  36. _logger.LogInformation($"{nameof(PushLocationDataAsync)}---{sn}----{JsonConvert.SerializeObject(status)}");
  37. var flag = status?.SendLocationDownloadTime != null && (DateTime.Now.Subtract(status.SendLocationDownloadTime.Value).TotalMinutes < 60);
  38. _logger.LogInformation($"{nameof(PushLocationDataAsync)}---{sn}---locationDownload指定只能1小时下发一次:{flag}\n status?.SendLocationDownloadTime: {status?.SendLocationDownloadTime}");
  39. if (flag) return;
  40. try
  41. {
  42. //调用GpsCard.Iot项目下的方法
  43. //var commandApi = new DeviceOpenApi( _configIot,_serviceGuardMq, _logger);
  44. var pushModel = new PushLocationModel
  45. {
  46. parameter = sn,
  47. Latitude = lat,
  48. Longitude = lng
  49. };
  50. for (int i = 0; i < 2; i++)
  51. {
  52. if (await _serviceDeviceIot.InvokeThingServiceAsync(sn, "LocationDownload", JsonConvert.SerializeObject(pushModel)).ConfigureAwait(false))
  53. {
  54. _logger.LogInformation($"{nameof(PushLocationDataAsync)}---{sn}---LocationDownload 推送位置");
  55. break; //如果成功,则跳出循环
  56. }
  57. else
  58. {
  59. if (i == 0)
  60. _logger.LogWarning($"推送位置信息失败,准备推送第二次...");
  61. else
  62. _logger.LogWarning($"第二次推送位置信息失败,不再推送该条记录");
  63. }
  64. }
  65. if (status == null) status = new DevicePositionStatus();
  66. status.SendLocationDownloadTime = DateTime.Now;
  67. _deviceCacheMgr.SetPositionStatusCache(sn, status);
  68. }
  69. catch (Exception ex)
  70. {
  71. _logger.LogError($"推送位置信息异常: {ex.Message}, {ex.StackTrace}");
  72. }
  73. }
  74. #endregion
  75. }
  76. }