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.

179 lines
8.5 KiB
C#

using DotNetty.Buffers;
using DotNetty.Codecs;
using DotNetty.Transport.Channels;
using LanShengModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommonExtend;
using CommonModel;
namespace LanShengService.Tcp
{
public class DecoderHandler : ByteToMessageDecoder
{
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List<object> output)
{
var request = new TcpDataLog<DeviceData>();
if (!input.HasArray)
{
return;
}
while (input.ReadableBytes >= 2)
{
if (input.ReadHex(1) == "FA")
{
if (input.ReadHex(1) == "AA")
{
request.Frame_START = "FAAA";
break;
}
else
{
input.SetReaderIndex(input.ReaderIndex - 1);
}
}
}
if (request.Frame_START != "FAAA")
{
return;
}
int ReadableBytes = input.ReadableBytes + 2;
if (ReadableBytes < 4)
{
input.SetReaderIndex(input.ReaderIndex - 2);
return;
}
request.Frame_Length = input.ReadInt(2);
if (ReadableBytes < request.Frame_Length)
{
input.ResetReaderIndex();
return;
}
request.Frame_number = input.ReadInt(2);
request.DTU_ID = input.ReadString(17, Encoding.ASCII);
request.Msg_ID = input.ReadHex(2);
IByteBuffer Content = Unpooled.Buffer((int)request.Frame_Length - 25 - 4);
input.ReadBytes(Content, (int)request.Frame_Length - 25 - 4);
try
{
request.Content = new DeviceData();
request.Content.Id = request.DTU_ID;
request.Content.Status = "online";
request.Content.UpdateDate = DateTime.Now;
request.Content.MsgType = request.Msg_ID;
switch (request.Msg_ID)
{
case "1001":
case "9104":
request.Content.AgreementVersion = Content.ReadInt(1);
request.Content.DataTick = Content.ReadInt(2);
request.Content.ICCID = Content.ReadString(20, Encoding.ASCII);
Content.SkipBytes(4);
request.Content.Longitude = Content.ReadString(9, Encoding.ASCII);
request.Content.Latitude = Content.ReadString(8, Encoding.ASCII);
request.Content.ConnectStrLength = Content.ReadInt(1);
request.Content.ConnectString = Content.ReadString((int)request.Content.ConnectStrLength, Encoding.ASCII);
break;
case "1103":
request.Content.Version = Content.ReadInt(1);
var Data2 = Content.ReadBitArray();
request.Content.Data2_B7 = Data2[0];
request.Content.Data2_B6 = Data2[1];
request.Content.Data2_B5 = Data2[2];
request.Content.Data2_B4 = Data2[3];
request.Content.Data2_B3 = Data2[4];
request.Content.Data2_B2 = Data2[5];
request.Content.Data2_B1 = Data2[6];
request.Content.Data2_B0 = Data2[7];
var Data3 = Content.ReadBitArray();
request.Content.Data3_B7 = Data3[0];
request.Content.Data3_B6 = Data3[1];
request.Content.Data3_B5 = Data3[2];
request.Content.Data3_B4 = Data3[3];
request.Content.Data3_B3 = Data3[4];
request.Content.Data3_B2 = Data3[5];
request.Content.Data3_B1 = Data3[6];
request.Content.Data3_B0 = Data3[7];
request.Content.Data4 = Content.ReadInt(1);
request.Content.Data5 = Content.ReadInt(1);
request.Content.Data6 = Content.ReadInt(1);
request.Content.Data7 = Content.ReadInt(1);
request.Content.Data8 = Content.ReadInt(2);
request.Content.Data10 = Content.ReadInt(1);
request.Content.Data11 = Content.ReadInt(1);
request.Content.Data12 = Content.ReadInt(1);
request.Content.Data13 = Content.ReadInt(1);
var Data14 = Content.ReadBitArray();
request.Content.Data14_B6 = Convert.ToInt32($"{Data14[1]}{Data14[0]}", 2);
request.Content.Data14_B5 = Data14[2];
request.Content.Data14_B4 = Data14[3];
request.Content.Data14_B3 = Data14[4];
request.Content.Data14_B2 = Data14[5];
request.Content.Data14_B1 = Data14[6];
request.Content.Data14_B0 = Data14[7];
var Data15 = Content.ReadBitArray();
request.Content.Data15_B6 = Convert.ToInt32($"{Data15[1]}{Data15[0]}", 2);
request.Content.Data15_B4 = Convert.ToInt32($"{Data15[3]}{Data15[2]}", 2);
request.Content.Data15_B3 = Data15[4];
request.Content.Data15_B2 = Data15[5];
request.Content.Data15_B1 = Data15[6];
request.Content.Data15_B0 = Data15[7];
var Data16 = Content.ReadBitArray();
request.Content.Data16_B7 = Data16[0];
request.Content.Data16_B6 = Data16[1];
request.Content.Data16_B5 = Data16[2];
request.Content.Data16_B4 = Data16[3];
request.Content.Data16_B2 = Convert.ToInt32($"{Data16[5]}{Data16[4]}", 2);
request.Content.Data16_B1 = Data16[6];
request.Content.Data16_B0 = Data16[7];
request.Content.Data17 = Content.ReadInt(1);
request.Content.Data18 = Content.ReadInt(1);
request.Content.Data19 = Content.ReadInt(1);
request.Content.Data20 = Content.ReadInt(1);
request.Content.Data21 = Content.ReadInt(1);
request.Content.Data22 = Content.ReadInt(1);
request.Content.Data23 = Content.ReadInt(1);
request.Content.Data24 = Content.ReadInt(1);
var Data25 = Content.ReadBitArray();
request.Content.Data25_B4 = Convert.ToInt32($"{Data25[3]}{Data25[2]}{Data25[1]}{Data25[0]}", 2);
request.Content.Data25_B0 = Convert.ToInt32($"{Data25[7]}{Data25[6]}{Data25[5]}{Data25[4]}", 2);
var Data26 = Content.ReadBitArray();
request.Content.Data26_B4 = Convert.ToInt32($"{Data26[3]}{Data26[2]}{Data26[1]}{Data26[0]}", 2);
request.Content.Data26_B0 = Convert.ToInt32($"{Data26[7]}{Data26[6]}{Data26[5]}{Data26[4]}", 2);
var Data27 = Content.ReadBitArray();
request.Content.Data27_B7 = Data27[0];
request.Content.Data27_B6 = Data27[1];
request.Content.Data27_B5 = Data27[2];
request.Content.Data27_B4 = Data27[3];
request.Content.Data27_B3 = Data27[4];
request.Content.Data27_B0 = Data27[7];
break;
default:
break;
}
request.Content = request.Content;
}
catch
{
request.Content = null;
}
request.Checksum = input.ReadInt(2);
request.Frame_END = input.ReadHex(2);
request.CreateDate = DateTime.Now;
if (request.Frame_END == "BBFB")
{
output.Add(request);
Content.Clear();
input.Clear();
}
else
{
return;
}
}
}
}