Commit 07cc2ab6 authored by Skye Yu's avatar Skye Yu

test:

parent 56d4dc7e
...@@ -75,6 +75,7 @@ function Jensen(log) { ...@@ -75,6 +75,7 @@ function Jensen(log) {
this.onreceive = null; this.onreceive = null;
const RECV_BUFF_SIZE = 51200; const RECV_BUFF_SIZE = 51200;
let worker = null;;
const _check_conn_status = () => { const _check_conn_status = () => {
if (device?.opened === false) { if (device?.opened === false) {
...@@ -145,6 +146,10 @@ function Jensen(log) { ...@@ -145,6 +146,10 @@ function Jensen(log) {
Logger.error('jensen', 'init', 'webusb not supported'); Logger.error('jensen', 'init', 'webusb not supported');
return; return;
} }
if (window.worker) {
worker = new Worker('./utils/worker.js');
Worker.postMessage('Hello, World.');
}
navigator.usb.onconnect = function (e) { navigator.usb.onconnect = function (e) {
self.tryconnect(); self.tryconnect();
}; };
...@@ -294,6 +299,7 @@ function Jensen(log) { ...@@ -294,6 +299,7 @@ function Jensen(log) {
if (device) if (device)
device.transferIn(2, RECV_BUFF_SIZE).then((r) => { device.transferIn(2, RECV_BUFF_SIZE).then((r) => {
Logger.save?.('jensen', 'tryReceive', r?.data); Logger.save?.('jensen', 'tryReceive', r?.data);
worker.postMessage(r);
receive(r); receive(r);
}); });
}; };
...@@ -701,6 +707,35 @@ Jensen.prototype.setTime = async function (time, seconds) { ...@@ -701,6 +707,35 @@ Jensen.prototype.setTime = async function (time, seconds) {
return this.send(new Command(SET_DEVICE_TIME).body(this.to_bcd(str)), seconds); return this.send(new Command(SET_DEVICE_TIME).body(this.to_bcd(str)), seconds);
}; };
Jensen.prototype.streaming = 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', 'streaming', `file download start. filename: ${filename}, length: ${length} `);
let fname = [];
for (let i = 0; i < filename.length; i++) fname.push(filename.charCodeAt(i));
let flen = 0;
let handler = (msg) => {
if (msg != null) {
flen += msg.body.length || msg.body.byteLength;
ondata(msg.body);
Logger.info('jensen', 'streaming length', `${length} ${flen}`);
if (flen >= length) {
Logger.info('jensen', 'streaming', 'file download finish.');
return 'OK';
}
} else {
Logger.info('jensen', 'streaming', 'file download fail.');
ondata('fail');
}
};
this.onreceive = onprogress;
Jensen.registerHandler(TRANSFER_FILE, handler);
this.send(new Command(TRANSFER_FILE).body(fname));
};
Jensen.prototype.getFile = async function (filename, length, ondata, onprogress) { Jensen.prototype.getFile = async function (filename, length, ondata, onprogress) {
if (typeof length != 'number') throw new Error('parameter `length` required'); if (typeof length != 'number') throw new Error('parameter `length` required');
if (length <= 0) throw new Error('parameter `length` must greater than zero'); if (length <= 0) throw new Error('parameter `length` must greater than zero');
......
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