UNPKG

@aiot-toolkit/emulator

Version:

vela emulator tool.

160 lines (156 loc) 6.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var adbMiwt = _interopRequireWildcard(require("@miwt/adb")); var _path = _interopRequireDefault(require("path")); var _utils = require("../utils"); var _common = _interopRequireDefault(require("./common")); var _Vvd = require("../typing/Vvd"); var _shared = require("../shared"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } 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; } /** * MiwearInstance * 针对 Vela正式版(4.0)的镜像 */ class MiwearInstance extends _common.default { static emulatorStartedFlag = /quickapp_rpk_installer_init|rpk installer init done/; static appInstalledFlag = /InstallState_Finished|install finished/; imageType = (() => _Vvd.VelaImageType.REL)(); appDir = '/data/quickapp/app'; static isAppInstalled(log, targetRpk) { // 注意:targetRpk 可能包含特殊字符,需要转义 const escapedRpk = targetRpk.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const reg = new RegExp(`executeInstall: rpk install: ${escapedRpk} .* success`); return reg.test(log); } /** * 使用 pm 安装快应用 * @param targeRpk 快应用的rpk文件路径 */ async install(targeRpk, options) { const installCmd = `adb -s ${this.sn} shell pm install ${targeRpk}`; this.logger(`Excuting: ${installCmd}`); adbMiwt.execAdbCmd(installCmd); return new Promise((resolve, reject) => { const clearFn = () => { clearTimeout(timer); this.stdoutReadline.off('line', func); }; const func = msg => { if (MiwearInstance.isAppInstalled(msg, targeRpk)) { this.logger(`Install to ${this.vvdName} ${targeRpk} successfully`); clearFn(); resolve(); } if (MiwearInstance.isAppInstallFailed(msg)) { this.logger(`Failed Install to ${this.vvdName} ${targeRpk}`); this.logger(_shared.RpkInstallFailedReason.LowStorage); clearFn(); reject(_shared.RpkInstallFailedReason.LowStorage); } // 当前安装进程被占用 if (msg.includes('installRpk: installation is running, please wait')) { this.logger(_shared.RpkInstallFailedReason.Busy); clearFn(); reject(_shared.RpkInstallFailedReason.Busy); } }; function getTimeout(size) { if (!size) return 10 * 1000; const mu = size / 1024 / 1024; return mu < 1 ? 10 * 1000 : mu < 4 ? 60 * 1000 : 2 * 60 * 1000; } const timeout = getTimeout(options?.size); let timer = setTimeout(() => { this.stdoutReadline.off('line', func); this.logger(`Install to ${this.vvdName} ${targeRpk} timeout`); reject(_shared.RpkInstallFailedReason.Timeout); }, timeout); this.stdoutReadline.on('line', func); this.stdoutReadline.on('close', () => { this.stdoutReadline.off('line', func); clearTimeout(timer); reject(_shared.RpkInstallFailedReason.Poweroff); }); }); } /** * 使用 pm 卸载快应用 * @param packageName 快应用的包名 */ uninstall(packageName) { adbMiwt.execAdbCmd(`adb -s ${this.sn} shell pm uninstall ${packageName}`); return new Promise((resolve, reject) => { const func = msg => { if (MiwearInstance.isAppUninstalled(msg, packageName)) { clearTimeout(timer); this.logger(`${this.vvdName} uninstalled ${packageName} successfully`); resolve(); } }; let timer = setTimeout(() => { this.stdoutReadline.off('line', func); this.logger(`unnstall ${this.vvdName} ${packageName} timeout`); reject('Uninstall timeout'); }, 2 * 60 * 1000); this.stdoutReadline.on('line', func); }); } /** 使用 am start 启动快应用 */ async startApp(packageName) { let debug = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; // 调试模式需要push一个文件至miwear中 if (debug) { const debuggerCfgFile = _path.default.join(__dirname, '../static/debugger_ip.cfg'); await adbMiwt.execAdbCmdAsync(`adb -s ${this.sn} push ${debuggerCfgFile} /data/debugger_ip.cfg`); } else { await adbMiwt.execAdbCmdAsync(`adb -s ${this.sn} shell rm /data/debugger_ip.cfg`); } const startCmd = `adb -s ${this.sn} shell am start ${packageName}`; this.logger(`Excuting: ${startCmd}`); await adbMiwt.execAdbCmdAsync(startCmd); } async closeApp(appName) { const stopCmd = `adb -s ${this.sn} shell am stop ${appName}`; this.logger(`Excuting: ${stopCmd}`); await adbMiwt.execAdbCmdAsync(stopCmd); this.logger(`Stop ${this.vvdName} ${appName} successfully`); } async reboot() { await super.reboot(); return new Promise((resolve, reject) => { const func = msg => { if (MiwearInstance.isEmulatorStarted(msg)) { clearTimeout(timer); resolve(); } }; let timer = setTimeout(() => { this.stdoutReadline.off('line', func); this.logger(`Reboot ${this.vvdName} timeout`); reject('reboot timeout'); }, 2 * 60 * 1000); this.stdoutReadline.on('line', func); }); } async reloadApp(appPackageName) { try { // 2. 执行am stop和am start命令 const stopCmd = `adb -s ${this.sn} shell am stop ${appPackageName}`; await adbMiwt.execAdbCmdAsync(stopCmd); this.logger(`Stop ${this.vvdName} ${appPackageName} successfully`); // 这里是为了等am stop命令清除资源等 await (0, _utils.sleep)(500); const startCmd = `adb -s ${this.sn} shell am start ${appPackageName}`; await adbMiwt.execAdbCmdAsync(startCmd); this.logger(`Start ${this.vvdName} ${appPackageName} successfully`); } catch (e) { this.logger(`${e}`); } } } var _default = exports.default = MiwearInstance;