public static Message Create(UInt16 mesId,string tel,UInt16 numSeq,byte[] body) { var mes=new Message(); mes.MessageId = mesId; mes.Tel = tel; mes.NumSeq = numSeq; if (body != null) { mes.BodyBytes = body; } return mes; }
public static Message ToMes(byte[] bt) { if (bt.Length < 2) { return null; } MemoryStream ms = new MemoryStream(); //转义 for (var i = 1; i < bt.Length; i++)//收尾0x7e 不处理 { if (bt[i] == 0x7d && (i + 1) < bt.Length) { var isAnd = false; if (bt[i + 1] == 0x02) { ms.WriteByte(0x7e); isAnd = true; } else if (bt[i + 1] == 0x01) { ms.WriteByte(0x7d); isAnd = true; } if (isAnd) { i += 1; } } else { ms.WriteByte(bt[i]); } } ms.Seek(0, SeekOrigin.Begin); BitReader br=new BitReader(ms); var mes = new Message(); mes.Head.MessageId = br.ReadUInt16(); mes.Head.BodyProp = br.ReadBytes(2); mes.Head.Tel = br.ReadBCD(); mes.Head.NumSeq = br.ReadUInt16(); if (mes.Head.isLong) { mes.Head.MesPackNum = br.ReadBytes(4); } var content = br.ReadBytes(mes.Head.contentLength); mes.BodyBytes=content; return mes; }
/// <summary> /// 终端通用应答 /// </summary> /// <param name="mes"></param> private void TerNormal(Message mes) { var body = mes.BodyBytes; var ms = new MemoryStream(body); var read = new BitReader(ms); var numseq = read.ReadUInt16();//应答流水号 var fromNum = read.ReadUInt16();//平台ID号 var result = read.ReadByte();//结果 Console.WriteLine(result); }
//注册回复 private void Register(Message from) { var mes = Message.Create(); mes.MessageId = 0x8100; mes.Tel=from.Tel; mes.NumSeq=from.NumSeq;//应答流水号 MemoryStream ms = new MemoryStream(); BitWriter bw = new BitWriter(ms); bw.Write(from.NumSeq); byte result = 0;//0:成功;1:车辆已被注册;2:数据库中无该车辆;3:终端已被注册;4:数据库中无该终端 bw.Write(result); bw.Write(Guid.NewGuid().ToByteArray()); mes.BodyBytes = ms.ToArray(); var sendBytes = mes.ToBytes(); EventRespons(sendBytes); }
/// <summary> /// 单次定位应答 /// </summary> /// <param name="mes"></param> private void SingleLocation(Message mes) { var body = mes.BodyBytes; var newBody = new byte[body.Length - 2];//把应答序列号排除 Array.Copy(body, 2, newBody, 0, newBody.Length); this.ReadGPS(newBody); this.NormalMes(mes, 0); }
//平台通用应答 private void NormalMes(Message from,byte res) { var mes = Message.Create(); mes.MessageId = 0x8001; mes.Tel = from.Tel; mes.NumSeq = from.NumSeq;//应答流水号 MemoryStream ms = new MemoryStream(); BitWriter bw = new BitWriter(ms); bw.Write(from.NumSeq); bw.Write(from.MessageId); bw.Write(res); mes.BodyBytes = ms.ToArray(); var sendBytes = mes.ToBytes(); EventRespons(sendBytes); }
/// <summary> /// GPS补录 /// </summary> /// <param name="from"></param> private void GpsAppend(Message from) { var ms = new MemoryStream(from.BodyBytes); var read = new BitReader(ms); var count = read.ReadUInt16();//位置信息个数 var type = read.ReadByte();//0:正常位置批量汇报,1:盲区补报 for (var i = 0; i < count; i++) { var gpsLength = read.ReadUInt16(); var bytes = read.ReadBytes(gpsLength); this.ReadGPS(bytes); } this.NormalMes(from, 0); }
//发送过来的Gps信息 private void Gps(Message from) { var body = from.BodyBytes; ReadGPS(body); NormalMes(from, 0); }
/* * * 0x00:IC 卡读卡成功; 0x01:读卡失败,原因为卡片密钥认证未通过; 0x02:读卡失败,原因为卡片已被锁定; 0x03:读卡失败,原因为卡片被拔出; 0x04:读卡失败,原因为数据校验错误。 * * * */ private void Driver(Message from) { var body = from.BodyBytes; MemoryStream ms = new MemoryStream(body); BitReader br = new BitReader(ms); var state=br.ReadByte(); var driver = new Driver(); var time = br.ReadDateTime();//读取打卡时间 driver.state = 0; if (state == 0x01)//终端时间未校准 使用服务器时间 { time = DateTime.Now; } var ic = br.ReadByte(); if (ic.Equals(0x00))//读卡成功 { //只有certificate 序列号读到 var driverNameLength = br.ReadByte(); var driverName = br.ReadString(driverNameLength); var certBytes = br.ReadBytes(20);//从业资格证 var nBytes=new byte[4]; Array.Copy(certBytes,16,nBytes,0,4); var certificate = BitConverter.ToUInt32(nBytes,0)+""; //certificate var licenceLength = br.ReadByte();//发证机关名称长度 var licenceName = br.ReadString(licenceLength);//发证机关名称 var certificateVaDate = DateTime.Now; //br.ReadDate();//证件有效期 读不到 driver.driverName=driverName; driver.certificate = certificate; driver.licenceName=licenceName; driver.certificateVaDate=certificateVaDate; } driver.time = time; this.EventDriver(driver); }
//终端鉴权 也是通用应答 private void CheckAuth(Message from) { NormalMes(from, 0); }