Commit a0cab8d1 authored by Skye Yu's avatar Skye Yu

feat(fileStreaming): Provides a file streaming API

parent 90957233
......@@ -68,6 +68,7 @@ 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;
getFile: (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']>;
requestFirmwareUpgrade: (vn: number, length: number, time?: number) => Promise<{ result: 'accepted' | 'fail' }>;
......
......@@ -143,6 +143,7 @@ function Jensen(log) {
worker.onmessage = (data) => {
if (this.streamingBegin) {
taskQueue.addTask(() => continueDecode(data.data));
tryReceive();
}
};
}
......@@ -317,8 +318,12 @@ 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 {
receive(r);
}
});
};
......@@ -752,7 +757,7 @@ Jensen.prototype.setTime = async function (time, seconds) {
return this.send(new Command(SET_DEVICE_TIME).body(this.to_bcd(str)), seconds);
};
Jensen.prototype.streaming = async function (filename, length, ondata, onprogress) {
Jensen.prototype.fileStreaming = 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');
......@@ -768,14 +773,17 @@ Jensen.prototype.streaming = async function (filename, length, ondata, onprogres
ondata(msg.body);
Logger.info('jensen', 'streaming length', `${length} ${flen}`);
if (flen >= length) {
this.resetStreamingData();
Logger.info('jensen', 'streaming', 'file download finish.');
return 'OK';
}
} else {
this.resetStreamingData();
Logger.info('jensen', 'streaming', 'file download fail.');
ondata('fail');
}
};
this.streamingBegin = true;
this.onreceive = onprogress;
Jensen.registerHandler(TRANSFER_FILE, handler);
this.send(new Command(TRANSFER_FILE).body(fname));
......@@ -840,7 +848,6 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress)
ondata(msg.body);
Logger.info('jensen', 'getFile length', `${length} ${flen}`);
if (flen >= length) {
this.resetStreamingData();
document.removeEventListener('visibilitychange', visibilitychange);
Logger.info('jensen', 'getFile', 'file download finish.');
clearEventAndTask();
......@@ -848,14 +855,12 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress)
return 'OK';
}
} else {
this.resetStreamingData();
document.removeEventListener('visibilitychange', visibilitychange);
clearEventAndTask();
Logger.info('jensen', 'getFile', 'file download fail.');
ondata('fail');
}
};
this.streamingBegin = true;
this.onreceive = onprogress;
Jensen.registerHandler(TRANSFER_FILE, handler);
this.send(new Command(TRANSFER_FILE).body(fname));
......
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