639 lines
21 KiB
C++
639 lines
21 KiB
C++
#ifndef SERIAL_TERMINAL_H
|
||
#define SERIAL_TERMINAL_H
|
||
|
||
const char serial_terminal_html[] PROGMEM = R"rawliteral(
|
||
<!DOCTYPE html>
|
||
<html>
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>Serial Terminal - Byun CAN Logger</title>
|
||
<style>
|
||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||
body {
|
||
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
min-height: 100vh;
|
||
padding: 10px;
|
||
}
|
||
.container {
|
||
max-width: 1400px;
|
||
margin: 0 auto;
|
||
background: white;
|
||
border-radius: 15px;
|
||
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
|
||
overflow: hidden;
|
||
}
|
||
.header {
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
color: white;
|
||
padding: 20px;
|
||
text-align: center;
|
||
}
|
||
.header h1 { font-size: 1.8em; margin-bottom: 5px; }
|
||
.header p { opacity: 0.9; font-size: 0.9em; }
|
||
.nav {
|
||
background: #2c3e50;
|
||
padding: 10px;
|
||
display: flex;
|
||
gap: 10px;
|
||
flex-wrap: wrap;
|
||
justify-content: center;
|
||
}
|
||
.nav a {
|
||
color: white;
|
||
text-decoration: none;
|
||
padding: 10px 15px;
|
||
border-radius: 5px;
|
||
transition: all 0.3s;
|
||
font-size: 0.9em;
|
||
}
|
||
.nav a:hover { background: #34495e; }
|
||
.nav a.active { background: #3498db; }
|
||
.content { padding: 20px; }
|
||
|
||
.controls {
|
||
display: grid;
|
||
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
||
gap: 15px;
|
||
margin-bottom: 20px;
|
||
background: #f8f9fa;
|
||
padding: 20px;
|
||
border-radius: 10px;
|
||
}
|
||
|
||
.control-group {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 8px;
|
||
}
|
||
|
||
.control-group label {
|
||
font-weight: 600;
|
||
color: #2c3e50;
|
||
font-size: 0.9em;
|
||
}
|
||
|
||
.control-group select,
|
||
.control-group input {
|
||
padding: 10px;
|
||
border: 2px solid #ddd;
|
||
border-radius: 8px;
|
||
font-size: 1em;
|
||
transition: all 0.3s;
|
||
}
|
||
|
||
.control-group select:focus,
|
||
.control-group input:focus {
|
||
outline: none;
|
||
border-color: #667eea;
|
||
}
|
||
|
||
.btn {
|
||
padding: 12px 24px;
|
||
border: none;
|
||
border-radius: 8px;
|
||
font-size: 1em;
|
||
font-weight: 600;
|
||
cursor: pointer;
|
||
transition: all 0.3s;
|
||
text-align: center;
|
||
}
|
||
|
||
.btn-primary {
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
color: white;
|
||
}
|
||
|
||
.btn-primary:hover {
|
||
transform: translateY(-2px);
|
||
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
|
||
}
|
||
|
||
.btn-success {
|
||
background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
|
||
color: white;
|
||
}
|
||
|
||
.btn-success:hover {
|
||
transform: translateY(-2px);
|
||
box-shadow: 0 4px 12px rgba(17, 153, 142, 0.4);
|
||
}
|
||
|
||
.btn-danger {
|
||
background: linear-gradient(135deg, #ff6b6b 0%, #ee5a6f 100%);
|
||
color: white;
|
||
}
|
||
|
||
.btn-danger:hover {
|
||
transform: translateY(-2px);
|
||
box-shadow: 0 4px 12px rgba(255, 107, 107, 0.4);
|
||
}
|
||
|
||
.btn-warning {
|
||
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
|
||
color: white;
|
||
}
|
||
|
||
.btn-secondary {
|
||
background: #95a5a6;
|
||
color: white;
|
||
}
|
||
|
||
.btn-group {
|
||
display: flex;
|
||
gap: 10px;
|
||
flex-wrap: wrap;
|
||
}
|
||
|
||
.terminal-container {
|
||
background: #1e1e1e;
|
||
border-radius: 10px;
|
||
padding: 15px;
|
||
margin-bottom: 20px;
|
||
box-shadow: inset 0 2px 10px rgba(0,0,0,0.5);
|
||
}
|
||
|
||
.terminal-output {
|
||
height: 400px;
|
||
overflow-y: auto;
|
||
font-family: 'Courier New', monospace;
|
||
font-size: 0.9em;
|
||
color: #0f0;
|
||
background: #000;
|
||
padding: 10px;
|
||
border-radius: 5px;
|
||
white-space: pre-wrap;
|
||
word-wrap: break-word;
|
||
}
|
||
|
||
.terminal-output::-webkit-scrollbar {
|
||
width: 8px;
|
||
}
|
||
|
||
.terminal-output::-webkit-scrollbar-track {
|
||
background: #2c3e50;
|
||
border-radius: 4px;
|
||
}
|
||
|
||
.terminal-output::-webkit-scrollbar-thumb {
|
||
background: #667eea;
|
||
border-radius: 4px;
|
||
}
|
||
|
||
.terminal-line {
|
||
margin-bottom: 2px;
|
||
line-height: 1.4;
|
||
}
|
||
|
||
.terminal-line.tx {
|
||
color: #ff6b6b;
|
||
}
|
||
|
||
.terminal-line.rx {
|
||
color: #38ef7d;
|
||
}
|
||
|
||
.terminal-timestamp {
|
||
color: #95a5a6;
|
||
margin-right: 8px;
|
||
}
|
||
|
||
.input-area {
|
||
display: flex;
|
||
gap: 10px;
|
||
margin-top: 15px;
|
||
}
|
||
|
||
.input-area input {
|
||
flex: 1;
|
||
padding: 12px;
|
||
border: 2px solid #ddd;
|
||
border-radius: 8px;
|
||
font-family: 'Courier New', monospace;
|
||
font-size: 1em;
|
||
}
|
||
|
||
.input-area input:focus {
|
||
outline: none;
|
||
border-color: #667eea;
|
||
}
|
||
|
||
.stats-grid {
|
||
display: grid;
|
||
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
|
||
gap: 15px;
|
||
margin-bottom: 20px;
|
||
}
|
||
|
||
.stat-card {
|
||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||
color: white;
|
||
padding: 15px;
|
||
border-radius: 10px;
|
||
text-align: center;
|
||
}
|
||
|
||
.stat-label {
|
||
font-size: 0.85em;
|
||
opacity: 0.9;
|
||
margin-bottom: 5px;
|
||
}
|
||
|
||
.stat-value {
|
||
font-size: 1.5em;
|
||
font-weight: 700;
|
||
font-family: 'Courier New', monospace;
|
||
}
|
||
|
||
.status-indicator {
|
||
display: inline-block;
|
||
width: 12px;
|
||
height: 12px;
|
||
border-radius: 50%;
|
||
margin-right: 8px;
|
||
}
|
||
|
||
.status-indicator.on {
|
||
background: #38ef7d;
|
||
box-shadow: 0 0 8px #38ef7d;
|
||
}
|
||
|
||
.status-indicator.off {
|
||
background: #95a5a6;
|
||
}
|
||
|
||
.status-text {
|
||
display: flex;
|
||
align-items: center;
|
||
font-weight: 600;
|
||
margin-bottom: 15px;
|
||
}
|
||
|
||
@media (max-width: 768px) {
|
||
.controls {
|
||
grid-template-columns: 1fr;
|
||
}
|
||
.terminal-output {
|
||
height: 300px;
|
||
font-size: 0.8em;
|
||
}
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="container">
|
||
<div class="header">
|
||
<h1>📡 Serial Terminal</h1>
|
||
<p>RS232 Communication Interface</p>
|
||
</div>
|
||
|
||
<div class="nav">
|
||
<a href="/">🏠 Monitor</a>
|
||
<a href="/transmit">📤 Transmit</a>
|
||
<a href="/graph">📊 Graph</a>
|
||
<a href="/serial" class="active">📡 Serial</a>
|
||
<a href="/settings">⚙️ Settings</a>
|
||
</div>
|
||
|
||
<div class="content">
|
||
<!-- 상태 통계 -->
|
||
<div class="stats-grid">
|
||
<div class="stat-card">
|
||
<div class="stat-label">Received</div>
|
||
<div class="stat-value" id="rx-count">0</div>
|
||
</div>
|
||
<div class="stat-card">
|
||
<div class="stat-label">Transmitted</div>
|
||
<div class="stat-value" id="tx-count">0</div>
|
||
</div>
|
||
<div class="stat-card">
|
||
<div class="stat-label">Logging Size</div>
|
||
<div class="stat-value" id="log-size">0 KB</div>
|
||
</div>
|
||
<div class="stat-card">
|
||
<div class="stat-label">Queue Usage</div>
|
||
<div class="stat-value" id="queue-usage">0%</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- Serial 설정 -->
|
||
<div class="controls">
|
||
<div class="control-group">
|
||
<label>Baud Rate</label>
|
||
<select id="baudrate">
|
||
<option value="9600">9600</option>
|
||
<option value="19200">19200</option>
|
||
<option value="38400">38400</option>
|
||
<option value="57600">57600</option>
|
||
<option value="115200" selected>115200</option>
|
||
<option value="230400">230400</option>
|
||
<option value="460800">460800</option>
|
||
<option value="921600">921600</option>
|
||
</select>
|
||
</div>
|
||
|
||
<div class="control-group">
|
||
<label>Data Bits</label>
|
||
<select id="databits">
|
||
<option value="5">5</option>
|
||
<option value="6">6</option>
|
||
<option value="7">7</option>
|
||
<option value="8" selected>8</option>
|
||
</select>
|
||
</div>
|
||
|
||
<div class="control-group">
|
||
<label>Parity</label>
|
||
<select id="parity">
|
||
<option value="0" selected>None</option>
|
||
<option value="1">Even</option>
|
||
<option value="2">Odd</option>
|
||
</select>
|
||
</div>
|
||
|
||
<div class="control-group">
|
||
<label>Stop Bits</label>
|
||
<select id="stopbits">
|
||
<option value="1" selected>1</option>
|
||
<option value="2">2</option>
|
||
</select>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 버튼 그룹 -->
|
||
<div class="btn-group" style="margin-bottom: 20px;">
|
||
<button class="btn btn-primary" onclick="applySerialConfig()">Apply Settings</button>
|
||
<button class="btn btn-success" id="log-btn" onclick="toggleSerialLogging()">
|
||
<span class="status-indicator off"></span>
|
||
Start Logging
|
||
</button>
|
||
<button class="btn btn-warning" onclick="clearTerminal()">Clear Terminal</button>
|
||
<button class="btn btn-secondary" onclick="autoScroll = !autoScroll; updateAutoScrollBtn()">
|
||
<span id="autoscroll-text">Auto-scroll: ON</span>
|
||
</button>
|
||
</div>
|
||
|
||
<!-- 터미널 -->
|
||
<div class="terminal-container">
|
||
<div class="status-text">
|
||
<span class="status-indicator" id="logging-status"></span>
|
||
<span id="logging-text">Serial Terminal Ready</span>
|
||
</div>
|
||
<div class="terminal-output" id="terminal"></div>
|
||
<div class="input-area">
|
||
<input type="text" id="serial-input" placeholder="Type command and press Enter..."
|
||
onkeypress="handleKeyPress(event)">
|
||
<button class="btn btn-primary" onclick="sendSerialData()">Send</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
||
<script>
|
||
let ws;
|
||
let autoScroll = true;
|
||
let serialLogging = false;
|
||
let totalRx = 0;
|
||
let totalTx = 0;
|
||
let logSize = 0;
|
||
let queueUsed = 0;
|
||
let queueSize = 100; // 200 → 100으로 변경
|
||
|
||
function initWebSocket() {
|
||
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||
const wsUrl = protocol + '//' + window.location.hostname + ':81';
|
||
|
||
ws = new WebSocket(wsUrl);
|
||
|
||
ws.onopen = function() {
|
||
console.log('WebSocket 연결됨');
|
||
requestSerialConfig();
|
||
};
|
||
|
||
ws.onmessage = function(event) {
|
||
try {
|
||
const data = JSON.parse(event.data);
|
||
|
||
if (data.type === 'update') {
|
||
updateStats(data);
|
||
|
||
// Serial 메시지 표시
|
||
if (data.serialMessages && data.serialMessages.length > 0) {
|
||
data.serialMessages.forEach(msg => {
|
||
addTerminalLine(msg);
|
||
});
|
||
}
|
||
} else if (data.type === 'serialConfig') {
|
||
loadSerialConfig(data);
|
||
}
|
||
} catch (e) {
|
||
console.error('메시지 파싱 오류:', e);
|
||
}
|
||
};
|
||
|
||
ws.onerror = function(error) {
|
||
console.error('WebSocket 오류:', error);
|
||
};
|
||
|
||
ws.onclose = function() {
|
||
console.log('WebSocket 연결 종료');
|
||
setTimeout(initWebSocket, 2000);
|
||
};
|
||
}
|
||
|
||
function requestSerialConfig() {
|
||
if (ws && ws.readyState === WebSocket.OPEN) {
|
||
ws.send(JSON.stringify({cmd: 'getSerialConfig'}));
|
||
}
|
||
}
|
||
|
||
function loadSerialConfig(config) {
|
||
document.getElementById('baudrate').value = config.baudRate || 115200;
|
||
document.getElementById('databits').value = config.dataBits || 8;
|
||
document.getElementById('parity').value = config.parity || 0;
|
||
document.getElementById('stopbits').value = config.stopBits || 1;
|
||
|
||
console.log('Serial 설정 로드됨:', config);
|
||
}
|
||
|
||
function applySerialConfig() {
|
||
const config = {
|
||
cmd: 'setSerialConfig',
|
||
baudRate: parseInt(document.getElementById('baudrate').value),
|
||
dataBits: parseInt(document.getElementById('databits').value),
|
||
parity: parseInt(document.getElementById('parity').value),
|
||
stopBits: parseInt(document.getElementById('stopbits').value)
|
||
};
|
||
|
||
if (ws && ws.readyState === WebSocket.OPEN) {
|
||
ws.send(JSON.stringify(config));
|
||
console.log('Serial 설정 적용:', config);
|
||
|
||
// 성공 피드백
|
||
const btn = event.target;
|
||
const originalText = btn.textContent;
|
||
btn.textContent = '✓ Applied!';
|
||
btn.style.background = 'linear-gradient(135deg, #11998e 0%, #38ef7d 100%)';
|
||
|
||
setTimeout(() => {
|
||
btn.textContent = originalText;
|
||
btn.style.background = '';
|
||
}, 2000);
|
||
}
|
||
}
|
||
|
||
function toggleSerialLogging() {
|
||
if (ws && ws.readyState === WebSocket.OPEN) {
|
||
if (serialLogging) {
|
||
ws.send(JSON.stringify({cmd: 'stopSerialLogging'}));
|
||
} else {
|
||
ws.send(JSON.stringify({cmd: 'startSerialLogging'}));
|
||
}
|
||
serialLogging = !serialLogging;
|
||
updateLoggingUI();
|
||
}
|
||
}
|
||
|
||
function updateLoggingUI() {
|
||
const btn = document.getElementById('log-btn');
|
||
const indicator = btn.querySelector('.status-indicator');
|
||
const status = document.getElementById('logging-status');
|
||
const text = document.getElementById('logging-text');
|
||
|
||
if (serialLogging) {
|
||
btn.innerHTML = '<span class="status-indicator on"></span>Stop Logging';
|
||
btn.className = 'btn btn-danger';
|
||
status.className = 'status-indicator on';
|
||
text.textContent = 'Logging Active';
|
||
} else {
|
||
btn.innerHTML = '<span class="status-indicator off"></span>Start Logging';
|
||
btn.className = 'btn btn-success';
|
||
status.className = 'status-indicator off';
|
||
text.textContent = 'Serial Terminal Ready';
|
||
}
|
||
}
|
||
|
||
function sendSerialData() {
|
||
const input = document.getElementById('serial-input');
|
||
const data = input.value.trim();
|
||
|
||
if (data.length === 0) return;
|
||
|
||
if (ws && ws.readyState === WebSocket.OPEN) {
|
||
ws.send(JSON.stringify({
|
||
cmd: 'sendSerial',
|
||
data: data
|
||
}));
|
||
|
||
input.value = '';
|
||
console.log('전송:', data);
|
||
}
|
||
}
|
||
|
||
function handleKeyPress(event) {
|
||
if (event.key === 'Enter') {
|
||
sendSerialData();
|
||
}
|
||
}
|
||
|
||
function addTerminalLine(msg) {
|
||
const terminal = document.getElementById('terminal');
|
||
const line = document.createElement('div');
|
||
line.className = 'terminal-line ' + (msg.isTx ? 'tx' : 'rx');
|
||
|
||
// 타임스탬프 포맷
|
||
const date = new Date(msg.timestamp / 1000);
|
||
const timestamp = date.toLocaleTimeString() + '.' +
|
||
String(date.getMilliseconds()).padStart(3, '0');
|
||
|
||
const direction = msg.isTx ? '[TX]' : '[RX]';
|
||
line.innerHTML = '<span class="terminal-timestamp">' + timestamp + '</span>' +
|
||
'<span style="font-weight:bold">' + direction + '</span> ' +
|
||
escapeHtml(msg.data);
|
||
|
||
terminal.appendChild(line);
|
||
|
||
// 자동 스크롤
|
||
if (autoScroll) {
|
||
terminal.scrollTop = terminal.scrollHeight;
|
||
}
|
||
|
||
// 최대 1000줄로 제한
|
||
while (terminal.children.length > 1000) {
|
||
terminal.removeChild(terminal.firstChild);
|
||
}
|
||
}
|
||
|
||
function escapeHtml(text) {
|
||
const map = {
|
||
'&': '&',
|
||
'<': '<',
|
||
'>': '>',
|
||
'"': '"',
|
||
"'": '''
|
||
};
|
||
return text.replace(/[&<>"']/g, m => map[m]);
|
||
}
|
||
|
||
function clearTerminal() {
|
||
document.getElementById('terminal').innerHTML = '';
|
||
console.log('터미널 초기화');
|
||
}
|
||
|
||
function updateAutoScrollBtn() {
|
||
const btn = event.target;
|
||
btn.querySelector('#autoscroll-text').textContent =
|
||
'Auto-scroll: ' + (autoScroll ? 'ON' : 'OFF');
|
||
|
||
if (autoScroll) {
|
||
btn.className = 'btn btn-secondary';
|
||
} else {
|
||
btn.className = 'btn btn-secondary';
|
||
btn.style.opacity = '0.7';
|
||
}
|
||
}
|
||
|
||
function updateStats(data) {
|
||
// RX/TX 카운터 업데이트
|
||
if (data.totalSerialRx !== undefined) {
|
||
totalRx = data.totalSerialRx;
|
||
document.getElementById('rx-count').textContent = totalRx.toLocaleString();
|
||
}
|
||
|
||
if (data.totalSerialTx !== undefined) {
|
||
totalTx = data.totalSerialTx;
|
||
document.getElementById('tx-count').textContent = totalTx.toLocaleString();
|
||
}
|
||
|
||
// 로그 파일 크기
|
||
if (data.serialFileSize !== undefined) {
|
||
logSize = data.serialFileSize;
|
||
const sizeKB = (logSize / 1024).toFixed(1);
|
||
document.getElementById('log-size').textContent = sizeKB + ' KB';
|
||
}
|
||
|
||
// Queue 사용량
|
||
if (data.serialQueueUsed !== undefined && data.serialQueueSize !== undefined) {
|
||
queueUsed = data.serialQueueUsed;
|
||
queueSize = data.serialQueueSize;
|
||
const percentage = ((queueUsed / queueSize) * 100).toFixed(0);
|
||
document.getElementById('queue-usage').textContent = percentage + '%';
|
||
}
|
||
|
||
// 로깅 상태 동기화
|
||
if (data.serialLogging !== undefined && data.serialLogging !== serialLogging) {
|
||
serialLogging = data.serialLogging;
|
||
updateLoggingUI();
|
||
}
|
||
}
|
||
|
||
// 초기화
|
||
initWebSocket();
|
||
</script>
|
||
</body>
|
||
</html>
|
||
)rawliteral";
|
||
|
||
#endif |