You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
6.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BatCharging.Model;
namespace BatCharging.Service
{
/// <summary>
/// 站控设置尖峰平谷时间段标识消息封装-3.4.9
/// </summary>
public class MsgPeakTimeEncoder
{
/// <summary>
/// 获取鉴权消息包字节列表
/// </summary>
/// <returns></returns>
public byte[] GetMsgPeakTimePeriod(byte[] destAddr, SharpPeakTimeRangeIde timeRng)
{
byte[] result = null;
APCI headAPCI = new APCI();
headAPCI.StartChar = CmnChargerConst.APCI_START_CHAR;
headAPCI.PackLen = 0; //临时赋值为零,具体数值看下面的计算结果
headAPCI.CtlArea = 0; //控制域
headAPCI.DestAddr = destAddr;//目的地址
headAPCI.SrcAddr = 0;//源地址本系统中源地址统一为00000000即为0
byte randomNum = CmnChargerParam.GetByteRandomNum();
ASDU headASDU = new ASDU();
headASDU.FrameTypeNo = 45; //帧类型号
headASDU.MsgBodyCount = 1; //信息体个数
headASDU.TransReason = 3; //传送原因
headASDU.PublicAddr = 0; //公共地址
headASDU.MsgBodyAddr = new byte[] { 0, 0, 0 }; //信息体地址
if(timeRng!=null)
{
timeRng.RecordType = 47; //记录类型
headASDU.MsgBodyContents = GetMsgContents(timeRng); //信息体
headAPCI.PackLen = Convert.ToUInt16(24 + headASDU.MsgBodyContents.Length); //报文长度
}
List<byte> lstResult = MsgHeadApciEncoder.BuildBaseAPCI(headAPCI);
lstResult.AddRange(MsgHeadAsduEncoder.BuildBaseASDU(headASDU).ToArray());
result = lstResult.ToArray();
return result;
}
/// <summary>
/// 获取鉴权消息体字节数组
/// </summary>
/// <param name="auth"></param>
/// <returns>鉴权消息体字节数组</returns>
private byte[] GetMsgContents(SharpPeakTimeRangeIde timeRng)
{
byte[] results = null;
if (timeRng != null)
{
List<byte> lstContent = new List<byte>();
lstContent.Add(timeRng.RecordType);
lstContent.Add(timeRng.NumTimePeriods);
for(int i=0;i<timeRng.NumTimePeriods;i++)
{
string strTimePeriod = "";
byte peakIden = 0;
switch (i)
{
case 0:
{
strTimePeriod = timeRng.StartTimePeriod1;
peakIden = timeRng.TimePeriodPeakIden1;
break;
}
case 1:
{
strTimePeriod = timeRng.StartTimePeriod2;
peakIden = timeRng.TimePeriodPeakIden2;
break;
}
case 2:
{
strTimePeriod = timeRng.StartTimePeriod3;
peakIden = timeRng.TimePeriodPeakIden3;
break;
}
case 3:
{
strTimePeriod = timeRng.StartTimePeriod4;
peakIden = timeRng.TimePeriodPeakIden4;
break;
}
case 4:
{
strTimePeriod = timeRng.StartTimePeriod5;
peakIden = timeRng.TimePeriodPeakIden5;
break;
}
case 5:
{
strTimePeriod = timeRng.StartTimePeriod6;
peakIden = timeRng.TimePeriodPeakIden6;
break;
}
case 6:
{
strTimePeriod = timeRng.StartTimePeriod7;
peakIden = timeRng.TimePeriodPeakIden7;
break;
}
case 7:
{
strTimePeriod = timeRng.StartTimePeriod8;
peakIden = timeRng.TimePeriodPeakIden8;
break;
}
}
if (strTimePeriod.Length > 0)
{
byte[] temps = GetTimePeriodBytes(strTimePeriod);
lstContent.Add(temps[0]);
lstContent.Add(temps[1]);
lstContent.Add(peakIden);
}
}
results = lstContent.ToArray();
}
return results;
}
/// <summary>
/// 把时段的BCD码字符串("HH:mm")转换为两个字节的字节数组
/// </summary>
/// <param name=""></param>
/// <returns></returns>
private byte[] GetTimePeriodBytes(string strTimePeriod)
{
byte[] results = new byte[] { 0, 0 };
if (!string.IsNullOrEmpty(strTimePeriod))
{
string[] strTemps = strTimePeriod.Split(":");
if(strTemps!=null)
{
if(strTemps.Length==2)
{
results[0] = ByteUtils.BCDToByte(Convert.ToByte(strTemps[0]));
results[1] = ByteUtils.BCDToByte(Convert.ToByte(strTemps[1]));
}
}
}
return results;
}
}
}