Commit e87b778c authored by martin hou's avatar martin hou

fix: 调整readFile实现支持大块传输

parent ca7daefb
......@@ -248,6 +248,11 @@ export function Home() {
// alert(fc?.count);
let files = await jensen.listFiles();
console.log(files);
let totalLength = 0;
for (const f of files) {
totalLength += f.length;
}
Logger.info('jensen', 'listFiles', 'Total Length: ' + (totalLength / 1024 / 1024).toFixed(2) + 'MB');
setFiles(files)
await releaseJensen();
}
......@@ -829,11 +834,19 @@ export function Home() {
if (jensen == null) return;
const fname = '2026Feb04-152156-Rec00.wav';
let rst = await jensen.readFile(fname, 0, 1024 * 1024 * 2);
console.log('xxxxxx0', rst);
console.log('xxxxxx0', rst.length);
// Logger.info('jensen', 'read-block', 'Read Block: ' + JSON.stringify(rst));
await releaseJensen();
}
const readCardInfo = async () => {
let jensen = await getJensen();
if (jensen == null) return;
let rst = await jensen.getCardInfo(5);
Logger.info('jensen', 'card-info', 'Get Card Info: ' + JSON.stringify(rst));
await releaseJensen();
}
return (
<>
<div className="btn-container" style={{ display: 'flex', flexDirection: 'row', gap: '16px', padding: '16px', alignItems: 'center', flexWrap: 'wrap' }}>
......@@ -842,6 +855,7 @@ export function Home() {
<button onClick={getTime}>Get Time</button>
<button onClick={listFiles}>List Files</button>
<button onClick={transferFile}>Transfer</button>
<button onClick={readCardInfo}>Card Info</button>
<button onClick={batteryStatus}>Battery</button>
<button onClick={getBluetoothStatus}>Bluetooth Status</button>
<button onClick={bluetoothScan}>Start Scan</button>
......
......@@ -339,7 +339,7 @@ function Jensen(log, conn) {
current = 'cmd-' + cmd.command + '-' + cmd.index;
Logger.debug('jensen', 'sendNext', pid + '-command: ' + current + ', data bytes: ' + data.byteLength);
self.timewait = cmd.command == TRANSFER_FILE || cmd.command == GET_FILE_BLOCK ? 1000 : 10;
self.timewait = cmd.command == TRANSFER_FILE || cmd.command == GET_FILE_BLOCK || cmd.command == TRANSFER_FILE_PARTIAL ? 1000 : 10;
console.log(pid + '-send', data);
if (self.onprogress && cmd.command == TRANSFER_FILE)
{
......@@ -1059,7 +1059,9 @@ Jensen.prototype.deleteFile = async function (filename, seconds) {
};
Jensen.prototype.readFile = async function (filename, offset, length, seconds) {
// 如果确定支持的版本号
if (typeof length != 'number') throw new Error('parameter `length` required');
if (length <= 0) throw new Error('parameter `length` must greater than zero');
let data = [];
data.push((offset >> 24) & 0xff);
data.push((offset >> 16) & 0xff);
......@@ -1070,6 +1072,33 @@ Jensen.prototype.readFile = async function (filename, offset, length, seconds) {
data.push((length >> 8) & 0xff);
data.push((length >> 0) & 0xff);
for (let i = 0; i < filename.length; i++) data.push(filename.charCodeAt(i));
let chunks = [];
let flen = 0;
let handler = (msg) => {
if (msg != null) {
let chunk = msg.body;
let chunkLen = chunk.length || chunk.byteLength;
flen += chunkLen;
chunks.push(chunk);
if (flen >= length) {
let result = new Uint8Array(Math.min(flen, length));
let pos = 0;
for (let i = 0; i < chunks.length && pos < result.length; i++) {
let c = chunks[i];
let cview = c instanceof Uint8Array ? c : new Uint8Array(c);
let toCopy = Math.min(cview.length, result.length - pos);
result.set(cview.subarray(0, toCopy), pos);
pos += toCopy;
}
return result;
}
} else {
Logger.info('jensen', 'readFile', 'file transfer fail.');
return null;
}
};
this.registerHandler(TRANSFER_FILE_PARTIAL, handler);
return this.send(new Command(TRANSFER_FILE_PARTIAL).body(data), seconds);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment