|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- using GpsCardGatewayPosition.Model.Cache;
- using GpsCardGatewayPosition.Model.Config;
- using GpsCardGatewayPosition.Model.GatewayEntity;
- using GpsCardGatewayPosition.Service.Biz.Iot;
- using GpsCardGatewayPosition.Service.Cache;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace GpsCardGatewayPosition.Service.Biz
- {
- public class GlobalService
- {
- private readonly ServiceAccessConfig _configServiceAccess;
- private readonly DeviceCacheManager _deviceCacheMgr;
- private readonly DeviceIotOpenService _serviceDeviceIot;
- private readonly ILogger<GlobalService> _logger;
-
- public GlobalService(IOptions<ServiceAccessConfig> optConfigServiceAccess,
- DeviceCacheManager deviceCacheMgr, DeviceIotOpenService serviceDeviceIot, ILogger<GlobalService> logger)
- {
- _configServiceAccess = optConfigServiceAccess.Value;
- _deviceCacheMgr = deviceCacheMgr;
- _serviceDeviceIot = serviceDeviceIot;
- _logger = logger;
- }
-
- #region 推送WIFI LBS转成GPS的信息到IOT平台(设备)
- public async Task PushLocationDataAsync(string sn, decimal lat, decimal lng)
- {
- if (!_configServiceAccess.EnableIotService) return;
-
- //locationDownload指定只能1小时下发一次
- var status = await _deviceCacheMgr.GetPositionStatusCacheAsync(sn);
- _logger.LogInformation($"{nameof(PushLocationDataAsync)}---{sn}----{JsonConvert.SerializeObject(status)}");
- var flag = status?.SendLocationDownloadTime != null && (DateTime.Now.Subtract(status.SendLocationDownloadTime.Value).TotalMinutes < 60);
- _logger.LogInformation($"{nameof(PushLocationDataAsync)}---{sn}---locationDownload指定只能1小时下发一次:{flag}\n status?.SendLocationDownloadTime: {status?.SendLocationDownloadTime}");
- if (flag) return;
-
- try
- {
- //调用GpsCard.Iot项目下的方法
- //var commandApi = new DeviceOpenApi( _configIot,_serviceGuardMq, _logger);
- var pushModel = new PushLocationModel
- {
- parameter = sn,
- Latitude = lat,
- Longitude = lng
- };
- for (int i = 0; i < 2; i++)
- {
- if (await _serviceDeviceIot.InvokeThingServiceAsync(sn, "LocationDownload", JsonConvert.SerializeObject(pushModel)).ConfigureAwait(false))
- {
- _logger.LogInformation($"{nameof(PushLocationDataAsync)}---{sn}---LocationDownload 推送位置");
- break; //如果成功,则跳出循环
- }
- else
- {
- if (i == 0)
- _logger.LogWarning($"推送位置信息失败,准备推送第二次...");
- else
- _logger.LogWarning($"第二次推送位置信息失败,不再推送该条记录");
- }
-
- }
-
- if (status == null) status = new DevicePositionStatus();
- status.SendLocationDownloadTime = DateTime.Now;
- _deviceCacheMgr.SetPositionStatusCache(sn, status);
- }
- catch (Exception ex)
- {
- _logger.LogError($"推送位置信息异常: {ex.Message}, {ex.StackTrace}");
- }
- }
- #endregion
- }
- }
|