diff --git a/.vscode/settings.json b/.vscode/settings.json index 8cb75a6..4c3850a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -53,7 +53,27 @@ "streambuf": "cpp", "cinttypes": "cpp", "typeinfo": "cpp", - "queue": "cpp" + "queue": "cpp", + "csignal": "cpp", + "any": "cpp", + "bitset": "cpp", + "charconv": "cpp", + "chrono": "cpp", + "codecvt": "cpp", + "complex": "cpp", + "condition_variable": "cpp", + "coroutine": "cpp", + "optional": "cpp", + "ratio": "cpp", + "regex": "cpp", + "source_location": "cpp", + "future": "cpp", + "mutex": "cpp", + "semaphore": "cpp", + "stop_token": "cpp", + "thread": "cpp", + "typeindex": "cpp", + "variant": "cpp" }, "C_Cpp.errorSquiggles": "disabled", "marscode.chatLanguage": "cn", diff --git a/data/msg/uPower_currentState_st.json b/data/msg/uPower_currentState_st.json index 1edaa85..0e6fa67 100644 --- a/data/msg/uPower_currentState_st.json +++ b/data/msg/uPower_currentState_st.json @@ -1,5 +1,5 @@ { - "HVABFTDev123Cur_uint16": 0, + "HVABFTDev123Cur_uint16": 1024, "HVABactCur_uint16": 0, "HVABbowRudCur_uint16": 0, "HVABbusBCur_uint16": 1792, diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8fa747c..5e05a09 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -16,6 +16,7 @@ INCLUDE_DIRECTORIES(${LOCAL_LIBRARY_DIRS}) # List the subdirectories to build... #============================================================================ ADD_SUBDIRECTORY(pPowerManger) +ADD_SUBDIRECTORY(appPowerMangerWebUI) ############################################################################## # END of CMakeLists.txt diff --git a/src/appPowerMangerWebUI/CMakeLists.txt b/src/appPowerMangerWebUI/CMakeLists.txt new file mode 100644 index 0000000..23f8a6b --- /dev/null +++ b/src/appPowerMangerWebUI/CMakeLists.txt @@ -0,0 +1,32 @@ +#-------------------------------------------------------- +# The CMakeLists.txt for: appPowerMangerWebUI +# Author(s): +#-------------------------------------------------------- +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# 设置 vcpkg 工具链文件路径 +set(CMAKE_TOOLCHAIN_FILE "/home/zjk/project/lib/vcpkg/scripts/buildsystems/vcpkg.cmake" CACHE STRING "") + +#find crow库 +find_package(Crow CONFIG REQUIRED) +# Find SQLite3 package +find_package(SQLite3 REQUIRED) + +SET(SRC + main.cpp +) + +ADD_EXECUTABLE(appPowerMangerWebUI ${SRC}) + +TARGET_LINK_LIBRARIES(appPowerMangerWebUI + ${MOOS_LIBRARIES} + mbutil + m + pthread + Crow::Crow + asio::asio + jsoncpp + SQLiteDB + sqlite3) + diff --git a/src/appPowerMangerWebUI/index.html b/src/appPowerMangerWebUI/index.html new file mode 100644 index 0000000..0cff833 --- /dev/null +++ b/src/appPowerMangerWebUI/index.html @@ -0,0 +1,281 @@ + + + + + + + 电源管理 + + + + + +
+ + + +
+ + +
+ +
+ +
+

数据库操作

+
+ + 正在连接数据库... +
+
+ +
+

DIS系统状态

+
正在加载...
+
+ +
+

CCU状态

+
正在加载...
+
+ +
+

运行日志

+
正在加载...
+
+ +
+

状态机状态

+
正在加载...
+
+ + + + + diff --git a/src/appPowerMangerWebUI/main.cpp b/src/appPowerMangerWebUI/main.cpp new file mode 100644 index 0000000..fd2e9fe --- /dev/null +++ b/src/appPowerMangerWebUI/main.cpp @@ -0,0 +1,66 @@ +#include +#include "crow.h" +#include +#include "json/json.h" +#include +#include +#include +#include +#include +#include +#include +#include "../pPowerManger/SQLiteDB.h" + +using namespace std; + +// 读取 HTML 文件内容 +string read_file(const string& path) { + ifstream file(path); + stringstream buffer; + buffer << file.rdbuf(); + return buffer.str(); +} + +int main() { + crow::SimpleApp app; + SQLiteDB db("/home/zjk/project/H100/H100PowerManger/data/power_data.db"); + // 初始化 SQLite 数据库 + + // 路由返回 HTML 页面 + CROW_ROUTE(app, "/")([](){ + // 读取 HTML 文件 + string html_content = read_file("/home/zjk/project/H100/H100PowerManger/src/appPowerMangerWebUI/index.html"); + return html_content; + }); + + 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"; // 替换为实际的数据库名称 + } else { + response["connected"] = false; + } + return response; + }); + + 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 buffer((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + // 将 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"); + return response; + }); + + // 启动服务器,监听端口 18080 + app.port(18080).multithreaded().run(); + + return 0; +} \ No newline at end of file diff --git a/src/pPowerManger/PowerManger.cpp b/src/pPowerManger/PowerManger.cpp index 2d47ea6..f6a5990 100644 --- a/src/pPowerManger/PowerManger.cpp +++ b/src/pPowerManger/PowerManger.cpp @@ -123,14 +123,7 @@ bool PowerManger::Iterate() else m_FsmThread.Stop(); // 数据库测试 - PowerData data; - // data.id = 1; - data.timestamp = "2023-05-22 10:00:00"; - data.voltage = 12.3 + rand() % 100 / 100.0; - data.current = 4.5 + rand() % 100 / 100.0; - data.power = 56.7 + rand() % 100 / 100.0; - data.temperature = 25.0 + rand() % 100 / 100.0; - m_db->insertPowerData(data.voltage, data.current, data.power, data.temperature); + //3. 输出报告 buildPowerSysReport(); //4. 打印错误信息 @@ -281,7 +274,6 @@ bool PowerManger::FsmLoop() return true; } - bool PowerManger::updatePmState() { diff --git a/src/pPowerManger/SQLiteDB.cpp b/src/pPowerManger/SQLiteDB.cpp index d425d17..738f5d7 100644 --- a/src/pPowerManger/SQLiteDB.cpp +++ b/src/pPowerManger/SQLiteDB.cpp @@ -97,6 +97,32 @@ void SQLiteDB::insertStateData(StateData stateData) { std::cerr << "Error preparing statement: " << sqlite3_errmsg(m_db) << std::endl; } } +bool SQLiteDB::isDatabaseConnected(const std::string& path, const std::string& dbName) { + if (!m_db) return false; + + // 尝试执行简单查询验证连接 + const char* sql = "SELECT name FROM sqlite_master WHERE type='table';"; + char* errMsg = nullptr; + + int rc = sqlite3_exec(m_db, sql, nullptr, nullptr, &errMsg); + if (rc != SQLITE_OK) { + std::cerr << "Database connection check failed: " << errMsg << std::endl; + sqlite3_free(errMsg); + return false; + } + + // 检查数据库路径和名称是否匹配 + std::string fullPath = path + "/" + dbName; + const char* dbPath = sqlite3_db_filename(m_db, "main"); + + if (!dbPath || fullPath != dbPath) { + std::cerr << "Database path mismatch: expected " << fullPath << ", got " << (dbPath ? dbPath : "null") << std::endl; + return false; + } + + return true; +} + std::vector SQLiteDB::getPowerData(int limit) { std::vector data; if (!m_db) return data; @@ -124,4 +150,4 @@ std::vector SQLiteDB::getPowerData(int limit) { } return data; -} \ No newline at end of file +} diff --git a/src/pPowerManger/SQLiteDB.h b/src/pPowerManger/SQLiteDB.h index 3145300..8d27bef 100644 --- a/src/pPowerManger/SQLiteDB.h +++ b/src/pPowerManger/SQLiteDB.h @@ -35,6 +35,9 @@ public: void insertStateData(StateData stateData); std::vector getPowerData(int limit = 100); + //使用函数 + bool isDatabaseConnected(const std::string& path, const std::string& dbName); + private: sqlite3* m_db; }; diff --git a/test/SQLiteDBTest.cpp b/test/SQLiteDBTest.cpp index fc9152a..ad27d39 100644 --- a/test/SQLiteDBTest.cpp +++ b/test/SQLiteDBTest.cpp @@ -9,12 +9,8 @@ TEST_CASE("SQLiteDB Test", "[SQLiteDB]") { db.createStateTables(); } - SECTION("Query Data") { - std::vector results = db.getPowerData(); - REQUIRE(results.size() == 1); - REQUIRE(results[0].voltage == 12.5); - REQUIRE(results[0].current == 2.5); - REQUIRE(results[0].power == 31.25); - REQUIRE(results[0].temperature == 25.0); + SECTION("Test isDatabaseConnected") { + REQUIRE(db.isDatabaseConnected("/home/zjk/project/H100/H100PowerManger/bin","test.db") == true); } + }