171 lines
5.8 KiB
C#
171 lines
5.8 KiB
C#
using JT808.Protocol.MessageBody;
|
|
using JT808.Protocol;
|
|
using langguanApi.Model;
|
|
using langguanApi.Service;
|
|
using langguanApi.Service.HJ212;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Org.BouncyCastle.Utilities;
|
|
using System.Text;
|
|
using JT808.Protocol.Extensions;
|
|
using static langguanApi.Service.WeatherService;
|
|
using langguanApi.Common.Gps;
|
|
using NPOI.SS.Formula.Functions;
|
|
using static langguanApi.Common.Gps.ProtocolParser;
|
|
|
|
namespace langguanApi.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class HomeController : ControllerBase
|
|
{
|
|
private readonly HomeService _homeService;
|
|
public HomeController(HomeService homeService)
|
|
{
|
|
_homeService = homeService;
|
|
}
|
|
/// <summary>
|
|
/// 首页view
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet("view")]
|
|
public async Task<IActionResult> View()
|
|
{
|
|
return Ok(await _homeService.View());
|
|
|
|
}
|
|
[HttpGet("test")]
|
|
/// <summary>
|
|
/// test
|
|
/// </summary>
|
|
/// <param name="num">数字</param>
|
|
/// <param name="key">字符串</param>
|
|
/// <returns></returns>
|
|
public async Task<IActionResult> test(int num = 1, string key = "")
|
|
{
|
|
// 示例数据(需替换为实际接收到的协议数据)
|
|
string hexData = "787811010868120321167279808D3202001AB12F0D0A";
|
|
byte[] protocolData = HexStringToByteArray(hexData);
|
|
// 进行转义还原
|
|
byte[] restoredData = RestoreEscape(protocolData);
|
|
|
|
// 验证校验码
|
|
if (!VerifyChecksum(restoredData))
|
|
{
|
|
Console.WriteLine("Checksum verification failed.");
|
|
}
|
|
Position? position = ParsePosition(restoredData);
|
|
|
|
if (position.HasValue)
|
|
{
|
|
Console.WriteLine($"Latitude: {position.Value.Latitude}");
|
|
Console.WriteLine($"Longitude: {position.Value.Longitude}");
|
|
Console.WriteLine($"Altitude: {position.Value.Altitude}");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Failed to parse position.");
|
|
}
|
|
// Console.WriteLine("经度:" + lon + " 纬度:" + lat);
|
|
|
|
// string rawText = "数据报:##0250QN=20240424224800000;ST=22;CN=2011;PW=123456;MN=LGYC022024690001;Flag=5;CP=&&DataTime=20240424224800;a34001-Rtd=356.2";
|
|
//NetPackage netPackage = NetPackage.Parse(rawText, null);
|
|
//((NetServer)Server).RaiseReceivedData(this, netPackage, rawText);
|
|
return Ok();
|
|
}
|
|
public static Position? ParsePosition(byte[] data)
|
|
{
|
|
// 根据协议中的位置信息的偏移量和长度进行解析
|
|
int positionOffset = 21; // 位置信息在数据中的偏移量
|
|
int latitudeOffset = positionOffset + 0;
|
|
int longitudeOffset = positionOffset + 4;
|
|
int altitudeOffset = positionOffset + 8;
|
|
|
|
// 检查数组长度是否足够长
|
|
if (data.Length < altitudeOffset + 2)
|
|
{
|
|
Console.WriteLine("Data array is not long enough to contain position information.");
|
|
return null;
|
|
}
|
|
|
|
// 解析纬度(单位:百万分之一度)
|
|
int rawLatitude = BitConverter.ToInt32(data, latitudeOffset);
|
|
double latitude = rawLatitude / 1000000.0;
|
|
|
|
// 解析经度(单位:百万分之一度)
|
|
int rawLongitude = BitConverter.ToInt32(data, longitudeOffset);
|
|
double longitude = rawLongitude / 1000000.0;
|
|
|
|
// 解析高度(单位:米)
|
|
int rawAltitude = BitConverter.ToInt16(data, altitudeOffset);
|
|
double altitude = rawAltitude;
|
|
|
|
return new Position
|
|
{
|
|
Latitude = latitude,
|
|
Longitude = longitude,
|
|
Altitude = altitude
|
|
};
|
|
}
|
|
|
|
private static bool VerifyChecksum(byte[] data)
|
|
{
|
|
// 校验码在数据包的倒数第二个字节
|
|
byte receivedChecksum = data[data.Length - 2];
|
|
byte calculatedChecksum = 0;
|
|
|
|
// 校验范围是从第一个字节到倒数第三个字节
|
|
for (int i = 0; i < data.Length - 2; i++)
|
|
{
|
|
calculatedChecksum ^= data[i];
|
|
}
|
|
|
|
return receivedChecksum == calculatedChecksum;
|
|
}
|
|
private static byte[] RestoreEscape(byte[] data)
|
|
{
|
|
List<byte> result = new List<byte>();
|
|
for (int i = 0; i < data.Length; i++)
|
|
{
|
|
if (data[i] == 0x7D)
|
|
{
|
|
if (i + 1 < data.Length)
|
|
{
|
|
if (data[i + 1] == 0x02)
|
|
{
|
|
result.Add(0x7E);
|
|
i++;
|
|
}
|
|
else if (data[i + 1] == 0x01)
|
|
{
|
|
result.Add(0x7D);
|
|
i++;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.Add(data[i]);
|
|
}
|
|
}
|
|
return result.ToArray();
|
|
}
|
|
private static byte[] HexStringToByteArray(string hex)
|
|
{
|
|
int NumberChars = hex.Length;
|
|
byte[] bytes = new byte[NumberChars / 2];
|
|
for (int i = 0; i < NumberChars; i += 2)
|
|
{
|
|
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
|
|
}
|
|
return bytes;
|
|
}
|
|
public struct Position
|
|
{
|
|
public double Latitude;
|
|
public double Longitude;
|
|
public double Altitude;
|
|
}
|
|
}
|
|
}
|