#
Junjie
2026-01-19 ecf6d2ccc69c1e8bd2068c16f232b98bf4a8c234
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package com.zy.asrs.service.impl;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zy.asrs.entity.TvDevice;
import com.zy.asrs.mapper.TvDeviceMapper;
import com.zy.asrs.service.TvDeviceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Date;
import java.util.List;
 
/**
 * 电视机设备Service实现类
 */
@Service
public class TvDeviceServiceImpl extends ServiceImpl<TvDeviceMapper, TvDevice> implements TvDeviceService {
 
    private static final Logger log = LoggerFactory.getLogger(TvDeviceServiceImpl.class);
 
    @Value("${adb.path}")
    private String adbPath;
 
    @Value("${adb.default-package:com.zy.monitor}")
    private String defaultPackage;
 
    @Override
    public String testConnection(Long id) throws Exception {
        TvDevice device = this.selectById(id);
        if (device == null) {
            throw new RuntimeException("设备不存在");
        }
 
        StringBuilder result = new StringBuilder();
        String adbAddress = device.getAdbAddress();
 
        try {
            // 断开已有连接
            executeAdbCommand("disconnect", adbAddress);
            Thread.sleep(500);
 
            // 尝试连接
            String connectResult = executeAdbCommand("connect", adbAddress);
            result.append("连接: ").append(connectResult).append("\n");
 
            // 检查连接状态
            boolean connected = connectResult.contains("connected") && !connectResult.contains("failed");
 
            if (connected) {
                // 获取设备信息
                String devicesResult = executeAdbCommand("devices");
                result.append("设备列表: ").append(devicesResult);
 
                // 更新设备状态
                device.setStatus((short) 1);
                device.setLastConnectTime(new Date());
            } else {
                device.setStatus((short) 0);
            }
 
            device.setUpdateTime(new Date());
            this.updateById(device);
 
            return result.toString();
        } catch (Exception e) {
            device.setStatus((short) 0);
            device.setUpdateTime(new Date());
            this.updateById(device);
            throw e;
        }
    }
 
    @Override
    public int refreshAllStatus() {
        List<TvDevice> devices = this.selectList(null);
        int count = 0;
        for (TvDevice device : devices) {
            try {
                testConnection(device.getId());
                count++;
            } catch (Exception e) {
                log.error("测试设备 {} 连接失败: {}", device.getName(), e.getMessage());
            }
        }
        return count;
    }
 
    @Override
    public String installApk(Long deviceId, String apkPath) throws Exception {
        TvDevice device = this.selectById(deviceId);
        if (device == null) {
            throw new RuntimeException("设备不存在");
        }
 
        File apkFile = new File(apkPath);
        if (!apkFile.exists()) {
            throw new RuntimeException("APK文件不存在: " + apkPath);
        }
 
        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(1000);
 
        // 安装APK
        String installResult = executeAdbCommand("-s", adbAddress, "install", "-r", apkPath);
        result.append("安装: ").append(installResult);
 
        // 更新设备状态
        device.setStatus((short) 1);
        device.setLastConnectTime(new Date());
        device.setUpdateTime(new Date());
        this.updateById(device);
 
        log.info("设备 {} 安装APK结果: {}", device.getName(), installResult);
        return result.toString();
    }
 
    @Override
    public List<String> batchInstallApk(List<Long> deviceIds, String apkPath) {
        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 = installApk(deviceId, apkPath);
                results.add(deviceName + ": 安装成功\n" + result);
            } catch (Exception e) {
                results.add(deviceName + ": 安装失败 - " + e.getMessage());
            }
        }
        return results;
    }
 
    @Override
    public List<TvDevice> getOnlineDevices() {
        return this.selectList(new EntityWrapper<TvDevice>().eq("status", 1));
    }
 
    @Override
    public String launchApp(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);
 
        // 方法1: 使用cmd package resolve-activity获取启动Activity(Android 7.0+)
        String launchResult = null;
        boolean launched = false;
 
        try {
            // 尝试使用dumpsys获取启动Activity
            String dumpResult = executeAdbCommand("-s", adbAddress, "shell",
                    "cmd", "package", "resolve-activity", "--brief", pkg);
 
            if (dumpResult != null && dumpResult.contains("/")) {
                // 解析出Activity名称
                String[] lines = dumpResult.split("\n");
                for (String line : lines) {
                    line = line.trim();
                    if (line.contains("/") && !line.startsWith("priority") && !line.startsWith("#")) {
                        // 找到类似 com.zy.app/.MainActivity 的格式
                        launchResult = executeAdbCommand("-s", adbAddress, "shell",
                                "am", "start", "-n", line);
                        launched = true;
                        break;
                    }
                }
            }
        } catch (Exception e) {
            log.warn("cmd package方式失败,尝试备用方式: {}", e.getMessage());
        }
 
        // 方法2: 如果方法1失败,使用am start -a android.intent.action.MAIN
        if (!launched) {
            launchResult = executeAdbCommand("-s", adbAddress, "shell",
                    "am", "start", "-a", "android.intent.action.MAIN",
                    "-c", "android.intent.category.LAUNCHER",
                    "-n", pkg + "/.MainActivity");
 
            // 如果还是失败,尝试常见的Activity名称
            if (launchResult != null && launchResult.contains("Error")) {
                // 尝试 .ui.MainActivity
                launchResult = executeAdbCommand("-s", adbAddress, "shell",
                        "am", "start", "-a", "android.intent.action.MAIN",
                        "-c", "android.intent.category.LAUNCHER",
                        "--package", pkg);
            }
        }
 
        result.append("启动: ").append(launchResult);
 
        // 检查是否启动成功
        if (launchResult != null && (launchResult.contains("Starting:") || launchResult.contains("cmp="))) {
            log.info("设备 {} 启动应用 {} 成功", device.getName(), pkg);
        } else {
            log.warn("设备 {} 启动应用 {} 可能失败: {}", device.getName(), pkg, launchResult);
        }
 
        // 更新设备状态
        device.setStatus((short) 1);
        device.setLastConnectTime(new Date());
        device.setUpdateTime(new Date());
        this.updateById(device);
 
        return result.toString();
    }
 
    @Override
    public List<String> batchLaunchApp(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 = launchApp(deviceId, packageName);
                results.add(deviceName + ": 启动成功\n" + result);
            } catch (Exception e) {
                results.add(deviceName + ": 启动失败 - " + e.getMessage());
            }
        }
        return results;
    }
 
    @Override
    public String captureScreen(Long deviceId) throws Exception {
        TvDevice device = this.selectById(deviceId);
        if (device == null) {
            throw new RuntimeException("设备不存在");
        }
 
        String adbAddress = device.getAdbAddress();
 
        // 先确保连接
        String connectResult = executeAdbCommand("connect", adbAddress);
        if (connectResult.contains("failed")) {
            device.setStatus((short) 0);
            device.setUpdateTime(new Date());
            this.updateById(device);
            throw new RuntimeException("连接设备失败: " + connectResult);
        }
 
        // 使用exec-out直接获取PNG二进制数据
        List<String> command = new ArrayList<>();
        command.add(adbPath);
        command.add("-s");
        command.add(adbAddress);
        command.add("exec-out");
        command.add("screencap");
        command.add("-p");
 
        log.info("执行截图命令: {}", String.join(" ", command));
 
        ProcessBuilder pb = new ProcessBuilder(command);
        Process process = pb.start();
 
        // 读取二进制PNG数据
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        InputStream is = process.getInputStream();
        byte[] buffer = new byte[8192];
        int len;
        while ((len = is.read(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
 
        int exitCode = process.waitFor();
        byte[] pngData = baos.toByteArray();
 
        if (exitCode != 0 || pngData.length < 100) {
            // 读取错误信息
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            StringBuilder errorMsg = new StringBuilder();
            String line;
            while ((line = errorReader.readLine()) != null) {
                errorMsg.append(line);
            }
            log.error("截图失败,退出码: {}, 数据长度: {}, 错误: {}", exitCode, pngData.length, errorMsg);
            throw new RuntimeException("截图失败: " + errorMsg);
        }
 
        // 更新设备状态
        device.setStatus((short) 1);
        device.setLastConnectTime(new Date());
        device.setUpdateTime(new Date());
        this.updateById(device);
 
        // 转换为Base64
        String base64Image = Base64.getEncoder().encodeToString(pngData);
        log.info("设备 {} 截图成功,图片大小: {} bytes", device.getName(), pngData.length);
 
        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命令
     */
    private String executeAdbCommand(String... args) throws Exception {
        List<String> command = new ArrayList<>();
        command.add(adbPath);
        command.addAll(Arrays.asList(args));
 
        log.info("执行ADB命令: {}", String.join(" ", command));
 
        ProcessBuilder pb = new ProcessBuilder(command);
        pb.redirectErrorStream(true);
 
        Process process = pb.start();
 
        StringBuilder output = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                output.append(line).append("\n");
            }
        }
 
        int exitCode = process.waitFor();
        String result = output.toString().trim();
 
        if (exitCode != 0 && !result.contains("Success") && !result.contains("connected")) {
            log.warn("ADB命令执行返回码: {}, 输出: {}", exitCode, result);
        }
 
        return result;
    }
}