|
- using AutoMapper.Internal;
- using GpsCardGatewayPosition.Common;
- using GpsCardGatewayPosition.Common.Helper;
- using GpsCardGatewayPosition.Model.Config;
- using GpsCardGatewayPosition.Model.Context;
- using GpsCardGatewayPosition.Model.Enum;
- using GpsCardGatewayPosition.Model.IoT;
- using GpsCardGatewayPosition.Model.Templates;
- using GpsCardGatewayPosition.Service.Biz;
- using GpsCardGatewayPosition.Service.Biz.Health;
- using GpsCardGatewayPosition.Service.Biz.Location;
- using GpsCardGatewayPosition.Service.Biz.Location.Dto;
- using GpsCardGatewayPosition.Service.Biz.Location.Dto.Gaode;
- using GpsCardGatewayPosition.Service.Biz.Location.Dto.Wayz;
- using GpsCardGatewayPosition.Service.Biz.Pay;
- using GpsCardGatewayPosition.Service.Biz.Sos;
- using GpsCardGatewayPosition.Service.Cache;
- using GpsCardGatewayPosition.Service.MqProducer;
- using GpsCardGatewayPosition.Service.MqProducer.Model;
- using GpsCardGatewayPosition.Service.Resolver.Interface;
- using GpsCardGatewayPosition.Service.Resolver.Property.Dto;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using TelpoDataService.Util.Entities.GpsCard;
- using TelpoDataService.Util.Entities.GpsLocationHistory;
- using static GpsCardGatewayPosition.Service.Biz.Location.Dto.Wayz.WayzRequest;
-
- namespace GpsCardGatewayPosition.Service.Resolver.Property
- {
- public class WifiPlus2PositionResolver : IPropertyResolver
- {
- private readonly ILogger<WifiPlus2PositionResolver> _logger;
- private readonly WifiParamConfig _configWifiParam;
-
- private readonly DeviceCacheManager _deviceCacheMgr;
- private readonly GaodeService _serviceGaode;
- private readonly WayzService _serviceWayz;
-
- private readonly MqProcessLogic _serviceMqProcess;
- private readonly GlobalService _serviceGlobal;
- private readonly PayLogic _servicePay;
- private readonly SosLogic _serviceSos;
- private readonly HealthLogic _serviceHealth;
- private readonly LocationLogic _serviceLocation;
-
-
- private AsyncLocal<string> _messageId = new AsyncLocal<string>();
- private AsyncLocal<PropertyModel> _package = new AsyncLocal<PropertyModel>();
- private AsyncLocal<PropertyItemModel<List<EachWifiPlus2Model>>> _propertyValue = new AsyncLocal<PropertyItemModel<List<EachWifiPlus2Model>>>();
- private AsyncLocal<PackageMsgModel> _packageMsgModel = new AsyncLocal<PackageMsgModel>();
-
- public WifiPlus2PositionResolver(ILogger<WifiPlus2PositionResolver> logger, IOptions<WifiParamConfig> optConfigWifiParam,
- MqProcessLogic serviceMqProcess, GlobalService serviceGlobal, PayLogic servicePay, HealthLogic serviceHealth, LocationLogic serviceLocation,
- WayzService serviceWayz, SosLogic serviceSos, GaodeService serviceGaode, DeviceCacheManager deviceCacheMgr
- )
- {
- _serviceGlobal = serviceGlobal;
- _servicePay = servicePay;
- _serviceHealth = serviceHealth;
- _serviceLocation = serviceLocation;
- _serviceSos = serviceSos;
- _serviceMqProcess = serviceMqProcess;
-
- _serviceGaode = serviceGaode;
- _serviceWayz = serviceWayz;
- _deviceCacheMgr = deviceCacheMgr;
- _logger = logger;
- _configWifiParam = optConfigWifiParam.Value;
- }
-
- public void SetResolveInfo(PackageMsgModel msg)
- {
- _messageId.Value = msg.MessageId;
- _package.Value = ((JToken)msg.TopicInfo).ToObject<PropertyModel>()!;
- _propertyValue.Value = ((JToken)msg.DetailData).ToObject<PropertyItemModel<List<EachWifiPlus2Model>>>()!;
- }
- public override string ToString()
- {
- return $"{nameof(WifiPlus2PositionResolver)}[{_messageId.Value}]";
- }
- public async Task ExecuteMessageAsync()
- {
- var package = _package.Value;
- var serialno = package?.DeviceName; //设备序列号
- var messageId = _messageId.Value;
- var propertyValue = _propertyValue.Value;
-
- var device = await _deviceCacheMgr.GetDeviceBySerialNoAsync(messageId!, serialno!).ConfigureAwait(false);
- if (device == null)
- {
- _logger.LogError($"非法设备:{serialno}");
- return;
- }
- //var device = new GpsDevice()
- //{
- // DeviceId = serialno,
- //};
-
-
-
- DateTime now = DateTime.Now;
- LocationType type = LocationType.LBS;
- foreach (var item in propertyValue!.Value)
- {
- try
- {
- var datetime = Utils.ConvertToLocalDateTime(item.Datetime);
- var mmac = item.Mmac;
- var macs = item.Macs;
- var imei = item.Imei;
- var smac = item.Smac;
- var imsi = item.Imsi;
- var nearbts = item.NearBts;
- var network = item.Network;
- var cdma = item.Cdma;
- var bts = item.Bts;
- var IDType = item.IdType;
- var IDNumber = item.IdNumber;
- var steps = SafeType.SafeInt(item.Steps); //2分钟内的步数值增量
- bool isRedressed = false;
-
- if (imei.Contains("unkonw") || bts.Length == 0) // || nearbts.Length == 0 //紧急修改,应急用
- {
- continue;
- }
-
- string cityCode = null;
- string addr = null;
- string province = null;
- string city = null;
- string district = null;
- decimal olat = 0;
- decimal olng = 0;
- decimal baiduLat = 0;
- decimal baiduLng = 0;
- decimal gaodeLat = 0;
- decimal gaodeLng = 0;
- int radius = 0;
- decimal[] latLngWifi = new decimal[4];
- decimal[] latLngLbs = new decimal[4];
- string hashParam = Guid.NewGuid().ToString();
- //bool isBtsOnly = false; //标识第二次主基站lbs查询
- //bool isBtsOnly = true; //取消标识第二次主基站lbs查询
- var WayzFullAddress = string.Empty;
-
- #region 定位解析
- //var wifiRequest = new GaodeWifiRequest
- //{
- // //Imei = serialno,
- // Macs = macs,
- // Mmac = mmac
- //};
- //var joWifi = await _serviceGaode.GetGaodeWifiAddressAsync(serialno, wifiRequest).ConfigureAwait(false);
-
- //var lbsRequest = new GaodeLbsRequest
- //{
- // Bts = bts,
- // Cdma = 0,
- // //Imei = serialno,
- // Imsi = imsi,
-
- // // NearBts = nearbts,
- // // 2022/11/21
- // // 所有的基站定位都采用只使用主基站进行获取高德定位数据。不用nearbts的附近基站定位
- // NearBts = string.Empty,
- // Smac = ""
- //};
- //var joLbs = await _serviceGaode.GetGaodeLbsAddressAsync(serialno, lbsRequest).ConfigureAwait(false);
-
- #region 维智地图请求
- // wayz wifi
- var wifiWayzItems = new List<WayzRequest.WifiItem>();
- // 已经接入的wifi mmac:80:8f:1d:bf:45:b1,-70,
- if (mmac.Split(',').Length > 2)
- {
- wifiWayzItems.Add(new WifiItem
- {
- Connected = true,
- SignalStrength = Math.Abs(int.Parse(mmac.Split(',')[1])),
- MacAddress = mmac.Split(',')[0]
- });
- }
- // wifi 列表 macs:08:9b:4b:b7:b3:5d,-70,|20:6b:e7:7e:4d:30,-70,|8c:a6:df:74:33:15,-74,|74:7d:24:0f:ff:60,-74,|78:44:76:0a:3c:74,-75,|02:0e:5e:ad:5d:d1,-78,|10:a4:be:f6:12:bf,-78,
-
- if (macs.Split('|').Length > 1)
- {
- macs.Split('|').ForAll(mac =>
- {
- wifiWayzItems.Add(new WifiItem
- {
- Connected = false,
- SignalStrength = Math.Abs(int.Parse(mac.Split(',')[1])),
- MacAddress = mac.Split(',')[0]
- });
- });
- }
- var wifiWayzRequest = new WayzWifiRequest
- {
- Location = new LocationDetail
- {
- Wifis = wifiWayzItems
- },
- Asset = new AssetDevice
- {
- Id = serialno,
- ImeiMd5 = serialno,
- UniqueId = device.DeviceId
- }
- };
- //wifiWayzRequest.Timestamp = Utils.ConvertToSTimeStamp(Utils.ConvertToLocalDateTime(item.Datetime));
- //wifiWayzRequest.Asset.Id = serialno;
- ////wifiWayzRequest.Asset.ImeiMd5 = Convert.ToBase64String(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(serialno)));
- //wifiWayzRequest.Asset.ImeiMd5 = serialno;
- //wifiWayzRequest.Asset.UniqueId = device.DeviceId;
- var joWayzWifi = await _serviceWayz.GetWayzWifiAddressAsync(serialno, wifiWayzRequest).ConfigureAwait(false);
-
- // wayz lbs
- var cellularWayzItems = new List<CellularItem>();
- // 非cdma:格式为mcc,mnc,lac,cellid,signal。
- if (cdma == 0)
- {
- var mcc = bts.Split(',')[0];
- var mnc = bts.Split(',')[1];
- var lac = bts.Split(',')[2];
- var cellId = bts.Split(',')[3];
- cellularWayzItems.Add(new CellularItem
- {
- CellId = int.Parse(cellId),
- RadioType = network,
- MobileCountryCode = int.Parse(mcc),
- MobileNetworkCode = int.Parse(mnc),
- LocationAreaCode = int.Parse(lac),
- });
-
- }
- // cdma:格式为sid,nid,bid,lon,lat,signal
- else
- {
- // sid
- var mnc = bts.Split(',')[0];
- // bid
- var cellId = bts.Split(',')[2];
- // nid
- var lac = bts.Split(',')[1];
-
- cellularWayzItems.Add(new CellularItem
- {
- CellId = int.Parse(cellId),
- RadioType = network,
- //MobileCountryCode = int.Parse(mcc),
- MobileNetworkCode = int.Parse(mnc),
- LocationAreaCode = int.Parse(lac),
- });
-
- }
-
- var lbsWayzRequest = new WayzLbsRequest
- {
- Location = new LocationDetail
- {
- Cellulars = cellularWayzItems
- },
- Asset = new AssetDevice
- {
- Id = serialno,
- ImeiMd5 = serialno,
- UniqueId = device.DeviceId
- }
-
- };
- //lbsWayzRequest.Timestamp = Utils.ConvertToSTimeStamp(Utils.ConvertToLocalDateTime(item.Datetime));
- //lbsWayzRequest.Asset.Id = serialno;
- ////lbsWayzRequest.Asset.ImeiMd5 = Convert.ToBase64String(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(serialno)));
- //lbsWayzRequest.Asset.ImeiMd5 = serialno;
- //lbsWayzRequest.Asset.UniqueId = device.DeviceId;
- var joWayzLbs = await _serviceWayz.GetWayzLbsAddressAsync(serialno, lbsWayzRequest).ConfigureAwait(false);
-
- #endregion
- // 只用通过主机站数据查询lbs
- //if (!joWayzLbs.Flag)
- //{
- // //通过主机站数据查询lbs
- // //lbsRequest = new GaodeLbsRequest
- // //{
- // // Bts = bts,
- // // Cdma = 0,
- // // Imsi = imsi,
- // // Smac = "",
- // //};
- // //joLbs = await _serviceGaode.GetGaodeLbsAddressAsync(serialno, lbsRequest).ConfigureAwait(false);
- // joWayzLbs = await _serviceWayz.GetWayzLbsAddressAsync(serialno, lbsWayzRequest).ConfigureAwait(false);
- // isBtsOnly = true;
- // WayzFullAddress = joWayzLbs.FullAddress;
-
- // }
-
- if (joWayzLbs.Flag)
- {
- cityCode = joWayzLbs.AdCode;
- addr = joWayzLbs.Address;
- province = joWayzLbs.Province;
- city = joWayzLbs.City;
- district = joWayzLbs.District;
- radius = joWayzLbs.Accuracy;
-
- //lbs对应经纬度
- latLngLbs = GeoConvert.ConvertGoogleBaiduLatLng(joWayzLbs.Lat, joWayzLbs.Lon);
- olat = joWayzLbs.Lat;
- olng = joWayzLbs.Lon;
- baiduLng = latLngLbs[3];
- baiduLat = latLngLbs[2];
- gaodeLng = latLngLbs[1];
- gaodeLat = latLngLbs[0];
- hashParam = joWayzLbs.HashParam;
- WayzFullAddress = joWayzLbs.FullAddress;
- }
-
-
- if (joWayzWifi.Flag) //wifi定位比lbs精准,所以优先采用wifi定位
- {
- //wifi对应经纬度
- latLngWifi = GeoConvert.ConvertGoogleBaiduLatLng(joWayzWifi.Lat, joWayzWifi.Lon);
-
- //计算wifi和lbs定位结果之间的直线距离
- //double distance_1st = 0, distance_2nd = 0;
- double distance_1st = 0;
- //var distance = distance_1st = joWayzLbs.Flag ? GeoUtils.GetDistance2((double)latLngWifi[1], (double)latLngWifi[0], (double)latLngLbs[1], (double)latLngLbs[0]) : 0;
- var distance = distance_1st = joWayzLbs.Flag ? GeoUtils.GetDistance2((double)joWayzWifi.Lon, (double)joWayzWifi.Lat, (double)joWayzLbs.Lon, (double)joWayzLbs.Lat) : 0;
- if (distance >= 1 * 10000)
- {
- _logger.LogWarning($"lbs漂移较大 [{joWayzLbs.AdCode}] [{joWayzLbs.Address}]");
-
- //if (!isBtsOnly)
- //{
- // ////通过主机站数据查询lbs
- // //lbsRequest = new GaodeLbsRequest
- // //{
- // // Bts = bts,
- // // Cdma = 0,
- // // Imsi = imsi,
- // // Smac = "",
- // //};
- // //joLbs = await _serviceGaode.GetGaodeLbsAddressAsync(serialno, lbsRequest).ConfigureAwait(false);
- // joWayzLbs = await _serviceWayz.GetWayzLbsAddressAsync(serialno, lbsWayzRequest).ConfigureAwait(false);
- // //lbs对应经纬度
- // latLngLbs = GeoConvert.ConvertGoogleBaiduLatLng(joWayzLbs.Lat, joWayzLbs.Lon);
- // //计算wifi和lbs定位结果之间的直线距离
- // //distance = distance_2nd = joWayzLbs.Flag ? GeoUtils.GetDistance2((double)latLngWifi[1], (double)latLngWifi[0], (double)latLngLbs[1], (double)latLngLbs[0]) : 0;
- // distance = distance_2nd = joWayzLbs.Flag ? GeoUtils.GetDistance2((double)joWayzWifi.Lon, (double)joWayzWifi.Lat, (double)joWayzLbs.Lon, (double)joWayzLbs.Lat) : 0;
- // isBtsOnly = true;
- // WayzFullAddress = joWayzLbs.FullAddress;
- // }
- }
-
- //2020/12/17山西设备(862622050677507) lbs直径距离大于1公里
- //讨论结果定为10公里
- if (distance < 1 * 10000) //两种定位方式的直线距离小于10km,否则抛弃定位
- {
- //cityCode = joWifi.AdCode;
- //addr = joWifi.Address;
- //province = joWifi.Province;
- //city = joWifi.City;
- //district = joWifi.District;
- //radius = joWifi.Accuracy;
-
- //olat = joWifi.Lat;
- //olng = joWifi.Lon;
- //baiduLng = latLngWifi[3];
- //baiduLat = latLngWifi[2];
- //gaodeLng = latLngWifi[1];
- //gaodeLat = latLngWifi[0];
-
- //type = LocationType.WIFI;
- //hashParam = joWifi.HashParam;
-
- #region 2022/11/11 增加判断wifi解析的地址是为空或null,就采用LBS的地址解析信息,否则用wifi解析的地址信息
- /**
- * 864002050460741 定位解析没有地址信息,入库也无法显示,需要切换LBS解析的处理。
- */
- // wifi解析的地址信息有效
- if (!string.IsNullOrEmpty(joWayzWifi.Address))
- {
- cityCode = joWayzWifi.AdCode;
- addr = joWayzWifi.Address;
- province = joWayzWifi.Province;
- city = joWayzWifi.City;
- district = joWayzWifi.District;
- radius = joWayzWifi.Accuracy;
-
- olat = joWayzWifi.Lat;
- olng = joWayzWifi.Lon;
- baiduLng = latLngWifi[3];
- baiduLat = latLngWifi[2];
- gaodeLng = latLngWifi[1];
- gaodeLat = latLngWifi[0];
-
- type = LocationType.WIFI;
- hashParam = joWayzWifi.HashParam;
- WayzFullAddress = joWayzWifi.FullAddress;
- }
- // wifi解析的地址信息无效,采用LBS的地址信息
- else if (!string.IsNullOrEmpty(joWayzLbs.Address))
- {
- cityCode = joWayzLbs.AdCode;
- addr = joWayzLbs.Address;
- province = joWayzLbs.Province;
- city = joWayzLbs.City;
- district = joWayzLbs.District;
- radius = joWayzLbs.Accuracy;
-
- olat = joWayzLbs.Lat;
- olng = joWayzLbs.Lon;
- baiduLng = latLngLbs[3];
- baiduLat = latLngLbs[2];
- gaodeLng = latLngLbs[1];
- gaodeLat = latLngLbs[0];
-
- type = LocationType.LBS;
- hashParam = joWayzLbs.HashParam;
- WayzFullAddress = joWayzLbs.FullAddress;
-
- }
- // LBS和Wifi都不能解析地址,不入库
- else
- {
- return;
- }
- #endregion
-
- }
- //else
- //{
- // //记录日志
- // await _telpoFailureApiClient.AddAsync(new TelpoFailureOperator
- // {
- // Schema="GaodeLbsError",
- // Table=messageId,
- // EntityFullname=$"距离 1st={distance_1st}, 2nd={distance_2nd}",
- // OperateType= DelayOperateTypeEnum.Insert,
- // OperateTime= datetime,
- // CreateTime =DateTime.Now,
- // JsonData=JsonConvert.SerializeObject(item),
- // State=1,
- // ErrorMessage=joLbs.Address
- // }, new RequestHeader { RequestId = messageId }).ConfigureAwait(false);
- // continue;
- //}
- }
- // 维智不能解析定位调用高德和纠偏
- else
- {
- _logger.LogInformation($"维智服务不能解析wifi数据,将采用高德服务解析");
- var wifiGaodeRequest = new GaodeWifiRequest
- {
- //Imei = serialno,
- Macs = macs,
- Mmac = mmac
- };
- var joGaodeWifi = await _serviceGaode.GetGaodeWifiAddressAsync(serialno, wifiGaodeRequest);
- if (joGaodeWifi.Flag)
- {
- _logger.LogInformation($"高德服务解析解析wifi定位数据");
- cityCode = joGaodeWifi.AdCode;
- addr = joGaodeWifi.Address;
- province = joGaodeWifi.Province;
- city = joGaodeWifi.City;
- district = joGaodeWifi.District;
- radius = joGaodeWifi.Accuracy;
-
- olat = joGaodeWifi.Lat;
- olng = joGaodeWifi.Lon;
-
- latLngWifi = GeoConvert.ConvertGoogleBaiduLatLng(joGaodeWifi.Lat, joGaodeWifi.Lon);
-
- baiduLng = latLngWifi[3];
- baiduLat = latLngWifi[2];
- gaodeLng = latLngWifi[1];
- gaodeLat = latLngWifi[0];
-
- type = LocationType.WIFI;
- hashParam = joGaodeWifi.HashParam;
-
- WayzFullAddress = string.Format("{0}|{1}", "_Gaode", joGaodeWifi.Address);
-
- //调用维智纠偏
- //await _serviceWayz.WayzWifiCalbration(wifiWayzRequest);
- /** 高德wifi与维智LBS对比
- latLngWifi = GeoConvert.ConvertGoogleBaiduLatLng(joWayzWifi.Lat, joWayzWifi.Lon);
- double distance_1st = 0;
- //var distance = distance_1st = joWayzLbs.Flag ? GeoUtils.GetDistance2((double)latLngWifi[1], (double)latLngWifi[0], (double)latLngLbs[1], (double)latLngLbs[0]) : 0;
- var distance = distance_1st = joWayzLbs.Flag ? GeoUtils.GetDistance2((double)joGaodeWifi.Lon, (double)joGaodeWifi.Lat, (double)joWayzLbs.Lon, (double)joWayzLbs.Lat) : 0;
- if (distance >= 1 * 10000)
- {
- _logger.LogWarning($"lbs漂移较大 [{joWayzLbs.AdCode}] [{joWayzLbs.Address}]");
- }
-
- if (distance < 1 * 10000)
- {
- cityCode = joGaodeWifi.AdCode;
- addr = joGaodeWifi.Address;
- province = joGaodeWifi.Province;
- city = joGaodeWifi.City;
- district = joGaodeWifi.District;
- radius = joGaodeWifi.Accuracy;
-
- olat = joGaodeWifi.Lat;
- olng = joGaodeWifi.Lon;
- baiduLng = latLngWifi[3];
- baiduLat = latLngWifi[2];
- gaodeLng = latLngWifi[1];
- gaodeLat = latLngWifi[0];
-
- type = LocationType.WIFI;
- hashParam = joGaodeWifi.HashParam;
-
- WayzFullAddress = string.Format("{0}|{1}", "_Gaode", joGaodeWifi.Address);
- }
- */
- }
- else
- {
- _logger.LogInformation($"维智服务和高德服务解析都不能解析wifi定位数据");
- }
-
- }
-
- //处理业务
- var positionStatus = await _deviceCacheMgr.GetPositionStatusCacheAsync(imei).ConfigureAwait(false);
- var eachParam = mmac.Split(',');
- var signal = Convert.ToInt32(eachParam[1]); //主wifi信号强度值
-
- if (positionStatus?.LastPosition != null)
- {
- if (!joWayzLbs.Flag && !joWayzWifi.Flag)
- {
- if (joWayzLbs.CanRetry || joWayzWifi.CanRetry)
- {
- await _serviceWayz.SendRealtimeLocationAsync(serialno, Utils.ConvertToLocalDateTime(propertyValue.Time), RealtimeLocationTypeFlag.All);
- }
- // 2022/11/29 高德解析不了地址,丢弃不入库,但写日志
- // isRedressed = true;
- _logger.LogInformation($"设备{serialno},{nameof(WifiPlus2PositionResolver)} 解析不了地址,丢弃不入库,但写日志");
- return;
- }
- //2022/11/07 取消LBS覆盖操作(HW) 统一都按LBS定位处理,作为无效定位,不更新定位数据,只更新时间
- //2022/07/19新需求:少于步数增量阈值的lbs定位需要用上一次成功定位点的值替换,并入库
- // if (type == LocationType.LBS && steps < _configWifiParam.ThresholdSteps)
- //{
- // isRedressed = true;
- //}
-
- // 2022/11/29 高德解析不了地址,丢弃不入库,但写日志
- // 2022/11/11 可能存在不是 LBS和wifi都解析地址就使用缓存的数据
- //if (isRedressed)
- //{
- // var pos = positionStatus.LastPosition;
- // type = (LocationType)pos.LocationType;
- // radius = pos.Radius;
- // addr = pos.Address;
- // baiduLat = pos.BaiduLat;
- // baiduLng = pos.BaiduLon;
- // olat = pos.OriginalLat;
- // olng = pos.OriginalLon;
- // gaodeLat = pos.GaodeLat;
- // gaodeLng = pos.GaodeLon;
- // province = pos.Province;
- // city = pos.City;
- // district = pos.District;
- //}
- }
-
- ////******2022/04/24 领导决定不采用最后一次有效定位填充当前无效定位值
- //if (signal > -70) //主wifi信号强度
- //{
-
- //}
- //else if (type == LocationType.WIFI && radius > Consts.RadiusThreshold)
- //{
- // var cachePosition = positionStatus?.LastPosition;
- // if (cachePosition != null)
- // {
- // // 如果主mac信号强度小于-70,则进行距离判断
- // // 计算当前wifi点和上一个可信点之间的直线距离
- // var positionDistance = GeoUtils.GetDistance2((double)cachePosition.GaodeLon, (double)cachePosition.GaodeLat, (double)latLngWifi[1], (double)latLngWifi[0]);
- // //换算步数之间的距离
- // var movingDistance = Utils.GetMovingDistance(steps, _configWifiParam);
- // //位置的确发生了变化的情况
- // // positionDistance大于100米同时步数距离大于100米,其他情况都认为没有动
- // if (positionDistance >= 100 && movingDistance >= 100) //使用新点,用新点更新缓存,认为设备移动了
- // {
-
- // }
- // else //取上一个可信点,认为设备没有移动
- // {
- // addr = cachePosition.Address;
- // olat = cachePosition.OriginalLat;
- // olng = cachePosition.OriginalLon;
- // baiduLng = cachePosition.BaiduLon;
- // baiduLat = cachePosition.BaiduLat;
- // gaodeLat = cachePosition.GaodeLat;
- // gaodeLng = cachePosition.GaodeLon;
- // cityCode = cachePosition.CityCode;
- // radius = cachePosition.Radius;
- // }
- // }
- //}
-
- #endregion
-
- #region 保存历史轨迹
- var loc = new LocationInfo
- {
- LocationId = Guid.NewGuid().ToString("D"),
- Address = addr,
- BaiduLat = baiduLat,
- BaiduLng = baiduLng,
- OLat = olat,
- OLng = olng,
- GLat = gaodeLat,
- GLng = gaodeLng,
- Course = 0,
- DeviceId = device.DeviceId,
- DeviceStatus = radius.ToString(),
- IsStop = false,
- LastUpdate = Utils.ConvertToLocalDateTime(propertyValue.Time), //LastUpdate = datetime,
- UtcDate = Utils.ConvertToUtcDateTime(propertyValue.Time), //UtcDate = datetime.ToUniversalTime(),
- LocationType = (int)type,
- Remarks = cityCode,
- SerialNo = serialno,
- Speed = 0,
- Postcode = cityCode,
- IsRedressed = isRedressed,
- HashParam = hashParam,
- Province = province,
- City = city,
- District = district
- };
- loc.Remarks = string.Format("Wayz_WifiPlus2|{0}", WayzFullAddress);
-
-
- var requestPositionData = new object();
- if (loc.LocationType == 2)
- {
- var requestLbsPositionData = new RequestLocationInfo<WayzLbsRequest>
- {
- MapSource = 2,
- RequestLocationType = loc.LocationType,
- RequestData = lbsWayzRequest
- };
- requestPositionData = requestLbsPositionData;
- }
-
- if (loc.LocationType == 3)
- {
- var requestWifiPositionData = new RequestLocationInfo<WayzWifiRequest>
- {
- MapSource = 2,
- RequestLocationType = loc.LocationType,
- RequestData = wifiWayzRequest
- };
- requestPositionData = requestWifiPositionData;
- }
-
-
-
- // 2022/11/23 定位缓存需要增加步数字段实现前端“静止/行走”状态展示,增加了steps参数
- var result = await _serviceLocation.AddLocationAsync(messageId, loc, positionStatus, propertyValue.Time, cityCode, steps, requestPositionData).ConfigureAwait(false);
- if (!result.IsSuccess)
- {
- _logger.LogError($"{result.Message}");
- return;
- }
- #endregion
-
- if (radius <= Consts.RadiusThreshold) //新需求:有效定位才做围栏判断
- {
- #region 处理围栏
- //过滤历史定位
- //if (!result.IsHistoryLocation)
- //{
- // _serviceLocation.ProcessGeofence(loc, messageId);
- //}
- //else
- //{
- // _logger.LogInformation($"设备{serialno},收到历史定位");
- //}
- #endregion
- }
-
- #region 保存最后位置信息
- var context = "online=1";
- var deviceStatus = await _deviceCacheMgr.GetDeviceStatusBySerialNoAsync(messageId, serialno).ConfigureAwait(false);
- var statusContext = new DeviceStatus();
- if (deviceStatus != null)
- {
- statusContext.Deserlize(deviceStatus.DeviceStatus);
- }
- statusContext.Deserlize(context);
-
- //重新构建设备状态数据
- deviceStatus = new GpsDeviceStatus
- {
- Address = addr,
- IsStop = (bool)loc.IsStop,
- BaiduLat = baiduLat,
- BaiduLng = baiduLng,
- Olat = olat,
- Olng = olng,
- Glat = gaodeLat,
- Glng = gaodeLng,
- Course = loc.Course,
- DeviceId = loc.DeviceId,
- DeviceStatus = statusContext.ToString(),
- DeviceUtcTime = loc.UtcDate,
- LastUpdate = loc.LastUpdate,
- LocationType = loc.LocationType,
- Remarks = loc.LocationId,
- Serialno = loc.SerialNo,
- //Speed = loc.Speed,
- Speed = SafeType.SafeInt(loc.DeviceStatus)
- };
-
- var updateOper = await _serviceLocation.UpdateDeviceStatusAsync(messageId, deviceStatus).ConfigureAwait(false);
- if (!updateOper.IsSuccess)
- {
- _logger.LogError($"{updateOper.Message}");
- return;
- }
-
- //THOMAS Kafka
- var positionData = new LocationDatas()
- {
- imei = loc.SerialNo,
- altitude = 0,
- address = loc.Address,
- baiduLatitude = loc.BaiduLat,
- baiduLongitude = loc.BaiduLng,
- gaodeLatitude = loc.GLat,
- gaodeLongitude = loc.GLng,
- originalLatitude = loc.OLat,
- originalLongitude = loc.OLng,
- locationType = loc.LocationType,
- postcode = loc.Postcode,
- hashParam = loc.HashParam,
- radius = radius,
- province = province,
- city = city,
- district = district
- };
- //await _serviceMqProcess.ProcessPositionAsync(messageId, positionData, loc.LastUpdate.Value.ToString("yyyy-MM-dd HH:mm:ss"), string.Join("|", wifiWayzItems.AsEnumerable().Select(i=>i.MacAddress)));
- await _serviceMqProcess.ProcessPositionAsync(messageId, positionData, loc.LastUpdate.Value.ToString("yyyy-MM-dd HH:mm:ss"), $"{mmac}|{macs}");
- #endregion
-
- #region 更新Alarm
- if (IDType == (int)IdType.Sos && !string.IsNullOrWhiteSpace(IDNumber))
- {
- // THOMAS Kafka
- //var SOSAlarm = new SoSTemplates()
- //{
- // imei = serialno,
- // sosId = IDNumber,
- // address = loc.Address,
- // info = "您的宝贝在求救",
- // baiduLatitude = loc.BaiduLat,
- // baiduLongitude = loc.BaiduLng,
- // gaodeLatitude = loc.GLat,
- // gaodeLongitude = loc.GLng,
- // originalLatitude = loc.OLat,
- // originalLongitude = loc.OLng,
- //};
- //_serviceMqProcess.ProcessAlarmSos(messageId, SOSAlarm, loc.LastUpdate.Value.ToString("yyyy-MM-dd HH:mm:ss"));
-
- var sosId = IDNumber;
-
- await _serviceSos.HandleSosAddressAsync(messageId, sosId, propertyValue.Time, () => new HisGpsAlarm
- {
- DeviceId = device.DeviceId,
- MessageId = sosId,
- Serialno = serialno,
- TypeId = (int)AlarmType.SosAlarm,
- CreateTime = DateTime.Now,
- DeviceUtcTime = Utils.ConvertToUtcDateTime(propertyValue.Time),
-
- Olat = loc.OLat,
- Olng = loc.OLng,
- BaiduLat = loc.BaiduLat,
- BaiduLng = loc.BaiduLng,
- Glat = loc.GLat,
- Glng = loc.GLng
- },
- a =>
- {
- a.Olat = loc.OLat;
- a.Olng = loc.OLng;
- a.BaiduLat = loc.BaiduLat;
- a.BaiduLng = loc.BaiduLng;
- a.Glat = loc.GLat;
- a.Glng = loc.GLng;
- return a;
- },
- async () => {
- var SOSAlarm = new SoSTemplates()
- {
- imei = serialno,
- sosId = IDNumber,
- address = loc.Address,
- info = "您的宝贝在求救",
- baiduLatitude = loc.BaiduLat,
- baiduLongitude = loc.BaiduLng,
- gaodeLatitude = loc.GLat,
- gaodeLongitude = loc.GLng,
- originalLatitude = loc.OLat,
- originalLongitude = loc.OLng,
- };
- await _serviceMqProcess.ProcessAlarmSosAsync(messageId, SOSAlarm, loc.LastUpdate.Value.ToString("yyyy-MM-dd HH:mm:ss"));
- });
- }
- else if (IDType == (int)IdType.PayLog && !string.IsNullOrWhiteSpace(IDNumber))
- {
- var payId = IDNumber;
- var pay = await _servicePay.GetPayLogAsync(messageId, $"{serialno}-{payId}").ConfigureAwait(false);
-
- if (pay == null)
- {
- pay = new GpsPayLog
- {
- Serialno = serialno,
- Payid = $"{serialno}-{payId}",
- DeviceId = device.DeviceId,
-
- Olat = loc.OLat,
- Olng = loc.OLng,
- BaiduLat = loc.BaiduLat,
- BaiduLng = loc.BaiduLng,
- Glat = loc.GLat,
- Glng = loc.GLng
- };
- await _servicePay.AddPayLogAsync(messageId, pay).ConfigureAwait(false);
- }
- else
- {
- pay.Olat = loc.OLat;
- pay.Olng = loc.OLng;
- pay.BaiduLat = loc.BaiduLat;
- pay.BaiduLng = loc.BaiduLng;
- pay.Glat = loc.GLat;
- pay.Glng = loc.GLng;
- await _servicePay.UpdatePayLogAsync(messageId, pay).ConfigureAwait(false);
- }
- }
- else if (IDType == (int)IdType.Temperature && !string.IsNullOrWhiteSpace(IDNumber)) //温度上报(带地址)
- {
- var tempId = IDNumber;
- //var hisTemp = await _serviceHealth.GetTemperatureAsync(messageId, $"{serialno}-{tempId}").ConfigureAwait(false);
-
- //if (hisTemp == null)
- //{
- // hisTemp = new HisGpsTemperature
- // {
- // Serialno = serialno,
- // TemperatureId = $"{serialno}-{tempId}",
- // TempId = tempId,
- // Address = addr,
- // };
- // await _serviceHealth.AddTemperatureAsync(messageId, hisTemp).ConfigureAwait(false);
- //}
- //else
- //{
- // hisTemp.Address = addr;
- // await _serviceHealth.UpdateTemperatureAsync(messageId, hisTemp).ConfigureAwait(false);
- //}
-
- //_serviceMqProcess.ProcessCommonInfo(messageId, DeviceInfoFlagType.Temperature, propertyValue.Time, propertyValue.Value, serialno);
-
- await _serviceHealth.HandleTemperatureAddressAsync(messageId, $"{serialno}-{tempId}", () => new HisGpsTemperature
- {
- TemperatureId = $"{serialno}-{tempId}",
- Serialno = serialno,
- Address = addr,
- Province = province,
- City = city,
- District = district,
- LastUpdate = Utils.ConvertToLocalDateTime(propertyValue.Time),
- TempId = tempId
- },
- a =>
- {
- a.Address = addr;
- a.Province = province;
- a.City = city;
- a.District = district;
- return a;
- },
- //2022/11/10 增加 type 参数判断定位类型
- null,
- type
- );
- }
- #endregion
-
- //推送数据到设备
- await _serviceGlobal.PushLocationDataAsync(serialno, olat, olng);
-
- }
- catch (Exception ex)
- {
-
- _logger.LogError($"ProcessWifiPlus2Info[异常], Item: {JsonConvert.SerializeObject(item)}\n {ex.Message}, {ex.StackTrace}");
- }
- }
- }
-
-
- }
- }
|