|
@@ -141,33 +141,38 @@ bool firingWidget::uploadToDanLing(const QJsonObject &jsonObj) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-bool firingWidget::recordBlastEquipments(const QJsonObject &jsonObj) {
|
|
|
+QList<HBlastEquipmentRecord *> firingWidget::recordBlastEquipments(const QJsonObject &jsonObj) {
|
|
|
+ QList<HBlastEquipmentRecord *> savedRecords;
|
|
|
QJsonArray regsArray = jsonObj["regs"].toArray();
|
|
|
for (const auto ®Value : regsArray) {
|
|
|
QJsonObject regObj = regValue.toObject();
|
|
|
- HBlastRegRecord recordReg;
|
|
|
+ HBlastEquipmentRecord *recordReg = new HBlastEquipmentRecord();
|
|
|
reg_uuid =
|
|
|
QUuid::createUuid().toString(QUuid::WithoutBraces).remove(QRegularExpression("-"));
|
|
|
- recordReg.setUuid(reg_uuid);
|
|
|
- recordReg.setEquipSn(regObj["equipment_sn"].toString());
|
|
|
- recordReg.setEquipVersion(regObj["equipment_version"].toString());
|
|
|
- recordReg.setRegDetoCount(regObj["reg_deto_count"].toString().toInt());
|
|
|
- recordReg.setErrorDetoCount(regObj["error_deto_count"].toString().toInt());
|
|
|
- recordReg.setAfterTestBusV(regObj["after_test_bus_v"].toString());
|
|
|
- recordReg.setAfterTestBusI(regObj["after_test_bus_i"].toString());
|
|
|
- recordReg.setBusLeakageCurrentI(regObj["bus_leakage_current_i"].toString());
|
|
|
- recordReg.setNetChargedV(regObj["net_charged_v"].toString());
|
|
|
- recordReg.setNetChargedI(regObj["net_charged_i"].toString());
|
|
|
- recordReg.setBeforeBlastingV(regObj["before_blasting_v"].toString());
|
|
|
- recordReg.setBeforeBlastingI(regObj["before_blasting_i"].toString());
|
|
|
- recordReg.setRecordUuid(blast_uuid);
|
|
|
- recordReg.setCreatedAt(QDateTime::currentDateTime());
|
|
|
- // 插入数据到数据库
|
|
|
- if (!daoReg.saveHBlastRegRecord(recordReg)) {
|
|
|
- return false;
|
|
|
+ recordReg->setUuid(reg_uuid);
|
|
|
+ recordReg->setEquipSn(regObj["equipment_sn"].toString());
|
|
|
+ recordReg->setEquipVersion(regObj["equipment_version"].toString());
|
|
|
+ recordReg->setRegDetoCount(regObj["reg_deto_count"].toString().toInt());
|
|
|
+ recordReg->setErrorDetoCount(regObj["error_deto_count"].toString().toInt());
|
|
|
+ recordReg->setAfterTestBusV(regObj["after_test_bus_v"].toString());
|
|
|
+ recordReg->setAfterTestBusI(regObj["after_test_bus_i"].toString());
|
|
|
+ recordReg->setBusLeakageCurrentI(regObj["bus_leakage_current_i"].toString());
|
|
|
+ recordReg->setNetChargedV(regObj["net_charged_v"].toString());
|
|
|
+ recordReg->setNetChargedI(regObj["net_charged_i"].toString());
|
|
|
+ recordReg->setBeforeBlastingV(regObj["before_blasting_v"].toString());
|
|
|
+ recordReg->setBeforeBlastingI(regObj["before_blasting_i"].toString());
|
|
|
+ recordReg->setRecordUuid(blast_uuid);
|
|
|
+ recordReg->setCreatedAt(QDateTime::currentDateTime());
|
|
|
+
|
|
|
+ if (daoReg.saveHBlastRegRecord(*recordReg)) {
|
|
|
+ savedRecords.append(recordReg);
|
|
|
+ } else {
|
|
|
+ Logger::getInstance().error(
|
|
|
+ QString("Failed to save HBlastRegRecord: %1").arg(recordReg->getEquipSn()));
|
|
|
+ delete recordReg;
|
|
|
}
|
|
|
}
|
|
|
- return true;
|
|
|
+ return savedRecords;
|
|
|
}
|
|
|
|
|
|
void firingWidget::handleMessageAndTopic(const QByteArray &message, const QMqttTopicName &topic) {
|
|
@@ -369,8 +374,8 @@ void firingWidget::on_sendTest_4_clicked() {}
|
|
|
void firingWidget::testonBlastSucess(const QJsonObject &jsonObj) { onBlastSucess(jsonObj); }
|
|
|
// 处理爆破成功,安全验证装置通过MQTT传回的数据
|
|
|
void firingWidget::onBlastSucess(const QJsonObject &jsonObj) {
|
|
|
- Logger::getInstance().info(QString("收到爆破完成回传记录. data: %1")
|
|
|
- .arg(QJsonDocument(jsonObj).toJson(QJsonDocument::Indented)));
|
|
|
+ Logger::getInstance("blastRecord.log")
|
|
|
+ .info(QJsonDocument(jsonObj).toJson(QJsonDocument::Indented));
|
|
|
blast_uuid = QUuid::createUuid().toString(QUuid::WithoutBraces).remove(QRegularExpression("-"));
|
|
|
lat = labLat;
|
|
|
lon = labLon;
|
|
@@ -379,15 +384,65 @@ void firingWidget::onBlastSucess(const QJsonObject &jsonObj) {
|
|
|
|
|
|
if (lat == "定位失败" && lat == "未知" && lon == "定位失败" && lon == "未知") {
|
|
|
QMessageBox::critical(nullptr, "错误", "未获取有效定位数据,数据将暂不上传丹灵");
|
|
|
- // 数据:存本地 不传丹灵
|
|
|
+ } else {
|
|
|
+ uploadToDanLing(jsonObj);
|
|
|
}
|
|
|
- uploadToDanLing(jsonObj);
|
|
|
}
|
|
|
|
|
|
-void firingWidget::uploadToServer(const QJsonObject &jsonObj) {
|
|
|
+void firingWidget::uploadBlastRecordToServer(HBlastRecord *blastRecord,
|
|
|
+ const QList<HBlastEquipmentRecord *> &equipmentRecords,
|
|
|
+ const QList<HBlastRecordDet *> &detsRecords) {
|
|
|
// upload blast project
|
|
|
- // upload blast reg dets
|
|
|
- // upload blast dets data
|
|
|
+ if (blastRecord == nullptr) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!backendAPIManager::uploadBlastProject(blastRecord->BlastRecordToJson())) {
|
|
|
+ Logger::getInstance().error(QString("Failed to upload blast project. data: %1")
|
|
|
+ .arg(QJsonDocument(blastRecord->BlastRecordToJson())
|
|
|
+ .toJson(QJsonDocument::Indented)));
|
|
|
+ }
|
|
|
+ if (!equipmentRecords.isEmpty()) {
|
|
|
+ for (const auto &equipmentRecord : equipmentRecords) {
|
|
|
+ if (!backendAPIManager::uploadBlastEquipments(equipmentRecord->ToJson())) {
|
|
|
+ Logger::getInstance().error(QString("Failed to upload blast equipment. data: %1")
|
|
|
+ .arg(QJsonDocument(equipmentRecord->ToJson())
|
|
|
+ .toJson(QJsonDocument::Indented)));
|
|
|
+ } else {
|
|
|
+ Logger::getInstance().info(
|
|
|
+ QString("Successfully uploaded blast equipment. data: %1")
|
|
|
+ .arg(QJsonDocument(equipmentRecord->ToJson())
|
|
|
+ .toJson(QJsonDocument::Indented)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Logger::getInstance().warn("No equipment records to upload.");
|
|
|
+ }
|
|
|
+
|
|
|
+ // dets数量可能较多,切片最多50条分批次发送
|
|
|
+ if (!detsRecords.isEmpty()) {
|
|
|
+ int batchSize = 50; // 每批次最多50条
|
|
|
+ for (int i = 0; i < detsRecords.size(); i += batchSize) {
|
|
|
+ QList<HBlastRecordDet *> batch = detsRecords.mid(i, batchSize);
|
|
|
+ QJsonArray detsArray;
|
|
|
+ for (const auto &det : batch) {
|
|
|
+ detsArray.append(det->ToJson());
|
|
|
+ }
|
|
|
+ QJsonObject detsJsonObject;
|
|
|
+ detsJsonObject["blast_record_dets"] = detsArray;
|
|
|
+
|
|
|
+ if (!backendAPIManager::uploadBlastProjectDets(detsJsonObject)) {
|
|
|
+ Logger::getInstance().error(
|
|
|
+ QString("Failed to upload blast dets. data: %1")
|
|
|
+ .arg(QString(QJsonDocument(detsArray).toJson(QJsonDocument::Indented))));
|
|
|
+ } else {
|
|
|
+ Logger::getInstance().info(
|
|
|
+ QString("Successfully uploaded blast dets. data: %1")
|
|
|
+ .arg(QString(QJsonDocument(detsArray).toJson(QJsonDocument::Indented))));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Logger::getInstance().warn("No det records to upload.");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void firingWidget::saveAndUploadRecord(const QJsonObject &jsonObj) {
|
|
@@ -401,40 +456,27 @@ void firingWidget::saveAndUploadRecord(const QJsonObject &jsonObj) {
|
|
|
HBlastRecord *blastRecord = recordBlastProject(jsonObj);
|
|
|
if (blastRecord == nullptr) {
|
|
|
Logger::getInstance("error_blast_records.log")
|
|
|
- .error(QString("Failed to create blast record. data: %1")
|
|
|
- .arg(QJsonDocument(jsonObj).toJson(QJsonDocument::Indented)));
|
|
|
- Logger::getInstance().info(
|
|
|
- QString("Failed to create blast record. try to rollback the transaction: %1")
|
|
|
- .arg(db.rollback()));
|
|
|
- return;
|
|
|
- }
|
|
|
- if (!recordBlastEquipments(jsonObj)) {
|
|
|
- if (db.rollback()) {
|
|
|
- qDebug() << "Transaction rolled back successfully for equiments.";
|
|
|
- } else {
|
|
|
- qDebug() << "Failed to roll back transaction:" << db.lastError().text();
|
|
|
- }
|
|
|
- return;
|
|
|
- }
|
|
|
- QList<HBlastRecordDet *> detsRecords = recordDetsData(jsonObj);
|
|
|
- if (detsRecords.isEmpty()) {
|
|
|
- if (db.rollback()) {
|
|
|
- qDebug() << "Transaction rolled back successfully.";
|
|
|
- } else {
|
|
|
- qDebug() << "Failed to roll back transaction:" << db.lastError().text();
|
|
|
- }
|
|
|
+ .error(QString("Failed to create blast record. projectName: %1, ProjectID: %2; "
|
|
|
+ "rollback transaction: %3")
|
|
|
+ .arg(jsonObj["project_name"].toString(), jsonObj["project_htid"].toString(),
|
|
|
+ db.rollback() ? "success" : "failed"));
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- db.commit();
|
|
|
+ QList<HBlastRecordDet *> detsRecords = recordBlastProjectDets(jsonObj);
|
|
|
+ QList<HBlastEquipmentRecord *> equipmentRecords = recordBlastEquipments(jsonObj);
|
|
|
|
|
|
- if (blastRecord != nullptr) {
|
|
|
- if (!backendAPIManager::uploadBlastProject(blastRecord->BlastRecordToJson())) {
|
|
|
- Logger::getInstance().error(QString("Failed to upload blast project. data: %1")
|
|
|
- .arg(QJsonDocument(blastRecord->BlastRecordToJson())
|
|
|
- .toJson(QJsonDocument::Indented)));
|
|
|
- }
|
|
|
+ if (db.commit()) {
|
|
|
+ Logger::getInstance().info(
|
|
|
+ QString("Transaction committed successfully. blast_uuid: %1").arg(blast_uuid));
|
|
|
+ } else {
|
|
|
+ Logger::getInstance().error(
|
|
|
+ QString("Failed to commit transaction: %1. try to rollback: %2")
|
|
|
+ .arg(db.lastError().text(), db.rollback() ? "success" : "failed"));
|
|
|
+ return;
|
|
|
}
|
|
|
+
|
|
|
+ uploadBlastRecordToServer(blastRecord, equipmentRecords, detsRecords);
|
|
|
}
|
|
|
|
|
|
HBlastRecord *firingWidget::recordBlastProject(const QJsonObject &jsonObj) {
|
|
@@ -449,7 +491,7 @@ HBlastRecord *firingWidget::recordBlastProject(const QJsonObject &jsonObj) {
|
|
|
record->setCompanyCode(jsonObj["company_code"].toString());
|
|
|
record->setAppVersion(jsonObj["app_version"].toString());
|
|
|
|
|
|
- record->setLongitude(jsonObj["blast_longitude"].toString().left(10));
|
|
|
+ record->setLongitude(jsonObj["blast_longitude"].toString().left(10)); // TODO: 确认地址长度
|
|
|
record->setLatitude(jsonObj["blast_latitude"].toString().left(10));
|
|
|
|
|
|
// record->setLongitude(lat);
|
|
@@ -473,7 +515,7 @@ HBlastRecord *firingWidget::recordBlastProject(const QJsonObject &jsonObj) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-QList<HBlastRecordDet *> firingWidget::recordDetsData(const QJsonObject &jsonObj) {
|
|
|
+QList<HBlastRecordDet *> firingWidget::recordBlastProjectDets(const QJsonObject &jsonObj) {
|
|
|
QList<HBlastRecordDet *> recordDets;
|
|
|
QJsonArray regsArray = jsonObj["regs"].toArray();
|
|
|
|
|
@@ -505,8 +547,7 @@ QList<HBlastRecordDet *> firingWidget::recordDetsData(const QJsonObject &jsonObj
|
|
|
} else {
|
|
|
Logger::getInstance().error(
|
|
|
QString("Failed to insert record det. data %1")
|
|
|
- .arg(QJsonDocument(recordDet->BlastRecordDetToJson())
|
|
|
- .toJson(QJsonDocument::Compact)));
|
|
|
+ .arg(QJsonDocument(recordDet->ToJson()).toJson(QJsonDocument::Compact)));
|
|
|
delete recordDet;
|
|
|
continue;
|
|
|
}
|