为状态机增加了线程运行,并补充了数据库
This commit is contained in:
@@ -1,47 +1,47 @@
|
||||
{
|
||||
"HVABFTDev123Cur_uint16": 34345,
|
||||
"HVABactCur_uint16": 22235,
|
||||
"HVABbowRudCur_uint16": 24015,
|
||||
"HVABbusBCur_uint16": 53248,
|
||||
"HVABbusBVol_uint16": 0,
|
||||
"HVABmastSteGeaCur_uint16": 127,
|
||||
"HVABopenWaterCur_uint16": 64891,
|
||||
"HVABxczCur_uint16": 63488,
|
||||
"HVBBCCUCur_uint16": 16752,
|
||||
"HVBBFCSSecurSysCur_uint16": 22235,
|
||||
"HVBBbatInsCur_uint16": 0,
|
||||
"HVBBbowLowVolDisBoxCur_uint16": 34349,
|
||||
"HVBBdcC1InsCur_uint16": 22235,
|
||||
"HVBBemer2BusVol_uint16": 32768,
|
||||
"HVBBemerBatCur_uint16": 0,
|
||||
"HVBBinsBusVol_uint16": 22528,
|
||||
"HVABFTDev123Cur_uint16": 0,
|
||||
"HVABactCur_uint16": 0,
|
||||
"HVABbowRudCur_uint16": 0,
|
||||
"HVABbusBCur_uint16": 1792,
|
||||
"HVABbusBVol_uint16": 127,
|
||||
"HVABmastSteGeaCur_uint16": 0,
|
||||
"HVABopenWaterCur_uint16": 23040,
|
||||
"HVABxczCur_uint16": 0,
|
||||
"HVBBCCUCur_uint16": 44210,
|
||||
"HVBBFCSSecurSysCur_uint16": 23197,
|
||||
"HVBBbatInsCur_uint16": 33584,
|
||||
"HVBBbowLowVolDisBoxCur_uint16": 44214,
|
||||
"HVBBdcC1InsCur_uint16": 23197,
|
||||
"HVBBemer2BusVol_uint16": 57344,
|
||||
"HVBBemerBatCur_uint16": 57344,
|
||||
"HVBBinsBusVol_uint16": 26624,
|
||||
"HVBBplcCur_uint16": 127,
|
||||
"HVBBreservedCur1_uint16": 127,
|
||||
"HVBBreservedCur2_uint16": 0,
|
||||
"HVBBunit4Cur_uint16": 0,
|
||||
"HVBFTDev45Cur_uint16": 32598,
|
||||
"HVBbatGroupCur_uint16": 61904,
|
||||
"HVBbowHVolDisBoxCur_uint16": 56157,
|
||||
"HVBBreservedCur2_uint16": 127,
|
||||
"HVBBunit4Cur_uint16": 23197,
|
||||
"HVBFTDev45Cur_uint16": 32765,
|
||||
"HVBbatGroupCur_uint16": 448,
|
||||
"HVBbowHVolDisBoxCur_uint16": 24982,
|
||||
"HVBbusACur_uint16": 0,
|
||||
"HVBbusAVol_uint16": 0,
|
||||
"HVBbusAVol_uint16": 40256,
|
||||
"HVBdcDc5Cur_uint16": 0,
|
||||
"HVBfbCur_uint16": 0,
|
||||
"HVBmotorCur_uint16": 0,
|
||||
"HVBpowerBusCur_uint16": 6,
|
||||
"HVBpowerBusVol_uint16": 32598,
|
||||
"HVBreservedCur1_uint16": 0,
|
||||
"HVBfbCur_uint16": 49495,
|
||||
"HVBmotorCur_uint16": 32602,
|
||||
"HVBpowerBusCur_uint16": 31744,
|
||||
"HVBpowerBusVol_uint16": 32602,
|
||||
"HVBreservedCur1_uint16": 24982,
|
||||
"HVBreservedCur2_uint16": 0,
|
||||
"HVBreservedCur3_uint16": 0,
|
||||
"HVBsternRud1Cur_uint16": 1,
|
||||
"HVBsternRud2Cur_uint16": 0,
|
||||
"HVBsternRud1Cur_uint16": 440,
|
||||
"HVBsternRud2Cur_uint16": 32765,
|
||||
"HVBtyzCur_uint16": 0,
|
||||
"LVBBbowPZDevCur_uint16": 127,
|
||||
"LVBBemerBatCur_uint16": 32768,
|
||||
"LVBBemerBusVol_uint16": 16752,
|
||||
"LVBBinsBusCur_uint16": 22235,
|
||||
"LVBBunit1Cur_uint16": 127,
|
||||
"LVBBunit2Cur_uint16": 256,
|
||||
"LVBBbowPZDevCur_uint16": 0,
|
||||
"LVBBemerBatCur_uint16": 768,
|
||||
"LVBBemerBusVol_uint16": 0,
|
||||
"LVBBinsBusCur_uint16": 97,
|
||||
"LVBBunit1Cur_uint16": 0,
|
||||
"LVBBunit2Cur_uint16": 0,
|
||||
"LVBBunit3Cur_uint16": 0,
|
||||
"LVBBunit5Cur_uint16": 22016,
|
||||
"LVBinsBusVol_uint16": 16752
|
||||
"LVBBunit5Cur_uint16": 0,
|
||||
"LVBinsBusVol_uint16": 38402
|
||||
}
|
||||
@@ -1,30 +1,30 @@
|
||||
{
|
||||
"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": 1.8734974449861263e+18,
|
||||
"fcuFaultLeve_uint8": 0,
|
||||
"fcuOutIns_fp32": 1.1693958999055459e-38,
|
||||
"fcuOutPower_fp32": 1.7755460477889746e-38,
|
||||
"fcuState_uint8": 0,
|
||||
"fcuVolIns_fp32": 2.0215532827359314e+18,
|
||||
"fcuVolPower_fp32": 1.1694265883419146e-38,
|
||||
"fcuVol_fp32": 1.1694265883419146e-38,
|
||||
"backpressure1_fp32": 7.7812956078560092e-10,
|
||||
"backpressure2_fp32": 0.0,
|
||||
"ch4oConcent1_fp32": -4.8547832420808845e-12,
|
||||
"ch4oConcent2_fp32": 1.1695613932541827e-38,
|
||||
"ch4o_fp32": 1.1695613932541827e-38,
|
||||
"excTemp_fp32": 1.1695613932541827e-38,
|
||||
"faultCode1_uint16": 34036,
|
||||
"faultCode2_uint16": 40361,
|
||||
"faultCode3_uint16": 32602,
|
||||
"fcuCurIns_fp32": -1.2859852072111266e-13,
|
||||
"fcuCurPower_fp32": 1.1695613932541827e-38,
|
||||
"fcuCur_fp32": -1.6155215737897056e+25,
|
||||
"fcuFaultLeve_uint8": 98,
|
||||
"fcuOutIns_fp32": 1.1695613932541827e-38,
|
||||
"fcuOutPower_fp32": 8.3046377128711946e+19,
|
||||
"fcuState_uint8": 105,
|
||||
"fcuVolIns_fp32": -1.0396555642088759e-25,
|
||||
"fcuVolPower_fp32": 1.1754003236953313e-38,
|
||||
"fcuVol_fp32": 1.1695613932541827e-38,
|
||||
"flameDet_uint8": 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
|
||||
"h2Concent1_fp32": 1.7280140038790768e-39,
|
||||
"h2Concent2_fp32": 0.0,
|
||||
"o2Concent1_fp32": -1.2491396805813793e-13,
|
||||
"o2Concent2_fp32": 1.1695613932541827e-38,
|
||||
"o2Level_fp32": 1.9618178500547439e-44,
|
||||
"o2_fp32": -5.3151927303929369e-14,
|
||||
"powLimit_fp32": 1.1695613932541827e-38,
|
||||
"powerGen_fp32": -1.2491396805813793e-13
|
||||
}
|
||||
@@ -1,38 +1,38 @@
|
||||
{
|
||||
"fcuCurToInsLimit1_uint8": 127,
|
||||
"fcuCurToinsLimit2_uint8": 0,
|
||||
"insBatCurElect_uint16": 0,
|
||||
"insBatLimit_uint16": 3817,
|
||||
"insBatCurElect_uint16": 32765,
|
||||
"insBatLimit_uint16": 40361,
|
||||
"insBatSoc_uint8": 0,
|
||||
"insBatTotalElect_uint16": 53312,
|
||||
"insChargeState_uint8": 0,
|
||||
"insCurSocHold1_uint8": 134,
|
||||
"insCurSocHold2_uint8": 219,
|
||||
"insCurSocHold3_uint8": 86,
|
||||
"insBatTotalElect_uint16": 244,
|
||||
"insChargeState_uint8": 202,
|
||||
"insCurSocHold1_uint8": 172,
|
||||
"insCurSocHold2_uint8": 157,
|
||||
"insCurSocHold3_uint8": 90,
|
||||
"insCurrent_uint16": 0,
|
||||
"insLinkVol_uint16": 0,
|
||||
"insLinkVol_uint16": 40362,
|
||||
"insNegRelayState_uint8": 0,
|
||||
"insNegRes_uint16": 0,
|
||||
"insPackVol_uint16": 0,
|
||||
"insPackVol_uint16": 32602,
|
||||
"insPosRelayState_uint8": 0,
|
||||
"insPosRes_uint16": 3,
|
||||
"insfaultCode_uint8": 88,
|
||||
"insPosRes_uint16": 37,
|
||||
"insfaultCode_uint8": 208,
|
||||
"powBatCurElect_uint16": 0,
|
||||
"powBatSoc_uint8": 0,
|
||||
"powBatTotalElect_uint16": 31581,
|
||||
"powChargeState_uint8": 0,
|
||||
"powBatTotalElect_uint16": 24982,
|
||||
"powChargeState_uint8": 16,
|
||||
"powCurSocHold1_uint8": 0,
|
||||
"powCurSocHold2_uint8": 208,
|
||||
"powCurSocHold3_uint8": 241,
|
||||
"powCurToInsLimit1_uint8": 93,
|
||||
"powCurToinsLimit2_uint8": 219,
|
||||
"powCur_uint16": 61767,
|
||||
"powCurSocHold2_uint8": 248,
|
||||
"powCurSocHold3_uint8": 0,
|
||||
"powCurToInsLimit1_uint8": 150,
|
||||
"powCurToinsLimit2_uint8": 97,
|
||||
"powCur_uint16": 35008,
|
||||
"powLinkVol_uint16": 0,
|
||||
"powNegRelayState_uint8": 85,
|
||||
"powNegRes_uint16": 32598,
|
||||
"powPosRelayState_uint8": 208,
|
||||
"powPosRes_uint16": 56067,
|
||||
"powerBatLimit_uint16": 0,
|
||||
"powNegRelayState_uint8": 133,
|
||||
"powNegRes_uint16": 32602,
|
||||
"powPosRelayState_uint8": 0,
|
||||
"powPosRes_uint16": 40361,
|
||||
"powerBatLimit_uint16": 32602,
|
||||
"powerPackVol_uint16": 0,
|
||||
"powfaultCode_uint8": 45
|
||||
"powfaultCode_uint8": 170
|
||||
}
|
||||
@@ -20,6 +20,12 @@ bool _ListenCB(void * pParam)
|
||||
return pMe->ListenLoopUDP();
|
||||
}
|
||||
|
||||
bool _FsmCB(void * pParam)
|
||||
{
|
||||
PowerManger* pMe = (PowerManger* )pParam;
|
||||
return pMe->FsmLoop();
|
||||
}
|
||||
|
||||
//---------------------------------------------------------
|
||||
// Constructor()
|
||||
|
||||
@@ -38,9 +44,9 @@ PowerManger::PowerManger()
|
||||
//初始化数据库
|
||||
string dbPath = "/home/zjk/project/H100/H100PowerManger/data/power_data.db";
|
||||
m_db = new SQLiteDB(dbPath);
|
||||
m_db->createTables();
|
||||
|
||||
|
||||
m_db->createPowerDataTables();
|
||||
m_db->createStateTables();
|
||||
|
||||
//注册状态机
|
||||
m_fsm.RegisterState("Init", 1, m_Inite_s1);
|
||||
m_fsm.RegisterState("Fault", 99, m_Fault_s99);
|
||||
@@ -111,10 +117,14 @@ bool PowerManger::Iterate()
|
||||
updatePmState();
|
||||
updateTime();
|
||||
//2. 运行状态机
|
||||
m_fsm.run(m_pmState,this);
|
||||
// m_fsm.run(m_pmState,this);
|
||||
if (FSM_RUN_IS_OK)
|
||||
m_FsmThread.Start();
|
||||
else
|
||||
m_FsmThread.Stop();
|
||||
// 数据库测试
|
||||
PowerData data;
|
||||
data.id = 1;
|
||||
// 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;
|
||||
@@ -188,6 +198,7 @@ bool PowerManger::OnStartUp()
|
||||
}
|
||||
m_ListenThread.Initialise(_ListenCB,this);
|
||||
m_ListenThread.Start();
|
||||
m_FsmThread.Initialise(_FsmCB,this);
|
||||
return(true);
|
||||
}
|
||||
|
||||
@@ -259,6 +270,17 @@ bool PowerManger::ListenLoopUDP()
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PowerManger::FsmLoop()
|
||||
{
|
||||
while (!m_FsmThread.IsQuitRequested())
|
||||
{
|
||||
m_fsm.run(m_pmState,this);
|
||||
//延时200ms
|
||||
usleep(200000);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool PowerManger::updatePmState()
|
||||
{
|
||||
@@ -338,12 +360,11 @@ string PowerManger::getTypeName(const string typeName)
|
||||
} else if (typeName == "St6vector" || typeName == "St5deque" || typeName == "St4list" || typeName == "St4pair") {
|
||||
return "_array";
|
||||
} else {
|
||||
// 默认情况下,我们假设它是一个对象
|
||||
// 默认情况
|
||||
return "_obj";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool PowerManger::buildPowerSysReport()
|
||||
{
|
||||
//TODO: 构建能源系统报告
|
||||
@@ -455,12 +476,6 @@ bool PowerManger::buildPowerSysReport()
|
||||
suffix = getTypeName(typeid(m_pmCurrentState.ccustate.flameDetection).name());
|
||||
fcs[VAR_TO_STR(flameDet)+suffix] = m_pmCurrentState.ccustate.flameDetection;
|
||||
|
||||
// unsigned char fcuGZ_1L;
|
||||
// unsigned char fcuGZ_1H;
|
||||
// unsigned char fcuGZ_2L;
|
||||
// unsigned char fcuGZ_2H;
|
||||
// unsigned char fcuGZ_3L;
|
||||
// unsigned char fcuGZ_3H;
|
||||
unsigned short faultCode1;
|
||||
unsigned short faultCode2;
|
||||
unsigned short faultCode3;
|
||||
@@ -801,6 +816,16 @@ bool PowerManger::disSystemOperation(disBreaker optBreaker, disBreakerOpt)
|
||||
|
||||
return true;
|
||||
}
|
||||
void PowerManger::printState(FSM_State st, string desp){
|
||||
cout << MOOS::ConsoleColours::Yellow() << "STATE " << st.id << " [" << st.name << "] " << MOOS::ConsoleColours::reset();
|
||||
cout << MOOS::ConsoleColours::Blue() << " -> " << MOOS::ConsoleColours::reset();
|
||||
// 检查desp是否有“故障”
|
||||
if (desp.find("故障") != string::npos) {
|
||||
cout << MOOS::ConsoleColours::Red() << desp << MOOS::ConsoleColours::reset() << endl;
|
||||
return;
|
||||
}
|
||||
cout << MOOS::ConsoleColours::Green() << desp << MOOS::ConsoleColours::reset() << endl;
|
||||
}
|
||||
|
||||
int PowerManger::handleInite_s1(FSM_State &fsm, void* pData)
|
||||
{
|
||||
|
||||
@@ -54,6 +54,7 @@ class PowerManger : public AppCastingMOOSApp
|
||||
~PowerManger();
|
||||
public:
|
||||
bool ListenLoopUDP();
|
||||
bool FsmLoop();
|
||||
|
||||
protected: // Standard MOOSApp functions to overload
|
||||
bool OnNewMail(MOOSMSG_LIST &NewMail);
|
||||
@@ -97,6 +98,7 @@ class PowerManger : public AppCastingMOOSApp
|
||||
udpComm m_udpComm;
|
||||
XPCUdpSocket *m_pCcuUdpComm;
|
||||
CMOOSThread m_ListenThread;
|
||||
CMOOSThread m_FsmThread;
|
||||
long m_lPort;
|
||||
unsigned int m_nReceiveBufferSizeKB;
|
||||
unsigned int m_nSendBufferSizeKB;
|
||||
@@ -113,17 +115,8 @@ class PowerManger : public AppCastingMOOSApp
|
||||
|
||||
public: //状态实现
|
||||
|
||||
void printState(FSM_State st, string desp){
|
||||
cout << MOOS::ConsoleColours::Yellow() << "STATE " << st.id << " [" << st.name << "] " << MOOS::ConsoleColours::reset();
|
||||
cout << MOOS::ConsoleColours::Blue() << " -> " << MOOS::ConsoleColours::reset();
|
||||
// 检查desp是否有“故障”
|
||||
if (desp.find("故障") != string::npos) {
|
||||
cout << MOOS::ConsoleColours::Red() << desp << MOOS::ConsoleColours::reset() << endl;
|
||||
return;
|
||||
}
|
||||
cout << MOOS::ConsoleColours::Green() << desp << MOOS::ConsoleColours::reset() << endl;
|
||||
}
|
||||
|
||||
void printState(FSM_State st, string desp);
|
||||
bool FSM_RUN_IS_OK = true;
|
||||
FSM_State m_pmState; //当前状态
|
||||
string m_pmEvent;
|
||||
FSM m_fsm;
|
||||
|
||||
@@ -16,7 +16,7 @@ SQLiteDB::~SQLiteDB() {
|
||||
}
|
||||
}
|
||||
|
||||
void SQLiteDB::createTables() {
|
||||
void SQLiteDB::createPowerDataTables() {
|
||||
if (!m_db) return;
|
||||
|
||||
const char* sql = "CREATE TABLE IF NOT EXISTS power_data ("
|
||||
@@ -34,6 +34,24 @@ void SQLiteDB::createTables() {
|
||||
}
|
||||
}
|
||||
|
||||
void SQLiteDB::createStateTables() {
|
||||
if (!m_db) return;
|
||||
|
||||
const char* sql = "CREATE TABLE IF NOT EXISTS state_data ("
|
||||
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||
"timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,"
|
||||
"state_id INTEGER NOT NULL,"
|
||||
"state_name TEXT NOT NULL,"
|
||||
"event TEXT NOT NULL,"
|
||||
"event_id INTEGER NOT NULL,"
|
||||
"fault_code INTEGER 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;
|
||||
|
||||
@@ -56,6 +74,29 @@ void SQLiteDB::insertPowerData(float voltage, float current, float power, float
|
||||
}
|
||||
}
|
||||
|
||||
void SQLiteDB::insertStateData(StateData stateData) {
|
||||
if (!m_db) return;
|
||||
|
||||
const char* sql = "INSERT INTO state_data (timestamp, state_id, state_name, event, event_id, fault_code) VALUES (?, ?, ?, ?, ?, ?);";
|
||||
sqlite3_stmt* stmt;
|
||||
|
||||
if (sqlite3_prepare_v2(m_db, sql, -1, &stmt, nullptr) == SQLITE_OK) {
|
||||
sqlite3_bind_text(stmt, 1, stateData.timestamp.c_str(), -1, SQLITE_TRANSIENT);
|
||||
sqlite3_bind_int(stmt, 2, stateData.state_id);
|
||||
sqlite3_bind_text(stmt, 3, stateData.state_name.c_str(), -1, SQLITE_TRANSIENT);
|
||||
sqlite3_bind_text(stmt, 4, stateData.event.c_str(), -1, SQLITE_TRANSIENT);
|
||||
sqlite3_bind_int(stmt, 5, stateData.event_id);
|
||||
sqlite3_bind_int(stmt, 6, stateData.fault_code);
|
||||
|
||||
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<PowerData> SQLiteDB::getPowerData(int limit) {
|
||||
std::vector<PowerData> data;
|
||||
if (!m_db) return data;
|
||||
@@ -68,7 +109,7 @@ std::vector<PowerData> SQLiteDB::getPowerData(int limit) {
|
||||
|
||||
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
PowerData record;
|
||||
record.id = sqlite3_column_int(stmt, 0);
|
||||
// record.id = sqlite3_column_int(stmt, 0);
|
||||
record.timestamp = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
|
||||
record.voltage = sqlite3_column_double(stmt, 2);
|
||||
record.current = sqlite3_column_double(stmt, 3);
|
||||
@@ -83,43 +124,4 @@ std::vector<PowerData> SQLiteDB::getPowerData(int limit) {
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
// 显式实例化模板
|
||||
template void SQLiteDB::insertStructData<PowerData>(const std::string&, const PowerData&);
|
||||
template<typename T>
|
||||
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<typename T>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -8,7 +8,6 @@
|
||||
// #include <nlohmann/json.hpp>
|
||||
|
||||
struct PowerData {
|
||||
int id;
|
||||
std::string timestamp;
|
||||
double voltage;
|
||||
double current;
|
||||
@@ -16,24 +15,28 @@ struct PowerData {
|
||||
double temperature;
|
||||
};
|
||||
|
||||
struct StateData {
|
||||
std::string timestamp;
|
||||
int state_id;
|
||||
std::string state_name;
|
||||
std::string event;
|
||||
int event_id;
|
||||
int fault_code;
|
||||
};
|
||||
|
||||
class SQLiteDB {
|
||||
public:
|
||||
SQLiteDB(const std::string& dbPath);
|
||||
~SQLiteDB();
|
||||
|
||||
void createTables();
|
||||
void createPowerDataTables();
|
||||
void insertPowerData(float voltage, float current, float power, float temperature);
|
||||
void createStateTables();
|
||||
void insertStateData(StateData stateData);
|
||||
std::vector<PowerData> getPowerData(int limit = 100);
|
||||
|
||||
template<typename T>
|
||||
void insertStructData(const std::string& tableName, const T& data);
|
||||
|
||||
|
||||
private:
|
||||
sqlite3* m_db;
|
||||
|
||||
template<typename T>
|
||||
std::string serializeStruct(const T& data);
|
||||
};
|
||||
|
||||
#endif // SQLITEDB_H
|
||||
|
||||
@@ -5,14 +5,10 @@ TEST_CASE("SQLiteDB Test", "[SQLiteDB]") {
|
||||
SQLiteDB db("test.db");
|
||||
|
||||
SECTION("Create Tables") {
|
||||
db.createTables();
|
||||
db.createPowerDataTables();
|
||||
db.createStateTables();
|
||||
}
|
||||
|
||||
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<PowerData> results = db.getPowerData();
|
||||
REQUIRE(results.size() == 1);
|
||||
|
||||
Reference in New Issue
Block a user