diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index f42eba8..7bce7ac 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -196,7 +196,7 @@ // 绑定显示函数 connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); - // 查询计数器的通道 并初始化串口 + // 查询计数器的通道 this->getChannelList(deviceId); } diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index f42eba8..7bce7ac 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -196,7 +196,7 @@ // 绑定显示函数 connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); - // 查询计数器的通道 并初始化串口 + // 查询计数器的通道 this->getChannelList(deviceId); } diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 0a907f1..9ac4497 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -64,45 +64,6 @@ return resultObj; } -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index f42eba8..7bce7ac 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -196,7 +196,7 @@ // 绑定显示函数 connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); - // 查询计数器的通道 并初始化串口 + // 查询计数器的通道 this->getChannelList(deviceId); } diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 0a907f1..9ac4497 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -64,45 +64,6 @@ return resultObj; } -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { diff --git a/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index a37122b..c84ad21 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -19,7 +19,6 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index f42eba8..7bce7ac 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -196,7 +196,7 @@ // 绑定显示函数 connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); - // 查询计数器的通道 并初始化串口 + // 查询计数器的通道 this->getChannelList(deviceId); } diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 0a907f1..9ac4497 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -64,45 +64,6 @@ return resultObj; } -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { diff --git a/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index a37122b..c84ad21 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -19,7 +19,6 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 77975b3..de50156 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -21,8 +21,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置stackWidget的大小 @@ -84,11 +83,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void DeviceHubWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void DeviceHubWindow::on_devTypeSelect_currentIndexChanged(int index) { ui->stackedWidget->setCurrentIndex(index); diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index f42eba8..7bce7ac 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -196,7 +196,7 @@ // 绑定显示函数 connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); - // 查询计数器的通道 并初始化串口 + // 查询计数器的通道 this->getChannelList(deviceId); } diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 0a907f1..9ac4497 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -64,45 +64,6 @@ return resultObj; } -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { diff --git a/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index a37122b..c84ad21 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -19,7 +19,6 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 77975b3..de50156 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -21,8 +21,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置stackWidget的大小 @@ -84,11 +83,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void DeviceHubWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void DeviceHubWindow::on_devTypeSelect_currentIndexChanged(int index) { ui->stackedWidget->setCurrentIndex(index); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index 39443a8..3b4c10a 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -50,7 +50,6 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); void on_devTypeSelect_currentIndexChanged(int index); void on_devSelect_currentIndexChanged(int index); diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index f42eba8..7bce7ac 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -196,7 +196,7 @@ // 绑定显示函数 connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); - // 查询计数器的通道 并初始化串口 + // 查询计数器的通道 this->getChannelList(deviceId); } diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 0a907f1..9ac4497 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -64,45 +64,6 @@ return resultObj; } -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { diff --git a/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index a37122b..c84ad21 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -19,7 +19,6 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 77975b3..de50156 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -21,8 +21,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置stackWidget的大小 @@ -84,11 +83,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void DeviceHubWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void DeviceHubWindow::on_devTypeSelect_currentIndexChanged(int index) { ui->stackedWidget->setCurrentIndex(index); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index 39443a8..3b4c10a 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -50,7 +50,6 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); void on_devTypeSelect_currentIndexChanged(int index); void on_devSelect_currentIndexChanged(int index); diff --git a/DeviceHub/DeviceHubWindow.ui b/DeviceHub/DeviceHubWindow.ui index cc6ccd0..70c0618 100644 --- a/DeviceHub/DeviceHubWindow.ui +++ b/DeviceHub/DeviceHubWindow.ui @@ -77,22 +77,6 @@ - - - - 1030 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index f42eba8..7bce7ac 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -196,7 +196,7 @@ // 绑定显示函数 connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); - // 查询计数器的通道 并初始化串口 + // 查询计数器的通道 this->getChannelList(deviceId); } diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 0a907f1..9ac4497 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -64,45 +64,6 @@ return resultObj; } -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { diff --git a/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index a37122b..c84ad21 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -19,7 +19,6 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 77975b3..de50156 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -21,8 +21,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置stackWidget的大小 @@ -84,11 +83,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void DeviceHubWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void DeviceHubWindow::on_devTypeSelect_currentIndexChanged(int index) { ui->stackedWidget->setCurrentIndex(index); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index 39443a8..3b4c10a 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -50,7 +50,6 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); void on_devTypeSelect_currentIndexChanged(int index); void on_devSelect_currentIndexChanged(int index); diff --git a/DeviceHub/DeviceHubWindow.ui b/DeviceHub/DeviceHubWindow.ui index cc6ccd0..70c0618 100644 --- a/DeviceHub/DeviceHubWindow.ui +++ b/DeviceHub/DeviceHubWindow.ui @@ -77,22 +77,6 @@ - - - - 1030 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 9c613db..d0e8a20 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -51,6 +51,10 @@ { this->devCode = devCode; } +QString PhaseDevice::getDeviceId() +{ + return this->deviceId; +} void PhaseDevice::setDeviceId(QString deviceId) { this->deviceId = deviceId; @@ -61,6 +65,11 @@ return this->serialUtil.isOpen(); } +void PhaseDevice::clearChannelPhaseData(int channelNo) +{ + this->phaseVector[channelNo - 1].clear(); +} + void PhaseDevice::initSerialPort() { int master = SettingConfig::getInstance().MASTER; @@ -87,6 +96,11 @@ this->serialUtil.sendData(startCmd.toLocal8Bit()); QThread::msleep(100); } + + // 开始计算后清除累积的相位数据和准确度计算结果 + for (int i = 0; i < phaseVector.size(); i++) { + phaseVector[i].clear(); + } } void PhaseDevice::stopWork() diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index f42eba8..7bce7ac 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -196,7 +196,7 @@ // 绑定显示函数 connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); - // 查询计数器的通道 并初始化串口 + // 查询计数器的通道 this->getChannelList(deviceId); } diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 0a907f1..9ac4497 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -64,45 +64,6 @@ return resultObj; } -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { diff --git a/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index a37122b..c84ad21 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -19,7 +19,6 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 77975b3..de50156 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -21,8 +21,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置stackWidget的大小 @@ -84,11 +83,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void DeviceHubWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void DeviceHubWindow::on_devTypeSelect_currentIndexChanged(int index) { ui->stackedWidget->setCurrentIndex(index); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index 39443a8..3b4c10a 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -50,7 +50,6 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); void on_devTypeSelect_currentIndexChanged(int index); void on_devSelect_currentIndexChanged(int index); diff --git a/DeviceHub/DeviceHubWindow.ui b/DeviceHub/DeviceHubWindow.ui index cc6ccd0..70c0618 100644 --- a/DeviceHub/DeviceHubWindow.ui +++ b/DeviceHub/DeviceHubWindow.ui @@ -77,22 +77,6 @@ - - - - 1030 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 9c613db..d0e8a20 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -51,6 +51,10 @@ { this->devCode = devCode; } +QString PhaseDevice::getDeviceId() +{ + return this->deviceId; +} void PhaseDevice::setDeviceId(QString deviceId) { this->deviceId = deviceId; @@ -61,6 +65,11 @@ return this->serialUtil.isOpen(); } +void PhaseDevice::clearChannelPhaseData(int channelNo) +{ + this->phaseVector[channelNo - 1].clear(); +} + void PhaseDevice::initSerialPort() { int master = SettingConfig::getInstance().MASTER; @@ -87,6 +96,11 @@ this->serialUtil.sendData(startCmd.toLocal8Bit()); QThread::msleep(100); } + + // 开始计算后清除累积的相位数据和准确度计算结果 + for (int i = 0; i < phaseVector.size(); i++) { + phaseVector[i].clear(); + } } void PhaseDevice::stopWork() diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 7531798..06a6b0f 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -27,8 +27,11 @@ void setBaudRate(int baudRate); QString getDevCode(); void setDevCode(QString devCode); + QString getDeviceId(); void setDeviceId(QString deviceId); + void clearChannelPhaseData(int channelNo); + bool isSerialOpen(); private: QString deviceId; diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index f42eba8..7bce7ac 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -196,7 +196,7 @@ // 绑定显示函数 connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); - // 查询计数器的通道 并初始化串口 + // 查询计数器的通道 this->getChannelList(deviceId); } diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 0a907f1..9ac4497 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -64,45 +64,6 @@ return resultObj; } -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { diff --git a/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index a37122b..c84ad21 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -19,7 +19,6 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 77975b3..de50156 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -21,8 +21,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置stackWidget的大小 @@ -84,11 +83,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void DeviceHubWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void DeviceHubWindow::on_devTypeSelect_currentIndexChanged(int index) { ui->stackedWidget->setCurrentIndex(index); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index 39443a8..3b4c10a 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -50,7 +50,6 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); void on_devTypeSelect_currentIndexChanged(int index); void on_devSelect_currentIndexChanged(int index); diff --git a/DeviceHub/DeviceHubWindow.ui b/DeviceHub/DeviceHubWindow.ui index cc6ccd0..70c0618 100644 --- a/DeviceHub/DeviceHubWindow.ui +++ b/DeviceHub/DeviceHubWindow.ui @@ -77,22 +77,6 @@ - - - - 1030 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 9c613db..d0e8a20 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -51,6 +51,10 @@ { this->devCode = devCode; } +QString PhaseDevice::getDeviceId() +{ + return this->deviceId; +} void PhaseDevice::setDeviceId(QString deviceId) { this->deviceId = deviceId; @@ -61,6 +65,11 @@ return this->serialUtil.isOpen(); } +void PhaseDevice::clearChannelPhaseData(int channelNo) +{ + this->phaseVector[channelNo - 1].clear(); +} + void PhaseDevice::initSerialPort() { int master = SettingConfig::getInstance().MASTER; @@ -87,6 +96,11 @@ this->serialUtil.sendData(startCmd.toLocal8Bit()); QThread::msleep(100); } + + // 开始计算后清除累积的相位数据和准确度计算结果 + for (int i = 0; i < phaseVector.size(); i++) { + phaseVector[i].clear(); + } } void PhaseDevice::stopWork() diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 7531798..06a6b0f 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -27,8 +27,11 @@ void setBaudRate(int baudRate); QString getDevCode(); void setDevCode(QString devCode); + QString getDeviceId(); void setDeviceId(QString deviceId); + void clearChannelPhaseData(int channelNo); + bool isSerialOpen(); private: QString deviceId; diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 51673f5..90db100 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -27,10 +27,9 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); - ui->endButt->move(screenRect.width() - 200, 10); - ui->startButt->move(screenRect.width() - 260, 10); + ui->minButt->move(screenRect.width() - 80, 10); + ui->endButt->move(screenRect.width() - 140, 10); + ui->startButt->move(screenRect.width() - 200, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -39,6 +38,16 @@ ui->logo->setText(QString("相位测量数据采集软件——%1").arg(SettingConfig::getInstance().MASTER == 1 ? "主" : "备")); + if (SettingConfig::getInstance().NEED_KAFKA == 1) { + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_CMD_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &PhaseWindow::onPhaseCommandReceived); + } + httpReq = new HttpRequestController(this); // 1. 获取访问接口需要的token int retCode = this->initHttpToken(); @@ -49,44 +58,17 @@ QTimer::singleShot(1000, qApp, SLOT(quit())); } // 2. 获取字典值:设备类型 - retCode = this->initDictDeviceTypes(); // 3. 获取比相仪设备列表 - QJsonObject devListRes = this->initDeviceList(); - if (devListRes.find("code")->toInt() == 200) - { - ui->devSelect->clear(); + initDeviceList(); - // 4. 将获取到的设备添加到下拉列表框中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - QJsonObject devItem = devArray.at(i).toObject(); - ui->devSelect->addItem(devItem.find("deviceName")->toString(), devItem.find("deviceNo")->toString()); + // 4. 打开串口 + for (int i = 0; i < deviceList.size(); i++) { + PhaseDevice * devicePtr = deviceList.at(i); + devicePtr->initSerialPort(); + devicePtr->startWork(); - PhaseDevice * device = new PhaseDevice(this); - deviceList.append(device); - - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceId(devItem.find("deviceId")->toString()); - - connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); - connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); - - device->initSerialPort(); - - QThread::msleep(100); - device->startWork(); - } - // 5. 设置下拉框的样式 - QStandardItemModel * model = qobject_cast(ui->devSelect->model()); - for (int i = 0; i < model->rowCount(); ++i) - { - QStandardItem * item = model->item(i); - item->setSizeHint({ 0, 30 }); - } + QThread::msleep(100); } // 6. 绘制一个设备的多个通道数据面板 @@ -316,15 +298,69 @@ SettingConfig::getInstance().APP_KEY); return response.find("code")->toInt(); } -int PhaseWindow::initDictDeviceTypes() + +void PhaseWindow::initDeviceList() { - QJsonObject response = httpReq->initDictDeviceType(); - return response.find("code")->toInt(); + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.find("code")->toInt() == 200) + { + ui->devSelect->clear(); + + // 4. 将获取到的设备添加到下拉列表框中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) + { + QJsonObject devItem = devArray.at(i).toObject(); + ui->devSelect->addItem(devItem.value("deviceName").toString(), devItem.value("deviceNo").toString()); + + QString deviceId = devItem.value("deviceId").toString(); + + PhaseDevice * device = new PhaseDevice(this); + deviceList.append(device); + + ConstCache::getInstance().deviceMap.insert(deviceId, device); + + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceId(deviceId); + + connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); + connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); + + this->getChannelList(deviceId); + } + // 5. 设置下拉框的样式 + QStandardItemModel * model = qobject_cast(ui->devSelect->model()); + for (int i = 0; i < model->rowCount(); ++i) + { + QStandardItem * item = model->item(i); + item->setSizeHint({ 0, 30 }); + } + } } -QJsonObject PhaseWindow::initDeviceList() + +void PhaseWindow::getChannelList(QString deviceId) { - QJsonObject response = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - return response; + // 查询计数器的通道 + QJsonObject response = httpReq->getPhaseChannelList(deviceId); + if (response.value("code").toInt() == 200) + { + // 将通道绘制在界面上 + QJsonArray channelArray = response.value("data").toArray(); + for (int i = 0; i < channelArray.size(); i++) + { + // 接口返回的channel对象 + QJsonObject channelItem = channelArray.at(i).toObject(); + QString channelId = channelItem.value("id").toString(); // 通道ID + int channelNo = channelItem.value("channelNo").toInt(); + + // 添加到通道的集合中 key=channelId value=deviceId-channelNo + if (channelNo > 0) { + ConstCache::getInstance().channelMap.insert(channelId, QString("%1-%2").arg(deviceId).arg(channelNo)); + } + } + } } void PhaseWindow::on_minButt_clicked() @@ -332,11 +368,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void PhaseWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void PhaseWindow::on_startButt_clicked() { int devSelected = ui->devSelect->currentIndex(); @@ -385,3 +416,46 @@ ((QLabel *)channelBox->children().at(20))->setText(QString("(%1)").arg(0)); // tau=10000 Count } } + +void PhaseWindow::onPhaseCommandReceived(QJsonObject command) +{ + // 缺少必须的字段deviceId 或 cmd 直接返回 + if (command.contains("deviceId") == false || command.contains("cmd") == false) { + return; + } + + QString deviceId = command.value("deviceId").toString(); + QString cmdType = command.value("cmd").toString(); + + // deviceId不属于子系统内的比相仪 直接返回 + if (ConstCache::getInstance().deviceMap.contains(deviceId) == false) { + return; + } + + if (cmdType.toUpper() == "START" || cmdType.toUpper() == "STOP") + { + // 设备开始采集或者停止采集指令 + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (devicePtr != nullptr) { + if (cmdType.toUpper() == "START") { + devicePtr->startWork(); + } else if (cmdType.toUpper() == "STOP") { + devicePtr->stopWork(); + } + } + } else if (cmdType.toUpper() == "CLEAR") + { + // 清除通道的数据 通道级指令 + QString channelId = command.value("channelId").toString(); + if (ConstCache::getInstance().channelMap.contains(channelId) == true) { + QString channelIndex = ConstCache::getInstance().channelMap.value(channelId); + QString channelDevId = channelIndex.split("-").at(0); + int channelNo = ((QString) channelIndex.split("-").at(1)).toInt(); + + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(channelDevId); + if (devicePtr != nullptr) { + devicePtr->clearChannelPhaseData(channelNo); + } + } + } +} diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index f42eba8..7bce7ac 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -196,7 +196,7 @@ // 绑定显示函数 connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); - // 查询计数器的通道 并初始化串口 + // 查询计数器的通道 this->getChannelList(deviceId); } diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 0a907f1..9ac4497 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -64,45 +64,6 @@ return resultObj; } -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { diff --git a/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index a37122b..c84ad21 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -19,7 +19,6 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 77975b3..de50156 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -21,8 +21,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置stackWidget的大小 @@ -84,11 +83,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void DeviceHubWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void DeviceHubWindow::on_devTypeSelect_currentIndexChanged(int index) { ui->stackedWidget->setCurrentIndex(index); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index 39443a8..3b4c10a 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -50,7 +50,6 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); void on_devTypeSelect_currentIndexChanged(int index); void on_devSelect_currentIndexChanged(int index); diff --git a/DeviceHub/DeviceHubWindow.ui b/DeviceHub/DeviceHubWindow.ui index cc6ccd0..70c0618 100644 --- a/DeviceHub/DeviceHubWindow.ui +++ b/DeviceHub/DeviceHubWindow.ui @@ -77,22 +77,6 @@ - - - - 1030 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 9c613db..d0e8a20 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -51,6 +51,10 @@ { this->devCode = devCode; } +QString PhaseDevice::getDeviceId() +{ + return this->deviceId; +} void PhaseDevice::setDeviceId(QString deviceId) { this->deviceId = deviceId; @@ -61,6 +65,11 @@ return this->serialUtil.isOpen(); } +void PhaseDevice::clearChannelPhaseData(int channelNo) +{ + this->phaseVector[channelNo - 1].clear(); +} + void PhaseDevice::initSerialPort() { int master = SettingConfig::getInstance().MASTER; @@ -87,6 +96,11 @@ this->serialUtil.sendData(startCmd.toLocal8Bit()); QThread::msleep(100); } + + // 开始计算后清除累积的相位数据和准确度计算结果 + for (int i = 0; i < phaseVector.size(); i++) { + phaseVector[i].clear(); + } } void PhaseDevice::stopWork() diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 7531798..06a6b0f 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -27,8 +27,11 @@ void setBaudRate(int baudRate); QString getDevCode(); void setDevCode(QString devCode); + QString getDeviceId(); void setDeviceId(QString deviceId); + void clearChannelPhaseData(int channelNo); + bool isSerialOpen(); private: QString deviceId; diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 51673f5..90db100 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -27,10 +27,9 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); - ui->endButt->move(screenRect.width() - 200, 10); - ui->startButt->move(screenRect.width() - 260, 10); + ui->minButt->move(screenRect.width() - 80, 10); + ui->endButt->move(screenRect.width() - 140, 10); + ui->startButt->move(screenRect.width() - 200, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -39,6 +38,16 @@ ui->logo->setText(QString("相位测量数据采集软件——%1").arg(SettingConfig::getInstance().MASTER == 1 ? "主" : "备")); + if (SettingConfig::getInstance().NEED_KAFKA == 1) { + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_CMD_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &PhaseWindow::onPhaseCommandReceived); + } + httpReq = new HttpRequestController(this); // 1. 获取访问接口需要的token int retCode = this->initHttpToken(); @@ -49,44 +58,17 @@ QTimer::singleShot(1000, qApp, SLOT(quit())); } // 2. 获取字典值:设备类型 - retCode = this->initDictDeviceTypes(); // 3. 获取比相仪设备列表 - QJsonObject devListRes = this->initDeviceList(); - if (devListRes.find("code")->toInt() == 200) - { - ui->devSelect->clear(); + initDeviceList(); - // 4. 将获取到的设备添加到下拉列表框中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - QJsonObject devItem = devArray.at(i).toObject(); - ui->devSelect->addItem(devItem.find("deviceName")->toString(), devItem.find("deviceNo")->toString()); + // 4. 打开串口 + for (int i = 0; i < deviceList.size(); i++) { + PhaseDevice * devicePtr = deviceList.at(i); + devicePtr->initSerialPort(); + devicePtr->startWork(); - PhaseDevice * device = new PhaseDevice(this); - deviceList.append(device); - - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceId(devItem.find("deviceId")->toString()); - - connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); - connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); - - device->initSerialPort(); - - QThread::msleep(100); - device->startWork(); - } - // 5. 设置下拉框的样式 - QStandardItemModel * model = qobject_cast(ui->devSelect->model()); - for (int i = 0; i < model->rowCount(); ++i) - { - QStandardItem * item = model->item(i); - item->setSizeHint({ 0, 30 }); - } + QThread::msleep(100); } // 6. 绘制一个设备的多个通道数据面板 @@ -316,15 +298,69 @@ SettingConfig::getInstance().APP_KEY); return response.find("code")->toInt(); } -int PhaseWindow::initDictDeviceTypes() + +void PhaseWindow::initDeviceList() { - QJsonObject response = httpReq->initDictDeviceType(); - return response.find("code")->toInt(); + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.find("code")->toInt() == 200) + { + ui->devSelect->clear(); + + // 4. 将获取到的设备添加到下拉列表框中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) + { + QJsonObject devItem = devArray.at(i).toObject(); + ui->devSelect->addItem(devItem.value("deviceName").toString(), devItem.value("deviceNo").toString()); + + QString deviceId = devItem.value("deviceId").toString(); + + PhaseDevice * device = new PhaseDevice(this); + deviceList.append(device); + + ConstCache::getInstance().deviceMap.insert(deviceId, device); + + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceId(deviceId); + + connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); + connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); + + this->getChannelList(deviceId); + } + // 5. 设置下拉框的样式 + QStandardItemModel * model = qobject_cast(ui->devSelect->model()); + for (int i = 0; i < model->rowCount(); ++i) + { + QStandardItem * item = model->item(i); + item->setSizeHint({ 0, 30 }); + } + } } -QJsonObject PhaseWindow::initDeviceList() + +void PhaseWindow::getChannelList(QString deviceId) { - QJsonObject response = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - return response; + // 查询计数器的通道 + QJsonObject response = httpReq->getPhaseChannelList(deviceId); + if (response.value("code").toInt() == 200) + { + // 将通道绘制在界面上 + QJsonArray channelArray = response.value("data").toArray(); + for (int i = 0; i < channelArray.size(); i++) + { + // 接口返回的channel对象 + QJsonObject channelItem = channelArray.at(i).toObject(); + QString channelId = channelItem.value("id").toString(); // 通道ID + int channelNo = channelItem.value("channelNo").toInt(); + + // 添加到通道的集合中 key=channelId value=deviceId-channelNo + if (channelNo > 0) { + ConstCache::getInstance().channelMap.insert(channelId, QString("%1-%2").arg(deviceId).arg(channelNo)); + } + } + } } void PhaseWindow::on_minButt_clicked() @@ -332,11 +368,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void PhaseWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void PhaseWindow::on_startButt_clicked() { int devSelected = ui->devSelect->currentIndex(); @@ -385,3 +416,46 @@ ((QLabel *)channelBox->children().at(20))->setText(QString("(%1)").arg(0)); // tau=10000 Count } } + +void PhaseWindow::onPhaseCommandReceived(QJsonObject command) +{ + // 缺少必须的字段deviceId 或 cmd 直接返回 + if (command.contains("deviceId") == false || command.contains("cmd") == false) { + return; + } + + QString deviceId = command.value("deviceId").toString(); + QString cmdType = command.value("cmd").toString(); + + // deviceId不属于子系统内的比相仪 直接返回 + if (ConstCache::getInstance().deviceMap.contains(deviceId) == false) { + return; + } + + if (cmdType.toUpper() == "START" || cmdType.toUpper() == "STOP") + { + // 设备开始采集或者停止采集指令 + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (devicePtr != nullptr) { + if (cmdType.toUpper() == "START") { + devicePtr->startWork(); + } else if (cmdType.toUpper() == "STOP") { + devicePtr->stopWork(); + } + } + } else if (cmdType.toUpper() == "CLEAR") + { + // 清除通道的数据 通道级指令 + QString channelId = command.value("channelId").toString(); + if (ConstCache::getInstance().channelMap.contains(channelId) == true) { + QString channelIndex = ConstCache::getInstance().channelMap.value(channelId); + QString channelDevId = channelIndex.split("-").at(0); + int channelNo = ((QString) channelIndex.split("-").at(1)).toInt(); + + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(channelDevId); + if (devicePtr != nullptr) { + devicePtr->clearChannelPhaseData(channelNo); + } + } + } +} diff --git a/PhaseCompAcq/PhaseWindow.h b/PhaseCompAcq/PhaseWindow.h index 572bbde..961e0b1 100644 --- a/PhaseCompAcq/PhaseWindow.h +++ b/PhaseCompAcq/PhaseWindow.h @@ -8,6 +8,7 @@ #include "PhaseDevice.h" #include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaConsumer.h" #include "common/HttpRequestController.h" #include "common/HttpServer.h" @@ -30,21 +31,22 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); - void on_startButt_clicked(); void on_endButt_clicked(); void on_devSelect_currentIndexChanged(int index); + void onPhaseCommandReceived(QJsonObject command); + private: int initHttpToken(); - int initDictDeviceTypes(); - QJsonObject initDeviceList(); + void initDeviceList(); + void getChannelList(QString deviceId); Ui::PhaseWindow *ui; + QKafkaConsumer * kafkaConsumer; HttpRequestController * httpReq; QList deviceList; diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index f42eba8..7bce7ac 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -196,7 +196,7 @@ // 绑定显示函数 connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); - // 查询计数器的通道 并初始化串口 + // 查询计数器的通道 this->getChannelList(deviceId); } diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 0a907f1..9ac4497 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -64,45 +64,6 @@ return resultObj; } -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { diff --git a/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index a37122b..c84ad21 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -19,7 +19,6 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 77975b3..de50156 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -21,8 +21,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置stackWidget的大小 @@ -84,11 +83,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void DeviceHubWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void DeviceHubWindow::on_devTypeSelect_currentIndexChanged(int index) { ui->stackedWidget->setCurrentIndex(index); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index 39443a8..3b4c10a 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -50,7 +50,6 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); void on_devTypeSelect_currentIndexChanged(int index); void on_devSelect_currentIndexChanged(int index); diff --git a/DeviceHub/DeviceHubWindow.ui b/DeviceHub/DeviceHubWindow.ui index cc6ccd0..70c0618 100644 --- a/DeviceHub/DeviceHubWindow.ui +++ b/DeviceHub/DeviceHubWindow.ui @@ -77,22 +77,6 @@ - - - - 1030 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 9c613db..d0e8a20 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -51,6 +51,10 @@ { this->devCode = devCode; } +QString PhaseDevice::getDeviceId() +{ + return this->deviceId; +} void PhaseDevice::setDeviceId(QString deviceId) { this->deviceId = deviceId; @@ -61,6 +65,11 @@ return this->serialUtil.isOpen(); } +void PhaseDevice::clearChannelPhaseData(int channelNo) +{ + this->phaseVector[channelNo - 1].clear(); +} + void PhaseDevice::initSerialPort() { int master = SettingConfig::getInstance().MASTER; @@ -87,6 +96,11 @@ this->serialUtil.sendData(startCmd.toLocal8Bit()); QThread::msleep(100); } + + // 开始计算后清除累积的相位数据和准确度计算结果 + for (int i = 0; i < phaseVector.size(); i++) { + phaseVector[i].clear(); + } } void PhaseDevice::stopWork() diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 7531798..06a6b0f 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -27,8 +27,11 @@ void setBaudRate(int baudRate); QString getDevCode(); void setDevCode(QString devCode); + QString getDeviceId(); void setDeviceId(QString deviceId); + void clearChannelPhaseData(int channelNo); + bool isSerialOpen(); private: QString deviceId; diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 51673f5..90db100 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -27,10 +27,9 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); - ui->endButt->move(screenRect.width() - 200, 10); - ui->startButt->move(screenRect.width() - 260, 10); + ui->minButt->move(screenRect.width() - 80, 10); + ui->endButt->move(screenRect.width() - 140, 10); + ui->startButt->move(screenRect.width() - 200, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -39,6 +38,16 @@ ui->logo->setText(QString("相位测量数据采集软件——%1").arg(SettingConfig::getInstance().MASTER == 1 ? "主" : "备")); + if (SettingConfig::getInstance().NEED_KAFKA == 1) { + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_CMD_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &PhaseWindow::onPhaseCommandReceived); + } + httpReq = new HttpRequestController(this); // 1. 获取访问接口需要的token int retCode = this->initHttpToken(); @@ -49,44 +58,17 @@ QTimer::singleShot(1000, qApp, SLOT(quit())); } // 2. 获取字典值:设备类型 - retCode = this->initDictDeviceTypes(); // 3. 获取比相仪设备列表 - QJsonObject devListRes = this->initDeviceList(); - if (devListRes.find("code")->toInt() == 200) - { - ui->devSelect->clear(); + initDeviceList(); - // 4. 将获取到的设备添加到下拉列表框中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - QJsonObject devItem = devArray.at(i).toObject(); - ui->devSelect->addItem(devItem.find("deviceName")->toString(), devItem.find("deviceNo")->toString()); + // 4. 打开串口 + for (int i = 0; i < deviceList.size(); i++) { + PhaseDevice * devicePtr = deviceList.at(i); + devicePtr->initSerialPort(); + devicePtr->startWork(); - PhaseDevice * device = new PhaseDevice(this); - deviceList.append(device); - - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceId(devItem.find("deviceId")->toString()); - - connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); - connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); - - device->initSerialPort(); - - QThread::msleep(100); - device->startWork(); - } - // 5. 设置下拉框的样式 - QStandardItemModel * model = qobject_cast(ui->devSelect->model()); - for (int i = 0; i < model->rowCount(); ++i) - { - QStandardItem * item = model->item(i); - item->setSizeHint({ 0, 30 }); - } + QThread::msleep(100); } // 6. 绘制一个设备的多个通道数据面板 @@ -316,15 +298,69 @@ SettingConfig::getInstance().APP_KEY); return response.find("code")->toInt(); } -int PhaseWindow::initDictDeviceTypes() + +void PhaseWindow::initDeviceList() { - QJsonObject response = httpReq->initDictDeviceType(); - return response.find("code")->toInt(); + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.find("code")->toInt() == 200) + { + ui->devSelect->clear(); + + // 4. 将获取到的设备添加到下拉列表框中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) + { + QJsonObject devItem = devArray.at(i).toObject(); + ui->devSelect->addItem(devItem.value("deviceName").toString(), devItem.value("deviceNo").toString()); + + QString deviceId = devItem.value("deviceId").toString(); + + PhaseDevice * device = new PhaseDevice(this); + deviceList.append(device); + + ConstCache::getInstance().deviceMap.insert(deviceId, device); + + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceId(deviceId); + + connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); + connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); + + this->getChannelList(deviceId); + } + // 5. 设置下拉框的样式 + QStandardItemModel * model = qobject_cast(ui->devSelect->model()); + for (int i = 0; i < model->rowCount(); ++i) + { + QStandardItem * item = model->item(i); + item->setSizeHint({ 0, 30 }); + } + } } -QJsonObject PhaseWindow::initDeviceList() + +void PhaseWindow::getChannelList(QString deviceId) { - QJsonObject response = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - return response; + // 查询计数器的通道 + QJsonObject response = httpReq->getPhaseChannelList(deviceId); + if (response.value("code").toInt() == 200) + { + // 将通道绘制在界面上 + QJsonArray channelArray = response.value("data").toArray(); + for (int i = 0; i < channelArray.size(); i++) + { + // 接口返回的channel对象 + QJsonObject channelItem = channelArray.at(i).toObject(); + QString channelId = channelItem.value("id").toString(); // 通道ID + int channelNo = channelItem.value("channelNo").toInt(); + + // 添加到通道的集合中 key=channelId value=deviceId-channelNo + if (channelNo > 0) { + ConstCache::getInstance().channelMap.insert(channelId, QString("%1-%2").arg(deviceId).arg(channelNo)); + } + } + } } void PhaseWindow::on_minButt_clicked() @@ -332,11 +368,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void PhaseWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void PhaseWindow::on_startButt_clicked() { int devSelected = ui->devSelect->currentIndex(); @@ -385,3 +416,46 @@ ((QLabel *)channelBox->children().at(20))->setText(QString("(%1)").arg(0)); // tau=10000 Count } } + +void PhaseWindow::onPhaseCommandReceived(QJsonObject command) +{ + // 缺少必须的字段deviceId 或 cmd 直接返回 + if (command.contains("deviceId") == false || command.contains("cmd") == false) { + return; + } + + QString deviceId = command.value("deviceId").toString(); + QString cmdType = command.value("cmd").toString(); + + // deviceId不属于子系统内的比相仪 直接返回 + if (ConstCache::getInstance().deviceMap.contains(deviceId) == false) { + return; + } + + if (cmdType.toUpper() == "START" || cmdType.toUpper() == "STOP") + { + // 设备开始采集或者停止采集指令 + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (devicePtr != nullptr) { + if (cmdType.toUpper() == "START") { + devicePtr->startWork(); + } else if (cmdType.toUpper() == "STOP") { + devicePtr->stopWork(); + } + } + } else if (cmdType.toUpper() == "CLEAR") + { + // 清除通道的数据 通道级指令 + QString channelId = command.value("channelId").toString(); + if (ConstCache::getInstance().channelMap.contains(channelId) == true) { + QString channelIndex = ConstCache::getInstance().channelMap.value(channelId); + QString channelDevId = channelIndex.split("-").at(0); + int channelNo = ((QString) channelIndex.split("-").at(1)).toInt(); + + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(channelDevId); + if (devicePtr != nullptr) { + devicePtr->clearChannelPhaseData(channelNo); + } + } + } +} diff --git a/PhaseCompAcq/PhaseWindow.h b/PhaseCompAcq/PhaseWindow.h index 572bbde..961e0b1 100644 --- a/PhaseCompAcq/PhaseWindow.h +++ b/PhaseCompAcq/PhaseWindow.h @@ -8,6 +8,7 @@ #include "PhaseDevice.h" #include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaConsumer.h" #include "common/HttpRequestController.h" #include "common/HttpServer.h" @@ -30,21 +31,22 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); - void on_startButt_clicked(); void on_endButt_clicked(); void on_devSelect_currentIndexChanged(int index); + void onPhaseCommandReceived(QJsonObject command); + private: int initHttpToken(); - int initDictDeviceTypes(); - QJsonObject initDeviceList(); + void initDeviceList(); + void getChannelList(QString deviceId); Ui::PhaseWindow *ui; + QKafkaConsumer * kafkaConsumer; HttpRequestController * httpReq; QList deviceList; diff --git a/PhaseCompAcq/PhaseWindow.ui b/PhaseCompAcq/PhaseWindow.ui index d083fb9..9f9456c 100644 --- a/PhaseCompAcq/PhaseWindow.ui +++ b/PhaseCompAcq/PhaseWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index f42eba8..7bce7ac 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -196,7 +196,7 @@ // 绑定显示函数 connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); - // 查询计数器的通道 并初始化串口 + // 查询计数器的通道 this->getChannelList(deviceId); } diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 0a907f1..9ac4497 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -64,45 +64,6 @@ return resultObj; } -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { diff --git a/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index a37122b..c84ad21 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -19,7 +19,6 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 77975b3..de50156 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -21,8 +21,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置stackWidget的大小 @@ -84,11 +83,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void DeviceHubWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void DeviceHubWindow::on_devTypeSelect_currentIndexChanged(int index) { ui->stackedWidget->setCurrentIndex(index); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index 39443a8..3b4c10a 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -50,7 +50,6 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); void on_devTypeSelect_currentIndexChanged(int index); void on_devSelect_currentIndexChanged(int index); diff --git a/DeviceHub/DeviceHubWindow.ui b/DeviceHub/DeviceHubWindow.ui index cc6ccd0..70c0618 100644 --- a/DeviceHub/DeviceHubWindow.ui +++ b/DeviceHub/DeviceHubWindow.ui @@ -77,22 +77,6 @@ - - - - 1030 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 9c613db..d0e8a20 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -51,6 +51,10 @@ { this->devCode = devCode; } +QString PhaseDevice::getDeviceId() +{ + return this->deviceId; +} void PhaseDevice::setDeviceId(QString deviceId) { this->deviceId = deviceId; @@ -61,6 +65,11 @@ return this->serialUtil.isOpen(); } +void PhaseDevice::clearChannelPhaseData(int channelNo) +{ + this->phaseVector[channelNo - 1].clear(); +} + void PhaseDevice::initSerialPort() { int master = SettingConfig::getInstance().MASTER; @@ -87,6 +96,11 @@ this->serialUtil.sendData(startCmd.toLocal8Bit()); QThread::msleep(100); } + + // 开始计算后清除累积的相位数据和准确度计算结果 + for (int i = 0; i < phaseVector.size(); i++) { + phaseVector[i].clear(); + } } void PhaseDevice::stopWork() diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 7531798..06a6b0f 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -27,8 +27,11 @@ void setBaudRate(int baudRate); QString getDevCode(); void setDevCode(QString devCode); + QString getDeviceId(); void setDeviceId(QString deviceId); + void clearChannelPhaseData(int channelNo); + bool isSerialOpen(); private: QString deviceId; diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 51673f5..90db100 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -27,10 +27,9 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); - ui->endButt->move(screenRect.width() - 200, 10); - ui->startButt->move(screenRect.width() - 260, 10); + ui->minButt->move(screenRect.width() - 80, 10); + ui->endButt->move(screenRect.width() - 140, 10); + ui->startButt->move(screenRect.width() - 200, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -39,6 +38,16 @@ ui->logo->setText(QString("相位测量数据采集软件——%1").arg(SettingConfig::getInstance().MASTER == 1 ? "主" : "备")); + if (SettingConfig::getInstance().NEED_KAFKA == 1) { + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_CMD_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &PhaseWindow::onPhaseCommandReceived); + } + httpReq = new HttpRequestController(this); // 1. 获取访问接口需要的token int retCode = this->initHttpToken(); @@ -49,44 +58,17 @@ QTimer::singleShot(1000, qApp, SLOT(quit())); } // 2. 获取字典值:设备类型 - retCode = this->initDictDeviceTypes(); // 3. 获取比相仪设备列表 - QJsonObject devListRes = this->initDeviceList(); - if (devListRes.find("code")->toInt() == 200) - { - ui->devSelect->clear(); + initDeviceList(); - // 4. 将获取到的设备添加到下拉列表框中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - QJsonObject devItem = devArray.at(i).toObject(); - ui->devSelect->addItem(devItem.find("deviceName")->toString(), devItem.find("deviceNo")->toString()); + // 4. 打开串口 + for (int i = 0; i < deviceList.size(); i++) { + PhaseDevice * devicePtr = deviceList.at(i); + devicePtr->initSerialPort(); + devicePtr->startWork(); - PhaseDevice * device = new PhaseDevice(this); - deviceList.append(device); - - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceId(devItem.find("deviceId")->toString()); - - connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); - connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); - - device->initSerialPort(); - - QThread::msleep(100); - device->startWork(); - } - // 5. 设置下拉框的样式 - QStandardItemModel * model = qobject_cast(ui->devSelect->model()); - for (int i = 0; i < model->rowCount(); ++i) - { - QStandardItem * item = model->item(i); - item->setSizeHint({ 0, 30 }); - } + QThread::msleep(100); } // 6. 绘制一个设备的多个通道数据面板 @@ -316,15 +298,69 @@ SettingConfig::getInstance().APP_KEY); return response.find("code")->toInt(); } -int PhaseWindow::initDictDeviceTypes() + +void PhaseWindow::initDeviceList() { - QJsonObject response = httpReq->initDictDeviceType(); - return response.find("code")->toInt(); + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.find("code")->toInt() == 200) + { + ui->devSelect->clear(); + + // 4. 将获取到的设备添加到下拉列表框中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) + { + QJsonObject devItem = devArray.at(i).toObject(); + ui->devSelect->addItem(devItem.value("deviceName").toString(), devItem.value("deviceNo").toString()); + + QString deviceId = devItem.value("deviceId").toString(); + + PhaseDevice * device = new PhaseDevice(this); + deviceList.append(device); + + ConstCache::getInstance().deviceMap.insert(deviceId, device); + + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceId(deviceId); + + connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); + connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); + + this->getChannelList(deviceId); + } + // 5. 设置下拉框的样式 + QStandardItemModel * model = qobject_cast(ui->devSelect->model()); + for (int i = 0; i < model->rowCount(); ++i) + { + QStandardItem * item = model->item(i); + item->setSizeHint({ 0, 30 }); + } + } } -QJsonObject PhaseWindow::initDeviceList() + +void PhaseWindow::getChannelList(QString deviceId) { - QJsonObject response = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - return response; + // 查询计数器的通道 + QJsonObject response = httpReq->getPhaseChannelList(deviceId); + if (response.value("code").toInt() == 200) + { + // 将通道绘制在界面上 + QJsonArray channelArray = response.value("data").toArray(); + for (int i = 0; i < channelArray.size(); i++) + { + // 接口返回的channel对象 + QJsonObject channelItem = channelArray.at(i).toObject(); + QString channelId = channelItem.value("id").toString(); // 通道ID + int channelNo = channelItem.value("channelNo").toInt(); + + // 添加到通道的集合中 key=channelId value=deviceId-channelNo + if (channelNo > 0) { + ConstCache::getInstance().channelMap.insert(channelId, QString("%1-%2").arg(deviceId).arg(channelNo)); + } + } + } } void PhaseWindow::on_minButt_clicked() @@ -332,11 +368,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void PhaseWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void PhaseWindow::on_startButt_clicked() { int devSelected = ui->devSelect->currentIndex(); @@ -385,3 +416,46 @@ ((QLabel *)channelBox->children().at(20))->setText(QString("(%1)").arg(0)); // tau=10000 Count } } + +void PhaseWindow::onPhaseCommandReceived(QJsonObject command) +{ + // 缺少必须的字段deviceId 或 cmd 直接返回 + if (command.contains("deviceId") == false || command.contains("cmd") == false) { + return; + } + + QString deviceId = command.value("deviceId").toString(); + QString cmdType = command.value("cmd").toString(); + + // deviceId不属于子系统内的比相仪 直接返回 + if (ConstCache::getInstance().deviceMap.contains(deviceId) == false) { + return; + } + + if (cmdType.toUpper() == "START" || cmdType.toUpper() == "STOP") + { + // 设备开始采集或者停止采集指令 + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (devicePtr != nullptr) { + if (cmdType.toUpper() == "START") { + devicePtr->startWork(); + } else if (cmdType.toUpper() == "STOP") { + devicePtr->stopWork(); + } + } + } else if (cmdType.toUpper() == "CLEAR") + { + // 清除通道的数据 通道级指令 + QString channelId = command.value("channelId").toString(); + if (ConstCache::getInstance().channelMap.contains(channelId) == true) { + QString channelIndex = ConstCache::getInstance().channelMap.value(channelId); + QString channelDevId = channelIndex.split("-").at(0); + int channelNo = ((QString) channelIndex.split("-").at(1)).toInt(); + + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(channelDevId); + if (devicePtr != nullptr) { + devicePtr->clearChannelPhaseData(channelNo); + } + } + } +} diff --git a/PhaseCompAcq/PhaseWindow.h b/PhaseCompAcq/PhaseWindow.h index 572bbde..961e0b1 100644 --- a/PhaseCompAcq/PhaseWindow.h +++ b/PhaseCompAcq/PhaseWindow.h @@ -8,6 +8,7 @@ #include "PhaseDevice.h" #include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaConsumer.h" #include "common/HttpRequestController.h" #include "common/HttpServer.h" @@ -30,21 +31,22 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); - void on_startButt_clicked(); void on_endButt_clicked(); void on_devSelect_currentIndexChanged(int index); + void onPhaseCommandReceived(QJsonObject command); + private: int initHttpToken(); - int initDictDeviceTypes(); - QJsonObject initDeviceList(); + void initDeviceList(); + void getChannelList(QString deviceId); Ui::PhaseWindow *ui; + QKafkaConsumer * kafkaConsumer; HttpRequestController * httpReq; QList deviceList; diff --git a/PhaseCompAcq/PhaseWindow.ui b/PhaseCompAcq/PhaseWindow.ui index d083fb9..9f9456c 100644 --- a/PhaseCompAcq/PhaseWindow.ui +++ b/PhaseCompAcq/PhaseWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/common/ConstCache.h b/PhaseCompAcq/common/ConstCache.h index 6cc831b..0379863 100644 --- a/PhaseCompAcq/common/ConstCache.h +++ b/PhaseCompAcq/common/ConstCache.h @@ -5,6 +5,7 @@ #include #include #include +#include "PhaseDevice.h" class ConstCache : public QObject { @@ -21,6 +22,8 @@ QMap deviceTypes; QList deviceList; + QMap deviceMap; // key=deviceId + QMap channelMap; // key=channelId value=deviceId-no private: ConstCache() {}; diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index f42eba8..7bce7ac 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -196,7 +196,7 @@ // 绑定显示函数 connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); - // 查询计数器的通道 并初始化串口 + // 查询计数器的通道 this->getChannelList(deviceId); } diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 0a907f1..9ac4497 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -64,45 +64,6 @@ return resultObj; } -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { diff --git a/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index a37122b..c84ad21 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -19,7 +19,6 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 77975b3..de50156 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -21,8 +21,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置stackWidget的大小 @@ -84,11 +83,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void DeviceHubWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void DeviceHubWindow::on_devTypeSelect_currentIndexChanged(int index) { ui->stackedWidget->setCurrentIndex(index); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index 39443a8..3b4c10a 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -50,7 +50,6 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); void on_devTypeSelect_currentIndexChanged(int index); void on_devSelect_currentIndexChanged(int index); diff --git a/DeviceHub/DeviceHubWindow.ui b/DeviceHub/DeviceHubWindow.ui index cc6ccd0..70c0618 100644 --- a/DeviceHub/DeviceHubWindow.ui +++ b/DeviceHub/DeviceHubWindow.ui @@ -77,22 +77,6 @@ - - - - 1030 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 9c613db..d0e8a20 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -51,6 +51,10 @@ { this->devCode = devCode; } +QString PhaseDevice::getDeviceId() +{ + return this->deviceId; +} void PhaseDevice::setDeviceId(QString deviceId) { this->deviceId = deviceId; @@ -61,6 +65,11 @@ return this->serialUtil.isOpen(); } +void PhaseDevice::clearChannelPhaseData(int channelNo) +{ + this->phaseVector[channelNo - 1].clear(); +} + void PhaseDevice::initSerialPort() { int master = SettingConfig::getInstance().MASTER; @@ -87,6 +96,11 @@ this->serialUtil.sendData(startCmd.toLocal8Bit()); QThread::msleep(100); } + + // 开始计算后清除累积的相位数据和准确度计算结果 + for (int i = 0; i < phaseVector.size(); i++) { + phaseVector[i].clear(); + } } void PhaseDevice::stopWork() diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 7531798..06a6b0f 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -27,8 +27,11 @@ void setBaudRate(int baudRate); QString getDevCode(); void setDevCode(QString devCode); + QString getDeviceId(); void setDeviceId(QString deviceId); + void clearChannelPhaseData(int channelNo); + bool isSerialOpen(); private: QString deviceId; diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 51673f5..90db100 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -27,10 +27,9 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); - ui->endButt->move(screenRect.width() - 200, 10); - ui->startButt->move(screenRect.width() - 260, 10); + ui->minButt->move(screenRect.width() - 80, 10); + ui->endButt->move(screenRect.width() - 140, 10); + ui->startButt->move(screenRect.width() - 200, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -39,6 +38,16 @@ ui->logo->setText(QString("相位测量数据采集软件——%1").arg(SettingConfig::getInstance().MASTER == 1 ? "主" : "备")); + if (SettingConfig::getInstance().NEED_KAFKA == 1) { + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_CMD_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &PhaseWindow::onPhaseCommandReceived); + } + httpReq = new HttpRequestController(this); // 1. 获取访问接口需要的token int retCode = this->initHttpToken(); @@ -49,44 +58,17 @@ QTimer::singleShot(1000, qApp, SLOT(quit())); } // 2. 获取字典值:设备类型 - retCode = this->initDictDeviceTypes(); // 3. 获取比相仪设备列表 - QJsonObject devListRes = this->initDeviceList(); - if (devListRes.find("code")->toInt() == 200) - { - ui->devSelect->clear(); + initDeviceList(); - // 4. 将获取到的设备添加到下拉列表框中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - QJsonObject devItem = devArray.at(i).toObject(); - ui->devSelect->addItem(devItem.find("deviceName")->toString(), devItem.find("deviceNo")->toString()); + // 4. 打开串口 + for (int i = 0; i < deviceList.size(); i++) { + PhaseDevice * devicePtr = deviceList.at(i); + devicePtr->initSerialPort(); + devicePtr->startWork(); - PhaseDevice * device = new PhaseDevice(this); - deviceList.append(device); - - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceId(devItem.find("deviceId")->toString()); - - connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); - connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); - - device->initSerialPort(); - - QThread::msleep(100); - device->startWork(); - } - // 5. 设置下拉框的样式 - QStandardItemModel * model = qobject_cast(ui->devSelect->model()); - for (int i = 0; i < model->rowCount(); ++i) - { - QStandardItem * item = model->item(i); - item->setSizeHint({ 0, 30 }); - } + QThread::msleep(100); } // 6. 绘制一个设备的多个通道数据面板 @@ -316,15 +298,69 @@ SettingConfig::getInstance().APP_KEY); return response.find("code")->toInt(); } -int PhaseWindow::initDictDeviceTypes() + +void PhaseWindow::initDeviceList() { - QJsonObject response = httpReq->initDictDeviceType(); - return response.find("code")->toInt(); + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.find("code")->toInt() == 200) + { + ui->devSelect->clear(); + + // 4. 将获取到的设备添加到下拉列表框中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) + { + QJsonObject devItem = devArray.at(i).toObject(); + ui->devSelect->addItem(devItem.value("deviceName").toString(), devItem.value("deviceNo").toString()); + + QString deviceId = devItem.value("deviceId").toString(); + + PhaseDevice * device = new PhaseDevice(this); + deviceList.append(device); + + ConstCache::getInstance().deviceMap.insert(deviceId, device); + + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceId(deviceId); + + connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); + connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); + + this->getChannelList(deviceId); + } + // 5. 设置下拉框的样式 + QStandardItemModel * model = qobject_cast(ui->devSelect->model()); + for (int i = 0; i < model->rowCount(); ++i) + { + QStandardItem * item = model->item(i); + item->setSizeHint({ 0, 30 }); + } + } } -QJsonObject PhaseWindow::initDeviceList() + +void PhaseWindow::getChannelList(QString deviceId) { - QJsonObject response = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - return response; + // 查询计数器的通道 + QJsonObject response = httpReq->getPhaseChannelList(deviceId); + if (response.value("code").toInt() == 200) + { + // 将通道绘制在界面上 + QJsonArray channelArray = response.value("data").toArray(); + for (int i = 0; i < channelArray.size(); i++) + { + // 接口返回的channel对象 + QJsonObject channelItem = channelArray.at(i).toObject(); + QString channelId = channelItem.value("id").toString(); // 通道ID + int channelNo = channelItem.value("channelNo").toInt(); + + // 添加到通道的集合中 key=channelId value=deviceId-channelNo + if (channelNo > 0) { + ConstCache::getInstance().channelMap.insert(channelId, QString("%1-%2").arg(deviceId).arg(channelNo)); + } + } + } } void PhaseWindow::on_minButt_clicked() @@ -332,11 +368,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void PhaseWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void PhaseWindow::on_startButt_clicked() { int devSelected = ui->devSelect->currentIndex(); @@ -385,3 +416,46 @@ ((QLabel *)channelBox->children().at(20))->setText(QString("(%1)").arg(0)); // tau=10000 Count } } + +void PhaseWindow::onPhaseCommandReceived(QJsonObject command) +{ + // 缺少必须的字段deviceId 或 cmd 直接返回 + if (command.contains("deviceId") == false || command.contains("cmd") == false) { + return; + } + + QString deviceId = command.value("deviceId").toString(); + QString cmdType = command.value("cmd").toString(); + + // deviceId不属于子系统内的比相仪 直接返回 + if (ConstCache::getInstance().deviceMap.contains(deviceId) == false) { + return; + } + + if (cmdType.toUpper() == "START" || cmdType.toUpper() == "STOP") + { + // 设备开始采集或者停止采集指令 + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (devicePtr != nullptr) { + if (cmdType.toUpper() == "START") { + devicePtr->startWork(); + } else if (cmdType.toUpper() == "STOP") { + devicePtr->stopWork(); + } + } + } else if (cmdType.toUpper() == "CLEAR") + { + // 清除通道的数据 通道级指令 + QString channelId = command.value("channelId").toString(); + if (ConstCache::getInstance().channelMap.contains(channelId) == true) { + QString channelIndex = ConstCache::getInstance().channelMap.value(channelId); + QString channelDevId = channelIndex.split("-").at(0); + int channelNo = ((QString) channelIndex.split("-").at(1)).toInt(); + + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(channelDevId); + if (devicePtr != nullptr) { + devicePtr->clearChannelPhaseData(channelNo); + } + } + } +} diff --git a/PhaseCompAcq/PhaseWindow.h b/PhaseCompAcq/PhaseWindow.h index 572bbde..961e0b1 100644 --- a/PhaseCompAcq/PhaseWindow.h +++ b/PhaseCompAcq/PhaseWindow.h @@ -8,6 +8,7 @@ #include "PhaseDevice.h" #include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaConsumer.h" #include "common/HttpRequestController.h" #include "common/HttpServer.h" @@ -30,21 +31,22 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); - void on_startButt_clicked(); void on_endButt_clicked(); void on_devSelect_currentIndexChanged(int index); + void onPhaseCommandReceived(QJsonObject command); + private: int initHttpToken(); - int initDictDeviceTypes(); - QJsonObject initDeviceList(); + void initDeviceList(); + void getChannelList(QString deviceId); Ui::PhaseWindow *ui; + QKafkaConsumer * kafkaConsumer; HttpRequestController * httpReq; QList deviceList; diff --git a/PhaseCompAcq/PhaseWindow.ui b/PhaseCompAcq/PhaseWindow.ui index d083fb9..9f9456c 100644 --- a/PhaseCompAcq/PhaseWindow.ui +++ b/PhaseCompAcq/PhaseWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/common/ConstCache.h b/PhaseCompAcq/common/ConstCache.h index 6cc831b..0379863 100644 --- a/PhaseCompAcq/common/ConstCache.h +++ b/PhaseCompAcq/common/ConstCache.h @@ -5,6 +5,7 @@ #include #include #include +#include "PhaseDevice.h" class ConstCache : public QObject { @@ -21,6 +22,8 @@ QMap deviceTypes; QList deviceList; + QMap deviceMap; // key=deviceId + QMap channelMap; // key=channelId value=deviceId-no private: ConstCache() {}; diff --git a/PhaseCompAcq/common/HttpRequestController.cpp b/PhaseCompAcq/common/HttpRequestController.cpp index 0a9f3b5..359a0fc 100644 --- a/PhaseCompAcq/common/HttpRequestController.cpp +++ b/PhaseCompAcq/common/HttpRequestController.cpp @@ -1,4 +1,4 @@ -#include "HttpRequestController.h" +#include "HttpRequestController.h" HttpRequestController::HttpRequestController(QObject *parent) : QObject(parent) { @@ -14,6 +14,8 @@ // 获取token的url地址 QUrl url = baseUrl + "/getTokenByClientId"; + qDebug() << url.toString(); + // 请求对象 QNetworkRequest request; request.setUrl(url); @@ -55,105 +57,14 @@ resultObj.insert("code", -1); } - return resultObj; -} - -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} - -QJsonObject HttpRequestController::initDeviceList(QString devType) -{ - QJsonObject resultObj; - - QString counterDevType = ""; - QMapIterator it(ConstCache::getInstance().deviceTypes); - while (it.hasNext()) - { - it.next(); - if (it.value().contains(devType) == true) - { - counterDevType = it.key(); - break; - } - } - - // 获取设备列表的接口地址 - QUrl url = baseUrl + "/device/list"; - QUrlQuery query; - query.addQueryItem("type", counterDevType); - url.setQuery(query); - - QNetworkRequest request; - request.setUrl(url); - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - request.setRawHeader("system", system.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceList.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceList.append(item); - } - } - } else - { - resultObj.insert("code", -1); - } + resultObj.insert("code", 200); // qDebug() << resultObj; return resultObj; } + QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { QJsonObject resultObj; @@ -187,3 +98,36 @@ return resultObj; } + +QJsonObject HttpRequestController::getPhaseChannelList(QString deviceId) +{ + QJsonObject resultObj; + + // 获取设备列表的接口地址 + QUrl url = baseUrl + "/channel/list"; + QUrlQuery query; + query.addQueryItem("deviceId", deviceId); + url.setQuery(query); + + QNetworkRequest request; + request.setUrl(url); + request.setRawHeader("Content-type", "application/json"); + request.setRawHeader("token", token.toLocal8Bit()); + + qDebug() << url; + + QNetworkReply * reply = httpUtil->sendGetRequest(request); + const QByteArray reply_data = reply->readAll(); + QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); + if(jsonDocument.isNull() == false) + { + resultObj = jsonDocument.object(); + } else + { + resultObj.insert("code", -1); + } + + qDebug() << resultObj; + + return resultObj; +} diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index f42eba8..7bce7ac 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -196,7 +196,7 @@ // 绑定显示函数 connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); - // 查询计数器的通道 并初始化串口 + // 查询计数器的通道 this->getChannelList(deviceId); } diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 0a907f1..9ac4497 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -64,45 +64,6 @@ return resultObj; } -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { diff --git a/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index a37122b..c84ad21 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -19,7 +19,6 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 77975b3..de50156 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -21,8 +21,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置stackWidget的大小 @@ -84,11 +83,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void DeviceHubWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void DeviceHubWindow::on_devTypeSelect_currentIndexChanged(int index) { ui->stackedWidget->setCurrentIndex(index); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index 39443a8..3b4c10a 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -50,7 +50,6 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); void on_devTypeSelect_currentIndexChanged(int index); void on_devSelect_currentIndexChanged(int index); diff --git a/DeviceHub/DeviceHubWindow.ui b/DeviceHub/DeviceHubWindow.ui index cc6ccd0..70c0618 100644 --- a/DeviceHub/DeviceHubWindow.ui +++ b/DeviceHub/DeviceHubWindow.ui @@ -77,22 +77,6 @@ - - - - 1030 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 9c613db..d0e8a20 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -51,6 +51,10 @@ { this->devCode = devCode; } +QString PhaseDevice::getDeviceId() +{ + return this->deviceId; +} void PhaseDevice::setDeviceId(QString deviceId) { this->deviceId = deviceId; @@ -61,6 +65,11 @@ return this->serialUtil.isOpen(); } +void PhaseDevice::clearChannelPhaseData(int channelNo) +{ + this->phaseVector[channelNo - 1].clear(); +} + void PhaseDevice::initSerialPort() { int master = SettingConfig::getInstance().MASTER; @@ -87,6 +96,11 @@ this->serialUtil.sendData(startCmd.toLocal8Bit()); QThread::msleep(100); } + + // 开始计算后清除累积的相位数据和准确度计算结果 + for (int i = 0; i < phaseVector.size(); i++) { + phaseVector[i].clear(); + } } void PhaseDevice::stopWork() diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 7531798..06a6b0f 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -27,8 +27,11 @@ void setBaudRate(int baudRate); QString getDevCode(); void setDevCode(QString devCode); + QString getDeviceId(); void setDeviceId(QString deviceId); + void clearChannelPhaseData(int channelNo); + bool isSerialOpen(); private: QString deviceId; diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 51673f5..90db100 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -27,10 +27,9 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); - ui->endButt->move(screenRect.width() - 200, 10); - ui->startButt->move(screenRect.width() - 260, 10); + ui->minButt->move(screenRect.width() - 80, 10); + ui->endButt->move(screenRect.width() - 140, 10); + ui->startButt->move(screenRect.width() - 200, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -39,6 +38,16 @@ ui->logo->setText(QString("相位测量数据采集软件——%1").arg(SettingConfig::getInstance().MASTER == 1 ? "主" : "备")); + if (SettingConfig::getInstance().NEED_KAFKA == 1) { + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_CMD_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &PhaseWindow::onPhaseCommandReceived); + } + httpReq = new HttpRequestController(this); // 1. 获取访问接口需要的token int retCode = this->initHttpToken(); @@ -49,44 +58,17 @@ QTimer::singleShot(1000, qApp, SLOT(quit())); } // 2. 获取字典值:设备类型 - retCode = this->initDictDeviceTypes(); // 3. 获取比相仪设备列表 - QJsonObject devListRes = this->initDeviceList(); - if (devListRes.find("code")->toInt() == 200) - { - ui->devSelect->clear(); + initDeviceList(); - // 4. 将获取到的设备添加到下拉列表框中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - QJsonObject devItem = devArray.at(i).toObject(); - ui->devSelect->addItem(devItem.find("deviceName")->toString(), devItem.find("deviceNo")->toString()); + // 4. 打开串口 + for (int i = 0; i < deviceList.size(); i++) { + PhaseDevice * devicePtr = deviceList.at(i); + devicePtr->initSerialPort(); + devicePtr->startWork(); - PhaseDevice * device = new PhaseDevice(this); - deviceList.append(device); - - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceId(devItem.find("deviceId")->toString()); - - connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); - connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); - - device->initSerialPort(); - - QThread::msleep(100); - device->startWork(); - } - // 5. 设置下拉框的样式 - QStandardItemModel * model = qobject_cast(ui->devSelect->model()); - for (int i = 0; i < model->rowCount(); ++i) - { - QStandardItem * item = model->item(i); - item->setSizeHint({ 0, 30 }); - } + QThread::msleep(100); } // 6. 绘制一个设备的多个通道数据面板 @@ -316,15 +298,69 @@ SettingConfig::getInstance().APP_KEY); return response.find("code")->toInt(); } -int PhaseWindow::initDictDeviceTypes() + +void PhaseWindow::initDeviceList() { - QJsonObject response = httpReq->initDictDeviceType(); - return response.find("code")->toInt(); + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.find("code")->toInt() == 200) + { + ui->devSelect->clear(); + + // 4. 将获取到的设备添加到下拉列表框中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) + { + QJsonObject devItem = devArray.at(i).toObject(); + ui->devSelect->addItem(devItem.value("deviceName").toString(), devItem.value("deviceNo").toString()); + + QString deviceId = devItem.value("deviceId").toString(); + + PhaseDevice * device = new PhaseDevice(this); + deviceList.append(device); + + ConstCache::getInstance().deviceMap.insert(deviceId, device); + + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceId(deviceId); + + connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); + connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); + + this->getChannelList(deviceId); + } + // 5. 设置下拉框的样式 + QStandardItemModel * model = qobject_cast(ui->devSelect->model()); + for (int i = 0; i < model->rowCount(); ++i) + { + QStandardItem * item = model->item(i); + item->setSizeHint({ 0, 30 }); + } + } } -QJsonObject PhaseWindow::initDeviceList() + +void PhaseWindow::getChannelList(QString deviceId) { - QJsonObject response = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - return response; + // 查询计数器的通道 + QJsonObject response = httpReq->getPhaseChannelList(deviceId); + if (response.value("code").toInt() == 200) + { + // 将通道绘制在界面上 + QJsonArray channelArray = response.value("data").toArray(); + for (int i = 0; i < channelArray.size(); i++) + { + // 接口返回的channel对象 + QJsonObject channelItem = channelArray.at(i).toObject(); + QString channelId = channelItem.value("id").toString(); // 通道ID + int channelNo = channelItem.value("channelNo").toInt(); + + // 添加到通道的集合中 key=channelId value=deviceId-channelNo + if (channelNo > 0) { + ConstCache::getInstance().channelMap.insert(channelId, QString("%1-%2").arg(deviceId).arg(channelNo)); + } + } + } } void PhaseWindow::on_minButt_clicked() @@ -332,11 +368,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void PhaseWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void PhaseWindow::on_startButt_clicked() { int devSelected = ui->devSelect->currentIndex(); @@ -385,3 +416,46 @@ ((QLabel *)channelBox->children().at(20))->setText(QString("(%1)").arg(0)); // tau=10000 Count } } + +void PhaseWindow::onPhaseCommandReceived(QJsonObject command) +{ + // 缺少必须的字段deviceId 或 cmd 直接返回 + if (command.contains("deviceId") == false || command.contains("cmd") == false) { + return; + } + + QString deviceId = command.value("deviceId").toString(); + QString cmdType = command.value("cmd").toString(); + + // deviceId不属于子系统内的比相仪 直接返回 + if (ConstCache::getInstance().deviceMap.contains(deviceId) == false) { + return; + } + + if (cmdType.toUpper() == "START" || cmdType.toUpper() == "STOP") + { + // 设备开始采集或者停止采集指令 + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (devicePtr != nullptr) { + if (cmdType.toUpper() == "START") { + devicePtr->startWork(); + } else if (cmdType.toUpper() == "STOP") { + devicePtr->stopWork(); + } + } + } else if (cmdType.toUpper() == "CLEAR") + { + // 清除通道的数据 通道级指令 + QString channelId = command.value("channelId").toString(); + if (ConstCache::getInstance().channelMap.contains(channelId) == true) { + QString channelIndex = ConstCache::getInstance().channelMap.value(channelId); + QString channelDevId = channelIndex.split("-").at(0); + int channelNo = ((QString) channelIndex.split("-").at(1)).toInt(); + + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(channelDevId); + if (devicePtr != nullptr) { + devicePtr->clearChannelPhaseData(channelNo); + } + } + } +} diff --git a/PhaseCompAcq/PhaseWindow.h b/PhaseCompAcq/PhaseWindow.h index 572bbde..961e0b1 100644 --- a/PhaseCompAcq/PhaseWindow.h +++ b/PhaseCompAcq/PhaseWindow.h @@ -8,6 +8,7 @@ #include "PhaseDevice.h" #include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaConsumer.h" #include "common/HttpRequestController.h" #include "common/HttpServer.h" @@ -30,21 +31,22 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); - void on_startButt_clicked(); void on_endButt_clicked(); void on_devSelect_currentIndexChanged(int index); + void onPhaseCommandReceived(QJsonObject command); + private: int initHttpToken(); - int initDictDeviceTypes(); - QJsonObject initDeviceList(); + void initDeviceList(); + void getChannelList(QString deviceId); Ui::PhaseWindow *ui; + QKafkaConsumer * kafkaConsumer; HttpRequestController * httpReq; QList deviceList; diff --git a/PhaseCompAcq/PhaseWindow.ui b/PhaseCompAcq/PhaseWindow.ui index d083fb9..9f9456c 100644 --- a/PhaseCompAcq/PhaseWindow.ui +++ b/PhaseCompAcq/PhaseWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/common/ConstCache.h b/PhaseCompAcq/common/ConstCache.h index 6cc831b..0379863 100644 --- a/PhaseCompAcq/common/ConstCache.h +++ b/PhaseCompAcq/common/ConstCache.h @@ -5,6 +5,7 @@ #include #include #include +#include "PhaseDevice.h" class ConstCache : public QObject { @@ -21,6 +22,8 @@ QMap deviceTypes; QList deviceList; + QMap deviceMap; // key=deviceId + QMap channelMap; // key=channelId value=deviceId-no private: ConstCache() {}; diff --git a/PhaseCompAcq/common/HttpRequestController.cpp b/PhaseCompAcq/common/HttpRequestController.cpp index 0a9f3b5..359a0fc 100644 --- a/PhaseCompAcq/common/HttpRequestController.cpp +++ b/PhaseCompAcq/common/HttpRequestController.cpp @@ -1,4 +1,4 @@ -#include "HttpRequestController.h" +#include "HttpRequestController.h" HttpRequestController::HttpRequestController(QObject *parent) : QObject(parent) { @@ -14,6 +14,8 @@ // 获取token的url地址 QUrl url = baseUrl + "/getTokenByClientId"; + qDebug() << url.toString(); + // 请求对象 QNetworkRequest request; request.setUrl(url); @@ -55,105 +57,14 @@ resultObj.insert("code", -1); } - return resultObj; -} - -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} - -QJsonObject HttpRequestController::initDeviceList(QString devType) -{ - QJsonObject resultObj; - - QString counterDevType = ""; - QMapIterator it(ConstCache::getInstance().deviceTypes); - while (it.hasNext()) - { - it.next(); - if (it.value().contains(devType) == true) - { - counterDevType = it.key(); - break; - } - } - - // 获取设备列表的接口地址 - QUrl url = baseUrl + "/device/list"; - QUrlQuery query; - query.addQueryItem("type", counterDevType); - url.setQuery(query); - - QNetworkRequest request; - request.setUrl(url); - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - request.setRawHeader("system", system.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceList.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceList.append(item); - } - } - } else - { - resultObj.insert("code", -1); - } + resultObj.insert("code", 200); // qDebug() << resultObj; return resultObj; } + QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { QJsonObject resultObj; @@ -187,3 +98,36 @@ return resultObj; } + +QJsonObject HttpRequestController::getPhaseChannelList(QString deviceId) +{ + QJsonObject resultObj; + + // 获取设备列表的接口地址 + QUrl url = baseUrl + "/channel/list"; + QUrlQuery query; + query.addQueryItem("deviceId", deviceId); + url.setQuery(query); + + QNetworkRequest request; + request.setUrl(url); + request.setRawHeader("Content-type", "application/json"); + request.setRawHeader("token", token.toLocal8Bit()); + + qDebug() << url; + + QNetworkReply * reply = httpUtil->sendGetRequest(request); + const QByteArray reply_data = reply->readAll(); + QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); + if(jsonDocument.isNull() == false) + { + resultObj = jsonDocument.object(); + } else + { + resultObj.insert("code", -1); + } + + qDebug() << resultObj; + + return resultObj; +} diff --git a/PhaseCompAcq/common/HttpRequestController.h b/PhaseCompAcq/common/HttpRequestController.h index 2610548..352a769 100644 --- a/PhaseCompAcq/common/HttpRequestController.h +++ b/PhaseCompAcq/common/HttpRequestController.h @@ -1,7 +1,9 @@ -#ifndef HTTPREQUESTCONTROLLER_H +#ifndef HTTPREQUESTCONTROLLER_H #define HTTPREQUESTCONTROLLER_H #include +#include +#include #include #include "utils/HttpRequestUtil.h" @@ -17,9 +19,8 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); - QJsonObject initDeviceList(QString devType); QJsonObject initDeviceList(QString devType, QString system); + QJsonObject getPhaseChannelList(QString deviceId); private: HttpRequestUtil * httpUtil; diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index f42eba8..7bce7ac 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -196,7 +196,7 @@ // 绑定显示函数 connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); - // 查询计数器的通道 并初始化串口 + // 查询计数器的通道 this->getChannelList(deviceId); } diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 0a907f1..9ac4497 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -64,45 +64,6 @@ return resultObj; } -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { diff --git a/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index a37122b..c84ad21 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -19,7 +19,6 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 77975b3..de50156 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -21,8 +21,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置stackWidget的大小 @@ -84,11 +83,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void DeviceHubWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void DeviceHubWindow::on_devTypeSelect_currentIndexChanged(int index) { ui->stackedWidget->setCurrentIndex(index); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index 39443a8..3b4c10a 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -50,7 +50,6 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); void on_devTypeSelect_currentIndexChanged(int index); void on_devSelect_currentIndexChanged(int index); diff --git a/DeviceHub/DeviceHubWindow.ui b/DeviceHub/DeviceHubWindow.ui index cc6ccd0..70c0618 100644 --- a/DeviceHub/DeviceHubWindow.ui +++ b/DeviceHub/DeviceHubWindow.ui @@ -77,22 +77,6 @@ - - - - 1030 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 9c613db..d0e8a20 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -51,6 +51,10 @@ { this->devCode = devCode; } +QString PhaseDevice::getDeviceId() +{ + return this->deviceId; +} void PhaseDevice::setDeviceId(QString deviceId) { this->deviceId = deviceId; @@ -61,6 +65,11 @@ return this->serialUtil.isOpen(); } +void PhaseDevice::clearChannelPhaseData(int channelNo) +{ + this->phaseVector[channelNo - 1].clear(); +} + void PhaseDevice::initSerialPort() { int master = SettingConfig::getInstance().MASTER; @@ -87,6 +96,11 @@ this->serialUtil.sendData(startCmd.toLocal8Bit()); QThread::msleep(100); } + + // 开始计算后清除累积的相位数据和准确度计算结果 + for (int i = 0; i < phaseVector.size(); i++) { + phaseVector[i].clear(); + } } void PhaseDevice::stopWork() diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 7531798..06a6b0f 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -27,8 +27,11 @@ void setBaudRate(int baudRate); QString getDevCode(); void setDevCode(QString devCode); + QString getDeviceId(); void setDeviceId(QString deviceId); + void clearChannelPhaseData(int channelNo); + bool isSerialOpen(); private: QString deviceId; diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 51673f5..90db100 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -27,10 +27,9 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); - ui->endButt->move(screenRect.width() - 200, 10); - ui->startButt->move(screenRect.width() - 260, 10); + ui->minButt->move(screenRect.width() - 80, 10); + ui->endButt->move(screenRect.width() - 140, 10); + ui->startButt->move(screenRect.width() - 200, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -39,6 +38,16 @@ ui->logo->setText(QString("相位测量数据采集软件——%1").arg(SettingConfig::getInstance().MASTER == 1 ? "主" : "备")); + if (SettingConfig::getInstance().NEED_KAFKA == 1) { + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_CMD_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &PhaseWindow::onPhaseCommandReceived); + } + httpReq = new HttpRequestController(this); // 1. 获取访问接口需要的token int retCode = this->initHttpToken(); @@ -49,44 +58,17 @@ QTimer::singleShot(1000, qApp, SLOT(quit())); } // 2. 获取字典值:设备类型 - retCode = this->initDictDeviceTypes(); // 3. 获取比相仪设备列表 - QJsonObject devListRes = this->initDeviceList(); - if (devListRes.find("code")->toInt() == 200) - { - ui->devSelect->clear(); + initDeviceList(); - // 4. 将获取到的设备添加到下拉列表框中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - QJsonObject devItem = devArray.at(i).toObject(); - ui->devSelect->addItem(devItem.find("deviceName")->toString(), devItem.find("deviceNo")->toString()); + // 4. 打开串口 + for (int i = 0; i < deviceList.size(); i++) { + PhaseDevice * devicePtr = deviceList.at(i); + devicePtr->initSerialPort(); + devicePtr->startWork(); - PhaseDevice * device = new PhaseDevice(this); - deviceList.append(device); - - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceId(devItem.find("deviceId")->toString()); - - connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); - connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); - - device->initSerialPort(); - - QThread::msleep(100); - device->startWork(); - } - // 5. 设置下拉框的样式 - QStandardItemModel * model = qobject_cast(ui->devSelect->model()); - for (int i = 0; i < model->rowCount(); ++i) - { - QStandardItem * item = model->item(i); - item->setSizeHint({ 0, 30 }); - } + QThread::msleep(100); } // 6. 绘制一个设备的多个通道数据面板 @@ -316,15 +298,69 @@ SettingConfig::getInstance().APP_KEY); return response.find("code")->toInt(); } -int PhaseWindow::initDictDeviceTypes() + +void PhaseWindow::initDeviceList() { - QJsonObject response = httpReq->initDictDeviceType(); - return response.find("code")->toInt(); + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.find("code")->toInt() == 200) + { + ui->devSelect->clear(); + + // 4. 将获取到的设备添加到下拉列表框中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) + { + QJsonObject devItem = devArray.at(i).toObject(); + ui->devSelect->addItem(devItem.value("deviceName").toString(), devItem.value("deviceNo").toString()); + + QString deviceId = devItem.value("deviceId").toString(); + + PhaseDevice * device = new PhaseDevice(this); + deviceList.append(device); + + ConstCache::getInstance().deviceMap.insert(deviceId, device); + + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceId(deviceId); + + connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); + connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); + + this->getChannelList(deviceId); + } + // 5. 设置下拉框的样式 + QStandardItemModel * model = qobject_cast(ui->devSelect->model()); + for (int i = 0; i < model->rowCount(); ++i) + { + QStandardItem * item = model->item(i); + item->setSizeHint({ 0, 30 }); + } + } } -QJsonObject PhaseWindow::initDeviceList() + +void PhaseWindow::getChannelList(QString deviceId) { - QJsonObject response = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - return response; + // 查询计数器的通道 + QJsonObject response = httpReq->getPhaseChannelList(deviceId); + if (response.value("code").toInt() == 200) + { + // 将通道绘制在界面上 + QJsonArray channelArray = response.value("data").toArray(); + for (int i = 0; i < channelArray.size(); i++) + { + // 接口返回的channel对象 + QJsonObject channelItem = channelArray.at(i).toObject(); + QString channelId = channelItem.value("id").toString(); // 通道ID + int channelNo = channelItem.value("channelNo").toInt(); + + // 添加到通道的集合中 key=channelId value=deviceId-channelNo + if (channelNo > 0) { + ConstCache::getInstance().channelMap.insert(channelId, QString("%1-%2").arg(deviceId).arg(channelNo)); + } + } + } } void PhaseWindow::on_minButt_clicked() @@ -332,11 +368,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void PhaseWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void PhaseWindow::on_startButt_clicked() { int devSelected = ui->devSelect->currentIndex(); @@ -385,3 +416,46 @@ ((QLabel *)channelBox->children().at(20))->setText(QString("(%1)").arg(0)); // tau=10000 Count } } + +void PhaseWindow::onPhaseCommandReceived(QJsonObject command) +{ + // 缺少必须的字段deviceId 或 cmd 直接返回 + if (command.contains("deviceId") == false || command.contains("cmd") == false) { + return; + } + + QString deviceId = command.value("deviceId").toString(); + QString cmdType = command.value("cmd").toString(); + + // deviceId不属于子系统内的比相仪 直接返回 + if (ConstCache::getInstance().deviceMap.contains(deviceId) == false) { + return; + } + + if (cmdType.toUpper() == "START" || cmdType.toUpper() == "STOP") + { + // 设备开始采集或者停止采集指令 + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (devicePtr != nullptr) { + if (cmdType.toUpper() == "START") { + devicePtr->startWork(); + } else if (cmdType.toUpper() == "STOP") { + devicePtr->stopWork(); + } + } + } else if (cmdType.toUpper() == "CLEAR") + { + // 清除通道的数据 通道级指令 + QString channelId = command.value("channelId").toString(); + if (ConstCache::getInstance().channelMap.contains(channelId) == true) { + QString channelIndex = ConstCache::getInstance().channelMap.value(channelId); + QString channelDevId = channelIndex.split("-").at(0); + int channelNo = ((QString) channelIndex.split("-").at(1)).toInt(); + + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(channelDevId); + if (devicePtr != nullptr) { + devicePtr->clearChannelPhaseData(channelNo); + } + } + } +} diff --git a/PhaseCompAcq/PhaseWindow.h b/PhaseCompAcq/PhaseWindow.h index 572bbde..961e0b1 100644 --- a/PhaseCompAcq/PhaseWindow.h +++ b/PhaseCompAcq/PhaseWindow.h @@ -8,6 +8,7 @@ #include "PhaseDevice.h" #include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaConsumer.h" #include "common/HttpRequestController.h" #include "common/HttpServer.h" @@ -30,21 +31,22 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); - void on_startButt_clicked(); void on_endButt_clicked(); void on_devSelect_currentIndexChanged(int index); + void onPhaseCommandReceived(QJsonObject command); + private: int initHttpToken(); - int initDictDeviceTypes(); - QJsonObject initDeviceList(); + void initDeviceList(); + void getChannelList(QString deviceId); Ui::PhaseWindow *ui; + QKafkaConsumer * kafkaConsumer; HttpRequestController * httpReq; QList deviceList; diff --git a/PhaseCompAcq/PhaseWindow.ui b/PhaseCompAcq/PhaseWindow.ui index d083fb9..9f9456c 100644 --- a/PhaseCompAcq/PhaseWindow.ui +++ b/PhaseCompAcq/PhaseWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/common/ConstCache.h b/PhaseCompAcq/common/ConstCache.h index 6cc831b..0379863 100644 --- a/PhaseCompAcq/common/ConstCache.h +++ b/PhaseCompAcq/common/ConstCache.h @@ -5,6 +5,7 @@ #include #include #include +#include "PhaseDevice.h" class ConstCache : public QObject { @@ -21,6 +22,8 @@ QMap deviceTypes; QList deviceList; + QMap deviceMap; // key=deviceId + QMap channelMap; // key=channelId value=deviceId-no private: ConstCache() {}; diff --git a/PhaseCompAcq/common/HttpRequestController.cpp b/PhaseCompAcq/common/HttpRequestController.cpp index 0a9f3b5..359a0fc 100644 --- a/PhaseCompAcq/common/HttpRequestController.cpp +++ b/PhaseCompAcq/common/HttpRequestController.cpp @@ -1,4 +1,4 @@ -#include "HttpRequestController.h" +#include "HttpRequestController.h" HttpRequestController::HttpRequestController(QObject *parent) : QObject(parent) { @@ -14,6 +14,8 @@ // 获取token的url地址 QUrl url = baseUrl + "/getTokenByClientId"; + qDebug() << url.toString(); + // 请求对象 QNetworkRequest request; request.setUrl(url); @@ -55,105 +57,14 @@ resultObj.insert("code", -1); } - return resultObj; -} - -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} - -QJsonObject HttpRequestController::initDeviceList(QString devType) -{ - QJsonObject resultObj; - - QString counterDevType = ""; - QMapIterator it(ConstCache::getInstance().deviceTypes); - while (it.hasNext()) - { - it.next(); - if (it.value().contains(devType) == true) - { - counterDevType = it.key(); - break; - } - } - - // 获取设备列表的接口地址 - QUrl url = baseUrl + "/device/list"; - QUrlQuery query; - query.addQueryItem("type", counterDevType); - url.setQuery(query); - - QNetworkRequest request; - request.setUrl(url); - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - request.setRawHeader("system", system.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceList.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceList.append(item); - } - } - } else - { - resultObj.insert("code", -1); - } + resultObj.insert("code", 200); // qDebug() << resultObj; return resultObj; } + QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { QJsonObject resultObj; @@ -187,3 +98,36 @@ return resultObj; } + +QJsonObject HttpRequestController::getPhaseChannelList(QString deviceId) +{ + QJsonObject resultObj; + + // 获取设备列表的接口地址 + QUrl url = baseUrl + "/channel/list"; + QUrlQuery query; + query.addQueryItem("deviceId", deviceId); + url.setQuery(query); + + QNetworkRequest request; + request.setUrl(url); + request.setRawHeader("Content-type", "application/json"); + request.setRawHeader("token", token.toLocal8Bit()); + + qDebug() << url; + + QNetworkReply * reply = httpUtil->sendGetRequest(request); + const QByteArray reply_data = reply->readAll(); + QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); + if(jsonDocument.isNull() == false) + { + resultObj = jsonDocument.object(); + } else + { + resultObj.insert("code", -1); + } + + qDebug() << resultObj; + + return resultObj; +} diff --git a/PhaseCompAcq/common/HttpRequestController.h b/PhaseCompAcq/common/HttpRequestController.h index 2610548..352a769 100644 --- a/PhaseCompAcq/common/HttpRequestController.h +++ b/PhaseCompAcq/common/HttpRequestController.h @@ -1,7 +1,9 @@ -#ifndef HTTPREQUESTCONTROLLER_H +#ifndef HTTPREQUESTCONTROLLER_H #define HTTPREQUESTCONTROLLER_H #include +#include +#include #include #include "utils/HttpRequestUtil.h" @@ -17,9 +19,8 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); - QJsonObject initDeviceList(QString devType); QJsonObject initDeviceList(QString devType, QString system); + QJsonObject getPhaseChannelList(QString deviceId); private: HttpRequestUtil * httpUtil; diff --git a/PhaseCompAcq/common/common.pri b/PhaseCompAcq/common/common.pri index b80a640..01ba6ee 100644 --- a/PhaseCompAcq/common/common.pri +++ b/PhaseCompAcq/common/common.pri @@ -4,6 +4,7 @@ SOURCES += $$PWD/utils/QSerialPortUtil.cpp SOURCES += $$PWD/utils/QLogUtil.cpp SOURCES += $$PWD/utils/QKafkaUtil.cpp +SOURCES += $$PWD/utils/QKafkaConsumer.cpp SOURCES += $$PWD/utils/HttpRequestUtil.cpp SOURCES += $$PWD/utils/MD5.cpp SOURCES += $$PWD/HttpRequestController.cpp @@ -14,6 +15,7 @@ HEADERS += $$PWD/utils/QSerialPortUtil.h HEADERS += $$PWD/utils/QLogUtil.h HEADERS += $$PWD/utils/QKafkaUtil.h +HEADERS += $$PWD/utils/QKafkaConsumer.h HEADERS += $$PWD/utils/HttpRequestUtil.h HEADERS += $$PWD/utils/DefHead.h HEADERS += $$PWD/utils/MD5.h diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index f42eba8..7bce7ac 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -196,7 +196,7 @@ // 绑定显示函数 connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); - // 查询计数器的通道 并初始化串口 + // 查询计数器的通道 this->getChannelList(deviceId); } diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 0a907f1..9ac4497 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -64,45 +64,6 @@ return resultObj; } -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { diff --git a/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index a37122b..c84ad21 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -19,7 +19,6 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 77975b3..de50156 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -21,8 +21,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置stackWidget的大小 @@ -84,11 +83,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void DeviceHubWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void DeviceHubWindow::on_devTypeSelect_currentIndexChanged(int index) { ui->stackedWidget->setCurrentIndex(index); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index 39443a8..3b4c10a 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -50,7 +50,6 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); void on_devTypeSelect_currentIndexChanged(int index); void on_devSelect_currentIndexChanged(int index); diff --git a/DeviceHub/DeviceHubWindow.ui b/DeviceHub/DeviceHubWindow.ui index cc6ccd0..70c0618 100644 --- a/DeviceHub/DeviceHubWindow.ui +++ b/DeviceHub/DeviceHubWindow.ui @@ -77,22 +77,6 @@ - - - - 1030 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 9c613db..d0e8a20 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -51,6 +51,10 @@ { this->devCode = devCode; } +QString PhaseDevice::getDeviceId() +{ + return this->deviceId; +} void PhaseDevice::setDeviceId(QString deviceId) { this->deviceId = deviceId; @@ -61,6 +65,11 @@ return this->serialUtil.isOpen(); } +void PhaseDevice::clearChannelPhaseData(int channelNo) +{ + this->phaseVector[channelNo - 1].clear(); +} + void PhaseDevice::initSerialPort() { int master = SettingConfig::getInstance().MASTER; @@ -87,6 +96,11 @@ this->serialUtil.sendData(startCmd.toLocal8Bit()); QThread::msleep(100); } + + // 开始计算后清除累积的相位数据和准确度计算结果 + for (int i = 0; i < phaseVector.size(); i++) { + phaseVector[i].clear(); + } } void PhaseDevice::stopWork() diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 7531798..06a6b0f 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -27,8 +27,11 @@ void setBaudRate(int baudRate); QString getDevCode(); void setDevCode(QString devCode); + QString getDeviceId(); void setDeviceId(QString deviceId); + void clearChannelPhaseData(int channelNo); + bool isSerialOpen(); private: QString deviceId; diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 51673f5..90db100 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -27,10 +27,9 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); - ui->endButt->move(screenRect.width() - 200, 10); - ui->startButt->move(screenRect.width() - 260, 10); + ui->minButt->move(screenRect.width() - 80, 10); + ui->endButt->move(screenRect.width() - 140, 10); + ui->startButt->move(screenRect.width() - 200, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -39,6 +38,16 @@ ui->logo->setText(QString("相位测量数据采集软件——%1").arg(SettingConfig::getInstance().MASTER == 1 ? "主" : "备")); + if (SettingConfig::getInstance().NEED_KAFKA == 1) { + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_CMD_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &PhaseWindow::onPhaseCommandReceived); + } + httpReq = new HttpRequestController(this); // 1. 获取访问接口需要的token int retCode = this->initHttpToken(); @@ -49,44 +58,17 @@ QTimer::singleShot(1000, qApp, SLOT(quit())); } // 2. 获取字典值:设备类型 - retCode = this->initDictDeviceTypes(); // 3. 获取比相仪设备列表 - QJsonObject devListRes = this->initDeviceList(); - if (devListRes.find("code")->toInt() == 200) - { - ui->devSelect->clear(); + initDeviceList(); - // 4. 将获取到的设备添加到下拉列表框中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - QJsonObject devItem = devArray.at(i).toObject(); - ui->devSelect->addItem(devItem.find("deviceName")->toString(), devItem.find("deviceNo")->toString()); + // 4. 打开串口 + for (int i = 0; i < deviceList.size(); i++) { + PhaseDevice * devicePtr = deviceList.at(i); + devicePtr->initSerialPort(); + devicePtr->startWork(); - PhaseDevice * device = new PhaseDevice(this); - deviceList.append(device); - - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceId(devItem.find("deviceId")->toString()); - - connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); - connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); - - device->initSerialPort(); - - QThread::msleep(100); - device->startWork(); - } - // 5. 设置下拉框的样式 - QStandardItemModel * model = qobject_cast(ui->devSelect->model()); - for (int i = 0; i < model->rowCount(); ++i) - { - QStandardItem * item = model->item(i); - item->setSizeHint({ 0, 30 }); - } + QThread::msleep(100); } // 6. 绘制一个设备的多个通道数据面板 @@ -316,15 +298,69 @@ SettingConfig::getInstance().APP_KEY); return response.find("code")->toInt(); } -int PhaseWindow::initDictDeviceTypes() + +void PhaseWindow::initDeviceList() { - QJsonObject response = httpReq->initDictDeviceType(); - return response.find("code")->toInt(); + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.find("code")->toInt() == 200) + { + ui->devSelect->clear(); + + // 4. 将获取到的设备添加到下拉列表框中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) + { + QJsonObject devItem = devArray.at(i).toObject(); + ui->devSelect->addItem(devItem.value("deviceName").toString(), devItem.value("deviceNo").toString()); + + QString deviceId = devItem.value("deviceId").toString(); + + PhaseDevice * device = new PhaseDevice(this); + deviceList.append(device); + + ConstCache::getInstance().deviceMap.insert(deviceId, device); + + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceId(deviceId); + + connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); + connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); + + this->getChannelList(deviceId); + } + // 5. 设置下拉框的样式 + QStandardItemModel * model = qobject_cast(ui->devSelect->model()); + for (int i = 0; i < model->rowCount(); ++i) + { + QStandardItem * item = model->item(i); + item->setSizeHint({ 0, 30 }); + } + } } -QJsonObject PhaseWindow::initDeviceList() + +void PhaseWindow::getChannelList(QString deviceId) { - QJsonObject response = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - return response; + // 查询计数器的通道 + QJsonObject response = httpReq->getPhaseChannelList(deviceId); + if (response.value("code").toInt() == 200) + { + // 将通道绘制在界面上 + QJsonArray channelArray = response.value("data").toArray(); + for (int i = 0; i < channelArray.size(); i++) + { + // 接口返回的channel对象 + QJsonObject channelItem = channelArray.at(i).toObject(); + QString channelId = channelItem.value("id").toString(); // 通道ID + int channelNo = channelItem.value("channelNo").toInt(); + + // 添加到通道的集合中 key=channelId value=deviceId-channelNo + if (channelNo > 0) { + ConstCache::getInstance().channelMap.insert(channelId, QString("%1-%2").arg(deviceId).arg(channelNo)); + } + } + } } void PhaseWindow::on_minButt_clicked() @@ -332,11 +368,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void PhaseWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void PhaseWindow::on_startButt_clicked() { int devSelected = ui->devSelect->currentIndex(); @@ -385,3 +416,46 @@ ((QLabel *)channelBox->children().at(20))->setText(QString("(%1)").arg(0)); // tau=10000 Count } } + +void PhaseWindow::onPhaseCommandReceived(QJsonObject command) +{ + // 缺少必须的字段deviceId 或 cmd 直接返回 + if (command.contains("deviceId") == false || command.contains("cmd") == false) { + return; + } + + QString deviceId = command.value("deviceId").toString(); + QString cmdType = command.value("cmd").toString(); + + // deviceId不属于子系统内的比相仪 直接返回 + if (ConstCache::getInstance().deviceMap.contains(deviceId) == false) { + return; + } + + if (cmdType.toUpper() == "START" || cmdType.toUpper() == "STOP") + { + // 设备开始采集或者停止采集指令 + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (devicePtr != nullptr) { + if (cmdType.toUpper() == "START") { + devicePtr->startWork(); + } else if (cmdType.toUpper() == "STOP") { + devicePtr->stopWork(); + } + } + } else if (cmdType.toUpper() == "CLEAR") + { + // 清除通道的数据 通道级指令 + QString channelId = command.value("channelId").toString(); + if (ConstCache::getInstance().channelMap.contains(channelId) == true) { + QString channelIndex = ConstCache::getInstance().channelMap.value(channelId); + QString channelDevId = channelIndex.split("-").at(0); + int channelNo = ((QString) channelIndex.split("-").at(1)).toInt(); + + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(channelDevId); + if (devicePtr != nullptr) { + devicePtr->clearChannelPhaseData(channelNo); + } + } + } +} diff --git a/PhaseCompAcq/PhaseWindow.h b/PhaseCompAcq/PhaseWindow.h index 572bbde..961e0b1 100644 --- a/PhaseCompAcq/PhaseWindow.h +++ b/PhaseCompAcq/PhaseWindow.h @@ -8,6 +8,7 @@ #include "PhaseDevice.h" #include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaConsumer.h" #include "common/HttpRequestController.h" #include "common/HttpServer.h" @@ -30,21 +31,22 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); - void on_startButt_clicked(); void on_endButt_clicked(); void on_devSelect_currentIndexChanged(int index); + void onPhaseCommandReceived(QJsonObject command); + private: int initHttpToken(); - int initDictDeviceTypes(); - QJsonObject initDeviceList(); + void initDeviceList(); + void getChannelList(QString deviceId); Ui::PhaseWindow *ui; + QKafkaConsumer * kafkaConsumer; HttpRequestController * httpReq; QList deviceList; diff --git a/PhaseCompAcq/PhaseWindow.ui b/PhaseCompAcq/PhaseWindow.ui index d083fb9..9f9456c 100644 --- a/PhaseCompAcq/PhaseWindow.ui +++ b/PhaseCompAcq/PhaseWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/common/ConstCache.h b/PhaseCompAcq/common/ConstCache.h index 6cc831b..0379863 100644 --- a/PhaseCompAcq/common/ConstCache.h +++ b/PhaseCompAcq/common/ConstCache.h @@ -5,6 +5,7 @@ #include #include #include +#include "PhaseDevice.h" class ConstCache : public QObject { @@ -21,6 +22,8 @@ QMap deviceTypes; QList deviceList; + QMap deviceMap; // key=deviceId + QMap channelMap; // key=channelId value=deviceId-no private: ConstCache() {}; diff --git a/PhaseCompAcq/common/HttpRequestController.cpp b/PhaseCompAcq/common/HttpRequestController.cpp index 0a9f3b5..359a0fc 100644 --- a/PhaseCompAcq/common/HttpRequestController.cpp +++ b/PhaseCompAcq/common/HttpRequestController.cpp @@ -1,4 +1,4 @@ -#include "HttpRequestController.h" +#include "HttpRequestController.h" HttpRequestController::HttpRequestController(QObject *parent) : QObject(parent) { @@ -14,6 +14,8 @@ // 获取token的url地址 QUrl url = baseUrl + "/getTokenByClientId"; + qDebug() << url.toString(); + // 请求对象 QNetworkRequest request; request.setUrl(url); @@ -55,105 +57,14 @@ resultObj.insert("code", -1); } - return resultObj; -} - -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} - -QJsonObject HttpRequestController::initDeviceList(QString devType) -{ - QJsonObject resultObj; - - QString counterDevType = ""; - QMapIterator it(ConstCache::getInstance().deviceTypes); - while (it.hasNext()) - { - it.next(); - if (it.value().contains(devType) == true) - { - counterDevType = it.key(); - break; - } - } - - // 获取设备列表的接口地址 - QUrl url = baseUrl + "/device/list"; - QUrlQuery query; - query.addQueryItem("type", counterDevType); - url.setQuery(query); - - QNetworkRequest request; - request.setUrl(url); - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - request.setRawHeader("system", system.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceList.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceList.append(item); - } - } - } else - { - resultObj.insert("code", -1); - } + resultObj.insert("code", 200); // qDebug() << resultObj; return resultObj; } + QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { QJsonObject resultObj; @@ -187,3 +98,36 @@ return resultObj; } + +QJsonObject HttpRequestController::getPhaseChannelList(QString deviceId) +{ + QJsonObject resultObj; + + // 获取设备列表的接口地址 + QUrl url = baseUrl + "/channel/list"; + QUrlQuery query; + query.addQueryItem("deviceId", deviceId); + url.setQuery(query); + + QNetworkRequest request; + request.setUrl(url); + request.setRawHeader("Content-type", "application/json"); + request.setRawHeader("token", token.toLocal8Bit()); + + qDebug() << url; + + QNetworkReply * reply = httpUtil->sendGetRequest(request); + const QByteArray reply_data = reply->readAll(); + QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); + if(jsonDocument.isNull() == false) + { + resultObj = jsonDocument.object(); + } else + { + resultObj.insert("code", -1); + } + + qDebug() << resultObj; + + return resultObj; +} diff --git a/PhaseCompAcq/common/HttpRequestController.h b/PhaseCompAcq/common/HttpRequestController.h index 2610548..352a769 100644 --- a/PhaseCompAcq/common/HttpRequestController.h +++ b/PhaseCompAcq/common/HttpRequestController.h @@ -1,7 +1,9 @@ -#ifndef HTTPREQUESTCONTROLLER_H +#ifndef HTTPREQUESTCONTROLLER_H #define HTTPREQUESTCONTROLLER_H #include +#include +#include #include #include "utils/HttpRequestUtil.h" @@ -17,9 +19,8 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); - QJsonObject initDeviceList(QString devType); QJsonObject initDeviceList(QString devType, QString system); + QJsonObject getPhaseChannelList(QString deviceId); private: HttpRequestUtil * httpUtil; diff --git a/PhaseCompAcq/common/common.pri b/PhaseCompAcq/common/common.pri index b80a640..01ba6ee 100644 --- a/PhaseCompAcq/common/common.pri +++ b/PhaseCompAcq/common/common.pri @@ -4,6 +4,7 @@ SOURCES += $$PWD/utils/QSerialPortUtil.cpp SOURCES += $$PWD/utils/QLogUtil.cpp SOURCES += $$PWD/utils/QKafkaUtil.cpp +SOURCES += $$PWD/utils/QKafkaConsumer.cpp SOURCES += $$PWD/utils/HttpRequestUtil.cpp SOURCES += $$PWD/utils/MD5.cpp SOURCES += $$PWD/HttpRequestController.cpp @@ -14,6 +15,7 @@ HEADERS += $$PWD/utils/QSerialPortUtil.h HEADERS += $$PWD/utils/QLogUtil.h HEADERS += $$PWD/utils/QKafkaUtil.h +HEADERS += $$PWD/utils/QKafkaConsumer.h HEADERS += $$PWD/utils/HttpRequestUtil.h HEADERS += $$PWD/utils/DefHead.h HEADERS += $$PWD/utils/MD5.h diff --git a/PhaseCompAcq/common/utils/QKafkaConsumer.cpp b/PhaseCompAcq/common/utils/QKafkaConsumer.cpp new file mode 100644 index 0000000..cb4fe37 --- /dev/null +++ b/PhaseCompAcq/common/utils/QKafkaConsumer.cpp @@ -0,0 +1,138 @@ +#include "QKafkaConsumer.h" +#include "SettingConfig.h" +#include +#include + +static volatile int runFlag = 1; +static bool exit_eof = false; + +QKafkaConsumer::QKafkaConsumer(QObject *parent) : QThread(parent) +{ + this->conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL); + this->tconf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC); + + conf->set("enable.partition.eof", "true", errStr); +} + +void QKafkaConsumer::setBrokers(QString brokers) +{ + this->brokers = brokers; +} +void QKafkaConsumer::setTopic(QString topic) +{ + this->topic = topic; +} + +int QKafkaConsumer::createConsumer() +{ + int ret = conf->set("metadata.broker.list", brokers.toStdString(), errStr); + if (SettingConfig::getInstance().NEED_SASL == 1) + { + conf->set("sasl.username", SettingConfig::getInstance().SASL_USERNAME.toStdString(), errStr); + conf->set("sasl.password", SettingConfig::getInstance().SASL_PASSWORD.toStdString(), errStr); + conf->set("security.protocol", "sasl_plaintext", errStr); + conf->set("sasl.mechanisms", "PLAIN", errStr); + } + + if (ret != RdKafka::Conf::CONF_OK) + { + std::cerr << "RdKafka conf set brokerlist failed :" << errStr.c_str() << std::endl; + } + + consumer = RdKafka::Consumer::create(conf, errStr); + if (!consumer) { + std::cerr << "Failed to create consumer: " << errStr << std::endl; + return -1; + } + + std::cout << "% Created consumer " << consumer->name() << std::endl; + + return 1; +} + +void QKafkaConsumer::run() +{ + RdKafka::Topic * topic = RdKafka::Topic::create(consumer, this->topic.toStdString(), tconf, errStr); + if (!topic) { + std::cerr << "Failed to create topic: " << errStr << std::endl; + } + + RdKafka::ErrorCode resp = consumer->start(topic, 0, RdKafka::Topic::OFFSET_END); + if (resp != RdKafka::ERR_NO_ERROR) { + std::cerr << "Failed to start consumer: " << RdKafka::err2str(resp) << std::endl; + } + + while (runFlag) + { + RdKafka::Message * message = consumer->consume(topic, 0, 200); + messageConsume(message); + } +} + +void QKafkaConsumer::exitThread() +{ + runFlag = false; +} + +void QKafkaConsumer::messageConsume(RdKafka::Message* message) { + const RdKafka::Headers *headers; + + switch (message->err()) { + case RdKafka::ERR__TIMED_OUT: + break; + + case RdKafka::ERR_NO_ERROR: + { + /* Real message */ +// std::cout << "Read msg at offset " << message->offset() << std::endl; +// printf("%.*s\n", static_cast(message->len()), static_cast(message->payload())); + + QString messageStr = static_cast(message->payload()); + QJsonParseError jsonErr; + QJsonDocument doc = QJsonDocument::fromJson(messageStr.toUtf8(), &jsonErr); + if (jsonErr.error == QJsonParseError::NoError) + { + QJsonObject obj = doc.object(); + obj.insert("cmdStr", messageStr); + emit messageRecieved(obj); + } + + if (message->key()) { + std::cout << "Key: " << *message->key() << std::endl; + } + headers = message->headers(); + if (headers) { + std::vector hdrs = headers->get_all(); + for (size_t i = 0 ; i < hdrs.size() ; i++) { + const RdKafka::Headers::Header hdr = hdrs[i]; + + if (hdr.value() != NULL) + printf(" Header: %s = \"%.*s\"\n", + hdr.key().c_str(), + (int)hdr.value_size(), (const char *)hdr.value()); + else + printf(" Header: %s = NULL\n", hdr.key().c_str()); + } + } + break; + } + + case RdKafka::ERR__PARTITION_EOF: + /* Last message */ + if (exit_eof) { + runFlag = 0; + } + break; + + case RdKafka::ERR__UNKNOWN_TOPIC: + case RdKafka::ERR__UNKNOWN_PARTITION: + std::cerr << "Consume failed: " << message->errstr() << std::endl; + runFlag = 0; + break; + + default: + /* Errors */ + std::cerr << "Consume failed: " << message->errstr() << std::endl; + runFlag = 0; + } +} diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index f42eba8..7bce7ac 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -196,7 +196,7 @@ // 绑定显示函数 connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); - // 查询计数器的通道 并初始化串口 + // 查询计数器的通道 this->getChannelList(deviceId); } diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 0a907f1..9ac4497 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -64,45 +64,6 @@ return resultObj; } -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { diff --git a/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index a37122b..c84ad21 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -19,7 +19,6 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 77975b3..de50156 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -21,8 +21,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置stackWidget的大小 @@ -84,11 +83,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void DeviceHubWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void DeviceHubWindow::on_devTypeSelect_currentIndexChanged(int index) { ui->stackedWidget->setCurrentIndex(index); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index 39443a8..3b4c10a 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -50,7 +50,6 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); void on_devTypeSelect_currentIndexChanged(int index); void on_devSelect_currentIndexChanged(int index); diff --git a/DeviceHub/DeviceHubWindow.ui b/DeviceHub/DeviceHubWindow.ui index cc6ccd0..70c0618 100644 --- a/DeviceHub/DeviceHubWindow.ui +++ b/DeviceHub/DeviceHubWindow.ui @@ -77,22 +77,6 @@ - - - - 1030 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 9c613db..d0e8a20 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -51,6 +51,10 @@ { this->devCode = devCode; } +QString PhaseDevice::getDeviceId() +{ + return this->deviceId; +} void PhaseDevice::setDeviceId(QString deviceId) { this->deviceId = deviceId; @@ -61,6 +65,11 @@ return this->serialUtil.isOpen(); } +void PhaseDevice::clearChannelPhaseData(int channelNo) +{ + this->phaseVector[channelNo - 1].clear(); +} + void PhaseDevice::initSerialPort() { int master = SettingConfig::getInstance().MASTER; @@ -87,6 +96,11 @@ this->serialUtil.sendData(startCmd.toLocal8Bit()); QThread::msleep(100); } + + // 开始计算后清除累积的相位数据和准确度计算结果 + for (int i = 0; i < phaseVector.size(); i++) { + phaseVector[i].clear(); + } } void PhaseDevice::stopWork() diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 7531798..06a6b0f 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -27,8 +27,11 @@ void setBaudRate(int baudRate); QString getDevCode(); void setDevCode(QString devCode); + QString getDeviceId(); void setDeviceId(QString deviceId); + void clearChannelPhaseData(int channelNo); + bool isSerialOpen(); private: QString deviceId; diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 51673f5..90db100 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -27,10 +27,9 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); - ui->endButt->move(screenRect.width() - 200, 10); - ui->startButt->move(screenRect.width() - 260, 10); + ui->minButt->move(screenRect.width() - 80, 10); + ui->endButt->move(screenRect.width() - 140, 10); + ui->startButt->move(screenRect.width() - 200, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -39,6 +38,16 @@ ui->logo->setText(QString("相位测量数据采集软件——%1").arg(SettingConfig::getInstance().MASTER == 1 ? "主" : "备")); + if (SettingConfig::getInstance().NEED_KAFKA == 1) { + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_CMD_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &PhaseWindow::onPhaseCommandReceived); + } + httpReq = new HttpRequestController(this); // 1. 获取访问接口需要的token int retCode = this->initHttpToken(); @@ -49,44 +58,17 @@ QTimer::singleShot(1000, qApp, SLOT(quit())); } // 2. 获取字典值:设备类型 - retCode = this->initDictDeviceTypes(); // 3. 获取比相仪设备列表 - QJsonObject devListRes = this->initDeviceList(); - if (devListRes.find("code")->toInt() == 200) - { - ui->devSelect->clear(); + initDeviceList(); - // 4. 将获取到的设备添加到下拉列表框中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - QJsonObject devItem = devArray.at(i).toObject(); - ui->devSelect->addItem(devItem.find("deviceName")->toString(), devItem.find("deviceNo")->toString()); + // 4. 打开串口 + for (int i = 0; i < deviceList.size(); i++) { + PhaseDevice * devicePtr = deviceList.at(i); + devicePtr->initSerialPort(); + devicePtr->startWork(); - PhaseDevice * device = new PhaseDevice(this); - deviceList.append(device); - - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceId(devItem.find("deviceId")->toString()); - - connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); - connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); - - device->initSerialPort(); - - QThread::msleep(100); - device->startWork(); - } - // 5. 设置下拉框的样式 - QStandardItemModel * model = qobject_cast(ui->devSelect->model()); - for (int i = 0; i < model->rowCount(); ++i) - { - QStandardItem * item = model->item(i); - item->setSizeHint({ 0, 30 }); - } + QThread::msleep(100); } // 6. 绘制一个设备的多个通道数据面板 @@ -316,15 +298,69 @@ SettingConfig::getInstance().APP_KEY); return response.find("code")->toInt(); } -int PhaseWindow::initDictDeviceTypes() + +void PhaseWindow::initDeviceList() { - QJsonObject response = httpReq->initDictDeviceType(); - return response.find("code")->toInt(); + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.find("code")->toInt() == 200) + { + ui->devSelect->clear(); + + // 4. 将获取到的设备添加到下拉列表框中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) + { + QJsonObject devItem = devArray.at(i).toObject(); + ui->devSelect->addItem(devItem.value("deviceName").toString(), devItem.value("deviceNo").toString()); + + QString deviceId = devItem.value("deviceId").toString(); + + PhaseDevice * device = new PhaseDevice(this); + deviceList.append(device); + + ConstCache::getInstance().deviceMap.insert(deviceId, device); + + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceId(deviceId); + + connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); + connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); + + this->getChannelList(deviceId); + } + // 5. 设置下拉框的样式 + QStandardItemModel * model = qobject_cast(ui->devSelect->model()); + for (int i = 0; i < model->rowCount(); ++i) + { + QStandardItem * item = model->item(i); + item->setSizeHint({ 0, 30 }); + } + } } -QJsonObject PhaseWindow::initDeviceList() + +void PhaseWindow::getChannelList(QString deviceId) { - QJsonObject response = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - return response; + // 查询计数器的通道 + QJsonObject response = httpReq->getPhaseChannelList(deviceId); + if (response.value("code").toInt() == 200) + { + // 将通道绘制在界面上 + QJsonArray channelArray = response.value("data").toArray(); + for (int i = 0; i < channelArray.size(); i++) + { + // 接口返回的channel对象 + QJsonObject channelItem = channelArray.at(i).toObject(); + QString channelId = channelItem.value("id").toString(); // 通道ID + int channelNo = channelItem.value("channelNo").toInt(); + + // 添加到通道的集合中 key=channelId value=deviceId-channelNo + if (channelNo > 0) { + ConstCache::getInstance().channelMap.insert(channelId, QString("%1-%2").arg(deviceId).arg(channelNo)); + } + } + } } void PhaseWindow::on_minButt_clicked() @@ -332,11 +368,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void PhaseWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void PhaseWindow::on_startButt_clicked() { int devSelected = ui->devSelect->currentIndex(); @@ -385,3 +416,46 @@ ((QLabel *)channelBox->children().at(20))->setText(QString("(%1)").arg(0)); // tau=10000 Count } } + +void PhaseWindow::onPhaseCommandReceived(QJsonObject command) +{ + // 缺少必须的字段deviceId 或 cmd 直接返回 + if (command.contains("deviceId") == false || command.contains("cmd") == false) { + return; + } + + QString deviceId = command.value("deviceId").toString(); + QString cmdType = command.value("cmd").toString(); + + // deviceId不属于子系统内的比相仪 直接返回 + if (ConstCache::getInstance().deviceMap.contains(deviceId) == false) { + return; + } + + if (cmdType.toUpper() == "START" || cmdType.toUpper() == "STOP") + { + // 设备开始采集或者停止采集指令 + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (devicePtr != nullptr) { + if (cmdType.toUpper() == "START") { + devicePtr->startWork(); + } else if (cmdType.toUpper() == "STOP") { + devicePtr->stopWork(); + } + } + } else if (cmdType.toUpper() == "CLEAR") + { + // 清除通道的数据 通道级指令 + QString channelId = command.value("channelId").toString(); + if (ConstCache::getInstance().channelMap.contains(channelId) == true) { + QString channelIndex = ConstCache::getInstance().channelMap.value(channelId); + QString channelDevId = channelIndex.split("-").at(0); + int channelNo = ((QString) channelIndex.split("-").at(1)).toInt(); + + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(channelDevId); + if (devicePtr != nullptr) { + devicePtr->clearChannelPhaseData(channelNo); + } + } + } +} diff --git a/PhaseCompAcq/PhaseWindow.h b/PhaseCompAcq/PhaseWindow.h index 572bbde..961e0b1 100644 --- a/PhaseCompAcq/PhaseWindow.h +++ b/PhaseCompAcq/PhaseWindow.h @@ -8,6 +8,7 @@ #include "PhaseDevice.h" #include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaConsumer.h" #include "common/HttpRequestController.h" #include "common/HttpServer.h" @@ -30,21 +31,22 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); - void on_startButt_clicked(); void on_endButt_clicked(); void on_devSelect_currentIndexChanged(int index); + void onPhaseCommandReceived(QJsonObject command); + private: int initHttpToken(); - int initDictDeviceTypes(); - QJsonObject initDeviceList(); + void initDeviceList(); + void getChannelList(QString deviceId); Ui::PhaseWindow *ui; + QKafkaConsumer * kafkaConsumer; HttpRequestController * httpReq; QList deviceList; diff --git a/PhaseCompAcq/PhaseWindow.ui b/PhaseCompAcq/PhaseWindow.ui index d083fb9..9f9456c 100644 --- a/PhaseCompAcq/PhaseWindow.ui +++ b/PhaseCompAcq/PhaseWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/common/ConstCache.h b/PhaseCompAcq/common/ConstCache.h index 6cc831b..0379863 100644 --- a/PhaseCompAcq/common/ConstCache.h +++ b/PhaseCompAcq/common/ConstCache.h @@ -5,6 +5,7 @@ #include #include #include +#include "PhaseDevice.h" class ConstCache : public QObject { @@ -21,6 +22,8 @@ QMap deviceTypes; QList deviceList; + QMap deviceMap; // key=deviceId + QMap channelMap; // key=channelId value=deviceId-no private: ConstCache() {}; diff --git a/PhaseCompAcq/common/HttpRequestController.cpp b/PhaseCompAcq/common/HttpRequestController.cpp index 0a9f3b5..359a0fc 100644 --- a/PhaseCompAcq/common/HttpRequestController.cpp +++ b/PhaseCompAcq/common/HttpRequestController.cpp @@ -1,4 +1,4 @@ -#include "HttpRequestController.h" +#include "HttpRequestController.h" HttpRequestController::HttpRequestController(QObject *parent) : QObject(parent) { @@ -14,6 +14,8 @@ // 获取token的url地址 QUrl url = baseUrl + "/getTokenByClientId"; + qDebug() << url.toString(); + // 请求对象 QNetworkRequest request; request.setUrl(url); @@ -55,105 +57,14 @@ resultObj.insert("code", -1); } - return resultObj; -} - -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} - -QJsonObject HttpRequestController::initDeviceList(QString devType) -{ - QJsonObject resultObj; - - QString counterDevType = ""; - QMapIterator it(ConstCache::getInstance().deviceTypes); - while (it.hasNext()) - { - it.next(); - if (it.value().contains(devType) == true) - { - counterDevType = it.key(); - break; - } - } - - // 获取设备列表的接口地址 - QUrl url = baseUrl + "/device/list"; - QUrlQuery query; - query.addQueryItem("type", counterDevType); - url.setQuery(query); - - QNetworkRequest request; - request.setUrl(url); - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - request.setRawHeader("system", system.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceList.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceList.append(item); - } - } - } else - { - resultObj.insert("code", -1); - } + resultObj.insert("code", 200); // qDebug() << resultObj; return resultObj; } + QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { QJsonObject resultObj; @@ -187,3 +98,36 @@ return resultObj; } + +QJsonObject HttpRequestController::getPhaseChannelList(QString deviceId) +{ + QJsonObject resultObj; + + // 获取设备列表的接口地址 + QUrl url = baseUrl + "/channel/list"; + QUrlQuery query; + query.addQueryItem("deviceId", deviceId); + url.setQuery(query); + + QNetworkRequest request; + request.setUrl(url); + request.setRawHeader("Content-type", "application/json"); + request.setRawHeader("token", token.toLocal8Bit()); + + qDebug() << url; + + QNetworkReply * reply = httpUtil->sendGetRequest(request); + const QByteArray reply_data = reply->readAll(); + QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); + if(jsonDocument.isNull() == false) + { + resultObj = jsonDocument.object(); + } else + { + resultObj.insert("code", -1); + } + + qDebug() << resultObj; + + return resultObj; +} diff --git a/PhaseCompAcq/common/HttpRequestController.h b/PhaseCompAcq/common/HttpRequestController.h index 2610548..352a769 100644 --- a/PhaseCompAcq/common/HttpRequestController.h +++ b/PhaseCompAcq/common/HttpRequestController.h @@ -1,7 +1,9 @@ -#ifndef HTTPREQUESTCONTROLLER_H +#ifndef HTTPREQUESTCONTROLLER_H #define HTTPREQUESTCONTROLLER_H #include +#include +#include #include #include "utils/HttpRequestUtil.h" @@ -17,9 +19,8 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); - QJsonObject initDeviceList(QString devType); QJsonObject initDeviceList(QString devType, QString system); + QJsonObject getPhaseChannelList(QString deviceId); private: HttpRequestUtil * httpUtil; diff --git a/PhaseCompAcq/common/common.pri b/PhaseCompAcq/common/common.pri index b80a640..01ba6ee 100644 --- a/PhaseCompAcq/common/common.pri +++ b/PhaseCompAcq/common/common.pri @@ -4,6 +4,7 @@ SOURCES += $$PWD/utils/QSerialPortUtil.cpp SOURCES += $$PWD/utils/QLogUtil.cpp SOURCES += $$PWD/utils/QKafkaUtil.cpp +SOURCES += $$PWD/utils/QKafkaConsumer.cpp SOURCES += $$PWD/utils/HttpRequestUtil.cpp SOURCES += $$PWD/utils/MD5.cpp SOURCES += $$PWD/HttpRequestController.cpp @@ -14,6 +15,7 @@ HEADERS += $$PWD/utils/QSerialPortUtil.h HEADERS += $$PWD/utils/QLogUtil.h HEADERS += $$PWD/utils/QKafkaUtil.h +HEADERS += $$PWD/utils/QKafkaConsumer.h HEADERS += $$PWD/utils/HttpRequestUtil.h HEADERS += $$PWD/utils/DefHead.h HEADERS += $$PWD/utils/MD5.h diff --git a/PhaseCompAcq/common/utils/QKafkaConsumer.cpp b/PhaseCompAcq/common/utils/QKafkaConsumer.cpp new file mode 100644 index 0000000..cb4fe37 --- /dev/null +++ b/PhaseCompAcq/common/utils/QKafkaConsumer.cpp @@ -0,0 +1,138 @@ +#include "QKafkaConsumer.h" +#include "SettingConfig.h" +#include +#include + +static volatile int runFlag = 1; +static bool exit_eof = false; + +QKafkaConsumer::QKafkaConsumer(QObject *parent) : QThread(parent) +{ + this->conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL); + this->tconf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC); + + conf->set("enable.partition.eof", "true", errStr); +} + +void QKafkaConsumer::setBrokers(QString brokers) +{ + this->brokers = brokers; +} +void QKafkaConsumer::setTopic(QString topic) +{ + this->topic = topic; +} + +int QKafkaConsumer::createConsumer() +{ + int ret = conf->set("metadata.broker.list", brokers.toStdString(), errStr); + if (SettingConfig::getInstance().NEED_SASL == 1) + { + conf->set("sasl.username", SettingConfig::getInstance().SASL_USERNAME.toStdString(), errStr); + conf->set("sasl.password", SettingConfig::getInstance().SASL_PASSWORD.toStdString(), errStr); + conf->set("security.protocol", "sasl_plaintext", errStr); + conf->set("sasl.mechanisms", "PLAIN", errStr); + } + + if (ret != RdKafka::Conf::CONF_OK) + { + std::cerr << "RdKafka conf set brokerlist failed :" << errStr.c_str() << std::endl; + } + + consumer = RdKafka::Consumer::create(conf, errStr); + if (!consumer) { + std::cerr << "Failed to create consumer: " << errStr << std::endl; + return -1; + } + + std::cout << "% Created consumer " << consumer->name() << std::endl; + + return 1; +} + +void QKafkaConsumer::run() +{ + RdKafka::Topic * topic = RdKafka::Topic::create(consumer, this->topic.toStdString(), tconf, errStr); + if (!topic) { + std::cerr << "Failed to create topic: " << errStr << std::endl; + } + + RdKafka::ErrorCode resp = consumer->start(topic, 0, RdKafka::Topic::OFFSET_END); + if (resp != RdKafka::ERR_NO_ERROR) { + std::cerr << "Failed to start consumer: " << RdKafka::err2str(resp) << std::endl; + } + + while (runFlag) + { + RdKafka::Message * message = consumer->consume(topic, 0, 200); + messageConsume(message); + } +} + +void QKafkaConsumer::exitThread() +{ + runFlag = false; +} + +void QKafkaConsumer::messageConsume(RdKafka::Message* message) { + const RdKafka::Headers *headers; + + switch (message->err()) { + case RdKafka::ERR__TIMED_OUT: + break; + + case RdKafka::ERR_NO_ERROR: + { + /* Real message */ +// std::cout << "Read msg at offset " << message->offset() << std::endl; +// printf("%.*s\n", static_cast(message->len()), static_cast(message->payload())); + + QString messageStr = static_cast(message->payload()); + QJsonParseError jsonErr; + QJsonDocument doc = QJsonDocument::fromJson(messageStr.toUtf8(), &jsonErr); + if (jsonErr.error == QJsonParseError::NoError) + { + QJsonObject obj = doc.object(); + obj.insert("cmdStr", messageStr); + emit messageRecieved(obj); + } + + if (message->key()) { + std::cout << "Key: " << *message->key() << std::endl; + } + headers = message->headers(); + if (headers) { + std::vector hdrs = headers->get_all(); + for (size_t i = 0 ; i < hdrs.size() ; i++) { + const RdKafka::Headers::Header hdr = hdrs[i]; + + if (hdr.value() != NULL) + printf(" Header: %s = \"%.*s\"\n", + hdr.key().c_str(), + (int)hdr.value_size(), (const char *)hdr.value()); + else + printf(" Header: %s = NULL\n", hdr.key().c_str()); + } + } + break; + } + + case RdKafka::ERR__PARTITION_EOF: + /* Last message */ + if (exit_eof) { + runFlag = 0; + } + break; + + case RdKafka::ERR__UNKNOWN_TOPIC: + case RdKafka::ERR__UNKNOWN_PARTITION: + std::cerr << "Consume failed: " << message->errstr() << std::endl; + runFlag = 0; + break; + + default: + /* Errors */ + std::cerr << "Consume failed: " << message->errstr() << std::endl; + runFlag = 0; + } +} diff --git a/PhaseCompAcq/common/utils/QKafkaConsumer.h b/PhaseCompAcq/common/utils/QKafkaConsumer.h new file mode 100644 index 0000000..84f4702 --- /dev/null +++ b/PhaseCompAcq/common/utils/QKafkaConsumer.h @@ -0,0 +1,40 @@ +#ifndef QKAFKACONSUMER_H +#define QKAFKACONSUMER_H + +#include +#include + +#include "include/librdkafka/rdkafkacpp.h" + +class QKafkaConsumer : public QThread +{ + Q_OBJECT +public: + explicit QKafkaConsumer(QObject *parent = nullptr); + + void setBrokers(QString brokers); + void setTopic(QString topic); + + int createConsumer(); + void run(); + void exitThread(); + + void messageConsume(RdKafka::Message * message); + +private: + QString brokers; + QString topic; + + std::string errStr; + + RdKafka::Conf * conf; + RdKafka::Conf * tconf; + + RdKafka::Consumer * consumer = 0; + +signals: + void messageRecieved(QJsonObject command); + +}; + +#endif // QKAFKACONSUMER_H diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index f42eba8..7bce7ac 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -196,7 +196,7 @@ // 绑定显示函数 connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); - // 查询计数器的通道 并初始化串口 + // 查询计数器的通道 this->getChannelList(deviceId); } diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 0a907f1..9ac4497 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -64,45 +64,6 @@ return resultObj; } -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { diff --git a/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index a37122b..c84ad21 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -19,7 +19,6 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 77975b3..de50156 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -21,8 +21,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置stackWidget的大小 @@ -84,11 +83,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void DeviceHubWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void DeviceHubWindow::on_devTypeSelect_currentIndexChanged(int index) { ui->stackedWidget->setCurrentIndex(index); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index 39443a8..3b4c10a 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -50,7 +50,6 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); void on_devTypeSelect_currentIndexChanged(int index); void on_devSelect_currentIndexChanged(int index); diff --git a/DeviceHub/DeviceHubWindow.ui b/DeviceHub/DeviceHubWindow.ui index cc6ccd0..70c0618 100644 --- a/DeviceHub/DeviceHubWindow.ui +++ b/DeviceHub/DeviceHubWindow.ui @@ -77,22 +77,6 @@ - - - - 1030 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 9c613db..d0e8a20 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -51,6 +51,10 @@ { this->devCode = devCode; } +QString PhaseDevice::getDeviceId() +{ + return this->deviceId; +} void PhaseDevice::setDeviceId(QString deviceId) { this->deviceId = deviceId; @@ -61,6 +65,11 @@ return this->serialUtil.isOpen(); } +void PhaseDevice::clearChannelPhaseData(int channelNo) +{ + this->phaseVector[channelNo - 1].clear(); +} + void PhaseDevice::initSerialPort() { int master = SettingConfig::getInstance().MASTER; @@ -87,6 +96,11 @@ this->serialUtil.sendData(startCmd.toLocal8Bit()); QThread::msleep(100); } + + // 开始计算后清除累积的相位数据和准确度计算结果 + for (int i = 0; i < phaseVector.size(); i++) { + phaseVector[i].clear(); + } } void PhaseDevice::stopWork() diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 7531798..06a6b0f 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -27,8 +27,11 @@ void setBaudRate(int baudRate); QString getDevCode(); void setDevCode(QString devCode); + QString getDeviceId(); void setDeviceId(QString deviceId); + void clearChannelPhaseData(int channelNo); + bool isSerialOpen(); private: QString deviceId; diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 51673f5..90db100 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -27,10 +27,9 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); - ui->endButt->move(screenRect.width() - 200, 10); - ui->startButt->move(screenRect.width() - 260, 10); + ui->minButt->move(screenRect.width() - 80, 10); + ui->endButt->move(screenRect.width() - 140, 10); + ui->startButt->move(screenRect.width() - 200, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -39,6 +38,16 @@ ui->logo->setText(QString("相位测量数据采集软件——%1").arg(SettingConfig::getInstance().MASTER == 1 ? "主" : "备")); + if (SettingConfig::getInstance().NEED_KAFKA == 1) { + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_CMD_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &PhaseWindow::onPhaseCommandReceived); + } + httpReq = new HttpRequestController(this); // 1. 获取访问接口需要的token int retCode = this->initHttpToken(); @@ -49,44 +58,17 @@ QTimer::singleShot(1000, qApp, SLOT(quit())); } // 2. 获取字典值:设备类型 - retCode = this->initDictDeviceTypes(); // 3. 获取比相仪设备列表 - QJsonObject devListRes = this->initDeviceList(); - if (devListRes.find("code")->toInt() == 200) - { - ui->devSelect->clear(); + initDeviceList(); - // 4. 将获取到的设备添加到下拉列表框中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - QJsonObject devItem = devArray.at(i).toObject(); - ui->devSelect->addItem(devItem.find("deviceName")->toString(), devItem.find("deviceNo")->toString()); + // 4. 打开串口 + for (int i = 0; i < deviceList.size(); i++) { + PhaseDevice * devicePtr = deviceList.at(i); + devicePtr->initSerialPort(); + devicePtr->startWork(); - PhaseDevice * device = new PhaseDevice(this); - deviceList.append(device); - - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceId(devItem.find("deviceId")->toString()); - - connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); - connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); - - device->initSerialPort(); - - QThread::msleep(100); - device->startWork(); - } - // 5. 设置下拉框的样式 - QStandardItemModel * model = qobject_cast(ui->devSelect->model()); - for (int i = 0; i < model->rowCount(); ++i) - { - QStandardItem * item = model->item(i); - item->setSizeHint({ 0, 30 }); - } + QThread::msleep(100); } // 6. 绘制一个设备的多个通道数据面板 @@ -316,15 +298,69 @@ SettingConfig::getInstance().APP_KEY); return response.find("code")->toInt(); } -int PhaseWindow::initDictDeviceTypes() + +void PhaseWindow::initDeviceList() { - QJsonObject response = httpReq->initDictDeviceType(); - return response.find("code")->toInt(); + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.find("code")->toInt() == 200) + { + ui->devSelect->clear(); + + // 4. 将获取到的设备添加到下拉列表框中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) + { + QJsonObject devItem = devArray.at(i).toObject(); + ui->devSelect->addItem(devItem.value("deviceName").toString(), devItem.value("deviceNo").toString()); + + QString deviceId = devItem.value("deviceId").toString(); + + PhaseDevice * device = new PhaseDevice(this); + deviceList.append(device); + + ConstCache::getInstance().deviceMap.insert(deviceId, device); + + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceId(deviceId); + + connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); + connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); + + this->getChannelList(deviceId); + } + // 5. 设置下拉框的样式 + QStandardItemModel * model = qobject_cast(ui->devSelect->model()); + for (int i = 0; i < model->rowCount(); ++i) + { + QStandardItem * item = model->item(i); + item->setSizeHint({ 0, 30 }); + } + } } -QJsonObject PhaseWindow::initDeviceList() + +void PhaseWindow::getChannelList(QString deviceId) { - QJsonObject response = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - return response; + // 查询计数器的通道 + QJsonObject response = httpReq->getPhaseChannelList(deviceId); + if (response.value("code").toInt() == 200) + { + // 将通道绘制在界面上 + QJsonArray channelArray = response.value("data").toArray(); + for (int i = 0; i < channelArray.size(); i++) + { + // 接口返回的channel对象 + QJsonObject channelItem = channelArray.at(i).toObject(); + QString channelId = channelItem.value("id").toString(); // 通道ID + int channelNo = channelItem.value("channelNo").toInt(); + + // 添加到通道的集合中 key=channelId value=deviceId-channelNo + if (channelNo > 0) { + ConstCache::getInstance().channelMap.insert(channelId, QString("%1-%2").arg(deviceId).arg(channelNo)); + } + } + } } void PhaseWindow::on_minButt_clicked() @@ -332,11 +368,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void PhaseWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void PhaseWindow::on_startButt_clicked() { int devSelected = ui->devSelect->currentIndex(); @@ -385,3 +416,46 @@ ((QLabel *)channelBox->children().at(20))->setText(QString("(%1)").arg(0)); // tau=10000 Count } } + +void PhaseWindow::onPhaseCommandReceived(QJsonObject command) +{ + // 缺少必须的字段deviceId 或 cmd 直接返回 + if (command.contains("deviceId") == false || command.contains("cmd") == false) { + return; + } + + QString deviceId = command.value("deviceId").toString(); + QString cmdType = command.value("cmd").toString(); + + // deviceId不属于子系统内的比相仪 直接返回 + if (ConstCache::getInstance().deviceMap.contains(deviceId) == false) { + return; + } + + if (cmdType.toUpper() == "START" || cmdType.toUpper() == "STOP") + { + // 设备开始采集或者停止采集指令 + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (devicePtr != nullptr) { + if (cmdType.toUpper() == "START") { + devicePtr->startWork(); + } else if (cmdType.toUpper() == "STOP") { + devicePtr->stopWork(); + } + } + } else if (cmdType.toUpper() == "CLEAR") + { + // 清除通道的数据 通道级指令 + QString channelId = command.value("channelId").toString(); + if (ConstCache::getInstance().channelMap.contains(channelId) == true) { + QString channelIndex = ConstCache::getInstance().channelMap.value(channelId); + QString channelDevId = channelIndex.split("-").at(0); + int channelNo = ((QString) channelIndex.split("-").at(1)).toInt(); + + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(channelDevId); + if (devicePtr != nullptr) { + devicePtr->clearChannelPhaseData(channelNo); + } + } + } +} diff --git a/PhaseCompAcq/PhaseWindow.h b/PhaseCompAcq/PhaseWindow.h index 572bbde..961e0b1 100644 --- a/PhaseCompAcq/PhaseWindow.h +++ b/PhaseCompAcq/PhaseWindow.h @@ -8,6 +8,7 @@ #include "PhaseDevice.h" #include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaConsumer.h" #include "common/HttpRequestController.h" #include "common/HttpServer.h" @@ -30,21 +31,22 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); - void on_startButt_clicked(); void on_endButt_clicked(); void on_devSelect_currentIndexChanged(int index); + void onPhaseCommandReceived(QJsonObject command); + private: int initHttpToken(); - int initDictDeviceTypes(); - QJsonObject initDeviceList(); + void initDeviceList(); + void getChannelList(QString deviceId); Ui::PhaseWindow *ui; + QKafkaConsumer * kafkaConsumer; HttpRequestController * httpReq; QList deviceList; diff --git a/PhaseCompAcq/PhaseWindow.ui b/PhaseCompAcq/PhaseWindow.ui index d083fb9..9f9456c 100644 --- a/PhaseCompAcq/PhaseWindow.ui +++ b/PhaseCompAcq/PhaseWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/common/ConstCache.h b/PhaseCompAcq/common/ConstCache.h index 6cc831b..0379863 100644 --- a/PhaseCompAcq/common/ConstCache.h +++ b/PhaseCompAcq/common/ConstCache.h @@ -5,6 +5,7 @@ #include #include #include +#include "PhaseDevice.h" class ConstCache : public QObject { @@ -21,6 +22,8 @@ QMap deviceTypes; QList deviceList; + QMap deviceMap; // key=deviceId + QMap channelMap; // key=channelId value=deviceId-no private: ConstCache() {}; diff --git a/PhaseCompAcq/common/HttpRequestController.cpp b/PhaseCompAcq/common/HttpRequestController.cpp index 0a9f3b5..359a0fc 100644 --- a/PhaseCompAcq/common/HttpRequestController.cpp +++ b/PhaseCompAcq/common/HttpRequestController.cpp @@ -1,4 +1,4 @@ -#include "HttpRequestController.h" +#include "HttpRequestController.h" HttpRequestController::HttpRequestController(QObject *parent) : QObject(parent) { @@ -14,6 +14,8 @@ // 获取token的url地址 QUrl url = baseUrl + "/getTokenByClientId"; + qDebug() << url.toString(); + // 请求对象 QNetworkRequest request; request.setUrl(url); @@ -55,105 +57,14 @@ resultObj.insert("code", -1); } - return resultObj; -} - -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} - -QJsonObject HttpRequestController::initDeviceList(QString devType) -{ - QJsonObject resultObj; - - QString counterDevType = ""; - QMapIterator it(ConstCache::getInstance().deviceTypes); - while (it.hasNext()) - { - it.next(); - if (it.value().contains(devType) == true) - { - counterDevType = it.key(); - break; - } - } - - // 获取设备列表的接口地址 - QUrl url = baseUrl + "/device/list"; - QUrlQuery query; - query.addQueryItem("type", counterDevType); - url.setQuery(query); - - QNetworkRequest request; - request.setUrl(url); - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - request.setRawHeader("system", system.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceList.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceList.append(item); - } - } - } else - { - resultObj.insert("code", -1); - } + resultObj.insert("code", 200); // qDebug() << resultObj; return resultObj; } + QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { QJsonObject resultObj; @@ -187,3 +98,36 @@ return resultObj; } + +QJsonObject HttpRequestController::getPhaseChannelList(QString deviceId) +{ + QJsonObject resultObj; + + // 获取设备列表的接口地址 + QUrl url = baseUrl + "/channel/list"; + QUrlQuery query; + query.addQueryItem("deviceId", deviceId); + url.setQuery(query); + + QNetworkRequest request; + request.setUrl(url); + request.setRawHeader("Content-type", "application/json"); + request.setRawHeader("token", token.toLocal8Bit()); + + qDebug() << url; + + QNetworkReply * reply = httpUtil->sendGetRequest(request); + const QByteArray reply_data = reply->readAll(); + QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); + if(jsonDocument.isNull() == false) + { + resultObj = jsonDocument.object(); + } else + { + resultObj.insert("code", -1); + } + + qDebug() << resultObj; + + return resultObj; +} diff --git a/PhaseCompAcq/common/HttpRequestController.h b/PhaseCompAcq/common/HttpRequestController.h index 2610548..352a769 100644 --- a/PhaseCompAcq/common/HttpRequestController.h +++ b/PhaseCompAcq/common/HttpRequestController.h @@ -1,7 +1,9 @@ -#ifndef HTTPREQUESTCONTROLLER_H +#ifndef HTTPREQUESTCONTROLLER_H #define HTTPREQUESTCONTROLLER_H #include +#include +#include #include #include "utils/HttpRequestUtil.h" @@ -17,9 +19,8 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); - QJsonObject initDeviceList(QString devType); QJsonObject initDeviceList(QString devType, QString system); + QJsonObject getPhaseChannelList(QString deviceId); private: HttpRequestUtil * httpUtil; diff --git a/PhaseCompAcq/common/common.pri b/PhaseCompAcq/common/common.pri index b80a640..01ba6ee 100644 --- a/PhaseCompAcq/common/common.pri +++ b/PhaseCompAcq/common/common.pri @@ -4,6 +4,7 @@ SOURCES += $$PWD/utils/QSerialPortUtil.cpp SOURCES += $$PWD/utils/QLogUtil.cpp SOURCES += $$PWD/utils/QKafkaUtil.cpp +SOURCES += $$PWD/utils/QKafkaConsumer.cpp SOURCES += $$PWD/utils/HttpRequestUtil.cpp SOURCES += $$PWD/utils/MD5.cpp SOURCES += $$PWD/HttpRequestController.cpp @@ -14,6 +15,7 @@ HEADERS += $$PWD/utils/QSerialPortUtil.h HEADERS += $$PWD/utils/QLogUtil.h HEADERS += $$PWD/utils/QKafkaUtil.h +HEADERS += $$PWD/utils/QKafkaConsumer.h HEADERS += $$PWD/utils/HttpRequestUtil.h HEADERS += $$PWD/utils/DefHead.h HEADERS += $$PWD/utils/MD5.h diff --git a/PhaseCompAcq/common/utils/QKafkaConsumer.cpp b/PhaseCompAcq/common/utils/QKafkaConsumer.cpp new file mode 100644 index 0000000..cb4fe37 --- /dev/null +++ b/PhaseCompAcq/common/utils/QKafkaConsumer.cpp @@ -0,0 +1,138 @@ +#include "QKafkaConsumer.h" +#include "SettingConfig.h" +#include +#include + +static volatile int runFlag = 1; +static bool exit_eof = false; + +QKafkaConsumer::QKafkaConsumer(QObject *parent) : QThread(parent) +{ + this->conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL); + this->tconf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC); + + conf->set("enable.partition.eof", "true", errStr); +} + +void QKafkaConsumer::setBrokers(QString brokers) +{ + this->brokers = brokers; +} +void QKafkaConsumer::setTopic(QString topic) +{ + this->topic = topic; +} + +int QKafkaConsumer::createConsumer() +{ + int ret = conf->set("metadata.broker.list", brokers.toStdString(), errStr); + if (SettingConfig::getInstance().NEED_SASL == 1) + { + conf->set("sasl.username", SettingConfig::getInstance().SASL_USERNAME.toStdString(), errStr); + conf->set("sasl.password", SettingConfig::getInstance().SASL_PASSWORD.toStdString(), errStr); + conf->set("security.protocol", "sasl_plaintext", errStr); + conf->set("sasl.mechanisms", "PLAIN", errStr); + } + + if (ret != RdKafka::Conf::CONF_OK) + { + std::cerr << "RdKafka conf set brokerlist failed :" << errStr.c_str() << std::endl; + } + + consumer = RdKafka::Consumer::create(conf, errStr); + if (!consumer) { + std::cerr << "Failed to create consumer: " << errStr << std::endl; + return -1; + } + + std::cout << "% Created consumer " << consumer->name() << std::endl; + + return 1; +} + +void QKafkaConsumer::run() +{ + RdKafka::Topic * topic = RdKafka::Topic::create(consumer, this->topic.toStdString(), tconf, errStr); + if (!topic) { + std::cerr << "Failed to create topic: " << errStr << std::endl; + } + + RdKafka::ErrorCode resp = consumer->start(topic, 0, RdKafka::Topic::OFFSET_END); + if (resp != RdKafka::ERR_NO_ERROR) { + std::cerr << "Failed to start consumer: " << RdKafka::err2str(resp) << std::endl; + } + + while (runFlag) + { + RdKafka::Message * message = consumer->consume(topic, 0, 200); + messageConsume(message); + } +} + +void QKafkaConsumer::exitThread() +{ + runFlag = false; +} + +void QKafkaConsumer::messageConsume(RdKafka::Message* message) { + const RdKafka::Headers *headers; + + switch (message->err()) { + case RdKafka::ERR__TIMED_OUT: + break; + + case RdKafka::ERR_NO_ERROR: + { + /* Real message */ +// std::cout << "Read msg at offset " << message->offset() << std::endl; +// printf("%.*s\n", static_cast(message->len()), static_cast(message->payload())); + + QString messageStr = static_cast(message->payload()); + QJsonParseError jsonErr; + QJsonDocument doc = QJsonDocument::fromJson(messageStr.toUtf8(), &jsonErr); + if (jsonErr.error == QJsonParseError::NoError) + { + QJsonObject obj = doc.object(); + obj.insert("cmdStr", messageStr); + emit messageRecieved(obj); + } + + if (message->key()) { + std::cout << "Key: " << *message->key() << std::endl; + } + headers = message->headers(); + if (headers) { + std::vector hdrs = headers->get_all(); + for (size_t i = 0 ; i < hdrs.size() ; i++) { + const RdKafka::Headers::Header hdr = hdrs[i]; + + if (hdr.value() != NULL) + printf(" Header: %s = \"%.*s\"\n", + hdr.key().c_str(), + (int)hdr.value_size(), (const char *)hdr.value()); + else + printf(" Header: %s = NULL\n", hdr.key().c_str()); + } + } + break; + } + + case RdKafka::ERR__PARTITION_EOF: + /* Last message */ + if (exit_eof) { + runFlag = 0; + } + break; + + case RdKafka::ERR__UNKNOWN_TOPIC: + case RdKafka::ERR__UNKNOWN_PARTITION: + std::cerr << "Consume failed: " << message->errstr() << std::endl; + runFlag = 0; + break; + + default: + /* Errors */ + std::cerr << "Consume failed: " << message->errstr() << std::endl; + runFlag = 0; + } +} diff --git a/PhaseCompAcq/common/utils/QKafkaConsumer.h b/PhaseCompAcq/common/utils/QKafkaConsumer.h new file mode 100644 index 0000000..84f4702 --- /dev/null +++ b/PhaseCompAcq/common/utils/QKafkaConsumer.h @@ -0,0 +1,40 @@ +#ifndef QKAFKACONSUMER_H +#define QKAFKACONSUMER_H + +#include +#include + +#include "include/librdkafka/rdkafkacpp.h" + +class QKafkaConsumer : public QThread +{ + Q_OBJECT +public: + explicit QKafkaConsumer(QObject *parent = nullptr); + + void setBrokers(QString brokers); + void setTopic(QString topic); + + int createConsumer(); + void run(); + void exitThread(); + + void messageConsume(RdKafka::Message * message); + +private: + QString brokers; + QString topic; + + std::string errStr; + + RdKafka::Conf * conf; + RdKafka::Conf * tconf; + + RdKafka::Consumer * consumer = 0; + +signals: + void messageRecieved(QJsonObject command); + +}; + +#endif // QKAFKACONSUMER_H diff --git a/PhaseCompAcq/common/utils/SettingConfig.cpp b/PhaseCompAcq/common/utils/SettingConfig.cpp index ed02ac1..1c15f63 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.cpp +++ b/PhaseCompAcq/common/utils/SettingConfig.cpp @@ -12,6 +12,7 @@ KAFKA_DATA_TOPIC = getProperty("kafka", "dataTopic").toString(); KAFKA_STATUS_TOPIC = getProperty("kafka", "statusTopic").toString(); KAFKA_PERFORM_TOPIC = getProperty("kafka", "performTopic").toString(); + KAFKA_CMD_TOPIC = getProperty("kafka", "cmdTopic").toString(); NEED_SASL = getProperty("kafka", "needSasl").toInt(); SASL_USERNAME = getProperty("kafka", "saslUsername").toString(); SASL_PASSWORD = getProperty("kafka", "saslPassword").toString(); diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index f42eba8..7bce7ac 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -196,7 +196,7 @@ // 绑定显示函数 connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); - // 查询计数器的通道 并初始化串口 + // 查询计数器的通道 this->getChannelList(deviceId); } diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 0a907f1..9ac4497 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -64,45 +64,6 @@ return resultObj; } -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { diff --git a/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index a37122b..c84ad21 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -19,7 +19,6 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 77975b3..de50156 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -21,8 +21,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置stackWidget的大小 @@ -84,11 +83,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void DeviceHubWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void DeviceHubWindow::on_devTypeSelect_currentIndexChanged(int index) { ui->stackedWidget->setCurrentIndex(index); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index 39443a8..3b4c10a 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -50,7 +50,6 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); void on_devTypeSelect_currentIndexChanged(int index); void on_devSelect_currentIndexChanged(int index); diff --git a/DeviceHub/DeviceHubWindow.ui b/DeviceHub/DeviceHubWindow.ui index cc6ccd0..70c0618 100644 --- a/DeviceHub/DeviceHubWindow.ui +++ b/DeviceHub/DeviceHubWindow.ui @@ -77,22 +77,6 @@ - - - - 1030 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 9c613db..d0e8a20 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -51,6 +51,10 @@ { this->devCode = devCode; } +QString PhaseDevice::getDeviceId() +{ + return this->deviceId; +} void PhaseDevice::setDeviceId(QString deviceId) { this->deviceId = deviceId; @@ -61,6 +65,11 @@ return this->serialUtil.isOpen(); } +void PhaseDevice::clearChannelPhaseData(int channelNo) +{ + this->phaseVector[channelNo - 1].clear(); +} + void PhaseDevice::initSerialPort() { int master = SettingConfig::getInstance().MASTER; @@ -87,6 +96,11 @@ this->serialUtil.sendData(startCmd.toLocal8Bit()); QThread::msleep(100); } + + // 开始计算后清除累积的相位数据和准确度计算结果 + for (int i = 0; i < phaseVector.size(); i++) { + phaseVector[i].clear(); + } } void PhaseDevice::stopWork() diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 7531798..06a6b0f 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -27,8 +27,11 @@ void setBaudRate(int baudRate); QString getDevCode(); void setDevCode(QString devCode); + QString getDeviceId(); void setDeviceId(QString deviceId); + void clearChannelPhaseData(int channelNo); + bool isSerialOpen(); private: QString deviceId; diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 51673f5..90db100 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -27,10 +27,9 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); - ui->endButt->move(screenRect.width() - 200, 10); - ui->startButt->move(screenRect.width() - 260, 10); + ui->minButt->move(screenRect.width() - 80, 10); + ui->endButt->move(screenRect.width() - 140, 10); + ui->startButt->move(screenRect.width() - 200, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -39,6 +38,16 @@ ui->logo->setText(QString("相位测量数据采集软件——%1").arg(SettingConfig::getInstance().MASTER == 1 ? "主" : "备")); + if (SettingConfig::getInstance().NEED_KAFKA == 1) { + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_CMD_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &PhaseWindow::onPhaseCommandReceived); + } + httpReq = new HttpRequestController(this); // 1. 获取访问接口需要的token int retCode = this->initHttpToken(); @@ -49,44 +58,17 @@ QTimer::singleShot(1000, qApp, SLOT(quit())); } // 2. 获取字典值:设备类型 - retCode = this->initDictDeviceTypes(); // 3. 获取比相仪设备列表 - QJsonObject devListRes = this->initDeviceList(); - if (devListRes.find("code")->toInt() == 200) - { - ui->devSelect->clear(); + initDeviceList(); - // 4. 将获取到的设备添加到下拉列表框中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - QJsonObject devItem = devArray.at(i).toObject(); - ui->devSelect->addItem(devItem.find("deviceName")->toString(), devItem.find("deviceNo")->toString()); + // 4. 打开串口 + for (int i = 0; i < deviceList.size(); i++) { + PhaseDevice * devicePtr = deviceList.at(i); + devicePtr->initSerialPort(); + devicePtr->startWork(); - PhaseDevice * device = new PhaseDevice(this); - deviceList.append(device); - - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceId(devItem.find("deviceId")->toString()); - - connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); - connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); - - device->initSerialPort(); - - QThread::msleep(100); - device->startWork(); - } - // 5. 设置下拉框的样式 - QStandardItemModel * model = qobject_cast(ui->devSelect->model()); - for (int i = 0; i < model->rowCount(); ++i) - { - QStandardItem * item = model->item(i); - item->setSizeHint({ 0, 30 }); - } + QThread::msleep(100); } // 6. 绘制一个设备的多个通道数据面板 @@ -316,15 +298,69 @@ SettingConfig::getInstance().APP_KEY); return response.find("code")->toInt(); } -int PhaseWindow::initDictDeviceTypes() + +void PhaseWindow::initDeviceList() { - QJsonObject response = httpReq->initDictDeviceType(); - return response.find("code")->toInt(); + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.find("code")->toInt() == 200) + { + ui->devSelect->clear(); + + // 4. 将获取到的设备添加到下拉列表框中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) + { + QJsonObject devItem = devArray.at(i).toObject(); + ui->devSelect->addItem(devItem.value("deviceName").toString(), devItem.value("deviceNo").toString()); + + QString deviceId = devItem.value("deviceId").toString(); + + PhaseDevice * device = new PhaseDevice(this); + deviceList.append(device); + + ConstCache::getInstance().deviceMap.insert(deviceId, device); + + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceId(deviceId); + + connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); + connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); + + this->getChannelList(deviceId); + } + // 5. 设置下拉框的样式 + QStandardItemModel * model = qobject_cast(ui->devSelect->model()); + for (int i = 0; i < model->rowCount(); ++i) + { + QStandardItem * item = model->item(i); + item->setSizeHint({ 0, 30 }); + } + } } -QJsonObject PhaseWindow::initDeviceList() + +void PhaseWindow::getChannelList(QString deviceId) { - QJsonObject response = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - return response; + // 查询计数器的通道 + QJsonObject response = httpReq->getPhaseChannelList(deviceId); + if (response.value("code").toInt() == 200) + { + // 将通道绘制在界面上 + QJsonArray channelArray = response.value("data").toArray(); + for (int i = 0; i < channelArray.size(); i++) + { + // 接口返回的channel对象 + QJsonObject channelItem = channelArray.at(i).toObject(); + QString channelId = channelItem.value("id").toString(); // 通道ID + int channelNo = channelItem.value("channelNo").toInt(); + + // 添加到通道的集合中 key=channelId value=deviceId-channelNo + if (channelNo > 0) { + ConstCache::getInstance().channelMap.insert(channelId, QString("%1-%2").arg(deviceId).arg(channelNo)); + } + } + } } void PhaseWindow::on_minButt_clicked() @@ -332,11 +368,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void PhaseWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void PhaseWindow::on_startButt_clicked() { int devSelected = ui->devSelect->currentIndex(); @@ -385,3 +416,46 @@ ((QLabel *)channelBox->children().at(20))->setText(QString("(%1)").arg(0)); // tau=10000 Count } } + +void PhaseWindow::onPhaseCommandReceived(QJsonObject command) +{ + // 缺少必须的字段deviceId 或 cmd 直接返回 + if (command.contains("deviceId") == false || command.contains("cmd") == false) { + return; + } + + QString deviceId = command.value("deviceId").toString(); + QString cmdType = command.value("cmd").toString(); + + // deviceId不属于子系统内的比相仪 直接返回 + if (ConstCache::getInstance().deviceMap.contains(deviceId) == false) { + return; + } + + if (cmdType.toUpper() == "START" || cmdType.toUpper() == "STOP") + { + // 设备开始采集或者停止采集指令 + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (devicePtr != nullptr) { + if (cmdType.toUpper() == "START") { + devicePtr->startWork(); + } else if (cmdType.toUpper() == "STOP") { + devicePtr->stopWork(); + } + } + } else if (cmdType.toUpper() == "CLEAR") + { + // 清除通道的数据 通道级指令 + QString channelId = command.value("channelId").toString(); + if (ConstCache::getInstance().channelMap.contains(channelId) == true) { + QString channelIndex = ConstCache::getInstance().channelMap.value(channelId); + QString channelDevId = channelIndex.split("-").at(0); + int channelNo = ((QString) channelIndex.split("-").at(1)).toInt(); + + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(channelDevId); + if (devicePtr != nullptr) { + devicePtr->clearChannelPhaseData(channelNo); + } + } + } +} diff --git a/PhaseCompAcq/PhaseWindow.h b/PhaseCompAcq/PhaseWindow.h index 572bbde..961e0b1 100644 --- a/PhaseCompAcq/PhaseWindow.h +++ b/PhaseCompAcq/PhaseWindow.h @@ -8,6 +8,7 @@ #include "PhaseDevice.h" #include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaConsumer.h" #include "common/HttpRequestController.h" #include "common/HttpServer.h" @@ -30,21 +31,22 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); - void on_startButt_clicked(); void on_endButt_clicked(); void on_devSelect_currentIndexChanged(int index); + void onPhaseCommandReceived(QJsonObject command); + private: int initHttpToken(); - int initDictDeviceTypes(); - QJsonObject initDeviceList(); + void initDeviceList(); + void getChannelList(QString deviceId); Ui::PhaseWindow *ui; + QKafkaConsumer * kafkaConsumer; HttpRequestController * httpReq; QList deviceList; diff --git a/PhaseCompAcq/PhaseWindow.ui b/PhaseCompAcq/PhaseWindow.ui index d083fb9..9f9456c 100644 --- a/PhaseCompAcq/PhaseWindow.ui +++ b/PhaseCompAcq/PhaseWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/common/ConstCache.h b/PhaseCompAcq/common/ConstCache.h index 6cc831b..0379863 100644 --- a/PhaseCompAcq/common/ConstCache.h +++ b/PhaseCompAcq/common/ConstCache.h @@ -5,6 +5,7 @@ #include #include #include +#include "PhaseDevice.h" class ConstCache : public QObject { @@ -21,6 +22,8 @@ QMap deviceTypes; QList deviceList; + QMap deviceMap; // key=deviceId + QMap channelMap; // key=channelId value=deviceId-no private: ConstCache() {}; diff --git a/PhaseCompAcq/common/HttpRequestController.cpp b/PhaseCompAcq/common/HttpRequestController.cpp index 0a9f3b5..359a0fc 100644 --- a/PhaseCompAcq/common/HttpRequestController.cpp +++ b/PhaseCompAcq/common/HttpRequestController.cpp @@ -1,4 +1,4 @@ -#include "HttpRequestController.h" +#include "HttpRequestController.h" HttpRequestController::HttpRequestController(QObject *parent) : QObject(parent) { @@ -14,6 +14,8 @@ // 获取token的url地址 QUrl url = baseUrl + "/getTokenByClientId"; + qDebug() << url.toString(); + // 请求对象 QNetworkRequest request; request.setUrl(url); @@ -55,105 +57,14 @@ resultObj.insert("code", -1); } - return resultObj; -} - -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} - -QJsonObject HttpRequestController::initDeviceList(QString devType) -{ - QJsonObject resultObj; - - QString counterDevType = ""; - QMapIterator it(ConstCache::getInstance().deviceTypes); - while (it.hasNext()) - { - it.next(); - if (it.value().contains(devType) == true) - { - counterDevType = it.key(); - break; - } - } - - // 获取设备列表的接口地址 - QUrl url = baseUrl + "/device/list"; - QUrlQuery query; - query.addQueryItem("type", counterDevType); - url.setQuery(query); - - QNetworkRequest request; - request.setUrl(url); - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - request.setRawHeader("system", system.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceList.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceList.append(item); - } - } - } else - { - resultObj.insert("code", -1); - } + resultObj.insert("code", 200); // qDebug() << resultObj; return resultObj; } + QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { QJsonObject resultObj; @@ -187,3 +98,36 @@ return resultObj; } + +QJsonObject HttpRequestController::getPhaseChannelList(QString deviceId) +{ + QJsonObject resultObj; + + // 获取设备列表的接口地址 + QUrl url = baseUrl + "/channel/list"; + QUrlQuery query; + query.addQueryItem("deviceId", deviceId); + url.setQuery(query); + + QNetworkRequest request; + request.setUrl(url); + request.setRawHeader("Content-type", "application/json"); + request.setRawHeader("token", token.toLocal8Bit()); + + qDebug() << url; + + QNetworkReply * reply = httpUtil->sendGetRequest(request); + const QByteArray reply_data = reply->readAll(); + QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); + if(jsonDocument.isNull() == false) + { + resultObj = jsonDocument.object(); + } else + { + resultObj.insert("code", -1); + } + + qDebug() << resultObj; + + return resultObj; +} diff --git a/PhaseCompAcq/common/HttpRequestController.h b/PhaseCompAcq/common/HttpRequestController.h index 2610548..352a769 100644 --- a/PhaseCompAcq/common/HttpRequestController.h +++ b/PhaseCompAcq/common/HttpRequestController.h @@ -1,7 +1,9 @@ -#ifndef HTTPREQUESTCONTROLLER_H +#ifndef HTTPREQUESTCONTROLLER_H #define HTTPREQUESTCONTROLLER_H #include +#include +#include #include #include "utils/HttpRequestUtil.h" @@ -17,9 +19,8 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); - QJsonObject initDeviceList(QString devType); QJsonObject initDeviceList(QString devType, QString system); + QJsonObject getPhaseChannelList(QString deviceId); private: HttpRequestUtil * httpUtil; diff --git a/PhaseCompAcq/common/common.pri b/PhaseCompAcq/common/common.pri index b80a640..01ba6ee 100644 --- a/PhaseCompAcq/common/common.pri +++ b/PhaseCompAcq/common/common.pri @@ -4,6 +4,7 @@ SOURCES += $$PWD/utils/QSerialPortUtil.cpp SOURCES += $$PWD/utils/QLogUtil.cpp SOURCES += $$PWD/utils/QKafkaUtil.cpp +SOURCES += $$PWD/utils/QKafkaConsumer.cpp SOURCES += $$PWD/utils/HttpRequestUtil.cpp SOURCES += $$PWD/utils/MD5.cpp SOURCES += $$PWD/HttpRequestController.cpp @@ -14,6 +15,7 @@ HEADERS += $$PWD/utils/QSerialPortUtil.h HEADERS += $$PWD/utils/QLogUtil.h HEADERS += $$PWD/utils/QKafkaUtil.h +HEADERS += $$PWD/utils/QKafkaConsumer.h HEADERS += $$PWD/utils/HttpRequestUtil.h HEADERS += $$PWD/utils/DefHead.h HEADERS += $$PWD/utils/MD5.h diff --git a/PhaseCompAcq/common/utils/QKafkaConsumer.cpp b/PhaseCompAcq/common/utils/QKafkaConsumer.cpp new file mode 100644 index 0000000..cb4fe37 --- /dev/null +++ b/PhaseCompAcq/common/utils/QKafkaConsumer.cpp @@ -0,0 +1,138 @@ +#include "QKafkaConsumer.h" +#include "SettingConfig.h" +#include +#include + +static volatile int runFlag = 1; +static bool exit_eof = false; + +QKafkaConsumer::QKafkaConsumer(QObject *parent) : QThread(parent) +{ + this->conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL); + this->tconf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC); + + conf->set("enable.partition.eof", "true", errStr); +} + +void QKafkaConsumer::setBrokers(QString brokers) +{ + this->brokers = brokers; +} +void QKafkaConsumer::setTopic(QString topic) +{ + this->topic = topic; +} + +int QKafkaConsumer::createConsumer() +{ + int ret = conf->set("metadata.broker.list", brokers.toStdString(), errStr); + if (SettingConfig::getInstance().NEED_SASL == 1) + { + conf->set("sasl.username", SettingConfig::getInstance().SASL_USERNAME.toStdString(), errStr); + conf->set("sasl.password", SettingConfig::getInstance().SASL_PASSWORD.toStdString(), errStr); + conf->set("security.protocol", "sasl_plaintext", errStr); + conf->set("sasl.mechanisms", "PLAIN", errStr); + } + + if (ret != RdKafka::Conf::CONF_OK) + { + std::cerr << "RdKafka conf set brokerlist failed :" << errStr.c_str() << std::endl; + } + + consumer = RdKafka::Consumer::create(conf, errStr); + if (!consumer) { + std::cerr << "Failed to create consumer: " << errStr << std::endl; + return -1; + } + + std::cout << "% Created consumer " << consumer->name() << std::endl; + + return 1; +} + +void QKafkaConsumer::run() +{ + RdKafka::Topic * topic = RdKafka::Topic::create(consumer, this->topic.toStdString(), tconf, errStr); + if (!topic) { + std::cerr << "Failed to create topic: " << errStr << std::endl; + } + + RdKafka::ErrorCode resp = consumer->start(topic, 0, RdKafka::Topic::OFFSET_END); + if (resp != RdKafka::ERR_NO_ERROR) { + std::cerr << "Failed to start consumer: " << RdKafka::err2str(resp) << std::endl; + } + + while (runFlag) + { + RdKafka::Message * message = consumer->consume(topic, 0, 200); + messageConsume(message); + } +} + +void QKafkaConsumer::exitThread() +{ + runFlag = false; +} + +void QKafkaConsumer::messageConsume(RdKafka::Message* message) { + const RdKafka::Headers *headers; + + switch (message->err()) { + case RdKafka::ERR__TIMED_OUT: + break; + + case RdKafka::ERR_NO_ERROR: + { + /* Real message */ +// std::cout << "Read msg at offset " << message->offset() << std::endl; +// printf("%.*s\n", static_cast(message->len()), static_cast(message->payload())); + + QString messageStr = static_cast(message->payload()); + QJsonParseError jsonErr; + QJsonDocument doc = QJsonDocument::fromJson(messageStr.toUtf8(), &jsonErr); + if (jsonErr.error == QJsonParseError::NoError) + { + QJsonObject obj = doc.object(); + obj.insert("cmdStr", messageStr); + emit messageRecieved(obj); + } + + if (message->key()) { + std::cout << "Key: " << *message->key() << std::endl; + } + headers = message->headers(); + if (headers) { + std::vector hdrs = headers->get_all(); + for (size_t i = 0 ; i < hdrs.size() ; i++) { + const RdKafka::Headers::Header hdr = hdrs[i]; + + if (hdr.value() != NULL) + printf(" Header: %s = \"%.*s\"\n", + hdr.key().c_str(), + (int)hdr.value_size(), (const char *)hdr.value()); + else + printf(" Header: %s = NULL\n", hdr.key().c_str()); + } + } + break; + } + + case RdKafka::ERR__PARTITION_EOF: + /* Last message */ + if (exit_eof) { + runFlag = 0; + } + break; + + case RdKafka::ERR__UNKNOWN_TOPIC: + case RdKafka::ERR__UNKNOWN_PARTITION: + std::cerr << "Consume failed: " << message->errstr() << std::endl; + runFlag = 0; + break; + + default: + /* Errors */ + std::cerr << "Consume failed: " << message->errstr() << std::endl; + runFlag = 0; + } +} diff --git a/PhaseCompAcq/common/utils/QKafkaConsumer.h b/PhaseCompAcq/common/utils/QKafkaConsumer.h new file mode 100644 index 0000000..84f4702 --- /dev/null +++ b/PhaseCompAcq/common/utils/QKafkaConsumer.h @@ -0,0 +1,40 @@ +#ifndef QKAFKACONSUMER_H +#define QKAFKACONSUMER_H + +#include +#include + +#include "include/librdkafka/rdkafkacpp.h" + +class QKafkaConsumer : public QThread +{ + Q_OBJECT +public: + explicit QKafkaConsumer(QObject *parent = nullptr); + + void setBrokers(QString brokers); + void setTopic(QString topic); + + int createConsumer(); + void run(); + void exitThread(); + + void messageConsume(RdKafka::Message * message); + +private: + QString brokers; + QString topic; + + std::string errStr; + + RdKafka::Conf * conf; + RdKafka::Conf * tconf; + + RdKafka::Consumer * consumer = 0; + +signals: + void messageRecieved(QJsonObject command); + +}; + +#endif // QKAFKACONSUMER_H diff --git a/PhaseCompAcq/common/utils/SettingConfig.cpp b/PhaseCompAcq/common/utils/SettingConfig.cpp index ed02ac1..1c15f63 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.cpp +++ b/PhaseCompAcq/common/utils/SettingConfig.cpp @@ -12,6 +12,7 @@ KAFKA_DATA_TOPIC = getProperty("kafka", "dataTopic").toString(); KAFKA_STATUS_TOPIC = getProperty("kafka", "statusTopic").toString(); KAFKA_PERFORM_TOPIC = getProperty("kafka", "performTopic").toString(); + KAFKA_CMD_TOPIC = getProperty("kafka", "cmdTopic").toString(); NEED_SASL = getProperty("kafka", "needSasl").toInt(); SASL_USERNAME = getProperty("kafka", "saslUsername").toString(); SASL_PASSWORD = getProperty("kafka", "saslPassword").toString(); diff --git a/PhaseCompAcq/common/utils/SettingConfig.h b/PhaseCompAcq/common/utils/SettingConfig.h index 1d2749a..4d13dad 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.h +++ b/PhaseCompAcq/common/utils/SettingConfig.h @@ -34,6 +34,7 @@ QString KAFKA_DATA_TOPIC; QString KAFKA_STATUS_TOPIC; QString KAFKA_PERFORM_TOPIC; + QString KAFKA_CMD_TOPIC; int NEED_SASL; QString SASL_USERNAME; QString SASL_PASSWORD; diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index f42eba8..7bce7ac 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -196,7 +196,7 @@ // 绑定显示函数 connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); - // 查询计数器的通道 并初始化串口 + // 查询计数器的通道 this->getChannelList(deviceId); } diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 0a907f1..9ac4497 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -64,45 +64,6 @@ return resultObj; } -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { diff --git a/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index a37122b..c84ad21 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -19,7 +19,6 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 77975b3..de50156 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -21,8 +21,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置stackWidget的大小 @@ -84,11 +83,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void DeviceHubWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void DeviceHubWindow::on_devTypeSelect_currentIndexChanged(int index) { ui->stackedWidget->setCurrentIndex(index); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index 39443a8..3b4c10a 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -50,7 +50,6 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); void on_devTypeSelect_currentIndexChanged(int index); void on_devSelect_currentIndexChanged(int index); diff --git a/DeviceHub/DeviceHubWindow.ui b/DeviceHub/DeviceHubWindow.ui index cc6ccd0..70c0618 100644 --- a/DeviceHub/DeviceHubWindow.ui +++ b/DeviceHub/DeviceHubWindow.ui @@ -77,22 +77,6 @@ - - - - 1030 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 9c613db..d0e8a20 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -51,6 +51,10 @@ { this->devCode = devCode; } +QString PhaseDevice::getDeviceId() +{ + return this->deviceId; +} void PhaseDevice::setDeviceId(QString deviceId) { this->deviceId = deviceId; @@ -61,6 +65,11 @@ return this->serialUtil.isOpen(); } +void PhaseDevice::clearChannelPhaseData(int channelNo) +{ + this->phaseVector[channelNo - 1].clear(); +} + void PhaseDevice::initSerialPort() { int master = SettingConfig::getInstance().MASTER; @@ -87,6 +96,11 @@ this->serialUtil.sendData(startCmd.toLocal8Bit()); QThread::msleep(100); } + + // 开始计算后清除累积的相位数据和准确度计算结果 + for (int i = 0; i < phaseVector.size(); i++) { + phaseVector[i].clear(); + } } void PhaseDevice::stopWork() diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 7531798..06a6b0f 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -27,8 +27,11 @@ void setBaudRate(int baudRate); QString getDevCode(); void setDevCode(QString devCode); + QString getDeviceId(); void setDeviceId(QString deviceId); + void clearChannelPhaseData(int channelNo); + bool isSerialOpen(); private: QString deviceId; diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 51673f5..90db100 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -27,10 +27,9 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); - ui->endButt->move(screenRect.width() - 200, 10); - ui->startButt->move(screenRect.width() - 260, 10); + ui->minButt->move(screenRect.width() - 80, 10); + ui->endButt->move(screenRect.width() - 140, 10); + ui->startButt->move(screenRect.width() - 200, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -39,6 +38,16 @@ ui->logo->setText(QString("相位测量数据采集软件——%1").arg(SettingConfig::getInstance().MASTER == 1 ? "主" : "备")); + if (SettingConfig::getInstance().NEED_KAFKA == 1) { + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_CMD_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &PhaseWindow::onPhaseCommandReceived); + } + httpReq = new HttpRequestController(this); // 1. 获取访问接口需要的token int retCode = this->initHttpToken(); @@ -49,44 +58,17 @@ QTimer::singleShot(1000, qApp, SLOT(quit())); } // 2. 获取字典值:设备类型 - retCode = this->initDictDeviceTypes(); // 3. 获取比相仪设备列表 - QJsonObject devListRes = this->initDeviceList(); - if (devListRes.find("code")->toInt() == 200) - { - ui->devSelect->clear(); + initDeviceList(); - // 4. 将获取到的设备添加到下拉列表框中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - QJsonObject devItem = devArray.at(i).toObject(); - ui->devSelect->addItem(devItem.find("deviceName")->toString(), devItem.find("deviceNo")->toString()); + // 4. 打开串口 + for (int i = 0; i < deviceList.size(); i++) { + PhaseDevice * devicePtr = deviceList.at(i); + devicePtr->initSerialPort(); + devicePtr->startWork(); - PhaseDevice * device = new PhaseDevice(this); - deviceList.append(device); - - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceId(devItem.find("deviceId")->toString()); - - connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); - connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); - - device->initSerialPort(); - - QThread::msleep(100); - device->startWork(); - } - // 5. 设置下拉框的样式 - QStandardItemModel * model = qobject_cast(ui->devSelect->model()); - for (int i = 0; i < model->rowCount(); ++i) - { - QStandardItem * item = model->item(i); - item->setSizeHint({ 0, 30 }); - } + QThread::msleep(100); } // 6. 绘制一个设备的多个通道数据面板 @@ -316,15 +298,69 @@ SettingConfig::getInstance().APP_KEY); return response.find("code")->toInt(); } -int PhaseWindow::initDictDeviceTypes() + +void PhaseWindow::initDeviceList() { - QJsonObject response = httpReq->initDictDeviceType(); - return response.find("code")->toInt(); + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.find("code")->toInt() == 200) + { + ui->devSelect->clear(); + + // 4. 将获取到的设备添加到下拉列表框中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) + { + QJsonObject devItem = devArray.at(i).toObject(); + ui->devSelect->addItem(devItem.value("deviceName").toString(), devItem.value("deviceNo").toString()); + + QString deviceId = devItem.value("deviceId").toString(); + + PhaseDevice * device = new PhaseDevice(this); + deviceList.append(device); + + ConstCache::getInstance().deviceMap.insert(deviceId, device); + + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceId(deviceId); + + connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); + connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); + + this->getChannelList(deviceId); + } + // 5. 设置下拉框的样式 + QStandardItemModel * model = qobject_cast(ui->devSelect->model()); + for (int i = 0; i < model->rowCount(); ++i) + { + QStandardItem * item = model->item(i); + item->setSizeHint({ 0, 30 }); + } + } } -QJsonObject PhaseWindow::initDeviceList() + +void PhaseWindow::getChannelList(QString deviceId) { - QJsonObject response = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - return response; + // 查询计数器的通道 + QJsonObject response = httpReq->getPhaseChannelList(deviceId); + if (response.value("code").toInt() == 200) + { + // 将通道绘制在界面上 + QJsonArray channelArray = response.value("data").toArray(); + for (int i = 0; i < channelArray.size(); i++) + { + // 接口返回的channel对象 + QJsonObject channelItem = channelArray.at(i).toObject(); + QString channelId = channelItem.value("id").toString(); // 通道ID + int channelNo = channelItem.value("channelNo").toInt(); + + // 添加到通道的集合中 key=channelId value=deviceId-channelNo + if (channelNo > 0) { + ConstCache::getInstance().channelMap.insert(channelId, QString("%1-%2").arg(deviceId).arg(channelNo)); + } + } + } } void PhaseWindow::on_minButt_clicked() @@ -332,11 +368,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void PhaseWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void PhaseWindow::on_startButt_clicked() { int devSelected = ui->devSelect->currentIndex(); @@ -385,3 +416,46 @@ ((QLabel *)channelBox->children().at(20))->setText(QString("(%1)").arg(0)); // tau=10000 Count } } + +void PhaseWindow::onPhaseCommandReceived(QJsonObject command) +{ + // 缺少必须的字段deviceId 或 cmd 直接返回 + if (command.contains("deviceId") == false || command.contains("cmd") == false) { + return; + } + + QString deviceId = command.value("deviceId").toString(); + QString cmdType = command.value("cmd").toString(); + + // deviceId不属于子系统内的比相仪 直接返回 + if (ConstCache::getInstance().deviceMap.contains(deviceId) == false) { + return; + } + + if (cmdType.toUpper() == "START" || cmdType.toUpper() == "STOP") + { + // 设备开始采集或者停止采集指令 + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (devicePtr != nullptr) { + if (cmdType.toUpper() == "START") { + devicePtr->startWork(); + } else if (cmdType.toUpper() == "STOP") { + devicePtr->stopWork(); + } + } + } else if (cmdType.toUpper() == "CLEAR") + { + // 清除通道的数据 通道级指令 + QString channelId = command.value("channelId").toString(); + if (ConstCache::getInstance().channelMap.contains(channelId) == true) { + QString channelIndex = ConstCache::getInstance().channelMap.value(channelId); + QString channelDevId = channelIndex.split("-").at(0); + int channelNo = ((QString) channelIndex.split("-").at(1)).toInt(); + + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(channelDevId); + if (devicePtr != nullptr) { + devicePtr->clearChannelPhaseData(channelNo); + } + } + } +} diff --git a/PhaseCompAcq/PhaseWindow.h b/PhaseCompAcq/PhaseWindow.h index 572bbde..961e0b1 100644 --- a/PhaseCompAcq/PhaseWindow.h +++ b/PhaseCompAcq/PhaseWindow.h @@ -8,6 +8,7 @@ #include "PhaseDevice.h" #include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaConsumer.h" #include "common/HttpRequestController.h" #include "common/HttpServer.h" @@ -30,21 +31,22 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); - void on_startButt_clicked(); void on_endButt_clicked(); void on_devSelect_currentIndexChanged(int index); + void onPhaseCommandReceived(QJsonObject command); + private: int initHttpToken(); - int initDictDeviceTypes(); - QJsonObject initDeviceList(); + void initDeviceList(); + void getChannelList(QString deviceId); Ui::PhaseWindow *ui; + QKafkaConsumer * kafkaConsumer; HttpRequestController * httpReq; QList deviceList; diff --git a/PhaseCompAcq/PhaseWindow.ui b/PhaseCompAcq/PhaseWindow.ui index d083fb9..9f9456c 100644 --- a/PhaseCompAcq/PhaseWindow.ui +++ b/PhaseCompAcq/PhaseWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/common/ConstCache.h b/PhaseCompAcq/common/ConstCache.h index 6cc831b..0379863 100644 --- a/PhaseCompAcq/common/ConstCache.h +++ b/PhaseCompAcq/common/ConstCache.h @@ -5,6 +5,7 @@ #include #include #include +#include "PhaseDevice.h" class ConstCache : public QObject { @@ -21,6 +22,8 @@ QMap deviceTypes; QList deviceList; + QMap deviceMap; // key=deviceId + QMap channelMap; // key=channelId value=deviceId-no private: ConstCache() {}; diff --git a/PhaseCompAcq/common/HttpRequestController.cpp b/PhaseCompAcq/common/HttpRequestController.cpp index 0a9f3b5..359a0fc 100644 --- a/PhaseCompAcq/common/HttpRequestController.cpp +++ b/PhaseCompAcq/common/HttpRequestController.cpp @@ -1,4 +1,4 @@ -#include "HttpRequestController.h" +#include "HttpRequestController.h" HttpRequestController::HttpRequestController(QObject *parent) : QObject(parent) { @@ -14,6 +14,8 @@ // 获取token的url地址 QUrl url = baseUrl + "/getTokenByClientId"; + qDebug() << url.toString(); + // 请求对象 QNetworkRequest request; request.setUrl(url); @@ -55,105 +57,14 @@ resultObj.insert("code", -1); } - return resultObj; -} - -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} - -QJsonObject HttpRequestController::initDeviceList(QString devType) -{ - QJsonObject resultObj; - - QString counterDevType = ""; - QMapIterator it(ConstCache::getInstance().deviceTypes); - while (it.hasNext()) - { - it.next(); - if (it.value().contains(devType) == true) - { - counterDevType = it.key(); - break; - } - } - - // 获取设备列表的接口地址 - QUrl url = baseUrl + "/device/list"; - QUrlQuery query; - query.addQueryItem("type", counterDevType); - url.setQuery(query); - - QNetworkRequest request; - request.setUrl(url); - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - request.setRawHeader("system", system.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceList.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceList.append(item); - } - } - } else - { - resultObj.insert("code", -1); - } + resultObj.insert("code", 200); // qDebug() << resultObj; return resultObj; } + QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { QJsonObject resultObj; @@ -187,3 +98,36 @@ return resultObj; } + +QJsonObject HttpRequestController::getPhaseChannelList(QString deviceId) +{ + QJsonObject resultObj; + + // 获取设备列表的接口地址 + QUrl url = baseUrl + "/channel/list"; + QUrlQuery query; + query.addQueryItem("deviceId", deviceId); + url.setQuery(query); + + QNetworkRequest request; + request.setUrl(url); + request.setRawHeader("Content-type", "application/json"); + request.setRawHeader("token", token.toLocal8Bit()); + + qDebug() << url; + + QNetworkReply * reply = httpUtil->sendGetRequest(request); + const QByteArray reply_data = reply->readAll(); + QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); + if(jsonDocument.isNull() == false) + { + resultObj = jsonDocument.object(); + } else + { + resultObj.insert("code", -1); + } + + qDebug() << resultObj; + + return resultObj; +} diff --git a/PhaseCompAcq/common/HttpRequestController.h b/PhaseCompAcq/common/HttpRequestController.h index 2610548..352a769 100644 --- a/PhaseCompAcq/common/HttpRequestController.h +++ b/PhaseCompAcq/common/HttpRequestController.h @@ -1,7 +1,9 @@ -#ifndef HTTPREQUESTCONTROLLER_H +#ifndef HTTPREQUESTCONTROLLER_H #define HTTPREQUESTCONTROLLER_H #include +#include +#include #include #include "utils/HttpRequestUtil.h" @@ -17,9 +19,8 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); - QJsonObject initDeviceList(QString devType); QJsonObject initDeviceList(QString devType, QString system); + QJsonObject getPhaseChannelList(QString deviceId); private: HttpRequestUtil * httpUtil; diff --git a/PhaseCompAcq/common/common.pri b/PhaseCompAcq/common/common.pri index b80a640..01ba6ee 100644 --- a/PhaseCompAcq/common/common.pri +++ b/PhaseCompAcq/common/common.pri @@ -4,6 +4,7 @@ SOURCES += $$PWD/utils/QSerialPortUtil.cpp SOURCES += $$PWD/utils/QLogUtil.cpp SOURCES += $$PWD/utils/QKafkaUtil.cpp +SOURCES += $$PWD/utils/QKafkaConsumer.cpp SOURCES += $$PWD/utils/HttpRequestUtil.cpp SOURCES += $$PWD/utils/MD5.cpp SOURCES += $$PWD/HttpRequestController.cpp @@ -14,6 +15,7 @@ HEADERS += $$PWD/utils/QSerialPortUtil.h HEADERS += $$PWD/utils/QLogUtil.h HEADERS += $$PWD/utils/QKafkaUtil.h +HEADERS += $$PWD/utils/QKafkaConsumer.h HEADERS += $$PWD/utils/HttpRequestUtil.h HEADERS += $$PWD/utils/DefHead.h HEADERS += $$PWD/utils/MD5.h diff --git a/PhaseCompAcq/common/utils/QKafkaConsumer.cpp b/PhaseCompAcq/common/utils/QKafkaConsumer.cpp new file mode 100644 index 0000000..cb4fe37 --- /dev/null +++ b/PhaseCompAcq/common/utils/QKafkaConsumer.cpp @@ -0,0 +1,138 @@ +#include "QKafkaConsumer.h" +#include "SettingConfig.h" +#include +#include + +static volatile int runFlag = 1; +static bool exit_eof = false; + +QKafkaConsumer::QKafkaConsumer(QObject *parent) : QThread(parent) +{ + this->conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL); + this->tconf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC); + + conf->set("enable.partition.eof", "true", errStr); +} + +void QKafkaConsumer::setBrokers(QString brokers) +{ + this->brokers = brokers; +} +void QKafkaConsumer::setTopic(QString topic) +{ + this->topic = topic; +} + +int QKafkaConsumer::createConsumer() +{ + int ret = conf->set("metadata.broker.list", brokers.toStdString(), errStr); + if (SettingConfig::getInstance().NEED_SASL == 1) + { + conf->set("sasl.username", SettingConfig::getInstance().SASL_USERNAME.toStdString(), errStr); + conf->set("sasl.password", SettingConfig::getInstance().SASL_PASSWORD.toStdString(), errStr); + conf->set("security.protocol", "sasl_plaintext", errStr); + conf->set("sasl.mechanisms", "PLAIN", errStr); + } + + if (ret != RdKafka::Conf::CONF_OK) + { + std::cerr << "RdKafka conf set brokerlist failed :" << errStr.c_str() << std::endl; + } + + consumer = RdKafka::Consumer::create(conf, errStr); + if (!consumer) { + std::cerr << "Failed to create consumer: " << errStr << std::endl; + return -1; + } + + std::cout << "% Created consumer " << consumer->name() << std::endl; + + return 1; +} + +void QKafkaConsumer::run() +{ + RdKafka::Topic * topic = RdKafka::Topic::create(consumer, this->topic.toStdString(), tconf, errStr); + if (!topic) { + std::cerr << "Failed to create topic: " << errStr << std::endl; + } + + RdKafka::ErrorCode resp = consumer->start(topic, 0, RdKafka::Topic::OFFSET_END); + if (resp != RdKafka::ERR_NO_ERROR) { + std::cerr << "Failed to start consumer: " << RdKafka::err2str(resp) << std::endl; + } + + while (runFlag) + { + RdKafka::Message * message = consumer->consume(topic, 0, 200); + messageConsume(message); + } +} + +void QKafkaConsumer::exitThread() +{ + runFlag = false; +} + +void QKafkaConsumer::messageConsume(RdKafka::Message* message) { + const RdKafka::Headers *headers; + + switch (message->err()) { + case RdKafka::ERR__TIMED_OUT: + break; + + case RdKafka::ERR_NO_ERROR: + { + /* Real message */ +// std::cout << "Read msg at offset " << message->offset() << std::endl; +// printf("%.*s\n", static_cast(message->len()), static_cast(message->payload())); + + QString messageStr = static_cast(message->payload()); + QJsonParseError jsonErr; + QJsonDocument doc = QJsonDocument::fromJson(messageStr.toUtf8(), &jsonErr); + if (jsonErr.error == QJsonParseError::NoError) + { + QJsonObject obj = doc.object(); + obj.insert("cmdStr", messageStr); + emit messageRecieved(obj); + } + + if (message->key()) { + std::cout << "Key: " << *message->key() << std::endl; + } + headers = message->headers(); + if (headers) { + std::vector hdrs = headers->get_all(); + for (size_t i = 0 ; i < hdrs.size() ; i++) { + const RdKafka::Headers::Header hdr = hdrs[i]; + + if (hdr.value() != NULL) + printf(" Header: %s = \"%.*s\"\n", + hdr.key().c_str(), + (int)hdr.value_size(), (const char *)hdr.value()); + else + printf(" Header: %s = NULL\n", hdr.key().c_str()); + } + } + break; + } + + case RdKafka::ERR__PARTITION_EOF: + /* Last message */ + if (exit_eof) { + runFlag = 0; + } + break; + + case RdKafka::ERR__UNKNOWN_TOPIC: + case RdKafka::ERR__UNKNOWN_PARTITION: + std::cerr << "Consume failed: " << message->errstr() << std::endl; + runFlag = 0; + break; + + default: + /* Errors */ + std::cerr << "Consume failed: " << message->errstr() << std::endl; + runFlag = 0; + } +} diff --git a/PhaseCompAcq/common/utils/QKafkaConsumer.h b/PhaseCompAcq/common/utils/QKafkaConsumer.h new file mode 100644 index 0000000..84f4702 --- /dev/null +++ b/PhaseCompAcq/common/utils/QKafkaConsumer.h @@ -0,0 +1,40 @@ +#ifndef QKAFKACONSUMER_H +#define QKAFKACONSUMER_H + +#include +#include + +#include "include/librdkafka/rdkafkacpp.h" + +class QKafkaConsumer : public QThread +{ + Q_OBJECT +public: + explicit QKafkaConsumer(QObject *parent = nullptr); + + void setBrokers(QString brokers); + void setTopic(QString topic); + + int createConsumer(); + void run(); + void exitThread(); + + void messageConsume(RdKafka::Message * message); + +private: + QString brokers; + QString topic; + + std::string errStr; + + RdKafka::Conf * conf; + RdKafka::Conf * tconf; + + RdKafka::Consumer * consumer = 0; + +signals: + void messageRecieved(QJsonObject command); + +}; + +#endif // QKAFKACONSUMER_H diff --git a/PhaseCompAcq/common/utils/SettingConfig.cpp b/PhaseCompAcq/common/utils/SettingConfig.cpp index ed02ac1..1c15f63 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.cpp +++ b/PhaseCompAcq/common/utils/SettingConfig.cpp @@ -12,6 +12,7 @@ KAFKA_DATA_TOPIC = getProperty("kafka", "dataTopic").toString(); KAFKA_STATUS_TOPIC = getProperty("kafka", "statusTopic").toString(); KAFKA_PERFORM_TOPIC = getProperty("kafka", "performTopic").toString(); + KAFKA_CMD_TOPIC = getProperty("kafka", "cmdTopic").toString(); NEED_SASL = getProperty("kafka", "needSasl").toInt(); SASL_USERNAME = getProperty("kafka", "saslUsername").toString(); SASL_PASSWORD = getProperty("kafka", "saslPassword").toString(); diff --git a/PhaseCompAcq/common/utils/SettingConfig.h b/PhaseCompAcq/common/utils/SettingConfig.h index 1d2749a..4d13dad 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.h +++ b/PhaseCompAcq/common/utils/SettingConfig.h @@ -34,6 +34,7 @@ QString KAFKA_DATA_TOPIC; QString KAFKA_STATUS_TOPIC; QString KAFKA_PERFORM_TOPIC; + QString KAFKA_CMD_TOPIC; int NEED_SASL; QString SASL_USERNAME; QString SASL_PASSWORD; diff --git a/PhaseCompAcq/conf/config.ini b/PhaseCompAcq/conf/config.ini index e7dc133..8b55919 100644 --- a/PhaseCompAcq/conf/config.ini +++ b/PhaseCompAcq/conf/config.ini @@ -7,6 +7,7 @@ dataTopic="phase-data" statusTopic="dev-status" performTopic="perform-data" +cmdTopic="phase-command" needSasl=1 saslUsername="admin" saslPassword="casicss" diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 9ffbaa8..0b60d21 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -27,8 +27,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -214,11 +213,6 @@ return response; } -void CounterWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void CounterWindow::on_minButt_clicked() { setWindowState(Qt::WindowMinimized | windowState()); diff --git a/CounterAcq/CounterWindow.h b/CounterAcq/CounterWindow.h index 084bb06..ec8c3f2 100644 --- a/CounterAcq/CounterWindow.h +++ b/CounterAcq/CounterWindow.h @@ -27,8 +27,6 @@ void drawCounterDataOnPage(CounterDataDto * counterData); private slots: - void on_exitButt_clicked(); - void on_minButt_clicked(); private: diff --git a/CounterAcq/CounterWindow.ui b/CounterAcq/CounterWindow.ui index 40c8335..659d15b 100644 --- a/CounterAcq/CounterWindow.ui +++ b/CounterAcq/CounterWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index f42eba8..7bce7ac 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -196,7 +196,7 @@ // 绑定显示函数 connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); - // 查询计数器的通道 并初始化串口 + // 查询计数器的通道 this->getChannelList(deviceId); } diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 0a907f1..9ac4497 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -64,45 +64,6 @@ return resultObj; } -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { diff --git a/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index a37122b..c84ad21 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -19,7 +19,6 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 77975b3..de50156 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -21,8 +21,7 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); + ui->minButt->move(screenRect.width() - 80, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置stackWidget的大小 @@ -84,11 +83,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void DeviceHubWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void DeviceHubWindow::on_devTypeSelect_currentIndexChanged(int index) { ui->stackedWidget->setCurrentIndex(index); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index 39443a8..3b4c10a 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -50,7 +50,6 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); void on_devTypeSelect_currentIndexChanged(int index); void on_devSelect_currentIndexChanged(int index); diff --git a/DeviceHub/DeviceHubWindow.ui b/DeviceHub/DeviceHubWindow.ui index cc6ccd0..70c0618 100644 --- a/DeviceHub/DeviceHubWindow.ui +++ b/DeviceHub/DeviceHubWindow.ui @@ -77,22 +77,6 @@ - - - - 1030 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 9c613db..d0e8a20 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -51,6 +51,10 @@ { this->devCode = devCode; } +QString PhaseDevice::getDeviceId() +{ + return this->deviceId; +} void PhaseDevice::setDeviceId(QString deviceId) { this->deviceId = deviceId; @@ -61,6 +65,11 @@ return this->serialUtil.isOpen(); } +void PhaseDevice::clearChannelPhaseData(int channelNo) +{ + this->phaseVector[channelNo - 1].clear(); +} + void PhaseDevice::initSerialPort() { int master = SettingConfig::getInstance().MASTER; @@ -87,6 +96,11 @@ this->serialUtil.sendData(startCmd.toLocal8Bit()); QThread::msleep(100); } + + // 开始计算后清除累积的相位数据和准确度计算结果 + for (int i = 0; i < phaseVector.size(); i++) { + phaseVector[i].clear(); + } } void PhaseDevice::stopWork() diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 7531798..06a6b0f 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -27,8 +27,11 @@ void setBaudRate(int baudRate); QString getDevCode(); void setDevCode(QString devCode); + QString getDeviceId(); void setDeviceId(QString deviceId); + void clearChannelPhaseData(int channelNo); + bool isSerialOpen(); private: QString deviceId; diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 51673f5..90db100 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -27,10 +27,9 @@ // 将窗口移动到左上角 move(0, 0); - ui->exitButt->move(screenRect.width() - 80, 10); - ui->minButt->move(screenRect.width() - 140, 10); - ui->endButt->move(screenRect.width() - 200, 10); - ui->startButt->move(screenRect.width() - 260, 10); + ui->minButt->move(screenRect.width() - 80, 10); + ui->endButt->move(screenRect.width() - 140, 10); + ui->startButt->move(screenRect.width() - 200, 10); ui->line->setGeometry(0, 59, screenRect.width(), 1); // 设置主体区域的大小和位置 @@ -39,6 +38,16 @@ ui->logo->setText(QString("相位测量数据采集软件——%1").arg(SettingConfig::getInstance().MASTER == 1 ? "主" : "备")); + if (SettingConfig::getInstance().NEED_KAFKA == 1) { + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_CMD_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &PhaseWindow::onPhaseCommandReceived); + } + httpReq = new HttpRequestController(this); // 1. 获取访问接口需要的token int retCode = this->initHttpToken(); @@ -49,44 +58,17 @@ QTimer::singleShot(1000, qApp, SLOT(quit())); } // 2. 获取字典值:设备类型 - retCode = this->initDictDeviceTypes(); // 3. 获取比相仪设备列表 - QJsonObject devListRes = this->initDeviceList(); - if (devListRes.find("code")->toInt() == 200) - { - ui->devSelect->clear(); + initDeviceList(); - // 4. 将获取到的设备添加到下拉列表框中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - QJsonObject devItem = devArray.at(i).toObject(); - ui->devSelect->addItem(devItem.find("deviceName")->toString(), devItem.find("deviceNo")->toString()); + // 4. 打开串口 + for (int i = 0; i < deviceList.size(); i++) { + PhaseDevice * devicePtr = deviceList.at(i); + devicePtr->initSerialPort(); + devicePtr->startWork(); - PhaseDevice * device = new PhaseDevice(this); - deviceList.append(device); - - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceId(devItem.find("deviceId")->toString()); - - connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); - connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); - - device->initSerialPort(); - - QThread::msleep(100); - device->startWork(); - } - // 5. 设置下拉框的样式 - QStandardItemModel * model = qobject_cast(ui->devSelect->model()); - for (int i = 0; i < model->rowCount(); ++i) - { - QStandardItem * item = model->item(i); - item->setSizeHint({ 0, 30 }); - } + QThread::msleep(100); } // 6. 绘制一个设备的多个通道数据面板 @@ -316,15 +298,69 @@ SettingConfig::getInstance().APP_KEY); return response.find("code")->toInt(); } -int PhaseWindow::initDictDeviceTypes() + +void PhaseWindow::initDeviceList() { - QJsonObject response = httpReq->initDictDeviceType(); - return response.find("code")->toInt(); + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.find("code")->toInt() == 200) + { + ui->devSelect->clear(); + + // 4. 将获取到的设备添加到下拉列表框中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) + { + QJsonObject devItem = devArray.at(i).toObject(); + ui->devSelect->addItem(devItem.value("deviceName").toString(), devItem.value("deviceNo").toString()); + + QString deviceId = devItem.value("deviceId").toString(); + + PhaseDevice * device = new PhaseDevice(this); + deviceList.append(device); + + ConstCache::getInstance().deviceMap.insert(deviceId, device); + + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceId(deviceId); + + connect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); + connect(device, &PhaseDevice::sendAllenToDraw, this, &PhaseWindow::drawPhaseAllenOnPage); + + this->getChannelList(deviceId); + } + // 5. 设置下拉框的样式 + QStandardItemModel * model = qobject_cast(ui->devSelect->model()); + for (int i = 0; i < model->rowCount(); ++i) + { + QStandardItem * item = model->item(i); + item->setSizeHint({ 0, 30 }); + } + } } -QJsonObject PhaseWindow::initDeviceList() + +void PhaseWindow::getChannelList(QString deviceId) { - QJsonObject response = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - return response; + // 查询计数器的通道 + QJsonObject response = httpReq->getPhaseChannelList(deviceId); + if (response.value("code").toInt() == 200) + { + // 将通道绘制在界面上 + QJsonArray channelArray = response.value("data").toArray(); + for (int i = 0; i < channelArray.size(); i++) + { + // 接口返回的channel对象 + QJsonObject channelItem = channelArray.at(i).toObject(); + QString channelId = channelItem.value("id").toString(); // 通道ID + int channelNo = channelItem.value("channelNo").toInt(); + + // 添加到通道的集合中 key=channelId value=deviceId-channelNo + if (channelNo > 0) { + ConstCache::getInstance().channelMap.insert(channelId, QString("%1-%2").arg(deviceId).arg(channelNo)); + } + } + } } void PhaseWindow::on_minButt_clicked() @@ -332,11 +368,6 @@ setWindowState(Qt::WindowMinimized | windowState()); } -void PhaseWindow::on_exitButt_clicked() -{ - QApplication::exit(0); -} - void PhaseWindow::on_startButt_clicked() { int devSelected = ui->devSelect->currentIndex(); @@ -385,3 +416,46 @@ ((QLabel *)channelBox->children().at(20))->setText(QString("(%1)").arg(0)); // tau=10000 Count } } + +void PhaseWindow::onPhaseCommandReceived(QJsonObject command) +{ + // 缺少必须的字段deviceId 或 cmd 直接返回 + if (command.contains("deviceId") == false || command.contains("cmd") == false) { + return; + } + + QString deviceId = command.value("deviceId").toString(); + QString cmdType = command.value("cmd").toString(); + + // deviceId不属于子系统内的比相仪 直接返回 + if (ConstCache::getInstance().deviceMap.contains(deviceId) == false) { + return; + } + + if (cmdType.toUpper() == "START" || cmdType.toUpper() == "STOP") + { + // 设备开始采集或者停止采集指令 + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (devicePtr != nullptr) { + if (cmdType.toUpper() == "START") { + devicePtr->startWork(); + } else if (cmdType.toUpper() == "STOP") { + devicePtr->stopWork(); + } + } + } else if (cmdType.toUpper() == "CLEAR") + { + // 清除通道的数据 通道级指令 + QString channelId = command.value("channelId").toString(); + if (ConstCache::getInstance().channelMap.contains(channelId) == true) { + QString channelIndex = ConstCache::getInstance().channelMap.value(channelId); + QString channelDevId = channelIndex.split("-").at(0); + int channelNo = ((QString) channelIndex.split("-").at(1)).toInt(); + + PhaseDevice * devicePtr = ConstCache::getInstance().deviceMap.value(channelDevId); + if (devicePtr != nullptr) { + devicePtr->clearChannelPhaseData(channelNo); + } + } + } +} diff --git a/PhaseCompAcq/PhaseWindow.h b/PhaseCompAcq/PhaseWindow.h index 572bbde..961e0b1 100644 --- a/PhaseCompAcq/PhaseWindow.h +++ b/PhaseCompAcq/PhaseWindow.h @@ -8,6 +8,7 @@ #include "PhaseDevice.h" #include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaConsumer.h" #include "common/HttpRequestController.h" #include "common/HttpServer.h" @@ -30,21 +31,22 @@ private slots: void on_minButt_clicked(); - void on_exitButt_clicked(); - void on_startButt_clicked(); void on_endButt_clicked(); void on_devSelect_currentIndexChanged(int index); + void onPhaseCommandReceived(QJsonObject command); + private: int initHttpToken(); - int initDictDeviceTypes(); - QJsonObject initDeviceList(); + void initDeviceList(); + void getChannelList(QString deviceId); Ui::PhaseWindow *ui; + QKafkaConsumer * kafkaConsumer; HttpRequestController * httpReq; QList deviceList; diff --git a/PhaseCompAcq/PhaseWindow.ui b/PhaseCompAcq/PhaseWindow.ui index d083fb9..9f9456c 100644 --- a/PhaseCompAcq/PhaseWindow.ui +++ b/PhaseCompAcq/PhaseWindow.ui @@ -83,22 +83,6 @@ Qt::AlignCenter - - - - 880 - 10 - 40 - 40 - - - - - - - 退出 - - diff --git a/PhaseCompAcq/common/ConstCache.h b/PhaseCompAcq/common/ConstCache.h index 6cc831b..0379863 100644 --- a/PhaseCompAcq/common/ConstCache.h +++ b/PhaseCompAcq/common/ConstCache.h @@ -5,6 +5,7 @@ #include #include #include +#include "PhaseDevice.h" class ConstCache : public QObject { @@ -21,6 +22,8 @@ QMap deviceTypes; QList deviceList; + QMap deviceMap; // key=deviceId + QMap channelMap; // key=channelId value=deviceId-no private: ConstCache() {}; diff --git a/PhaseCompAcq/common/HttpRequestController.cpp b/PhaseCompAcq/common/HttpRequestController.cpp index 0a9f3b5..359a0fc 100644 --- a/PhaseCompAcq/common/HttpRequestController.cpp +++ b/PhaseCompAcq/common/HttpRequestController.cpp @@ -1,4 +1,4 @@ -#include "HttpRequestController.h" +#include "HttpRequestController.h" HttpRequestController::HttpRequestController(QObject *parent) : QObject(parent) { @@ -14,6 +14,8 @@ // 获取token的url地址 QUrl url = baseUrl + "/getTokenByClientId"; + qDebug() << url.toString(); + // 请求对象 QNetworkRequest request; request.setUrl(url); @@ -55,105 +57,14 @@ resultObj.insert("code", -1); } - return resultObj; -} - -QJsonObject HttpRequestController::initDictDeviceType() -{ - QJsonObject resultObj; - - // 获取字典值的接口地址 - QUrl url = baseUrl + "/sys/dict/code/deviceType"; - - // - QNetworkRequest request; - request.setUrl(url); - - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceTypes.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceTypes.insert(item.find("value")->toString(), item.find("name")->toString()); - } - } - } else - { - resultObj.insert("code", -1); - } - - return resultObj; -} - -QJsonObject HttpRequestController::initDeviceList(QString devType) -{ - QJsonObject resultObj; - - QString counterDevType = ""; - QMapIterator it(ConstCache::getInstance().deviceTypes); - while (it.hasNext()) - { - it.next(); - if (it.value().contains(devType) == true) - { - counterDevType = it.key(); - break; - } - } - - // 获取设备列表的接口地址 - QUrl url = baseUrl + "/device/list"; - QUrlQuery query; - query.addQueryItem("type", counterDevType); - url.setQuery(query); - - QNetworkRequest request; - request.setUrl(url); - request.setRawHeader("Content-type", "application/json"); - request.setRawHeader("token", token.toLocal8Bit()); - request.setRawHeader("system", system.toLocal8Bit()); - - QNetworkReply * reply = httpUtil->sendGetRequest(request); - const QByteArray reply_data = reply->readAll(); - QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); - if(jsonDocument.isNull() == false) { - resultObj = jsonDocument.object(); - - if (resultObj.find("code")->toInt() == 200) - { - ConstCache::getInstance().deviceList.clear(); - - QJsonArray data = resultObj.find("data")->toArray(); - for (int i = 0; i < data.size(); i++) - { - QJsonObject item = data.at(i).toObject(); - - ConstCache::getInstance().deviceList.append(item); - } - } - } else - { - resultObj.insert("code", -1); - } + resultObj.insert("code", 200); // qDebug() << resultObj; return resultObj; } + QJsonObject HttpRequestController::initDeviceList(QString devType, QString system) { QJsonObject resultObj; @@ -187,3 +98,36 @@ return resultObj; } + +QJsonObject HttpRequestController::getPhaseChannelList(QString deviceId) +{ + QJsonObject resultObj; + + // 获取设备列表的接口地址 + QUrl url = baseUrl + "/channel/list"; + QUrlQuery query; + query.addQueryItem("deviceId", deviceId); + url.setQuery(query); + + QNetworkRequest request; + request.setUrl(url); + request.setRawHeader("Content-type", "application/json"); + request.setRawHeader("token", token.toLocal8Bit()); + + qDebug() << url; + + QNetworkReply * reply = httpUtil->sendGetRequest(request); + const QByteArray reply_data = reply->readAll(); + QJsonDocument jsonDocument = QJsonDocument::fromJson(reply_data); + if(jsonDocument.isNull() == false) + { + resultObj = jsonDocument.object(); + } else + { + resultObj.insert("code", -1); + } + + qDebug() << resultObj; + + return resultObj; +} diff --git a/PhaseCompAcq/common/HttpRequestController.h b/PhaseCompAcq/common/HttpRequestController.h index 2610548..352a769 100644 --- a/PhaseCompAcq/common/HttpRequestController.h +++ b/PhaseCompAcq/common/HttpRequestController.h @@ -1,7 +1,9 @@ -#ifndef HTTPREQUESTCONTROLLER_H +#ifndef HTTPREQUESTCONTROLLER_H #define HTTPREQUESTCONTROLLER_H #include +#include +#include #include #include "utils/HttpRequestUtil.h" @@ -17,9 +19,8 @@ explicit HttpRequestController(QObject *parent = nullptr); QJsonObject getTokenByClientId(QString clientId, QString key); - QJsonObject initDictDeviceType(); - QJsonObject initDeviceList(QString devType); QJsonObject initDeviceList(QString devType, QString system); + QJsonObject getPhaseChannelList(QString deviceId); private: HttpRequestUtil * httpUtil; diff --git a/PhaseCompAcq/common/common.pri b/PhaseCompAcq/common/common.pri index b80a640..01ba6ee 100644 --- a/PhaseCompAcq/common/common.pri +++ b/PhaseCompAcq/common/common.pri @@ -4,6 +4,7 @@ SOURCES += $$PWD/utils/QSerialPortUtil.cpp SOURCES += $$PWD/utils/QLogUtil.cpp SOURCES += $$PWD/utils/QKafkaUtil.cpp +SOURCES += $$PWD/utils/QKafkaConsumer.cpp SOURCES += $$PWD/utils/HttpRequestUtil.cpp SOURCES += $$PWD/utils/MD5.cpp SOURCES += $$PWD/HttpRequestController.cpp @@ -14,6 +15,7 @@ HEADERS += $$PWD/utils/QSerialPortUtil.h HEADERS += $$PWD/utils/QLogUtil.h HEADERS += $$PWD/utils/QKafkaUtil.h +HEADERS += $$PWD/utils/QKafkaConsumer.h HEADERS += $$PWD/utils/HttpRequestUtil.h HEADERS += $$PWD/utils/DefHead.h HEADERS += $$PWD/utils/MD5.h diff --git a/PhaseCompAcq/common/utils/QKafkaConsumer.cpp b/PhaseCompAcq/common/utils/QKafkaConsumer.cpp new file mode 100644 index 0000000..cb4fe37 --- /dev/null +++ b/PhaseCompAcq/common/utils/QKafkaConsumer.cpp @@ -0,0 +1,138 @@ +#include "QKafkaConsumer.h" +#include "SettingConfig.h" +#include +#include + +static volatile int runFlag = 1; +static bool exit_eof = false; + +QKafkaConsumer::QKafkaConsumer(QObject *parent) : QThread(parent) +{ + this->conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL); + this->tconf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC); + + conf->set("enable.partition.eof", "true", errStr); +} + +void QKafkaConsumer::setBrokers(QString brokers) +{ + this->brokers = brokers; +} +void QKafkaConsumer::setTopic(QString topic) +{ + this->topic = topic; +} + +int QKafkaConsumer::createConsumer() +{ + int ret = conf->set("metadata.broker.list", brokers.toStdString(), errStr); + if (SettingConfig::getInstance().NEED_SASL == 1) + { + conf->set("sasl.username", SettingConfig::getInstance().SASL_USERNAME.toStdString(), errStr); + conf->set("sasl.password", SettingConfig::getInstance().SASL_PASSWORD.toStdString(), errStr); + conf->set("security.protocol", "sasl_plaintext", errStr); + conf->set("sasl.mechanisms", "PLAIN", errStr); + } + + if (ret != RdKafka::Conf::CONF_OK) + { + std::cerr << "RdKafka conf set brokerlist failed :" << errStr.c_str() << std::endl; + } + + consumer = RdKafka::Consumer::create(conf, errStr); + if (!consumer) { + std::cerr << "Failed to create consumer: " << errStr << std::endl; + return -1; + } + + std::cout << "% Created consumer " << consumer->name() << std::endl; + + return 1; +} + +void QKafkaConsumer::run() +{ + RdKafka::Topic * topic = RdKafka::Topic::create(consumer, this->topic.toStdString(), tconf, errStr); + if (!topic) { + std::cerr << "Failed to create topic: " << errStr << std::endl; + } + + RdKafka::ErrorCode resp = consumer->start(topic, 0, RdKafka::Topic::OFFSET_END); + if (resp != RdKafka::ERR_NO_ERROR) { + std::cerr << "Failed to start consumer: " << RdKafka::err2str(resp) << std::endl; + } + + while (runFlag) + { + RdKafka::Message * message = consumer->consume(topic, 0, 200); + messageConsume(message); + } +} + +void QKafkaConsumer::exitThread() +{ + runFlag = false; +} + +void QKafkaConsumer::messageConsume(RdKafka::Message* message) { + const RdKafka::Headers *headers; + + switch (message->err()) { + case RdKafka::ERR__TIMED_OUT: + break; + + case RdKafka::ERR_NO_ERROR: + { + /* Real message */ +// std::cout << "Read msg at offset " << message->offset() << std::endl; +// printf("%.*s\n", static_cast(message->len()), static_cast(message->payload())); + + QString messageStr = static_cast(message->payload()); + QJsonParseError jsonErr; + QJsonDocument doc = QJsonDocument::fromJson(messageStr.toUtf8(), &jsonErr); + if (jsonErr.error == QJsonParseError::NoError) + { + QJsonObject obj = doc.object(); + obj.insert("cmdStr", messageStr); + emit messageRecieved(obj); + } + + if (message->key()) { + std::cout << "Key: " << *message->key() << std::endl; + } + headers = message->headers(); + if (headers) { + std::vector hdrs = headers->get_all(); + for (size_t i = 0 ; i < hdrs.size() ; i++) { + const RdKafka::Headers::Header hdr = hdrs[i]; + + if (hdr.value() != NULL) + printf(" Header: %s = \"%.*s\"\n", + hdr.key().c_str(), + (int)hdr.value_size(), (const char *)hdr.value()); + else + printf(" Header: %s = NULL\n", hdr.key().c_str()); + } + } + break; + } + + case RdKafka::ERR__PARTITION_EOF: + /* Last message */ + if (exit_eof) { + runFlag = 0; + } + break; + + case RdKafka::ERR__UNKNOWN_TOPIC: + case RdKafka::ERR__UNKNOWN_PARTITION: + std::cerr << "Consume failed: " << message->errstr() << std::endl; + runFlag = 0; + break; + + default: + /* Errors */ + std::cerr << "Consume failed: " << message->errstr() << std::endl; + runFlag = 0; + } +} diff --git a/PhaseCompAcq/common/utils/QKafkaConsumer.h b/PhaseCompAcq/common/utils/QKafkaConsumer.h new file mode 100644 index 0000000..84f4702 --- /dev/null +++ b/PhaseCompAcq/common/utils/QKafkaConsumer.h @@ -0,0 +1,40 @@ +#ifndef QKAFKACONSUMER_H +#define QKAFKACONSUMER_H + +#include +#include + +#include "include/librdkafka/rdkafkacpp.h" + +class QKafkaConsumer : public QThread +{ + Q_OBJECT +public: + explicit QKafkaConsumer(QObject *parent = nullptr); + + void setBrokers(QString brokers); + void setTopic(QString topic); + + int createConsumer(); + void run(); + void exitThread(); + + void messageConsume(RdKafka::Message * message); + +private: + QString brokers; + QString topic; + + std::string errStr; + + RdKafka::Conf * conf; + RdKafka::Conf * tconf; + + RdKafka::Consumer * consumer = 0; + +signals: + void messageRecieved(QJsonObject command); + +}; + +#endif // QKAFKACONSUMER_H diff --git a/PhaseCompAcq/common/utils/SettingConfig.cpp b/PhaseCompAcq/common/utils/SettingConfig.cpp index ed02ac1..1c15f63 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.cpp +++ b/PhaseCompAcq/common/utils/SettingConfig.cpp @@ -12,6 +12,7 @@ KAFKA_DATA_TOPIC = getProperty("kafka", "dataTopic").toString(); KAFKA_STATUS_TOPIC = getProperty("kafka", "statusTopic").toString(); KAFKA_PERFORM_TOPIC = getProperty("kafka", "performTopic").toString(); + KAFKA_CMD_TOPIC = getProperty("kafka", "cmdTopic").toString(); NEED_SASL = getProperty("kafka", "needSasl").toInt(); SASL_USERNAME = getProperty("kafka", "saslUsername").toString(); SASL_PASSWORD = getProperty("kafka", "saslPassword").toString(); diff --git a/PhaseCompAcq/common/utils/SettingConfig.h b/PhaseCompAcq/common/utils/SettingConfig.h index 1d2749a..4d13dad 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.h +++ b/PhaseCompAcq/common/utils/SettingConfig.h @@ -34,6 +34,7 @@ QString KAFKA_DATA_TOPIC; QString KAFKA_STATUS_TOPIC; QString KAFKA_PERFORM_TOPIC; + QString KAFKA_CMD_TOPIC; int NEED_SASL; QString SASL_USERNAME; QString SASL_PASSWORD; diff --git a/PhaseCompAcq/conf/config.ini b/PhaseCompAcq/conf/config.ini index e7dc133..8b55919 100644 --- a/PhaseCompAcq/conf/config.ini +++ b/PhaseCompAcq/conf/config.ini @@ -7,6 +7,7 @@ dataTopic="phase-data" statusTopic="dev-status" performTopic="perform-data" +cmdTopic="phase-command" needSasl=1 saslUsername="admin" saslPassword="casicss" diff --git a/ZXSSCJ.pro b/ZXSSCJ.pro index 26983b2..cfcfec9 100644 --- a/ZXSSCJ.pro +++ b/ZXSSCJ.pro @@ -3,12 +3,12 @@ #定义了ordered表示子项目按照添加的顺序来编译 #CONFIG += ordered -SUBDIRS += CounterAcq #计数器数据采集 +#SUBDIRS += CounterAcq #计数器数据采集 #SUBDIRS += CounterAcqBM #6906计数器数据采集 SUBDIRS += CounterRealTime SUBDIRS += PhaseCompAcq #比相仪数据采集 #SUBDIRS += DevStatusAcq -SUBDIRS += DeviceHub # +#SUBDIRS += DeviceHub # #SUBDIRS += HClockAcq #氢钟状态数据采集 #SUBDIRS += ClockUtils #钟差数据处理小工具