using CommonExtend; using CommonModel; 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; namespace LanShengService.Tcp { public class EncoderHandler : MessageToByteEncoder> { protected override void Encode(IChannelHandlerContext context, TcpDataLog message, IByteBuffer output) { List Temp = new List(); Temp.AddRange(message.Frame_START!.HexString2Byte().BytePadLeft(2)); Temp.AddRange(((int)message.Frame_number!).Int2Byte().BytePadLeft(2)); Temp.AddRange(Encoding.ASCII.GetBytes(message.DTU_ID!).BytePadLeft(17)); Temp.AddRange(message.Msg_ID!.HexString2Byte().BytePadLeft(2)); switch (message.Msg_ID) { case "1104": if (message.Content!.Id!.StartsWith("QC") || message.Content!.Id!.StartsWith("ZK")) { Encode1104ZK(Temp, message.Content!); } else { Encode1104RY(Temp,message.Content!); } break; case "1107": Temp.AddRange(((int)message.Content!.UVersion!).Int2Byte().BytePadLeft(2)); var FtpUrlByte = Encoding.ASCII.GetBytes(message.Content.FtpUrl!); message.Content.FtpLength = message.Content.FtpUrl!.Length; Temp.AddRange(((int)message.Content.FtpLength!).Int2Byte().BytePadLeft(1)); Temp.AddRange(FtpUrlByte); var FtpUserByte = Encoding.ASCII.GetBytes(message.Content.UserPwd!); message.Content.UserLength = message.Content.UserPwd!.Length; Temp.AddRange(((int)message.Content.UserLength!).Int2Byte().BytePadLeft(1)); Temp.AddRange(FtpUserByte); var BinUrlByte = Encoding.ASCII.GetBytes(message.Content.BinUrl!); message.Content.BinLength = message.Content.BinUrl!.Length; Temp.AddRange(((int)message.Content.BinLength!).Int2Byte().BytePadLeft(1)); Temp.AddRange(BinUrlByte); Temp.AddRange(((int)message.Content!.BinSize!).Int2Byte().BytePadLeft(3)); Temp.AddRange(((int)message.Content!.UpdateSpan!).Int2Byte().BytePadLeft(1)); break; default: break; } message.Frame_Length = Temp.Count + 6; Temp.InsertRange(2, ((int)message.Frame_Length!).Int2Byte().BytePadLeft(2)); message.Checksum = Temp.ToArray().Byte2HexSum(); Temp.AddRange(((int)message.Checksum!).Int2Byte().BytePadLeft(2)); Temp.AddRange(message.Frame_END!.HexString2Byte().BytePadLeft(2)); output.WriteBytes(Temp.ToArray()); } private void Encode1104RY(List bytes,DeviceData device) { bytes.AddRange(((int)device.Version!).Int2Byte().BytePadLeft(1)); bytes.AddRange(((int)device.DataTick!).Int2Byte().BytePadLeft(2)); int[] Data1 = new int[] { (int)device.Data27_B7!, (int)device.Data27_B6!, (int)device.Data27_B5!, (int)device.Data27_B4!, (int)device.Data27_B3!, 0, 0, (int)device.Data27_B0!, }; bytes.AddRange(Data1.BitArray2Byte()); int[] Data2 = new int[] { (int)device.Data23!, (int)device.Data20!, (int)device.Data19!, (int)device.Data18!, ((int)device.Data16_B2!).Int2Bit(2)[1], ((int)device.Data16_B2!).Int2Bit(2)[0], 0, 0 }; bytes.AddRange(Data2.BitArray2Byte()); bytes.AddRange(((int)device.Data25_B4!).Int2Byte().BytePadLeft(1)); bytes.AddRange(((int)device.Data25_B0!).Int2Byte().BytePadLeft(1)); bytes.AddRange(((int)device.Data26_B4!).Int2Byte().BytePadLeft(1)); bytes.AddRange(((int)device.Data26_B0!).Int2Byte().BytePadLeft(1)); bytes.AddRange(((int)device.UpdateTag!).Int2Byte().BytePadLeft(1)); var ConnectByte = Encoding.ASCII.GetBytes(device.ConnectString!); device.ConnectStrLength = device.ConnectString!.Length; bytes.AddRange(((int)device.ConnectStrLength!).Int2Byte().BytePadLeft(1)); bytes.AddRange(ConnectByte); } private void Encode1104ZK(List bytes, DeviceData device) { bytes.AddRange(((int)device.Version!).Int2Byte().BytePadLeft(1)); bytes.AddRange(((int)device.DataTick!).Int2Byte().BytePadLeft(2)); int[] Data1 = new int[] { (int)device.Data27_B7!, (int)device.Data27_B6!, (int)device.Data27_B5!, (int)device.Data27_B4!, (int)device.Data27_B3!, 0, 0, 0 }; bytes.AddRange(Data1.BitArray2Byte()); int[] Data2 = new int[] { (int)device.Data23!, 0, ((int)device.Data18!).Int2Bit(2)[1], ((int)device.Data18!).Int2Bit(2)[0], ((int)device.Data16_B2!).Int2Bit(2)[1], ((int)device.Data16_B2!).Int2Bit(2)[0], 0, 0 }; bytes.AddRange(Data2.BitArray2Byte()); bytes.AddRange(((int)device.Data25_B4!).Int2Byte().BytePadLeft(1)); bytes.AddRange(((int)device.Data25_B0!).Int2Byte().BytePadLeft(1)); bytes.AddRange(((int)device.Data26_B4!).Int2Byte().BytePadLeft(1)); bytes.AddRange(((int)device.Data26_B0!).Int2Byte().BytePadLeft(1)); int[] Data7 = new int[] { (int)device.Data28_B7!, (int)device.Data28_B6!, (int)device.Data28_B5!, (int)device.Data28_B4!, (int)device.Data28_B3!, (int)device.Data28_B2!, (int)device.Data28_B1!, (int)device.Data28_B0! }; bytes.AddRange(Data7.BitArray2Byte()); int[] Data8 = new int[] { (int)device.Data8_B7_1104!, (int)device.Data29_B6!, (int)device.Data29_B5!, (int)device.Data29_B4!, (int)device.Data29_B3!, 0, 0, 0 }; bytes.AddRange(Data8.BitArray2Byte()); bytes.AddRange(((int)device.Data30!).Int2Byte().BytePadLeft(1)); bytes.AddRange(((int)device.Data31!).Int2Byte().BytePadLeft(1)); bytes.AddRange(((int)device.Data32_33!).Int2Byte().BytePadLeft(2)); bytes.AddRange(((int)device.Data34!).Int2Byte().BytePadLeft(1)); bytes.AddRange(((int)device.Data35!).Int2Byte().BytePadLeft(1)); bytes.AddRange(((int)device.Data36!).Int2Byte().BytePadLeft(1)); bytes.AddRange(((int)device.Data37!).Int2Byte().BytePadLeft(1)); bytes.AddRange(((int)device.Data38_39!).Int2Byte().BytePadLeft(2)); bytes.AddRange(((int)device.Data40!).Int2Byte().BytePadLeft(1)); bytes.AddRange(((int)device.Data41!).Int2Byte().BytePadLeft(1)); bytes.AddRange(((int)device.Data42_43!).Int2Byte().BytePadLeft(2)); bytes.AddRange(((int)device.Data44!).Int2Byte().BytePadLeft(1)); bytes.AddRange(((int)device.Data45_46!).Int2Byte().BytePadLeft(2)); bytes.AddRange(((int)device.Data47_48!).Int2Byte().BytePadLeft(2)); bytes.AddRange(((int)device.Data49_50!).Int2Byte().BytePadLeft(2)); bytes.AddRange(((int)device.Data51_52!).Int2Byte().BytePadLeft(2)); bytes.AddRange(((int)device.Data53_54!).Int2Byte().BytePadLeft(2)); bytes.AddRange(((int)device.Data55_56!).Int2Byte().BytePadLeft(2)); bytes.AddRange(((int)device.Data57_58!).Int2Byte().BytePadLeft(2)); bytes.AddRange(((int)device.Data59_60!).Int2Byte().BytePadLeft(2)); bytes.AddRange(((int)device.Data61_62!).Int2Byte().BytePadLeft(2)); bytes.AddRange(((int)device.Data63_64!).Int2Byte().BytePadLeft(2)); bytes.AddRange(((int)device.Data65_66!).Int2Byte().BytePadLeft(2)); int[] Data32 = new int[] { (int)device.Data67_B7!, (int)device.Data67_B6!, (int)device.Data67_B5!, (int)device.Data67_B4!, (int)device.Data67_B3!, (int)device.Data67_B2!, (int)device.Data67_B1!, (int)device.Data67_B0! }; bytes.AddRange(Data32.BitArray2Byte()); int[] Data33 = new int[] { (int)device.Data68_B7!, (int)device.Data68_B6!, (int)device.Data68_B5!, (int)device.Data68_B4!, (int)device.Data68_B3!, (int)device.Data68_B2!, (int)device.Data68_B1!, (int)device.Data68_B0! }; bytes.AddRange(Data33.BitArray2Byte()); int[] Data34 = new int[] { (int)device.Data69_B7!, (int)device.Data69_B6!, (int)device.Data69_B5!, (int)device.Data69_B4!, (int)device.Data69_B3!, (int)device.Data69_B2!, (int)device.Data69_B1!, (int)device.Data69_B0! }; bytes.AddRange(Data34.BitArray2Byte()); int[] Data35 = new int[] { (int)device.Data70_B7!, (int)device.Data70_B6!, (int)device.Data70_B5!, (int)device.Data70_B4!, (int)device.Data70_B3!, (int)device.Data70_B2!, (int)device.Data70_B1!, (int)device.Data70_B0! }; bytes.AddRange(Data35.BitArray2Byte()); int[] Data36 = new int[] { (int)device.Data71_B7!, (int)device.Data71_B6!, (int)device.Data71_B5!, (int)device.Data71_B4!, (int)device.Data71_B3!, (int)device.Data71_B2!, (int)device.Data71_B1!, (int)device.Data71_B0! }; bytes.AddRange(Data36.BitArray2Byte()); int[] Data37 = new int[] { (int)device.Data72_B7!, (int)device.Data72_B6!, (int)device.Data72_B5!, (int)device.Data72_B4!, 0, 0, 0, 0 }; bytes.AddRange(Data37.BitArray2Byte()); var ConnectByte = Encoding.ASCII.GetBytes(device.ConnectString!); device.ConnectStrLength = device.ConnectString!.Length; bytes.AddRange(((int)device.ConnectStrLength!).Int2Byte().BytePadLeft(1)); bytes.AddRange(ConnectByte); } } }