@onekeyfe/hd-transport-lowlevel 1.2.0-alpha.16 → 1.2.0-alpha.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/firmware-upload-progress.test.js +42 -0
- package/dist/index.d.ts +10 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +43 -3
- package/package.json +4 -4
- package/src/index.ts +57 -2
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
|
+
const { getProtocolV1SendOptions, shouldLogFirmwareUploadProgress } = require('../src');
|
|
3
|
+
|
|
4
|
+
describe('firmware upload progress logging', () => {
|
|
5
|
+
test('按 5% 进度限流打印', () => {
|
|
6
|
+
expect(
|
|
7
|
+
shouldLogFirmwareUploadProgress({
|
|
8
|
+
percent: 9,
|
|
9
|
+
lastLoggedPercent: 5,
|
|
10
|
+
now: 5_000,
|
|
11
|
+
lastLoggedAt: 0,
|
|
12
|
+
})
|
|
13
|
+
).toBe(false);
|
|
14
|
+
|
|
15
|
+
expect(
|
|
16
|
+
shouldLogFirmwareUploadProgress({
|
|
17
|
+
percent: 10,
|
|
18
|
+
lastLoggedPercent: 5,
|
|
19
|
+
now: 5_000,
|
|
20
|
+
lastLoggedAt: 0,
|
|
21
|
+
})
|
|
22
|
+
).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('进度不足 5% 时最长每 10 秒打印一次心跳', () => {
|
|
26
|
+
expect(
|
|
27
|
+
shouldLogFirmwareUploadProgress({
|
|
28
|
+
percent: 7,
|
|
29
|
+
lastLoggedPercent: 5,
|
|
30
|
+
now: 10_000,
|
|
31
|
+
lastLoggedAt: 0,
|
|
32
|
+
})
|
|
33
|
+
).toBe(true);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('firmware upload write mode', () => {
|
|
38
|
+
test('固件上传使用带响应写入,普通命令保持默认模式', () => {
|
|
39
|
+
expect(getProtocolV1SendOptions('FirmwareUpload')).toEqual({ withoutResponse: false });
|
|
40
|
+
expect(getProtocolV1SendOptions('Initialize')).toBeUndefined();
|
|
41
|
+
});
|
|
42
|
+
});
|
package/dist/index.d.ts
CHANGED
|
@@ -7,6 +7,15 @@ type LowLevelAcquireInput = {
|
|
|
7
7
|
expectedProtocol?: ProtocolType;
|
|
8
8
|
};
|
|
9
9
|
|
|
10
|
+
declare function shouldLogFirmwareUploadProgress({ percent, lastLoggedPercent, now, lastLoggedAt, }: {
|
|
11
|
+
percent: number;
|
|
12
|
+
lastLoggedPercent: number;
|
|
13
|
+
now: number;
|
|
14
|
+
lastLoggedAt: number;
|
|
15
|
+
}): boolean;
|
|
16
|
+
declare function getProtocolV1SendOptions(name: string): {
|
|
17
|
+
withoutResponse: boolean;
|
|
18
|
+
} | undefined;
|
|
10
19
|
declare class LowlevelTransport {
|
|
11
20
|
_messages: ReturnType<typeof transport__default.parseConfigure> | undefined;
|
|
12
21
|
_messagesV2: ReturnType<typeof transport__default.parseConfigure> | undefined;
|
|
@@ -51,4 +60,4 @@ declare class LowlevelTransport {
|
|
|
51
60
|
cancel(): void;
|
|
52
61
|
}
|
|
53
62
|
|
|
54
|
-
export { LowlevelTransport as default };
|
|
63
|
+
export { LowlevelTransport as default, getProtocolV1SendOptions, shouldLogFirmwareUploadProgress };
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA,OAAO,SAWN,MAAM,wBAAwB,CAAC;AAEhC,OAAO,KAAK,YAAY,MAAM,QAAQ,CAAC;AACvC,OAAO,KAAK,EACV,cAAc,EACd,6BAA6B,EAC7B,YAAY,EACZ,oBAAoB,EACrB,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.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IAO9E,SAAS,CAAC,UAAU,EAAE,GAAG;IAMzB,mBAAmB,CAAC,UAAU,EAAE,GAAG;IAOnC,MAAM;IAIA,SAAS;IAWT,OAAO,CAAC,KAAK,EAAE,oBAAoB;;;;IA6BnC,OAAO,CAAC,IAAI,EAAE,MAAM;IAgBpB,IAAI,CACR,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,OAAO,CAAC,EAAE,oBAAoB;YAsBlB,cAAc;IAmE5B,OAAO,CAAC,0BAA0B;IAOlC,OAAO,CAAC,2BAA2B;IAOnC,OAAO,CAAC,4BAA4B;IAOpC,OAAO,CAAC,kBAAkB;YAMZ,cAAc;YAsDd,yBAAyB;YAiCzB,eAAe;YAgBf,eAAe;YA6Bf,UAAU;YAUV,qBAAqB;YAmBrB,mBAAmB;YAqBnB,oBAAoB;YAOpB,cAAc;IA6B5B,OAAO,CAAC,uBAAuB;IAkC/B,OAAO,CAAC,2BAA2B;IAMnC,MAAM;CAGP"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
3
5
|
var hdShared = require('@onekeyfe/hd-shared');
|
|
4
6
|
var transport = require('@onekeyfe/hd-transport');
|
|
5
7
|
|
|
@@ -42,6 +44,16 @@ const PROTOCOL_PROBE_TIMEOUT_MS = 1000;
|
|
|
42
44
|
const PROTOCOL_V2_PROBE_TIMEOUT_MS = 5000;
|
|
43
45
|
const LOWLEVEL_PROTOCOL_TIMEOUT_MS = 30000;
|
|
44
46
|
const LOWLEVEL_PROTOCOL_V2_PACKET_LENGTH = 64;
|
|
47
|
+
const FIRMWARE_UPLOAD_LOG_PERCENT_STEP = 5;
|
|
48
|
+
const FIRMWARE_UPLOAD_LOG_INTERVAL_MS = 10000;
|
|
49
|
+
function shouldLogFirmwareUploadProgress({ percent, lastLoggedPercent, now, lastLoggedAt, }) {
|
|
50
|
+
return (percent === 100 ||
|
|
51
|
+
percent - lastLoggedPercent >= FIRMWARE_UPLOAD_LOG_PERCENT_STEP ||
|
|
52
|
+
now - lastLoggedAt >= FIRMWARE_UPLOAD_LOG_INTERVAL_MS);
|
|
53
|
+
}
|
|
54
|
+
function getProtocolV1SendOptions(name) {
|
|
55
|
+
return name === 'FirmwareUpload' ? { withoutResponse: false } : undefined;
|
|
56
|
+
}
|
|
45
57
|
function inferProtocolHintFromDeviceName(name) {
|
|
46
58
|
return /\bpro\s*2\b/i.test(name !== null && name !== void 0 ? name : '') ? 'V2' : undefined;
|
|
47
59
|
}
|
|
@@ -182,16 +194,42 @@ class LowlevelTransport {
|
|
|
182
194
|
});
|
|
183
195
|
}
|
|
184
196
|
callProtocolV1(uuid, name, data, options) {
|
|
197
|
+
var _a;
|
|
185
198
|
return __awaiter(this, void 0, void 0, function* () {
|
|
186
199
|
if (!this._messages) {
|
|
187
200
|
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportNotConfigured);
|
|
188
201
|
}
|
|
189
202
|
const messages = this._messages;
|
|
190
203
|
const buffers = ProtocolV1.encodeTransportPackets(messages, name, data);
|
|
191
|
-
|
|
204
|
+
const isFirmwareUpload = name === 'FirmwareUpload';
|
|
205
|
+
const uploadStartedAt = Date.now();
|
|
206
|
+
const totalBytes = buffers.reduce((sum, buffer) => sum + buffer.limit, 0);
|
|
207
|
+
let sentBytes = 0;
|
|
208
|
+
let lastLoggedPercent = 0;
|
|
209
|
+
let lastLoggedAt = uploadStartedAt;
|
|
210
|
+
for (const [index, o] of buffers.entries()) {
|
|
192
211
|
const outData = o.toString('hex');
|
|
193
212
|
try {
|
|
194
|
-
yield this.plugin.send(uuid, outData);
|
|
213
|
+
yield this.plugin.send(uuid, outData, getProtocolV1SendOptions(name));
|
|
214
|
+
sentBytes += o.limit;
|
|
215
|
+
if (isFirmwareUpload) {
|
|
216
|
+
const now = Date.now();
|
|
217
|
+
const percent = Math.floor(((index + 1) / buffers.length) * 100);
|
|
218
|
+
if (shouldLogFirmwareUploadProgress({
|
|
219
|
+
percent,
|
|
220
|
+
lastLoggedPercent,
|
|
221
|
+
now,
|
|
222
|
+
lastLoggedAt,
|
|
223
|
+
})) {
|
|
224
|
+
const elapsedSeconds = Math.max((now - uploadStartedAt) / 1000, 0.001);
|
|
225
|
+
const kibPerSecond = sentBytes / 1024 / elapsedSeconds;
|
|
226
|
+
(_a = this.Log) === null || _a === void 0 ? void 0 : _a.debug(`[LowlevelTransport] FirmwareUpload progress: ${percent}% ` +
|
|
227
|
+
`(${index + 1}/${buffers.length} packets, ${sentBytes}/${totalBytes} bytes, ` +
|
|
228
|
+
`${elapsedSeconds.toFixed(1)}s, ${kibPerSecond.toFixed(1)} KiB/s)`);
|
|
229
|
+
lastLoggedPercent = percent;
|
|
230
|
+
lastLoggedAt = now;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
195
233
|
}
|
|
196
234
|
catch (e) {
|
|
197
235
|
this.Log.debug('lowlevel transport send error: ', e);
|
|
@@ -459,4 +497,6 @@ class LowlevelTransport {
|
|
|
459
497
|
}
|
|
460
498
|
}
|
|
461
499
|
|
|
462
|
-
|
|
500
|
+
exports["default"] = LowlevelTransport;
|
|
501
|
+
exports.getProtocolV1SendOptions = getProtocolV1SendOptions;
|
|
502
|
+
exports.shouldLogFirmwareUploadProgress = shouldLogFirmwareUploadProgress;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-lowlevel",
|
|
3
|
-
"version": "1.2.0-alpha.
|
|
3
|
+
"version": "1.2.0-alpha.17",
|
|
4
4
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"lint:fix": "eslint . --fix"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@onekeyfe/hd-shared": "1.2.0-alpha.
|
|
24
|
-
"@onekeyfe/hd-transport": "1.2.0-alpha.
|
|
23
|
+
"@onekeyfe/hd-shared": "1.2.0-alpha.17",
|
|
24
|
+
"@onekeyfe/hd-transport": "1.2.0-alpha.17"
|
|
25
25
|
},
|
|
26
|
-
"gitHead": "
|
|
26
|
+
"gitHead": "9027b743e4f73beb42e1a7950397504b4191f2a3"
|
|
27
27
|
}
|
package/src/index.ts
CHANGED
|
@@ -27,6 +27,30 @@ const PROTOCOL_PROBE_TIMEOUT_MS = 1000;
|
|
|
27
27
|
const PROTOCOL_V2_PROBE_TIMEOUT_MS = 5000;
|
|
28
28
|
const LOWLEVEL_PROTOCOL_TIMEOUT_MS = 30_000;
|
|
29
29
|
const LOWLEVEL_PROTOCOL_V2_PACKET_LENGTH = 64;
|
|
30
|
+
const FIRMWARE_UPLOAD_LOG_PERCENT_STEP = 5;
|
|
31
|
+
const FIRMWARE_UPLOAD_LOG_INTERVAL_MS = 10_000;
|
|
32
|
+
|
|
33
|
+
export function shouldLogFirmwareUploadProgress({
|
|
34
|
+
percent,
|
|
35
|
+
lastLoggedPercent,
|
|
36
|
+
now,
|
|
37
|
+
lastLoggedAt,
|
|
38
|
+
}: {
|
|
39
|
+
percent: number;
|
|
40
|
+
lastLoggedPercent: number;
|
|
41
|
+
now: number;
|
|
42
|
+
lastLoggedAt: number;
|
|
43
|
+
}) {
|
|
44
|
+
return (
|
|
45
|
+
percent === 100 ||
|
|
46
|
+
percent - lastLoggedPercent >= FIRMWARE_UPLOAD_LOG_PERCENT_STEP ||
|
|
47
|
+
now - lastLoggedAt >= FIRMWARE_UPLOAD_LOG_INTERVAL_MS
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function getProtocolV1SendOptions(name: string) {
|
|
52
|
+
return name === 'FirmwareUpload' ? { withoutResponse: false } : undefined;
|
|
53
|
+
}
|
|
30
54
|
|
|
31
55
|
function inferProtocolHintFromDeviceName(name?: string | null): ProtocolType | undefined {
|
|
32
56
|
return /\bpro\s*2\b/i.test(name ?? '') ? 'V2' : undefined;
|
|
@@ -216,10 +240,41 @@ export default class LowlevelTransport {
|
|
|
216
240
|
|
|
217
241
|
const messages = this._messages;
|
|
218
242
|
const buffers = ProtocolV1.encodeTransportPackets(messages, name, data);
|
|
219
|
-
|
|
243
|
+
const isFirmwareUpload = name === 'FirmwareUpload';
|
|
244
|
+
const uploadStartedAt = Date.now();
|
|
245
|
+
const totalBytes = buffers.reduce((sum, buffer) => sum + buffer.limit, 0);
|
|
246
|
+
let sentBytes = 0;
|
|
247
|
+
let lastLoggedPercent = 0;
|
|
248
|
+
let lastLoggedAt = uploadStartedAt;
|
|
249
|
+
|
|
250
|
+
for (const [index, o] of buffers.entries()) {
|
|
220
251
|
const outData = o.toString('hex');
|
|
221
252
|
try {
|
|
222
|
-
await this.plugin.send(uuid, outData);
|
|
253
|
+
await this.plugin.send(uuid, outData, getProtocolV1SendOptions(name));
|
|
254
|
+
sentBytes += o.limit;
|
|
255
|
+
|
|
256
|
+
if (isFirmwareUpload) {
|
|
257
|
+
const now = Date.now();
|
|
258
|
+
const percent = Math.floor(((index + 1) / buffers.length) * 100);
|
|
259
|
+
if (
|
|
260
|
+
shouldLogFirmwareUploadProgress({
|
|
261
|
+
percent,
|
|
262
|
+
lastLoggedPercent,
|
|
263
|
+
now,
|
|
264
|
+
lastLoggedAt,
|
|
265
|
+
})
|
|
266
|
+
) {
|
|
267
|
+
const elapsedSeconds = Math.max((now - uploadStartedAt) / 1000, 0.001);
|
|
268
|
+
const kibPerSecond = sentBytes / 1024 / elapsedSeconds;
|
|
269
|
+
this.Log?.debug(
|
|
270
|
+
`[LowlevelTransport] FirmwareUpload progress: ${percent}% ` +
|
|
271
|
+
`(${index + 1}/${buffers.length} packets, ${sentBytes}/${totalBytes} bytes, ` +
|
|
272
|
+
`${elapsedSeconds.toFixed(1)}s, ${kibPerSecond.toFixed(1)} KiB/s)`
|
|
273
|
+
);
|
|
274
|
+
lastLoggedPercent = percent;
|
|
275
|
+
lastLoggedAt = now;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
223
278
|
} catch (e) {
|
|
224
279
|
this.Log.debug('lowlevel transport send error: ', e);
|
|
225
280
|
throw ERRORS.TypedError(HardwareErrorCode.BleWriteCharacteristicError, e.reason);
|