using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BatCharging.Model;
namespace BatCharging.Service
{
///
/// 心跳包报文封装
///
public class MsgHeartResEncoder
{
///
/// 获取监控平台应答心跳报文字节数组
///
///
public byte[] GetMsgPackHeartRes(byte[] destAddr)
{
try
{
byte[] result = null;
APCI headAPCI = new APCI();
headAPCI.StartChar = CmnChargerConst.APCI_START_CHAR;
headAPCI.PackLen = 0; //临时赋值为零,具体数值看下面的计算结果
headAPCI.CtlArea = 0; //控制域
//目的地址
headAPCI.DestAddr = destAddr;
//源地址,本系统中,源地址统一为:00000000(即为0)
headAPCI.SrcAddr = 0;
byte randomNum = CmnChargerParam.GetByteRandomNum();
ASDU headASDU = new ASDU();
headASDU.FrameTypeNo = 45; //帧类型号
headASDU.MsgBodyCount = 0; //信息体个数
headASDU.TransReason = 4; //传送原因
headASDU.PublicAddr = 0; //公共地址
headASDU.MsgBodyAddr = new byte[] { 0, 0, 0 }; //信息体地址
HeartBeatRes heartBeat = new HeartBeatRes();
heartBeat.RecordType = 14;
heartBeat.Result = 0;
headASDU.MsgBodyContents = GetMsgContents(heartBeat); //信息体
headAPCI.PackLen = Convert.ToUInt16(24 + headASDU.MsgBodyContents.Length); //报文长度
List lstResult = MsgHeadApciEncoder.BuildBaseAPCI(headAPCI);
lstResult.AddRange(MsgHeadAsduEncoder.BuildBaseASDU(headASDU).ToArray());
result = lstResult.ToArray();
return result;
}
catch (Exception ex)
{
ex.ToString();
return null;
}
}
///
/// 获取心跳回复消息体字节数组
///
///
/// 心跳回复消息体字节数组
private byte[] GetMsgContents(HeartBeatRes heartBeat)
{
byte[] results = null;
if (heartBeat != null)
{
List lstContent = new List();
lstContent.Add(heartBeat.RecordType);
lstContent.Add(heartBeat.Result);
results = lstContent.ToArray();
}
return results;
}
}
}