package com.zy.asrs.controller;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
import com.baomidou.mybatisplus.plugins.Page;
|
import com.core.annotations.ManagerAuth;
|
import com.core.common.Cools;
|
import com.core.common.R;
|
import com.core.controller.AbstractBaseController;
|
import com.zy.asrs.entity.ApkBuildTask;
|
import com.zy.asrs.entity.TvDevice;
|
import com.zy.asrs.service.ApkBuildTaskService;
|
import com.zy.asrs.service.TvDeviceService;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.multipart.MultipartFile;
|
import java.nio.file.Files;
|
import java.nio.file.Path;
|
import java.nio.file.Paths;
|
import java.util.*;
|
|
/**
|
* 电视机设备Controller
|
*/
|
@RestController
|
public class TvDeviceController extends AbstractBaseController {
|
|
@Autowired
|
private TvDeviceService tvDeviceService;
|
|
@Autowired
|
private ApkBuildTaskService apkBuildTaskService;
|
|
@Value("${apk-build.apk-download-path}")
|
private String apkDownloadPath;
|
|
/**
|
* 查询单个设备
|
*/
|
@RequestMapping(value = "/tvDevice/{id}/auth")
|
@ManagerAuth
|
public R get(@PathVariable("id") Long id) {
|
return R.ok(tvDeviceService.selectById(id));
|
}
|
|
/**
|
* 分页查询设备列表
|
*/
|
@RequestMapping(value = "/tvDevice/list/auth")
|
@ManagerAuth
|
public R list(@RequestParam(defaultValue = "1") Integer curr,
|
@RequestParam(defaultValue = "10") Integer limit,
|
@RequestParam Map<String, Object> param) {
|
excludeTrash(param);
|
EntityWrapper<TvDevice> wrapper = new EntityWrapper<>();
|
|
for (Map.Entry<String, Object> entry : param.entrySet()) {
|
String key = entry.getKey();
|
Object value = entry.getValue();
|
if (value == null || "".equals(value.toString())) {
|
continue;
|
}
|
if ("status".equals(key)) {
|
wrapper.eq(key, value);
|
} else {
|
wrapper.like(key, String.valueOf(value));
|
}
|
}
|
|
wrapper.orderBy("id", false);
|
return R.ok(tvDeviceService.selectPage(new Page<>(curr, limit), wrapper));
|
}
|
|
/**
|
* 获取所有设备(不分页)
|
*/
|
@RequestMapping(value = "/tvDevice/all/auth")
|
@ManagerAuth
|
public R listAll() {
|
EntityWrapper<TvDevice> wrapper = new EntityWrapper<>();
|
wrapper.orderBy("name", true);
|
return R.ok(tvDeviceService.selectList(wrapper));
|
}
|
|
/**
|
* 获取所有在线设备
|
*/
|
@RequestMapping(value = "/tvDevice/online/auth")
|
@ManagerAuth
|
public R listOnline() {
|
return R.ok(tvDeviceService.getOnlineDevices());
|
}
|
|
/**
|
* 新增设备
|
*/
|
@RequestMapping(value = "/tvDevice/add/auth", method = RequestMethod.POST)
|
@ManagerAuth
|
public R add(@RequestBody TvDevice device) {
|
if (Cools.isEmpty(device.getName())) {
|
return R.error("设备名称不能为空");
|
}
|
if (Cools.isEmpty(device.getIp())) {
|
return R.error("设备IP不能为空");
|
}
|
|
// 检查IP是否已存在
|
EntityWrapper<TvDevice> wrapper = new EntityWrapper<>();
|
wrapper.eq("ip", device.getIp());
|
if (tvDeviceService.selectCount(wrapper) > 0) {
|
return R.error("该IP已存在");
|
}
|
|
if (device.getPort() == null) {
|
device.setPort(5555);
|
}
|
device.setStatus((short) 0);
|
device.setCreateTime(new Date());
|
device.setUpdateTime(new Date());
|
tvDeviceService.insert(device);
|
return R.ok(device);
|
}
|
|
/**
|
* 更新设备
|
*/
|
@RequestMapping(value = "/tvDevice/update/auth", method = RequestMethod.POST)
|
@ManagerAuth
|
public R update(@RequestBody TvDevice device) {
|
if (device.getId() == null) {
|
return R.error("设备ID不能为空");
|
}
|
|
// 检查IP是否已被其他设备使用
|
if (!Cools.isEmpty(device.getIp())) {
|
EntityWrapper<TvDevice> wrapper = new EntityWrapper<>();
|
wrapper.eq("ip", device.getIp()).ne("id", device.getId());
|
if (tvDeviceService.selectCount(wrapper) > 0) {
|
return R.error("该IP已被其他设备使用");
|
}
|
}
|
|
device.setUpdateTime(new Date());
|
tvDeviceService.updateById(device);
|
return R.ok();
|
}
|
|
/**
|
* 删除设备
|
*/
|
@RequestMapping(value = "/tvDevice/delete/auth", method = RequestMethod.POST)
|
@ManagerAuth
|
public R delete(Long[] ids) {
|
if (ids == null || ids.length == 0) {
|
return R.error();
|
}
|
tvDeviceService.deleteBatchIds(Arrays.asList(ids));
|
return R.ok();
|
}
|
|
/**
|
* 测试连接
|
*/
|
@RequestMapping(value = "/tvDevice/testConnection/{id}/auth", method = RequestMethod.POST)
|
@ManagerAuth
|
public R testConnection(@PathVariable("id") Long id) {
|
try {
|
String result = tvDeviceService.testConnection(id);
|
Map<String, Object> data = new HashMap<>();
|
data.put("result", result);
|
data.put("device", tvDeviceService.selectById(id));
|
return R.ok(data);
|
} catch (Exception e) {
|
return R.error("连接失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 刷新所有设备状态
|
*/
|
@RequestMapping(value = "/tvDevice/refreshAll/auth", method = RequestMethod.POST)
|
@ManagerAuth
|
public R refreshAllStatus() {
|
try {
|
int count = tvDeviceService.refreshAllStatus();
|
Map<String, Object> data = new HashMap<>();
|
data.put("refreshedCount", count);
|
return R.ok(data);
|
} catch (Exception e) {
|
return R.error("刷新失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 安装打包任务的APK到设备
|
*/
|
@RequestMapping(value = "/tvDevice/installFromTask/auth", method = RequestMethod.POST)
|
@ManagerAuth
|
public R installFromTask(@RequestBody JSONObject param) {
|
try {
|
Long taskId = param.getLong("taskId");
|
List<Long> deviceIds = param.getJSONArray("deviceIds").toJavaList(Long.class);
|
|
if (taskId == null) {
|
return R.error("请选择打包任务");
|
}
|
if (deviceIds == null || deviceIds.isEmpty()) {
|
return R.error("请选择安装设备");
|
}
|
|
ApkBuildTask task = apkBuildTaskService.selectById(taskId);
|
if (task == null) {
|
return R.error("打包任务不存在");
|
}
|
if (task.getApkPath() == null) {
|
return R.error("APK文件未下载,请先下载");
|
}
|
|
List<String> results = tvDeviceService.batchInstallApk(deviceIds, task.getApkPath());
|
return R.ok(results);
|
} catch (Exception e) {
|
return R.error("安装失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 上传APK并安装到设备
|
*/
|
@RequestMapping(value = "/tvDevice/uploadAndInstall/auth", method = RequestMethod.POST)
|
@ManagerAuth
|
public R uploadAndInstall(@RequestParam("file") MultipartFile file,
|
@RequestParam("deviceIds") String deviceIdsStr) {
|
try {
|
if (file.isEmpty()) {
|
return R.error("请选择APK文件");
|
}
|
|
String originalFilename = file.getOriginalFilename();
|
if (originalFilename == null || !originalFilename.toLowerCase().endsWith(".apk")) {
|
return R.error("请上传APK文件");
|
}
|
|
// 解析设备ID
|
List<Long> deviceIds = new ArrayList<>();
|
for (String idStr : deviceIdsStr.split(",")) {
|
if (!idStr.trim().isEmpty()) {
|
deviceIds.add(Long.parseLong(idStr.trim()));
|
}
|
}
|
|
if (deviceIds.isEmpty()) {
|
return R.error("请选择安装设备");
|
}
|
|
// 保存文件
|
Path uploadDir = Paths.get(apkDownloadPath, "uploads");
|
if (!Files.exists(uploadDir)) {
|
Files.createDirectories(uploadDir);
|
}
|
|
String fileName = System.currentTimeMillis() + "_" + originalFilename;
|
Path filePath = uploadDir.resolve(fileName);
|
file.transferTo(filePath.toFile());
|
|
// 安装到设备
|
List<String> results = tvDeviceService.batchInstallApk(deviceIds, filePath.toString());
|
|
Map<String, Object> data = new HashMap<>();
|
data.put("results", results);
|
data.put("apkPath", filePath.toString());
|
return R.ok(data);
|
} catch (Exception e) {
|
return R.error("安装失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 启动设备上的应用
|
*/
|
@RequestMapping(value = "/tvDevice/launchApp/{id}/auth", method = RequestMethod.POST)
|
@ManagerAuth
|
public R launchApp(@PathVariable("id") Long id, @RequestBody(required = false) JSONObject param) {
|
try {
|
String packageName = param != null ? param.getString("packageName") : null;
|
String result = tvDeviceService.launchApp(id, packageName);
|
Map<String, Object> data = new HashMap<>();
|
data.put("result", result);
|
data.put("device", tvDeviceService.selectById(id));
|
return R.ok(data);
|
} catch (Exception e) {
|
return R.error("启动失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 批量启动设备上的应用
|
*/
|
@RequestMapping(value = "/tvDevice/batchLaunchApp/auth", method = RequestMethod.POST)
|
@ManagerAuth
|
public R batchLaunchApp(@RequestBody JSONObject param) {
|
try {
|
List<Long> deviceIds = param.getJSONArray("deviceIds").toJavaList(Long.class);
|
String packageName = param.getString("packageName");
|
|
if (deviceIds == null || deviceIds.isEmpty()) {
|
return R.error("请选择设备");
|
}
|
|
List<String> results = tvDeviceService.batchLaunchApp(deviceIds, packageName);
|
return R.ok(results);
|
} catch (Exception e) {
|
return R.error("启动失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 获取设备屏幕截图
|
*/
|
@RequestMapping(value = "/tvDevice/screenshot/{id}/auth", method = RequestMethod.GET)
|
@ManagerAuth
|
public R captureScreen(@PathVariable("id") Long id) {
|
try {
|
String base64Image = tvDeviceService.captureScreen(id);
|
Map<String, Object> data = new HashMap<>();
|
data.put("image", base64Image);
|
data.put("device", tvDeviceService.selectById(id));
|
return R.ok(data);
|
} catch (Exception e) {
|
return R.error("截图失败: " + e.getMessage());
|
}
|
}
|
}
|