增加了下载功能
This commit is contained in:
@@ -16,11 +16,20 @@
|
||||
padding: 20px;
|
||||
height: 100vh;
|
||||
display: grid;
|
||||
grid-template-rows: auto 1fr 1fr;
|
||||
grid-template-rows: auto auto 1fr 1fr;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
gap: 20px;
|
||||
}
|
||||
|
||||
.db-status-panel {
|
||||
grid-column: 1 / -1;
|
||||
background-color: #333;
|
||||
border: 1px solid #61dafb;
|
||||
border-radius: 8px;
|
||||
padding: 20px;
|
||||
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.control-panel {
|
||||
grid-column: 1 / -1;
|
||||
display: flex;
|
||||
@@ -32,16 +41,6 @@
|
||||
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.db-status-panel {
|
||||
grid-column: 1 / -1;
|
||||
background-color: #333;
|
||||
border: 1px solid #61dafb;
|
||||
border-radius: 8px;
|
||||
padding: 20px;
|
||||
margin-top: 20px;
|
||||
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.db-status {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
@@ -123,22 +122,22 @@
|
||||
|
||||
.log-panel {
|
||||
grid-column: 1;
|
||||
grid-row: 3;
|
||||
grid-row: 4;
|
||||
}
|
||||
|
||||
.ccu-state-panel {
|
||||
grid-column: 2;
|
||||
grid-row: 2;
|
||||
grid-row: 3;
|
||||
}
|
||||
|
||||
.dis-sys-panel {
|
||||
grid-column: 1;
|
||||
grid-row: 2;
|
||||
grid-row: 3;
|
||||
}
|
||||
|
||||
.state-machine-panel {
|
||||
grid-column: 2;
|
||||
grid-row: 3;
|
||||
grid-row: 4;
|
||||
}
|
||||
|
||||
pre {
|
||||
@@ -148,10 +147,44 @@
|
||||
font-size: 13px;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.file-input {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.file-label {
|
||||
display: inline-block;
|
||||
padding: 10px 15px;
|
||||
background: #61dafb;
|
||||
color: #282c34;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
margin-left: 10px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="db-status-panel">
|
||||
<!-- <h2>连接状态</h2> -->
|
||||
<div class="status-columns">
|
||||
<div class="status-column">
|
||||
<h3>MOOSDB</h3>
|
||||
<div class="db-status" id="moos-db-status">
|
||||
<i class="fas fa-database"></i>
|
||||
<span>正在连接MOOSDB...</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="status-column">
|
||||
<h3>本地数据库</h3>
|
||||
<div class="db-status" id="db-status">
|
||||
<i class="fas fa-database"></i>
|
||||
<span>正在连接数据库...</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="control-panel">
|
||||
<button id="mode-switch">
|
||||
<i class="fas fa-exchange-alt"></i>
|
||||
@@ -166,21 +199,11 @@
|
||||
系统复位操作
|
||||
</button>
|
||||
<div class="control-panel">
|
||||
<!-- 其他按钮 -->
|
||||
<button id="download-db">
|
||||
<i class="fas fa-download"></i>
|
||||
下载数据库
|
||||
</button>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<div class="db-status-panel">
|
||||
<h2>数据库操作</h2>
|
||||
<div class="db-status" id="db-status">
|
||||
<i class="fas fa-database"></i>
|
||||
<span>正在连接数据库...</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel dis-sys-panel">
|
||||
@@ -204,6 +227,32 @@
|
||||
</div>
|
||||
|
||||
<script>
|
||||
let currentDbPath = '/home/zjk/project/H100/H100PowerManger/data/power_data.db';
|
||||
|
||||
// 数据库下载功能
|
||||
document.getElementById('download-db').addEventListener('click', function() {
|
||||
fetch('/api/download-db')
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error('下载失败:' + response.statusText);
|
||||
}
|
||||
return response.blob();
|
||||
})
|
||||
.then(blob => {
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = 'power_data.db';
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
window.URL.revokeObjectURL(url);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('下载数据库失败:', error);
|
||||
alert('下载数据库失败:' + error.message);
|
||||
});
|
||||
});
|
||||
|
||||
// 更新页面数据的函数
|
||||
function updateData() {
|
||||
fetch('/api/status')
|
||||
@@ -224,7 +273,28 @@
|
||||
.catch(error => console.error('Error fetching data:', error));
|
||||
}
|
||||
|
||||
// 检查数据库连接状态
|
||||
// 检查MOOS数据库连接状态
|
||||
function checkMoosDbConnection() {
|
||||
fetch('/api/moos-db')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
const moosDbStatus = document.getElementById('moos-db-status');
|
||||
if (data.connected) {
|
||||
moosDbStatus.className = 'db-status connected';
|
||||
moosDbStatus.innerHTML = `<i class="fas fa-database"></i><span>已连接到MOOS数据库:${data.host}:${data.port}</span>`;
|
||||
} else {
|
||||
moosDbStatus.className = 'db-status disconnected';
|
||||
moosDbStatus.innerHTML = `<i class="fas fa-database"></i><span>MOOS数据库连接失败</span>`;
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
const moosDbStatus = document.getElementById('moos-db-status');
|
||||
moosDbStatus.className = 'db-status disconnected';
|
||||
moosDbStatus.innerHTML = `<i class="fas fa-database"></i><span>MOOS数据库连接错误</span>`;
|
||||
});
|
||||
}
|
||||
|
||||
// 检查本地数据库连接状态
|
||||
function checkDbConnection() {
|
||||
fetch('/api/db-status')
|
||||
.then(response => response.json())
|
||||
@@ -244,37 +314,18 @@
|
||||
dbStatus.innerHTML = `<i class="fas fa-database"></i><span>数据库连接错误</span>`;
|
||||
});
|
||||
}
|
||||
document.getElementById('download-db').addEventListener('click', function() {
|
||||
const dbPath = 'data/power_data.db';
|
||||
fetch(dbPath)
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error('Network response was not ok');
|
||||
}
|
||||
return response.blob();
|
||||
})
|
||||
.then(blob => {
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = 'power_data.db'; // 文件名
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
window.URL.revokeObjectURL(url);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error downloading database:', error);
|
||||
alert('下载数据库失败,请检查文件路径或网络连接');
|
||||
});
|
||||
});
|
||||
|
||||
// 每2秒更新一次数据
|
||||
setInterval(updateData, 2000);
|
||||
// 每1秒更新一次数据
|
||||
setInterval(updateData, 1000);
|
||||
updateData(); // 初始加载
|
||||
|
||||
// 每5秒检查一次数据库连接状态
|
||||
setInterval(checkDbConnection, 5000);
|
||||
// 每3秒检查一次数据库连接状态
|
||||
setInterval(() => {
|
||||
checkDbConnection();
|
||||
checkMoosDbConnection();
|
||||
}, 3000);
|
||||
checkDbConnection(); // 初始检查
|
||||
checkMoosDbConnection(); // 初始检查
|
||||
</script>
|
||||
</body>
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include "../pPowerManger/SQLiteDB.h"
|
||||
#include <MOOS/libMOOS/Comms/MOOSAsyncCommClient.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -24,38 +25,72 @@ string read_file(const string& path) {
|
||||
int main() {
|
||||
crow::SimpleApp app;
|
||||
SQLiteDB db("/home/zjk/project/H100/H100PowerManger/data/power_data.db");
|
||||
// 初始化 SQLite 数据库
|
||||
MOOS::MOOSAsyncCommClient moosClient;
|
||||
moosClient.Run("localhost", 9000, "appPowerMangerWebUI");
|
||||
|
||||
// 路由返回 HTML 页面
|
||||
// 定义路由,处理根路径的请求
|
||||
CROW_ROUTE(app, "/")([](){
|
||||
// 读取 HTML 文件
|
||||
string html_content = read_file("/home/zjk/project/H100/H100PowerManger/src/appPowerMangerWebUI/index.html");
|
||||
return html_content;
|
||||
});
|
||||
|
||||
// 定义路由,处理 /api/power-data 的请求
|
||||
CROW_ROUTE(app, "/api/db-status")
|
||||
([&db]() {
|
||||
crow::json::wvalue response;
|
||||
if (db.isDatabaseConnected("/home/zjk/project/H100/H100PowerManger/data", "power_data.db")) {
|
||||
response["connected"] = true;
|
||||
response["database"] = "YourDatabaseName"; // 替换为实际的数据库名称
|
||||
response["database"] = db.getDBPatch(); // 替换为实际的数据库名称
|
||||
} else {
|
||||
response["connected"] = false;
|
||||
}
|
||||
return response;
|
||||
});
|
||||
|
||||
// 定义路由,处理 /api/power-data 的请求
|
||||
CROW_ROUTE(app, "/api/download-db")
|
||||
([&db]() {
|
||||
std::ifstream file("/home/zjk/project/H100/H100PowerManger/data/power_data.db", std::ios::binary);
|
||||
if (!file) {
|
||||
return crow::response(404, "Database file not found");
|
||||
}
|
||||
std::vector<char> buffer((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
||||
// 将 buffer.size() 转换为 std::string
|
||||
std::string contentLength = std::to_string(buffer.size());
|
||||
crow::response response(buffer.size(), contentLength, "application/octet-stream");
|
||||
response.add_header("Content-Disposition", "attachment; filename=power_data.db");
|
||||
|
||||
// 获取文件大小
|
||||
file.seekg(0, std::ios::end);
|
||||
size_t fileSize = file.tellg();
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
// 创建响应对象
|
||||
crow::response response;
|
||||
response.code = 200;
|
||||
response.set_header("Content-Type", "application/octet-stream");
|
||||
response.set_header("Content-Disposition", "attachment; filename=power_data.db");
|
||||
response.set_header("Content-Length", std::to_string(fileSize));
|
||||
|
||||
// 直接返回文件流
|
||||
response.body = std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
||||
file.close();
|
||||
|
||||
return response;
|
||||
});
|
||||
// 定义路由,处理 /api/moos-db 的请求
|
||||
CROW_ROUTE(app, "/api/moos-db")
|
||||
([&moosClient]() -> crow::json::wvalue {
|
||||
crow::json::wvalue response;
|
||||
if (moosClient.IsConnected()) {
|
||||
response["connected"] = true;
|
||||
response["host"] = moosClient.GetDBHostname();
|
||||
response["port"] = std::to_string(moosClient.GetDBHostPort());
|
||||
}
|
||||
else {
|
||||
response["connected"] = false;
|
||||
}
|
||||
return response;
|
||||
});
|
||||
// 定义路由,处理 /api/moos-notify 的请求(POST)
|
||||
CROW_ROUTE(app, "/api/moos-notify")
|
||||
([&moosClient]() {
|
||||
crow::json::wvalue response;
|
||||
response["message"] = "Received MOOS notification";
|
||||
return response;
|
||||
});
|
||||
|
||||
@@ -63,4 +98,4 @@ int main() {
|
||||
app.port(18080).multithreaded().run();
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ SQLiteDB::SQLiteDB(const std::string& dbPath) {
|
||||
std::cerr << "Error opening database: " << sqlite3_errmsg(m_db) << std::endl;
|
||||
m_db = nullptr;
|
||||
}
|
||||
m_dbPath = dbPath;
|
||||
}
|
||||
|
||||
SQLiteDB::~SQLiteDB() {
|
||||
@@ -151,3 +152,7 @@ std::vector<PowerData> SQLiteDB::getPowerData(int limit) {
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
string SQLiteDB::getDBPatch(){
|
||||
return m_dbPath;
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "json/json.h"
|
||||
// #include <nlohmann/json.hpp>
|
||||
|
||||
using namespace std;
|
||||
struct PowerData {
|
||||
std::string timestamp;
|
||||
double voltage;
|
||||
@@ -38,8 +39,12 @@ public:
|
||||
//使用函数
|
||||
bool isDatabaseConnected(const std::string& path, const std::string& dbName);
|
||||
|
||||
//获取数据库状态函数
|
||||
string getDBPatch();
|
||||
|
||||
private:
|
||||
sqlite3* m_db;
|
||||
string m_dbPath;
|
||||
};
|
||||
|
||||
#endif // SQLITEDB_H
|
||||
|
||||
Reference in New Issue
Block a user