Commit 293a23bd authored by carl's avatar carl

feat: 新增onerror方法错误提示

parent 0cf71b5f
...@@ -139,6 +139,8 @@ declare class Jensen { ...@@ -139,6 +139,8 @@ declare class Jensen {
connect: () => Promise<void>; connect: () => Promise<void>;
// 事件:当连接完成时触发 // 事件:当连接完成时触发
onconnect: () => void; onconnect: () => void;
// 事件:当连接异常时触发
onerror: (e: Error) => void;
// 主动断开连接(单连接时适用) // 主动断开连接(单连接时适用)
disconnect: () => void; disconnect: () => void;
// 事件:当连接断开时触发 // 事件:当连接断开时触发
......
This diff is collapsed.
...@@ -15,9 +15,16 @@ export function Home() { ...@@ -15,9 +15,16 @@ export function Home() {
jensen.onconnect = () => { jensen.onconnect = () => {
console.log('connect successfully'); console.log('connect successfully');
jensen.getDeviceInfo().then((info) => { jensen.getDeviceInfo().then((info) => {
console.log('getDeviceInfo', info);
alert(info.sn + ' connected'); alert(info.sn + ' connected');
}).catch((e) => {
console.log('getDeviceInfo error', e);
}); });
}; };
// jensen.onerror = (e) => {
// console.log('onerror', e);
// alert('此设备已经在其它已打开的HiNotes网页上建立连接');
// };
}, []); }, []);
const getFilePart = () => { const getFilePart = () => {
...@@ -40,6 +47,25 @@ export function Home() { ...@@ -40,6 +47,25 @@ export function Home() {
setDevices(devices); setDevices(devices);
setGreeting(null) setGreeting(null)
} }
const connecty = async () => {
const usb = (navigator as any).usb;
let conn = await usb.requestDevice({
filters: [{ vendorId: 0x10d6 }, { vendorId: 0x3887 }]
});
if (conn == null) return null;
// if (conn.opened) throw new Error('device_already_opened');
await conn.open();
let jensen = new Jensen(Logger, conn);
jensen.onerror = (e) => {
console.log('onerror', e);
alert('此设备已经在其它已打开的HiNotes网页上建立连接');
};
await jensen.initialize();
let dinfo = await jensen.getDeviceInfo();
if (dinfo)
console.log('dinfo', dinfo);
return null;
}
const connect = async () => { const connect = async () => {
await jensen.connect(); await jensen.connect();
...@@ -220,6 +246,7 @@ export function Home() { ...@@ -220,6 +246,7 @@ export function Home() {
return ( return (
<> <>
<div style={{ display: 'flex', flexDirection: 'row', gap: '16px', padding: '16px', alignItems: 'center', flexWrap: 'wrap' }}> <div style={{ display: 'flex', flexDirection: 'row', gap: '16px', padding: '16px', alignItems: 'center', flexWrap: 'wrap' }}>
<button onClick={connecty}>连接新设备</button>
<button onClick={connect}>连接</button> <button onClick={connect}>连接</button>
<button onClick={getFilePart}>获取文件</button> <button onClick={getFilePart}>获取文件</button>
<button onClick={writeSN}>SN写号</button> <button onClick={writeSN}>SN写号</button>
......
...@@ -99,6 +99,7 @@ function Jensen(log, conn) { ...@@ -99,6 +99,7 @@ function Jensen(log, conn) {
this.isStopConnectionCheck = false; this.isStopConnectionCheck = false;
this.onconnect = null; this.onconnect = null;
this.onreceive = null; this.onreceive = null;
this.onerror = null;
const RECV_BUFF_SIZE = 51200; const RECV_BUFF_SIZE = 51200;
...@@ -382,7 +383,11 @@ function Jensen(log, conn) { ...@@ -382,7 +383,11 @@ function Jensen(log, conn) {
Logger.save?.('jensen', 'tryReceive', r?.data); Logger.save?.('jensen', 'tryReceive', r?.data);
console.log(pid + ' Receive', r?.data); console.log(pid + ' Receive', r?.data);
receive(r); receive(r);
}).catch((e) => {
console.log('tryReceive error', e)
self.onerror?.(e);
}); });
}; };
const read_int = function (a, b, c, d) { const read_int = function (a, b, c, d) {
...@@ -394,6 +399,10 @@ function Jensen(log, conn) { ...@@ -394,6 +399,10 @@ function Jensen(log, conn) {
}; };
const receive = function (result) { const receive = function (result) {
if(result instanceof Error) {
console.log('receive error', result);
return
}
totalBytes += result.data.byteLength; totalBytes += result.data.byteLength;
// 做一个回调,怎么样找到它呢? // 做一个回调,怎么样找到它呢?
blocks.push(result.data); blocks.push(result.data);
......
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