"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deviceManager = void 0;
const tslib_1 = require("tslib");
const log_1 = require("@/utils/log");
const db_1 = require("@/db");
const debug_target_1 = require("@/utils/debug-target");
const config_1 = require("@/config");
const app_client_manager_1 = require("@/client/app-client-manager");
const pub_sub_manager_1 = require("@/controller/pub-sub-manager");
const log = new log_1.Logger('device-manager');
class DeviceManager {
    constructor() {
        this.deviceList = [];
    }
    /**
     * app 断连，清理调试对象
     */
    onAppDisconnect() {
        const device = this.deviceList[0];
        if (!device)
            return;
        // 通过 tunnel 通道创建的 debugTarget 的 clientId 为 devicename
        pub_sub_manager_1.cleanDebugTarget(device.devicename, false);
    }
    /**
     * app 连接，添加调试对象，并订阅上行调试指令
     */
    onAppConnect() {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            log.info('app connect, %j', this.deviceList);
            const device = this.deviceList[0];
            if (!device)
                return log.warn('no device connect!');
            const useTunnel = app_client_manager_1.appClientManager.shouldUseAppClientType(device.platform, "TunnelAppClient" /* Tunnel */);
            log.info('useTunnel %j, is connected %j', useTunnel, device.physicalstatus === "1" /* Connected */);
            if (device.physicalstatus === "1" /* Connected */ && useTunnel) {
                try {
                    let debugTarget = debug_target_1.createTargetByDeviceInfo(device);
                    debugTarget = yield debug_target_1.patchDebugTarget(debugTarget);
                    const { DB } = db_1.getDBOperator();
                    log.info('before upsert db %j', debugTarget);
                    new DB(config_1.config.redis.debugTargetTable).upsert(debugTarget.clientId, debugTarget);
                    pub_sub_manager_1.subscribeCommand(debugTarget);
                }
                catch (e) {
                    log.info('app connect e, %j, %j', (_a = e) === null || _a === void 0 ? void 0 : _a.stack, e);
                }
            }
        });
    }
    /**
     * 查询 USB 连接的设备列表
     */
    getDeviceList() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            global.addon.getDeviceList((devices) => {
                log.info('getDeviceList: %j', devices);
                this.deviceList = devices;
                if (devices.length) {
                    const isDeviceDisconnect = devices[0].physicalstatus === "2" /* Disconnected */;
                    if (isDeviceDisconnect)
                        return;
                    // 目前暂不支持多设备连接，所以默认选择第一个设备
                    const device = this.deviceList[0];
                    const deviceId = device.deviceid;
                    log.info(`selectDevice ${deviceId}`);
                    global.addon.selectDevice(deviceId);
                }
            });
        });
    }
}
exports.deviceManager = new DeviceManager();
