public bool Parse(byte[] data, string alamdate, string ACID) { bool flag = true; try { byte[] deviceType = new byte[2]; Array.ConstrainedCopy(data, 4, deviceType, 0, 2); DeviceType = Utility.byte2DeviceType(deviceType);//设备类型 byte[] imei = new byte[6]; Array.ConstrainedCopy(data, 6, imei, 0, 6); IMEI = Utility.byte2IMEI(imei);//设备编码 byte[] dataBlockCount = new byte[1]; Array.ConstrainedCopy(data, 12, dataBlockCount, 0, 1); DataBlockCount = byte2WirelessValue(dataBlockCount); //主程序版本、硬件版本、批量数据传送共3个数据块 byte[] mainProgramVersionIdentity = new byte[2]; Array.ConstrainedCopy(data, 13, mainProgramVersionIdentity, 0, 2); Array.Reverse(mainProgramVersionIdentity); MainProgramVersionIdentity = Utility.ToHexString(mainProgramVersionIdentity); if (MainProgramVersionIdentity == "0005") { byte[] mainProgramVersion = new byte[4]; Array.ConstrainedCopy(data, 15, mainProgramVersion, 0, 4); MainProgramVersion = Utility.byte2Version(mainProgramVersion); } byte[] hardwareVersionIdentity = new byte[2]; Array.ConstrainedCopy(data, 19, hardwareVersionIdentity, 0, 2); Array.Reverse(hardwareVersionIdentity); HardwareVersionIdentity = Utility.ToHexString(hardwareVersionIdentity); if (HardwareVersionIdentity == "0006") { byte[] hardwareVersion = new byte[4]; Array.ConstrainedCopy(data, 21, hardwareVersion, 0, 4); HardwareVersion = Utility.byte2Version(hardwareVersion); } byte[] batchDataIdentity = new byte[2]; Array.ConstrainedCopy(data, 25, batchDataIdentity, 0, 2); Array.Reverse(batchDataIdentity); BatchDataIdentity = Utility.ToHexString(batchDataIdentity); if (BatchDataIdentity == "7F00") { byte[] bytedatastatistics = new byte[13]; Array.ConstrainedCopy(data, 27, bytedatastatistics, 0, 13); Bodydata = new BodyData(); Bodydata = byte2DataStatistics(bytedatastatistics, DeviceType); Bodydata.CollectTime = DateTime.Parse(alamdate); } ReceiveData my = new ReceiveData(); my.IMEI = IMEI; my.DeviceType = DeviceType; my.DataBlockCount = DataBlockCount; my.MainProgramVersionIdentity = MainProgramVersionIdentity; my.MainProgramVersion = MainProgramVersion; my.HardwareVersionIdentity = HardwareVersionIdentity; my.HardwareVersion = HardwareVersion; my.BatchDataIdentity = BatchDataIdentity; my.Bodydata = new BodyData(); my.Bodydata = Bodydata; InsertData.Insert(my, ACID); } catch (Exception ex) { flag = false; } return(flag); }
/// <summary> /// 解析数据 /// </summary> /// <param name="inbyte"></param> /// <param name="deviceType">设备类型</param> /// <returns></returns> private static BodyData byte2DataStatistics(byte[] inbyte, string deviceType) { if (inbyte.Length < 13) { return(null); } BodyData data = new BodyData(); byte[] dataSegmentLength = new byte[2]; Array.ConstrainedCopy(inbyte, 0, dataSegmentLength, 0, 2); data.DataSegmentLength = ConvertDataLength(dataSegmentLength); if (data.DataSegmentLength == "11") { byte[] productLine = new byte[1]; Array.ConstrainedCopy(inbyte, 2, productLine, 0, 1); data.ProductLine = Utility.ToHexString(productLine); byte[] smokeAlarmStatus = new byte[1]; Array.ConstrainedCopy(inbyte, 3, smokeAlarmStatus, 0, 1); data.AlarmStatus = Utility.ToHexString(smokeAlarmStatus); data.AlarmStatusText = ConvertAlarmStatusText(data.AlarmStatus, deviceType); byte[] reserve1 = new byte[1]; Array.ConstrainedCopy(inbyte, 4, reserve1, 0, 1); data.Reserve1 = Utility.ConvertString(reserve1[0], 10); byte[] reserve2 = new byte[1]; Array.ConstrainedCopy(inbyte, 5, reserve2, 0, 1); data.Reserve2 = Utility.ConvertString(reserve2[0], 10); byte[] reserve3 = new byte[1]; Array.ConstrainedCopy(inbyte, 6, reserve3, 0, 1); data.Reserve3 = Utility.ConvertString(reserve3[0], 10); byte[] wirelessType = new byte[1]; Array.ConstrainedCopy(inbyte, 7, wirelessType, 0, 1); data.WirelessType = Utility.ToHexString(wirelessType); data.WirelessTypeText = ConvertWirelessTypeText(data.WirelessType); byte[] wirelessValue = new byte[1]; Array.ConstrainedCopy(inbyte, 8, wirelessValue, 0, 1); data.WirelessValue = byte2WirelessValue(wirelessValue); byte[] collectorBatteryStatus = new byte[1]; Array.ConstrainedCopy(inbyte, 9, collectorBatteryStatus, 0, 1); data.CollectorBatteryStatus = Convert.ToInt32(collectorBatteryStatus[0].ToString()); byte[] collectorBatteryValue = new byte[1]; Array.ConstrainedCopy(inbyte, 10, collectorBatteryValue, 0, 1); data.CollectorBatteryValue = byte2WirelessValue(collectorBatteryValue); byte[] communicationBatteryStatus = new byte[1]; Array.ConstrainedCopy(inbyte, 11, communicationBatteryStatus, 0, 1); data.CommunicationBatteryStatus = Convert.ToInt32(communicationBatteryStatus[0].ToString()); byte[] communicationBatteryValue = new byte[1]; Array.ConstrainedCopy(inbyte, 12, communicationBatteryValue, 0, 1); data.CommunicationBatteryValue = Convert.ToInt32(communicationBatteryValue[0].ToString()); } return(data); }