Commit 7befde52 authored by martin hou's avatar martin hou

Merge branch 'v20250816' into device-mgr

parents 2df424ff 7f27a5e9
...@@ -301,6 +301,8 @@ declare class Jensen { ...@@ -301,6 +301,8 @@ declare class Jensen {
// 更新设备UAC固件 // 更新设备UAC固件
updateUAC: (data: Uint8Array, seconds?: number) => Promise<ReturnStruct['common']>; updateUAC: (data: Uint8Array, seconds?: number) => Promise<ReturnStruct['common']>;
dump: (void);
} }
export = Jensen; export = Jensen;
...@@ -244,6 +244,10 @@ function Jensen(log, conn) { ...@@ -244,6 +244,10 @@ function Jensen(log, conn) {
return device; return device;
} }
this.busy = function() {
return Boolean(current);
}
this.setUSBDevice = function (dev) { this.setUSBDevice = function (dev) {
device = dev; device = dev;
ready = false; ready = false;
...@@ -290,15 +294,13 @@ function Jensen(log, conn) { ...@@ -290,15 +294,13 @@ function Jensen(log, conn) {
let data = cmd.make(); let data = cmd.make();
current = 'cmd-' + cmd.command + '-' + cmd.index; current = 'cmd-' + cmd.command + '-' + cmd.index;
Logger.debug('jensen', 'sendNext', pid + '-command: ' + cmd.command + ', data bytes: ' + data.byteLength); Logger.debug('jensen', 'sendNext', pid + '-command: ' + current + ', data bytes: ' + data.byteLength);
self.timewait = cmd.command == TRANSFER_FILE || cmd.command == GET_FILE_BLOCK ? 1000 : 10; self.timewait = cmd.command == TRANSFER_FILE || cmd.command == GET_FILE_BLOCK ? 1000 : 10;
console.log(pid + '-send', data); console.log(pid + '-send', data);
if (self.onprogress && (cmd.command == TRANSFER_FILE || cmd.command == GET_FILE_BLOCK)) if (self.onprogress && cmd.command == TRANSFER_FILE)
{ {
self.transferStartTime = new Date().getTime(); try { self.onprogress(0); } catch(e) {}
Logger.debug('jensen', 'file-transfer', new Date().getTime());
try { self.onprogress(0); } catch(e) { }
} }
await device.transferOut(1, data).catch((e) => crash('sendNext', e)); await device.transferOut(1, data).catch((e) => crash('sendNext', e));
if (cmd.onprogress) cmd.onprogress(1, 1); if (cmd.onprogress) cmd.onprogress(1, 1);
...@@ -337,9 +339,11 @@ function Jensen(log, conn) { ...@@ -337,9 +339,11 @@ function Jensen(log, conn) {
timeout(tag); timeout(tag);
}, seconds * 1000) }, seconds * 1000)
: null; : null;
Logger.debug('jensen', 'register', tag);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
actions[tag] = { actions[tag] = {
tag: tag, tag: tag,
createtime: new Date().getTime(),
resolve: resolve, resolve: resolve,
reject: reject, reject: reject,
timeout: t timeout: t
...@@ -353,18 +357,21 @@ function Jensen(log, conn) { ...@@ -353,18 +357,21 @@ function Jensen(log, conn) {
const trigger = function (resp, msgid) { const trigger = function (resp, msgid) {
if (current == null) return; if (current == null) return;
Logger.debug('jensen', 'trigger', pid + ':trigger - ' + current.substring(0, current.lastIndexOf('-')) + ' <---> cmd-' + (msgid || 'err')); Logger.debug('jensen', 'trigger', pid + ':trigger - ' + current + ' <---> cmd-' + (msgid || 'err'));
if (current.substring(0, current.lastIndexOf('-')) != 'cmd-' + msgid) { if (current.substring(0, current.lastIndexOf('-')) != 'cmd-' + msgid) {
// current = null; // current = null;
Logger.info('jensen', 'trigger', 'return cuz cmd mismatched');
return; return;
} }
if (current in actions == false) { if (current in actions == false) {
Logger.debug('jensen', 'trigger', pid + ':no action registered'); Logger.debug('jensen', 'trigger', pid + ':no action registered');
current = null;
return; return;
} }
let p = actions[current]; let p = actions[current];
if (p.timeout) clearTimeout(p.timeout); if (p.timeout) clearTimeout(p.timeout);
p.resolve(resp); p.resolve(resp);
Logger.info('jensen', 'trigger', p.tag + ', cost: ' + (new Date().getTime() - p.createtime) + 'ms');
delete actions[current]; delete actions[current];
current = null; current = null;
}; };
...@@ -381,12 +388,13 @@ function Jensen(log, conn) { ...@@ -381,12 +388,13 @@ function Jensen(log, conn) {
console.log('pending commands', commands); console.log('pending commands', commands);
console.log('current', current); console.log('current', current);
console.log('device', device); console.log('device', device);
}; return {
'actions': actions,
this.busy = function() { 'commands': commands,
return Boolean(current); 'current': current,
// return false 'device': device
} }
};
const tryReceive = function () { const tryReceive = function () {
if (device) if (device)
...@@ -474,7 +482,7 @@ function Jensen(log, conn) { ...@@ -474,7 +482,7 @@ function Jensen(log, conn) {
Logger.debug( Logger.debug(
'jensen', 'jensen',
'receive', 'receive',
pid + '-recv: ' + cname + ', seq: ' + msg.sequence + ', data bytes: ' + msg.body?.byteLength + ', data: ' + heading.join(' ') pid + '-recv: cmd=' + msg.id + ', seq: ' + msg.sequence + ', data bytes: ' + msg.body?.byteLength + ', data: ' + heading.join(' ')
); );
try { try {
let handler = handlers[msg.id]; let handler = handlers[msg.id];
...@@ -1013,18 +1021,22 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress) ...@@ -1013,18 +1021,22 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress)
}; };
// this.onreceive = onprogress; // this.onreceive = onprogress;
this._progress_report = {}; this._progress_report = {};
// try { onprogress(0); } catch(e) {}
let self = this;
this.onprogress = onprogress;
this.onreceive = function(recvBytes) this.onreceive = function(recvBytes)
{ {
let percent = Math.floor(recvBytes / length * 100); let percent = Math.floor(recvBytes / length * 100);
let k = 't' + percent; let k = 't' + percent;
if (percent % 5 == 0 && percent > 0 && percent < 100) if (percent > 0 && percent < 100)
{ {
this._progress_report[k] && onprogress?.(recvBytes); if (!(k in self._progress_report)) onprogress?.(recvBytes);
this._progress_report[k] = true; self._progress_report[k] = true;
} }
if (percent == 100) if (percent >= 100 && !(k in self._progress_report))
{ {
onprogress?.(recvBytes); onprogress?.(length);
self._progress_report[k] = true;
} }
} }
this.registerHandler(TRANSFER_FILE, handler); this.registerHandler(TRANSFER_FILE, handler);
...@@ -1139,11 +1151,10 @@ Jensen.prototype.getFileBlock = async function (filename, length, ondata) { ...@@ -1139,11 +1151,10 @@ Jensen.prototype.getFileBlock = async function (filename, length, ondata) {
Jensen.prototype.getBatteryStatus = function(seconds) Jensen.prototype.getBatteryStatus = function(seconds)
{ {
if (this.model != 'hidock-p1') return null; if (this.model != 'hidock-p1') return null;
// 如果正在处理其它消息任务,则直接取消电池电量查询消息
if (this.busy()) if (this.busy())
{ {
// Logger.info('jensen', 'dispatcher', 'cancel battery status query...'); Logger.debug('jensen', 'dispatcher', 'cancel getBatteryStatus cuz busy');
// return null; return;
} }
return this.send(new Command(GET_BATTERY_STATUS), seconds); return this.send(new Command(GET_BATTERY_STATUS), seconds);
} }
......
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