|
| 1 | +importScripts("/node_modules/lodash/lodash.min.js"); |
| 2 | + |
| 3 | +onmessage = function (event) { |
| 4 | + const header = `<?xml version="1.0" encoding="UTF-8"?> |
| 5 | + <gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.topografix.com/GPX/gpx_style/0/2 http://www.topografix.com/GPX/gpx_style/0/2/gpx_style.xsd" xmlns:gpx_style="http://www.topografix.com/GPX/gpx_style/0/2" |
| 6 | + version="1.1" |
| 7 | + creator="https://github.com/betaflight/blackbox-log-viewer"> |
| 8 | + <metadata> |
| 9 | + <author> |
| 10 | + <name>Betaflight Blackbox Explorer</name> |
| 11 | + <link href="https://github.com/betaflight/blackbox-log-viewer"></link> |
| 12 | + </author> |
| 13 | + </metadata>`; |
| 14 | + |
| 15 | + const footer = "</gpx>"; |
| 16 | + |
| 17 | + const timeIndex = event.data.fieldNames.indexOf("time"); |
| 18 | + const latIndex = event.data.fieldNames.indexOf("GPS_coord[0]"); |
| 19 | + const lngIndex = event.data.fieldNames.indexOf("GPS_coord[1]"); |
| 20 | + const altitudeIndex = event.data.fieldNames.indexOf("GPS_altitude"); |
| 21 | + const speedIndex = event.data.fieldNames.indexOf("GPS_speed"); |
| 22 | + const groundCourseIndex = event.data.fieldNames.indexOf("GPS_ground_course"); |
| 23 | + const numSatIndex = event.data.fieldNames.indexOf("GPS_numSat"); |
| 24 | + |
| 25 | + let trkpts = ""; |
| 26 | + for (var chunk of event.data.frames) { |
| 27 | + for (var frame of chunk) { |
| 28 | + if (!frame[latIndex] || !frame[lngIndex]) continue; |
| 29 | + const timeMillis = Math.floor(frame[timeIndex] / 1000); |
| 30 | + const lat = frame[latIndex] / 10000000; |
| 31 | + const lng = frame[lngIndex] / 10000000; |
| 32 | + const altitude = frame[altitudeIndex] / 10; |
| 33 | + const speed = frame[speedIndex] / 100; |
| 34 | + const groundCourse = frame[groundCourseIndex] / 10; |
| 35 | + const numSat = frame[numSatIndex]; |
| 36 | + |
| 37 | + let date = new Date(event.data.sysConfig["Log start datetime"]); |
| 38 | + date.setTime(date.getTime() + timeMillis); |
| 39 | + |
| 40 | + let trkpt = `<trkpt lat="${lat}" lon="${lng}">`; |
| 41 | + trkpt += `<ele>${altitude}</ele>`; |
| 42 | + trkpt += `<time>${date.toISOString()}</time>`; |
| 43 | + trkpt += `<speed>${speed}</speed>`; |
| 44 | + trkpt += `<degreesType>${groundCourse}</degreesType>`; |
| 45 | + trkpt += `<sat>${numSat}</sat>`; |
| 46 | + trkpt += `</trkpt>\n`; |
| 47 | + |
| 48 | + trkpts += trkpt; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + let trk = |
| 53 | + `<trk> |
| 54 | + <name>new</name> |
| 55 | + <type>Flight</type> |
| 56 | + <trkseg>${trkpts}</trkseg> |
| 57 | + </trk>`; |
| 58 | + |
| 59 | + postMessage(header + "\n" + trk + "\n" + footer); |
| 60 | +}; |
0 commit comments