@onekeyfe/hd-core 0.1.0 → 0.1.3

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.
Files changed (46) hide show
  1. package/dist/api/CheckBLEFirmwareRelease.d.ts +8 -1
  2. package/dist/api/CheckBLEFirmwareRelease.d.ts.map +1 -1
  3. package/dist/api/CheckFirmwareRelease.d.ts +8 -1
  4. package/dist/api/CheckFirmwareRelease.d.ts.map +1 -1
  5. package/dist/constants/errors.d.ts +1 -0
  6. package/dist/constants/errors.d.ts.map +1 -1
  7. package/dist/core/index.d.ts +1 -0
  8. package/dist/core/index.d.ts.map +1 -1
  9. package/dist/data-manager/DataManager.d.ts +10 -0
  10. package/dist/data-manager/DataManager.d.ts.map +1 -1
  11. package/dist/device/Device.d.ts +2 -1
  12. package/dist/device/Device.d.ts.map +1 -1
  13. package/dist/device/DeviceCommands.d.ts.map +1 -1
  14. package/dist/device/DeviceList.d.ts.map +1 -1
  15. package/dist/events/call.d.ts +7 -0
  16. package/dist/events/call.d.ts.map +1 -1
  17. package/dist/events/core.d.ts +2 -2
  18. package/dist/events/core.d.ts.map +1 -1
  19. package/dist/events/iframe.d.ts +1 -0
  20. package/dist/events/iframe.d.ts.map +1 -1
  21. package/dist/index.d.ts +42 -6
  22. package/dist/index.js +143 -47
  23. package/dist/types/api/checkBLEFirmwareRelease.d.ts +12 -2
  24. package/dist/types/api/checkBLEFirmwareRelease.d.ts.map +1 -1
  25. package/dist/types/api/checkFirmwareRelease.d.ts +11 -1
  26. package/dist/types/api/checkFirmwareRelease.d.ts.map +1 -1
  27. package/dist/types/api/index.d.ts +1 -1
  28. package/dist/types/api/index.d.ts.map +1 -1
  29. package/dist/utils/release.d.ts +4 -0
  30. package/dist/utils/release.d.ts.map +1 -0
  31. package/package.json +3 -3
  32. package/src/api/CheckBLEFirmwareRelease.ts +7 -1
  33. package/src/api/CheckFirmwareRelease.ts +7 -1
  34. package/src/constants/errors.ts +1 -0
  35. package/src/core/index.ts +28 -1
  36. package/src/data-manager/DataManager.ts +36 -20
  37. package/src/device/Device.ts +18 -11
  38. package/src/device/DeviceCommands.ts +4 -0
  39. package/src/device/DeviceList.ts +13 -1
  40. package/src/events/call.ts +6 -0
  41. package/src/events/core.ts +2 -1
  42. package/src/events/iframe.ts +1 -0
  43. package/src/types/api/checkBLEFirmwareRelease.ts +12 -4
  44. package/src/types/api/checkFirmwareRelease.ts +11 -1
  45. package/src/types/api/index.ts +1 -1
  46. package/src/utils/release.ts +24 -0
@@ -18,6 +18,7 @@ import type {
18
18
  IDeviceBLEFirmwareStatus,
19
19
  ITransportStatus,
20
20
  } from '../types';
21
+ import { getReleaseChangelog, getReleaseStatus } from '../utils/release';
21
22
 
22
23
  export default class DataManager {
23
24
  static deviceMap: DeviceTypeMap = {
@@ -59,21 +60,30 @@ export default class DataManager {
59
60
  }
60
61
 
61
62
  const targetDeviceConfigList = this.deviceMap[deviceType]?.firmware ?? [];
62
- const latestFirmware = targetDeviceConfigList[targetDeviceConfigList.length - 1];
63
-
64
- if (!latestFirmware) return 'valid';
65
-
66
- const latestVersion = latestFirmware.version.join('.');
67
63
  const currentVersion = deviceFirmwareVersion.join('.');
64
+ return getReleaseStatus(targetDeviceConfigList, currentVersion);
65
+ };
68
66
 
69
- /** latest is greater or equal current */
70
- if (semver.gt(latestVersion, currentVersion)) {
71
- if (latestFirmware.required) return 'required';
67
+ static getFirmwareChangelog = (features: Features) => {
68
+ const deviceType = getDeviceType(features);
69
+ const deviceFirmwareVersion = getDeviceFirmwareVersion(features);
72
70
 
73
- return 'outdated';
71
+ if (
72
+ features.firmware_present === false ||
73
+ (deviceType === 'classic' && features.bootloader_mode)
74
+ ) {
75
+ return [];
74
76
  }
75
77
 
76
- return 'valid';
78
+ const targetDeviceConfigList = this.deviceMap[deviceType]?.firmware ?? [];
79
+ const currentVersion = deviceFirmwareVersion.join('.');
80
+ return getReleaseChangelog(targetDeviceConfigList, currentVersion);
81
+ };
82
+
83
+ static getFirmwareLeatestRelease = (features: Features) => {
84
+ const deviceType = getDeviceType(features);
85
+ const targetDeviceConfigList = this.deviceMap[deviceType]?.firmware ?? [];
86
+ return targetDeviceConfigList[targetDeviceConfigList.length - 1];
77
87
  };
78
88
 
79
89
  static getBLEFirmwareStatus = (features: Features): IDeviceBLEFirmwareStatus => {
@@ -86,21 +96,27 @@ export default class DataManager {
86
96
  }
87
97
 
88
98
  const targetDeviceConfigList = this.deviceMap[deviceType]?.ble ?? [];
89
- const latestBLEFirmware = targetDeviceConfigList[targetDeviceConfigList.length - 1];
90
-
91
- if (!latestBLEFirmware) return 'valid';
92
-
93
- const latestVersion = latestBLEFirmware.version.join('.');
94
99
  const currentVersion = deviceBLEFirmwareVersion.join('.');
100
+ return getReleaseStatus(targetDeviceConfigList, currentVersion);
101
+ };
95
102
 
96
- /** latest is greater or equal current */
97
- if (semver.gt(latestVersion, currentVersion)) {
98
- if (latestBLEFirmware.required) return 'required';
103
+ static getBleFirmwareChangelog = (features: Features) => {
104
+ const deviceType = getDeviceType(features);
105
+ const deviceBLEFirmwareVersion = getDeviceBLEFirmwareVersion(features);
99
106
 
100
- return 'outdated';
107
+ if (!deviceBLEFirmwareVersion) {
108
+ return [];
101
109
  }
102
110
 
103
- return 'valid';
111
+ const targetDeviceConfigList = this.deviceMap[deviceType]?.ble ?? [];
112
+ const currentVersion = deviceBLEFirmwareVersion.join('.');
113
+ return getReleaseChangelog(targetDeviceConfigList, currentVersion);
114
+ };
115
+
116
+ static getBleFirmwareLeatestRelease = (features: Features) => {
117
+ const deviceType = getDeviceType(features);
118
+ const targetDeviceConfigList = this.deviceMap[deviceType]?.ble ?? [];
119
+ return targetDeviceConfigList[targetDeviceConfigList.length - 1];
104
120
  };
105
121
 
106
122
  static getTransportStatus = (localVersion: string): ITransportStatus => {
@@ -85,8 +85,6 @@ export class Device extends EventEmitter {
85
85
 
86
86
  internalState: string[] = [];
87
87
 
88
- loaded = false;
89
-
90
88
  needReloadDevice = false;
91
89
 
92
90
  /**
@@ -281,10 +279,19 @@ export class Device extends EventEmitter {
281
279
  }
282
280
  }
283
281
 
282
+ updateFromCache(device: Device) {
283
+ this.mainId = device.mainId;
284
+ this.commands = device.commands;
285
+ this.updateDescriptor(device.originalDescriptor);
286
+ if (device.features) {
287
+ this._updateFeatures(device.features);
288
+ }
289
+ }
290
+
284
291
  async run(fn?: () => Promise<void>, options?: RunOptions) {
285
292
  if (this.runPromise) {
286
- Log.error('[Device] run error:', 'Device is running');
287
- throw ERRORS.TypedError('Device_CallInProgress');
293
+ this.interruption();
294
+ Log.debug('[Device] run error:', 'Device is running, but will cancel previous operate');
288
295
  }
289
296
 
290
297
  options = parseRunOptions(options);
@@ -319,11 +326,6 @@ export class Device extends EventEmitter {
319
326
  await fn();
320
327
  }
321
328
 
322
- // reload features
323
- if (this.loaded && this.features) {
324
- await this.getFeatures();
325
- }
326
-
327
329
  if (
328
330
  (!this.keepSession && typeof options.keepSession !== 'boolean') ||
329
331
  options.keepSession === false
@@ -338,9 +340,14 @@ export class Device extends EventEmitter {
338
340
  }
339
341
 
340
342
  this.runPromise = null;
343
+ }
341
344
 
342
- if (!this.loaded) {
343
- this.loaded = true;
345
+ interruption() {
346
+ if (this.commands) {
347
+ this.commands.dispose();
348
+ }
349
+ if (this.runPromise) {
350
+ this.runPromise.reject(ERRORS.TypedError('Device_Interrupted'));
344
351
  }
345
352
  }
346
353
 
@@ -56,7 +56,11 @@ export class DeviceCommands {
56
56
 
57
57
  dispose() {
58
58
  this.disposed = true;
59
+ if (this._cancelableRequest) {
60
+ this._cancelableRequest();
61
+ }
59
62
  this._cancelableRequest = undefined;
63
+ this.transport.cancel?.();
60
64
  }
61
65
 
62
66
  // Sends an async message to the opened device.
@@ -2,6 +2,11 @@ import EventEmitter from 'events';
2
2
  import DeviceConnector from './DeviceConnector';
3
3
  import { Device } from './Device';
4
4
  import { getDeviceUUID } from '../utils/deviceFeaturesUtils';
5
+ import { initLog } from '../utils';
6
+
7
+ const cacheDeviceMap = new Map<string, Device>();
8
+
9
+ const Log = initLog('DeviceList');
5
10
 
6
11
  export class DeviceList extends EventEmitter {
7
12
  devices: Record<string, Device> = {};
@@ -20,7 +25,7 @@ export class DeviceList extends EventEmitter {
20
25
  const deviceList = [];
21
26
  console.log('get device list');
22
27
  for await (const descriptor of descriptorList) {
23
- const device = Device.fromDescriptor(descriptor);
28
+ let device = Device.fromDescriptor(descriptor);
24
29
  device.deviceConnector = this.connector;
25
30
  await device.connect();
26
31
  await device.initialize();
@@ -30,7 +35,14 @@ export class DeviceList extends EventEmitter {
30
35
  deviceList.push(device);
31
36
  if (device.features) {
32
37
  const uuid = getDeviceUUID(device.features);
38
+ if (cacheDeviceMap.has(uuid)) {
39
+ const cache = cacheDeviceMap.get(uuid);
40
+ cache?.updateFromCache(device);
41
+ device = cache as Device;
42
+ Log.debug('use cache device: ', uuid);
43
+ }
33
44
  this.devices[uuid] = device;
45
+ cacheDeviceMap.set(uuid, device);
34
46
  }
35
47
  }
36
48
 
@@ -46,6 +46,12 @@ export interface IFrameCallMessage {
46
46
  payload: CallMethodPayload;
47
47
  }
48
48
 
49
+ export interface IFrameCancelMessage {
50
+ event: typeof IFRAME.CANCEL;
51
+ type: typeof IFRAME.CANCEL;
52
+ payload: { connectId?: string };
53
+ }
54
+
49
55
  export const RESPONSE_EVENT = 'RESPONSE_EVENT';
50
56
 
51
57
  export interface MethodResponseMessage {
@@ -1,5 +1,5 @@
1
1
  import { Unsuccessful } from '../types/params';
2
- import { IFrameCallMessage } from './call';
2
+ import { IFrameCallMessage, IFrameCancelMessage } from './call';
3
3
  import { DeviceEventMessage } from './device';
4
4
  import { IFrameEventMessage } from './iframe';
5
5
  import { UiEventMessage } from './ui-request';
@@ -13,6 +13,7 @@ export type CoreMessage = {
13
13
  } & (
14
14
  | IFrameEventMessage
15
15
  | IFrameCallMessage
16
+ | IFrameCancelMessage
16
17
  | UiResponseMessage
17
18
  | UiEventMessage
18
19
  | DeviceEventMessage
@@ -6,6 +6,7 @@ export const IFRAME = {
6
6
  INIT: 'iframe-init',
7
7
  INIT_BRIDGE: 'iframe-init-bridge',
8
8
  CALL: 'iframe-call',
9
+ CANCEL: 'iframe-cancel',
9
10
  } as const;
10
11
 
11
12
  export interface IFrameInit {
@@ -1,6 +1,14 @@
1
1
  import type { Response } from '../params';
2
- import type { IDeviceFirmwareStatus } from '../device';
2
+ import type { IDeviceBLEFirmwareStatus } from '../device';
3
+ import { IBLEFirmwareReleaseInfo } from '../settings';
3
4
 
4
- export declare function checkBLEFirmwareRelease(
5
- connectId?: string
6
- ): Response<IDeviceFirmwareStatus>;
5
+ type BleFirmwareRelease = {
6
+ status: IDeviceBLEFirmwareStatus;
7
+ changelog: {
8
+ 'zh-CN': string;
9
+ 'en-US': string;
10
+ }[];
11
+ release: IBLEFirmwareReleaseInfo;
12
+ };
13
+
14
+ export declare function checkBLEFirmwareRelease(connectId?: string): Response<BleFirmwareRelease>;
@@ -1,4 +1,14 @@
1
1
  import type { Response } from '../params';
2
2
  import type { IDeviceFirmwareStatus } from '../device';
3
+ import { IFirmwareReleaseInfo } from '../settings';
3
4
 
4
- export declare function checkFirmwareRelease(connectId?: string): Response<IDeviceFirmwareStatus>;
5
+ type FirmwareRelease = {
6
+ status: IDeviceFirmwareStatus;
7
+ changelog: {
8
+ 'en-US': string;
9
+ 'zh-CN': string;
10
+ }[];
11
+ release: IFirmwareReleaseInfo;
12
+ };
13
+
14
+ export declare function checkFirmwareRelease(connectId?: string): Response<FirmwareRelease>;
@@ -54,7 +54,7 @@ export type CoreApi = {
54
54
  dispose: () => void;
55
55
  call: (params: any) => Promise<any>;
56
56
  uiResponse: typeof uiResponse;
57
- cancel: (params?: string) => void;
57
+ cancel: (connectId?: string) => void;
58
58
 
59
59
  /**
60
60
  * Core function
@@ -0,0 +1,24 @@
1
+ import semver from 'semver';
2
+ import { IBLEFirmwareReleaseInfo, IDeviceFirmwareStatus, IFirmwareReleaseInfo } from '../types';
3
+
4
+ export const getReleaseStatus = (
5
+ releases: (IFirmwareReleaseInfo | IBLEFirmwareReleaseInfo)[],
6
+ currentVersion: string
7
+ ): IDeviceFirmwareStatus => {
8
+ const newVersions = releases.filter(r => semver.gt(r.version.join('.'), currentVersion));
9
+ if (newVersions.length === 0) {
10
+ return 'valid';
11
+ }
12
+ if (newVersions.some(r => r.required)) {
13
+ return 'required';
14
+ }
15
+ return 'outdated';
16
+ };
17
+
18
+ export const getReleaseChangelog = (
19
+ releases: (IFirmwareReleaseInfo | IBLEFirmwareReleaseInfo)[],
20
+ currentVersion: string
21
+ ): IFirmwareReleaseInfo['changelog'][] => {
22
+ const newVersions = releases.filter(r => semver.gt(r.version.join('.'), currentVersion));
23
+ return newVersions.map(r => r.changelog);
24
+ };