|
| 1 | +import {FileOps} from '@adafruit/circuitpython-repl-js'; |
| 2 | + |
| 3 | +class FileTransferClient { |
| 4 | + constructor(connectionStatusCB, repl) { |
| 5 | + this.connectionStatus = connectionStatusCB; |
| 6 | + this._dirHandle = null; |
| 7 | + this._fileops = new FileOps(repl, false); |
| 8 | + this._isReadOnly = null; |
| 9 | + } |
| 10 | + |
| 11 | + async readOnly() { |
| 12 | + await this._checkConnection(); |
| 13 | + return this._isReadOnly; |
| 14 | + } |
| 15 | + |
| 16 | + async _checkConnection() { |
| 17 | + if (!this.connectionStatus(true)) { |
| 18 | + throw new Error("Unable to perform file operation. Not Connected."); |
| 19 | + } |
| 20 | + |
| 21 | + if (this._isReadOnly === null) { |
| 22 | + this._isReadOnly = await this._fileops.isReadOnly(); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + async _checkWritable() { |
| 27 | + if (await this.readOnly()) { |
| 28 | + throw new Error("File System is Read Only."); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + async readFile(path, raw = false) { |
| 33 | + await this._checkConnection(); |
| 34 | + let contents = await this._fileops.readFile(path, raw); |
| 35 | + if (contents === null) { |
| 36 | + return raw ? null : ""; |
| 37 | + } |
| 38 | + return contents; |
| 39 | + } |
| 40 | + |
| 41 | + async writeFile(path, offset, contents, modificationTime, raw = false) { |
| 42 | + await this._checkConnection(); |
| 43 | + await this._checkWritable(); |
| 44 | + |
| 45 | + if (!raw) { |
| 46 | + let encoder = new TextEncoder(); |
| 47 | + let same = contents.slice(0, offset); |
| 48 | + let different = contents.slice(offset); |
| 49 | + offset = encoder.encode(same).byteLength; |
| 50 | + contents = encoder.encode(different); |
| 51 | + } else if (offset > 0) { |
| 52 | + contents = contents.slice(offset); |
| 53 | + } |
| 54 | + |
| 55 | + return await this._fileops.writeFile(path, contents, offset, modificationTime, raw); |
| 56 | + } |
| 57 | + |
| 58 | + async makeDir(path, modificationTime = Date.now()) { |
| 59 | + await this._checkConnection(); |
| 60 | + await this._checkWritable(); |
| 61 | + |
| 62 | + return await this._fileops.makeDir(path, modificationTime); |
| 63 | + } |
| 64 | + |
| 65 | + // Returns an array of objects, one object for each file or directory in the given path |
| 66 | + async listDir(path) { |
| 67 | + await this._checkConnection(); |
| 68 | + return await this._fileops.listDir(path); |
| 69 | + } |
| 70 | + |
| 71 | + // Deletes the file or directory at the given path. Directories must be empty. |
| 72 | + async delete(path) { |
| 73 | + await this._checkConnection(); |
| 74 | + await this._checkWritable(); |
| 75 | + |
| 76 | + return await this._fileops.delete(path); |
| 77 | + } |
| 78 | + |
| 79 | + // Moves the file or directory from oldPath to newPath. |
| 80 | + async move(oldPath, newPath) { |
| 81 | + await this._checkConnection(); |
| 82 | + await this._checkWritable(); |
| 83 | + |
| 84 | + return await this._fileops.move(oldPath, newPath); |
| 85 | + } |
| 86 | + |
| 87 | + async versionInfo() { |
| 88 | + // Possibly open /boot_out.txt and read the version info |
| 89 | + let versionInfo = {}; |
| 90 | + console.log("Reading version info"); |
| 91 | + let bootout = await this.readFile('/boot_out.txt', false); |
| 92 | + console.log(bootout); |
| 93 | + if (!bootout) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + // Add these items as they are found |
| 98 | + const searchItems = { |
| 99 | + version: /Adafruit CircuitPython (.*?) on/, |
| 100 | + build_date: /on ([0-9]{4}-[0-9]{2}-[0-9]{2});/, |
| 101 | + board_name: /; (.*?) with/, |
| 102 | + mcu_name: /with (.*?)\r?\n/, |
| 103 | + board_id: /Board ID:(.*?)\r?\n/, |
| 104 | + uid: /UID:([0-9A-F]{12})\r?\n/, |
| 105 | + } |
| 106 | + |
| 107 | + for (const [key, regex] of Object.entries(searchItems)) { |
| 108 | + const match = bootout.match(regex); |
| 109 | + |
| 110 | + if (match) { |
| 111 | + versionInfo[key] = match[1]; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return versionInfo; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +export {FileTransferClient}; |
0 commit comments