Commit 0ded4c3a authored by Skye Yu's avatar Skye Yu

feat: add getFilePartStreaming api

parent b5227a0a
......@@ -104,6 +104,12 @@ declare class Jensen {
getDeviceInfo: (time?: number) => Promise<DeviceInfo>;
listFiles: (time?: number) => Promise<FileInfo[]>;
fileStreaming: (fileName: string, length: number, on?: (msg: Uint8Array | 'fail') => void, onprogress?: (size: number) => void) => void;
getFilePartStreaming: (
fileName: string,
length: number,
on?: (msg: Uint8Array | 'fail') => void,
onprogress?: (size: number) => void
) => void;
getFile: (fileName: string, length: number, on?: (msg: Uint8Array | 'fail') => void, onprogress?: (size: number) => void) => void;
getFilePart: (fileName: string, length: number, on?: (msg: Uint8Array | 'fail') => void, onprogress?: (size: number) => void) => void;
getFileBlock: (fileName: string, length: number, on?: (msg: Uint8Array | 'fail') => void) => Promise<ReturnStruct['common']>;
......
......@@ -336,7 +336,6 @@ function Jensen(log) {
if (device)
device.transferIn(2, RECV_BUFF_SIZE).then((r) => {
Logger.save?.('jensen', 'tryReceive', r?.data);
console.log('tryReceive', self.streamingBegin);
if (self.streamingBegin) {
worker.postMessage(r.data);
} else {
......@@ -491,7 +490,6 @@ function Jensen(log) {
let dataLen = buffLength - startIndex;
if (dataLen < 12) return null;
if (dataView[startIndex + 0] !== 0x12 || dataView[startIndex + 1] !== 0x34) {
console.log('0000000000000000000000', dataView);
throw new Error('invalid header');
}
// 2 字节的指令id
......@@ -518,10 +516,8 @@ function Jensen(log) {
// 需要去除的字节数
var cutLen = 0;
// 数据还没有完全准备好
if (dataLen < 12 + len + padding) {
console.log(dataLen, len, padding);
return null;
}
if (dataLen < 12 + len + padding) return null;
// 去掉header部分
// 下面这一行做什么用的?
// for (let i = 0; i < 12; i++) this.buffer[i + cutLen];
......@@ -835,6 +831,41 @@ Jensen.prototype.fileStreaming = async function (filename, length, ondata, onpro
this.send(new Command(TRANSFER_FILE).body(fname));
};
Jensen.prototype.getFilePartStreaming = async function (filename, length, ondata, onprogress) {
if (typeof length != 'number') throw new Error('parameter `length` required');
if (length <= 0) throw new Error('parameter `length` must greater than zero');
Logger.info('jensen', 'getFilePart', `file download start. filename: ${filename}, length: ${length} `);
let data = [];
data.push((length >> 24) & 0xff);
data.push((length >> 16) & 0xff);
data.push((length >> 8) & 0xff);
data.push((length >> 0) & 0xff);
for (let i = 0; i < filename.length; i++) data.push(filename.charCodeAt(i));
let flen = 0;
let handler = (msg) => {
if (msg != null) {
flen += msg.body.length || msg.body.byteLength;
ondata(msg.body);
if (flen >= length) {
this.resetStreamingData();
Logger.info('jensen', 'getFilePart', 'file download finish.');
return 'OK';
}
} else {
this.resetStreamingData();
Logger.info('jensen', 'getFilePart', 'file download fail.');
ondata('fail');
}
};
this.streamingBegin = true;
this.onreceive = onprogress;
Jensen.registerHandler(GET_FILE_BLOCK, handler);
this.send(new Command(GET_FILE_BLOCK).body(data));
};
Jensen.prototype.getFilePart = async function (filename, length, ondata, onprogress) {
if (typeof length != 'number') throw new Error('parameter `length` required');
if (length <= 0) throw new Error('parameter `length` must greater than zero');
......@@ -1062,7 +1093,6 @@ Jensen.prototype.getRealtime = async function (frames) {
Jensen.registerHandler(REALTIME_CONTROL, commonMessageParser);
Jensen.registerHandler(REALTIME_READ_SETTING, (msg) => {
console.log(msg);
return msg;
});
Jensen.registerHandler(REALTIME_TRANSFER, (msg) => {
......
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