Commit 00e8a809 authored by martin hou's avatar martin hou

feat: 增加实例内handler注册机制

parent 68b04970
...@@ -80,11 +80,11 @@ function Jensen(log, conn) { ...@@ -80,11 +80,11 @@ function Jensen(log, conn) {
let sequence = 0; let sequence = 0;
let current = null; let current = null;
let commands = []; let commands = [];
let handlers = [];
let statusTimeout = null; let statusTimeout = null;
let recv = false; let recv = false;
let ready = false; let ready = false;
let totalBytes = 0; let totalBytes = 0;
let handlers = {};
let self = this; let self = this;
this.data = {}; this.data = {};
...@@ -419,7 +419,8 @@ function Jensen(log, conn) { ...@@ -419,7 +419,8 @@ function Jensen(log, conn) {
'recv: ' + cname + ', seq: ' + msg.sequence + ', data bytes: ' + msg.body?.byteLength + ', data: ' + heading.join(' ') 'recv: ' + cname + ', seq: ' + msg.sequence + ', data bytes: ' + msg.body?.byteLength + ', data: ' + heading.join(' ')
); );
try { try {
let handler = Jensen.handlers[msg.id]; let handler = handlers[msg.id];
if (!handler) handler = Jensen.handlers[msg.id];
let r = handler(msg, self); let r = handler(msg, self);
if (r) trigger(r, msg.id); if (r) trigger(r, msg.id);
} catch (e) { } catch (e) {
...@@ -434,7 +435,8 @@ function Jensen(log, conn) { ...@@ -434,7 +435,8 @@ function Jensen(log, conn) {
// 5是msgid // 5是msgid
let msgid = parseInt(current.replace(/^cmd-(\d+)-(\d+)$/gi, '$1')); let msgid = parseInt(current.replace(/^cmd-(\d+)-(\d+)$/gi, '$1'));
try { try {
let handler = Jensen.handlers[msgid]; let handler = handlers[msgid];
if (!handler) handler = Jensen.handlers[msgid];
handler(null, self); handler(null, self);
} catch (e) { } catch (e) {
trigger(e); trigger(e);
...@@ -523,6 +525,10 @@ function Jensen(log, conn) { ...@@ -523,6 +525,10 @@ function Jensen(log, conn) {
} }
return str; return str;
}; };
this.registerHandler = function (cmdid, handler) {
handlers[cmdid] = handler;
};
} }
function Command(id) { function Command(id) {
...@@ -631,7 +637,7 @@ Jensen.prototype.getBluetoothStatus = async function (seconds) { ...@@ -631,7 +637,7 @@ Jensen.prototype.getBluetoothStatus = async function (seconds) {
}; };
Jensen.prototype.listFiles = async function () { Jensen.prototype.listFiles = async function () {
let tag = 'filelist'; let tag = 'filelist-' + this.serialNumber;
if (this[tag] != null) return null; if (this[tag] != null) return null;
let fc = null; let fc = null;
...@@ -645,7 +651,7 @@ Jensen.prototype.listFiles = async function () { ...@@ -645,7 +651,7 @@ Jensen.prototype.listFiles = async function () {
let self = this; let self = this;
// let tag = 'data_' + new Date().getTime(); // let tag = 'data_' + new Date().getTime();
this[tag] = []; this[tag] = [];
Jensen.registerHandler(QUERY_FILE_LIST, (msg, jensen) => { this.registerHandler(QUERY_FILE_LIST, (msg, jensen) => {
if (msg.body.length == 0) { if (msg.body.length == 0) {
jensen[tag] = null; jensen[tag] = null;
return []; return [];
...@@ -944,7 +950,7 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress) ...@@ -944,7 +950,7 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress)
onprogress?.(recvBytes); onprogress?.(recvBytes);
} }
} }
Jensen.registerHandler(TRANSFER_FILE, handler); this.registerHandler(TRANSFER_FILE, handler);
this.send(new Command(TRANSFER_FILE).body(fname)); 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