@aiot-toolkit/emulator 2.0.6-beta.25 → 2.0.6-beta.28

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.
@@ -0,0 +1,21 @@
1
+ import Aedes from 'aedes';
2
+ import { TlsOptions } from 'tls';
3
+ import { IncomingMessage, ServerResponse, Server as HttpServer } from 'http';
4
+ export interface BrokerOptions {
5
+ preferredMqttPort?: number;
6
+ preferredHttpPort?: number;
7
+ tlsDir?: string;
8
+ host?: string;
9
+ onHttpRequest?: (req: IncomingMessage, res: ServerResponse) => boolean;
10
+ }
11
+ export interface BrokerInstance {
12
+ aedes: Aedes;
13
+ mqttPort: number;
14
+ httpPort: number;
15
+ mqttHost: string;
16
+ tlsOptions: TlsOptions;
17
+ httpServer: HttpServer;
18
+ close(): Promise<void>;
19
+ }
20
+ export declare function getLocalIP(): string;
21
+ export declare function startBroker(options?: BrokerOptions): Promise<BrokerInstance>;
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getLocalIP = getLocalIP;
7
+ exports.startBroker = startBroker;
8
+ var _aedes = _interopRequireDefault(require("aedes"));
9
+ var _tls = require("tls");
10
+ var _http = require("http");
11
+ var _path = _interopRequireDefault(require("path"));
12
+ var _fs = _interopRequireDefault(require("fs"));
13
+ var _os = _interopRequireDefault(require("os"));
14
+ var _getPort = _interopRequireDefault(require("get-port"));
15
+ var _aedesPersistence = _interopRequireDefault(require("aedes-persistence"));
16
+ var _selfsigned = _interopRequireDefault(require("selfsigned"));
17
+ var _constants = require("../emulatorutil/constants");
18
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
19
+ function getLocalIP() {
20
+ const interfaces = _os.default.networkInterfaces();
21
+ const skip = ['VMware', 'VirtualBox', 'Hyper-V', 'DockerNAT', 'vEthernet', 'WSL', 'Parallels'];
22
+ for (const name in interfaces) {
23
+ if (skip.some(s => name.includes(s))) continue;
24
+ for (const alias of interfaces[name] ?? []) {
25
+ if (alias.family === 'IPv4' && !alias.internal) return alias.address;
26
+ }
27
+ }
28
+ return '127.0.0.1';
29
+ }
30
+ function ensureTlsCerts(tlsDir) {
31
+ const keyPath = _path.default.join(tlsDir, 'key.pem');
32
+ const certPath = _path.default.join(tlsDir, 'cert.pem');
33
+ if (!_fs.default.existsSync(keyPath) || !_fs.default.existsSync(certPath)) {
34
+ const pems = _selfsigned.default.generate([{
35
+ name: 'commonName',
36
+ value: 'localhost'
37
+ }], {
38
+ days: 365
39
+ });
40
+ _fs.default.mkdirSync(tlsDir, {
41
+ recursive: true
42
+ });
43
+ _fs.default.writeFileSync(keyPath, pems.private);
44
+ _fs.default.writeFileSync(certPath, pems.cert);
45
+ }
46
+ return {
47
+ key: _fs.default.readFileSync(keyPath),
48
+ cert: _fs.default.readFileSync(certPath),
49
+ rejectUnauthorized: false
50
+ };
51
+ }
52
+ async function startBroker(options) {
53
+ const {
54
+ preferredMqttPort = 1884,
55
+ preferredHttpPort = 8883,
56
+ tlsDir = _path.default.join(_constants.defaultSDKHome, 'tls'),
57
+ host,
58
+ onHttpRequest
59
+ } = options ?? {};
60
+ const mqttHost = host ?? getLocalIP();
61
+ const persistence = (0, _aedesPersistence.default)();
62
+ const aedes = new _aedes.default({
63
+ persistence,
64
+ queueLimit: 80
65
+ });
66
+ const tlsOptions = ensureTlsCerts(tlsDir);
67
+ const tlsServer = (0, _tls.createServer)(tlsOptions, aedes.handle);
68
+ const httpServer = (0, _http.createServer)((req, res) => {
69
+ if (onHttpRequest?.(req, res)) return;
70
+ res.statusCode = 404;
71
+ res.end();
72
+ });
73
+ const mqttPort = await (0, _getPort.default)({
74
+ port: preferredMqttPort
75
+ });
76
+ const httpPort = await (0, _getPort.default)({
77
+ port: preferredHttpPort
78
+ });
79
+ await new Promise((resolve, reject) => {
80
+ tlsServer.once('error', reject);
81
+ tlsServer.listen(mqttPort, '0.0.0.0', () => {
82
+ httpServer.listen(httpPort, '0.0.0.0', () => resolve());
83
+ });
84
+ });
85
+ return {
86
+ aedes,
87
+ mqttPort,
88
+ httpPort,
89
+ mqttHost,
90
+ tlsOptions,
91
+ httpServer,
92
+ async close() {
93
+ await new Promise(r => httpServer.close(() => r()));
94
+ await new Promise(r => tlsServer.close(() => r()));
95
+ aedes.close();
96
+ }
97
+ };
98
+ }
@@ -48,11 +48,27 @@ const defaultDebugPort = exports.defaultDebugPort = 10055;
48
48
  async function getSdkUrl() {
49
49
  const externalBaseUrl = 'https://vela-ide.cnbj3-fusion.mi-fds.com/vela-ide';
50
50
  const internalBaseUrl = 'https://cnbj3-fusion-fds.api.xiaomi.net/vela-ide';
51
- // 优先使用内网域名,因为云上工程没有外网环境
52
- let base = internalBaseUrl;
53
- await fetch(`${base}/versions.json`).catch(() => {
54
- base = externalBaseUrl;
55
- });
51
+
52
+ // 带超时的 fetch 探测,避免在网络黑洞场景下永远 hang
53
+ const probeWithTimeout = async function (url) {
54
+ let timeoutMs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 3000;
55
+ const ctrl = new AbortController();
56
+ const timer = setTimeout(() => ctrl.abort(), timeoutMs);
57
+ try {
58
+ const res = await fetch(url, {
59
+ signal: ctrl.signal
60
+ });
61
+ return res.ok;
62
+ } catch {
63
+ return false;
64
+ } finally {
65
+ clearTimeout(timer);
66
+ }
67
+ };
68
+
69
+ // 优先使用内网域名,失败则降级到外网
70
+ const internalOk = await probeWithTimeout(`${internalBaseUrl}/versions.json`);
71
+ const base = internalOk ? internalBaseUrl : externalBaseUrl;
56
72
  return {
57
73
  baseUrl: base,
58
74
  versionUrl: `${base}/versions.json`,
@@ -72,14 +88,21 @@ const VelaImageVersionList = exports.VelaImageVersionList = [{
72
88
  label: 'vela-miwear-watch-5',
73
89
  description: '适用于手表/手环的,带表盘的 vela 5.0 镜像,不可自定义模拟器尺寸',
74
90
  value: _Vvd.VelaImageType.VELA_MIWEAR_WATCH_5,
75
- time: '20260130',
91
+ time: '20250716',
92
+ hide: false,
93
+ icon: ''
94
+ }, {
95
+ label: 'vela-miwear-watch-5.0(开发者大赛)',
96
+ description: '2026 openvela AI 硬件开发者大赛专供,部分 API 为实验性接口,不适合日常开发使用',
97
+ value: _Vvd.VelaImageType.VELA_MIWEAR_WATCH_5_BETA,
98
+ time: '20260717',
76
99
  hide: false,
77
100
  icon: ''
78
101
  }, {
79
102
  label: 'vela-watch-5',
80
103
  description: '适用于手表/手环的,不带表盘的 vela 5.0 镜像,可自定义模拟器尺寸',
81
104
  value: _Vvd.VelaImageType.VELA_WATCH_5,
82
- time: '20260127',
105
+ time: '20250716',
83
106
  hide: false,
84
107
  icon: ''
85
108
  }, {
@@ -113,7 +136,7 @@ const VelaImageVersionList = exports.VelaImageVersionList = [{
113
136
  }];
114
137
  const EmulatorEnvVersion = exports.EmulatorEnvVersion = {
115
138
  name: '模拟器资源版本管理',
116
- [_Vvd.SDKParts.EMULATOR]: '0.2.0',
139
+ [_Vvd.SDKParts.EMULATOR]: '0.1.0',
117
140
  [_Vvd.SDKParts.QA]: '0.0.1',
118
141
  [_Vvd.SDKParts.SKINS]: '0.0.12',
119
142
  [_Vvd.SDKParts.SYSTEM_IMAGES]: VelaImageVersionList[0].time,
package/lib/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from './broker';
1
2
  export * from './vvd';
2
3
  export { getSystemArch, tryRun } from './utils';
3
4
  export * from './instance';
package/lib/index.js CHANGED
@@ -19,6 +19,18 @@ Object.defineProperty(exports, "tryRun", {
19
19
  return _utils.tryRun;
20
20
  }
21
21
  });
22
+ var _broker = require("./broker");
23
+ Object.keys(_broker).forEach(function (key) {
24
+ if (key === "default" || key === "__esModule") return;
25
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
26
+ if (key in exports && exports[key] === _broker[key]) return;
27
+ Object.defineProperty(exports, key, {
28
+ enumerable: true,
29
+ get: function () {
30
+ return _broker[key];
31
+ }
32
+ });
33
+ });
22
34
  var _vvd = require("./vvd");
23
35
  Object.keys(_vvd).forEach(function (key) {
24
36
  if (key === "default" || key === "__esModule") return;
@@ -8,7 +8,8 @@ import { Vela5Instance } from './vela5';
8
8
  import { VelaMiwear5 } from './miwear5';
9
9
  import { MiniSound5 } from './minisound';
10
10
  import { OpenSmartspeaker5 } from './openSmartspeaker5';
11
- declare function getInstanceClass(imageType: VelaImageType): typeof GoldfishInstance | typeof MiwearInstance | typeof PreInstance | typeof Vela5Instance | typeof VelaMiwear5 | typeof MiniSound5 | typeof OpenSmartspeaker5;
11
+ import { VelaMiwear5Beta } from './miwear5beta';
12
+ declare function getInstanceClass(imageType: VelaImageType): typeof GoldfishInstance | typeof MiwearInstance | typeof PreInstance | typeof Vela5Instance | typeof VelaMiwear5 | typeof MiniSound5 | typeof OpenSmartspeaker5 | typeof VelaMiwear5Beta;
12
13
  /**
13
14
  * 根据镜像决定使用哪个instance
14
15
  * Vela正式版(4.0) -> MiwearInstance
@@ -44,6 +44,7 @@ var _vela = require("./vela5");
44
44
  var _miwear2 = require("./miwear5");
45
45
  var _minisound = require("./minisound");
46
46
  var _openSmartspeaker = require("./openSmartspeaker5");
47
+ var _miwear5beta = require("./miwear5beta");
47
48
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
48
49
  function getInstanceClass(imageType) {
49
50
  const map = {
@@ -53,7 +54,8 @@ function getInstanceClass(imageType) {
53
54
  [_Vvd.VelaImageType.VELA_WATCH_5]: _vela.Vela5Instance,
54
55
  [_Vvd.VelaImageType.VELA_MIWEAR_WATCH_5]: _miwear2.VelaMiwear5,
55
56
  [_Vvd.VelaImageType.VELA_MIWEAR_MINISOUND_5]: _minisound.MiniSound5,
56
- [_Vvd.VelaImageType.OPENVELA_SMARTSPEAKER_5]: _openSmartspeaker.OpenSmartspeaker5
57
+ [_Vvd.VelaImageType.OPENVELA_SMARTSPEAKER_5]: _openSmartspeaker.OpenSmartspeaker5,
58
+ [_Vvd.VelaImageType.VELA_MIWEAR_WATCH_5_BETA]: _miwear5beta.VelaMiwear5Beta
57
59
  };
58
60
  return map[imageType] || _miwear2.VelaMiwear5;
59
61
  }
@@ -0,0 +1,5 @@
1
+ import { VelaImageType } from '../typing/Vvd';
2
+ import { VelaMiwear5 } from './miwear5';
3
+ export declare class VelaMiwear5Beta extends VelaMiwear5 {
4
+ imageType: VelaImageType;
5
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.VelaMiwear5Beta = void 0;
7
+ var _Vvd = require("../typing/Vvd");
8
+ var _miwear = require("./miwear5");
9
+ class VelaMiwear5Beta extends _miwear.VelaMiwear5 {
10
+ imageType = (() => _Vvd.VelaImageType.VELA_MIWEAR_WATCH_5_BETA)();
11
+ }
12
+ exports.VelaMiwear5Beta = VelaMiwear5Beta;
@@ -14,7 +14,8 @@ export declare enum VelaImageType {
14
14
  VELA_WATCH_5 = "vela-watch-5.0",
15
15
  REL = "vela-release-4.0",
16
16
  PRE = "vela-pre-4.0",
17
- DEV = "vela-dev-0.0.4"
17
+ DEV = "vela-dev-0.0.4",
18
+ VELA_MIWEAR_WATCH_5_BETA = "vela-miwear-watch-5.0-beta"
18
19
  }
19
20
  export interface IVvdParams {
20
21
  name: string;
package/lib/typing/Vvd.js CHANGED
@@ -17,6 +17,7 @@ let VelaImageType = exports.VelaImageType = /*#__PURE__*/function (VelaImageType
17
17
  VelaImageType["REL"] = "vela-release-4.0";
18
18
  VelaImageType["PRE"] = "vela-pre-4.0";
19
19
  VelaImageType["DEV"] = "vela-dev-0.0.4";
20
+ VelaImageType["VELA_MIWEAR_WATCH_5_BETA"] = "vela-miwear-watch-5.0-beta";
20
21
  return VelaImageType;
21
22
  }({});
22
23
  let SDKParts = exports.SDKParts = /*#__PURE__*/function (SDKParts) {
package/lib/vvd/index.js CHANGED
@@ -235,6 +235,9 @@ class VvdManager {
235
235
  /** 获取已经创建的模拟器列表 */
236
236
  getVvdList() {
237
237
  const avdList = [];
238
+ if (!_fs.default.existsSync(this.vvdHome)) {
239
+ return avdList;
240
+ }
238
241
  const files = _fs.default.readdirSync(this.vvdHome);
239
242
  const regex = /^([\d\D]*)\.(avd|vvd)$/;
240
243
  for (const fileName of files) {
@@ -451,7 +454,7 @@ class VvdManager {
451
454
  length: 100
452
455
  }).map((_, i) => 10055 + i)
453
456
  }));
454
- let portMappingStr = `-network-user-mode-options hostfwd=tcp:127.0.0.1:${degbugProt}-10.0.2.15:101`;
457
+ let portMappingStr = '';
455
458
 
456
459
  // qemu 配置
457
460
  const HOST_9PFS_DIR = _path.default.join(vvdDir, 'share');
@@ -489,6 +492,12 @@ class VvdManager {
489
492
 
490
493
  await this.oldEmulatorMigrate(vvdName);
491
494
  const vvdInfo = this.getVvdInfo(vvdName);
495
+
496
+ // vela4 系列或未指定 imageType 时需要端口转发
497
+ const v4Types = [_Vvd.VelaImageType.REL, _Vvd.VelaImageType.PRE, _Vvd.VelaImageType.DEV];
498
+ if (!vvdInfo.imageType || v4Types.includes(vvdInfo.imageType)) {
499
+ portMappingStr = `-network-user-mode-options hostfwd=tcp:127.0.0.1:${degbugProt}-10.0.2.15:101`;
500
+ }
492
501
  if (vvdInfo.imageType === _Vvd.VelaImageType.OPENVELA_SMARTSPEAKER_5) {
493
502
  qemuOption = `-device virtio-snd,bus=virtio-mmio-bus.2 -allow-host-audio -semihosting -smp 4 -device virtio-gpu-device,xres=480,yres=800,bus=virtio-mmio-bus.5`;
494
503
  }
@@ -636,7 +645,7 @@ class VvdManager {
636
645
  readlines.stdoutReadline.off('line', emulatorStartedHandler);
637
646
  const velaAgent = (0, _grpc.createGrpcClient)(e);
638
647
  await velaAgent.waitForReady();
639
- if ([_Vvd.VelaImageType.VELA_MIWEAR_WATCH_5, _Vvd.VelaImageType.REL].includes(imageType)) {
648
+ if ([_Vvd.VelaImageType.VELA_MIWEAR_WATCH_5, _Vvd.VelaImageType.VELA_MIWEAR_WATCH_5_BETA, _Vvd.VelaImageType.REL].includes(imageType)) {
640
649
  // 这个时候系统启动成功了,但是 rpk 安装程序还没准备好
641
650
  const miwearXmsReadyHander = msg => {
642
651
  const startedFunc = emulatorInstance.isXmsInited.bind(emulatorInstance);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiot-toolkit/emulator",
3
- "version": "2.0.6-beta.25",
3
+ "version": "2.0.6-beta.28",
4
4
  "description": "vela emulator tool.",
5
5
  "homepage": "",
6
6
  "license": "ISC",
@@ -32,11 +32,13 @@
32
32
  "emulator"
33
33
  ],
34
34
  "dependencies": {
35
- "@aiot-toolkit/shared-utils": "2.0.6-beta.25",
35
+ "@aiot-toolkit/shared-utils": "2.0.6-beta.28",
36
36
  "@grpc/grpc-js": "^1.13.3",
37
37
  "@grpc/proto-loader": "^0.7.13",
38
38
  "@miwt/adb": "0.10.5",
39
39
  "adm-zip": "^0.5.16",
40
+ "aedes": "^0.51.3",
41
+ "aedes-persistence": "^10.0.0",
40
42
  "core-js": "^3.48.0",
41
43
  "dayjs": "^1.11.12",
42
44
  "find-process": "^1.4.7",
@@ -44,11 +46,11 @@
44
46
  "ini": "^4.1.3",
45
47
  "ipull": "^3.6.2",
46
48
  "portfinder": "^1.0.32",
49
+ "selfsigned": "^3.0.1",
47
50
  "semver": "^7.6.3"
48
51
  },
49
52
  "devDependencies": {
50
53
  "@types/adm-zip": "^0.5.5",
51
54
  "@types/ini": "^4.1.1"
52
- },
53
- "gitHead": "25b954200dd056d25c2c9fa2c2ccd04f261d57af"
55
+ }
54
56
  }