新增接口
This commit is contained in:
parent
3cb4527d14
commit
5061f4f140
6
pom.xml
6
pom.xml
@ -74,6 +74,12 @@
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.83</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Boot Actuator 依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
@ -15,6 +15,8 @@ import org.springframework.web.bind.annotation.*;
|
||||
// 使用完全限定名称避免与自定义PrintService接口冲突
|
||||
import java.awt.print.PrinterJob;
|
||||
import java.io.File;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@ -41,10 +43,19 @@ public class PrintController implements PrintService {
|
||||
* @return 打印机名称列表
|
||||
*/
|
||||
@GetMapping("printerList")
|
||||
public List<String> printerList() {
|
||||
public List<Map<String, Object>> printerList() {
|
||||
javax.print.PrintService[] printServices = PrinterJob.lookupPrintServices();
|
||||
Set<String> collect = Arrays.stream(printServices).map(service -> service.getName()).collect(Collectors.toSet());
|
||||
return collect.stream().sorted().collect(Collectors.toList());
|
||||
return Arrays.stream(printServices)
|
||||
.map(service -> {
|
||||
Map<String, Object> printer = new HashMap<>();
|
||||
printer.put("name", service.getName());
|
||||
printer.put("status", "online"); // 简化处理,假设所有打印机都在线
|
||||
printer.put("type", "Unknown"); // 可以根据需要扩展
|
||||
printer.put("location", "Local"); // 可以根据需要扩展
|
||||
return printer;
|
||||
})
|
||||
.sorted((a, b) -> ((String) a.get("name")).compareTo((String) b.get("name")))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -86,12 +97,165 @@ public class PrintController implements PrintService {
|
||||
@GetMapping("queue/tasks")
|
||||
public Map<String, Object> getQueueTasks() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("currentTask", printQueueService.getCurrentTaskInfo());
|
||||
result.put("currentTasks", printQueueService.getCurrentTaskInfo() != null ?
|
||||
List.of(printQueueService.getCurrentTaskInfo()) : List.of());
|
||||
result.put("queuedTasks", printQueueService.getQueuedTasksInfo());
|
||||
result.put("timestamp", System.currentTimeMillis());
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 搜索打印任务
|
||||
*
|
||||
* @param printer 打印机名称(可选)
|
||||
* @param status 任务状态(可选)
|
||||
* @param fileUrl 文件URL(可选)
|
||||
* @return 搜索结果
|
||||
*/
|
||||
@GetMapping("tasks/search")
|
||||
public List<Map<String, Object>> searchTasks(
|
||||
@RequestParam(required = false) String printer,
|
||||
@RequestParam(required = false) String status,
|
||||
@RequestParam(required = false) String fileUrl) {
|
||||
|
||||
List<com.goeing.printserver.main.domain.PrintTask> allTasks = new ArrayList<>();
|
||||
|
||||
// 获取历史任务
|
||||
allTasks.addAll(printQueueService.getHistoryService().getAllHistory());
|
||||
|
||||
// 获取当前任务
|
||||
if (printQueueService.getCurrentTask() != null) {
|
||||
allTasks.add(printQueueService.getCurrentTask());
|
||||
}
|
||||
|
||||
// 获取队列中的任务
|
||||
allTasks.addAll(printQueueService.getQueuedTasks());
|
||||
|
||||
// 应用过滤条件
|
||||
return allTasks.stream()
|
||||
.filter(task -> printer == null || task.getPrinter().contains(printer))
|
||||
.filter(task -> status == null || task.getStatus().equals(status))
|
||||
.filter(task -> fileUrl == null || task.getFileUrl().contains(fileUrl))
|
||||
.map(task -> {
|
||||
Map<String, Object> taskMap = new HashMap<>();
|
||||
taskMap.put("id", task.getFileUrl().hashCode()); // 简单的ID生成
|
||||
taskMap.put("fileName", extractFileName(task.getFileUrl()));
|
||||
taskMap.put("printer", task.getPrinter());
|
||||
taskMap.put("status", task.getStatus());
|
||||
taskMap.put("createTime", task.getQueuedTime());
|
||||
taskMap.put("fileUrl", task.getFileUrl());
|
||||
return taskMap;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取统计信息
|
||||
*
|
||||
* @return 统计信息
|
||||
*/
|
||||
@GetMapping("statistics")
|
||||
public Map<String, Object> getStatistics() {
|
||||
List<com.goeing.printserver.main.domain.PrintTask> allHistory =
|
||||
printQueueService.getHistoryService().getAllHistory();
|
||||
|
||||
long totalTasks = allHistory.size();
|
||||
long completedTasks = allHistory.stream()
|
||||
.filter(task -> "completed".equals(task.getStatus()))
|
||||
.count();
|
||||
long failedTasks = allHistory.stream()
|
||||
.filter(task -> "failed".equals(task.getStatus()))
|
||||
.count();
|
||||
|
||||
Map<String, Object> stats = new HashMap<>();
|
||||
stats.put("totalTasks", totalTasks);
|
||||
stats.put("completedTasks", completedTasks);
|
||||
stats.put("failedTasks", failedTasks);
|
||||
stats.put("queueSize", printQueueService.getQueueSize());
|
||||
stats.put("uptime", getUptime());
|
||||
return stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统设置
|
||||
*
|
||||
* @return 系统设置
|
||||
*/
|
||||
@GetMapping("settings")
|
||||
public Map<String, Object> getSystemSettings() {
|
||||
Map<String, Object> settings = new HashMap<>();
|
||||
settings.put("defaultPrinter", config.getDefaultPrinter() != null ? config.getDefaultPrinter() : "");
|
||||
settings.put("maxQueueSize", printQueueService.getMaxQueueSize());
|
||||
settings.put("enableNotifications", config.isEnableNotifications());
|
||||
settings.put("autoStart", config.isAutoStart());
|
||||
settings.put("websocketUrl", config.getWebsocketUrl() != null ? config.getWebsocketUrl() : "ws://localhost:8080/ws");
|
||||
settings.put("printerId", config.getPrinterId() != null ? config.getPrinterId() : "PRINTER_001");
|
||||
settings.put("apiKey", config.getApiKey() != null ? config.getApiKey() : "****-****-****-****");
|
||||
return settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存系统设置
|
||||
*
|
||||
* @param settings 设置参数
|
||||
* @return 保存结果
|
||||
*/
|
||||
@PostMapping("settings")
|
||||
public Map<String, String> saveSystemSettings(@RequestBody Map<String, Object> settings) {
|
||||
try {
|
||||
// 更新最大队列大小
|
||||
if (settings.containsKey("maxQueueSize")) {
|
||||
int maxQueueSize = (Integer) settings.get("maxQueueSize");
|
||||
printQueueService.setMaxQueueSize(maxQueueSize);
|
||||
}
|
||||
|
||||
// 这里可以添加其他设置的保存逻辑
|
||||
log.info("系统设置已保存: {}", settings);
|
||||
|
||||
Map<String, String> result = new HashMap<>();
|
||||
result.put("status", "success");
|
||||
result.put("message", "设置保存成功");
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error("保存系统设置失败", e);
|
||||
Map<String, String> result = new HashMap<>();
|
||||
result.put("status", "error");
|
||||
result.put("message", "保存设置失败: " + e.getMessage());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统日志
|
||||
*
|
||||
* @return 系统日志列表
|
||||
*/
|
||||
@GetMapping("logs")
|
||||
public List<Map<String, Object>> getSystemLogs() {
|
||||
// 这里返回模拟的日志数据,实际项目中可以集成日志框架
|
||||
List<Map<String, Object>> logs = new ArrayList<>();
|
||||
|
||||
Map<String, Object> log1 = new HashMap<>();
|
||||
log1.put("level", "info");
|
||||
log1.put("time", LocalDateTime.now().minusHours(1).toString());
|
||||
log1.put("message", "打印服务启动成功");
|
||||
logs.add(log1);
|
||||
|
||||
Map<String, Object> log2 = new HashMap<>();
|
||||
log2.put("level", "info");
|
||||
log2.put("time", LocalDateTime.now().minusMinutes(30).toString());
|
||||
log2.put("message", "连接到打印机: " + (config.getDefaultPrinter() != null ? config.getDefaultPrinter() : "默认打印机"));
|
||||
logs.add(log2);
|
||||
|
||||
Map<String, Object> log3 = new HashMap<>();
|
||||
log3.put("level", "info");
|
||||
log3.put("time", LocalDateTime.now().minusMinutes(10).toString());
|
||||
log3.put("message", "当前队列大小: " + printQueueService.getQueueSize());
|
||||
logs.add(log3);
|
||||
|
||||
return logs;
|
||||
}
|
||||
|
||||
@PostMapping("print")
|
||||
public String print(@RequestBody PrintRequest request) {
|
||||
// 记录请求信息
|
||||
@ -167,4 +331,50 @@ public class PrintController implements PrintService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从文件URL中提取文件名
|
||||
*
|
||||
* @param fileUrl 文件URL
|
||||
* @return 文件名
|
||||
*/
|
||||
private String extractFileName(String fileUrl) {
|
||||
if (fileUrl == null || fileUrl.isEmpty()) {
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
// 从URL中提取文件名
|
||||
String fileName = fileUrl;
|
||||
int lastSlashIndex = fileName.lastIndexOf('/');
|
||||
if (lastSlashIndex >= 0 && lastSlashIndex < fileName.length() - 1) {
|
||||
fileName = fileName.substring(lastSlashIndex + 1);
|
||||
}
|
||||
|
||||
// 如果文件名为空,返回默认值
|
||||
return fileName.isEmpty() ? "Unknown" : fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统运行时间
|
||||
*
|
||||
* @return 运行时间字符串
|
||||
*/
|
||||
private String getUptime() {
|
||||
long uptimeMillis = System.currentTimeMillis() - startTime;
|
||||
long seconds = uptimeMillis / 1000;
|
||||
long minutes = seconds / 60;
|
||||
long hours = minutes / 60;
|
||||
long days = hours / 24;
|
||||
|
||||
if (days > 0) {
|
||||
return String.format("%d天 %d小时 %d分钟", days, hours % 24, minutes % 60);
|
||||
} else if (hours > 0) {
|
||||
return String.format("%d小时 %d分钟", hours, minutes % 60);
|
||||
} else {
|
||||
return String.format("%d分钟", minutes);
|
||||
}
|
||||
}
|
||||
|
||||
// 记录服务启动时间
|
||||
private static final long startTime = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
@ -350,6 +350,15 @@ public class PrintQueueService {
|
||||
return maxQueueSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取历史服务实例
|
||||
*
|
||||
* @return 历史服务
|
||||
*/
|
||||
public PrintHistoryService getHistoryService() {
|
||||
return historyService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭打印任务处理线程池
|
||||
*/
|
||||
|
||||
@ -17,3 +17,8 @@ app.force.headless=false
|
||||
logging.level.com.goeing.printserver.main.gui.PrinterStatusPanel=WARN
|
||||
logging.level.com.goeing.printserver.main.gui.PrintSettingsPanel=WARN
|
||||
|
||||
# Actuator 健康检测配置
|
||||
management.endpoints.web.exposure.include=health,info
|
||||
management.endpoint.health.show-details=when-authorized
|
||||
management.endpoints.web.base-path=/actuator
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user