459 lines
12 KiB
C++
459 lines
12 KiB
C++
#ifndef HTTP_SERVER_TESTPAGE_H
|
|
#define HTTP_SERVER_TESTPAGE_H
|
|
|
|
#include <string>
|
|
|
|
const std::string TESTPAGE_HTML = R"row(
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>测试页面</title>
|
|
<link rel="stylesheet" href="/static/style.css">
|
|
<style>
|
|
:root {
|
|
--primary-color: #2563eb;
|
|
--primary-hover: #1d4ed8;
|
|
--bg-color: #f8fafc;
|
|
--sidebar-width: 60px;
|
|
--sidebar-expanded: 220px;
|
|
--header-height: 36px;
|
|
}
|
|
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
|
|
background-color: var(--bg-color);
|
|
color: #1a1a1a;
|
|
}
|
|
|
|
.container {
|
|
display: grid;
|
|
grid-template-areas:
|
|
"sidebar main-content";
|
|
grid-template-columns: var(--sidebar-width) 1fr;
|
|
grid-template-rows: 1fr;
|
|
min-height: 100vh;
|
|
gap: 0;
|
|
}
|
|
|
|
.header {
|
|
grid-area: unset;
|
|
background: white;
|
|
padding: 0 0.5rem;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
|
|
position: fixed;
|
|
top: 0;
|
|
right: 0;
|
|
left: var(--sidebar-width);
|
|
height: var(--header-height);
|
|
z-index: 10;
|
|
}
|
|
|
|
.header h1 {
|
|
font-size: 1.25rem;
|
|
font-weight: 600;
|
|
color: #1a1a1a;
|
|
}
|
|
|
|
.sidebar {
|
|
grid-area: sidebar;
|
|
background: white;
|
|
width: var(--sidebar-width);
|
|
height: 100vh;
|
|
position: fixed;
|
|
left: 0;
|
|
top: 0;
|
|
padding: 0.5rem 0.25rem;
|
|
transition: width 0.3s ease;
|
|
box-shadow: 1px 0 3px rgba(0, 0, 0, 0.1);
|
|
z-index: 20;
|
|
}
|
|
|
|
.sidebar:hover {
|
|
width: var(--sidebar-expanded);
|
|
}
|
|
|
|
.sidebar-menu {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 0.5rem;
|
|
}
|
|
|
|
.sidebar-button {
|
|
display: flex;
|
|
align-items: center;
|
|
padding: 0.5rem 0.75rem;
|
|
border-radius: 0.375rem;
|
|
color: #4b5563;
|
|
text-decoration: none;
|
|
transition: all 0.2s ease;
|
|
white-space: nowrap;
|
|
overflow: hidden;
|
|
}
|
|
|
|
.sidebar-button:hover {
|
|
background: var(--bg-color);
|
|
color: var(--primary-color);
|
|
}
|
|
|
|
.sidebar-icon {
|
|
min-width: 1.5rem;
|
|
margin-right: 1rem;
|
|
font-size: 1.25rem;
|
|
}
|
|
|
|
.main-content {
|
|
grid-area: main-content;
|
|
padding: 1px;
|
|
margin-top: var(--header-height);
|
|
}
|
|
|
|
.status-bar {
|
|
position: fixed;
|
|
top: 4px;
|
|
right: 8px;
|
|
background: white;
|
|
padding: 4px 8px;
|
|
border-radius: 4px;
|
|
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
|
display: flex;
|
|
gap: 8px;
|
|
z-index: 15;
|
|
}
|
|
|
|
.status-info {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
color: #4b5563;
|
|
font-size: 0.875rem;
|
|
}
|
|
|
|
.data-panel {
|
|
background: white;
|
|
border-radius: 4px;
|
|
padding: 16px;
|
|
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
|
height: calc(100vh - var(--header-height) + 3px);
|
|
overflow: auto;
|
|
}
|
|
|
|
.panel-title {
|
|
font-size: 1rem;
|
|
font-weight: 600;
|
|
color: #1a1a1a;
|
|
margin-bottom: 16px;
|
|
padding-bottom: 8px;
|
|
border-bottom: 1px solid #e5e7eb;
|
|
}
|
|
|
|
.form-group {
|
|
margin-bottom: 16px;
|
|
}
|
|
|
|
.form-label {
|
|
display: block;
|
|
margin-bottom: 8px;
|
|
font-weight: 500;
|
|
color: #374151;
|
|
}
|
|
|
|
.form-input {
|
|
width: 100%;
|
|
padding: 8px 12px;
|
|
border: 1px solid #d1d5db;
|
|
border-radius: 4px;
|
|
font-size: 14px;
|
|
transition: border-color 0.2s;
|
|
}
|
|
|
|
.form-input:focus {
|
|
outline: none;
|
|
border-color: var(--primary-color);
|
|
box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.1);
|
|
}
|
|
|
|
.btn {
|
|
display: inline-block;
|
|
padding: 8px 16px;
|
|
background-color: var(--primary-color);
|
|
color: white;
|
|
border: none;
|
|
border-radius: 4px;
|
|
font-size: 14px;
|
|
font-weight: 500;
|
|
cursor: pointer;
|
|
transition: background-color 0.2s;
|
|
}
|
|
|
|
.btn:hover {
|
|
background-color: var(--primary-hover);
|
|
}
|
|
|
|
@media (max-width: 768px) {
|
|
.container {
|
|
grid-template-columns: 1fr;
|
|
grid-template-areas:
|
|
"header"
|
|
"main-content";
|
|
}
|
|
|
|
.sidebar {
|
|
transform: translateX(-100%);
|
|
}
|
|
|
|
.header {
|
|
left: 0;
|
|
}
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class="sidebar">
|
|
<div class="sidebar-menu">
|
|
<a href="/" class="sidebar-button">
|
|
<span class="sidebar-icon">🏠</span>
|
|
<span>系统主页</span>
|
|
</a>
|
|
<a href="/data" class="sidebar-button">
|
|
<span class="sidebar-icon">📊</span>
|
|
<span>系统数据</span>
|
|
</a>
|
|
<a href="/dashboard" class="sidebar-button">
|
|
<span class="sidebar-icon">⚡</span>
|
|
<span>配电操作</span>
|
|
</a>
|
|
<a href="/logs" class="sidebar-button">
|
|
<span class="sidebar-icon">📝</span>
|
|
<span>系统日志</span>
|
|
</a>
|
|
<a href="/testpage" class="sidebar-button">
|
|
<span class="sidebar-icon">🧪</span>
|
|
<span>测试页面</span>
|
|
</a>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="header">
|
|
<h1>测试页面</h1>
|
|
<div class="status-bar">
|
|
<div class="status-info">
|
|
<span>🟢 系统状态: <span id="systemStatus">运行中</span></span>
|
|
<span>⚡ 应用频率: <span id="appFreq">0</span> Hz</span>
|
|
<span>📡 通信频率: <span id="commFreq">0</span> Hz</span>
|
|
<span>💻 CPU负载: <span id="cpuLoad">0</span>%</span>
|
|
<span>⏱️ 迭代间隔: <span id="timeSinceIterate">0</span>ms</span>
|
|
<span>🕒 更新时间: <time id="updateTime">2025-05-19 11:54</time></span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="main-content">
|
|
<div class="data-panel">
|
|
<div class="panel-title">测试表单</div>
|
|
<form id="testForm">
|
|
<div class="form-section">
|
|
<div class="panel-subtitle">测试类型/ID设置</div>
|
|
<div class="form-group">
|
|
<label for="type" class="form-label">类型</label>
|
|
<input type="number" id="type" name="type" class="form-input" min="0" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="id" class="form-label">ID</label>
|
|
<input type="number" id="id" name="id" class="form-input" min="0" required>
|
|
</div>
|
|
<button type="button" class="btn" onclick="submitTestForm()">提交类型/ID</button>
|
|
</div>
|
|
|
|
<div class="form-section">
|
|
<div class="panel-subtitle">MOOSDB测试消息设置</div>
|
|
<div class="form-group">
|
|
<label for="key" class="form-label">键(Key)</label>
|
|
<input type="text" id="key" name="key" class="form-input" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="value" class="form-label">值(Value)</label>
|
|
<input type="text" id="value" name="value" class="form-input" required>
|
|
</div>
|
|
<button type="button" class="btn" onclick="submitKeyValueForm()" style="background-color: #4f46e5;">提交键值对</button>
|
|
</div>
|
|
</form>
|
|
|
|
<style>
|
|
.form-section {
|
|
margin-bottom: 24px;
|
|
padding: 16px;
|
|
background: #f9fafb;
|
|
border-radius: 8px;
|
|
}
|
|
.panel-subtitle {
|
|
font-size: 0.9rem;
|
|
font-weight: 600;
|
|
color: #4b5563;
|
|
margin-bottom: 12px;
|
|
padding-bottom: 4px;
|
|
border-bottom: 1px solid #e5e7eb;
|
|
}
|
|
</style>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
let ws = null;
|
|
|
|
function initWebSocket() {
|
|
ws = new WebSocket('ws://' + window.location.host + '/ws');
|
|
|
|
ws.onopen = () => {
|
|
console.log('WebSocket连接已建立');
|
|
updateStatus('运行中', true);
|
|
};
|
|
|
|
ws.onmessage = (event) => {
|
|
const data = JSON.parse(event.data);
|
|
console.log('收到系统数据:', data);
|
|
|
|
// 更新MOOSDB信息
|
|
if (data.moosdb) {
|
|
document.getElementById('appFreq').textContent = data.moosdb.appFreq.toFixed(2);
|
|
document.getElementById('commFreq').textContent = data.moosdb.commFreq.toFixed(2);
|
|
document.getElementById('cpuLoad').textContent = data.moosdb.cpuLoad.toFixed(1);
|
|
document.getElementById('timeSinceIterate').textContent = data.moosdb.timeSinceIterate.toFixed(0);
|
|
}
|
|
|
|
updateTime();
|
|
};
|
|
|
|
ws.onclose = () => {
|
|
console.log('WebSocket连接已关闭');
|
|
updateStatus('已断开', false);
|
|
setTimeout(initWebSocket, 5000);
|
|
};
|
|
|
|
ws.onerror = (error) => {
|
|
console.error('WebSocket错误:', error);
|
|
updateStatus('错误', false);
|
|
};
|
|
}
|
|
|
|
function updateStatus(status, isConnected) {
|
|
const statusElement = document.querySelector('.status-info span:first-child');
|
|
statusElement.innerHTML = `${isConnected ? '🟢' : '🔴'} 系统状态: ${status}`;
|
|
}
|
|
|
|
function updateTime() {
|
|
const now = new Date();
|
|
const timeStr = now.toLocaleString('zh-CN', {
|
|
year: 'numeric',
|
|
month: '2-digit',
|
|
day: '2-digit',
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
second: '2-digit',
|
|
hour12: false
|
|
});
|
|
document.getElementById('updateTime').textContent = timeStr;
|
|
}
|
|
|
|
function submitTestForm() {
|
|
const form = document.getElementById('testForm');
|
|
const typeValue = parseInt(form.type.value);
|
|
const idValue = parseInt(form.id.value);
|
|
|
|
if (typeValue < 0 || idValue < 0) {
|
|
alert('请输入非负整数');
|
|
return;
|
|
}
|
|
|
|
const formData = {
|
|
type: typeValue,
|
|
id: idValue
|
|
};
|
|
|
|
sendFormData(formData);
|
|
}
|
|
|
|
function submitKeyValueForm() {
|
|
const key = document.getElementById('key').value.trim();
|
|
const value = document.getElementById('value').value.trim();
|
|
|
|
if (!key || !value) {
|
|
alert('请输入键和值');
|
|
return;
|
|
}
|
|
|
|
// 直接构造JSON对象,不通过form
|
|
const payload = {
|
|
key: key,
|
|
value: value
|
|
};
|
|
|
|
// 专门发送到MOOSDB测试消息接口
|
|
fetch('/moosdb_test', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(payload)
|
|
})
|
|
.then(response => {
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
}
|
|
return response.json();
|
|
})
|
|
.then(data => {
|
|
if (data.success) {
|
|
alert('MOOSDB测试消息提交成功: ' + data.message);
|
|
} else {
|
|
alert('MOOSDB测试消息提交失败: ' + data.error);
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('MOOSDB测试消息提交错误:', error);
|
|
alert('MOOSDB测试消息提交过程中发生错误: ' + error.message);
|
|
});
|
|
}
|
|
|
|
function sendFormData(formData) {
|
|
|
|
fetch('/testpage', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(formData)
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
alert('提交成功: ' + data.message);
|
|
} else {
|
|
alert('提交失败: ' + data.error);
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('提交错误:', error);
|
|
alert('提交过程中发生错误');
|
|
});
|
|
}
|
|
|
|
window.addEventListener('load', initWebSocket);
|
|
</script>
|
|
</body>
|
|
</html>
|
|
)row";
|
|
|
|
#endif // HTTP_SERVER_TESTPAGE_H
|