Commit b61d70a9 authored by Skye Yu's avatar Skye Yu

test: download File

parent 6f1ab5dd
import { Logger } from './utils/utils';
import {Logger} from './utils/utils';
const QUERY_DEVICE_INFO = 0x01;
const QUERY_DEVICE_TIME = 0x02;
......@@ -124,7 +124,7 @@ function Jensen() {
if (r) return;
let conn = await navigator.usb.requestDevice({
filters: [{ vendorId: 0x10d6 }],
filters: [{vendorId: 0x10d6}],
});
await conn.open();
self.model = conn.productId == 45069 ? 'hidock-h1e' : 'hidock-h1';
......@@ -172,7 +172,8 @@ function Jensen() {
Logger.info('jensen', 'disconnect', 'disconnect');
try {
await device?.close();
} catch (e) {}
} catch (e) {
}
};
this.send = function (cmd, seconds, onprogress) {
......@@ -313,7 +314,8 @@ function Jensen() {
if (self.onreceive)
try {
self.onreceive(totalBytes);
} catch (e) {}
} catch (e) {
}
};
const tryDecode = function () {
......@@ -453,7 +455,7 @@ function Jensen() {
// for (let i = 0; i < padding; i++) this.buffer[i + cutLen];
cutLen += padding;
// self.buffer = self.buffer.slice(cutLen);
return { message: new Message(cmdid, sequeue, body), length: cutLen };
return {message: new Message(cmdid, sequeue, body), length: cutLen};
};
this.to_bcd = function (str) {
......@@ -590,7 +592,7 @@ Jensen.prototype.listFiles = async function () {
let fnpad = function (v) {
return v > 9 ? v : '0' + v;
};
for (let i = start; i < data.length; ) {
for (let i = start; i < data.length;) {
let len = 0;
let fname = [];
......@@ -693,18 +695,30 @@ Jensen.prototype.getFile = async function (filename, length, ondata, onprogress)
let fname = [];
for (let i = 0; i < filename.length; i++) fname.push(filename.charCodeAt(i));
function visibilitychange() {
if (document.visibilityState === 'hidden') {
console.log('Page is hidden');
// 可以选择暂停数据传输,或通过 Web Worker 继续后台任务
} else {
console.log('Page is visible');
// 重新开始或恢复数据传输
}
}
document.addEventListener('visibilitychange', visibilitychange);
let flen = 0;
let handler = (msg) => {
if (msg != null) {
flen += msg.body.length || msg.body.byteLength;
// let hexString = Array.from(msg.body).map(b => b.toString(16).padStart(2, '0')).join('');
// let regex = /4869446f636b564d.*/;
// let result = hexString.replace(regex, '');
// let data = new Uint8Array(result.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
ondata(msg.body);
if (flen >= length) {
document.removeEventListener('visibilitychange', visibilitychange);
// return OK indicates all file blocks received
if (flen >= length) return 'OK';
return 'OK';
}
} else {
document.removeEventListener('visibilitychange', visibilitychange);
ondata('fail');
}
};
......@@ -742,25 +756,25 @@ Jensen.prototype.endBNC = async function (seconds) {
Jensen.prototype.getSettings = async function (seconds) {
if (this.versionNumber < 327714) {
return { autoRecord: false, autoPlay: false };
return {autoRecord: false, autoPlay: false};
}
return this.send(new Command(GET_SETTINGS), seconds);
};
Jensen.prototype.setAutoRecord = function (enable, seconds) {
if (this.versionNumber < 327714) return { result: false };
if (this.versionNumber < 327714) return {result: false};
return this.send(new Command(SET_SETTINGS).body([0, 0, 0, enable ? 1 : 2]), seconds);
};
Jensen.prototype.setAutoPlay = function (enable, seconds) {
if (this.versionNumber < 327714) return { result: false };
if (this.versionNumber < 327714) return {result: false};
return this.send(new Command(SET_SETTINGS).body([0, 0, 0, 0, 0, 0, 0, enable ? 1 : 2,]), seconds);
};
Jensen.prototype.setNotification = function (enable, seconds) {
if (this.versionNumber < 327714) return { result: false };
if (this.versionNumber < 327714) return {result: false};
return this.send(new Command(SET_SETTINGS).body([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, enable ? 1 : 2]), seconds);
};
Jensen.prototype.setBluetoothPromptPlay = function(enable, seconds) {
if (this.model === 'hidock-h1e' && this.versionNumber < 393218) return { result: false }
if (this.model === 'hidock-h1' && this.versionNumber < 327732) return { result: false }
Jensen.prototype.setBluetoothPromptPlay = function (enable, seconds) {
if (this.model === 'hidock-h1e' && this.versionNumber < 393218) return {result: false}
if (this.model === 'hidock-h1' && this.versionNumber < 327732) return {result: false}
return this.send(new Command(SET_SETTINGS).body([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, enable ? 1 : 2]), seconds);
}
......@@ -788,8 +802,8 @@ Jensen.prototype.test = async function (seconds) {
return this.send(new Command(DEVICE_MSG_TEST), seconds);
};
Jensen.prototype.getFileBlock = async function(filename, length, ondata) {
if (typeof(length) != 'number') throw new Error('parameter `length` required');
Jensen.prototype.getFileBlock = async function (filename, length, ondata) {
if (typeof (length) != 'number') throw new Error('parameter `length` required');
if (length <= 0) throw new Error('parameter `length` must greater than zero');
if (this.fileLength > 0) return null;
......@@ -808,7 +822,7 @@ Jensen.prototype.getFileBlock = async function(filename, length, ondata) {
}
const commonMessageParser = (msg) => {
return { result: msg.body[0] === 0x00 ? 'success' : 'failed' };
return {result: msg.body[0] === 0x00 ? 'success' : 'failed'};
};
Jensen.registerHandler(SET_DEVICE_TIME, commonMessageParser);
Jensen.registerHandler(BNC_DEMO_TEST, commonMessageParser);
......@@ -817,7 +831,7 @@ Jensen.registerHandler(DELETE_FILE, (msg) => {
if (msg.body[0] === 0x00) rst = 'success';
else if (msg.body[0] === 0x01) rst = 'not-exists';
else if (msg.body[0] === 0x02) rst = 'failed';
return { result: rst };
return {result: rst};
});
Jensen.registerHandler(QUERY_DEVICE_INFO, (msg, jensen) => {
let vc = [],
......@@ -859,19 +873,19 @@ Jensen.registerHandler(QUERY_DEVICE_TIME, (msg, jensen) => {
};
});
Jensen.registerHandler(QUERY_FILE_COUNT, (msg) => {
if (msg.body.length === 0) return { count: 0 };
if (msg.body.length === 0) return {count: 0};
let c = 0;
for (let i = 0; i < 4; i++) {
let b = msg.body[i] & 0xff;
c = c | (b << ((4 - i - 1) * 8));
}
return { count: c };
return {count: c};
});
Jensen.registerHandler(GET_SETTINGS, (msg) => {
let r1 = msg.body[3];
let r2 = msg.body[7];
let r4 = msg.body[15];
let rst = { autoRecord: r1 === 1, autoPlay: r2 === 1, bluetoothTone: r4 === 1 };
let rst = {autoRecord: r1 === 1, autoPlay: r2 === 1, bluetoothTone: r4 === 1};
if (msg.body.length >= 12) {
let r3 = msg.body[11] === 1;
rst['notification'] = r3;
......@@ -888,11 +902,11 @@ Jensen.registerHandler(REQUEST_FIRMWARE_UPGRADE, (msg) => {
if (c === 0x01) rst = 'wrong-version';
if (c === 0x02) rst = 'busy';
if (c === 0x03) return 'unknown';
return { result: rst };
return {result: rst};
});
Jensen.registerHandler(FIRMWARE_UPLOAD, (msg) => {
let c = msg.body[0];
return { result: c === 0x00 ? 'success' : 'failed' };
return {result: c === 0x00 ? 'success' : 'failed'};
});
Jensen.registerHandler(READ_CARD_INFO, (msg) => {
let i = 0;
......@@ -900,11 +914,11 @@ Jensen.registerHandler(READ_CARD_INFO, (msg) => {
let capacity = ((msg.body[i++] & 0xff) << 24) | ((msg.body[i++] & 0xff) << 16) | ((msg.body[i++] & 0xff) << 8) | (msg.body[i++] & 0xff);
let status = ((msg.body[i++] & 0xff) << 24) | ((msg.body[i++] & 0xff) << 16) | ((msg.body[i++] & 0xff) << 8) | (msg.body[i++] & 0xff);
return { used: used, capacity: capacity, status: status.toString(16) };
return {used: used, capacity: capacity, status: status.toString(16)};
});
Jensen.registerHandler(FORMAT_CARD, commonMessageParser);
Jensen.registerHandler(GET_RECORDING_FILE, (msg) => {
if (msg.body == null || msg.body.length === 0) return { recording: null };
if (msg.body == null || msg.body.length === 0) return {recording: null};
else {
let fname = [];
for (var i = 0; i < msg.body.length; i++) {
......@@ -947,4 +961,4 @@ Jensen.registerHandler(RECORD_TEST_END, commonMessageParser);
Jensen.registerHandler(DEVICE_MSG_TEST, commonMessageParser);
Jensen.registerHandler(GET_FILE_BLOCK, commonMessageParser);
export { Jensen };
export {Jensen};
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