JiShe.CollectBus/JiShe.CollectBus.Protocol/StandardProtocolPlugin.cs

299 lines
11 KiB
C#
Raw Normal View History

2024-10-30 17:49:05 +08:00
using JiShe.CollectBus.Common.Enums;
2024-10-29 16:28:14 +08:00
using JiShe.CollectBus.Common.Extensions;
2024-10-25 19:11:43 +08:00
using JiShe.CollectBus.Common.Extensions.DependencyInjections;
2024-10-29 16:28:14 +08:00
using JiShe.CollectBus.Common.Models;
2024-10-21 13:30:53 +08:00
using JiShe.CollectBus.Protocol.Contracts.Abstracts;
2024-10-08 14:41:41 +08:00
using JiShe.CollectBus.Protocol.Contracts.Attributes;
using JiShe.CollectBus.Protocol.Contracts.Models;
2024-10-28 16:23:39 +08:00
using JiShe.CollectBus.RabbitMQ.Senders;
2024-10-29 16:28:14 +08:00
using Microsoft.Extensions.Logging;
2024-09-30 17:10:43 +08:00
namespace JiShe.CollectBus.Protocol
{
2024-10-08 14:41:41 +08:00
[ProtocolName("StandardProtocol")]
2024-10-29 16:28:14 +08:00
public class StandardProtocolPlugin(INSender nSender, ILogger<BaseProtocolPlugin> logger) : BaseProtocolPlugin(logger), ISingletonDependency
2024-09-30 17:10:43 +08:00
{
2024-10-25 19:11:43 +08:00
public override async Task<ProtocolInfo> GetAsync()
2024-09-30 17:53:14 +08:00
{
2024-10-25 19:11:43 +08:00
var info = new ProtocolInfo("Standard", "376.1", "TCP", "376.1协议", "DTS1980");
return await Task.FromResult(info);
2024-09-30 17:10:43 +08:00
}
2024-10-29 16:28:14 +08:00
public override async Task AnalyzeAsync(MessageReceivedEvent messageReceivedEvent, Action<byte[]>? sendAction = null)
2024-09-30 17:10:43 +08:00
{
2024-10-28 16:23:39 +08:00
var cmdResult = AnalysisCmd(messageReceivedEvent.MessageHexString);
2024-10-21 13:30:53 +08:00
if (cmdResult == null)
{
return;
}
AnalysisData(cmdResult);
2024-10-25 19:11:43 +08:00
await Task.CompletedTask;
2024-09-30 17:10:43 +08:00
}
2024-10-29 16:28:14 +08:00
public override async Task LoginAsync(MessageReceivedLoginEvent messageReceivedEvent, Action<byte[]>? sendAction = null)
2024-10-21 13:30:53 +08:00
{
2024-10-29 16:28:14 +08:00
async void SendAction(byte[] bytes)
2024-10-21 16:24:29 +08:00
{
2024-10-30 17:49:05 +08:00
await nSender.SendToIssuedAsync(new MessageIssuedEvent { ClientId = messageReceivedEvent.ClientId, DeviceNo = messageReceivedEvent.DeviceNo, Message = bytes, Type = IssuedEventType.Login,MessageId = messageReceivedEvent.MessageId});
2024-10-29 16:28:14 +08:00
}
2024-10-21 13:30:53 +08:00
2024-10-29 16:28:14 +08:00
await base.LoginAsync(messageReceivedEvent, SendAction);
}
2024-10-21 13:30:53 +08:00
2024-10-29 16:28:14 +08:00
public override async Task HeartbeatAsync(MessageReceivedHeartbeatEvent messageReceivedEvent, Action<byte[]>? sendAction = null)
{
async void SendAction(byte[] bytes)
{
2024-10-30 17:49:05 +08:00
await nSender.SendToIssuedAsync(new MessageIssuedEvent { ClientId = messageReceivedEvent.ClientId, DeviceNo = messageReceivedEvent.DeviceNo, Message = bytes, Type = IssuedEventType.Login, MessageId = messageReceivedEvent.MessageId });
2024-10-21 13:30:53 +08:00
}
2024-10-29 16:28:14 +08:00
await base.HeartbeatAsync(messageReceivedEvent, SendAction);
2024-10-21 13:30:53 +08:00
}
2024-10-21 16:24:29 +08:00
#region
//68
//32 00
//32 00
//68
//C9 1100'1001. 控制域C。
// D7=1, (终端发送)上行方向。
// D6=1, 此帧来自启动站。
// D5=0, (上行方向)要求访问位。表示终端无事件数据等待访问。
// D4=0, 保留
// D3~D0=9, 功能码。链路测试
//20 32 行政区划码
//90 26 终端地址
//00 主站地址和组地址标志。终端为单地址。 //3220 09 87 2
// 终端启动的发送帧的 MSA 应为 0, 其主站响应帧的 MSA 也应为 0.
//02 应用层功能码。AFN=2, 链路接口检测
//70 0111'0000. 帧序列域。无时间标签、单帧、需要确认。
//00 00 信息点。DA1和DA2全为“0”时表示终端信息点。
//01 00 信息类。F1, 登录。
//44 帧尾,包含用户区数据校验和
//16 帧结束标志
2024-10-21 13:30:53 +08:00
/// <summary>
2024-10-21 16:24:29 +08:00
/// 解析上行命令
2024-10-21 13:30:53 +08:00
/// </summary>
2024-10-21 16:24:29 +08:00
/// <param name="cmd"></param>
2024-10-21 13:30:53 +08:00
/// <returns></returns>
2024-10-21 16:24:29 +08:00
public CommandReulst? AnalysisCmd(string cmd)
2024-10-21 13:30:53 +08:00
{
2024-10-21 16:24:29 +08:00
CommandReulst? commandReulst = null;
2024-10-29 16:28:14 +08:00
var hexStringList = cmd.StringToPairs();
2024-10-21 13:30:53 +08:00
2024-10-21 16:24:29 +08:00
if (hexStringList.Count < hearderLen)
2024-10-21 13:30:53 +08:00
{
2024-10-21 16:24:29 +08:00
return commandReulst;
2024-10-21 13:30:53 +08:00
}
2024-10-21 16:24:29 +08:00
//验证起始字符
if (hexStringList[0] != stx || hexStringList[5] != stx)
2024-10-21 13:30:53 +08:00
{
2024-10-21 16:24:29 +08:00
return commandReulst;
2024-10-21 13:30:53 +08:00
}
2024-10-21 16:24:29 +08:00
var lenHexStr = $"{hexStringList[2]}{hexStringList[1]}";
2024-10-29 16:28:14 +08:00
var lenBin = lenHexStr.HexToBin();
var len = lenBin.Remove(lenBin.Length - 2).BinToDec();
2024-10-21 16:24:29 +08:00
//验证长度
if (hexStringList.Count - 2 != hearderLen + len)
return commandReulst;
2024-10-21 13:30:53 +08:00
2024-10-21 16:24:29 +08:00
var userDataIndex = hearderLen;
var c = hexStringList[userDataIndex];//控制域 1字节
userDataIndex += 1;
2024-10-21 13:30:53 +08:00
2024-10-21 16:24:29 +08:00
var aHexList = hexStringList.Skip(userDataIndex).Take(5).ToList();//地址域 5字节
var a = AnalysisA(aHexList);
2024-10-29 16:28:14 +08:00
var a3Bin = aHexList[4].HexToBin().PadLeft(8, '0');
var mSA = a3Bin.Substring(0, 7).BinToDec();
2024-10-21 16:24:29 +08:00
userDataIndex += 5;
2024-10-21 13:30:53 +08:00
2024-10-29 16:28:14 +08:00
var aFN = (AFN)hexStringList[userDataIndex].HexToDec();//1字节
2024-10-21 16:24:29 +08:00
userDataIndex += 1;
2024-10-21 13:30:53 +08:00
2024-10-29 16:28:14 +08:00
var seq = hexStringList[userDataIndex].HexToBin().PadLeft(8, '0');
2024-10-21 16:24:29 +08:00
var tpV = (TpV)Convert.ToInt32(seq.Substring(0, 1));
var fIRFIN = (FIRFIN)Convert.ToInt32(seq.Substring(1, 2));
var cON = (CON)Convert.ToInt32(seq.Substring(3, 1));
var prseqBin = seq.Substring(4, 4);
userDataIndex += 1;
2024-10-21 13:30:53 +08:00
2024-10-21 16:24:29 +08:00
// (DA2 - 1) * 8 + DA1 = pn
2024-10-29 16:28:14 +08:00
var da1Bin = hexStringList[userDataIndex].HexToBin();
2024-10-21 16:24:29 +08:00
var da1 = da1Bin == "0" ? 0 : da1Bin.Length;
userDataIndex += 1;
2024-10-29 16:28:14 +08:00
var da2 = hexStringList[userDataIndex].HexToDec();
2024-10-21 16:24:29 +08:00
var pn = da2 == 0 ? 0 : (da2 - 1) * 8 + da1;
2024-10-29 16:28:14 +08:00
userDataIndex += 1;
2024-10-21 16:24:29 +08:00
//(DT2*8)+DT1=fn
2024-10-29 16:28:14 +08:00
var dt1Bin = hexStringList[userDataIndex].HexToBin();
2024-10-21 16:24:29 +08:00
var dt1 = dt1Bin != "0" ? dt1Bin.Length : 0;
userDataIndex += 1;
2024-10-29 16:28:14 +08:00
var dt2 = hexStringList[userDataIndex].HexToDec();
2024-10-22 20:57:26 +08:00
var fn = dt2 * 8 + dt1;
2024-10-29 16:28:14 +08:00
userDataIndex += 1;
2024-10-21 13:30:53 +08:00
2024-10-21 16:24:29 +08:00
//数据单元
var datas = hexStringList.Skip(userDataIndex).Take(len + hearderLen - userDataIndex).ToList();
2024-10-21 13:30:53 +08:00
2024-10-21 16:24:29 +08:00
//EC
//Tp
commandReulst = new CommandReulst()
{
A = a,
MSA = mSA,
AFN = aFN,
Seq = new Seq()
{
TpV = tpV,
FIRFIN = fIRFIN,
CON = cON,
2024-10-29 16:28:14 +08:00
PRSEQ = prseqBin.BinToDec(),
2024-10-21 16:24:29 +08:00
},
CmdLength = len,
Pn = pn,
Fn = fn,
HexDatas = datas
};
2024-10-21 13:30:53 +08:00
2024-10-21 16:24:29 +08:00
return commandReulst;
2024-10-21 13:30:53 +08:00
}
/// <summary>
2024-10-21 16:24:29 +08:00
/// 解析地址
2024-10-21 13:30:53 +08:00
/// </summary>
2024-10-21 16:24:29 +08:00
/// <param name="aHexList"></param>
2024-10-21 13:30:53 +08:00
/// <returns></returns>
2024-10-21 16:24:29 +08:00
private string AnalysisA(List<string> aHexList)
2024-10-21 13:30:53 +08:00
{
2024-10-21 16:24:29 +08:00
var a1 = aHexList[1] + aHexList[0];
var a2 = aHexList[3] + aHexList[2];
2024-10-29 16:28:14 +08:00
var a2Dec = a2.HexToDec();
2024-10-21 16:24:29 +08:00
var a3 = aHexList[4];
var a = $"{a1}{a2Dec.ToString().PadLeft(5, '0')}";
return a;
2024-10-21 13:30:53 +08:00
}
/// <summary>
2024-10-21 16:24:29 +08:00
/// 解析上行命令数据包
2024-10-21 13:30:53 +08:00
/// </summary>
2024-10-21 16:24:29 +08:00
/// <param name="commandReulst"></param>
public void AnalysisData(CommandReulst commandReulst)
2024-10-21 13:30:53 +08:00
{
2024-10-21 16:24:29 +08:00
switch (commandReulst.AFN)
2024-10-21 13:30:53 +08:00
{
case AFN.:
2024-10-21 16:24:29 +08:00
//commandReulst.fn
//1:全部确认
//2:全部否认
//3:按数据单元表示确认和否认
//4 硬件安全认证错误应答
2024-10-21 13:30:53 +08:00
break;
case AFN.:
break;
case AFN.:
2024-10-21 16:24:29 +08:00
AnalysisAFN02(commandReulst);
2024-10-21 13:30:53 +08:00
break;
case AFN.:
break;
case AFN.:
break;
case AFN.:
break;
case AFN.:
break;
case AFN.:
break;
case AFN.:
break;
case AFN.:
break;
case AFN.:
break;
case AFN.:
break;
case AFN.:
break;
case AFN.:
break;
case AFN.:
break;
case AFN.:
break;
case AFN.:
break;
default:
break;
}
}
2024-10-21 16:24:29 +08:00
//终端启动发送帧的MSA应为零其主站响应帧的MSA也应为零
public void AnalysisAFN02(CommandReulst commandReulst)
2024-10-21 13:30:53 +08:00
{
2024-10-21 16:24:29 +08:00
if (commandReulst.Fn == 1) //登录
{
Console.WriteLine($"{commandReulst.A},登录:{DateTime.Now}");
var reqParam = new ReqParameter2()
{
AFN = AFN.,
2024-10-22 14:02:11 +08:00
FunCode = (int)CFromStationFunCode.,
2024-10-21 16:24:29 +08:00
PRM = PRM.,
A = commandReulst.A,
Seq = new Seq()
{
TpV = TpV.,
FIRFIN = FIRFIN.,
2024-10-22 20:57:26 +08:00
CON = CON.,
2024-10-21 16:24:29 +08:00
PRSEQ = commandReulst.Seq.PRSEQ,
},
MSA = commandReulst.MSA,
Pn = 0,
Fn = 1
};
commandReulst.ReplyBytes = GetCommandBytes(reqParam);
}
else if (commandReulst.Fn == 2)//退出登录
{
Console.WriteLine($"{commandReulst.A},退出登录:{DateTime.Now}");
}
else if (commandReulst.Fn == 3)//心跳
{
Console.WriteLine($"{commandReulst.A},心跳:{DateTime.Now}");
AnalysisHeartbeat(commandReulst);
}
}
public void AnalysisHeartbeat(CommandReulst commandReulst)
{
if (commandReulst.Seq.TpV == TpV.)
{
//解析
2024-10-21 13:30:53 +08:00
2024-10-21 16:24:29 +08:00
}
if (commandReulst.Seq.CON == CON.)
{
var reqParam = new ReqParameter2()
{
AFN = AFN.,
2024-10-22 14:02:11 +08:00
FunCode = (int)CFromStationFunCode.,
2024-10-21 16:24:29 +08:00
PRM = PRM.,
A = commandReulst.A,
Seq = new Seq()
{
TpV = TpV.,
FIRFIN = FIRFIN.,
CON = CON.,
PRSEQ = commandReulst.Seq.PRSEQ,
},
MSA = commandReulst.MSA,
Pn = 0,
Fn = 1
};
commandReulst.ReplyBytes = GetCommandBytes(reqParam);
}
2024-10-21 13:30:53 +08:00
}
2024-10-21 16:24:29 +08:00
#endregion
2024-09-30 17:10:43 +08:00
}
}