log.uts
4.05 KB
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
import services from "@/services";
import { uploadLogs } from "@/services/logger/LogUploadService.uts";
import { checkFileExists, deleteFile, readDirectory } from "@/utils/plus-io.uts";
import { WsCommand } from "../../constants.uts";
import { asNumber, asString, sendError, sendOk, WsCommandRouter } from "../CommandCore.uts";
function parseDatePart(fileName: string): Date | null {
if (!fileName.startsWith("log_") || !fileName.endsWith(".txt")) {
return null;
}
const dateText = fileName.slice(4, 14);
const d = new Date(`${dateText}T00:00:00`);
if (Number.isNaN(d.getTime())) {
return null;
}
return d;
}
async function removeExpiredLogFiles(days: number): Promise<number> {
const dirPath = services.logger.getLogDirPath();
const exists = await checkFileExists(dirPath);
if (!exists) {
return 0;
}
const names = await readDirectory(dirPath).catch((): string[] => []);
if (names.length == 0) {
return 0;
}
const cutoff = new Date();
cutoff.setHours(0, 0, 0, 0);
cutoff.setDate(cutoff.getDate() - days);
let removed = 0;
for (const name of names) {
const date = parseDatePart(name);
if (date == null) {
continue;
}
if (date.getTime() < cutoff.getTime()) {
const path = `${dirPath}/${name}`;
await deleteFile(path).then((): void => {
removed += 1;
}).catch((): void => {});
}
}
return removed;
}
export function registerLogHandlers(router: WsCommandRouter): void {
router.register(WsCommand.GetLogList as string, async (ctx, payload): Promise<void> => {
const date = asString(payload.date);
if (date != "") {
const files = await services.logger.getLogFilesByDate(date);
await sendOk(ctx, "获取日志列表成功", { logList: files }, payload);
return;
}
const days = asNumber(payload.days, 7);
const files = await services.logger.getLogFiles(days);
await sendOk(ctx, "获取日志列表成功", { logList: files }, payload);
});
router.register(WsCommand.RemoveLog as string, async (ctx, payload): Promise<void> => {
const days = asNumber(payload.days, 0);
if (days <= 0) {
await sendError(ctx, "删除日志失败,days 必须大于0", payload);
return;
}
const count = await removeExpiredLogFiles(days);
await sendOk(ctx, "删除日志成功", { removed: count, days }, payload);
});
router.register(WsCommand.GetLog as string, async (ctx, payload): Promise<void> => {
const days = asNumber(payload.days, 7);
const serverUrl = asString(payload.serverUrl);
const payloadDeviceId = asNumber(payload.deviceId, 0);
const deviceIdNum = payloadDeviceId > 0 ? payloadDeviceId : asNumber(ctx.deviceId, 0);
if (deviceIdNum <= 0) {
await sendError(ctx, "日志上传失败,deviceId 无效", payload);
return;
}
try {
const result = await uploadLogs({
days,
deviceId: deviceIdNum,
serverUrl
});
// ✅ 只返回上传成功的最后10个服务器地址
const allUrls = result.uploadedUrls;
const totalCount = allUrls.length;
// 如果超过10个,截取最后10个;不足10个则全部返回
const startIndex = totalCount > 10 ? totalCount - 10 : 0;
const lastTenUrls = allUrls.slice(startIndex);
const responseObj = {
uploadedUrls: lastTenUrls,
totalCount: totalCount,
successCount: result.successCount,
failCount: result.failCount
} as UTSJSONObject;
await sendOk(ctx, "日志上传完成", responseObj, payload);
} catch (error: any) {
await sendError(ctx, `日志上传失败: ${error}`, payload);
}
});
router.register(WsCommand.LiveLog as string, async (ctx, payload): Promise<void> => {
const action = asString(payload.action, "start");
if (action == "start") {
await ctx.startLiveLog(async (line: string): Promise<void> => {
await ctx.send({
command: WsCommand.LiveLog,
type: "liveLog",
data: { line }
} as UTSJSONObject, "private");
});
await sendOk(ctx, "实时日志已开启", null, payload);
return;
}
if (action == "stop") {
ctx.stopLiveLog();
await sendOk(ctx, "实时日志已关闭", null, payload);
return;
}
await sendError(ctx, `未知动作: ${action}`, payload);
});
}