优化了主页的读取方式
This commit is contained in:
@@ -550,27 +550,98 @@ bool SQLiteDB::insertStateData(StateData stateData,bool filter) {
|
||||
}
|
||||
|
||||
bool SQLiteDB::getStateData(StateData &stateData) {
|
||||
if (!m_db) return false;
|
||||
if (!m_db) {
|
||||
std::cerr << "Database not initialized" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* sql = "SELECT * FROM state_data ORDER BY timestamp DESC LIMIT 1;";
|
||||
sqlite3_stmt* stmt;
|
||||
sqlite3_stmt* stmt = nullptr;
|
||||
bool result = false;
|
||||
|
||||
if (sqlite3_prepare_v2(m_db, sql, -1, &stmt, nullptr) == SQLITE_OK) {
|
||||
if (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
try {
|
||||
if (sqlite3_prepare_v2(m_db, sql, -1, &stmt, nullptr) != SQLITE_OK) {
|
||||
throw std::runtime_error(sqlite3_errmsg(m_db));
|
||||
}
|
||||
|
||||
int stepResult = sqlite3_step(stmt);
|
||||
if (stepResult == SQLITE_ROW) {
|
||||
stateData.state_id = sqlite3_column_int(stmt, 1);
|
||||
stateData.state_name = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3));
|
||||
stateData.event = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 4));
|
||||
|
||||
const char* stateName = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3));
|
||||
stateData.state_name = stateName ? stateName : "";
|
||||
|
||||
const char* event = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 4));
|
||||
stateData.event = event ? event : "";
|
||||
|
||||
stateData.event_id = sqlite3_column_int(stmt, 5);
|
||||
stateData.fault_code = sqlite3_column_int(stmt, 6);
|
||||
stateData.note = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 7));
|
||||
sqlite3_finalize(stmt);
|
||||
return true;
|
||||
|
||||
const char* note = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 7));
|
||||
stateData.note = note ? note : "";
|
||||
|
||||
result = true;
|
||||
} else if (stepResult != SQLITE_DONE) {
|
||||
throw std::runtime_error(sqlite3_errmsg(m_db));
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
} else {
|
||||
std::cerr << "Error preparing statement: " << sqlite3_errmsg(m_db) << std::endl;
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error retrieving state data: " << e.what() << std::endl;
|
||||
}
|
||||
return false;
|
||||
|
||||
if (stmt) {
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool SQLiteDB::getStateData(StateData &stateData, string &time) {
|
||||
if (!m_db) {
|
||||
std::cerr << "Database not initialized" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* sql = "SELECT * FROM state_data ORDER BY timestamp DESC LIMIT 1;";
|
||||
sqlite3_stmt* stmt = nullptr;
|
||||
bool result = false;
|
||||
|
||||
try {
|
||||
if (sqlite3_prepare_v2(m_db, sql, -1, &stmt, nullptr) != SQLITE_OK) {
|
||||
throw std::runtime_error(sqlite3_errmsg(m_db));
|
||||
}
|
||||
|
||||
int stepResult = sqlite3_step(stmt);
|
||||
if (stepResult == SQLITE_ROW) {
|
||||
stateData.state_id = sqlite3_column_int(stmt, 1);
|
||||
|
||||
const char* timestamp = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
|
||||
time = timestamp ? timestamp : "";
|
||||
|
||||
const char* stateName = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3));
|
||||
stateData.state_name = stateName ? stateName : "";
|
||||
|
||||
const char* event = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 4));
|
||||
stateData.event = event ? event : "";
|
||||
|
||||
stateData.event_id = sqlite3_column_int(stmt, 5);
|
||||
stateData.fault_code = sqlite3_column_int(stmt, 6);
|
||||
|
||||
const char* note = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 7));
|
||||
stateData.note = note ? note : "";
|
||||
|
||||
result = true;
|
||||
} else if (stepResult != SQLITE_DONE) {
|
||||
throw std::runtime_error(sqlite3_errmsg(m_db));
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error retrieving state data: " << e.what() << std::endl;
|
||||
}
|
||||
|
||||
if (stmt) {
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool SQLiteDB::isDatabaseConnected(const std::string& path, const std::string& dbName) {
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "pmSysvariable.h"
|
||||
#include "driver.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
struct PowerData {
|
||||
double voltage;
|
||||
@@ -26,13 +27,6 @@ struct StateData {
|
||||
std::string note;
|
||||
};
|
||||
|
||||
// 前向声明
|
||||
// struct ccuState;
|
||||
// struct disHighVolBusState;
|
||||
// struct disHighAVolBusState;
|
||||
// struct disHighBVolBusState;
|
||||
// struct disLowBusState;
|
||||
|
||||
class SQLiteDB {
|
||||
public:
|
||||
SQLiteDB(const std::string& dbPath);
|
||||
@@ -76,6 +70,7 @@ public:
|
||||
|
||||
// 获取最新的状态数据
|
||||
bool getStateData(StateData &stateData);
|
||||
bool getStateData(StateData &stateData,string &time);
|
||||
|
||||
// 使用函数
|
||||
bool isDatabaseConnected(const std::string& path, const std::string& dbName);
|
||||
|
||||
Reference in New Issue
Block a user