public void AddReport(DateTime date, byte[] recordBuf) { SPEED_RECORD record; MySpeedRecord sRecord; record = (SPEED_RECORD)Lib.ByteArrayToObject(recordBuf, typeof(SPEED_RECORD)); if (record.crc == Lib.CalcCrc(recordBuf, 0, recordBuf.Length - 1)) { if ((record.currentTime.hour < 24) && (record.currentTime.min < 60) && (record.currentTime.sec < 60)) { if ((record.lat >= -90) && (record.lat <= 90) && (record.lng >= -180) && (record.lng <= 180)) { if (record.speed < 300) { sRecord = new MySpeedRecord(date, record); speedRecords.Add(sRecord); } } } } }
private void ProcessReceivedHIDData() { byte crc; int length; byte opcode; UInt32 packetNo; byte[] bTemp; // should have something in the buffer length = BitConverter.ToInt16(hidInBuffer, LENGTH_OFFSET); if (length <= 0) { return; } // validate the CRC crc = hidInBuffer[HEADER_SIZE + length]; if (crc != Lib.CalcCrc(hidInBuffer, DATA_OFFSET, length)) { return; } opcode = hidInBuffer[OPCODE_OFFSET]; switch (opcode) { case BOOT_MODE_OPCODE: bootMode = true; this.Invoke((MethodInvoker) delegate { LoadBootModeLayout(); }); break; case READ_CONFIG_OPCODE: // config data packetNo = BitConverter.ToUInt32(hidInBuffer, PACKET_NO_OFFSET); if (packetNo == FINISH_PACKET_NO) { cfg = (TRACKER_CONFIG_DATA)Lib.ByteArrayToObject(cfgData, typeof(TRACKER_CONFIG_DATA)); } if (packetNo == ERROR_PACKET_NO) { return; } if (packetNo == DEFAULT_PACKET_NO) { cfgDataSize = BitConverter.ToUInt32(hidInBuffer, DATA_WITH_PACKET_NO_OFFSET); cfgDeviceBuffSize = BitConverter.ToUInt32(hidInBuffer, DATA_WITH_PACKET_NO_OFFSET + 4); cfgDataOffset = 0; length = (int)cfgDeviceBuffSize; } else { Array.Copy(hidInBuffer, DATA_WITH_PACKET_NO_OFFSET, cfgData, packetNo, length - PACKET_NO_SIZE); cfgDataOffset = (int)packetNo + length - PACKET_NO_SIZE; length = (int)cfgDeviceBuffSize; if (cfgDataOffset > cfgDataSize - cfgDeviceBuffSize) { length = (int)cfgDataSize - cfgDataOffset; } } if (cfgDataOffset >= cfgDataSize) // last packet { cfg = (TRACKER_CONFIG_DATA)Lib.ByteArrayToObject(cfgData, typeof(TRACKER_CONFIG_DATA)); // read config successfully if (bootMode) { bootMode = false; this.Invoke((MethodInvoker) delegate { LoadAppModeLayout(); }); } return; } CreateHIDBuffer(PACKET_NO_OFFSET + 2, READ_CONFIG_OPCODE, (UInt32)cfgDataOffset); Array.Copy(BitConverter.GetBytes(length), 0, hidOutBuffer, DATA_WITH_PACKET_NO_OFFSET, 2); hidOutBuffer[SIMPLE_CRC_WITH_PACKET_NO_OFFSET + 2] = Lib.CalcCrc(hidOutBuffer, PACKET_NO_OFFSET, PACKET_NO_SIZE + 2); SendHIDData(hidOutBuffer, 0, SIMPLE_PACKET_SIZE + 2); break; case UPLOAD_LOG_OPCODE: // file data packetNo = BitConverter.ToUInt32(hidInBuffer, PACKET_NO_OFFSET); if (packetNo == ERROR_PACKET_NO) { // file is not exist readingReportFile = false; return; } else if (packetNo == DEFAULT_PACKET_NO) { cfgDataSize = BitConverter.ToUInt32(hidInBuffer, DATA_WITH_PACKET_NO_OFFSET); cfgDeviceBuffSize = BitConverter.ToUInt32(hidInBuffer, DATA_WITH_PACKET_NO_OFFSET + 4); cfgDataOffset = 0; length = (int)cfgDeviceBuffSize; offsetData = 0; lastLogOffset = (int)(cfgDataSize / SPEED_RECORD_SIZE) * SPEED_RECORD_SIZE; if (lastLogOffset == cfgDataSize) { lastLogOffset = (int)cfgDataSize - SPEED_RECORD_SIZE; } } else { length = BitConverter.ToUInt16(hidInBuffer, LENGTH_OFFSET) - PACKET_NO_SIZE; Array.Copy(hidInBuffer, DATA_WITH_PACKET_NO_OFFSET, myBuff, offsetData, length); offsetData += length; if (offsetData >= SPEED_RECORD_SIZE) { offsetData = 0; bTemp = new byte[SPEED_RECORD_SIZE]; Array.Copy(myBuff, 0, bTemp, 0, bTemp.Length); try { currentReport[currentDriver].AddReport(currentReportDate, bTemp); } catch (Exception) { } cfgDataOffset += (int)(logInterval * SPEED_RECORD_SIZE); } if ((cfgDataOffset + offsetData) == lastLogOffset) { lastLogRead = true; } if ((cfgDataOffset + offsetData) >= cfgDataSize) { if (!lastLogRead) { // read the last log in file lastLogRead = true; cfgDataOffset = lastLogOffset; offsetData = 0; } else { // end of file readingReportFile = false; return; } } } CreateHIDBuffer(PACKET_NO_OFFSET, UPLOAD_LOG_OPCODE, (UInt32)(cfgDataOffset + offsetData)); hidOutBuffer[SIMPLE_CRC_WITH_PACKET_NO_OFFSET] = Lib.CalcCrc(hidOutBuffer, PACKET_NO_OFFSET, PACKET_NO_SIZE); SendHIDData(hidOutBuffer, 0, SIMPLE_PACKET_SIZE, 1000); break; case 0x5A: // ACK break; case 0xA5: // NACK break; default: break; } }
public MySpeedRecord(DateTime date, byte[] recordBuf) { this.date = date; this.record = (SPEED_RECORD)Lib.ByteArrayToObject(recordBuf, typeof(SPEED_RECORD)); }