You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
5.2 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2025 WenJY 保留所有权利。
* CLR版本4.0.30319.42000
* 机器名称Mr.Wen's MacBook Pro
* 命名空间Sln.Iot.Business
* 唯一标识9444FDB4-4E04-4E00-9CF5-168CDE43F77B
*
* 创建者WenJY
* 电子邮箱:
* 创建时间2025-05-08 17:19:21
* 版本V1.0.0
* 描述:
*
*--------------------------------------------------------------------
* 修改人:
* 时间:
* 修改说明:
*
* 版本V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Sln.Iot.Business.@base;
using Sln.Iot.Common;
using Sln.Iot.Config;
using Sln.Iot.Model.dao;
using Sln.Iot.Model.dto;
using Sln.Iot.Serilog;
using Sln.Iot.Socket.Adapter;
using TouchSocket.Core;
using TouchSocket.Sockets;
namespace Sln.Iot.Business
{
public class LocalBusiness:BaseBusiness
{
public LocalBusiness(SerilogHelper logger, AppConfig appConfig, StringChange stringChange) : base(logger, appConfig, stringChange)
{
}
public override FilterResult BufferAnalysis(ISocketClient client, BufferRequestInfo requestInfo, int bodyLength)
{
ByteBlock byteBlock = new ByteBlock(requestInfo.Body);
if (byteBlock.CanReadLen < 1)
{
return FilterResult.Cache;
}
int pos = byteBlock.Pos;
try
{
var amount = requestInfo.BufferLength / bodyLength;
_logger.Info($"收到{amount}个物联网环境数据,开始循环解析......");
for (int i = 0; i < amount; i++)
{
byte[] ids = new byte[3];
Array.Copy(requestInfo.buffer, 1, ids, 0, ids.Length);
string idsStr = _stringChange.bytesToHexStr(ids,ids.Length);
_logger.Info($"设备编号:{idsStr}");
do
{
byteBlock.Read(out byte[] b_UA_flag, 2);
base._stringChange.ConvertBytesToUInt16(b_UA_flag, out uint flag);
switch (flag)
{
case 0xB401: //纬度
byteBlock.Read(out byte[] latitudeBuffer, 5);
var latitudeStr = _stringChange.bytesToHexStr(latitudeBuffer,latitudeBuffer.Length);
decimal latitude = decimal.Parse(latitudeStr.Substring(0, 2) + "." + latitudeStr.Substring(2, 6));
int latitudeType = int.Parse(latitudeStr.Substring(latitudeStr.Length - 2));
_logger.Info($"{(latitudeType == 10 ? "":"")}{latitude}°");
break;
case 0xB402: //经度
byteBlock.Read(out byte[] longitudeBuffer, 6);
var longitudeStr = _stringChange.bytesToHexStr(longitudeBuffer,longitudeBuffer.Length);
decimal longitude = decimal.Parse(longitudeStr.Substring(0, 3) + "." + longitudeStr.Substring(3, 7));
int longitudeType = int.Parse(longitudeStr.Substring(longitudeStr.Length - 2));
_logger.Info($"{(longitudeType == 20 ? "":"西")}{longitude}°");
break;
case 0xB403: //卫星数量
byteBlock.Read(out byte[] satelliteBuffer, 1);
var satelliteStr = _stringChange.bytesToHexStr(satelliteBuffer,satelliteBuffer.Length);
_logger.Info($"卫星数量:{satelliteStr}");
break;
case 0xB404: //时间
byteBlock.Read(out byte[] timeBuffer, 6);
var timeStr = _stringChange.bytesToHexStr(timeBuffer,timeBuffer.Length);
_logger.Info($"时间:{timeStr}");
break;
default:
break;
}
} while (byteBlock.Pos % bodyLength != 0);
}
return FilterResult.Success;
}
catch (Exception e)
{
base._logger.Error($"物联网数据解析异常:{e.Message}");
}
return FilterResult.Cache;
}
public override void ResponseHandle(ISocketClient client, BufferRequestInfo requestInfo)
{
MessagePack SendMessagePackInfo = new MessagePack()
{
//m_MessageType = 0xB5
};
base.GetMessagePack(ref SendMessagePackInfo, requestInfo.buffer);
base.SendMessageAsync(client,SendMessagePackInfo);
}
}
}