mainwindow.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #include "mainwindow.h"
  2. #include <QtCore/qlist.h>
  3. #include <QtCore/qobject.h>
  4. #include <QDebug>
  5. #include <QPushButton>
  6. #include <QWidget>
  7. #include "../components/loadingWidget.h"
  8. #include "../mqtt/mqttclient.h"
  9. #include "../serial/serialgpsthread.h"
  10. #include "../ui_mainwindow.h"
  11. #include "../utils/backendapimanager.h"
  12. #include "../utils/global.h"
  13. #include "../utils/jobs.h"
  14. #include "../utils/logger.h"
  15. // 定义 ANzI 转义序列来设置颜色
  16. #define ANSI_COLOR_GREEN "\x1B[32m"
  17. #define ANSI_COLOR_RESET "\x1B[0m"
  18. #include <QMessageBox>
  19. #include <exception>
  20. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
  21. try {
  22. this->setWindowFlags(Qt::FramelessWindowHint | Qt::Window);
  23. this->setWindowState(Qt::WindowMaximized); // Maximizes the window
  24. this->setStyleSheet("QWidget { font-size: 16px; }");
  25. this->setStyleSheet("QPushButton { font-size: 16px; }");
  26. ui->setupUi(this);
  27. LoadingWidget::init(ui->stackedWidget);
  28. initializeAnimate();
  29. initialMqttService();
  30. pageFactories[ui->btnHome] = new HomepageFactory();
  31. pageFactories[ui->btnNew] = new AddressFactory();
  32. pageFactories[ui->btnBlastProject] = new BlastProjectFactory();
  33. pageFactories[ui->btnEquipment] = new EquipmentFactory();
  34. pageFactories[ui->btnDet] = new DetInfoFactory();
  35. pageFactories[ui->btnBlastOper] = new BlastOperationFactory();
  36. pageFactories[ui->btnRecord] = new BlastRecordFactory();
  37. connect(ui->btnToggle, &QPushButton::clicked, this, &MainWindow::onToggleButtonClicked);
  38. for (auto *widget : left_button_station) {
  39. QPushButton *button = qobject_cast<QPushButton *>(widget);
  40. if (button) {
  41. connect(button, &QPushButton::clicked, this, [this, button] { onButtonClicked(button); });
  42. }
  43. }
  44. initDateTime();
  45. initialBtnSerial();
  46. // initialGPSSerial();
  47. ui->labLat->setText("经度: " + lat);
  48. ui->labLon->setText("维度: " + lon);
  49. connect(ui->btnClose, &QPushButton::clicked, this, &MainWindow::close);
  50. connect(this, &MainWindow::menubarTitleChanged, this, &MainWindow::updateProjectTitleLabel);
  51. } catch (const std::exception &ex) {
  52. Logger::getInstance().error(QString("Application crashed: %1").arg(ex.what()));
  53. QMessageBox::critical(this, "Error", QString("Application crashed: %1").arg(ex.what()));
  54. throw; // rethrow to allow further handling if needed
  55. } catch (...) {
  56. Logger::getInstance().error("Application crashed: Unknown exception");
  57. QMessageBox::critical(this, "Error", "Application crashed: Unknown exception");
  58. throw;
  59. }
  60. this->switchPage(ui->btnHome); // 默认显示首页
  61. }
  62. void MainWindow::updateProjectTitleLabel(const QString &newTitle) { ui->projectTitleLable->setText(newTitle); }
  63. void MainWindow::handleRefreshPage() {
  64. // 刷新页面
  65. ui->stackedWidget->currentWidget()->update();
  66. }
  67. void MainWindow::setMenubarTitle(const QString &newTitle) {
  68. if (m_currentProjectTitle != newTitle) {
  69. m_currentProjectTitle = newTitle;
  70. // Emit the signal to notify listeners (like our QLabel slot)
  71. emit menubarTitleChanged(m_currentProjectTitle);
  72. }
  73. }
  74. void MainWindow::initializeAnimate() {
  75. move(200, 200);
  76. animate_leftFrame = new QPropertyAnimation(ui->leftFrame, "minimumWidth");
  77. animate_leftFrame->setDuration(300);
  78. for (QObject *child : ui->left_buttonsBox->children()) {
  79. if (qobject_cast<QWidget *>(child)) {
  80. left_button_station.append(qobject_cast<QWidget *>(child));
  81. }
  82. }
  83. }
  84. void MainWindow::onToggleButtonClicked() {
  85. // 执行动画
  86. JOBS ::btn_animation(ui->leftFrame, animate_leftFrame);
  87. for (QWidget *b : left_button_station) {
  88. b->setProperty("spread", !b->property("spread").toBool());
  89. b->setStyleSheet(b->styleSheet());
  90. }
  91. }
  92. // 选中按钮
  93. void MainWindow::onButtonClicked(QPushButton *button) {
  94. setStyleSheets(static_cast<QPushButton *>(button));
  95. switchPage(static_cast<QPushButton *>(button));
  96. }
  97. void MainWindow::switchPage(QWidget *button) {
  98. LoadingWidget::showLoading(this, "请稍等");
  99. if (pageFactories.contains(button)) {
  100. PageFactory *factory = pageFactories[button];
  101. if (createdPageByButton.contains(button)) {
  102. QWidget *existingPage = createdPageByButton[button];
  103. existingPage->hide();
  104. ui->stackedWidget->removeWidget(existingPage);
  105. createdPageByButton.remove(button);
  106. }
  107. QWidget *newPage = factory->createPage(this);
  108. ui->stackedWidget->addWidget(newPage);
  109. ui->stackedWidget->setCurrentWidget(newPage);
  110. setMenubarTitle(qobject_cast<QPushButton *>(button)->text());
  111. createdPageByButton.insert(button, newPage);
  112. int pageCount = ui->stackedWidget->count();
  113. }
  114. LoadingWidget::hideLoading();
  115. }
  116. void MainWindow::initialMqttService() {
  117. Logger::getInstance().info("Start init Mqtt server.");
  118. if (mainMqttClient != nullptr) {
  119. QMqttSubscription *projectMsgSubsciber = mainMqttClient->subscribeToTopic(MQTT_TOPIC_COMPANY_PROJECTS_SUB);
  120. if (projectMsgSubsciber != nullptr) {
  121. connect(projectMsgSubsciber, &QMqttSubscription::messageReceived, this,
  122. &MainWindow::handleMqttProjectsMessage);
  123. } else {
  124. Logger::getInstance().error("Failed to subscribe to MQTT topic: " + MQTT_TOPIC_COMPANY_PROJECTS_SUB);
  125. }
  126. } else {
  127. Logger::getInstance().error("mainMqttClient is null, cannot initialize MQTT service");
  128. }
  129. }
  130. void MainWindow::publishBlastProjects() {
  131. // TODO: publish by emit message
  132. // QJsonArray jsonArray;
  133. // HProjectDao dao = HProjectDao(DatabaseManager::getInstance().getDatabase());
  134. // QList<QSharedPointer<HProject>> projectsReg = dao.getAllHProjectsReg();
  135. // for (const auto &projectPtr : projectsReg) {
  136. // if (projectPtr) {
  137. // QByteArray projectJson = projectPtr->ProjectToJson(*projectPtr);
  138. // QJsonDocument doc = QJsonDocument::fromJson(projectJson);
  139. // jsonArray.append(doc.object());
  140. // }
  141. // }
  142. // QJsonDocument jsonDoc(jsonArray);
  143. // QByteArray jsonData = jsonDoc.toJson(QJsonDocument::Indented);
  144. // mainMqttClient->sendMessage(MQTT_TOPIC_COMPANY_PROJECTS_PUBLISH, jsonData, 2, true);
  145. }
  146. void MainWindow::handleMqttProjectsMessage(const QMqttMessage &message) {
  147. Logger::getInstance().info("Received Mqtt message on topic: " + message.topic().name() +
  148. ", message: " + QString::fromUtf8(message.payload()));
  149. QJsonDocument jsonDoc = QJsonDocument::fromJson(message.payload());
  150. if (!jsonDoc.isNull() && jsonDoc.isObject()) {
  151. // 注册工程信息
  152. QJsonObject jsonObj = jsonDoc.object();
  153. if (jsonObj.contains("uuid") && jsonObj.contains("status")) {
  154. QJsonValue uuidValue = jsonObj["uuid"];
  155. QJsonValue statusValue = jsonObj["status"];
  156. if (statusValue.isString() && statusValue.toString() == BlastStatus::Created) { // "1" 未注册
  157. if (uuidValue.isNull()) {
  158. qDebug() << "uuid 的值为 null";
  159. } else {
  160. QString uuid = uuidValue.toString();
  161. HProjectDao dao = HProjectDao(DatabaseManager::getInstance().getDatabase());
  162. dao.updateBlastStatusByUuid(uuid, BlastStatus::Registered);
  163. }
  164. }
  165. } else if (jsonObj.contains("msgType") && jsonObj["msgType"].toString() == "blastSuccess") {
  166. // 爆破成功, 更新状态; 尤其是离线爆破的; 服务器收到爆破记录后发布
  167. HProjectDao dao = HProjectDao(DatabaseManager::getInstance().getDatabase());
  168. for (const QJsonValue &projectUuid : jsonObj["projectUuids"].toArray()) {
  169. qDebug() << "Received offline project blasted UUID: " << projectUuid;
  170. dao.updateBlastStatusByUuid(projectUuid.toString(), BlastStatus::Blasted);
  171. }
  172. publishBlastProjects();
  173. } else if (jsonObj.contains("msgType") && jsonObj["msgType"].toString() == "safeCheck") {
  174. // 处理安全验证消息
  175. if (jsonObj["status"] != "ok") {
  176. QMessageBox::warning(this, "安全验证失败", QString("安全验证未通过,请联系安全员确认"));
  177. return;
  178. }
  179. const QString addressUuid = jsonObj["addressUuid"].toString();
  180. QJsonArray addressList = backendAPIManager::getHAddresses();
  181. QList<QString> subAddressUuids = findAllChildUuids(addressList, addressUuid);
  182. subAddressUuids.append(addressUuid); // 添加当前地址的 UUID
  183. if (subAddressUuids.isEmpty()) {
  184. QMessageBox::information(nullptr, "安全验证失败", QString("未找到子地址,请联系安全员确认"));
  185. return;
  186. }
  187. HProjectDao dao = HProjectDao(DatabaseManager::getInstance().getDatabase());
  188. QList<QSharedPointer<HProject>> pendingCheckProjects = dao.getRegistedProjectByAddressUuid(subAddressUuids);
  189. for (const auto &project : pendingCheckProjects) {
  190. dao.updateBlastStatusByUuid(project->getUuid(), BlastStatus::SafeChecked);
  191. }
  192. if (!pendingCheckProjects.isEmpty()) {
  193. QMessageBox::information(nullptr, "安全员确认信息",
  194. QString("收到工程被安全员确认,请刷新查看爆破作业列表"));
  195. return;
  196. }
  197. }
  198. }
  199. }
  200. void MainWindow::setStyleSheets(QPushButton *selectedButton) {
  201. for (auto *b : left_button_station) {
  202. b->setProperty("selected", b == selectedButton);
  203. b->setStyleSheet(b->styleSheet()); // 刷新显示
  204. }
  205. }
  206. // 处理 MQTT 连接成功的槽函数
  207. void MainWindow::onMqttConnected() {
  208. m_isMqttConnected = true;
  209. Logger::getInstance().info("Mqtt connected.");
  210. }
  211. void MainWindow::initialBtnSerial() {
  212. bool success;
  213. serialTool = SerialTool::getInstance(this, &success);
  214. connect(serialTool, &SerialTool::serialPortOpened, this, &MainWindow::onSerialToolCreated);
  215. serialTool->setupSerialPort();
  216. Logger::getInstance().info("Fire buttons initialized");
  217. }
  218. void MainWindow::onSerialToolCreated() {
  219. m_btnSerialInitialized = true;
  220. serialTool->releaseInstance();
  221. qDebug() << ANSI_COLOR_GREEN << "Serial tool initialized" << ANSI_COLOR_RESET;
  222. Logger::getInstance().info("SerialTool initialized");
  223. }
  224. void MainWindow::initialGPSSerial() {
  225. Logger::getInstance().info("开始初始化GPS");
  226. SerialGPSThread *threadGPS = new SerialGPSThread(this);
  227. connect(threadGPS, &SerialGPSThread::storedGNRMCDataUpdated, this, &MainWindow::handleStoredGNRMCData);
  228. threadGPS->start();
  229. }
  230. // 槽函数,用于接收 RMCData 数据
  231. void MainWindow::handleStoredGNRMCData(const RMCData &data) {
  232. if (data.isValid) {
  233. lat = QString::number(data.latitude);
  234. lon = QString::number(data.longitude);
  235. } else {
  236. lat = "定位失败";
  237. lon = "定位失败";
  238. }
  239. ui->labLat->setText("经度: " + lat);
  240. ui->labLon->setText("纬度: " + lon);
  241. labLat = lat;
  242. labLon = lon;
  243. }
  244. void MainWindow::initDateTime() {
  245. timeThread = new TimeUpdateThread(this);
  246. connect(timeThread, &TimeUpdateThread::timeUpdated, this, &MainWindow::onTimeUpdated);
  247. timeThread->start();
  248. }
  249. void MainWindow::onTimeUpdated(const QString &timeString) { ui->dateTimeShow->setText(timeString); }
  250. MainWindow::~MainWindow() {
  251. timeThread->stop();
  252. delete ui;
  253. }
  254. void MainWindow::mousePressEvent(QMouseEvent *event) {
  255. // if (event->button() == Qt::LeftButton) {
  256. // m_dragPosition = event->globalPos() - frameGeometry().topLeft();
  257. // event->accept();
  258. // }
  259. }
  260. void MainWindow::mouseMoveEvent(QMouseEvent *event) {
  261. // if (event->buttons() & Qt::LeftButton) {
  262. // move(event->globalPos() - m_dragPosition);
  263. // event->accept();
  264. // }
  265. }
  266. QList<QString> MainWindow::findAllChildUuids(const QJsonArray &addressArray, const QString targetUuid) {
  267. QList<QString> childrenUuids;
  268. for (const QJsonValue &value : addressArray) {
  269. if (!value.isObject()) continue;
  270. QJsonObject addressObj = value.toObject();
  271. QString currentUuid = addressObj["uuid"].toString();
  272. if (currentUuid == targetUuid) {
  273. // Found the target address, now collect all children UUIDs recursively
  274. if (addressObj.contains("children") && addressObj["children"].isArray()) {
  275. QJsonArray children = addressObj["children"].toArray();
  276. for (const QJsonValue &child : children) {
  277. if (child.isObject()) {
  278. QString childUuid = child.toObject()["uuid"].toString();
  279. childrenUuids.append(childUuid);
  280. // max depth 3
  281. if (child.toObject().contains("children") && child.toObject()["children"].isArray()) {
  282. QJsonArray grandchildren = child.toObject()["children"].toArray();
  283. for (const QJsonValue &grandchild : grandchildren) {
  284. if (grandchild.isObject()) {
  285. QString grandchildUuid = grandchild.toObject()["uuid"].toString();
  286. childrenUuids.append(grandchildUuid);
  287. }
  288. }
  289. }
  290. }
  291. }
  292. return childrenUuids;
  293. }
  294. } else if (addressObj.contains("children") && addressObj["children"].isArray()) {
  295. // Continue searching in children
  296. QJsonArray children = addressObj["children"].toArray();
  297. QList<QString> result = findAllChildUuids(children, targetUuid);
  298. if (!result.isEmpty()) {
  299. return result;
  300. }
  301. }
  302. }
  303. return childrenUuids;
  304. }