Commit ddc583d2 by yahan.li

为数据库操作添加打印

	修改:     RadomForestSAC/RamdomForestCalculate.cpp
	修改:     RadomForestSAC/SignalFeatures.h
	修改:     package/E3/SigerCalculation
parent e4915983
...@@ -43,33 +43,6 @@ void delete_excess_rf_models() ...@@ -43,33 +43,6 @@ void delete_excess_rf_models()
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"[delete_rf_models] to_delete={} done={}", to_delete, done); SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"[delete_rf_models] to_delete={} done={}", to_delete, done);
} }
bool checkSqliteDb(const QString& dbPath)
{
QFile file(dbPath);
if (!file.open(QIODevice::ReadOnly)) {
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"[isSqliteDb] open database failed");
return false;
}
QByteArray header = file.read(16);
file.close();
if (header.size() < 16) {
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"[isSqliteDb] read_bytes lt 16: {}", header.size());
QFile::remove(dbPath);
return false;
}
static const QByteArray sqliteMagic("SQLite format 3");
if (!header.startsWith(sqliteMagic)){
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"[isSqliteDb] read sqlite magic error");
QFile::remove(dbPath);
return false;
}
return true;
}
int pickPriorityValue(double xpred, double ypred, double zpred) int pickPriorityValue(double xpred, double ypred, double zpred)
{ {
// 四舍五入 // 四舍五入
...@@ -90,20 +63,25 @@ void CreatTable() ...@@ -90,20 +63,25 @@ void CreatTable()
QString dbPath = QString("/home/pi/SigerTMS/stream/SAC/%1.db") QString dbPath = QString("/home/pi/SigerTMS/stream/SAC/%1.db")
.arg("RamdomForestFeatureValue"); .arg("RamdomForestFeatureValue");
delete_excess_rf_models();
checkSqliteDb(dbPath);
// 判断数据库文件是否存在 // 判断数据库文件是否存在
QFileInfo fi(dbPath); QFileInfo fi(dbPath);
if (!fi.exists()) { if (fi.exists()){
qDebug() << "Database not exists. Creating..."; if (fi.size() > 0)
return;
int ret = unlink(dbPath.toStdString().c_str());
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"delete {} for zero-size: {}", dbPath.toStdString(), ret);
}
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"), "Database not exists. Creating...");
// ---- 短连接:打开 -> 执行建表语句 -> 关闭 ---- // ---- 短连接:打开 -> 执行建表语句 -> 关闭 ----
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "InitConnection"); QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "InitConnection");
db.setDatabaseName(dbPath); db.setDatabaseName(dbPath);
if (!db.open()) { if (!db.open()) {
qDebug() << "Create DB failed:" << db.lastError().text(); QString error = db.lastError().text();
SPDLOG_LOGGER_ERROR(spdlog::get("logger"), "Create DB failed: {}", error.toUtf8().constData());
return; return;
} }
...@@ -122,14 +100,14 @@ void CreatTable() ...@@ -122,14 +100,14 @@ void CreatTable()
QSqlQuery query(db); QSqlQuery query(db);
if (!query.exec(createTable)) { if (!query.exec(createTable)) {
qDebug() << "Create table error:" << query.lastError().text(); QString error = db.lastError().text();
SPDLOG_LOGGER_ERROR(spdlog::get("logger"), "Create table error: {}", error.toUtf8().constData());
} }
db.close(); db.close();
QSqlDatabase::removeDatabase("InitConnection"); QSqlDatabase::removeDatabase("InitConnection");
qDebug() << "Database and table created."; SPDLOG_LOGGER_ERROR(spdlog::get("logger"), "Database and table created.");
}
} }
bool execShortConn(QString sql, const QString &dbFile) bool execShortConn(QString sql, const QString &dbFile)
...@@ -160,7 +138,8 @@ bool execShortConn(QString sql, const QString &dbFile) ...@@ -160,7 +138,8 @@ bool execShortConn(QString sql, const QString &dbFile)
QSqlQuery query(db); QSqlQuery query(db);
ok = query.exec(sql); ok = query.exec(sql);
if (!ok) { if (!ok) {
qWarning() << "SQL ERROR:" << query.lastError(); QString error = query.lastError().text();
SPDLOG_LOGGER_ERROR(spdlog::get("logger"), "SQL ERROR: {}", error.toUtf8().constData());
} }
db.close(); db.close();
...@@ -188,6 +167,7 @@ RamdomForestCalculate::RamdomForestCalculate(QObject *parent) ...@@ -188,6 +167,7 @@ RamdomForestCalculate::RamdomForestCalculate(QObject *parent)
CreatTable(); CreatTable();
//3.训练初始模型 //3.训练初始模型
delete_excess_rf_models();
Retrain(); Retrain();
//GLS Test //GLS Test
...@@ -256,91 +236,8 @@ void RamdomForestCalculate::SendStreamAlarminfo(const StreamInfo &alarmInfo) ...@@ -256,91 +236,8 @@ void RamdomForestCalculate::SendStreamAlarminfo(const StreamInfo &alarmInfo)
tcp_client.sendData(QString(QJsonDocument(json).toJson(QJsonDocument::Compact)).toStdString().c_str()); tcp_client.sendData(QString(QJsonDocument(json).toJson(QJsonDocument::Compact)).toStdString().c_str());
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"发送流式报警信息成功"); SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"发送流式报警信息成功");
// 创建短连接 TcpClient
// TcpClient* client = new TcpClient();
// // 构造消息
// QJsonObject message;
// message["source"] = STREAM_MESSAGE;
// message["typealarm"] = QString::number(alarmInfo.alarmtype);
// message["target"] = alarmInfo.target;
// message["startIds"] = alarmInfo.startIds;
// message["endIds"] = alarmInfo.endIds;
// // 连接成功后发送消息
// QObject::connect(client, &TcpClient::connected, [client, message]() {
// qDebug() << "=== 连接成功,开始发送报警消息 ===";
// client->sendMessage(message);
// // 延迟关闭,确保消息发送完成
// QTimer::singleShot(200, client, [client]() {
// //client->disconnectFromServer();
// client->deleteLater();
// });
// SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),
// "发送流式报警信息成功");
// });
// // 连接错误也自动释放
// QObject::connect(client, &TcpClient::errorOccurred, [client](const QString &err) {
// qWarning() << "TcpClient错误:" << err;
// client->deleteLater();
// });
// // 发起连接
// client->connectToServer("127.0.0.1", STREAM_PORT);
} }
//void RamdomForestCalculate::SendStreamAlarminfo(const StreamInfo &alarmInfo)
//{
// TcpClient* client = new TcpClient();
// // 统一的清理函数
// auto cleanup = [client]() {
// static bool cleaned = false; // 静态变量确保只清理一次收到 TMS 信号
// if (!cleaned) {
// cleaned = true;
// //qDebug() << "清理客户端资源";
// client->disconnectFromServer();
// QTimer::singleShot(0, client, &QObject::deleteLater); // 延迟删除确保安全
// }
// };
// // 连接所有信号到统一的清理函数
// QObject::connect(client, &TcpClient::connected, [client, alarmInfo, cleanup]() {
// qDebug() << "=== 连接成功,开始发送报警消息 ===";
// QJsonObject message;
// message["source"] = STREAM_MESSAGE;
// message["typealarm"] = QString::number(alarmInfo.alarmtype);
// message["target"] = alarmInfo.target;
// message["startIds"] = alarmInfo.startIds;
// message["endIds"] = alarmInfo.endIds;
// //message["power"] = alarmInfo.endIds;
// client->sendMessage(message);
// SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"发送给流式告警信息成功");
// // 发送完成后立即开始清理
// cleanup();
// });
// QObject::connect(client, &TcpClient::disconnected, cleanup);
// QObject::connect(client, &TcpClient::messageReceived, cleanup);
// QObject::connect(client, &TcpClient::errorOccurred, cleanup);
// // 设置连接超时(5秒)
// QTimer::singleShot(5000, client, cleanup);
// // 连接到服务器
// qDebug() << "正在连接到服务器...";
// client->connectToServer("127.0.0.1", STREAM_PORT);
//}
int RamdomForestCalculate::Save_SACDATA(const Dc_SacData &sacdata) int RamdomForestCalculate::Save_SACDATA(const Dc_SacData &sacdata)
{ {
std::array<std::vector<float>, 3> raw; std::array<std::vector<float>, 3> raw;
...@@ -350,7 +247,7 @@ int RamdomForestCalculate::Save_SACDATA(const Dc_SacData &sacdata) ...@@ -350,7 +247,7 @@ int RamdomForestCalculate::Save_SACDATA(const Dc_SacData &sacdata)
//step1: 读取二进制原始文件 //step1: 读取二进制原始文件
VibrationDataReader reader; VibrationDataReader reader;
if (reader.readFileRaw(FilePath)) { if (reader.readFileRaw(FilePath)) {
reader.printDataInfo(); //reader.printDataInfo();
} }
FeatureExtractor extractor; FeatureExtractor extractor;
...@@ -412,7 +309,7 @@ int RamdomForestCalculate::Save_SACDATA(const Dc_SacData &sacdata) ...@@ -412,7 +309,7 @@ int RamdomForestCalculate::Save_SACDATA(const Dc_SacData &sacdata)
bool ok = execShortConn(sql, dbPath); bool ok = execShortConn(sql, dbPath);
if (!ok) { if (!ok) {
qWarning() << "Insert failed!"; SPDLOG_LOGGER_ERROR(spdlog::get("logger"), "Insert failed!");
} }
//dbFunc->addDataToQueue(data); //dbFunc->addDataToQueue(data);
...@@ -489,8 +386,6 @@ bool RamdomForestCalculate::queryRecordsByLabel(const QString &dbFileName, ...@@ -489,8 +386,6 @@ bool RamdomForestCalculate::queryRecordsByLabel(const QString &dbFileName,
} else { } else {
// 随机替换 // 随机替换
int replace_index = std::rand() % max_normal_per_target; int replace_index = std::rand() % max_normal_per_target;
if (replace_index < 0)
replace_index = -replace_index;
target_pair.second[replace_index] = record; target_pair.second[replace_index] = record;
} }
} }
......
...@@ -73,12 +73,17 @@ struct SignalFeatures ...@@ -73,12 +73,17 @@ struct SignalFeatures
bool isValid() bool isValid()
{ {
bool isAllZero = true;
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
{ {
if (!std::isfinite(data[i]) || std::abs(data[i]) > 1e10f) if (!std::isfinite(data[i]) || std::abs(data[i]) > 1e10f)
return false; return false;
if (std::abs(data[i]) > 1e-10f)
isAllZero = false;
} }
return true;
return (!isAllZero);
} }
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment