#
Junjie
2026-01-21 d565b3796eb2029797d4bc3f962a7daf38fcaf00
src/main/java/com/zy/asrs/service/impl/TvDeviceServiceImpl.java
@@ -334,6 +334,78 @@
        return base64Image;
    }
    @Override
    public String stopApp(Long deviceId, String packageName) throws Exception {
        TvDevice device = this.selectById(deviceId);
        if (device == null) {
            throw new RuntimeException("设备不存在");
        }
        String pkg = (packageName != null && !packageName.trim().isEmpty()) ? packageName.trim() : defaultPackage;
        StringBuilder result = new StringBuilder();
        String adbAddress = device.getAdbAddress();
        // 先连接设备
        String connectResult = executeAdbCommand("connect", adbAddress);
        result.append("连接: ").append(connectResult).append("\n");
        if (connectResult.contains("failed")) {
            device.setStatus((short) 0);
            device.setUpdateTime(new Date());
            this.updateById(device);
            throw new RuntimeException("连接设备失败: " + connectResult);
        }
        Thread.sleep(500);
        // 使用am force-stop关闭应用
        String stopResult = executeAdbCommand("-s", adbAddress, "shell", "am", "force-stop", pkg);
        result.append("关闭: ").append(stopResult.isEmpty() ? "成功" : stopResult);
        // 更新设备状态
        device.setStatus((short) 1);
        device.setLastConnectTime(new Date());
        device.setUpdateTime(new Date());
        this.updateById(device);
        log.info("设备 {} 关闭应用 {} 成功", device.getName(), pkg);
        return result.toString();
    }
    @Override
    public String restartApp(Long deviceId, String packageName) throws Exception {
        StringBuilder result = new StringBuilder();
        // 先关闭应用
        String stopResult = stopApp(deviceId, packageName);
        result.append("【关闭应用】\n").append(stopResult).append("\n\n");
        // 等待1秒确保应用完全关闭
        Thread.sleep(1000);
        // 再启动应用
        String launchResult = launchApp(deviceId, packageName);
        result.append("【启动应用】\n").append(launchResult);
        return result.toString();
    }
    @Override
    public List<String> batchRestartApp(List<Long> deviceIds, String packageName) {
        List<String> results = new ArrayList<>();
        for (Long deviceId : deviceIds) {
            TvDevice device = this.selectById(deviceId);
            String deviceName = device != null ? device.getName() : "ID:" + deviceId;
            try {
                String result = restartApp(deviceId, packageName);
                results.add(deviceName + ": 重启成功\n" + result);
            } catch (Exception e) {
                results.add(deviceName + ": 重启失败 - " + e.getMessage());
            }
        }
        return results;
    }
    /**
     * 执行ADB命令
     */