UNPKG

@aiot-toolkit/emulator

Version:

vela emulator tool.

98 lines (94 loc) 4.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Vela5Instance = void 0; var _shared = require("../shared"); var _Vvd = require("../typing/Vvd"); var _logcat = require("../vvd/logcat"); var _miwear = _interopRequireDefault(require("./miwear")); var adbMiwt = _interopRequireWildcard(require("@miwt/adb")); var _util = require("util"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } class Vela5Instance extends _miwear.default { static emulatorStartedFlag = /booting completed|Boot completed/; static appStartedFlag = /Start main loop/; imageType = (() => _Vvd.VelaImageType.VELA_WATCH_5)(); appDir = '/data/app'; /** * 使用 pm 安装快应用 * @param targeRpk 快应用的rpk文件路径 */ async install(targeRpk) { const installCmd = `adb -s ${this.sn} shell pm install ${targeRpk}`; this.logger(`Excuting: ${installCmd}`); const res = await adbMiwt.execAdbCmdAsync(installCmd); await (0, _util.promisify)(process.nextTick)(); this.logger(`Install result: ${res}`); // vela 5 安装成功的日志为: // onInstallResult: com.application.watch.demo(success 0) if (res.includes('(success 0)')) { return Promise.resolve(); } else { return Promise.reject(res); } } /** 使用 am start 启动快应用 */ async startApp(packageName) { const startCmd = `adb -s ${this.sn} shell am start ${packageName}`; this.logger(`Excuting: ${startCmd}`); adbMiwt.execAdbCmdAsync(startCmd); return new Promise((resolve, reject) => { const clearFn = () => { clearTimeout(timer); this.stdoutReadline.off('line', func); }; const func = msg => { if (Vela5Instance.appStartedFlag.test(msg)) { this.logger(`Start ${packageName} for ${this.vvdName} successfully`); clearFn(); resolve(); } const escapedPackageName = packageName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const reg = new RegExp(`Failed to open: .+?/${escapedPackageName}/manifest\.json`); if (reg.test(msg)) { this.logger(`Failed Start ${packageName} for ${this.vvdName}`); clearFn(); reject(_shared.RpkStartFailedReason.getManifestFailed); } }; let timer = setTimeout(() => { this.stdoutReadline.off('line', func); this.logger(`Start ${packageName} for ${this.vvdName} timeout`); reject('Timeout'); }, 8 * 1000); this.stdoutReadline.on('line', func); }); } /** 重启模拟器 */ async reboot() { const rebootCmd = `adb -s ${this.sn} shell reboot`; this.logger(`Excuting: ${rebootCmd}`); await adbMiwt.execAdbCmdAsync(rebootCmd); await this.isConnected(); if (this.logcatProcess.exitCode !== null) { // 如果 logcat 进程被杀死,则重新创建 const r = (0, _logcat.creatLogcat)(this.sn, this.onStdout, this.onErrout); this.logcatProcess = r.logcatProcess; this.stdoutReadline = r.stdoutReadline; this.stderrReadline = r.stderrReadline; this.disposeReadlines = r.dispose; } } async closeApp(appName) { await super.closeApp(appName); await new Promise(resolve => { setTimeout(() => { resolve(); }, 2000); }); } } exports.Vela5Instance = Vela5Instance;