|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using DotNetty.Buffers;
|
|
|
|
|
using DotNetty.Codecs;
|
|
|
|
|
using DotNetty.Transport.Channels;
|
|
|
|
|
using HybirdFrameworkCore.Utils;
|
|
|
|
|
using HybirdFrameworkDriver.Session;
|
|
|
|
|
using log4net;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Service.Cloud.Msg;
|
|
|
|
|
using Service.Fire.Common;
|
|
|
|
|
using Service.Fire.Msg;
|
|
|
|
|
using Service.Fire.Msg.Fire;
|
|
|
|
|
using Service.Fire.Msg.Fire.Req;
|
|
|
|
|
|
|
|
|
|
namespace Service.Fire.Codec;
|
|
|
|
|
|
|
|
|
|
public class Decoder : ByteToMessageDecoder
|
|
|
|
|
{
|
|
|
|
|
private readonly IByteBuffer[] _delimiters = { Unpooled.CopiedBuffer(FireConst.StartChar) };
|
|
|
|
|
|
|
|
|
|
private ILog Log(string? chargerSn)
|
|
|
|
|
{
|
|
|
|
|
if (ObjUtils.IsNotNullOrWhiteSpace(chargerSn))
|
|
|
|
|
{
|
|
|
|
|
return LogManager.GetLogger("Charger" + chargerSn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return LogManager.GetLogger(typeof(Decoder));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Decode(IChannelHandlerContext context, IByteBuffer buffer, List<object> output)
|
|
|
|
|
{
|
|
|
|
|
string? chargerSn = ChannelUtils.GetAttr(context.Channel, FireConst.FireSn);
|
|
|
|
|
IByteBuffer? delimiter = FindDelimiter(buffer);
|
|
|
|
|
if (delimiter != null)
|
|
|
|
|
{
|
|
|
|
|
//分隔符索引
|
|
|
|
|
int delimiterIndex = IndexOf(buffer, delimiter);
|
|
|
|
|
//帧长索引
|
|
|
|
|
int frameLengthIndex = delimiterIndex + delimiter.Capacity + 1;
|
|
|
|
|
|
|
|
|
|
if (delimiterIndex > 0)
|
|
|
|
|
{
|
|
|
|
|
buffer.SkipBytes(delimiterIndex);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (buffer.ReadableBytes < frameLengthIndex + 2)
|
|
|
|
|
{
|
|
|
|
|
// 数据不足,等待更多数据
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 读取长度字段
|
|
|
|
|
byte frameLength = buffer.GetByte(buffer.ReaderIndex + frameLengthIndex);
|
|
|
|
|
//总帧长
|
|
|
|
|
int totalFrameLength = delimiterIndex + delimiter.Capacity + 2 + 2 + frameLength;
|
|
|
|
|
// 最小总帧长过滤
|
|
|
|
|
if (totalFrameLength < 5)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (buffer.ReadableBytes < totalFrameLength)
|
|
|
|
|
{
|
|
|
|
|
// 数据不足,等待更多数据
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[]? data = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ASDU asdu = Parse(buffer, totalFrameLength, delimiter, out data);
|
|
|
|
|
Log(chargerSn)
|
|
|
|
|
.Info(
|
|
|
|
|
$"receive {BitUtls.BytesToHexStr(data)}:{JsonConvert.SerializeObject(asdu)} from {chargerSn}");
|
|
|
|
|
output.Add(asdu);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Log(chargerSn).Error($"decode fail msg={BitUtls.BytesToHexStr(data)}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IByteBuffer? FindDelimiter(IByteBuffer buffer)
|
|
|
|
|
{
|
|
|
|
|
foreach (IByteBuffer delimiter in _delimiters)
|
|
|
|
|
{
|
|
|
|
|
int delimiterIndex = IndexOf(buffer, delimiter);
|
|
|
|
|
if (delimiterIndex >= 0)
|
|
|
|
|
{
|
|
|
|
|
return delimiter;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int IndexOf(IByteBuffer haystack, IByteBuffer needle)
|
|
|
|
|
{
|
|
|
|
|
for (int i = haystack.ReaderIndex; i < haystack.WriterIndex; i++)
|
|
|
|
|
{
|
|
|
|
|
int num = i;
|
|
|
|
|
int j;
|
|
|
|
|
for (j = 0; j < needle.Capacity && haystack.GetByte(num) == needle.GetByte(j); j++)
|
|
|
|
|
{
|
|
|
|
|
num++;
|
|
|
|
|
if (num == haystack.WriterIndex && j != needle.Capacity - 1)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (j == needle.Capacity)
|
|
|
|
|
{
|
|
|
|
|
return i - haystack.ReaderIndex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ASDU Parse(IByteBuffer byteBuffer, int totalFrameLength, IByteBuffer delimiter, out byte[] data)
|
|
|
|
|
{
|
|
|
|
|
data = new byte[totalFrameLength];
|
|
|
|
|
byteBuffer.ReadBytes(data);
|
|
|
|
|
|
|
|
|
|
int removeIndex = delimiter.Capacity;
|
|
|
|
|
|
|
|
|
|
ushort cmd = data[2 + removeIndex];
|
|
|
|
|
byte[] bytes = new byte[data[2]];
|
|
|
|
|
Array.Copy(data, 2 + removeIndex, bytes, 0, bytes.Length);
|
|
|
|
|
byte length = data[2];
|
|
|
|
|
ASDU asdu = cmd switch
|
|
|
|
|
{
|
|
|
|
|
0xA1 => ModelConvert.Decode<BroadcastResetReq>(bytes),
|
|
|
|
|
0xA2 => ModelConvert.Decode<PollingReq>(bytes),
|
|
|
|
|
0xA5 => length switch
|
|
|
|
|
{
|
|
|
|
|
20 => ModelConvert.Decode<DataNoAddresReq>(bytes),
|
|
|
|
|
40 => ModelConvert.Decode<DataReq>(bytes),
|
|
|
|
|
_ => new ASDU(),
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_ => new ASDU(),
|
|
|
|
|
};
|
|
|
|
|
ASDU.ParseHeader(data, asdu);
|
|
|
|
|
|
|
|
|
|
return asdu;
|
|
|
|
|
}
|
|
|
|
|
}
|