/// <summary> /// /// </summary> /// <param name="reader"></param> /// <param name="config"></param> /// <returns></returns> public JT808Header Deserialize(ref JT808MessagePackReader reader, IJT808Config config) { JT808Header jT808Header = new JT808Header(); // 1.消息ID jT808Header.MsgId = reader.ReadUInt16(); // 2.消息体属性 jT808Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty(reader.ReadUInt16()); if (jT808Header.MessageBodyProperty.VersionFlag) { // 2019 版本 // 3.协议版本号 jT808Header.ProtocolVersion = reader.ReadByte(); // 4.终端手机号 jT808Header.TerminalPhoneNo = reader.ReadBCD(20, config.Trim); } else { // 2013 版本 // 3.终端手机号 jT808Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim); } jT808Header.MsgNum = reader.ReadUInt16(); // 4.判断有无分包 if (jT808Header.MessageBodyProperty.IsPackage) { //5.读取消息包总数 jT808Header.PackgeCount = reader.ReadUInt16(); //6.读取消息包序号 jT808Header.PackageIndex = reader.ReadUInt16(); } return jT808Header; }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <param name="config"></param> /// <returns></returns> public JT808_CarDVR_Down_0xC4 Deserialize(ref JT808MessagePackReader reader, IJT808Config config) { JT808_CarDVR_Down_0xC4 value = new JT808_CarDVR_Down_0xC4(); value.RealTime = reader.ReadDateTime_yyMMddHHmmss(); value.FirstInstallTime = reader.ReadDateTime_yyMMddHHmmss(); value.FirstMileage = reader.ReadBCD(8); value.TotalMilage = reader.ReadBCD(8); return(value); }
public JT808_CarDVR_Up_0x03 Deserialize(ref JT808MessagePackReader reader, IJT808Config config) { JT808_CarDVR_Up_0x03 value = new JT808_CarDVR_Up_0x03(); value.RealTime = reader.ReadDateTime6(); value.FirstInstallTime = reader.ReadDateTime6(); value.FirstMileage = reader.ReadBCD(8); value.TotalMilage = reader.ReadBCD(8); return(value); }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <param name="writer"></param> /// <param name="config"></param> public void Analyze(ref JT808MessagePackReader reader, Utf8JsonWriter writer, IJT808Config config) { JT808_CarDVR_Down_0xC4 value = new JT808_CarDVR_Down_0xC4(); value.RealTime = reader.ReadDateTime_yyMMddHHmmss(); writer.WriteString($"[{value.RealTime:yyMMddHHmmss}]当前时间", value.RealTime.ToString("yyyy-MM-dd HH:mm:ss")); value.FirstInstallTime = reader.ReadDateTime_yyMMddHHmmss(); writer.WriteString($"[{value.FirstInstallTime:yyMMddHHmmss}]初次安装时间", value.FirstInstallTime.ToString("yyyy-MM-dd HH:mm:ss")); value.FirstMileage = reader.ReadBCD(8); writer.WriteString($"[{value.FirstMileage}]初始里程", value.FirstMileage); value.TotalMilage = reader.ReadBCD(8); writer.WriteString($"[{value.TotalMilage}]累计里程", value.TotalMilage); }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <param name="writer"></param> /// <param name="config"></param> public void Analyze(ref JT808MessagePackReader reader, Utf8JsonWriter writer, IJT808Config config) { JT808_0x8103_0x007C_TimerWakeDayParamter value = new JT808_0x8103_0x007C_TimerWakeDayParamter(); value.TimerWakeEnableFlag = reader.ReadByte(); writer.WriteString($"[{value.TimerWakeEnableFlag.ReadNumber()}]定时唤醒启用标志", TimerWakeEnableFlagDisplay(value.TimerWakeEnableFlag)); value.TimePeriod1WakeTime = reader.ReadBCD(4); writer.WriteString($"[{value.TimePeriod1WakeTime}]时间段1唤醒时间", value.TimePeriod1WakeTime); value.TimePeriod1CloseTime = reader.ReadBCD(4); writer.WriteString($"[{value.TimePeriod1CloseTime}]时间段1关闭时间", value.TimePeriod1CloseTime); value.TimePeriod2WakeTime = reader.ReadBCD(4); writer.WriteString($"[{value.TimePeriod2WakeTime}]时间段2唤醒时间", value.TimePeriod2WakeTime); value.TimePeriod2CloseTime = reader.ReadBCD(4); writer.WriteString($"[{value.TimePeriod2CloseTime}]时间段2关闭时间", value.TimePeriod2CloseTime); value.TimePeriod3WakeTime = reader.ReadBCD(4); writer.WriteString($"[{value.TimePeriod3WakeTime}]时间段3唤醒时间", value.TimePeriod3WakeTime); value.TimePeriod3CloseTime = reader.ReadBCD(4); writer.WriteString($"[{value.TimePeriod3CloseTime}]时间段3关闭时间", value.TimePeriod3CloseTime); value.TimePeriod4WakeTime = reader.ReadBCD(4); writer.WriteString($"[{value.TimePeriod4WakeTime}]时间段4唤醒时间", value.TimePeriod4WakeTime); value.TimePeriod4CloseTime = reader.ReadBCD(4); writer.WriteString($"[{value.TimePeriod4CloseTime}]时间段4关闭时间", value.TimePeriod4CloseTime); string TimerWakeEnableFlagDisplay(byte TimerWakeEnableFlag) { string timerWakeEnableFlagDisplay = string.Empty; timerWakeEnableFlagDisplay += (TimerWakeEnableFlag & 0x01) == 1 ? ",时间段1唤醒时间启用" : ""; timerWakeEnableFlagDisplay += ((TimerWakeEnableFlag >> 1) & 0x01) == 1 ? ",时间段2唤醒时间启用" : ""; timerWakeEnableFlagDisplay += ((TimerWakeEnableFlag >> 2) & 0x01) == 1 ? ",时间段3唤醒时间启用" : ""; timerWakeEnableFlagDisplay += ((TimerWakeEnableFlag >> 3) & 0x01) == 1 ? ",时间段4唤醒时间启用" : ""; return(timerWakeEnableFlagDisplay.Length > 0 ? timerWakeEnableFlagDisplay.Substring(1) : ""); } }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <param name="config"></param> /// <returns></returns> public JT808_0x0107 Deserialize(ref JT808MessagePackReader reader, IJT808Config config) { JT808_0x0107 jT808_0X0107 = new JT808_0x0107(); jT808_0X0107.TerminalType = reader.ReadUInt16(); if (reader.Version == JT808Version.JTT2019) { jT808_0X0107.MakerId = reader.ReadString(11); jT808_0X0107.TerminalModel = reader.ReadString(30); jT808_0X0107.TerminalId = reader.ReadString(30); } else { jT808_0X0107.MakerId = reader.ReadString(5); jT808_0X0107.TerminalModel = reader.ReadString(20); jT808_0X0107.TerminalId = reader.ReadString(7); } jT808_0X0107.Terminal_SIM_ICCID = reader.ReadBCD(20, config.Trim); jT808_0X0107.Terminal_Hardware_Version_Length = reader.ReadByte(); jT808_0X0107.Terminal_Hardware_Version_Num = reader.ReadString(jT808_0X0107.Terminal_Hardware_Version_Length); jT808_0X0107.Terminal_Firmware_Version_Length = reader.ReadByte(); jT808_0X0107.Terminal_Firmware_Version_Num = reader.ReadString(jT808_0X0107.Terminal_Firmware_Version_Length); jT808_0X0107.GNSSModule = reader.ReadByte(); jT808_0X0107.CommunicationModule = reader.ReadByte(); return(jT808_0X0107); }
public void Analyze(ref JT808MessagePackReader reader, Utf8JsonWriter writer, IJT808Config config) { JT808_CarDVR_Up_0x03 value = new JT808_CarDVR_Up_0x03(); var hex = reader.ReadVirtualArray(6); value.RealTime = reader.ReadDateTime6(); writer.WriteString($"[{hex.ToArray().ToHexString()}]实时时间", value.RealTime); hex = reader.ReadVirtualArray(6); value.FirstInstallTime = reader.ReadDateTime6(); writer.WriteString($"[{hex.ToArray().ToHexString()}]初次安装时间", value.RealTime); hex = reader.ReadVirtualArray(4); value.FirstMileage = reader.ReadBCD(8); writer.WriteString($"[{hex.ToArray().ToHexString()}]初始里程", value.FirstMileage); hex = reader.ReadVirtualArray(4); value.TotalMilage = reader.ReadBCD(8); writer.WriteString($"[{hex.ToArray().ToHexString()}]累计里程", value.TotalMilage); }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <param name="config"></param> /// <returns></returns> public JT808_CarDVR_Up_0x00 Deserialize(ref JT808MessagePackReader reader, IJT808Config config) { JT808_CarDVR_Up_0x00 value = new JT808_CarDVR_Up_0x00(); value.StandardYear = reader.ReadBCD(2); value.ModifyNumber = reader.ReadByte(); return(value); }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <param name="writer"></param> /// <param name="config"></param> public void Analyze(ref JT808MessagePackReader reader, Utf8JsonWriter writer, IJT808Config config) { JT808_CarDVR_Up_0x00 value = new JT808_CarDVR_Up_0x00(); value.StandardYear = reader.ReadBCD(2); writer.WriteString($"[{value.StandardYear}]记录仪执行标准年号", value.StandardYear); value.ModifyNumber = reader.ReadByte(); writer.WriteNumber($"[{value.ModifyNumber.ReadNumber()}]修改单号", value.ModifyNumber); }
public void ReadBCDTest2() { byte[] bytes = "7E 12 34 7E".ToHexBytes(); JT808MessagePackReader jT808MessagePackReader = new JT808MessagePackReader(bytes); Assert.Equal(JT808Package.BeginFlag, jT808MessagePackReader.ReadStart()); Assert.Equal("1234", jT808MessagePackReader.ReadBCD(5)); Assert.Equal(JT808Package.EndFlag, jT808MessagePackReader.ReadEnd()); }
public JT808Header Deserialize(ref JT808MessagePackReader reader, IJT808Config config) { JT808Header jT808Header = new JT808Header(); // 1.消息ID jT808Header.MsgId = reader.ReadUInt16(); // 2.消息体属性 jT808Header.MessageBodyProperty = JT808HeaderMessageBodyPropertyFormatter.Instance.Deserialize(ref reader, config); // 3.终端手机号 jT808Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength); jT808Header.MsgNum = reader.ReadUInt16(); return(jT808Header); }
public JT808_0x8103_0x007C_TimerWakeDayParamter Deserialize(ref JT808MessagePackReader reader, IJT808Config config) { JT808_0x8103_0x007C_TimerWakeDayParamter jT808_0x8103_0x007C_TimerWakeDayParamter = new JT808_0x8103_0x007C_TimerWakeDayParamter(); jT808_0x8103_0x007C_TimerWakeDayParamter.TimerWakeEnableFlag = reader.ReadByte(); jT808_0x8103_0x007C_TimerWakeDayParamter.TimePeriod1WakeTime = reader.ReadBCD(4); jT808_0x8103_0x007C_TimerWakeDayParamter.TimePeriod1CloseTime = reader.ReadBCD(4); jT808_0x8103_0x007C_TimerWakeDayParamter.TimePeriod2WakeTime = reader.ReadBCD(4); jT808_0x8103_0x007C_TimerWakeDayParamter.TimePeriod2CloseTime = reader.ReadBCD(4); jT808_0x8103_0x007C_TimerWakeDayParamter.TimePeriod3WakeTime = reader.ReadBCD(4); jT808_0x8103_0x007C_TimerWakeDayParamter.TimePeriod3CloseTime = reader.ReadBCD(4); jT808_0x8103_0x007C_TimerWakeDayParamter.TimePeriod4WakeTime = reader.ReadBCD(4); jT808_0x8103_0x007C_TimerWakeDayParamter.TimePeriod4CloseTime = reader.ReadBCD(4); return(jT808_0x8103_0x007C_TimerWakeDayParamter); }
public JT808HeaderPackage Deserialize(ref JT808MessagePackReader reader, IJT808Config config) { // 1. 验证校验和 if (!config.SkipCRCCode) { if (reader.CheckXorCodeVali) { throw new JT808Exception(JT808ErrorCode.CheckCodeNotEqual, $"{reader.RealCheckXorCode}!={reader.CalculateCheckXorCode}"); } } JT808HeaderPackage jT808Package = new JT808HeaderPackage(); // ---------------开始解包-------------- // 2.读取起始位置 jT808Package.Begin = reader.ReadStart(); // 3.读取头部信息 jT808Package.Header = new JT808Header(); // 3.1.读取消息Id jT808Package.Header.MsgId = reader.ReadUInt16(); // 3.2.读取消息体属性 jT808Package.Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty(); ushort messageBodyPropertyValue = reader.ReadUInt16(); // 3.2.1.解包消息体属性 jT808Package.Header.MessageBodyProperty.Unwrap(messageBodyPropertyValue, config); // 3.3.读取终端手机号 jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength); // 3.4.读取消息流水号 jT808Package.Header.MsgNum = reader.ReadUInt16(); // 3.5.判断有无分包 if (jT808Package.Header.MessageBodyProperty.IsPackge) { //3.5.1.读取消息包总数 jT808Package.Header.MessageBodyProperty.PackgeCount = reader.ReadUInt16(); //3.5.2.读取消息包序号 jT808Package.Header.MessageBodyProperty.PackageIndex = reader.ReadUInt16(); } // 4.处理数据体 // 4.1.判断有无数据体 if (jT808Package.Header.MessageBodyProperty.DataLength > 0) { jT808Package.Bodies = reader.ReadContent().ToArray(); } // 5.读取校验码 jT808Package.CheckCode = reader.ReadByte(); // 6.读取终止位置 jT808Package.End = reader.ReadEnd(); // ---------------解包完成-------------- return(jT808Package); }
public JT808Header Deserialize(ref JT808MessagePackReader reader, IJT808Config config) { JT808Header jT808Header = new JT808Header(); // 1.消息ID jT808Header.MsgId = reader.ReadUInt16(); // 2.消息体属性 jT808Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty(reader.ReadUInt16()); // 3.终端手机号 jT808Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim); jT808Header.MsgNum = reader.ReadUInt16(); // 4.判断有无分包 if (jT808Header.MessageBodyProperty.IsPackage) { //5.读取消息包总数 jT808Header.PackgeCount = reader.ReadUInt16(); //6.读取消息包序号 jT808Header.PackageIndex = reader.ReadUInt16(); } return(jT808Header); }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <param name="config"></param> /// <returns></returns> public JT808Package Deserialize(ref JT808MessagePackReader reader, IJT808Config config) { // 1. 验证校验和 if (!config.SkipCRCCode) { if (!reader.CheckXorCodeVali) { throw new JT808Exception(JT808ErrorCode.CheckCodeNotEqual, $"{reader.RealCheckXorCode}!={reader.CalculateCheckXorCode}"); } } JT808Package jT808Package = new JT808Package(); // ---------------开始解包-------------- // 2.读取起始位置 jT808Package.Begin = reader.ReadStart(); // 3.读取头部信息 jT808Package.Header = new JT808Header(); // 3.1.读取消息Id jT808Package.Header.MsgId = reader.ReadUInt16(); // 3.2.读取消息体属性 jT808Package.Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty(reader.ReadUInt16()); if (reader.Version == JT808Version.JTT2013Force) { jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim); reader.Version = JT808Version.JTT2013; } else { if (reader.Version == JT808Version.JTT2019 || jT808Package.Header.MessageBodyProperty.VersionFlag) { //2019版本 jT808Package.Header.ProtocolVersion = reader.ReadByte(); // 3.4.读取终端手机号 jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(20, config.Trim); reader.Version = JT808Version.JTT2019; } else { //2013版本 // 3.3.读取终端手机号 jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim); } } jT808Package.Version = reader.Version; // 3.4.读取消息流水号 jT808Package.Header.MsgNum = reader.ReadUInt16(); // 3.5.判断有无分包 if (jT808Package.Header.MessageBodyProperty.IsPackage) { //3.5.1.读取消息包总数 jT808Package.Header.PackgeCount = reader.ReadUInt16(); //3.5.2.读取消息包序号 jT808Package.Header.PackageIndex = reader.ReadUInt16(); } // 4.处理数据体 // 4.1.判断有无数据体 if (jT808Package.Header.MessageBodyProperty.DataLength > 0) { if (config.MsgIdFactory.TryGetValue(jT808Package.Header.MsgId, out object instance)) { if (jT808Package.Header.MessageBodyProperty.IsPackage) { //读取分包的数据体 try { jT808Package.SubDataBodies = reader.ReadArray(jT808Package.Header.MessageBodyProperty.DataLength).ToArray(); } catch (Exception ex) { throw new JT808Exception(JT808ErrorCode.BodiesParseError, ex); } } else { try { //4.2.处理消息体 jT808Package.Bodies = JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize( instance, ref reader, config); } catch (Exception ex) { throw new JT808Exception(JT808ErrorCode.BodiesParseError, ex); } } } } // 5.读取校验码 jT808Package.CheckCode = reader.ReadByte(); // 6.读取终止位置 jT808Package.End = reader.ReadEnd(); // ---------------解包完成-------------- return(jT808Package); }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <param name="writer"></param> /// <param name="config"></param> public void Analyze(ref JT808MessagePackReader reader, Utf8JsonWriter writer, IJT808Config config) { // ---------------开始解析对象-------------- writer.WriteStartObject(); // 1. 验证校验和 if (!reader.CheckXorCodeVali) { writer.WriteString("检验和错误", $"{reader.RealCheckXorCode}!={reader.CalculateCheckXorCode}"); } // 2.读取起始位置 byte start = reader.ReadEnd(); writer.WriteNumber($"[{start.ReadNumber()}]开始", start); var msgid = reader.ReadUInt16(); writer.WriteNumber($"[{msgid.ReadNumber()}]消息Id", msgid); ushort messageBodyPropertyValue = reader.ReadUInt16(); var headerMessageBodyProperty = new JT808HeaderMessageBodyProperty(messageBodyPropertyValue); //消息体属性对象 开始 writer.WriteStartObject("消息体属性对象"); ReadOnlySpan <char> messageBodyPropertyReadOnlySpan = messageBodyPropertyValue.ReadBinary(); writer.WriteNumber($"[{messageBodyPropertyReadOnlySpan.ToString()}]消息体属性", messageBodyPropertyValue); if (reader.Version == JT808Version.JTT2013Force) { reader.Version = JT808Version.JTT2013; writer.WriteString("版本号", JT808Version.JTT2013.ToString()); writer.WriteNumber("[bit15]保留", 0); writer.WriteNumber("[bit14]保留", 0); writer.WriteBoolean("[bit13]是否分包", headerMessageBodyProperty.IsPackage); writer.WriteString("[bit10~bit12]数据加密", headerMessageBodyProperty.Encrypt.ToString()); writer.WriteNumber("[bit0~bit9]消息体长度", headerMessageBodyProperty.DataLength); writer.WriteEndObject(); //2013版本 // 3.3.读取终端手机号 var terminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, false); //消息体属性对象 结束 writer.WriteString($"[{terminalPhoneNo.PadLeft(config.TerminalPhoneNoLength, '0')}]终端手机号", terminalPhoneNo); } else { if (reader.Version == JT808Version.JTT2019 || headerMessageBodyProperty.VersionFlag) { reader.Version = JT808Version.JTT2019; writer.WriteString("版本号", JT808Version.JTT2019.ToString()); writer.WriteNumber("[bit15]保留", 0); writer.WriteBoolean("[bit14]协议版本标识", headerMessageBodyProperty.VersionFlag); writer.WriteBoolean("[bit13]是否分包", headerMessageBodyProperty.IsPackage); writer.WriteString("[bit10~bit12]数据加密", headerMessageBodyProperty.Encrypt.ToString()); writer.WriteNumber("[bit0~bit9]消息体长度", headerMessageBodyProperty.DataLength); //消息体属性对象 结束 writer.WriteEndObject(); //2019版本 var protocolVersion = reader.ReadByte(); writer.WriteNumber($"[{protocolVersion.ReadNumber()}]协议版本号(2019)", protocolVersion); // 3.4.读取终端手机号 var terminalPhoneNo = reader.ReadBCD(20, config.Trim); writer.WriteString($"[{terminalPhoneNo.PadLeft(20, '0')}]终端手机号", terminalPhoneNo); } else { reader.Version = JT808Version.JTT2013; writer.WriteString("版本号", JT808Version.JTT2013.ToString()); writer.WriteNumber("[bit15]保留", 0); writer.WriteNumber("[bit14]保留", 0); writer.WriteBoolean("[bit13]是否分包", headerMessageBodyProperty.IsPackage); writer.WriteString("[bit10~bit12]数据加密", headerMessageBodyProperty.Encrypt.ToString()); writer.WriteNumber("[bit0~bit9]消息体长度", headerMessageBodyProperty.DataLength); writer.WriteEndObject(); //2013版本 // 3.3.读取终端手机号 var terminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, false); //消息体属性对象 结束 writer.WriteString($"[{terminalPhoneNo.PadLeft(config.TerminalPhoneNoLength, '0')}]终端手机号", terminalPhoneNo); } } // 3.4.读取消息流水号 var msgNum = reader.ReadUInt16(); writer.WriteNumber($"[{msgNum.ReadNumber()}]消息流水号", msgNum); // 3.5.判断有无分包 uint packgeCount = 0, packageIndex = 0; if (headerMessageBodyProperty.IsPackage) { //3.5.1.读取消息包总数 packgeCount = reader.ReadUInt16(); writer.WriteNumber($"[{packgeCount.ReadNumber()}]消息包总数", packgeCount); //3.5.2.读取消息包序号 packageIndex = reader.ReadUInt16(); writer.WriteNumber($"[{packageIndex.ReadNumber()}]消息包序号", packageIndex); } // 4.处理数据体 // 4.1.判断有无数据体 if (headerMessageBodyProperty.DataLength > 0) { //数据体属性对象 开始 writer.WriteStartObject("数据体对象"); string description = "数据体"; if (headerMessageBodyProperty.IsPackage) { //读取分包的数据体 try { writer.WriteString($"[分包]数据体", reader.ReadArray(reader.ReadCurrentRemainContentLength()).ToArray().ToHexString()); } catch (IndexOutOfRangeException ex) { writer.WriteString($"数据体解析异常,无可用数据体进行解析", ex.StackTrace); } catch (ArgumentOutOfRangeException ex) { writer.WriteString($"[分包]数据体解析异常,无可用数据体进行解析", ex.StackTrace); } catch (Exception ex) { writer.WriteString($"[分包]数据体异常", ex.StackTrace); } } else { if (config.MsgIdFactory.TryGetValue(msgid, out object instance)) { if (instance is IJT808Description jT808Description) { //4.2.处理消息体 description = jT808Description.Description; } try { //数据体长度正常 writer.WriteString($"{description}", reader.ReadVirtualArray(reader.ReadCurrentRemainContentLength()).ToArray().ToHexString()); if (instance is IJT808Analyze analyze) { //4.2.处理消息体 analyze.Analyze(ref reader, writer, config); } } catch (IndexOutOfRangeException ex) { writer.WriteString($"数据体解析异常,无可用数据体进行解析", ex.StackTrace); } catch (ArgumentOutOfRangeException ex) { writer.WriteString($"数据体解析异常,无可用数据体进行解析", ex.StackTrace); } catch (Exception ex) { writer.WriteString($"数据体异常", ex.StackTrace); } } else { writer.WriteString($"[未知]数据体", reader.ReadArray(reader.ReadCurrentRemainContentLength()).ToArray().ToHexString()); } } //数据体属性对象 结束 writer.WriteEndObject(); } else { if (config.MsgIdFactory.TryGetValue(msgid, out object instance)) { //数据体属性对象 开始 writer.WriteStartObject("数据体对象"); string description = "[Null]数据体"; if (instance is IJT808Description jT808Description) { //4.2.处理消息体 description = jT808Description.Description; } writer.WriteNull(description); //数据体属性对象 结束 writer.WriteEndObject(); } else { writer.WriteNull($"[Null]数据体"); } } try { // 5.读取校验码 reader.ReadByte(); writer.WriteNumber($"[{reader.RealCheckXorCode.ReadNumber()}]校验码", reader.RealCheckXorCode); // 6.读取终止位置 byte end = reader.ReadEnd(); writer.WriteNumber($"[{end.ReadNumber()}]结束", end); } catch (ArgumentOutOfRangeException ex) { writer.WriteString($"数据解析异常,无可用数据进行解析", ex.StackTrace); } catch (Exception ex) { writer.WriteString($"数据解析异常", ex.StackTrace); } finally { writer.WriteEndObject(); } }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <param name="writer"></param> /// <param name="config"></param> public void Analyze(ref JT808MessagePackReader reader, Utf8JsonWriter writer, IJT808Config config) { JT808_0x0107 jT808_0X0107 = new JT808_0x0107(); jT808_0X0107.TerminalType = reader.ReadUInt16(); writer.WriteNumber($"[{jT808_0X0107.TerminalType.ReadNumber()}]终端类型", jT808_0X0107.TerminalType); ReadOnlySpan <char> terminalTypeBits = string.Join("", Convert.ToString(jT808_0X0107.TerminalType, 2).PadLeft(16, '0').Reverse()).AsSpan(); writer.WriteStartObject("终端类型"); writer.WriteString("bit0", terminalTypeBits[0] == '0' ? "不适用客运车辆" : "适用客运车辆"); writer.WriteString("bit1", terminalTypeBits[1] == '0' ? "不适用危险品车辆" : "适用危险品车辆"); writer.WriteString("bit2", terminalTypeBits[2] == '0' ? "不适用普通货运车辆" : "适用普通货运车辆"); writer.WriteString("bit3", terminalTypeBits[3] == '0' ? "不适用出租车辆" : "适用出租车辆"); writer.WriteString("bit6", terminalTypeBits[6] == '0' ? "不支持硬盘录像" : "支持硬盘录像"); writer.WriteString("bit7", terminalTypeBits[7] == '0' ? "一体机" : "分体机"); if (reader.Version == JT808Version.JTT2019) { writer.WriteString("bit8", terminalTypeBits[8] == '0' ? "不适用挂车" : "适用挂车"); } writer.WriteEndObject(); if (reader.Version == JT808Version.JTT2019) { ReadOnlySpan <byte> makerIdSpan = reader.ReadVirtualArray(11); jT808_0X0107.MakerId = reader.ReadString(11); writer.WriteString($"[{makerIdSpan.ToArray().ToHexString()}]制造商ID", jT808_0X0107.MakerId); ReadOnlySpan <byte> terminalModelSpan = reader.ReadVirtualArray(30); jT808_0X0107.TerminalModel = reader.ReadString(30); writer.WriteString($"[{terminalModelSpan.ToArray().ToHexString()}]终端型号", jT808_0X0107.TerminalModel); ReadOnlySpan <byte> terminalIdSpan = reader.ReadVirtualArray(30); jT808_0X0107.TerminalId = reader.ReadString(30); writer.WriteString($"[{terminalIdSpan.ToArray().ToHexString()}]终端ID", jT808_0X0107.TerminalId); } else { ReadOnlySpan <byte> makerIdSpan = reader.ReadVirtualArray(5); jT808_0X0107.MakerId = reader.ReadString(5); writer.WriteString($"[{makerIdSpan.ToArray().ToHexString()}]制造商ID", jT808_0X0107.MakerId); ReadOnlySpan <byte> terminalModelSpan = reader.ReadVirtualArray(20); jT808_0X0107.TerminalModel = reader.ReadString(20); writer.WriteString($"[{terminalModelSpan.ToArray().ToHexString()}]终端型号", jT808_0X0107.TerminalModel); ReadOnlySpan <byte> terminalIdSpan = reader.ReadVirtualArray(7); jT808_0X0107.TerminalId = reader.ReadString(7); writer.WriteString($"[{terminalIdSpan.ToArray().ToHexString()}]终端ID", jT808_0X0107.TerminalId); } ReadOnlySpan <byte> iccidSpan = reader.ReadVirtualArray(10); jT808_0X0107.Terminal_SIM_ICCID = reader.ReadBCD(20, config.Trim); writer.WriteString($"[{iccidSpan.ToArray().ToHexString()}]终端SIM卡ICCID", jT808_0X0107.Terminal_SIM_ICCID); jT808_0X0107.Terminal_Hardware_Version_Length = reader.ReadByte(); writer.WriteNumber($"[{jT808_0X0107.Terminal_Hardware_Version_Length.ReadNumber()}]终端硬件版本号长度", jT808_0X0107.Terminal_Hardware_Version_Length); ReadOnlySpan <byte> hardwareVersionNumSpan = reader.ReadVirtualArray(jT808_0X0107.Terminal_Hardware_Version_Length); jT808_0X0107.Terminal_Hardware_Version_Num = reader.ReadString(jT808_0X0107.Terminal_Hardware_Version_Length); writer.WriteString($"[{hardwareVersionNumSpan.ToArray().ToHexString()}]终端硬件版本号", jT808_0X0107.Terminal_Hardware_Version_Num); jT808_0X0107.Terminal_Firmware_Version_Length = reader.ReadByte(); writer.WriteNumber($"[{jT808_0X0107.Terminal_Firmware_Version_Length.ReadNumber()}]终端硬件版本号长度", jT808_0X0107.Terminal_Firmware_Version_Length); ReadOnlySpan <byte> firmwareVersionNumSpan = reader.ReadVirtualArray(jT808_0X0107.Terminal_Firmware_Version_Length); jT808_0X0107.Terminal_Firmware_Version_Num = reader.ReadString(jT808_0X0107.Terminal_Firmware_Version_Length); writer.WriteString($"[{firmwareVersionNumSpan.ToArray().ToHexString()}]终端固件版本号", jT808_0X0107.Terminal_Firmware_Version_Num); jT808_0X0107.GNSSModule = reader.ReadByte(); ReadOnlySpan <char> gNSSModuleBits = string.Join("", Convert.ToString(jT808_0X0107.GNSSModule, 2).PadLeft(8, '0').Reverse()).AsSpan(); writer.WriteNumber($"[{jT808_0X0107.GNSSModule.ReadNumber()}]GNSS模块属性", jT808_0X0107.GNSSModule); writer.WriteStartObject("GNSS模块属性"); writer.WriteString("bit0", gNSSModuleBits[0] == '0' ? "不支持GPS定位" : "支持GPS定位"); writer.WriteString("bit1", gNSSModuleBits[1] == '0' ? "不支持北斗定位" : "支持北斗定位"); writer.WriteString("bit2", gNSSModuleBits[2] == '0' ? "不支持GLONASS定位" : "支持GLONASS定位"); writer.WriteString("bit3", gNSSModuleBits[3] == '0' ? "不支持Galileo定位" : "支持Galileo定位"); writer.WriteEndObject(); jT808_0X0107.CommunicationModule = reader.ReadByte(); ReadOnlySpan <char> communicationModuleBits = string.Join("", Convert.ToString(jT808_0X0107.CommunicationModule, 2).PadLeft(8, '0').Reverse()).AsSpan(); writer.WriteNumber($"[{jT808_0X0107.CommunicationModule.ReadNumber()}]通信模块属性", jT808_0X0107.CommunicationModule); writer.WriteStartObject("通信模块属性"); writer.WriteString("bit0", communicationModuleBits[0] == '0' ? "不支持GPRS通信" : "支持GPRS通信"); writer.WriteString("bit1", communicationModuleBits[1] == '0' ? "不支持CDMA通信" : "支持CDMA通信"); writer.WriteString("bit2", communicationModuleBits[2] == '0' ? "不支持TD-SCDMA通信" : "支持TD-SCDMA通信"); writer.WriteString("bit3", communicationModuleBits[3] == '0' ? "不支持WCDMA通信" : "支持WCDMA通信"); writer.WriteString("bit4", communicationModuleBits[4] == '0' ? "不支持CDMA2000通信" : "支持CDMA2000通信"); writer.WriteString("bit5", communicationModuleBits[5] == '0' ? "不支持TD-LTE通信" : "支持TD-LTE通信"); writer.WriteString("bit6", "保留"); writer.WriteString("bit7", communicationModuleBits[7] == '0' ? "不支持其他通信方式" : "不支持其他通信方式"); writer.WriteEndObject(); }
public JT808Package Deserialize(ref JT808MessagePackReader reader, IJT808Config config) { // 1. 验证校验和 if (!config.SkipCRCCode) { if (!reader.CheckXorCodeVali) { throw new JT808Exception(JT808ErrorCode.CheckCodeNotEqual, $"{reader.RealCheckXorCode}!={reader.CalculateCheckXorCode}"); } } JT808Package jT808Package = new JT808Package(); // ---------------开始解包-------------- // 2.读取起始位置 jT808Package.Begin = reader.ReadStart(); // 3.读取头部信息 jT808Package.Header = new JT808Header(); // 3.1.读取消息Id jT808Package.Header.MsgId = reader.ReadUInt16(); // 3.2.读取消息体属性 jT808Package.Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty(); ushort messageBodyPropertyValue = reader.ReadUInt16(); // 3.2.1.解包消息体属性 jT808Package.Header.MessageBodyProperty.Unwrap(messageBodyPropertyValue, config); // 3.3.读取终端手机号 jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength); // 3.4.读取消息流水号 jT808Package.Header.MsgNum = reader.ReadUInt16(); // 3.5.判断有无分包 if (jT808Package.Header.MessageBodyProperty.IsPackge) { //3.5.1.读取消息包总数 jT808Package.Header.MessageBodyProperty.PackgeCount = reader.ReadUInt16(); //3.5.2.读取消息包序号 jT808Package.Header.MessageBodyProperty.PackageIndex = reader.ReadUInt16(); } // 4.处理数据体 // 4.1.判断有无数据体 if (jT808Package.Header.MessageBodyProperty.DataLength > 0) { Type jT808BodiesImplType = config.MsgIdFactory.GetBodiesImplTypeByMsgId(jT808Package.Header.MsgId, jT808Package.Header.TerminalPhoneNo); if (jT808BodiesImplType != null) { if (jT808Package.Header.MessageBodyProperty.IsPackge) { if (jT808Package.Header.MessageBodyProperty.PackageIndex > 1) { try { //4.2处理第二包之后的分包数据消息体 jT808Package.Bodies = JT808SplitPackageBodiesFormatter.Instance.Deserialize(ref reader, config); } catch (Exception ex) { throw new JT808Exception(JT808ErrorCode.BodiesParseError, ex); } } else { try { //4.2.处理消息体 jT808Package.Bodies = JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize( config.GetMessagePackFormatterByType(jT808BodiesImplType), ref reader, config); } catch (Exception ex) { throw new JT808Exception(JT808ErrorCode.BodiesParseError, ex); } } } else { try { //4.2.处理消息体 jT808Package.Bodies = JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize( config.GetMessagePackFormatterByType(jT808BodiesImplType), ref reader, config); } catch (Exception ex) { throw new JT808Exception(JT808ErrorCode.BodiesParseError, ex); } } } } // 5.读取校验码 jT808Package.CheckCode = reader.ReadByte(); // 6.读取终止位置 jT808Package.End = reader.ReadEnd(); // ---------------解包完成-------------- return(jT808Package); }
public JT808HeaderPackage(ref JT808MessagePackReader reader, IJT808Config config) { // 1. 验证校验和 if (!config.SkipCRCCode) { if (!reader.CheckXorCodeVali) { throw new JT808Exception(JT808ErrorCode.CheckCodeNotEqual, $"{reader.RealCheckXorCode}!={reader.CalculateCheckXorCode}"); } } // ---------------开始解包-------------- // 2.读取起始位置 this.Begin = reader.ReadStart(); // 3.读取头部信息 this.Header = new JT808Header(); // 3.1.读取消息Id this.Header.MsgId = reader.ReadUInt16(); // 3.2.读取消息体属性 ushort messageBodyPropertyValue = reader.ReadUInt16(); // 3.2.1.解包消息体属性 this.Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty(messageBodyPropertyValue); if (this.Header.MessageBodyProperty.VersionFlag) { //2019版本 // 3.3.读取协议版本号 this.Header.ProtocolVersion = reader.ReadByte(); // 3.4.读取终端手机号 this.Header.TerminalPhoneNo = reader.ReadBCD(20, config.Trim); reader.Version = JT808Version.JTT2019; } else { //2013版本 // 3.3.读取终端手机号 this.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim); } // 3.4.读取消息流水号 this.Header.MsgNum = reader.ReadUInt16(); // 3.5.判断有无分包 if (this.Header.MessageBodyProperty.IsPackage) { //3.5.1.读取消息包总数 this.Header.PackgeCount = reader.ReadUInt16(); //3.5.2.读取消息包序号 this.Header.PackageIndex = reader.ReadUInt16(); } // 4.处理数据体 // 4.1.判断有无数据体 if (this.Header.MessageBodyProperty.DataLength > 0) { this.Bodies = reader.ReadContent(); } else { this.Bodies = ReadOnlySpan <byte> .Empty; } // 5.读取校验码 this.CheckCode = reader.ReadByte(); // 6.读取终止位置 this.End = reader.ReadEnd(); // ---------------解包完成-------------- this.OriginalData = reader.SrcBuffer; }