From 59faa14afb9bc57a891a72c0762f21c5820cbb1f Mon Sep 17 00:00:00 2001 From: zhaojingkui <1553836110@qq.com> Date: Mon, 10 Feb 2025 16:37:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=8F=8A=E6=B5=8B=E8=AF=95=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 1 + data/msg/uPower_currentState_st.json | 58 ++++++------- data/msg/uPower_disSysState_st.json | 4 +- data/msg/uPower_fcsState_st.json | 48 +++++----- data/msg/uPower_liBatState_st.json | 48 +++++----- src/pPowerManger/CMakeLists.txt | 14 ++- src/pPowerManger/PowerManger.cpp | 15 ++++ src/pPowerManger/PowerManger.h | 6 +- src/pPowerManger/SQLiteDB.cpp | 125 +++++++++++++++++++++++++++ src/pPowerManger/SQLiteDB.h | 39 +++++++++ test/CMakeLists.txt | 26 ++++++ test/SQLiteDBTest.cpp | 24 +++++ 12 files changed, 326 insertions(+), 82 deletions(-) create mode 100644 src/pPowerManger/SQLiteDB.cpp create mode 100644 src/pPowerManger/SQLiteDB.h create mode 100644 test/CMakeLists.txt create mode 100644 test/SQLiteDBTest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7320b85..068914a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -103,3 +103,4 @@ ENDIF( ${WIN32} ) # Add Subdirectories #======================================================================= ADD_SUBDIRECTORY( src ) +ADD_SUBDIRECTORY( test ) diff --git a/data/msg/uPower_currentState_st.json b/data/msg/uPower_currentState_st.json index a8a0701..d1fdd26 100644 --- a/data/msg/uPower_currentState_st.json +++ b/data/msg/uPower_currentState_st.json @@ -1,47 +1,47 @@ { - "HVABFTDev123Cur_uint16": 0, - "HVABactCur_uint16": 0, - "HVABbowRudCur_uint16": 0, - "HVABbusBCur_uint16": 0, + "HVABFTDev123Cur_uint16": 34345, + "HVABactCur_uint16": 22235, + "HVABbowRudCur_uint16": 24015, + "HVABbusBCur_uint16": 53248, "HVABbusBVol_uint16": 0, - "HVABmastSteGeaCur_uint16": 0, - "HVABopenWaterCur_uint16": 0, - "HVABxczCur_uint16": 1, - "HVBBCCUCur_uint16": 0, - "HVBBFCSSecurSysCur_uint16": 0, + "HVABmastSteGeaCur_uint16": 127, + "HVABopenWaterCur_uint16": 64891, + "HVABxczCur_uint16": 63488, + "HVBBCCUCur_uint16": 16752, + "HVBBFCSSecurSysCur_uint16": 22235, "HVBBbatInsCur_uint16": 0, - "HVBBbowLowVolDisBoxCur_uint16": 0, - "HVBBdcC1InsCur_uint16": 0, - "HVBBemer2BusVol_uint16": 0, + "HVBBbowLowVolDisBoxCur_uint16": 34349, + "HVBBdcC1InsCur_uint16": 22235, + "HVBBemer2BusVol_uint16": 32768, "HVBBemerBatCur_uint16": 0, - "HVBBinsBusVol_uint16": 0, - "HVBBplcCur_uint16": 0, - "HVBBreservedCur1_uint16": 0, + "HVBBinsBusVol_uint16": 22528, + "HVBBplcCur_uint16": 127, + "HVBBreservedCur1_uint16": 127, "HVBBreservedCur2_uint16": 0, "HVBBunit4Cur_uint16": 0, - "HVBFTDev45Cur_uint16": 0, - "HVBbatGroupCur_uint16": 0, - "HVBbowHVolDisBoxCur_uint16": 0, + "HVBFTDev45Cur_uint16": 32598, + "HVBbatGroupCur_uint16": 61904, + "HVBbowHVolDisBoxCur_uint16": 56157, "HVBbusACur_uint16": 0, "HVBbusAVol_uint16": 0, "HVBdcDc5Cur_uint16": 0, "HVBfbCur_uint16": 0, "HVBmotorCur_uint16": 0, - "HVBpowerBusCur_uint16": 0, - "HVBpowerBusVol_uint16": 0, + "HVBpowerBusCur_uint16": 6, + "HVBpowerBusVol_uint16": 32598, "HVBreservedCur1_uint16": 0, "HVBreservedCur2_uint16": 0, "HVBreservedCur3_uint16": 0, - "HVBsternRud1Cur_uint16": 0, + "HVBsternRud1Cur_uint16": 1, "HVBsternRud2Cur_uint16": 0, "HVBtyzCur_uint16": 0, - "LVBBbowPZDevCur_uint16": 0, - "LVBBemerBatCur_uint16": 0, - "LVBBemerBusVol_uint16": 0, - "LVBBinsBusCur_uint16": 0, - "LVBBunit1Cur_uint16": 0, - "LVBBunit2Cur_uint16": 0, + "LVBBbowPZDevCur_uint16": 127, + "LVBBemerBatCur_uint16": 32768, + "LVBBemerBusVol_uint16": 16752, + "LVBBinsBusCur_uint16": 22235, + "LVBBunit1Cur_uint16": 127, + "LVBBunit2Cur_uint16": 256, "LVBBunit3Cur_uint16": 0, - "LVBBunit5Cur_uint16": 0, - "LVBinsBusVol_uint16": 0 + "LVBBunit5Cur_uint16": 22016, + "LVBinsBusVol_uint16": 16752 } \ No newline at end of file diff --git a/data/msg/uPower_disSysState_st.json b/data/msg/uPower_disSysState_st.json index ffc37b2..ed8e1bf 100644 --- a/data/msg/uPower_disSysState_st.json +++ b/data/msg/uPower_disSysState_st.json @@ -1,8 +1,8 @@ { "HVolABusBreaker_unit16": 0, - "HVolBBusBreaker_unit16": 127, + "HVolBBusBreaker_unit16": 0, "HVolBusBreaker_uint16": 0, - "LVolBusBreaker_uint16": 127, + "LVolBusBreaker_uint16": 0, "faultCode_array": [ 0 diff --git a/data/msg/uPower_fcsState_st.json b/data/msg/uPower_fcsState_st.json index 27efb55..4010c8e 100644 --- a/data/msg/uPower_fcsState_st.json +++ b/data/msg/uPower_fcsState_st.json @@ -1,30 +1,30 @@ { - "backpressure1_fp32": 7.0, - "backpressure2_fp32": 7.0, - "ch4oConcent1_fp32": 75.0, - "ch4oConcent2_fp32": 11.0, - "ch4o_fp32": 0.0, - "excTemp_fp32": 64.0, - "faultCode1_uint16": 0, - "faultCode2_uint16": 0, - "faultCode3_uint16": 0, - "fcuCurIns_fp32": 0.0, + "backpressure1_fp32": 2.0254939324098806e+18, + "backpressure2_fp32": 1.1694265883419146e-38, + "ch4oConcent1_fp32": -2.7734143515560723e-33, + "ch4oConcent2_fp32": 1.1694265883419146e-38, + "ch4o_fp32": 1.1694265883419146e-38, + "excTemp_fp32": 1.1754039670713386e-38, + "faultCode1_uint16": 25312, + "faultCode2_uint16": 56201, + "faultCode3_uint16": 32598, + "fcuCurIns_fp32": 2.5111268480700722e-42, "fcuCurPower_fp32": 0.0, - "fcuCur_fp32": 22.0, + "fcuCur_fp32": 1.8734974449861263e+18, "fcuFaultLeve_uint8": 0, - "fcuOutIns_fp32": 99.0, - "fcuOutPower_fp32": 260.0, + "fcuOutIns_fp32": 1.1693958999055459e-38, + "fcuOutPower_fp32": 1.7755460477889746e-38, "fcuState_uint8": 0, - "fcuVolIns_fp32": 0.0, - "fcuVolPower_fp32": 0.0, - "fcuVol_fp32": 244.0, + "fcuVolIns_fp32": 2.0215532827359314e+18, + "fcuVolPower_fp32": 1.1694265883419146e-38, + "fcuVol_fp32": 1.1694265883419146e-38, "flameDet_uint8": 0, - "h2Concent1_fp32": 58.0, - "h2Concent2_fp32": 59.0, - "o2Concent1_fp32": 9.0, - "o2Concent2_fp32": 38.0, - "o2Level_fp32": 0.0, - "o2_fp32": 0.0, - "powLimit_fp32": 41.0, - "powerGen_fp32": 911.0 + "h2Concent1_fp32": 0.0, + "h2Concent2_fp32": 1.1693958999055459e-38, + "o2Concent1_fp32": -10734197014528.0, + "o2Concent2_fp32": 7.8705161391451957e-39, + "o2Level_fp32": 1.1754039670713386e-38, + "o2_fp32": 1.8514036025451028e-37, + "powLimit_fp32": 0.0, + "powerGen_fp32": 1.4181007727976608e-36 } \ No newline at end of file diff --git a/data/msg/uPower_liBatState_st.json b/data/msg/uPower_liBatState_st.json index 633ec6f..576095c 100644 --- a/data/msg/uPower_liBatState_st.json +++ b/data/msg/uPower_liBatState_st.json @@ -1,38 +1,38 @@ { - "fcuCurToInsLimit1_uint8": 0, + "fcuCurToInsLimit1_uint8": 127, "fcuCurToinsLimit2_uint8": 0, "insBatCurElect_uint16": 0, - "insBatLimit_uint16": 39, - "insBatSoc_uint8": 85, - "insBatTotalElect_uint16": 304, + "insBatLimit_uint16": 3817, + "insBatSoc_uint8": 0, + "insBatTotalElect_uint16": 53312, "insChargeState_uint8": 0, - "insCurSocHold1_uint8": 0, - "insCurSocHold2_uint8": 0, - "insCurSocHold3_uint8": 0, - "insCurrent_uint16": 78, + "insCurSocHold1_uint8": 134, + "insCurSocHold2_uint8": 219, + "insCurSocHold3_uint8": 86, + "insCurrent_uint16": 0, "insLinkVol_uint16": 0, "insNegRelayState_uint8": 0, "insNegRes_uint16": 0, "insPackVol_uint16": 0, - "insPosRelayState_uint8": 1, - "insPosRes_uint16": 0, - "insfaultCode_uint8": 0, + "insPosRelayState_uint8": 0, + "insPosRes_uint16": 3, + "insfaultCode_uint8": 88, "powBatCurElect_uint16": 0, - "powBatSoc_uint8": 60, - "powBatTotalElect_uint16": 413, + "powBatSoc_uint8": 0, + "powBatTotalElect_uint16": 31581, "powChargeState_uint8": 0, "powCurSocHold1_uint8": 0, - "powCurSocHold2_uint8": 0, - "powCurSocHold3_uint8": 0, - "powCurToInsLimit1_uint8": 0, - "powCurToinsLimit2_uint8": 0, - "powCur_uint16": 72, + "powCurSocHold2_uint8": 208, + "powCurSocHold3_uint8": 241, + "powCurToInsLimit1_uint8": 93, + "powCurToinsLimit2_uint8": 219, + "powCur_uint16": 61767, "powLinkVol_uint16": 0, - "powNegRelayState_uint8": 0, - "powNegRes_uint16": 0, - "powPosRelayState_uint8": 0, - "powPosRes_uint16": 0, - "powerBatLimit_uint16": 82, + "powNegRelayState_uint8": 85, + "powNegRes_uint16": 32598, + "powPosRelayState_uint8": 208, + "powPosRes_uint16": 56067, + "powerBatLimit_uint16": 0, "powerPackVol_uint16": 0, - "powfaultCode_uint8": 0 + "powfaultCode_uint8": 45 } \ No newline at end of file diff --git a/src/pPowerManger/CMakeLists.txt b/src/pPowerManger/CMakeLists.txt index 7b368bb..d2b564e 100644 --- a/src/pPowerManger/CMakeLists.txt +++ b/src/pPowerManger/CMakeLists.txt @@ -15,12 +15,16 @@ else (${WIN32}) pthread ) endif (${WIN32}) +# Find SQLite3 package +find_package(SQLite3 REQUIRED) + SET(SRC PowerManger.cpp PowerManger_Info.cpp udpComm.cpp fsm.cpp main.cpp + SQLiteDB.cpp ) ADD_EXECUTABLE(pPowerManger ${SRC}) @@ -32,5 +36,13 @@ TARGET_LINK_LIBRARIES(pPowerManger m pthread jsoncpp - ${SYSTEM_LIBS}) + ${SYSTEM_LIBS} + SQLiteDB + sqlite3) +# Add SQLiteDB static library +SET(SQLITEDB_SRC + SQLiteDB.cpp +) + +ADD_LIBRARY(SQLiteDB STATIC ${SQLITEDB_SRC}) diff --git a/src/pPowerManger/PowerManger.cpp b/src/pPowerManger/PowerManger.cpp index 6465d83..ee7a5f8 100644 --- a/src/pPowerManger/PowerManger.cpp +++ b/src/pPowerManger/PowerManger.cpp @@ -35,6 +35,12 @@ PowerManger::PowerManger() m_faultNum = 0; faultCode.push_back(0); + //初始化数据库 + string dbPath = "/home/zjk/project/H100/H100PowerManger/data/power_data.db"; + m_db = new SQLiteDB(dbPath); + m_db->createTables(); + + //注册状态机 m_fsm.RegisterState("Init", 1, m_Inite_s1); m_fsm.RegisterState("Fault", 99, m_Fault_s99); @@ -106,6 +112,15 @@ bool PowerManger::Iterate() updateTime(); //2. 运行状态机 m_fsm.run(m_pmState,this); + // 数据库测试 + 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. 打印错误信息 diff --git a/src/pPowerManger/PowerManger.h b/src/pPowerManger/PowerManger.h index e097bd7..ad98ffc 100644 --- a/src/pPowerManger/PowerManger.h +++ b/src/pPowerManger/PowerManger.h @@ -30,6 +30,7 @@ #include "udpComm.h" #include "pmSysvariable.h" #include "fsm.h" +#include "SQLiteDB.h" #define VAR_TO_STR(var) #var @@ -69,8 +70,6 @@ class PowerManger : public AppCastingMOOSApp bool IsSkewed(double dfTimeNow, double * pdfSkew = NULL); string getTypeName(const string typeName); - - //通信相关 XPCUdpSocket* setUdpScoket(long lPort); bool disSystemOperation(disBreaker optBreaker, disBreakerOpt); @@ -104,6 +103,9 @@ class PowerManger : public AppCastingMOOSApp double m_current_time; unsigned int m_faultNum; + //数据库 + SQLiteDB* m_db; + // CMOOSThread m_SendThread; private: // bool inline saveStringToJsonFile(const string &strJson, const string &strFileName); diff --git a/src/pPowerManger/SQLiteDB.cpp b/src/pPowerManger/SQLiteDB.cpp new file mode 100644 index 0000000..e271be4 --- /dev/null +++ b/src/pPowerManger/SQLiteDB.cpp @@ -0,0 +1,125 @@ +#include "SQLiteDB.h" +#include +#include +#include + +SQLiteDB::SQLiteDB(const std::string& dbPath) { + if (sqlite3_open(dbPath.c_str(), &m_db) != SQLITE_OK) { + std::cerr << "Error opening database: " << sqlite3_errmsg(m_db) << std::endl; + m_db = nullptr; + } +} + +SQLiteDB::~SQLiteDB() { + if (m_db) { + sqlite3_close(m_db); + } +} + +void SQLiteDB::createTables() { + if (!m_db) return; + + const char* sql = "CREATE TABLE IF NOT EXISTS power_data (" + "id INTEGER PRIMARY KEY AUTOINCREMENT," + "timestamp DATETIME DEFAULT CURRENT_TIMESTAMP," + "voltage REAL NOT NULL," + "current REAL NOT NULL," + "power REAL NOT NULL," + "temperature REAL NOT NULL);"; + + char* errMsg = nullptr; + if (sqlite3_exec(m_db, sql, nullptr, nullptr, &errMsg) != SQLITE_OK) { + std::cerr << "SQL error: " << errMsg << std::endl; + sqlite3_free(errMsg); + } +} + +void SQLiteDB::insertPowerData(float voltage, float current, float power, float temperature) { + if (!m_db) return; + + const char* sql = "INSERT INTO power_data (voltage, current, power, temperature) VALUES (?, ?, ?, ?);"; + sqlite3_stmt* stmt; + + if (sqlite3_prepare_v2(m_db, sql, -1, &stmt, nullptr) == SQLITE_OK) { + sqlite3_bind_double(stmt, 1, voltage); + sqlite3_bind_double(stmt, 2, current); + sqlite3_bind_double(stmt, 3, power); + sqlite3_bind_double(stmt, 4, temperature); + + if (sqlite3_step(stmt) != SQLITE_DONE) { + std::cerr << "Error inserting data: " << sqlite3_errmsg(m_db) << std::endl; + } + + sqlite3_finalize(stmt); + } else { + std::cerr << "Error preparing statement: " << sqlite3_errmsg(m_db) << std::endl; + } +} + +std::vector SQLiteDB::getPowerData(int limit) { + std::vector data; + if (!m_db) return data; + + const char* sql = "SELECT id, timestamp, voltage, current, power, temperature FROM power_data ORDER BY timestamp DESC LIMIT ?;"; + sqlite3_stmt* stmt; + + if (sqlite3_prepare_v2(m_db, sql, -1, &stmt, nullptr) == SQLITE_OK) { + sqlite3_bind_int(stmt, 1, limit); + + while (sqlite3_step(stmt) == SQLITE_ROW) { + PowerData record; + record.id = sqlite3_column_int(stmt, 0); + record.timestamp = reinterpret_cast(sqlite3_column_text(stmt, 1)); + record.voltage = sqlite3_column_double(stmt, 2); + record.current = sqlite3_column_double(stmt, 3); + record.power = sqlite3_column_double(stmt, 4); + record.temperature = sqlite3_column_double(stmt, 5); + data.push_back(record); + } + + sqlite3_finalize(stmt); + } else { + std::cerr << "Error preparing statement: " << sqlite3_errmsg(m_db) << std::endl; + } + + return data; +} +// 显式实例化模板 +template void SQLiteDB::insertStructData(const std::string&, const PowerData&); +template +std::string SQLiteDB::serializeStruct(const T& data) { + Json::Value j; + j["id"] = data.id; + j["timestamp"] = data.timestamp; + j["voltage"] = data.voltage; + j["current"] = data.current; + j["power"] = data.power; + j["temperature"] = data.temperature; + + Json::StreamWriterBuilder writer; + return Json::writeString(writer, j); +} + +template +void SQLiteDB::insertStructData(const std::string& tableName, const T& data) { + if (!m_db) return; + + std::string jsonData = serializeStruct(data); + + std::string sql = "INSERT INTO " + tableName + " (data) VALUES (?);"; + sqlite3_stmt* stmt; + + if (sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, nullptr) == SQLITE_OK) { + sqlite3_bind_text(stmt, 1, jsonData.c_str(), -1, SQLITE_TRANSIENT); + + if (sqlite3_step(stmt) != SQLITE_DONE) { + std::cerr << "Error inserting data: " << sqlite3_errmsg(m_db) << std::endl; + } + + sqlite3_finalize(stmt); + } else { + std::cerr << "Error preparing statement: " << sqlite3_errmsg(m_db) << std::endl; + } +} + + diff --git a/src/pPowerManger/SQLiteDB.h b/src/pPowerManger/SQLiteDB.h new file mode 100644 index 0000000..70a3fa8 --- /dev/null +++ b/src/pPowerManger/SQLiteDB.h @@ -0,0 +1,39 @@ +#ifndef SQLITEDB_H +#define SQLITEDB_H + +#include +#include +#include +#include "json/json.h" +// #include + +struct PowerData { + int id; + std::string timestamp; + double voltage; + double current; + double power; + double temperature; +}; + + +class SQLiteDB { +public: + SQLiteDB(const std::string& dbPath); + ~SQLiteDB(); + + void createTables(); + void insertPowerData(float voltage, float current, float power, float temperature); + std::vector getPowerData(int limit = 100); + + template + void insertStructData(const std::string& tableName, const T& data); + +private: + sqlite3* m_db; + + template + std::string serializeStruct(const T& data); +}; + +#endif // SQLITEDB_H diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..cb11118 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,26 @@ +cmake_minimum_required(VERSION 3.10) +project(SQLiteDBTest) + +# 设置 C++ 标准 +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# 查找 SQLiteDB 项目库 +find_library(SQLiteDB_LIB NAMES SQLiteDB PATHS /home/zjk/project/H100/H100PowerManger/lib) +if(NOT SQLiteDB_LIB) + message(FATAL_ERROR "SQLiteDB library not found!") +endif() + +# 查找 Catch2 测试框架 +find_package(Catch2 REQUIRED) + +# 添加测试可执行文件 +add_executable(SQLiteDBTest SQLiteDBTest.cpp) + +# 链接库文件(注意顺序:先项目库后系统库) +target_link_libraries(SQLiteDBTest + ${SQLiteDB_LIB} # 项目SQLiteDB库 + Catch2::Catch2WithMain # 测试框架 + sqlite3 + jsoncpp +) diff --git a/test/SQLiteDBTest.cpp b/test/SQLiteDBTest.cpp new file mode 100644 index 0000000..ee80376 --- /dev/null +++ b/test/SQLiteDBTest.cpp @@ -0,0 +1,24 @@ +#include "catch2/catch.hpp" +#include "../src/pPowerManger/SQLiteDB.h" + +TEST_CASE("SQLiteDB Test", "[SQLiteDB]") { + SQLiteDB db("test.db"); + + SECTION("Create Tables") { + db.createTables(); + } + + SECTION("Insert Data") { + PowerData data = {0, "2025-02-10 14:09:00", 12.5, 2.5, 31.25, 25.0}; + db.insertStructData("power_data", data); + } + + 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); + } +}