Commit 39cf30f8 authored by 曾思程's avatar 曾思程

jensen里的log可以外部导入

parent 30fd5b86
import { Logger } from './src/utils/utils'
declare module 'jensen' { declare module 'jensen' {
export type DeviceInfo = { export type DeviceInfo = {
sn: string; sn: string;
...@@ -17,6 +19,7 @@ declare module 'jensen' { ...@@ -17,6 +19,7 @@ declare module 'jensen' {
signature: string; signature: string;
}; };
export default class Jensen { export default class Jensen {
constructor(log: typeof Logger)
onconnect: Function; onconnect: Function;
onmessage: (data: any) => void | null; onmessage: (data: any) => void | null;
constructor(); constructor();
......
{ {
"name": "jensen", "name": "jensen",
"version": "1.0.0", "version": "1.1.3",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "jensen", "name": "jensen",
"version": "1.0.0", "version": "1.1.3",
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"prettier": "^3.3.3" "prettier": "^3.3.3"
......
import {Logger} from './utils/utils'; import { Logger as internalLogger } from './utils/utils';
const QUERY_DEVICE_INFO = 0x01; const QUERY_DEVICE_INFO = 0x01;
const QUERY_DEVICE_TIME = 0x02; const QUERY_DEVICE_TIME = 0x02;
...@@ -47,7 +47,8 @@ const COMMAND_NAMES = [ ...@@ -47,7 +47,8 @@ const COMMAND_NAMES = [
'factory reset', 'factory reset',
]; ];
function Jensen() { function Jensen(log) {
const Logger = log || internalLogger
let device = null; let device = null;
let actions = {}; let actions = {};
let buffer = []; let buffer = [];
...@@ -124,7 +125,7 @@ function Jensen() { ...@@ -124,7 +125,7 @@ function Jensen() {
if (r) return; if (r) return;
let conn = await navigator.usb.requestDevice({ let conn = await navigator.usb.requestDevice({
filters: [{vendorId: 0x10d6}], filters: [{ vendorId: 0x10d6 }],
}); });
await conn.open(); await conn.open();
self.model = conn.productId == 45069 ? 'hidock-h1e' : 'hidock-h1'; self.model = conn.productId == 45069 ? 'hidock-h1e' : 'hidock-h1';
...@@ -455,7 +456,7 @@ function Jensen() { ...@@ -455,7 +456,7 @@ function Jensen() {
// for (let i = 0; i < padding; i++) this.buffer[i + cutLen]; // for (let i = 0; i < padding; i++) this.buffer[i + cutLen];
cutLen += padding; cutLen += padding;
// self.buffer = self.buffer.slice(cutLen); // 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) { this.to_bcd = function (str) {
...@@ -756,25 +757,25 @@ Jensen.prototype.endBNC = async function (seconds) { ...@@ -756,25 +757,25 @@ Jensen.prototype.endBNC = async function (seconds) {
Jensen.prototype.getSettings = async function (seconds) { Jensen.prototype.getSettings = async function (seconds) {
if (this.versionNumber < 327714) { if (this.versionNumber < 327714) {
return {autoRecord: false, autoPlay: false}; return { autoRecord: false, autoPlay: false };
} }
return this.send(new Command(GET_SETTINGS), seconds); return this.send(new Command(GET_SETTINGS), seconds);
}; };
Jensen.prototype.setAutoRecord = function (enable, 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); return this.send(new Command(SET_SETTINGS).body([0, 0, 0, enable ? 1 : 2]), seconds);
}; };
Jensen.prototype.setAutoPlay = function (enable, 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); 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) { 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); 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) { Jensen.prototype.setBluetoothPromptPlay = function (enable, seconds) {
if (this.model === 'hidock-h1e' && this.versionNumber < 393218) return {result: false} if (this.model === 'hidock-h1e' && this.versionNumber < 393218) return { result: false }
if (this.model === 'hidock-h1' && this.versionNumber < 327732) 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); 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);
} }
...@@ -822,7 +823,7 @@ Jensen.prototype.getFileBlock = async function (filename, length, ondata) { ...@@ -822,7 +823,7 @@ Jensen.prototype.getFileBlock = async function (filename, length, ondata) {
} }
const commonMessageParser = (msg) => { 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(SET_DEVICE_TIME, commonMessageParser);
Jensen.registerHandler(BNC_DEMO_TEST, commonMessageParser); Jensen.registerHandler(BNC_DEMO_TEST, commonMessageParser);
...@@ -831,7 +832,7 @@ Jensen.registerHandler(DELETE_FILE, (msg) => { ...@@ -831,7 +832,7 @@ Jensen.registerHandler(DELETE_FILE, (msg) => {
if (msg.body[0] === 0x00) rst = 'success'; if (msg.body[0] === 0x00) rst = 'success';
else if (msg.body[0] === 0x01) rst = 'not-exists'; else if (msg.body[0] === 0x01) rst = 'not-exists';
else if (msg.body[0] === 0x02) rst = 'failed'; else if (msg.body[0] === 0x02) rst = 'failed';
return {result: rst}; return { result: rst };
}); });
Jensen.registerHandler(QUERY_DEVICE_INFO, (msg, jensen) => { Jensen.registerHandler(QUERY_DEVICE_INFO, (msg, jensen) => {
let vc = [], let vc = [],
...@@ -873,19 +874,19 @@ Jensen.registerHandler(QUERY_DEVICE_TIME, (msg, jensen) => { ...@@ -873,19 +874,19 @@ Jensen.registerHandler(QUERY_DEVICE_TIME, (msg, jensen) => {
}; };
}); });
Jensen.registerHandler(QUERY_FILE_COUNT, (msg) => { 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; let c = 0;
for (let i = 0; i < 4; i++) { for (let i = 0; i < 4; i++) {
let b = msg.body[i] & 0xff; let b = msg.body[i] & 0xff;
c = c | (b << ((4 - i - 1) * 8)); c = c | (b << ((4 - i - 1) * 8));
} }
return {count: c}; return { count: c };
}); });
Jensen.registerHandler(GET_SETTINGS, (msg) => { Jensen.registerHandler(GET_SETTINGS, (msg) => {
let r1 = msg.body[3]; let r1 = msg.body[3];
let r2 = msg.body[7]; let r2 = msg.body[7];
let r4 = msg.body[15]; 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) { if (msg.body.length >= 12) {
let r3 = msg.body[11] === 1; let r3 = msg.body[11] === 1;
rst['notification'] = r3; rst['notification'] = r3;
...@@ -902,11 +903,11 @@ Jensen.registerHandler(REQUEST_FIRMWARE_UPGRADE, (msg) => { ...@@ -902,11 +903,11 @@ Jensen.registerHandler(REQUEST_FIRMWARE_UPGRADE, (msg) => {
if (c === 0x01) rst = 'wrong-version'; if (c === 0x01) rst = 'wrong-version';
if (c === 0x02) rst = 'busy'; if (c === 0x02) rst = 'busy';
if (c === 0x03) return 'unknown'; if (c === 0x03) return 'unknown';
return {result: rst}; return { result: rst };
}); });
Jensen.registerHandler(FIRMWARE_UPLOAD, (msg) => { Jensen.registerHandler(FIRMWARE_UPLOAD, (msg) => {
let c = msg.body[0]; let c = msg.body[0];
return {result: c === 0x00 ? 'success' : 'failed'}; return { result: c === 0x00 ? 'success' : 'failed' };
}); });
Jensen.registerHandler(READ_CARD_INFO, (msg) => { Jensen.registerHandler(READ_CARD_INFO, (msg) => {
let i = 0; let i = 0;
...@@ -914,11 +915,11 @@ Jensen.registerHandler(READ_CARD_INFO, (msg) => { ...@@ -914,11 +915,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 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); 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(FORMAT_CARD, commonMessageParser);
Jensen.registerHandler(GET_RECORDING_FILE, (msg) => { 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 { else {
let fname = []; let fname = [];
for (var i = 0; i < msg.body.length; i++) { for (var i = 0; i < msg.body.length; i++) {
...@@ -961,4 +962,4 @@ Jensen.registerHandler(RECORD_TEST_END, commonMessageParser); ...@@ -961,4 +962,4 @@ Jensen.registerHandler(RECORD_TEST_END, commonMessageParser);
Jensen.registerHandler(DEVICE_MSG_TEST, commonMessageParser); Jensen.registerHandler(DEVICE_MSG_TEST, commonMessageParser);
Jensen.registerHandler(GET_FILE_BLOCK, 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