private void MainForm_Load(object sender, EventArgs e) { registerToolStripMenuItem.Visible = isInTrialPeriod; cfg = new TRACKER_CONFIG_DATA(); cfg.driverList = new DRIVER_INFO[10]; for (int i = 0; i < 10; i++) { cfg.driverList[i] = new DRIVER_INFO(); } logIntervalCmbBox.SelectedIndex = 2; reports = new List <IndividualReport[]>(); reportDriverNameAdded = new bool[10]; prevLogReceivedTime = DateTime.Now; bootMode = false; // initialize localized strings InitLocalizedStrings(); OpenHIDDevice(); UpdateUI(); }
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 static void GenerateKML(List<IndividualReport[]> reports, string filePath) //{ // StreamWriter sw = new StreamWriter(filePath); // sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); // sw.WriteLine("<kml xmlns=\"http://earth.google.com/kml/2.1\">"); // sw.WriteLine("<Document>"); // sw.WriteLine("<Placemark>"); // sw.WriteLine("<color>ffffff00</color>"); // sw.WriteLine("<width>6</width>"); // sw.WriteLine("<MultiGeometry>"); // sw.WriteLine("<LineString>"); // sw.WriteLine("<coordinates>"); // foreach (IndividualReport[] reportArr in reports) // { // foreach (IndividualReport report in reportArr) // { // report.speedRecords.ForEach(spr => // { // if (spr.record.lat != 0 || spr.record.lng != 0) // { // sw.Write(Lib.latlngToString(spr.record.lng) + "," + Lib.latlngToString(spr.record.lat) + " "); // } // }); // } // } // sw.WriteLine("</coordinates>"); // sw.WriteLine("</LineString>"); // sw.WriteLine("</MultiGeometry>"); // sw.WriteLine("</Placemark>"); // sw.WriteLine("<Folder>"); // foreach (IndividualReport[] reportArr in reports) // { // foreach (IndividualReport report in reportArr) // { // report.speedRecords.ForEach(spr => // { // if (spr.record.lat != 0 || spr.record.lng != 0) // { // sw.WriteLine("<Placemark>"); // sw.WriteLine("<name>" + spr.record.currentTime.hour + ":" + spr.record.currentTime.min + ":" + spr.record.currentTime.sec + " " + spr.date.Day + "/" + spr.date.Month + "/" + spr.date.Year + " " + spr.record.speed.ToString(".#") + " km/h</name>"); // sw.WriteLine("<Point>"); // sw.WriteLine("<coordinates>" + Lib.latlngToString(spr.record.lng) + ", " + Lib.latlngToString(spr.record.lat) + ",0.0</coordinates>"); // sw.WriteLine("</Point>"); // sw.WriteLine("</Placemark>"); // } // }); // } // } // sw.WriteLine("</Folder>"); // sw.WriteLine("</Document>"); // sw.WriteLine("</kml>"); // sw.Close(); //} public static void GenerateKML(TRACKER_CONFIG_DATA cfg, List <IndividualReport[]> reports, string filePath) { StreamWriter sw; DateTime beginTime, endTime; MySpeedRecord beginReport, endReport; int beginDriver, endDriver; if ((reports == null) && (reports.Count > 0)) { return; } sw = new StreamWriter(filePath); sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>"); sw.WriteLine("<kml>"); sw.WriteLine("<Document>"); sw.WriteLine("<Style id=\"s_ylw-pushpin\"><IconStyle><scale>1.1</scale><Icon><href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href></Icon><hotSpot x=\"20\" y=\"2\" xunits=\"pixels\" yunits=\"pixels\"/></IconStyle></Style>"); sw.WriteLine("<Style id=\"s_blue-pushpin\"><IconStyle><scale>1.1</scale><Icon><href>https://maps.google.com/mapfiles/kml/pushpin/blue-pushpin.png</href></Icon><hotSpot x=\"20\" y=\"2\" xunits=\"pixels\" yunits=\"pixels\"/></IconStyle></Style>"); sw.WriteLine("<Style id=\"s_ylw-pushpin_hl\"><IconStyle><scale>1.3</scale><Icon><href>http://maps.google.com/mapfiles/kml/pushpin/red-pushpin.png</href></Icon><hotSpot x=\"20\" y=\"2\" xunits=\"pixels\" yunits=\"pixels\"/></IconStyle></Style>"); sw.WriteLine("<StyleMap id=\"m_ylw-pushpin\"><Pair><key>normal</key><styleUrl>#s_ylw-pushpin</styleUrl></Pair><Pair><key>highlight</key><styleUrl>#s_ylw-pushpin_hl</styleUrl></Pair><Pair><key>highlight1</key><styleUrl>#s_blue-pushpin</styleUrl></Pair></StyleMap>"); beginTime = DateTime.MaxValue; beginReport = null; beginDriver = 0; for (int i = 0; i < 10; i++) { IndividualReport report = reports[0][i]; if ((report.speedRecords != null) && (report.speedRecords.Count > 0)) { if (report.speedRecords[0].GetCurrentTime() < beginTime) { beginTime = report.speedRecords[0].GetCurrentTime(); beginReport = report.speedRecords[0]; beginDriver = i; } } } endTime = DateTime.MinValue; endReport = null; endDriver = 0; for (int i = 0; i < 10; i++) { IndividualReport report = reports[reports.Count - 1][i]; if ((report.speedRecords != null) && (report.speedRecords.Count > 0)) { if (report.speedRecords[report.speedRecords.Count - 1].GetCurrentTime() > endTime) { endTime = report.speedRecords[report.speedRecords.Count - 1].GetCurrentTime(); endReport = report.speedRecords[report.speedRecords.Count - 1]; endDriver = i; } } } sw.WriteLine("<Placemark>"); sw.WriteLine("<name>Điểm xuất phát : " + cfg.plateNo + "</name>"); sw.WriteLine("<description>"); sw.WriteLine("Thời gian : " + beginReport.GetCurrentTime().ToString("HH:mm:ss dd/MM/yyyy").Replace('-', '/') + "<br/>"); sw.WriteLine("Lái xe : " + cfg.driverList[beginDriver].driverName + "<br/>"); sw.WriteLine("Biển số : " + cfg.plateNo + "<br/>"); sw.WriteLine("Vận tốc : " + beginReport.record.speed + "<br/>"); sw.WriteLine("No.Đóng mở : " + beginReport.record.doorOpenCount + "<br/>"); sw.WriteLine("No.Quá tốc : " + beginReport.record.overSpeedCount + "<br/>"); sw.WriteLine("No.Dừng đỗ : " + beginReport.record.parkingCount + "<br/>"); sw.WriteLine("</description>"); sw.WriteLine("<styleUrl>#s_blue-pushpin</styleUrl>"); sw.WriteLine("<Point>"); sw.WriteLine("<coordinates>" + beginReport.record.lng + "," + beginReport.record.lat + "</coordinates>"); sw.WriteLine("</Point>"); sw.WriteLine("</Placemark>"); foreach (IndividualReport[] reportArr in reports) { for (int i = 0; i < 10; i++) { IndividualReport report = reportArr[i]; report.speedRecords.ForEach(spr => { if (spr.record.lat != 0 || spr.record.lng != 0) { sw.WriteLine("<Placemark>"); sw.WriteLine("<name>Biển số xe : " + cfg.plateNo + "</name>"); sw.WriteLine("<description>"); sw.WriteLine("Thời gian : " + spr.GetCurrentTime().ToString("HH:mm:ss dd/MM/yyyy").Replace('-', '/') + "<br/>"); sw.WriteLine("Lái xe : " + cfg.driverList[i].driverName + "<br/>"); sw.WriteLine("Biển số : " + cfg.plateNo + "<br/>"); sw.WriteLine("Vận tốc : " + spr.record.speed + "<br/>"); sw.WriteLine("No.Đóng mở : " + spr.record.doorOpenCount + "<br/>"); sw.WriteLine("No.Quá tốc : " + spr.record.overSpeedCount + "<br/>"); sw.WriteLine("No.Dừng đỗ : " + spr.record.parkingCount + "<br/>"); sw.WriteLine("</description>"); sw.WriteLine("<styleUrl>#s_ylw-pushpin</styleUrl>"); sw.WriteLine("<Point>"); sw.WriteLine("<coordinates>" + spr.record.lng + "," + spr.record.lat + "</coordinates>"); sw.WriteLine("</Point>"); sw.WriteLine("</Placemark>"); } }); } } sw.WriteLine("<Placemark>"); sw.WriteLine("<name>Điểm kết thúc : " + cfg.plateNo + "</name>"); sw.WriteLine("<description>"); sw.WriteLine("Thời gian : " + endReport.GetCurrentTime().ToString("HH:mm:ss dd/MM/yyyy").Replace('-', '/') + "<br/>"); sw.WriteLine("Lái xe : " + cfg.driverList[endDriver].driverName + "<br/>"); sw.WriteLine("Biển số : " + cfg.plateNo + "<br/>"); sw.WriteLine("Vận tốc : " + endReport.record.speed + "<br/>"); sw.WriteLine("No.Đóng mở : " + endReport.record.doorOpenCount + "<br/>"); sw.WriteLine("No.Quá tốc : " + endReport.record.overSpeedCount + "<br/>"); sw.WriteLine("No.Dừng đỗ : " + endReport.record.parkingCount + "<br/>"); sw.WriteLine("</description>"); sw.WriteLine("<styleUrl>#s_blue-pushpin</styleUrl>"); sw.WriteLine("<Point>"); sw.WriteLine("<coordinates>" + endReport.record.lng + "," + endReport.record.lat + "</coordinates>"); sw.WriteLine("</Point>"); sw.WriteLine("</Placemark>"); sw.WriteLine("<Placemark>"); sw.WriteLine("<name>Route</name>"); sw.WriteLine("<color>ff00ffff</color>"); sw.WriteLine("<width>6</width>"); sw.WriteLine("<MultiGeometry>"); sw.WriteLine("<LineString>"); sw.WriteLine("<coordinates>"); foreach (IndividualReport[] reportArr in reports) { foreach (IndividualReport report in reportArr) { report.speedRecords.ForEach(spr => { if (spr.record.lat != 0 || spr.record.lng != 0) { sw.Write(spr.record.lng + "," + spr.record.lat + ",0.000000" + Environment.NewLine); } }); } } sw.WriteLine("</coordinates>"); sw.WriteLine("</LineString>"); sw.WriteLine("</MultiGeometry>"); sw.WriteLine("</Placemark>"); sw.WriteLine("</Document>"); sw.WriteLine("</kml>"); sw.Close(); }