firingwidget.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. #include "firingwidget.h"
  2. #include "ui_firingwidget.h"
  3. #include "../countdownwidget.h"
  4. #include <QNetworkAccessManager>
  5. #include <QNetworkRequest>
  6. #include <QNetworkReply>
  7. #include <QUrl>
  8. #include <QUrlQuery>
  9. #include <QJsonDocument>
  10. #include <QJsonObject>
  11. #include <QJsonArray>
  12. #include <QDebug>
  13. #include <QSqlQuery>
  14. #include <QMessageBox>
  15. #include <QTimeZone>
  16. #include "../global.h"
  17. #define ANSI_COLOR_GREEN "\x1B[32m"
  18. #define ANSI_COLOR_RESET "\x1B[0m"
  19. firingWidget::firingWidget(const int &row, const bool &select, const QString &uuid, QWidget *parent) : QWidget(parent),
  20. ui(new Ui::firingWidget),
  21. m_uuid(uuid),
  22. m_row(row),
  23. m_select(select),
  24. dao(DatabaseManager::getInstance().getDatabase()),
  25. daoDet(DatabaseManager::getInstance().getDatabase()),
  26. daoReg(DatabaseManager::getInstance().getDatabase())
  27. {
  28. ui->setupUi(this);
  29. ui->pushButton_2->setText("立即测试");
  30. ui->textEdit->setStyleSheet("border: none;background: transparent;");
  31. ui->frame_2->setStyleSheet("border: none;background: transparent;");
  32. QFrame *frame = ui->frame_2; // 使用你为 QFrame 设置的对象名称
  33. navProgress = new NavProgress(frame);
  34. ui->verticalLayout->setContentsMargins(0, 30, 0, 0);
  35. ui->scrollArea->setStyleSheet("background: rgba(0, 0, 0, 22); border-radius: 20px;");
  36. ui->verticalLayout->addWidget(navProgress);
  37. mqttThread = new MqttThread(this);
  38. QStringList topics = {"hxgc/" + uuid + "/B"};
  39. mqttThread->setConnectionInfo("114.55.233.194", 1883, "hxgc", "hxgc123456", "P" + uuid, topics);
  40. connect(mqttThread, &MqttThread::mqttConnected, this, &firingWidget::onMqttConnected);
  41. mqttThread->start();
  42. connect(mqttThread, &MqttThread::messageAndTopicReceived, this, &firingWidget::handleMessageAndTopic);
  43. connect(this, &firingWidget::lastStageChanged, this, &firingWidget::onLastStageChanged);
  44. connect(this, &firingWidget::updateData, this, &firingWidget::onBlastSucess);
  45. startBlasting();
  46. }
  47. firingWidget::~firingWidget()
  48. {
  49. delete ui;
  50. }
  51. void firingWidget::sendMqttMessage(const QString &topic, const QByteArray &message)
  52. {
  53. if (mqttThread && m_isMqttConnected)
  54. {
  55. emit mqttThread->sendMessageRequested(topic, message);
  56. }
  57. else
  58. {
  59. qDebug() << "MQTT 未连接,无法发送消息";
  60. }
  61. }
  62. void firingWidget::startBlasting()
  63. {
  64. // TODO 检测起爆器是否在线
  65. QTimer::singleShot(1000, this, [this]()
  66. {
  67. lastStage=0;
  68. emit lastStageChanged(0); });
  69. }
  70. void firingWidget::on_pushButton_2_clicked()
  71. {
  72. emit countdown(m_uuid, topic, "message");
  73. }
  74. bool firingWidget::uploadToDanLing(const QJsonObject &jsonObj)
  75. {
  76. QString htid = jsonObj["project_htid"].toString();
  77. QString htm = ""; //
  78. QString xmbh = jsonObj["project_xmbh"].toString();
  79. QString sbbh = jsonObj["equipment_sn"].toString();
  80. QString bprysfz = jsonObj["operator_identity"].toString();
  81. QString dwdm = jsonObj["company_code"].toString();
  82. QString xtm = ""; //
  83. QString jd = jsonObj["blast_longitude"].toString();
  84. QString bpsj = jsonObj["blast_time"].toString();
  85. QString wd = jsonObj["blast_latitude"].toString();
  86. QStringList uidList;
  87. QJsonArray regs = jsonObj["regs"].toArray();
  88. for (const auto &reg : regs)
  89. {
  90. QJsonObject regObj = reg.toObject();
  91. QJsonArray dets = regObj["dets"].toArray();
  92. for (const auto &det : dets)
  93. {
  94. QJsonObject detObj = det.toObject();
  95. uidList.append(detObj["uid"].toString());
  96. }
  97. }
  98. QString uid = uidList.join(",");
  99. QString plainText = QString("{\"htid\":\"%1\",\"htm\":\"%2\",\"xmbh\":\"%3\",\"sbbh\":\"%4\","
  100. "\"bprysfz\":\"%5\",\"dwdm\":\"%6\",\"xtm\":\"%7\",\"jd\":\"%8\","
  101. "\"bpsj\":\"%9\",\"wd\":\"%10\",\"uid\":\"%11\"}")
  102. .arg(htid)
  103. .arg(htm)
  104. .arg(xmbh)
  105. .arg(sbbh)
  106. .arg(bprysfz)
  107. .arg(dwdm)
  108. .arg(xtm)
  109. .arg(jd)
  110. .arg(bpsj)
  111. .arg(wd)
  112. .arg(uid);
  113. Des3Encryption des3;
  114. QByteArray key = "jadl12345678912345678912";
  115. QByteArray encryptedData = des3.des3Encrypt(plainText.toUtf8(), key);
  116. QByteArray base64Encoded = encryptedData.toBase64();
  117. QString encodedJson = QUrl::toPercentEncoding(base64Encoded);
  118. QString baseUrl = "http://test.mbdzlg.com/mbdzlgtxzx/servlet/DzlgSysbJsonServlert";
  119. QUrl url(baseUrl);
  120. QUrlQuery query;
  121. query.addQueryItem("param", encodedJson);
  122. url.setQuery(query);
  123. QNetworkAccessManager manager;
  124. QNetworkRequest request(url);
  125. request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
  126. QByteArray postData;
  127. QNetworkReply *reply = manager.post(request, postData);
  128. QEventLoop loop;
  129. QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
  130. loop.exec();
  131. if (reply->error() == QNetworkReply::NoError)
  132. {
  133. QByteArray responseData = reply->readAll();
  134. qDebug() << "Response:" << QString(responseData);
  135. }
  136. else
  137. {
  138. qDebug() << "Error:" << reply->errorString();
  139. }
  140. reply->deleteLater();
  141. return true;
  142. }
  143. bool firingWidget::recordBlastReg(const QJsonObject &jsonObj)
  144. {
  145. QJsonArray regsArray = jsonObj["regs"].toArray();
  146. for (const auto &regValue : regsArray)
  147. {
  148. QJsonObject regObj = regValue.toObject();
  149. HBlastRegRecord recordReg;
  150. reg_uuid = QUuid::createUuid().toString(QUuid::WithoutBraces).remove(QRegularExpression("-"));
  151. recordReg.setUuid(reg_uuid);
  152. recordReg.setEquipSn(regObj["equipment_sn"].toString());
  153. recordReg.setEquipVersion(regObj["equipment_version"].toString());
  154. recordReg.setRegDetoCount(regObj["reg_deto_count"].toString().toInt());
  155. recordReg.setErrorDetoCount(regObj["error_deto_count"].toString().toInt());
  156. recordReg.setAfterTestBusV(regObj["after_test_bus_v"].toString());
  157. recordReg.setAfterTestBusI(regObj["after_test_bus_i"].toString());
  158. recordReg.setBusLeakageCurrentI(regObj["bus_leakage_current_i"].toString());
  159. recordReg.setNetChargedV(regObj["net_charged_v"].toString());
  160. recordReg.setNetChargedI(regObj["net_charged_i"].toString());
  161. recordReg.setBeforeBlastingV(regObj["before_blasting_v"].toString());
  162. recordReg.setBeforeBlastingI(regObj["before_blasting_i"].toString());
  163. recordReg.setRecordUuid(blast_uuid);
  164. recordReg.setCreatedAt(QDateTime::currentDateTime());
  165. // 插入数据到数据库
  166. if (!daoReg.saveHBlastRegRecord(recordReg))
  167. {
  168. return false;
  169. }
  170. }
  171. return true;
  172. }
  173. void firingWidget::handleMessageAndTopic(const QByteArray &message, const QMqttTopicName &topic)
  174. {
  175. QString msgText = QString("Topic: %1, Message: %2").arg(topic.name(), QString(message));
  176. QTextEdit *textEdit = ui->scrollAreaWidgetContents->findChild<QTextEdit *>("textEdit");
  177. if (textEdit)
  178. {
  179. textEdit->append(msgText);
  180. }
  181. int stage = 0;
  182. QJsonDocument jsonDoc = QJsonDocument::fromJson(message);
  183. if (!jsonDoc.isNull() && jsonDoc.isObject())
  184. {
  185. QJsonObject jsonObj = jsonDoc.object();
  186. if (jsonObj.contains("type") && jsonObj["type"].toInt() == 1)
  187. {
  188. if (jsonObj.contains("data") && jsonObj["data"].isObject())
  189. {
  190. QJsonObject dataObject = jsonObj["data"].toObject();
  191. stage = dataObject["stage"].toInt();
  192. if (stage != lastStage)
  193. {
  194. lastStage = stage;
  195. switch (stage)
  196. {
  197. case 0:
  198. navProgress->setState(0);
  199. ui->pushButton_2->setText("立即测试");
  200. ui->pushButton_2->setEnabled(false);
  201. break;
  202. case 1:
  203. navProgress->setState(1);
  204. emit progressChanged(1, m_row);
  205. emit updateBlastStatus(1, m_row);
  206. ui->pushButton_2->setText("立即测试");
  207. ui->pushButton_2->setEnabled(false);
  208. break;
  209. case 2:
  210. ui->pushButton_2->setEnabled(true);
  211. emit progressChanged(2, m_row);
  212. emit updateBlastStatus(2, m_row);
  213. qDebug() << "stage:" << stage;
  214. ui->pushButton_2->setText("充电");
  215. navProgress->setState(2);
  216. break;
  217. case 3:
  218. ui->pushButton_2->setEnabled(false);
  219. emit progressChanged(3, m_row);
  220. emit updateBlastStatus(3, m_row);
  221. ui->pushButton_2->setText("充电");
  222. navProgress->setState(3);
  223. break;
  224. case 4:
  225. ui->pushButton_2->setEnabled(true);
  226. emit progressChanged(4, m_row);
  227. emit updateBlastStatus(4, m_row);
  228. ui->pushButton_2->setText("起爆");
  229. navProgress->setState(4);
  230. break;
  231. case 5:
  232. ui->pushButton_2->setEnabled(false);
  233. emit progressChanged(5, m_row);
  234. emit updateBlastStatus(5, m_row);
  235. emit updateButton(5, m_row);
  236. ui->pushButton_2->setText("起爆");
  237. navProgress->setState(5);
  238. break;
  239. case 6:
  240. ui->pushButton_2->setEnabled(false);
  241. emit progressChanged(6, m_row);
  242. emit updateBlastStatus(6, m_row);
  243. emit updateProjectStatus(m_uuid);
  244. ui->pushButton_2->setText("已完成起爆");
  245. navProgress->setState(6);
  246. break;
  247. case 7:
  248. qDebug() << "status:" << stage;
  249. ui->pushButton_2->setEnabled(false);
  250. emit progressChanged(0, m_row); // 发送进度值
  251. emit updateBlastStatus(0, m_row); // 正在测试
  252. ui->pushButton_2->setText("已完成起爆");
  253. navProgress->setState(6);
  254. break;
  255. default:
  256. break;
  257. }
  258. emit lastStageChanged(lastStage);
  259. }
  260. }
  261. }
  262. if (jsonObj.contains("type") && jsonObj["type"].toInt() == 2)
  263. {
  264. if (jsonObj.contains("data") && jsonObj["data"].isObject())
  265. {
  266. QJsonObject dataObject = jsonObj["data"].toObject();
  267. emit updateData(dataObject);
  268. emit closeFiring(m_uuid);
  269. }
  270. }
  271. }
  272. }
  273. void firingWidget::onCountdownFinished(const QString &topic, const QString &message)
  274. {
  275. sendMqttMessage(topic, message.toUtf8());
  276. }
  277. void firingWidget::onMqttConnected()
  278. {
  279. m_isMqttConnected = true;
  280. }
  281. void firingWidget::onButtonPressedReceived(const QString &topic, const QString &message)
  282. {
  283. QByteArray data = "\r\nDISABLE_BUTTON\r\n";
  284. // 调用发送数据的方法
  285. bool success = serialTool->sendData(data);
  286. if (success)
  287. {
  288. qDebug() << "Data sent successfully";
  289. }
  290. else
  291. {
  292. qDebug() << "Failed to send data";
  293. }
  294. emit countdown(m_uuid, topic, message);
  295. }
  296. // 状态改变
  297. void firingWidget::onLastStageChanged(int newStage)
  298. {
  299. QString topic = "hxgc/" + m_uuid + "/P";
  300. QString message;
  301. QString buttonText;
  302. switch (lastStage)
  303. {
  304. case 0:
  305. message = "起爆测试";
  306. buttonText = "立即测试";
  307. sendMqttMessage(topic, message.toUtf8());
  308. break;
  309. case 2:
  310. message = "开始充电";
  311. buttonText = "充电";
  312. sendMqttMessage(topic, message.toUtf8());
  313. break;
  314. case 4:
  315. message = "起爆";
  316. buttonText = "起爆";
  317. break;
  318. case 5:
  319. if (!m_select)
  320. {
  321. if (connection)
  322. {
  323. disconnect(connection);
  324. qDebug() << "Connection disconnected.";
  325. }
  326. if (connectionPress)
  327. {
  328. disconnect(connectionPress);
  329. qDebug() << "connectionPress disconnected.";
  330. }
  331. serialTool->releaseInstance();
  332. }
  333. qDebug() << ANSI_COLOR_GREEN << "释放按键 5" << ANSI_COLOR_RESET;
  334. break;
  335. default:
  336. return;
  337. }
  338. if (!message.isEmpty())
  339. {
  340. if (lastStage == 4 && !m_select)
  341. {
  342. bool success2;
  343. serialTool = SerialTool::getInstance(nullptr, &success2);
  344. if (serialTool)
  345. {
  346. QByteArray data = "\r\nENABLE_BUTTON\r\n";
  347. bool success = serialTool->sendData(data);
  348. if (success)
  349. {
  350. qDebug() << "Data sent successfully";
  351. }
  352. else
  353. {
  354. qDebug() << "Failed to send data";
  355. }
  356. connection = connect(serialTool, &SerialTool::enableButtonReceived, [this]()
  357. { emit updateBlastStatus(10, m_row); });
  358. connectionPress = connect(serialTool, &SerialTool::buttonPressedReceived, [this, topic, message]()
  359. { this->onButtonPressedReceived(topic, message); });
  360. }
  361. else
  362. {
  363. qDebug() << "serialTool Not fond.";
  364. // 在准备起爆状态下 但是没有获取串口
  365. lastStage = 10;
  366. }
  367. }
  368. else if (lastStage == 4 && m_select)
  369. {
  370. qDebug() << "多台起爆:待起爆状态";
  371. emit selectSignal(m_uuid);
  372. }
  373. else
  374. { //????
  375. qDebug() << "Stage 值变为: " << newStage << "发送消息" << message.toUtf8();
  376. }
  377. }
  378. }
  379. void firingWidget::cancelBlasting()
  380. {
  381. QString message = "取消流程";
  382. if (!message.isEmpty())
  383. {
  384. sendMqttMessage(topic, message.toUtf8());
  385. }
  386. }
  387. void firingWidget::on_sendTest_4_clicked()
  388. {
  389. // qDebug() << "navProgress 取消流程";
  390. // QString message = "取消流程";
  391. // if (!message.isEmpty()) {
  392. // sendMqttMessage(topic, message.toUtf8());
  393. // }
  394. lat = labLat;
  395. lon = labLon;
  396. QSqlDatabase db = DatabaseManager::getInstance().getDatabase();
  397. if (!db.transaction())
  398. {
  399. qDebug() << "Failed to start transaction:" << db.lastError().text();
  400. // return false;
  401. }
  402. // 假设的 JSON 数据
  403. QString jsonData = R"(
  404. {
  405. "blast_latitude": "30.2039",
  406. "project_xmbh": "",
  407. "app_version": "1.52",
  408. "operator_identity": "330781198509079292",
  409. "blast_time": "2017-07-12 15:00:15",
  410. "regs": [
  411. {
  412. "net_charged_v": "13499",
  413. "after_test_bus_i": "0",
  414. "after_test_bus_v": "7006",
  415. "equipment_version": "AK01_V3.2_240726",
  416. "bus_leakage_current_i": "0",
  417. "reg_deto_count": "2",
  418. "before_blasting_i": "1",
  419. "error_deto_count": "2",
  420. "net_charged_i": "0",
  421. "before_blasting_v": "13484",
  422. "dets": [
  423. {
  424. "uid": "24211104F18004",
  425. "freq": "80000",
  426. "out_code": "2411104F18000",
  427. "tag": "1-1-1",
  428. "in_code": "045AC8360A4C0061",
  429. "status": "0x03",
  430. "delay_time": "0.0"
  431. },
  432. {
  433. "uid": "24211104F18005",
  434. "freq": "80000",
  435. "out_code": "2411104F18001",
  436. "tag": "1-2-1",
  437. "in_code": "055AC8360A4C0088",
  438. "status": "0x03",
  439. "delay_time": "20.0"
  440. }
  441. ],
  442. "equipment_sn": "F34A0000001"
  443. }
  444. ],
  445. "reg_deto_count": "2",
  446. "project_name": "sidf",
  447. "operator_name": "樊工",
  448. "phone": "18611112222",
  449. "project_htid": "",
  450. "company_code": "3701234300003",
  451. "blast_longitude": "120.196",
  452. "error_deto_count": "2",
  453. "equipment_sn": "F34A0000001"
  454. }
  455. )";
  456. QJsonDocument doc = QJsonDocument::fromJson(jsonData.toUtf8());
  457. QJsonObject jsonObj = doc.object();
  458. // 创建 HBlastRecord 对象
  459. HBlastRecord record;
  460. record.setProjectName(jsonObj["project_name"].toString());
  461. record.setProjectHtid(jsonObj["project_htid"].toString());
  462. record.setProjectXmbh(jsonObj["project_xmbh"].toString());
  463. record.setOperatorName(jsonObj["operator_name"].toString());
  464. record.setPhone(jsonObj["phone"].toString());
  465. record.setOperatorIdentity(jsonObj["operator_identity"].toString());
  466. record.setEquipmentSn(jsonObj["equipment_sn"].toString());
  467. record.setCompanyCode(jsonObj["company_code"].toString());
  468. record.setAppVersion(jsonObj["app_version"].toString());
  469. // record.setLongitude(jsonObj["blast_longitude"].toString());
  470. // record.setLatitude(jsonObj["blast_latitude"].toString());
  471. record.setLongitude(lat);
  472. record.setLatitude(lon);
  473. record.setRegDetCount(jsonObj["reg_deto_count"].toInt());
  474. record.setErrorDetCount(jsonObj["error_deto_count"].toInt());
  475. record.setBlastAt(QDateTime::fromString(jsonObj["blast_time"].toString(), "yyyy-MM-dd hh:mm:ss"));
  476. record.setCreatedAt(QDateTime::currentDateTime());
  477. qDebug() << "currentDateTime." << QDateTime::currentDateTime();
  478. record.setUuid(QUuid::createUuid().toString(QUuid::WithoutBraces).remove(QRegularExpression("-")));
  479. // if (dao.addHBlastRecord(record)) {
  480. // qDebug() << "Record inserted successfully.";
  481. // }else{
  482. // qDebug() << "Failed to insert record.";
  483. // }
  484. // if (!addDetData(jsonObj)) {
  485. // qDebug() << "Insert failed for UID:"; // 假设 record 有 uid 成员
  486. // if (db.rollback()) {
  487. // qDebug() << "Transaction rolled back successfully.";
  488. // } else {
  489. // qDebug() << "Failed to roll back transaction:" << db.lastError().text();
  490. // }
  491. // }
  492. // if (!addRegData(jsonObj)) {
  493. // if (db.rollback()) {
  494. // qDebug() << "Transaction rolled back successfully.";
  495. // } else {
  496. // qDebug() << "Failed to roll back transaction:" << db.lastError().text();
  497. // }
  498. // }
  499. // db.commit();
  500. if (lat == "定位失败" && lat == "未知" && lon == "定位失败" && lon == "未知")
  501. {
  502. // 数据出本地 不传丹灵
  503. if (dao.addHBlastRecord(record))
  504. {
  505. qDebug() << "Record inserted successfully.";
  506. }
  507. else
  508. {
  509. qDebug() << "Failed to insert record.";
  510. }
  511. if (!recordDetsData(jsonObj))
  512. {
  513. qDebug() << "Insert failed for UID:"; // 假设 record 有 uid 成员
  514. if (db.rollback())
  515. {
  516. qDebug() << "Transaction rolled back successfully.";
  517. }
  518. else
  519. {
  520. qDebug() << "Failed to roll back transaction:" << db.lastError().text();
  521. }
  522. }
  523. if (!recordBlastReg(jsonObj))
  524. {
  525. if (db.rollback())
  526. {
  527. qDebug() << "Transaction rolled back successfully.";
  528. }
  529. else
  530. {
  531. qDebug() << "Failed to roll back transaction:" << db.lastError().text();
  532. }
  533. }
  534. db.commit();
  535. }
  536. // danUpload(jsonObj);
  537. // if (db.commit()) {
  538. // qDebug() << "Transaction committed successfully.";
  539. // } else {
  540. // qDebug() << "Failed to commit transaction:" << db.lastError().text();
  541. // }
  542. }
  543. // 处理爆破成功,安全验证装置通过MQTT传回的数据
  544. void firingWidget::onBlastSucess(const QJsonObject &jsonObj)
  545. {
  546. Logger::getInstance().info(QString("收到爆破完成回传记录. data: %1").arg(QJsonDocument(jsonObj).toJson(QJsonDocument::Indented)));
  547. blast_uuid = QUuid::createUuid().toString(QUuid::WithoutBraces).remove(QRegularExpression("-"));
  548. lat = labLat;
  549. lon = labLon;
  550. saveDataToLocalDB(jsonObj);
  551. uploadToServer(jsonObj);
  552. if (lat == "定位失败" && lat == "未知" && lon == "定位失败" && lon == "未知"){
  553. QMessageBox::critical(nullptr, "错误", "未获取有效定位数据,数据将暂不上传丹灵");
  554. // 数据:存本地 不传丹灵
  555. }
  556. // TODO location数据如果错误,不提交丹灵
  557. // danUpload(jsonObj);
  558. // if (db.commit()) {
  559. // qDebug() << "Transaction committed successfully.";
  560. // } else {
  561. // qDebug() << "Failed to commit transaction:" << db.lastError().text();
  562. // }
  563. }
  564. /*
  565. Id int `json:"-" comment:"主键编码"` // 主键编码
  566. Uuid string `json:"uuid" comment:""`
  567. ProjectName string `json:"project_name" comment:"工程名称"`
  568. ProjectHtid string `json:"project_htid" comment:"合同编号"`
  569. ProjectXmbh string `json:"project_xmbh" comment:"项目编号"`
  570. OperatorName string `json:"operator_name" comment:"操作员"`
  571. Phone string `json:"phone" comment:"操作员电话"`
  572. OperatorIdentity string `json:"operator_identity" comment:"操作员身份证"`
  573. EquipmentSn string `json:"equipment_sn" comment:"设备序列号"`
  574. CompanyCode string `json:"company_code" comment:"公司代码"`
  575. AppVersion string `json:"app_version" comment:"app版本"`
  576. Longitude string `json:"blast_longitude" comment:"经度"`
  577. Latitude string `json:"blast_latitude" comment:"纬度"`
  578. EquipmentCount string `json:"equipment_count" comment:"设备数量"`
  579. RegDetCount string `json:"reg_deto_count" comment:"注册雷管数量"`
  580. ErrorDetCount string `json:"error_deto_count" comment:"异常雷管数量"`
  581. BlastAt string `json:"blast_time" comment:"爆破时间"`
  582. Regs []HBlastRecordEquipmentInsertReq `json:"regs"`
  583. WBlasterId string `json:"w_blaster_id"`
  584. */
  585. void firingWidget::uploadToServer(const QJsonObject &jsonObj)
  586. {
  587. // upload blast project
  588. // upload blast reg dets
  589. // upload blast dets data
  590. }
  591. void firingWidget::saveDataToLocalDB(const QJsonObject &jsonObj)
  592. {
  593. QSqlDatabase db = DatabaseManager::getInstance().getDatabase();
  594. if (!db.transaction())
  595. {
  596. Logger::getInstance().critical(QString("Failed to start DB trasaction. Error: %1").arg(db.lastError().text()));
  597. return;
  598. }
  599. if (!recordBlastData(jsonObj))
  600. {
  601. if (db.rollback())
  602. {
  603. qDebug() << "Transaction rolled back successfully.";
  604. }
  605. else
  606. {
  607. qDebug() << "Failed to roll back transaction:" << db.lastError().text();
  608. }
  609. }
  610. if (!recordBlastReg(jsonObj))
  611. {
  612. if (db.rollback())
  613. {
  614. qDebug() << "Transaction rolled back successfully.";
  615. }
  616. else
  617. {
  618. qDebug() << "Failed to roll back transaction:" << db.lastError().text();
  619. }
  620. }
  621. if (!recordDetsData(jsonObj))
  622. {
  623. qDebug() << "Insert failed for UID:"; // 假设 record 有 uid 成员
  624. if (db.rollback())
  625. {
  626. qDebug() << "Transaction rolled back successfully.";
  627. }
  628. else
  629. {
  630. qDebug() << "Failed to roll back transaction:" << db.lastError().text();
  631. }
  632. }
  633. db.commit();
  634. }
  635. HBlastRecord* firingWidget::recordBlastData(const QJsonObject &jsonObj)
  636. {
  637. HBlastRecord record;
  638. record.setProjectName(jsonObj["project_name"].toString());
  639. record.setProjectHtid(jsonObj["project_htid"].toString());
  640. record.setProjectXmbh(jsonObj["project_xmbh"].toString());
  641. record.setOperatorName(jsonObj["operator_name"].toString());
  642. record.setPhone(jsonObj["phone"].toString());
  643. record.setOperatorIdentity(jsonObj["operator_identity"].toString());
  644. record.setEquipmentSn(jsonObj["equipment_sn"].toString());
  645. record.setCompanyCode(jsonObj["company_code"].toString());
  646. record.setAppVersion(jsonObj["app_version"].toString());
  647. record.setLongitude(jsonObj["blast_longitude"].toString());
  648. record.setLatitude(jsonObj["blast_latitude"].toString());
  649. // record.setLongitude(lat);
  650. // record.setLatitude(lon);
  651. record.setRegDetCount(jsonObj["reg_deto_count"].toString().toInt());
  652. record.setErrorDetCount(jsonObj["error_deto_count"].toString().toInt());
  653. QDateTime blastTime = QDateTime::fromString(jsonObj["blast_time"].toString(), "yyyy-MM-dd hh:mm:ss +08:00");
  654. blastTime.setTimeZone(QTimeZone(QByteArrayLiteral("Asia/Shanghai")));
  655. record.setBlastAt(blastTime);
  656. record.setCreatedAt(QDateTime::currentDateTime());
  657. qDebug() << "currentDateTime." << QDateTime::currentDateTime();
  658. record.setUuid(blast_uuid);
  659. if (dao.addHBlastRecord(record))
  660. {
  661. qDebug() << "Record inserted successfully.";
  662. return &record;
  663. }
  664. else
  665. {
  666. qDebug() << "Failed to insert record.";
  667. return nullptr;
  668. }
  669. }
  670. bool firingWidget::recordDetsData(const QJsonObject &jsonObj)
  671. {
  672. QJsonArray regsArray = jsonObj["regs"].toArray();
  673. for (const auto &regValue : regsArray)
  674. {
  675. QJsonObject regObj = regValue.toObject();
  676. QJsonArray detsArray = regObj["dets"].toArray();
  677. for (const auto &detValue : detsArray)
  678. {
  679. QJsonObject detObj = detValue.toObject();
  680. HBlastRecordDet recordDet;
  681. recordDet.setId(0);
  682. recordDet.setUuid(QUuid::createUuid().toString(QUuid::WithoutBraces).remove(QRegularExpression("-")));
  683. recordDet.setOutCode(detObj["out_code"].toString());
  684. recordDet.setInnerCode(detObj["in_code"].toString());
  685. recordDet.setUid(detObj["uid"].toString());
  686. recordDet.setStatus(detObj["status"].toString());
  687. recordDet.setFreq(detObj["freq"].toString());
  688. recordDet.setDelayTime(detObj["delay_time"].toString());
  689. recordDet.setTag(detObj["tag"].toString());
  690. recordDet.setCreatedAt(QDateTime::currentDateTime());
  691. recordDet.setBlastRecordId(blast_uuid);
  692. recordDet.setRegId(reg_uuid);
  693. recordDet.setCreatedAt(QDateTime::currentDateTime());
  694. if (!daoDet.addHBlastRecordDet(recordDet))
  695. {
  696. return false;
  697. }
  698. }
  699. }
  700. return true;
  701. }