|
| 1 | +import { Adb, AdbDaemonTransport, AdbShellProtocolProcess, encodeUtf8 } from "@yume-chan/adb"; |
| 2 | +import AdbWebCredentialStore from "@yume-chan/adb-credential-web"; |
| 3 | +import { AdbDaemonWebUsbDevice, AdbDaemonWebUsbDeviceManager } from "@yume-chan/adb-daemon-webusb"; |
| 4 | + |
| 5 | + |
| 6 | +async function sendDjiGoFileViaAdb(data: Blob) { |
| 7 | + const adb = await _getAdbConnection(); |
| 8 | + if (!adb) return; |
| 9 | + |
| 10 | + const base64String = await _encodeDataAsBase64String(data); |
| 11 | + |
| 12 | + // Need to find first existing waypoint directory available |
| 13 | + const waypointDir = `/sdcard/Android/data`; |
| 14 | + const listDirs = await adb.subprocess.shellProtocol!.spawn( |
| 15 | + `ls -1 ${waypointDir}` |
| 16 | + ); |
| 17 | + const dirOutput = await _readShellOutput(listDirs); |
| 18 | + const dirs = dirOutput.split('\n').filter(Boolean); |
| 19 | + if (dirs.length === 0) { |
| 20 | + throw new Error(`A waypoint flight must flown first`); |
| 21 | + } |
| 22 | + const firstDir = dirs[0]; |
| 23 | + // Then find the .kmz file within that dir |
| 24 | + const targetPath = `${waypointDir}/${firstDir}`; |
| 25 | + const listFiles = await adb.subprocess.shellProtocol!.spawn( |
| 26 | + `ls -1 ${targetPath}/*.kmz` |
| 27 | + ); |
| 28 | + const filesOutput = await _readShellOutput(listFiles); |
| 29 | + const kmzFiles = filesOutput.split('\n').filter(Boolean); |
| 30 | + if (kmzFiles.length === 0) { |
| 31 | + throw new Error(`No .kmz files found in ${targetPath}`); |
| 32 | + } |
| 33 | + const targetFile = kmzFiles[0]; |
| 34 | + console.log(`Replacing: ${targetFile}`); |
| 35 | + |
| 36 | + // // Send file to phone via ADB STDIN |
| 37 | + const process = await adb.subprocess.shellProtocol!.spawn( |
| 38 | + `sh -c "base64 -d > '${targetFile}'"` |
| 39 | + ); |
| 40 | + const writer = process.stdin.getWriter(); |
| 41 | + await writer.write(encodeUtf8(base64String)); |
| 42 | + console.log(`Successfully replaced: ${targetFile}`); |
| 43 | + console.log(`Copied flightplan to ${targetFile}`) |
| 44 | +} |
| 45 | + |
| 46 | +async function sendPotensicProFileViaAdb(data: Blob) { |
| 47 | + const adb = await _getAdbConnection(); |
| 48 | + if (!adb) return; |
| 49 | + |
| 50 | + const base64String = await _encodeDataAsBase64String(data); |
| 51 | + |
| 52 | + // Cleanup old journal files |
| 53 | + await adb.subprocess.shellProtocol!.spawn("run-as com.ipotensic.potensicpro rm -f databases/map.db-journal"); |
| 54 | + console.log('Deleted db journal') |
| 55 | + |
| 56 | + // Send file to phone via ADB STDIN |
| 57 | + const process = await adb.subprocess.shellProtocol!.spawn( |
| 58 | + `run-as com.ipotensic.potensicpro sh -c "base64 -d > databases/test.db"` |
| 59 | + ); |
| 60 | + const writer = process.stdin.getWriter(); |
| 61 | + // Send as UTF-8 encoded data |
| 62 | + await writer.write(encodeUtf8(base64String)); |
| 63 | + console.log('Copied flightplan to databases/map.db') |
| 64 | +} |
| 65 | + |
| 66 | +async function _readShellOutput(process: AdbShellProtocolProcess): Promise<string> { |
| 67 | + const decoder = new TextDecoder(); |
| 68 | + const reader = process.stdout.getReader(); |
| 69 | + let output = ""; |
| 70 | + while (true) { |
| 71 | + const { value, done } = await reader.read(); |
| 72 | + if (done) break; |
| 73 | + output += decoder.decode(value); |
| 74 | + } |
| 75 | + return output; |
| 76 | +} |
| 77 | + |
| 78 | +async function _encodeDataAsBase64String(data: Blob): Promise<string> { |
| 79 | + return await new Promise<string>((resolve) => { |
| 80 | + const reader = new FileReader(); |
| 81 | + reader.onload = () => { |
| 82 | + const arrayBuffer = reader.result as ArrayBuffer; |
| 83 | + const bytes = new Uint8Array(arrayBuffer); |
| 84 | + let binary = ""; |
| 85 | + for (let i = 0; i < bytes.byteLength; i++) { |
| 86 | + binary += String.fromCharCode(bytes[i]); |
| 87 | + } |
| 88 | + resolve(btoa(binary)); |
| 89 | + }; |
| 90 | + reader.readAsArrayBuffer(data); |
| 91 | + }); |
| 92 | +} |
| 93 | + |
| 94 | +async function _getAdbConnection(): Promise<Adb | undefined> { |
| 95 | + const Manager: AdbDaemonWebUsbDeviceManager | undefined = AdbDaemonWebUsbDeviceManager.BROWSER; |
| 96 | + |
| 97 | + if (!Manager) { |
| 98 | + alert("WebUSB is not supported in this browser"); |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + const CredentialStore = new AdbWebCredentialStore(); |
| 103 | + |
| 104 | + const device: AdbDaemonWebUsbDevice | undefined = await Manager.requestDevice(); |
| 105 | + if (!device) { |
| 106 | + alert("No device selected"); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + const connection = await device.connect(); |
| 111 | + const adb = new Adb( |
| 112 | + await AdbDaemonTransport.authenticate({ |
| 113 | + serial: device.serial, |
| 114 | + connection, |
| 115 | + credentialStore: CredentialStore, |
| 116 | + }) |
| 117 | + ); |
| 118 | + |
| 119 | + return adb; |
| 120 | +} |
| 121 | + |
| 122 | +export { |
| 123 | + sendDjiGoFileViaAdb, |
| 124 | + sendPotensicProFileViaAdb, |
| 125 | +} |
0 commit comments