|
|
@@ -24,6 +24,7 @@
|
|
|
projectInfo: null,
|
|
|
activeFile: null,
|
|
|
resources: { categories: [], activeCategory: null, detail: null, output: '', busy: false },
|
|
|
+ log: { initialized: false, mode: 'text', listening: false, lines: [] },
|
|
|
mcp: null,
|
|
|
};
|
|
|
|
|
|
@@ -89,7 +90,7 @@
|
|
|
|
|
|
/* ── Navigation ─────────────────────────────────────────────── */
|
|
|
|
|
|
- const VIEWS = ['flash', 'projects', 'resources', 'mcp'];
|
|
|
+ const VIEWS = ['flash', 'log', 'projects', 'resources', 'mcp'];
|
|
|
|
|
|
function showView(name) {
|
|
|
if (!VIEWS.includes(name)) return;
|
|
|
@@ -106,11 +107,235 @@
|
|
|
const flashTop = $('#btn-flash-top');
|
|
|
if (flashTop) flashTop.style.display = name === 'flash' ? '' : 'none';
|
|
|
|
|
|
+ if (name === 'log') initSerialLogView().catch((error) => toast(error.message, 'err'));
|
|
|
if (name === 'projects') loadProjects().catch((error) => toast(error.message, 'err'));
|
|
|
if (name === 'resources') loadResources().catch((error) => toast(error.message, 'err'));
|
|
|
if (name === 'mcp') loadMcp().catch((error) => toast(error.message, 'err'));
|
|
|
}
|
|
|
|
|
|
+ /* ── Serial log ─────────────────────────────────────────────── */
|
|
|
+
|
|
|
+ function serialLogLevel(level) {
|
|
|
+ const value = String(level || '').toLowerCase();
|
|
|
+ if (value.includes('error') || value.includes('fatal')) return 'err';
|
|
|
+ if (value.includes('warn')) return 'warn';
|
|
|
+ if (value.includes('debug') || value.includes('verbose')) return 'muted';
|
|
|
+ return 'info';
|
|
|
+ }
|
|
|
+
|
|
|
+ function appendSerialLog(text, level = 'info') {
|
|
|
+ if (!text) return;
|
|
|
+ const log = $('#serial-log');
|
|
|
+ if (!log) return;
|
|
|
+ // eslint-disable-next-line no-control-regex
|
|
|
+ const cleanText = String(text).replace(/\u001b\[[0-9;]*m/g, '');
|
|
|
+ const lines = cleanText.replace(/\r/g, '').split('\n').filter((line) => line.length > 0);
|
|
|
+ for (const raw of lines) {
|
|
|
+ const line = `[${new Date().toTimeString().slice(0, 8)}] ${raw}`;
|
|
|
+ state.log.lines.push(line);
|
|
|
+ if (state.log.lines.length > 5000) state.log.lines.splice(0, state.log.lines.length - 5000);
|
|
|
+
|
|
|
+ const span = document.createElement('span');
|
|
|
+ span.className = 'line';
|
|
|
+ const time = document.createElement('span');
|
|
|
+ time.className = 't';
|
|
|
+ time.textContent = new Date().toTimeString().slice(0, 8);
|
|
|
+ const body = document.createElement('span');
|
|
|
+ body.className = level;
|
|
|
+ body.textContent = raw;
|
|
|
+ span.append(time, body);
|
|
|
+ log.appendChild(span);
|
|
|
+ while (log.children.length > 5000) log.firstElementChild?.remove();
|
|
|
+ }
|
|
|
+ const count = state.log.lines.length;
|
|
|
+ $('#log-count').textContent = `${count} 行`;
|
|
|
+ const nearBottom = log.scrollHeight - log.scrollTop - log.clientHeight < 80;
|
|
|
+ if (nearBottom) log.scrollTop = log.scrollHeight;
|
|
|
+ }
|
|
|
+
|
|
|
+ function appendLogEntry(entry = {}) {
|
|
|
+ const time = entry.device_time || entry.timestamp || '';
|
|
|
+ const module = entry.module || entry.tag || '';
|
|
|
+ const level = String(entry.level || 'info');
|
|
|
+ const message = entry.message || entry.line || JSON.stringify(entry);
|
|
|
+ const prefix = [time, level, module].filter(Boolean).join(' ');
|
|
|
+ appendSerialLog(prefix ? `${prefix} ${message}` : message, serialLogLevel(level));
|
|
|
+ }
|
|
|
+
|
|
|
+ function updateLogButtons() {
|
|
|
+ const listening = state.log.listening;
|
|
|
+ $('#btn-log-start').classList.toggle('hidden', listening);
|
|
|
+ $('#btn-log-stop').classList.toggle('hidden', !listening);
|
|
|
+ $('#log-port').disabled = listening;
|
|
|
+ $('#log-baud').disabled = listening;
|
|
|
+ $('#btn-log-refresh').disabled = listening;
|
|
|
+ $$('#log-mode button').forEach((button) => {
|
|
|
+ button.disabled = listening;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ function setLogMode(mode) {
|
|
|
+ state.log.mode = mode;
|
|
|
+ $$('#log-mode button').forEach((button) => {
|
|
|
+ const active = button.dataset.mode === mode;
|
|
|
+ button.classList.toggle('active', active);
|
|
|
+ button.setAttribute('aria-selected', active ? 'true' : 'false');
|
|
|
+ });
|
|
|
+ const probe = $('#log-probe');
|
|
|
+ if (probe) probe.disabled = mode !== 'binary';
|
|
|
+ if (mode === 'binary') {
|
|
|
+ $('#log-baud').value = '2000000';
|
|
|
+ } else {
|
|
|
+ $('#log-baud').value = '921600';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async function refreshLogPorts() {
|
|
|
+ const select = $('#log-port');
|
|
|
+ if (!select) return;
|
|
|
+ select.textContent = '';
|
|
|
+ const result = await api.listPorts();
|
|
|
+ const ports = result?.ok ? result.ports || [] : [];
|
|
|
+ if (!ports.length) {
|
|
|
+ const option = document.createElement('option');
|
|
|
+ option.value = '';
|
|
|
+ option.textContent = result?.ok ? '未发现串口' : `扫描失败:${result?.error || ''}`;
|
|
|
+ select.appendChild(option);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (const port of ports) {
|
|
|
+ const option = document.createElement('option');
|
|
|
+ option.value = port.port_name || '';
|
|
|
+ const bits = [port.port_name];
|
|
|
+ if (port.product) bits.push(port.product);
|
|
|
+ option.textContent = bits.join(' · ');
|
|
|
+ select.appendChild(option);
|
|
|
+ }
|
|
|
+ const preferred = window.__luatos?.state?.port;
|
|
|
+ if (preferred && ports.some((port) => port.port_name === preferred)) {
|
|
|
+ select.value = preferred;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async function startSerialLog() {
|
|
|
+ if (state.log.listening) return;
|
|
|
+ const port = $('#log-port').value;
|
|
|
+ if (!port) {
|
|
|
+ toast('请选择串口', 'err');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const mode = state.log.mode;
|
|
|
+ const baud = $('#log-baud').value;
|
|
|
+ const args = ['log', mode === 'binary' ? 'view-binary' : 'view', '--port', port, '--baud', baud];
|
|
|
+ if (mode === 'binary' && $('#log-probe').checked) args.push('--probe');
|
|
|
+ if ($('#log-smart').checked) args.push('--smart');
|
|
|
+ if ($('#log-rts').checked) args.push('--rts-reset');
|
|
|
+
|
|
|
+ state.log.listening = true;
|
|
|
+ state.log.stopping = false;
|
|
|
+ updateLogButtons();
|
|
|
+ $('#log-status').textContent = '正在连接…';
|
|
|
+ appendSerialLog(`开始监听 ${port} @ ${baud} bps(${mode === 'binary' ? '二进制' : '文本'})`, 'cmd');
|
|
|
+ try {
|
|
|
+ const result = await api.startCliStream('log', args);
|
|
|
+ if (result?.error && !state.log.stopping) {
|
|
|
+ appendSerialLog(result.error, 'err');
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ appendSerialLog(error.message || String(error), 'err');
|
|
|
+ toast('串口日志启动失败', 'err');
|
|
|
+ } finally {
|
|
|
+ state.log.listening = false;
|
|
|
+ updateLogButtons();
|
|
|
+ if (!state.log.stopping) $('#log-status').textContent = '已停止';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async function stopSerialLog() {
|
|
|
+ if (!state.log.listening) return;
|
|
|
+ state.log.stopping = true;
|
|
|
+ $('#log-status').textContent = '正在停止…';
|
|
|
+ const result = await api.cancelCliStream('log');
|
|
|
+ if (!result?.ok) {
|
|
|
+ appendSerialLog(result?.error || '停止失败', 'err');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async function saveSerialLog() {
|
|
|
+ if (!state.log.lines.length) {
|
|
|
+ toast('日志为空', 'err');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const result = await api.saveLog(state.log.lines.join('\n'));
|
|
|
+ if (result?.ok) {
|
|
|
+ toast(`日志已保存:${result.path}`, 'ok');
|
|
|
+ } else if (result?.error) {
|
|
|
+ toast(result.error, 'err');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function clearSerialLog() {
|
|
|
+ state.log.lines = [];
|
|
|
+ const log = $('#serial-log');
|
|
|
+ if (log) log.textContent = '';
|
|
|
+ $('#log-count').textContent = '0 行';
|
|
|
+ }
|
|
|
+
|
|
|
+ function bindSerialLogEvents() {
|
|
|
+ $('#btn-log-start').addEventListener('click', () => startSerialLog().catch((error) => toast(error.message, 'err')));
|
|
|
+ $('#btn-log-stop').addEventListener('click', () => stopSerialLog().catch((error) => toast(error.message, 'err')));
|
|
|
+ $('#btn-log-refresh').addEventListener('click', () => refreshLogPorts().catch((error) => toast(error.message, 'err')));
|
|
|
+ $('#btn-log-clear').addEventListener('click', clearSerialLog);
|
|
|
+ $('#btn-log-save').addEventListener('click', () => saveSerialLog().catch((error) => toast(error.message, 'err')));
|
|
|
+ $('#log-mode').addEventListener('click', (event) => {
|
|
|
+ const button = event.target.closest('button[data-mode]');
|
|
|
+ if (!button || state.log.listening) return;
|
|
|
+ setLogMode(button.dataset.mode);
|
|
|
+ });
|
|
|
+
|
|
|
+ api.onCliEvent('log', (event) => {
|
|
|
+ if (!event || typeof event !== 'object') return;
|
|
|
+ if (event.type === 'log_entry') {
|
|
|
+ appendLogEntry(event.entry || {});
|
|
|
+ } else if (event.type === 'message') {
|
|
|
+ appendSerialLog(event.message || '', serialLogLevel(event.level));
|
|
|
+ } else if (event.type === 'diagnostic') {
|
|
|
+ const diagnostic = event.diagnostic || {};
|
|
|
+ appendSerialLog(`[诊断] ${diagnostic.message || JSON.stringify(diagnostic)}`, 'warn');
|
|
|
+ } else if (event.type === 'error') {
|
|
|
+ appendSerialLog(event.message || '日志错误', 'err');
|
|
|
+ } else if (event.type === 'result') {
|
|
|
+ appendSerialLog(`日志会话结束:${event.status}`, event.status === 'ok' ? 'ok' : 'warn');
|
|
|
+ }
|
|
|
+ });
|
|
|
+ api.onCliLine('log', ({ line, source }) => {
|
|
|
+ if (line) appendSerialLog(line, source === 'stderr' ? 'muted' : 'info');
|
|
|
+ });
|
|
|
+ api.onCliExit('log', ({ code, signal, error }) => {
|
|
|
+ state.log.listening = false;
|
|
|
+ state.log.stopping = false;
|
|
|
+ updateLogButtons();
|
|
|
+ if (error) {
|
|
|
+ appendSerialLog(error, 'err');
|
|
|
+ $('#log-status').textContent = '异常退出';
|
|
|
+ } else {
|
|
|
+ const suffix = signal ? `signal=${signal}` : `code=${code}`;
|
|
|
+ $('#log-status').textContent = `已停止(${suffix})`;
|
|
|
+ appendSerialLog(`监听结束(${suffix})`, code === 0 ? 'ok' : 'warn');
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ async function initSerialLogView() {
|
|
|
+ if (!state.log.initialized) {
|
|
|
+ state.log.initialized = true;
|
|
|
+ bindSerialLogEvents();
|
|
|
+ updateLogButtons();
|
|
|
+ setLogMode('text');
|
|
|
+ await refreshLogPorts();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/* ── Projects ───────────────────────────────────────────────── */
|
|
|
|
|
|
function projectLog(text, level = 'info') {
|
|
|
@@ -823,6 +1048,7 @@
|
|
|
'guide_models',
|
|
|
'firmware_info',
|
|
|
'flash_firmware',
|
|
|
+ 'serial_log',
|
|
|
'resource_list',
|
|
|
'resource_download',
|
|
|
'project_list',
|