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.
354 lines
12 KiB
354 lines
12 KiB
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace FangYar.IOT_DH_Plugin
|
|
{
|
|
public class NBHelper
|
|
{
|
|
|
|
#region 上行
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 上行数据包
|
|
public static ApplyData UnBase64toApplyData(string uncodestr)
|
|
{
|
|
byte[] data = Convert.FromBase64String(uncodestr);
|
|
ApplyData appdata = new ApplyData();
|
|
appdata.data_len = CommonHelper.bytesToInt(data);
|
|
appdata.cmdtype = data[2];
|
|
appdata.data = CommonHelper.SplitArray(data, 0, data.Length - 1);
|
|
appdata.CheckSum = CommonHelper.CalcSum(CommonHelper.SplitArray(data, 3, data.Length - 1));
|
|
return appdata;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// byte[]数组转应用数据单元模型
|
|
/// </summary>
|
|
/// <param name="code"></param>
|
|
/// <returns></returns>
|
|
public static ApplyData BytestoApplyData(byte[] data)
|
|
{
|
|
ApplyData appdata = new ApplyData();
|
|
appdata.data_len = CommonHelper.bytesToInt(data);
|
|
appdata.cmdtype = data[2];
|
|
appdata.data = CommonHelper.SplitArray(data, 0, data.Length - 1);
|
|
appdata.CheckSum = CommonHelper.CalcSum(CommonHelper.SplitArray(data, 3, data.Length - 1));
|
|
return appdata;
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region 1注册
|
|
/// <summary>
|
|
/// byte[]数组转应用数据单元模型
|
|
/// </summary>
|
|
/// <param name="code"></param>
|
|
/// <returns></returns>
|
|
public static RegisterData BytestoRegisterData(byte[] data)
|
|
{
|
|
RegisterData appdata = new RegisterData();
|
|
//
|
|
appdata.pversion = data[0].ToString("X2") + " " + data[1].ToString("X2");
|
|
|
|
byte[] iddata = CommonHelper.SplitArray(data, 2, 9);
|
|
appdata.id = CommonHelper.byteToHexStr(iddata);
|
|
|
|
byte[] intervaldata = CommonHelper.SplitArray(data, 10, 13);
|
|
appdata.interval = CommonHelper.bytesToInt(intervaldata);
|
|
|
|
appdata.sversion = "V" + data[15] + "." + data[14];
|
|
|
|
|
|
byte[] reservedata = CommonHelper.SplitArray(data, 16, 17);
|
|
appdata.reserve = CommonHelper.byteToHexStr(reservedata);
|
|
|
|
byte[] sndata = CommonHelper.SplitArray(data, 18, 33);
|
|
appdata.sn = Encoding.ASCII.GetString(sndata);
|
|
|
|
byte[] cciddata = CommonHelper.SplitArray(data, 34, 43);
|
|
appdata.ccid = CommonHelper.byteToHexStr(cciddata);
|
|
|
|
byte[] eddydata = CommonHelper.SplitArray(data, 44, 45);
|
|
appdata.eddy = CommonHelper.bytesToInt(eddydata) / 100;
|
|
|
|
byte[] qydydata = CommonHelper.SplitArray(data, 46, 47);
|
|
appdata.qydy = CommonHelper.bytesToInt(qydydata) / 100;
|
|
|
|
appdata.nbpd = data[48];
|
|
|
|
byte[] imsidata = CommonHelper.SplitArray(data, 49, 56);
|
|
appdata.imsi = CommonHelper.byteToHexStr(imsidata);
|
|
byte[] celliddata = CommonHelper.SplitArray(data, 57, 61);
|
|
appdata.cellid = CommonHelper.byteToHexStr(celliddata);
|
|
|
|
byte[] reserve2data = CommonHelper.SplitArray(data, 62, 75);
|
|
appdata.reserve2 = CommonHelper.byteToHexStr(reserve2data);
|
|
appdata.xtlx = data[76];
|
|
appdata.ext = data[77];
|
|
|
|
return appdata;
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
#region 2发送数据
|
|
|
|
#region 系统状态
|
|
/// <summary>
|
|
/// byte[]数组转系统状态模型
|
|
/// </summary>
|
|
/// <param name="code"></param>
|
|
/// <returns></returns>
|
|
public static List<SysStatus> BytestoSysStatusList(byte[] datas)
|
|
{
|
|
|
|
List<SysStatus> list = new List<SysStatus>();
|
|
int num = datas[1];
|
|
byte[] updatas = CommonHelper.SplitArray(datas, 2, datas.Length - 1);
|
|
for (int i = 0; i < num; i++)
|
|
{
|
|
int sindex = (i - 1) * 10;
|
|
int eindex = i * 10 - 1;
|
|
byte[] data = CommonHelper.SplitArray(datas, sindex, eindex);
|
|
list.Add(BytestoSysStatus(data));
|
|
}
|
|
return list;
|
|
}
|
|
/// <summary>
|
|
/// byte[]数组转系统状态模型
|
|
/// </summary>
|
|
/// <param name="code"></param>
|
|
/// <returns></returns>
|
|
public static SysStatus BytestoSysStatus(byte[] data)
|
|
{
|
|
SysStatus sysStatus = new SysStatus();
|
|
sysStatus.xtlx = data[0];
|
|
sysStatus.xtdz = data[1];
|
|
byte[] status = CommonHelper.SplitArray(data, 2, 3);
|
|
BitArray bits = new BitArray(status);
|
|
sysStatus.xtzt = status;
|
|
sysStatus.xtzt_bit = bits;
|
|
byte[] time = CommonHelper.SplitArray(data, 4, 9);
|
|
sysStatus.sjbq = CommonHelper.BytestoDateTime(time);
|
|
return sysStatus;
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region 部件状态
|
|
/// <summary>
|
|
/// byte[]数组转部件状态模型
|
|
/// </summary>
|
|
/// <param name="code"></param>
|
|
/// <returns></returns>
|
|
public static List<XFDVCStatus> BytestoDVCStatusList(byte[] datas)
|
|
{
|
|
List<XFDVCStatus> list = new List<XFDVCStatus>();
|
|
int num = datas[1];
|
|
byte[] updatas = CommonHelper.SplitArray(datas, 2, datas.Length - 1);
|
|
for (int i = 0; i < num; i++)
|
|
{
|
|
int sindex = (i - 1) * 46;
|
|
int eindex = i * 46 - 1;
|
|
byte[] data = CommonHelper.SplitArray(updatas, sindex, eindex);
|
|
list.Add(BytestoDVCStatus(data));
|
|
}
|
|
return list;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// byte[]数组转部件状态模型
|
|
/// </summary>
|
|
/// <param name="code"></param>
|
|
/// <returns></returns>
|
|
public static XFDVCStatus BytestoDVCStatus(byte[] data)
|
|
{
|
|
XFDVCStatus dvcStatus = new XFDVCStatus();
|
|
dvcStatus.xtlx = data[0];
|
|
dvcStatus.xtdz = data[1];
|
|
dvcStatus.bjlx = data[2];
|
|
byte[] addr = CommonHelper.SplitArray(data, 3, 6);
|
|
dvcStatus.bjdz = CommonHelper.byteToHexStr(addr);
|
|
|
|
byte[] status = CommonHelper.SplitArray(data, 7, 8);
|
|
BitArray bits = new BitArray(status);
|
|
dvcStatus.bjzt = status;
|
|
dvcStatus.bjzt_bit = bits;
|
|
|
|
byte[] sm = CommonHelper.SplitArray(data, 9, 39);
|
|
dvcStatus.bjsm = Encoding.ASCII.GetString(sm);
|
|
|
|
|
|
byte[] time = CommonHelper.SplitArray(data, 40, 45);
|
|
dvcStatus.sjbq = CommonHelper.BytestoDateTime(time);
|
|
return dvcStatus;
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
#region 部件模拟量
|
|
/// <summary>
|
|
/// byte[]数组转部件状态模型
|
|
/// </summary>
|
|
/// <param name="code"></param>
|
|
/// <returns></returns>
|
|
public static XFDVCVal BytestoXFDVCVal(byte[] datas)
|
|
{
|
|
XFDVCVal model = new XFDVCVal();
|
|
model.xtlx = datas[2];
|
|
model.xtdz = datas[3];
|
|
model.bjlx = datas[4];
|
|
byte[] addr = CommonHelper.SplitArray(datas, 5, 8);
|
|
model.bjdz = CommonHelper.byteToHexStr(addr);
|
|
int num = datas[9];
|
|
byte[] updatas = CommonHelper.SplitArray(datas, 10, datas.Length - 7);
|
|
for (int i = 0; i < num; i++)
|
|
{
|
|
int sindex = (i - 1) * 4;
|
|
int eindex = i * 4 - 1;
|
|
byte[] data = CommonHelper.SplitArray(updatas, sindex, eindex);
|
|
model.mnllist.Add(BytestoMNLData(data));
|
|
}
|
|
byte[] time = CommonHelper.SplitArray(datas, datas.Length - 6, datas.Length - 1);
|
|
model.sjbq = CommonHelper.BytestoDateTime(time);
|
|
return model;
|
|
}
|
|
|
|
/// <summary>
|
|
/// byte[]数组转部件状态模型
|
|
/// </summary>
|
|
/// <param name="code"></param>
|
|
/// <returns></returns>
|
|
public static MNLData BytestoMNLData(byte[] data)
|
|
{
|
|
MNLData model = new MNLData();
|
|
byte[] lx = CommonHelper.SplitArray(data, 0, 1);
|
|
model.mnllx = CommonHelper.bytesToInt(lx);
|
|
byte[] val = CommonHelper.SplitArray(data,2,3);
|
|
model.mnlval = CommonHelper.bytesToInt(val);
|
|
return model;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
|
|
#region 3消音
|
|
|
|
/// <summary>
|
|
/// 组下发消音数据
|
|
/// </summary>
|
|
/// <param name="code"></param>
|
|
/// <returns></returns>
|
|
public static byte[] XY_CMDtoByte(XYDATA xymodel)
|
|
{
|
|
List<byte> list = new List<byte>();
|
|
byte[] xy_byte = new byte[] { 249, 1, };
|
|
byte[] xtlx = new byte[] { (byte)xymodel.xtlx };
|
|
byte[] xtdz = BitConverter.GetBytes(xymodel.xtdz);
|
|
byte[] bjlx = BitConverter.GetBytes(xymodel.bjlx);
|
|
byte[] bjdz = BitConverter.GetBytes(xymodel.bjdz);
|
|
byte[] xyzq = BitConverter.GetBytes(xymodel.xyzq);
|
|
list.AddRange(xy_byte);
|
|
list.AddRange(xtlx);
|
|
list.AddRange(xtdz);
|
|
list.AddRange(bjlx);
|
|
list.AddRange(bjdz);
|
|
list.AddRange(xyzq);
|
|
return list.ToArray();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 4操作命令
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region 组织下发命令
|
|
/// <summary>
|
|
/// 下发数据
|
|
/// </summary>
|
|
/// <param name="serial_no"></param>
|
|
/// <returns></returns>
|
|
public static string XF_string_DATA(int serial_no, int cmd, byte[] data)
|
|
{
|
|
byte[] retu= XF_byre_DATA(serial_no,cmd,data);
|
|
return CommonHelper.UnBase64String(retu);
|
|
}
|
|
/// <summary>
|
|
/// 下发数据
|
|
/// </summary>
|
|
/// <param name="serial_no"></param>
|
|
/// <returns></returns>
|
|
public static byte[] XF_byre_DATA(int serial_no,int cmd,byte[] data)
|
|
{
|
|
byte[] nos = BitConverter.GetBytes(serial_no);
|
|
byte[] byte1 = new byte[] { nos[3],nos[4], (byte)cmd };
|
|
byte[] bt2 = CommonHelper.MergerArray(byte1,data);
|
|
int cknum = CommonHelper.CalcSum(bt2);
|
|
bt2[bt2.Length]= (byte)cknum;
|
|
return bt2;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 回复确认否认
|
|
|
|
/// <summary>
|
|
/// 确认回复
|
|
/// </summary>
|
|
/// <param name="serial_no"></param>
|
|
/// <returns></returns>
|
|
public static string Cmd_QR_DATA(int serial_no)
|
|
{
|
|
byte[] bdata = Cmd_QR_byte(serial_no);
|
|
return CommonHelper.UnBase64String(bdata);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 确认回复
|
|
/// </summary>
|
|
/// <param name="serial_no"></param>
|
|
/// <returns></returns>
|
|
public static byte[] Cmd_QR_byte(int serial_no) {
|
|
byte[] Confirm = new byte[] { (byte)serial_no,4,0,0,3,3 };
|
|
return Confirm;
|
|
}
|
|
/// <summary>
|
|
/// 否认回复
|
|
/// </summary>
|
|
/// <param name="serial_no"></param>
|
|
/// <returns></returns>
|
|
public static byte[] Cmd_FR_byte(int serial_no)
|
|
{
|
|
byte[] Confirm = new byte[] { (byte)serial_no, 4, 0, 0, 3, 3 };
|
|
return Confirm;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 否定回复
|
|
/// </summary>
|
|
/// <param name="serial_no"></param>
|
|
/// <returns></returns>
|
|
public static byte[] Cmd__byte(int serial_no,int cmd,byte[] data)
|
|
{
|
|
byte[] Confirm = new byte[] { (byte)serial_no, 4, 0, 0, 3, 3 };
|
|
return Confirm;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|
|
|