diff --git a/CounterRealTime/CounterChannel.cpp b/CounterRealTime/CounterChannel.cpp index 978b969..0a4b534 100644 --- a/CounterRealTime/CounterChannel.cpp +++ b/CounterRealTime/CounterChannel.cpp @@ -30,6 +30,14 @@ { return delays; } +QString CounterChannel::getIsUse() +{ + return isUse; +} +QString CounterChannel::getName() +{ + return name; +} void CounterChannel::setActive(bool active) { this->active = active; @@ -54,14 +62,13 @@ { this->delays = delays; } - -int CounterChannel::getFormRowIndex() +void CounterChannel::setIsUse(QString isUse) { - return formRowIndex; + this->isUse = isUse; } -void CounterChannel::setFormRowIndex(int rowIndex) +void CounterChannel::setName(QString name) { - this->formRowIndex = rowIndex; + this->name = name; } QList CounterChannel::getHisData() diff --git a/CounterRealTime/CounterChannel.cpp b/CounterRealTime/CounterChannel.cpp index 978b969..0a4b534 100644 --- a/CounterRealTime/CounterChannel.cpp +++ b/CounterRealTime/CounterChannel.cpp @@ -30,6 +30,14 @@ { return delays; } +QString CounterChannel::getIsUse() +{ + return isUse; +} +QString CounterChannel::getName() +{ + return name; +} void CounterChannel::setActive(bool active) { this->active = active; @@ -54,14 +62,13 @@ { this->delays = delays; } - -int CounterChannel::getFormRowIndex() +void CounterChannel::setIsUse(QString isUse) { - return formRowIndex; + this->isUse = isUse; } -void CounterChannel::setFormRowIndex(int rowIndex) +void CounterChannel::setName(QString name) { - this->formRowIndex = rowIndex; + this->name = name; } QList CounterChannel::getHisData() diff --git a/CounterRealTime/CounterChannel.h b/CounterRealTime/CounterChannel.h index afc5dc2..8f2c35e 100644 --- a/CounterRealTime/CounterChannel.h +++ b/CounterRealTime/CounterChannel.h @@ -15,15 +15,16 @@ QString getChannelCode(); QString getChannelNo(); QString getDelays(); + QString getIsUse(); + QString getName(); void setActive(bool active); void setDeviceId(QString deviceId); void setChannelId(QString channelId); void setChannelCode(QString channelCode); void setChannelNo(QString channelNo); void setDelays(QString delays); - - int getFormRowIndex(); - void setFormRowIndex(int rowIndex); + void setIsUse(QString isUse); + void setName(QString name); QList getHisData(); void appendHisData(QString timestampStr, QString valueStr); @@ -35,13 +36,12 @@ QString channelCode; QString channelNo; QString delays; + QString isUse; + QString name; int maxHisDataCount = 3600; QList hisDataList; - // 通道位于界面上的哪一行 列的位置通过计数器来确定 - int formRowIndex; - signals: }; diff --git a/CounterRealTime/CounterChannel.cpp b/CounterRealTime/CounterChannel.cpp index 978b969..0a4b534 100644 --- a/CounterRealTime/CounterChannel.cpp +++ b/CounterRealTime/CounterChannel.cpp @@ -30,6 +30,14 @@ { return delays; } +QString CounterChannel::getIsUse() +{ + return isUse; +} +QString CounterChannel::getName() +{ + return name; +} void CounterChannel::setActive(bool active) { this->active = active; @@ -54,14 +62,13 @@ { this->delays = delays; } - -int CounterChannel::getFormRowIndex() +void CounterChannel::setIsUse(QString isUse) { - return formRowIndex; + this->isUse = isUse; } -void CounterChannel::setFormRowIndex(int rowIndex) +void CounterChannel::setName(QString name) { - this->formRowIndex = rowIndex; + this->name = name; } QList CounterChannel::getHisData() diff --git a/CounterRealTime/CounterChannel.h b/CounterRealTime/CounterChannel.h index afc5dc2..8f2c35e 100644 --- a/CounterRealTime/CounterChannel.h +++ b/CounterRealTime/CounterChannel.h @@ -15,15 +15,16 @@ QString getChannelCode(); QString getChannelNo(); QString getDelays(); + QString getIsUse(); + QString getName(); void setActive(bool active); void setDeviceId(QString deviceId); void setChannelId(QString channelId); void setChannelCode(QString channelCode); void setChannelNo(QString channelNo); void setDelays(QString delays); - - int getFormRowIndex(); - void setFormRowIndex(int rowIndex); + void setIsUse(QString isUse); + void setName(QString name); QList getHisData(); void appendHisData(QString timestampStr, QString valueStr); @@ -35,13 +36,12 @@ QString channelCode; QString channelNo; QString delays; + QString isUse; + QString name; int maxHisDataCount = 3600; QList hisDataList; - // 通道位于界面上的哪一行 列的位置通过计数器来确定 - int formRowIndex; - signals: }; diff --git a/CounterRealTime/CounterDevice.cpp b/CounterRealTime/CounterDevice.cpp index 63200bb..1528373 100644 --- a/CounterRealTime/CounterDevice.cpp +++ b/CounterRealTime/CounterDevice.cpp @@ -1,6 +1,5 @@ #include "CounterDevice.h" -#include #include #include @@ -95,7 +94,7 @@ void CounterDevice::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); -// std::cout << this->dataBuff.toStdString(); +// qDebug() << this->dataBuff; CounterDataDto * counterData = new CounterDataDto(this); if (CounterProtocolXH::checkFrame(this->dataBuff) == true) @@ -117,7 +116,7 @@ } } else if (this->dataBuff.size() > COUNTER_FRAME_MIN_LENGTH) { - std::cout << QString("%1").arg(this->dataBuff.size()).toStdString() << std::endl; + qDebug() << QString("%1").arg(this->dataBuff.size()); this->dataBuff.clear(); } @@ -193,7 +192,7 @@ } // qDebug() << counterData->rawFrame; - qDebug() << messageArray; +// qDebug() << messageArray; // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(messageArray); diff --git a/CounterRealTime/CounterChannel.cpp b/CounterRealTime/CounterChannel.cpp index 978b969..0a4b534 100644 --- a/CounterRealTime/CounterChannel.cpp +++ b/CounterRealTime/CounterChannel.cpp @@ -30,6 +30,14 @@ { return delays; } +QString CounterChannel::getIsUse() +{ + return isUse; +} +QString CounterChannel::getName() +{ + return name; +} void CounterChannel::setActive(bool active) { this->active = active; @@ -54,14 +62,13 @@ { this->delays = delays; } - -int CounterChannel::getFormRowIndex() +void CounterChannel::setIsUse(QString isUse) { - return formRowIndex; + this->isUse = isUse; } -void CounterChannel::setFormRowIndex(int rowIndex) +void CounterChannel::setName(QString name) { - this->formRowIndex = rowIndex; + this->name = name; } QList CounterChannel::getHisData() diff --git a/CounterRealTime/CounterChannel.h b/CounterRealTime/CounterChannel.h index afc5dc2..8f2c35e 100644 --- a/CounterRealTime/CounterChannel.h +++ b/CounterRealTime/CounterChannel.h @@ -15,15 +15,16 @@ QString getChannelCode(); QString getChannelNo(); QString getDelays(); + QString getIsUse(); + QString getName(); void setActive(bool active); void setDeviceId(QString deviceId); void setChannelId(QString channelId); void setChannelCode(QString channelCode); void setChannelNo(QString channelNo); void setDelays(QString delays); - - int getFormRowIndex(); - void setFormRowIndex(int rowIndex); + void setIsUse(QString isUse); + void setName(QString name); QList getHisData(); void appendHisData(QString timestampStr, QString valueStr); @@ -35,13 +36,12 @@ QString channelCode; QString channelNo; QString delays; + QString isUse; + QString name; int maxHisDataCount = 3600; QList hisDataList; - // 通道位于界面上的哪一行 列的位置通过计数器来确定 - int formRowIndex; - signals: }; diff --git a/CounterRealTime/CounterDevice.cpp b/CounterRealTime/CounterDevice.cpp index 63200bb..1528373 100644 --- a/CounterRealTime/CounterDevice.cpp +++ b/CounterRealTime/CounterDevice.cpp @@ -1,6 +1,5 @@ #include "CounterDevice.h" -#include #include #include @@ -95,7 +94,7 @@ void CounterDevice::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); -// std::cout << this->dataBuff.toStdString(); +// qDebug() << this->dataBuff; CounterDataDto * counterData = new CounterDataDto(this); if (CounterProtocolXH::checkFrame(this->dataBuff) == true) @@ -117,7 +116,7 @@ } } else if (this->dataBuff.size() > COUNTER_FRAME_MIN_LENGTH) { - std::cout << QString("%1").arg(this->dataBuff.size()).toStdString() << std::endl; + qDebug() << QString("%1").arg(this->dataBuff.size()); this->dataBuff.clear(); } @@ -193,7 +192,7 @@ } // qDebug() << counterData->rawFrame; - qDebug() << messageArray; +// qDebug() << messageArray; // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(messageArray); diff --git a/CounterRealTime/CounterSettingForm.cpp b/CounterRealTime/CounterSettingForm.cpp index c095523..225ec88 100644 --- a/CounterRealTime/CounterSettingForm.cpp +++ b/CounterRealTime/CounterSettingForm.cpp @@ -283,43 +283,68 @@ { QString deviceId = ui->selectCounter->currentData().toString(); - for (int i = 0; i < 8; i++) + QJsonArray channelSettingUpdated; + + for (int i = 0; i < 16; i++) { + QString channelCode; + double delay; + bool active; + if (i < 8) { + channelCode = clockListLeft.at(i)->text(); + delay = delayListLeft.at(i)->value(); + active = activeListLeft.at(i)->isChecked(); + } else if (i < 16) { + channelCode = clockListRight.at(i - 8)->text(); + delay = delayListRight.at(i - 8)->value(); + active = activeListRight.at(i - 8)->isChecked(); + } + CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); if (channelPrt != nullptr) { - QString channelCode = clockListLeft.at(i)->text(); // 通道钟号 - double delay = delayListLeft.at(i)->value(); // 通道时延值 + bool isUpdated = isChannelSettingUpdated(i, channelPrt); + if (isUpdated == true) { + channelPrt->setChannelCode(channelCode); + channelPrt->setDelays(QString::number(delay)); + channelPrt->setActive(active); - channelPrt->setChannelCode(channelCode); - channelPrt->setDelays(QString::number(delay)); - channelPrt->setActive(activeListLeft.at(i)->isChecked()); - } else if (activeListLeft.at(i)->isChecked() == true) { + QJsonObject channelObj; + channelObj.insert("id", channelPrt->getChannelId()); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", channelPrt->getChannelNo()); + channelObj.insert("isUse", channelPrt->getIsUse()); + channelObj.insert("name", channelPrt->getName()); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); + } + } else if (active == true) { CounterChannel * newChannel = new CounterChannel(); newChannel->setActive(true); - newChannel->setChannelCode(clockListLeft.at(i)->text()); - newChannel->setDelays(QString::number(delayListLeft.at(i)->value())); + newChannel->setChannelCode(channelCode); + newChannel->setDelays(QString::number(delay)); ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1), newChannel); + + QJsonObject channelObj; + channelObj.insert("id", ""); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", QString::number(i + 1)); + channelObj.insert("isUse", SettingConfig::getInstance().MASTER); + channelObj.insert("name", QString("钟%1-1PPS").arg(channelCode)); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); } + } - CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr) - { - QString channelCode = clockListRight.at(i)->text(); // 通道钟号 - double delay = delayListRight.at(i)->value(); // 通道时延值 - - channelRightPrt->setChannelCode(channelCode); - channelRightPrt->setDelays(QString::number(delay)); - channelRightPrt->setActive(activeListRight.at(i)->isChecked()); - } else if (activeListRight.at(i)->isChecked() == true) { - CounterChannel * newChannel = new CounterChannel(); - newChannel->setActive(true); - newChannel->setChannelCode(clockListRight.at(i)->text()); - newChannel->setDelays(QString::number(delayListRight.at(i)->value())); - - ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1+8), newChannel); - } + if (channelSettingUpdated.size() > 0) { + emit this->channelSettingSubmit(channelSettingUpdated); } this->close(); @@ -341,10 +366,10 @@ void CounterSettingForm::on_selectCounter_currentIndexChanged(int index) { - if (index == 0) + clearChannelSettingValue(); + + if (index > 0) { - clearChannelSettingValue(); - } else { QString deviceId = ui->selectCounter->currentData().toString(); for (int i = 0; i < 8; i++) @@ -353,45 +378,37 @@ activeListRight.at(i)->setCheckable(true); CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); - if (channelPrt != nullptr && channelPrt->isActive() == true) + if (channelPrt != nullptr) { QString channelCode = channelPrt->getChannelCode(); // 通道钟号 QString delays = channelPrt->getDelays(); // 通道时延值 - activeListLeft.at(i)->setChecked(true); - activeListLeft.at(i)->setText("启用"); clockListLeft.at(i)->setText(channelCode); - clockListLeft.at(i)->setReadOnly(false); delayListLeft.at(i)->setValue(delays.toDouble()); - delayListLeft.at(i)->setReadOnly(false); - } else { - activeListLeft.at(i)->setChecked(false); - activeListLeft.at(i)->setText("未启用"); - clockListLeft.at(i)->setText("-"); - clockListLeft.at(i)->setReadOnly(true); - delayListLeft.at(i)->setValue(0.000); - delayListLeft.at(i)->setReadOnly(true); + + if (channelPrt->isActive() == true) { + activeListLeft.at(i)->setChecked(true); + activeListLeft.at(i)->setText("启用"); + clockListLeft.at(i)->setReadOnly(false); + delayListLeft.at(i)->setReadOnly(false); + } } CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr && channelRightPrt->isActive() == true) + if (channelRightPrt != nullptr) { QString channelCode = channelRightPrt->getChannelCode(); // 通道钟号 QString delays = channelRightPrt->getDelays(); // 通道时延值 - activeListRight.at(i)->setChecked(true); - activeListRight.at(i)->setText("启用"); clockListRight.at(i)->setText(channelCode); - clockListRight.at(i)->setReadOnly(false); delayListRight.at(i)->setValue(delays.toDouble()); - delayListRight.at(i)->setReadOnly(false); - } else { - activeListRight.at(i)->setChecked(false); - activeListRight.at(i)->setText("未启用"); - clockListRight.at(i)->setText("-"); - clockListRight.at(i)->setReadOnly(true); - delayListRight.at(i)->setValue(0.000); - delayListRight.at(i)->setReadOnly(true); + + if (channelRightPrt->isActive() == true) { + activeListRight.at(i)->setChecked(true); + activeListRight.at(i)->setText("启用"); + clockListRight.at(i)->setReadOnly(false); + delayListRight.at(i)->setReadOnly(false); + } } } } @@ -423,3 +440,35 @@ delayListRight.at(index - 8)->setReadOnly(!enable); } } + +bool CounterSettingForm::isChannelSettingUpdated(int index, CounterChannel *channelPtr) +{ + QString channelCode = ""; // 通道钟号 + double delay = 0.0; // 通道时延值 + bool active = false; + + if (index < 8) { + channelCode = clockListLeft.at(index)->text(); // 通道钟号 + delay = delayListLeft.at(index)->value(); // 通道时延值 + active = activeListLeft.at(index)->isChecked(); + } else if (index < 16) { + index = index - 8; + channelCode = clockListRight.at(index)->text(); // 通道钟号 + delay = delayListRight.at(index)->value(); // 通道时延值 + active = activeListRight.at(index)->isChecked(); + } + + if (channelCode != channelPtr->getChannelCode()) { + return true; + } + + if (active != channelPtr->isActive()) { + return true; + } + + if (delay - channelPtr->getDelays().toDouble() != 0) { + return true; + } + + return false; +} diff --git a/CounterRealTime/CounterChannel.cpp b/CounterRealTime/CounterChannel.cpp index 978b969..0a4b534 100644 --- a/CounterRealTime/CounterChannel.cpp +++ b/CounterRealTime/CounterChannel.cpp @@ -30,6 +30,14 @@ { return delays; } +QString CounterChannel::getIsUse() +{ + return isUse; +} +QString CounterChannel::getName() +{ + return name; +} void CounterChannel::setActive(bool active) { this->active = active; @@ -54,14 +62,13 @@ { this->delays = delays; } - -int CounterChannel::getFormRowIndex() +void CounterChannel::setIsUse(QString isUse) { - return formRowIndex; + this->isUse = isUse; } -void CounterChannel::setFormRowIndex(int rowIndex) +void CounterChannel::setName(QString name) { - this->formRowIndex = rowIndex; + this->name = name; } QList CounterChannel::getHisData() diff --git a/CounterRealTime/CounterChannel.h b/CounterRealTime/CounterChannel.h index afc5dc2..8f2c35e 100644 --- a/CounterRealTime/CounterChannel.h +++ b/CounterRealTime/CounterChannel.h @@ -15,15 +15,16 @@ QString getChannelCode(); QString getChannelNo(); QString getDelays(); + QString getIsUse(); + QString getName(); void setActive(bool active); void setDeviceId(QString deviceId); void setChannelId(QString channelId); void setChannelCode(QString channelCode); void setChannelNo(QString channelNo); void setDelays(QString delays); - - int getFormRowIndex(); - void setFormRowIndex(int rowIndex); + void setIsUse(QString isUse); + void setName(QString name); QList getHisData(); void appendHisData(QString timestampStr, QString valueStr); @@ -35,13 +36,12 @@ QString channelCode; QString channelNo; QString delays; + QString isUse; + QString name; int maxHisDataCount = 3600; QList hisDataList; - // 通道位于界面上的哪一行 列的位置通过计数器来确定 - int formRowIndex; - signals: }; diff --git a/CounterRealTime/CounterDevice.cpp b/CounterRealTime/CounterDevice.cpp index 63200bb..1528373 100644 --- a/CounterRealTime/CounterDevice.cpp +++ b/CounterRealTime/CounterDevice.cpp @@ -1,6 +1,5 @@ #include "CounterDevice.h" -#include #include #include @@ -95,7 +94,7 @@ void CounterDevice::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); -// std::cout << this->dataBuff.toStdString(); +// qDebug() << this->dataBuff; CounterDataDto * counterData = new CounterDataDto(this); if (CounterProtocolXH::checkFrame(this->dataBuff) == true) @@ -117,7 +116,7 @@ } } else if (this->dataBuff.size() > COUNTER_FRAME_MIN_LENGTH) { - std::cout << QString("%1").arg(this->dataBuff.size()).toStdString() << std::endl; + qDebug() << QString("%1").arg(this->dataBuff.size()); this->dataBuff.clear(); } @@ -193,7 +192,7 @@ } // qDebug() << counterData->rawFrame; - qDebug() << messageArray; +// qDebug() << messageArray; // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(messageArray); diff --git a/CounterRealTime/CounterSettingForm.cpp b/CounterRealTime/CounterSettingForm.cpp index c095523..225ec88 100644 --- a/CounterRealTime/CounterSettingForm.cpp +++ b/CounterRealTime/CounterSettingForm.cpp @@ -283,43 +283,68 @@ { QString deviceId = ui->selectCounter->currentData().toString(); - for (int i = 0; i < 8; i++) + QJsonArray channelSettingUpdated; + + for (int i = 0; i < 16; i++) { + QString channelCode; + double delay; + bool active; + if (i < 8) { + channelCode = clockListLeft.at(i)->text(); + delay = delayListLeft.at(i)->value(); + active = activeListLeft.at(i)->isChecked(); + } else if (i < 16) { + channelCode = clockListRight.at(i - 8)->text(); + delay = delayListRight.at(i - 8)->value(); + active = activeListRight.at(i - 8)->isChecked(); + } + CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); if (channelPrt != nullptr) { - QString channelCode = clockListLeft.at(i)->text(); // 通道钟号 - double delay = delayListLeft.at(i)->value(); // 通道时延值 + bool isUpdated = isChannelSettingUpdated(i, channelPrt); + if (isUpdated == true) { + channelPrt->setChannelCode(channelCode); + channelPrt->setDelays(QString::number(delay)); + channelPrt->setActive(active); - channelPrt->setChannelCode(channelCode); - channelPrt->setDelays(QString::number(delay)); - channelPrt->setActive(activeListLeft.at(i)->isChecked()); - } else if (activeListLeft.at(i)->isChecked() == true) { + QJsonObject channelObj; + channelObj.insert("id", channelPrt->getChannelId()); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", channelPrt->getChannelNo()); + channelObj.insert("isUse", channelPrt->getIsUse()); + channelObj.insert("name", channelPrt->getName()); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); + } + } else if (active == true) { CounterChannel * newChannel = new CounterChannel(); newChannel->setActive(true); - newChannel->setChannelCode(clockListLeft.at(i)->text()); - newChannel->setDelays(QString::number(delayListLeft.at(i)->value())); + newChannel->setChannelCode(channelCode); + newChannel->setDelays(QString::number(delay)); ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1), newChannel); + + QJsonObject channelObj; + channelObj.insert("id", ""); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", QString::number(i + 1)); + channelObj.insert("isUse", SettingConfig::getInstance().MASTER); + channelObj.insert("name", QString("钟%1-1PPS").arg(channelCode)); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); } + } - CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr) - { - QString channelCode = clockListRight.at(i)->text(); // 通道钟号 - double delay = delayListRight.at(i)->value(); // 通道时延值 - - channelRightPrt->setChannelCode(channelCode); - channelRightPrt->setDelays(QString::number(delay)); - channelRightPrt->setActive(activeListRight.at(i)->isChecked()); - } else if (activeListRight.at(i)->isChecked() == true) { - CounterChannel * newChannel = new CounterChannel(); - newChannel->setActive(true); - newChannel->setChannelCode(clockListRight.at(i)->text()); - newChannel->setDelays(QString::number(delayListRight.at(i)->value())); - - ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1+8), newChannel); - } + if (channelSettingUpdated.size() > 0) { + emit this->channelSettingSubmit(channelSettingUpdated); } this->close(); @@ -341,10 +366,10 @@ void CounterSettingForm::on_selectCounter_currentIndexChanged(int index) { - if (index == 0) + clearChannelSettingValue(); + + if (index > 0) { - clearChannelSettingValue(); - } else { QString deviceId = ui->selectCounter->currentData().toString(); for (int i = 0; i < 8; i++) @@ -353,45 +378,37 @@ activeListRight.at(i)->setCheckable(true); CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); - if (channelPrt != nullptr && channelPrt->isActive() == true) + if (channelPrt != nullptr) { QString channelCode = channelPrt->getChannelCode(); // 通道钟号 QString delays = channelPrt->getDelays(); // 通道时延值 - activeListLeft.at(i)->setChecked(true); - activeListLeft.at(i)->setText("启用"); clockListLeft.at(i)->setText(channelCode); - clockListLeft.at(i)->setReadOnly(false); delayListLeft.at(i)->setValue(delays.toDouble()); - delayListLeft.at(i)->setReadOnly(false); - } else { - activeListLeft.at(i)->setChecked(false); - activeListLeft.at(i)->setText("未启用"); - clockListLeft.at(i)->setText("-"); - clockListLeft.at(i)->setReadOnly(true); - delayListLeft.at(i)->setValue(0.000); - delayListLeft.at(i)->setReadOnly(true); + + if (channelPrt->isActive() == true) { + activeListLeft.at(i)->setChecked(true); + activeListLeft.at(i)->setText("启用"); + clockListLeft.at(i)->setReadOnly(false); + delayListLeft.at(i)->setReadOnly(false); + } } CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr && channelRightPrt->isActive() == true) + if (channelRightPrt != nullptr) { QString channelCode = channelRightPrt->getChannelCode(); // 通道钟号 QString delays = channelRightPrt->getDelays(); // 通道时延值 - activeListRight.at(i)->setChecked(true); - activeListRight.at(i)->setText("启用"); clockListRight.at(i)->setText(channelCode); - clockListRight.at(i)->setReadOnly(false); delayListRight.at(i)->setValue(delays.toDouble()); - delayListRight.at(i)->setReadOnly(false); - } else { - activeListRight.at(i)->setChecked(false); - activeListRight.at(i)->setText("未启用"); - clockListRight.at(i)->setText("-"); - clockListRight.at(i)->setReadOnly(true); - delayListRight.at(i)->setValue(0.000); - delayListRight.at(i)->setReadOnly(true); + + if (channelRightPrt->isActive() == true) { + activeListRight.at(i)->setChecked(true); + activeListRight.at(i)->setText("启用"); + clockListRight.at(i)->setReadOnly(false); + delayListRight.at(i)->setReadOnly(false); + } } } } @@ -423,3 +440,35 @@ delayListRight.at(index - 8)->setReadOnly(!enable); } } + +bool CounterSettingForm::isChannelSettingUpdated(int index, CounterChannel *channelPtr) +{ + QString channelCode = ""; // 通道钟号 + double delay = 0.0; // 通道时延值 + bool active = false; + + if (index < 8) { + channelCode = clockListLeft.at(index)->text(); // 通道钟号 + delay = delayListLeft.at(index)->value(); // 通道时延值 + active = activeListLeft.at(index)->isChecked(); + } else if (index < 16) { + index = index - 8; + channelCode = clockListRight.at(index)->text(); // 通道钟号 + delay = delayListRight.at(index)->value(); // 通道时延值 + active = activeListRight.at(index)->isChecked(); + } + + if (channelCode != channelPtr->getChannelCode()) { + return true; + } + + if (active != channelPtr->isActive()) { + return true; + } + + if (delay - channelPtr->getDelays().toDouble() != 0) { + return true; + } + + return false; +} diff --git a/CounterRealTime/CounterSettingForm.h b/CounterRealTime/CounterSettingForm.h index b4949fb..9c41362 100644 --- a/CounterRealTime/CounterSettingForm.h +++ b/CounterRealTime/CounterSettingForm.h @@ -55,10 +55,12 @@ void clearChannelSettingValue(); void enableChannel(int index, bool enable); + bool isChannelSettingUpdated(int index, CounterChannel * channelPtr); signals: void swiftDisplayLoop(); void swiftDisplayCount(); + void channelSettingSubmit(QJsonArray channelSettings); }; #endif // COUNTERSETTINGFORM_H diff --git a/CounterRealTime/CounterChannel.cpp b/CounterRealTime/CounterChannel.cpp index 978b969..0a4b534 100644 --- a/CounterRealTime/CounterChannel.cpp +++ b/CounterRealTime/CounterChannel.cpp @@ -30,6 +30,14 @@ { return delays; } +QString CounterChannel::getIsUse() +{ + return isUse; +} +QString CounterChannel::getName() +{ + return name; +} void CounterChannel::setActive(bool active) { this->active = active; @@ -54,14 +62,13 @@ { this->delays = delays; } - -int CounterChannel::getFormRowIndex() +void CounterChannel::setIsUse(QString isUse) { - return formRowIndex; + this->isUse = isUse; } -void CounterChannel::setFormRowIndex(int rowIndex) +void CounterChannel::setName(QString name) { - this->formRowIndex = rowIndex; + this->name = name; } QList CounterChannel::getHisData() diff --git a/CounterRealTime/CounterChannel.h b/CounterRealTime/CounterChannel.h index afc5dc2..8f2c35e 100644 --- a/CounterRealTime/CounterChannel.h +++ b/CounterRealTime/CounterChannel.h @@ -15,15 +15,16 @@ QString getChannelCode(); QString getChannelNo(); QString getDelays(); + QString getIsUse(); + QString getName(); void setActive(bool active); void setDeviceId(QString deviceId); void setChannelId(QString channelId); void setChannelCode(QString channelCode); void setChannelNo(QString channelNo); void setDelays(QString delays); - - int getFormRowIndex(); - void setFormRowIndex(int rowIndex); + void setIsUse(QString isUse); + void setName(QString name); QList getHisData(); void appendHisData(QString timestampStr, QString valueStr); @@ -35,13 +36,12 @@ QString channelCode; QString channelNo; QString delays; + QString isUse; + QString name; int maxHisDataCount = 3600; QList hisDataList; - // 通道位于界面上的哪一行 列的位置通过计数器来确定 - int formRowIndex; - signals: }; diff --git a/CounterRealTime/CounterDevice.cpp b/CounterRealTime/CounterDevice.cpp index 63200bb..1528373 100644 --- a/CounterRealTime/CounterDevice.cpp +++ b/CounterRealTime/CounterDevice.cpp @@ -1,6 +1,5 @@ #include "CounterDevice.h" -#include #include #include @@ -95,7 +94,7 @@ void CounterDevice::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); -// std::cout << this->dataBuff.toStdString(); +// qDebug() << this->dataBuff; CounterDataDto * counterData = new CounterDataDto(this); if (CounterProtocolXH::checkFrame(this->dataBuff) == true) @@ -117,7 +116,7 @@ } } else if (this->dataBuff.size() > COUNTER_FRAME_MIN_LENGTH) { - std::cout << QString("%1").arg(this->dataBuff.size()).toStdString() << std::endl; + qDebug() << QString("%1").arg(this->dataBuff.size()); this->dataBuff.clear(); } @@ -193,7 +192,7 @@ } // qDebug() << counterData->rawFrame; - qDebug() << messageArray; +// qDebug() << messageArray; // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(messageArray); diff --git a/CounterRealTime/CounterSettingForm.cpp b/CounterRealTime/CounterSettingForm.cpp index c095523..225ec88 100644 --- a/CounterRealTime/CounterSettingForm.cpp +++ b/CounterRealTime/CounterSettingForm.cpp @@ -283,43 +283,68 @@ { QString deviceId = ui->selectCounter->currentData().toString(); - for (int i = 0; i < 8; i++) + QJsonArray channelSettingUpdated; + + for (int i = 0; i < 16; i++) { + QString channelCode; + double delay; + bool active; + if (i < 8) { + channelCode = clockListLeft.at(i)->text(); + delay = delayListLeft.at(i)->value(); + active = activeListLeft.at(i)->isChecked(); + } else if (i < 16) { + channelCode = clockListRight.at(i - 8)->text(); + delay = delayListRight.at(i - 8)->value(); + active = activeListRight.at(i - 8)->isChecked(); + } + CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); if (channelPrt != nullptr) { - QString channelCode = clockListLeft.at(i)->text(); // 通道钟号 - double delay = delayListLeft.at(i)->value(); // 通道时延值 + bool isUpdated = isChannelSettingUpdated(i, channelPrt); + if (isUpdated == true) { + channelPrt->setChannelCode(channelCode); + channelPrt->setDelays(QString::number(delay)); + channelPrt->setActive(active); - channelPrt->setChannelCode(channelCode); - channelPrt->setDelays(QString::number(delay)); - channelPrt->setActive(activeListLeft.at(i)->isChecked()); - } else if (activeListLeft.at(i)->isChecked() == true) { + QJsonObject channelObj; + channelObj.insert("id", channelPrt->getChannelId()); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", channelPrt->getChannelNo()); + channelObj.insert("isUse", channelPrt->getIsUse()); + channelObj.insert("name", channelPrt->getName()); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); + } + } else if (active == true) { CounterChannel * newChannel = new CounterChannel(); newChannel->setActive(true); - newChannel->setChannelCode(clockListLeft.at(i)->text()); - newChannel->setDelays(QString::number(delayListLeft.at(i)->value())); + newChannel->setChannelCode(channelCode); + newChannel->setDelays(QString::number(delay)); ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1), newChannel); + + QJsonObject channelObj; + channelObj.insert("id", ""); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", QString::number(i + 1)); + channelObj.insert("isUse", SettingConfig::getInstance().MASTER); + channelObj.insert("name", QString("钟%1-1PPS").arg(channelCode)); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); } + } - CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr) - { - QString channelCode = clockListRight.at(i)->text(); // 通道钟号 - double delay = delayListRight.at(i)->value(); // 通道时延值 - - channelRightPrt->setChannelCode(channelCode); - channelRightPrt->setDelays(QString::number(delay)); - channelRightPrt->setActive(activeListRight.at(i)->isChecked()); - } else if (activeListRight.at(i)->isChecked() == true) { - CounterChannel * newChannel = new CounterChannel(); - newChannel->setActive(true); - newChannel->setChannelCode(clockListRight.at(i)->text()); - newChannel->setDelays(QString::number(delayListRight.at(i)->value())); - - ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1+8), newChannel); - } + if (channelSettingUpdated.size() > 0) { + emit this->channelSettingSubmit(channelSettingUpdated); } this->close(); @@ -341,10 +366,10 @@ void CounterSettingForm::on_selectCounter_currentIndexChanged(int index) { - if (index == 0) + clearChannelSettingValue(); + + if (index > 0) { - clearChannelSettingValue(); - } else { QString deviceId = ui->selectCounter->currentData().toString(); for (int i = 0; i < 8; i++) @@ -353,45 +378,37 @@ activeListRight.at(i)->setCheckable(true); CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); - if (channelPrt != nullptr && channelPrt->isActive() == true) + if (channelPrt != nullptr) { QString channelCode = channelPrt->getChannelCode(); // 通道钟号 QString delays = channelPrt->getDelays(); // 通道时延值 - activeListLeft.at(i)->setChecked(true); - activeListLeft.at(i)->setText("启用"); clockListLeft.at(i)->setText(channelCode); - clockListLeft.at(i)->setReadOnly(false); delayListLeft.at(i)->setValue(delays.toDouble()); - delayListLeft.at(i)->setReadOnly(false); - } else { - activeListLeft.at(i)->setChecked(false); - activeListLeft.at(i)->setText("未启用"); - clockListLeft.at(i)->setText("-"); - clockListLeft.at(i)->setReadOnly(true); - delayListLeft.at(i)->setValue(0.000); - delayListLeft.at(i)->setReadOnly(true); + + if (channelPrt->isActive() == true) { + activeListLeft.at(i)->setChecked(true); + activeListLeft.at(i)->setText("启用"); + clockListLeft.at(i)->setReadOnly(false); + delayListLeft.at(i)->setReadOnly(false); + } } CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr && channelRightPrt->isActive() == true) + if (channelRightPrt != nullptr) { QString channelCode = channelRightPrt->getChannelCode(); // 通道钟号 QString delays = channelRightPrt->getDelays(); // 通道时延值 - activeListRight.at(i)->setChecked(true); - activeListRight.at(i)->setText("启用"); clockListRight.at(i)->setText(channelCode); - clockListRight.at(i)->setReadOnly(false); delayListRight.at(i)->setValue(delays.toDouble()); - delayListRight.at(i)->setReadOnly(false); - } else { - activeListRight.at(i)->setChecked(false); - activeListRight.at(i)->setText("未启用"); - clockListRight.at(i)->setText("-"); - clockListRight.at(i)->setReadOnly(true); - delayListRight.at(i)->setValue(0.000); - delayListRight.at(i)->setReadOnly(true); + + if (channelRightPrt->isActive() == true) { + activeListRight.at(i)->setChecked(true); + activeListRight.at(i)->setText("启用"); + clockListRight.at(i)->setReadOnly(false); + delayListRight.at(i)->setReadOnly(false); + } } } } @@ -423,3 +440,35 @@ delayListRight.at(index - 8)->setReadOnly(!enable); } } + +bool CounterSettingForm::isChannelSettingUpdated(int index, CounterChannel *channelPtr) +{ + QString channelCode = ""; // 通道钟号 + double delay = 0.0; // 通道时延值 + bool active = false; + + if (index < 8) { + channelCode = clockListLeft.at(index)->text(); // 通道钟号 + delay = delayListLeft.at(index)->value(); // 通道时延值 + active = activeListLeft.at(index)->isChecked(); + } else if (index < 16) { + index = index - 8; + channelCode = clockListRight.at(index)->text(); // 通道钟号 + delay = delayListRight.at(index)->value(); // 通道时延值 + active = activeListRight.at(index)->isChecked(); + } + + if (channelCode != channelPtr->getChannelCode()) { + return true; + } + + if (active != channelPtr->isActive()) { + return true; + } + + if (delay - channelPtr->getDelays().toDouble() != 0) { + return true; + } + + return false; +} diff --git a/CounterRealTime/CounterSettingForm.h b/CounterRealTime/CounterSettingForm.h index b4949fb..9c41362 100644 --- a/CounterRealTime/CounterSettingForm.h +++ b/CounterRealTime/CounterSettingForm.h @@ -55,10 +55,12 @@ void clearChannelSettingValue(); void enableChannel(int index, bool enable); + bool isChannelSettingUpdated(int index, CounterChannel * channelPtr); signals: void swiftDisplayLoop(); void swiftDisplayCount(); + void channelSettingSubmit(QJsonArray channelSettings); }; #endif // COUNTERSETTINGFORM_H diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index 55db2ed..86a7776 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -12,6 +12,7 @@ resize(SettingConfig::getInstance().WINDOW_WIDTH, SettingConfig::getInstance().WINDOW_HEIGHT); settingForm = new CounterSettingForm(); + connect(settingForm, &CounterSettingForm::channelSettingSubmit, this, &CounterWindowRT::onChannelSettingSubmit); QFile file(":/qss/mainClock.css"); if (file.open(QFile::ReadOnly)) { @@ -22,6 +23,14 @@ file.close(); } + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_MSG_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &CounterWindowRT::onKafkaMessageReceived); + // 绘制界面上的表格 initChannelForm(); @@ -31,7 +40,6 @@ // 重新选择显示多少个计数器 connect(settingForm, &CounterSettingForm::swiftDisplayLoop, this, &CounterWindowRT::updateDeviceWidget); - connect(settingForm, &CounterSettingForm::swiftDisplayCount, this, &CounterWindowRT::initChannelForm); httpReq = new HttpRequestController(this); @@ -47,7 +55,7 @@ // 初始化设备和通道 getDeviceList(); -// QThread::msleep(1000 * 3); + QThread::msleep(1000 * 3); for (int i = 0; i < ConstCache::getInstance().deviceIdLoopA.size(); i++) { CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(ConstCache::getInstance().deviceIdLoopA.at(i)); @@ -58,7 +66,6 @@ CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(ConstCache::getInstance().deviceIdLoopB.at(i)); devicePtr->initSerialPort(); } - } CounterWindowRT::~CounterWindowRT() @@ -77,85 +84,94 @@ } } -/** - * @brief CounterWindowRT::drawCounterDataOnPage - * @param messageArray - * 根据发送到Kafka中的数据串来绘制界面 - * - */ -void CounterWindowRT::drawCounterDataOnPage(QJsonArray messageArray) +void CounterWindowRT::onChannelSettingSubmit(QJsonArray channelSettings) { - if (messageArray.isEmpty() == false) - { - // 获取设备id属性 - QString deviceId = messageArray.at(0).toObject().find("deviceId")->toString(); - - // 如果不是当前显示的计数器 则直接返回 - CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); - if (SettingConfig::getInstance().MASTER != devicePtr->getIsUse()) - { - return; - } - - // 根据设备id 从map中取出列索引的值 - int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); - if (devIdx > SettingConfig::getInstance().DISPLAY_COUNT) - { - return; - } - - // 参考通道 - int refChNo = messageArray.at(0).toObject().find("data")->toObject().find("channelRefNo")->toInt(); - CounterChannel * refChann = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(refChNo)); - if (refChann != nullptr) - { - if (devIdx < devRefList.size()) { - devRefList.at(devIdx)->setText(QString("REF:%1").arg(refChann->getChannelCode())); - } - } - - updateChannelDataForm(messageArray); - } + httpReq->updateChannelInfo(channelSettings); } -void CounterWindowRT::updateDeviceWidget() +int CounterWindowRT::initHttpToken() { - QList devIds = SettingConfig::getInstance().MASTER == 1 ? ConstCache::getInstance().deviceIdLoopA : ConstCache::getInstance().deviceIdLoopB; - - for (int i = 0; i < SettingConfig::getInstance().DISPLAY_COUNT; i++) + QJsonObject response = httpReq->getTokenByClientId(SettingConfig::getInstance().CLIENT_ID, + SettingConfig::getInstance().APP_KEY); + return response.value("code").toInt(); +} +void CounterWindowRT::getDeviceList() +{ + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.value("code").toInt() == 200) { - if (i < devIds.size()) + // 将获取到的设备绘制到表格中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) { - CounterDevice * dev = ConstCache::getInstance().deviceMap.value(devIds.at(i)); + // 接口返回的device对象 + QJsonObject devItem = devArray.at(i).toObject(); + QString devName = devItem.value("simpleName").toString(); // 设备名称 + QString deviceId = devItem.value("deviceId").toString(); // 设备ID + QString isUse = devItem.value("isUse").toString(); // 标识A路/B路 - QString devName = dev->getDeviceName(); // 设备名称 - devTitleList.at(i)->setText(devName.right(devName.length() - 8)); - dev->setFormColIndex(i); - } else { - devTitleList.at(i)->setText(""); - devRefList.at(i)->setText(""); + // 构建Device对象 并赋值id、串口、波特率、设备编号等属性 + CounterDevice * device = new CounterDevice(this); + device->setDeviceId(deviceId); + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceName(devName); + device->setIsUse(isUse.toInt()); - for (int j = 0; j < 16; j++) + ConstCache::getInstance().deviceMap.insert(deviceId, device); + if (isUse == "1") { - channelNameList.at(i * 16 + j)->setText(""); - channelValueList.at(i * 16 + j)->setText(""); + ConstCache::getInstance().deviceIdLoopA.append(deviceId); + } else if (isUse == "0") { + ConstCache::getInstance().deviceIdLoopB.append(deviceId); } + + // 绑定显示函数 + connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); + + // 查询计数器的通道 并初始化串口 + this->getChannelList(deviceId); } + + updateDeviceWidget(); } } - -void CounterWindowRT::initChannelForm() +void CounterWindowRT::getChannelList(QString deviceId) { - // 1. 首先清除界面上的所有内容 - qDeleteAll(ui->widgetContent->children()); + // 查询计数器的通道 + QJsonObject response = httpReq->getChannelList(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 + QString channelCode = channelItem.value("channelCode").toString(); // 通道钟号 + QString channelNo = QString::number(channelItem.value("channelNo").toInt()); // 通道序号 + QString delays = channelItem.value("delays").toString(); // 通道时延值 + QString isUse = channelItem.value("isUse").toString(); + QString name = channelItem.value("name").toString(); + bool active = channelItem.value("isActive").toString().toInt(); // 通道是否启用 - // 将lab列表的数量置0 - devTitleList.resize(0); - devRefList.resize(0); - channelNameList.resize(0); - channelValueList.resize(0); + // 构建通道对象 并设置属性 + CounterChannel * channel = new CounterChannel(this); + channel->setDeviceId(deviceId); + channel->setChannelId(channelId); + channel->setChannelCode(channelCode); + channel->setChannelNo(channelNo); + channel->setDelays(delays); + channel->setActive(active); + channel->setIsUse(isUse); + channel->setName(name); - initDevAndChannelForm(SettingConfig::getInstance().DISPLAY_COUNT); + // 添加到通道的集合中 key=deviceId-channelNo + ConstCache::getInstance().channelMap.insert(deviceId + "-" + channelNo, channel); + } + } } void CounterWindowRT::initDevAndChannelForm(int count) @@ -221,7 +237,7 @@ for ( int i = 0; i < 16; i++ ) { QLabelDblClick * labChannelName = new QLabelDblClick(widgetDev); // 通道名 - QLabel * labChannelValue = new QLabel(widgetDev); // 通道测量值 + QLabelDblClick * labChannelValue = new QLabelDblClick(widgetDev); // 通道测量值 // 默认显示内容 labChannelName->setText(QString("CH%1").arg(i+1, 2, 10, QLatin1Char('0'))); @@ -247,6 +263,7 @@ // 添加通道名称的双击事件,显示数据和残差图表 connect(labChannelName, &QLabelDblClick::doubleClicked, this, &CounterWindowRT::onChannelNameLabelDblClicked); + connect(labChannelValue, &QLabelDblClick::doubleClicked, this, &CounterWindowRT::onChannelNameLabelDblClicked); channelNameList.append(labChannelName); channelValueList.append(labChannelValue); @@ -263,14 +280,13 @@ widgetDev->setLayout(layoutChannelPage); } } - void CounterWindowRT::updateChannelDataForm(QJsonArray messageArray) { // 获取设备id属性 - QString deviceId = messageArray.at(0).toObject().find("deviceId")->toString(); + QString deviceId = messageArray.at(0).toObject().value("deviceId").toString(); // 参考通道号 - int refChNo = messageArray.at(0).toObject().find("data")->toObject().find("channelRefNo")->toInt(); + int refChNo = messageArray.at(0).toObject().value("data").toObject().value("channelRefNo").toInt(); // 根据设备id 从map中取出列索引的值 int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); @@ -285,7 +301,7 @@ for (int i = 0; i < messageArray.size(); i++) { QJsonObject channelDataItem = messageArray.at(i).toObject(); - QString channelNo = QString::number(channelDataItem.find("channelNo")->toInt()); // 通道序号 + QString channelNo = QString::number(channelDataItem.value("channelNo").toInt()); // 通道序号 channelDataJsonMap.insert(channelNo, channelDataItem); } @@ -306,13 +322,13 @@ CounterChannel * channel = ConstCache::getInstance().channelMap.value(deviceId + "-" + channelNo); // 从map中找到对应的通道 // 通道Label的序号 - int chanelIdx = devIdx * 16 + channelDataItem.find("channelNo")->toInt() - 1; + int chanelIdx = devIdx * 16 + channelDataItem.value("channelNo").toInt() - 1; if (channel != nullptr && channel->isActive() == true) { // 通道时延值 并计算 减去时延值的测量值 double delay = channel->getDelays().isEmpty() == false ? channel->getDelays().toDouble() : 0.0; - double valueMinusDelay = channelDataItem.find("data")->toObject().find("dataValue")->toString().toDouble() - delay * 1E-9; + double valueMinusDelay = channelDataItem.value("data").toObject().value("dataValue").toString().toDouble() - delay * 1E-9; // 设置通道名和测量值的QLabel channelNameList.at(chanelIdx)->setText(QString("CH%1:%2").arg(channelNoStr).arg(channel->getChannelCode())); @@ -321,6 +337,10 @@ channelNameList.at(chanelIdx)->setProperty("channelId", channel->getChannelId()); channelNameList.at(chanelIdx)->setProperty("deviceId", deviceId); channelNameList.at(chanelIdx)->setProperty("channelNo", channelNo); + + channelValueList.at(chanelIdx)->setProperty("channelId", channel->getChannelId()); + channelValueList.at(chanelIdx)->setProperty("deviceId", deviceId); + channelValueList.at(chanelIdx)->setProperty("channelNo", channelNo); } else { channelNameList.at(chanelIdx)->setText(QString("CH%1:%2").arg(channelNoStr).arg("-")); channelValueList.at(chanelIdx)->setText(""); @@ -333,84 +353,11 @@ } } -int CounterWindowRT::initHttpToken() +void CounterWindowRT::updateDateAndTime() { - QJsonObject response = httpReq->getTokenByClientId(SettingConfig::getInstance().CLIENT_ID, - SettingConfig::getInstance().APP_KEY); - return response.find("code")->toInt(); -} -void CounterWindowRT::getDeviceList() -{ - QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - if (devListRes.find("code")->toInt() == 200) - { - // 将获取到的设备绘制到表格中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - // 接口返回的device对象 - QJsonObject devItem = devArray.at(i).toObject(); - QString devName = devItem.find("deviceName")->toString(); // 设备名称 - QString deviceId = devItem.find("deviceId")->toString(); // 设备ID - QString isUse = devItem.find("isUse")->toString(); // 标识A路/B路 - - // 构建Device对象 并赋值id、串口、波特率、设备编号等属性 - CounterDevice * device = new CounterDevice(this); - device->setDeviceId(deviceId); - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceName(devName); - device->setIsUse(isUse.toInt()); - - ConstCache::getInstance().deviceMap.insert(deviceId, device); - if (isUse == "1") - { - ConstCache::getInstance().deviceIdLoopA.append(deviceId); - } else if (isUse == "0") { - ConstCache::getInstance().deviceIdLoopB.append(deviceId); - } - - // 绑定显示函数 - connect(device, &CounterDevice::sendDataToDraw, - this, &CounterWindowRT::drawCounterDataOnPage); - - // 查询计数器的通道 并初始化串口 - this->getChannelList(deviceId); - } - - updateDeviceWidget(); - } -} -void CounterWindowRT::getChannelList(QString deviceId) -{ - // 查询计数器的通道 - QJsonObject response = httpReq->getChannelList(deviceId); - if (response.find("code")->toInt() == 200) - { - // 将通道绘制在界面上 - QJsonArray channelArray = response.find("data")->toArray(); - for (int i = 0; i < channelArray.size(); i++) - { - // 接口返回的channel对象 - QJsonObject channelItem = channelArray.at(i).toObject(); - QString channelId = channelItem.find("id")->toString(); // 通道ID - QString channelCode = channelItem.find("channelCode")->toString(); // 通道钟号 - QString channelNo = QString::number(channelItem.find("channelNo")->toInt()); // 通道序号 - QString delays = channelItem.find("delays")->toString(); // 通道时延值 - - // 构建通道对象 并设置属性 - CounterChannel * channel = new CounterChannel(this); - channel->setDeviceId(deviceId); - channel->setChannelId(channelId); - channel->setChannelCode(channelCode); - channel->setChannelNo(channelNo); - channel->setDelays(delays); - - // 添加到通道的集合中 key=deviceId-channelNo - ConstCache::getInstance().channelMap.insert(deviceId + "-" + channelNo, channel); - } - } + QString date = QDate::currentDate().toString("yyyy-MM-dd"); + QString time = QTime::currentTime().toString("HH:mm:ss"); + ui->labTime->setText(date + " " + time); } void CounterWindowRT::on_btnMenuSetting_clicked() @@ -425,6 +372,47 @@ setWindowState(Qt::WindowMinimized | windowState()); } +void CounterWindowRT::initChannelForm() +{ + // 1. 首先清除界面上的所有内容 + qDeleteAll(ui->widgetContent->children()); + + // 将lab列表的数量置0 + devTitleList.resize(0); + devRefList.resize(0); + channelNameList.resize(0); + channelValueList.resize(0); + + initDevAndChannelForm(SettingConfig::getInstance().DISPLAY_COUNT); +} + +void CounterWindowRT::updateDeviceWidget() +{ + QList devIds = SettingConfig::getInstance().MASTER == 1 ? ConstCache::getInstance().deviceIdLoopA : ConstCache::getInstance().deviceIdLoopB; + + for (int i = 0; i < SettingConfig::getInstance().DISPLAY_COUNT; i++) + { + if (i < devIds.size()) + { + CounterDevice * dev = ConstCache::getInstance().deviceMap.value(devIds.at(i)); + + if (dev != nullptr) { + devTitleList.at(i)->setText(dev->getDeviceName()); // 设备名称 + dev->setFormColIndex(i); + } + } else { + devTitleList.at(i)->setText(""); + devRefList.at(i)->setText(""); + + for (int j = 0; j < 16; j++) + { + channelNameList.at(i * 16 + j)->setText(""); + channelValueList.at(i * 16 + j)->setText(""); + } + } + } +} + void CounterWindowRT::onChannelNameLabelDblClicked() { QLabelDblClick * channelLab = (QLabelDblClick *) sender(); @@ -445,10 +433,50 @@ } } -void CounterWindowRT::updateDateAndTime() + +/** + * @brief CounterWindowRT::drawCounterDataOnPage + * @param messageArray + * 根据发送到Kafka中的数据串来绘制界面 + * + */ +void CounterWindowRT::drawCounterDataOnPage(QJsonArray messageArray) { - QString date = QDate::currentDate().toString("yyyy-MM-dd"); - QString time = QTime::currentTime().toString("HH:mm:ss"); - ui->labTime->setText(date + " " + time); + if (messageArray.isEmpty() == false) + { + // 获取设备id属性 + QString deviceId = messageArray.at(0).toObject().value("deviceId").toString(); + + // 如果不是当前显示的计数器 则直接返回 + CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (SettingConfig::getInstance().MASTER != devicePtr->getIsUse()) + { + return; + } + + // 根据设备id 从map中取出列索引的值 + int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); + if (devIdx > SettingConfig::getInstance().DISPLAY_COUNT) + { + return; + } + + // 参考通道 + int refChNo = messageArray.at(0).toObject().value("data").toObject().value("channelRefNo").toInt(); + CounterChannel * refChann = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(refChNo)); + if (refChann != nullptr) + { + if (devIdx < devRefList.size()) { + devRefList.at(devIdx)->setText(QString("REF:%1").arg(refChann->getChannelCode())); + } + } + + updateChannelDataForm(messageArray); + } } + +void CounterWindowRT::onKafkaMessageReceived(QJsonObject message) +{ + qDebug() << message; +} diff --git a/CounterRealTime/CounterChannel.cpp b/CounterRealTime/CounterChannel.cpp index 978b969..0a4b534 100644 --- a/CounterRealTime/CounterChannel.cpp +++ b/CounterRealTime/CounterChannel.cpp @@ -30,6 +30,14 @@ { return delays; } +QString CounterChannel::getIsUse() +{ + return isUse; +} +QString CounterChannel::getName() +{ + return name; +} void CounterChannel::setActive(bool active) { this->active = active; @@ -54,14 +62,13 @@ { this->delays = delays; } - -int CounterChannel::getFormRowIndex() +void CounterChannel::setIsUse(QString isUse) { - return formRowIndex; + this->isUse = isUse; } -void CounterChannel::setFormRowIndex(int rowIndex) +void CounterChannel::setName(QString name) { - this->formRowIndex = rowIndex; + this->name = name; } QList CounterChannel::getHisData() diff --git a/CounterRealTime/CounterChannel.h b/CounterRealTime/CounterChannel.h index afc5dc2..8f2c35e 100644 --- a/CounterRealTime/CounterChannel.h +++ b/CounterRealTime/CounterChannel.h @@ -15,15 +15,16 @@ QString getChannelCode(); QString getChannelNo(); QString getDelays(); + QString getIsUse(); + QString getName(); void setActive(bool active); void setDeviceId(QString deviceId); void setChannelId(QString channelId); void setChannelCode(QString channelCode); void setChannelNo(QString channelNo); void setDelays(QString delays); - - int getFormRowIndex(); - void setFormRowIndex(int rowIndex); + void setIsUse(QString isUse); + void setName(QString name); QList getHisData(); void appendHisData(QString timestampStr, QString valueStr); @@ -35,13 +36,12 @@ QString channelCode; QString channelNo; QString delays; + QString isUse; + QString name; int maxHisDataCount = 3600; QList hisDataList; - // 通道位于界面上的哪一行 列的位置通过计数器来确定 - int formRowIndex; - signals: }; diff --git a/CounterRealTime/CounterDevice.cpp b/CounterRealTime/CounterDevice.cpp index 63200bb..1528373 100644 --- a/CounterRealTime/CounterDevice.cpp +++ b/CounterRealTime/CounterDevice.cpp @@ -1,6 +1,5 @@ #include "CounterDevice.h" -#include #include #include @@ -95,7 +94,7 @@ void CounterDevice::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); -// std::cout << this->dataBuff.toStdString(); +// qDebug() << this->dataBuff; CounterDataDto * counterData = new CounterDataDto(this); if (CounterProtocolXH::checkFrame(this->dataBuff) == true) @@ -117,7 +116,7 @@ } } else if (this->dataBuff.size() > COUNTER_FRAME_MIN_LENGTH) { - std::cout << QString("%1").arg(this->dataBuff.size()).toStdString() << std::endl; + qDebug() << QString("%1").arg(this->dataBuff.size()); this->dataBuff.clear(); } @@ -193,7 +192,7 @@ } // qDebug() << counterData->rawFrame; - qDebug() << messageArray; +// qDebug() << messageArray; // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(messageArray); diff --git a/CounterRealTime/CounterSettingForm.cpp b/CounterRealTime/CounterSettingForm.cpp index c095523..225ec88 100644 --- a/CounterRealTime/CounterSettingForm.cpp +++ b/CounterRealTime/CounterSettingForm.cpp @@ -283,43 +283,68 @@ { QString deviceId = ui->selectCounter->currentData().toString(); - for (int i = 0; i < 8; i++) + QJsonArray channelSettingUpdated; + + for (int i = 0; i < 16; i++) { + QString channelCode; + double delay; + bool active; + if (i < 8) { + channelCode = clockListLeft.at(i)->text(); + delay = delayListLeft.at(i)->value(); + active = activeListLeft.at(i)->isChecked(); + } else if (i < 16) { + channelCode = clockListRight.at(i - 8)->text(); + delay = delayListRight.at(i - 8)->value(); + active = activeListRight.at(i - 8)->isChecked(); + } + CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); if (channelPrt != nullptr) { - QString channelCode = clockListLeft.at(i)->text(); // 通道钟号 - double delay = delayListLeft.at(i)->value(); // 通道时延值 + bool isUpdated = isChannelSettingUpdated(i, channelPrt); + if (isUpdated == true) { + channelPrt->setChannelCode(channelCode); + channelPrt->setDelays(QString::number(delay)); + channelPrt->setActive(active); - channelPrt->setChannelCode(channelCode); - channelPrt->setDelays(QString::number(delay)); - channelPrt->setActive(activeListLeft.at(i)->isChecked()); - } else if (activeListLeft.at(i)->isChecked() == true) { + QJsonObject channelObj; + channelObj.insert("id", channelPrt->getChannelId()); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", channelPrt->getChannelNo()); + channelObj.insert("isUse", channelPrt->getIsUse()); + channelObj.insert("name", channelPrt->getName()); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); + } + } else if (active == true) { CounterChannel * newChannel = new CounterChannel(); newChannel->setActive(true); - newChannel->setChannelCode(clockListLeft.at(i)->text()); - newChannel->setDelays(QString::number(delayListLeft.at(i)->value())); + newChannel->setChannelCode(channelCode); + newChannel->setDelays(QString::number(delay)); ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1), newChannel); + + QJsonObject channelObj; + channelObj.insert("id", ""); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", QString::number(i + 1)); + channelObj.insert("isUse", SettingConfig::getInstance().MASTER); + channelObj.insert("name", QString("钟%1-1PPS").arg(channelCode)); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); } + } - CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr) - { - QString channelCode = clockListRight.at(i)->text(); // 通道钟号 - double delay = delayListRight.at(i)->value(); // 通道时延值 - - channelRightPrt->setChannelCode(channelCode); - channelRightPrt->setDelays(QString::number(delay)); - channelRightPrt->setActive(activeListRight.at(i)->isChecked()); - } else if (activeListRight.at(i)->isChecked() == true) { - CounterChannel * newChannel = new CounterChannel(); - newChannel->setActive(true); - newChannel->setChannelCode(clockListRight.at(i)->text()); - newChannel->setDelays(QString::number(delayListRight.at(i)->value())); - - ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1+8), newChannel); - } + if (channelSettingUpdated.size() > 0) { + emit this->channelSettingSubmit(channelSettingUpdated); } this->close(); @@ -341,10 +366,10 @@ void CounterSettingForm::on_selectCounter_currentIndexChanged(int index) { - if (index == 0) + clearChannelSettingValue(); + + if (index > 0) { - clearChannelSettingValue(); - } else { QString deviceId = ui->selectCounter->currentData().toString(); for (int i = 0; i < 8; i++) @@ -353,45 +378,37 @@ activeListRight.at(i)->setCheckable(true); CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); - if (channelPrt != nullptr && channelPrt->isActive() == true) + if (channelPrt != nullptr) { QString channelCode = channelPrt->getChannelCode(); // 通道钟号 QString delays = channelPrt->getDelays(); // 通道时延值 - activeListLeft.at(i)->setChecked(true); - activeListLeft.at(i)->setText("启用"); clockListLeft.at(i)->setText(channelCode); - clockListLeft.at(i)->setReadOnly(false); delayListLeft.at(i)->setValue(delays.toDouble()); - delayListLeft.at(i)->setReadOnly(false); - } else { - activeListLeft.at(i)->setChecked(false); - activeListLeft.at(i)->setText("未启用"); - clockListLeft.at(i)->setText("-"); - clockListLeft.at(i)->setReadOnly(true); - delayListLeft.at(i)->setValue(0.000); - delayListLeft.at(i)->setReadOnly(true); + + if (channelPrt->isActive() == true) { + activeListLeft.at(i)->setChecked(true); + activeListLeft.at(i)->setText("启用"); + clockListLeft.at(i)->setReadOnly(false); + delayListLeft.at(i)->setReadOnly(false); + } } CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr && channelRightPrt->isActive() == true) + if (channelRightPrt != nullptr) { QString channelCode = channelRightPrt->getChannelCode(); // 通道钟号 QString delays = channelRightPrt->getDelays(); // 通道时延值 - activeListRight.at(i)->setChecked(true); - activeListRight.at(i)->setText("启用"); clockListRight.at(i)->setText(channelCode); - clockListRight.at(i)->setReadOnly(false); delayListRight.at(i)->setValue(delays.toDouble()); - delayListRight.at(i)->setReadOnly(false); - } else { - activeListRight.at(i)->setChecked(false); - activeListRight.at(i)->setText("未启用"); - clockListRight.at(i)->setText("-"); - clockListRight.at(i)->setReadOnly(true); - delayListRight.at(i)->setValue(0.000); - delayListRight.at(i)->setReadOnly(true); + + if (channelRightPrt->isActive() == true) { + activeListRight.at(i)->setChecked(true); + activeListRight.at(i)->setText("启用"); + clockListRight.at(i)->setReadOnly(false); + delayListRight.at(i)->setReadOnly(false); + } } } } @@ -423,3 +440,35 @@ delayListRight.at(index - 8)->setReadOnly(!enable); } } + +bool CounterSettingForm::isChannelSettingUpdated(int index, CounterChannel *channelPtr) +{ + QString channelCode = ""; // 通道钟号 + double delay = 0.0; // 通道时延值 + bool active = false; + + if (index < 8) { + channelCode = clockListLeft.at(index)->text(); // 通道钟号 + delay = delayListLeft.at(index)->value(); // 通道时延值 + active = activeListLeft.at(index)->isChecked(); + } else if (index < 16) { + index = index - 8; + channelCode = clockListRight.at(index)->text(); // 通道钟号 + delay = delayListRight.at(index)->value(); // 通道时延值 + active = activeListRight.at(index)->isChecked(); + } + + if (channelCode != channelPtr->getChannelCode()) { + return true; + } + + if (active != channelPtr->isActive()) { + return true; + } + + if (delay - channelPtr->getDelays().toDouble() != 0) { + return true; + } + + return false; +} diff --git a/CounterRealTime/CounterSettingForm.h b/CounterRealTime/CounterSettingForm.h index b4949fb..9c41362 100644 --- a/CounterRealTime/CounterSettingForm.h +++ b/CounterRealTime/CounterSettingForm.h @@ -55,10 +55,12 @@ void clearChannelSettingValue(); void enableChannel(int index, bool enable); + bool isChannelSettingUpdated(int index, CounterChannel * channelPtr); signals: void swiftDisplayLoop(); void swiftDisplayCount(); + void channelSettingSubmit(QJsonArray channelSettings); }; #endif // COUNTERSETTINGFORM_H diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index 55db2ed..86a7776 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -12,6 +12,7 @@ resize(SettingConfig::getInstance().WINDOW_WIDTH, SettingConfig::getInstance().WINDOW_HEIGHT); settingForm = new CounterSettingForm(); + connect(settingForm, &CounterSettingForm::channelSettingSubmit, this, &CounterWindowRT::onChannelSettingSubmit); QFile file(":/qss/mainClock.css"); if (file.open(QFile::ReadOnly)) { @@ -22,6 +23,14 @@ file.close(); } + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_MSG_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &CounterWindowRT::onKafkaMessageReceived); + // 绘制界面上的表格 initChannelForm(); @@ -31,7 +40,6 @@ // 重新选择显示多少个计数器 connect(settingForm, &CounterSettingForm::swiftDisplayLoop, this, &CounterWindowRT::updateDeviceWidget); - connect(settingForm, &CounterSettingForm::swiftDisplayCount, this, &CounterWindowRT::initChannelForm); httpReq = new HttpRequestController(this); @@ -47,7 +55,7 @@ // 初始化设备和通道 getDeviceList(); -// QThread::msleep(1000 * 3); + QThread::msleep(1000 * 3); for (int i = 0; i < ConstCache::getInstance().deviceIdLoopA.size(); i++) { CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(ConstCache::getInstance().deviceIdLoopA.at(i)); @@ -58,7 +66,6 @@ CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(ConstCache::getInstance().deviceIdLoopB.at(i)); devicePtr->initSerialPort(); } - } CounterWindowRT::~CounterWindowRT() @@ -77,85 +84,94 @@ } } -/** - * @brief CounterWindowRT::drawCounterDataOnPage - * @param messageArray - * 根据发送到Kafka中的数据串来绘制界面 - * - */ -void CounterWindowRT::drawCounterDataOnPage(QJsonArray messageArray) +void CounterWindowRT::onChannelSettingSubmit(QJsonArray channelSettings) { - if (messageArray.isEmpty() == false) - { - // 获取设备id属性 - QString deviceId = messageArray.at(0).toObject().find("deviceId")->toString(); - - // 如果不是当前显示的计数器 则直接返回 - CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); - if (SettingConfig::getInstance().MASTER != devicePtr->getIsUse()) - { - return; - } - - // 根据设备id 从map中取出列索引的值 - int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); - if (devIdx > SettingConfig::getInstance().DISPLAY_COUNT) - { - return; - } - - // 参考通道 - int refChNo = messageArray.at(0).toObject().find("data")->toObject().find("channelRefNo")->toInt(); - CounterChannel * refChann = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(refChNo)); - if (refChann != nullptr) - { - if (devIdx < devRefList.size()) { - devRefList.at(devIdx)->setText(QString("REF:%1").arg(refChann->getChannelCode())); - } - } - - updateChannelDataForm(messageArray); - } + httpReq->updateChannelInfo(channelSettings); } -void CounterWindowRT::updateDeviceWidget() +int CounterWindowRT::initHttpToken() { - QList devIds = SettingConfig::getInstance().MASTER == 1 ? ConstCache::getInstance().deviceIdLoopA : ConstCache::getInstance().deviceIdLoopB; - - for (int i = 0; i < SettingConfig::getInstance().DISPLAY_COUNT; i++) + QJsonObject response = httpReq->getTokenByClientId(SettingConfig::getInstance().CLIENT_ID, + SettingConfig::getInstance().APP_KEY); + return response.value("code").toInt(); +} +void CounterWindowRT::getDeviceList() +{ + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.value("code").toInt() == 200) { - if (i < devIds.size()) + // 将获取到的设备绘制到表格中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) { - CounterDevice * dev = ConstCache::getInstance().deviceMap.value(devIds.at(i)); + // 接口返回的device对象 + QJsonObject devItem = devArray.at(i).toObject(); + QString devName = devItem.value("simpleName").toString(); // 设备名称 + QString deviceId = devItem.value("deviceId").toString(); // 设备ID + QString isUse = devItem.value("isUse").toString(); // 标识A路/B路 - QString devName = dev->getDeviceName(); // 设备名称 - devTitleList.at(i)->setText(devName.right(devName.length() - 8)); - dev->setFormColIndex(i); - } else { - devTitleList.at(i)->setText(""); - devRefList.at(i)->setText(""); + // 构建Device对象 并赋值id、串口、波特率、设备编号等属性 + CounterDevice * device = new CounterDevice(this); + device->setDeviceId(deviceId); + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceName(devName); + device->setIsUse(isUse.toInt()); - for (int j = 0; j < 16; j++) + ConstCache::getInstance().deviceMap.insert(deviceId, device); + if (isUse == "1") { - channelNameList.at(i * 16 + j)->setText(""); - channelValueList.at(i * 16 + j)->setText(""); + ConstCache::getInstance().deviceIdLoopA.append(deviceId); + } else if (isUse == "0") { + ConstCache::getInstance().deviceIdLoopB.append(deviceId); } + + // 绑定显示函数 + connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); + + // 查询计数器的通道 并初始化串口 + this->getChannelList(deviceId); } + + updateDeviceWidget(); } } - -void CounterWindowRT::initChannelForm() +void CounterWindowRT::getChannelList(QString deviceId) { - // 1. 首先清除界面上的所有内容 - qDeleteAll(ui->widgetContent->children()); + // 查询计数器的通道 + QJsonObject response = httpReq->getChannelList(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 + QString channelCode = channelItem.value("channelCode").toString(); // 通道钟号 + QString channelNo = QString::number(channelItem.value("channelNo").toInt()); // 通道序号 + QString delays = channelItem.value("delays").toString(); // 通道时延值 + QString isUse = channelItem.value("isUse").toString(); + QString name = channelItem.value("name").toString(); + bool active = channelItem.value("isActive").toString().toInt(); // 通道是否启用 - // 将lab列表的数量置0 - devTitleList.resize(0); - devRefList.resize(0); - channelNameList.resize(0); - channelValueList.resize(0); + // 构建通道对象 并设置属性 + CounterChannel * channel = new CounterChannel(this); + channel->setDeviceId(deviceId); + channel->setChannelId(channelId); + channel->setChannelCode(channelCode); + channel->setChannelNo(channelNo); + channel->setDelays(delays); + channel->setActive(active); + channel->setIsUse(isUse); + channel->setName(name); - initDevAndChannelForm(SettingConfig::getInstance().DISPLAY_COUNT); + // 添加到通道的集合中 key=deviceId-channelNo + ConstCache::getInstance().channelMap.insert(deviceId + "-" + channelNo, channel); + } + } } void CounterWindowRT::initDevAndChannelForm(int count) @@ -221,7 +237,7 @@ for ( int i = 0; i < 16; i++ ) { QLabelDblClick * labChannelName = new QLabelDblClick(widgetDev); // 通道名 - QLabel * labChannelValue = new QLabel(widgetDev); // 通道测量值 + QLabelDblClick * labChannelValue = new QLabelDblClick(widgetDev); // 通道测量值 // 默认显示内容 labChannelName->setText(QString("CH%1").arg(i+1, 2, 10, QLatin1Char('0'))); @@ -247,6 +263,7 @@ // 添加通道名称的双击事件,显示数据和残差图表 connect(labChannelName, &QLabelDblClick::doubleClicked, this, &CounterWindowRT::onChannelNameLabelDblClicked); + connect(labChannelValue, &QLabelDblClick::doubleClicked, this, &CounterWindowRT::onChannelNameLabelDblClicked); channelNameList.append(labChannelName); channelValueList.append(labChannelValue); @@ -263,14 +280,13 @@ widgetDev->setLayout(layoutChannelPage); } } - void CounterWindowRT::updateChannelDataForm(QJsonArray messageArray) { // 获取设备id属性 - QString deviceId = messageArray.at(0).toObject().find("deviceId")->toString(); + QString deviceId = messageArray.at(0).toObject().value("deviceId").toString(); // 参考通道号 - int refChNo = messageArray.at(0).toObject().find("data")->toObject().find("channelRefNo")->toInt(); + int refChNo = messageArray.at(0).toObject().value("data").toObject().value("channelRefNo").toInt(); // 根据设备id 从map中取出列索引的值 int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); @@ -285,7 +301,7 @@ for (int i = 0; i < messageArray.size(); i++) { QJsonObject channelDataItem = messageArray.at(i).toObject(); - QString channelNo = QString::number(channelDataItem.find("channelNo")->toInt()); // 通道序号 + QString channelNo = QString::number(channelDataItem.value("channelNo").toInt()); // 通道序号 channelDataJsonMap.insert(channelNo, channelDataItem); } @@ -306,13 +322,13 @@ CounterChannel * channel = ConstCache::getInstance().channelMap.value(deviceId + "-" + channelNo); // 从map中找到对应的通道 // 通道Label的序号 - int chanelIdx = devIdx * 16 + channelDataItem.find("channelNo")->toInt() - 1; + int chanelIdx = devIdx * 16 + channelDataItem.value("channelNo").toInt() - 1; if (channel != nullptr && channel->isActive() == true) { // 通道时延值 并计算 减去时延值的测量值 double delay = channel->getDelays().isEmpty() == false ? channel->getDelays().toDouble() : 0.0; - double valueMinusDelay = channelDataItem.find("data")->toObject().find("dataValue")->toString().toDouble() - delay * 1E-9; + double valueMinusDelay = channelDataItem.value("data").toObject().value("dataValue").toString().toDouble() - delay * 1E-9; // 设置通道名和测量值的QLabel channelNameList.at(chanelIdx)->setText(QString("CH%1:%2").arg(channelNoStr).arg(channel->getChannelCode())); @@ -321,6 +337,10 @@ channelNameList.at(chanelIdx)->setProperty("channelId", channel->getChannelId()); channelNameList.at(chanelIdx)->setProperty("deviceId", deviceId); channelNameList.at(chanelIdx)->setProperty("channelNo", channelNo); + + channelValueList.at(chanelIdx)->setProperty("channelId", channel->getChannelId()); + channelValueList.at(chanelIdx)->setProperty("deviceId", deviceId); + channelValueList.at(chanelIdx)->setProperty("channelNo", channelNo); } else { channelNameList.at(chanelIdx)->setText(QString("CH%1:%2").arg(channelNoStr).arg("-")); channelValueList.at(chanelIdx)->setText(""); @@ -333,84 +353,11 @@ } } -int CounterWindowRT::initHttpToken() +void CounterWindowRT::updateDateAndTime() { - QJsonObject response = httpReq->getTokenByClientId(SettingConfig::getInstance().CLIENT_ID, - SettingConfig::getInstance().APP_KEY); - return response.find("code")->toInt(); -} -void CounterWindowRT::getDeviceList() -{ - QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - if (devListRes.find("code")->toInt() == 200) - { - // 将获取到的设备绘制到表格中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - // 接口返回的device对象 - QJsonObject devItem = devArray.at(i).toObject(); - QString devName = devItem.find("deviceName")->toString(); // 设备名称 - QString deviceId = devItem.find("deviceId")->toString(); // 设备ID - QString isUse = devItem.find("isUse")->toString(); // 标识A路/B路 - - // 构建Device对象 并赋值id、串口、波特率、设备编号等属性 - CounterDevice * device = new CounterDevice(this); - device->setDeviceId(deviceId); - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceName(devName); - device->setIsUse(isUse.toInt()); - - ConstCache::getInstance().deviceMap.insert(deviceId, device); - if (isUse == "1") - { - ConstCache::getInstance().deviceIdLoopA.append(deviceId); - } else if (isUse == "0") { - ConstCache::getInstance().deviceIdLoopB.append(deviceId); - } - - // 绑定显示函数 - connect(device, &CounterDevice::sendDataToDraw, - this, &CounterWindowRT::drawCounterDataOnPage); - - // 查询计数器的通道 并初始化串口 - this->getChannelList(deviceId); - } - - updateDeviceWidget(); - } -} -void CounterWindowRT::getChannelList(QString deviceId) -{ - // 查询计数器的通道 - QJsonObject response = httpReq->getChannelList(deviceId); - if (response.find("code")->toInt() == 200) - { - // 将通道绘制在界面上 - QJsonArray channelArray = response.find("data")->toArray(); - for (int i = 0; i < channelArray.size(); i++) - { - // 接口返回的channel对象 - QJsonObject channelItem = channelArray.at(i).toObject(); - QString channelId = channelItem.find("id")->toString(); // 通道ID - QString channelCode = channelItem.find("channelCode")->toString(); // 通道钟号 - QString channelNo = QString::number(channelItem.find("channelNo")->toInt()); // 通道序号 - QString delays = channelItem.find("delays")->toString(); // 通道时延值 - - // 构建通道对象 并设置属性 - CounterChannel * channel = new CounterChannel(this); - channel->setDeviceId(deviceId); - channel->setChannelId(channelId); - channel->setChannelCode(channelCode); - channel->setChannelNo(channelNo); - channel->setDelays(delays); - - // 添加到通道的集合中 key=deviceId-channelNo - ConstCache::getInstance().channelMap.insert(deviceId + "-" + channelNo, channel); - } - } + QString date = QDate::currentDate().toString("yyyy-MM-dd"); + QString time = QTime::currentTime().toString("HH:mm:ss"); + ui->labTime->setText(date + " " + time); } void CounterWindowRT::on_btnMenuSetting_clicked() @@ -425,6 +372,47 @@ setWindowState(Qt::WindowMinimized | windowState()); } +void CounterWindowRT::initChannelForm() +{ + // 1. 首先清除界面上的所有内容 + qDeleteAll(ui->widgetContent->children()); + + // 将lab列表的数量置0 + devTitleList.resize(0); + devRefList.resize(0); + channelNameList.resize(0); + channelValueList.resize(0); + + initDevAndChannelForm(SettingConfig::getInstance().DISPLAY_COUNT); +} + +void CounterWindowRT::updateDeviceWidget() +{ + QList devIds = SettingConfig::getInstance().MASTER == 1 ? ConstCache::getInstance().deviceIdLoopA : ConstCache::getInstance().deviceIdLoopB; + + for (int i = 0; i < SettingConfig::getInstance().DISPLAY_COUNT; i++) + { + if (i < devIds.size()) + { + CounterDevice * dev = ConstCache::getInstance().deviceMap.value(devIds.at(i)); + + if (dev != nullptr) { + devTitleList.at(i)->setText(dev->getDeviceName()); // 设备名称 + dev->setFormColIndex(i); + } + } else { + devTitleList.at(i)->setText(""); + devRefList.at(i)->setText(""); + + for (int j = 0; j < 16; j++) + { + channelNameList.at(i * 16 + j)->setText(""); + channelValueList.at(i * 16 + j)->setText(""); + } + } + } +} + void CounterWindowRT::onChannelNameLabelDblClicked() { QLabelDblClick * channelLab = (QLabelDblClick *) sender(); @@ -445,10 +433,50 @@ } } -void CounterWindowRT::updateDateAndTime() + +/** + * @brief CounterWindowRT::drawCounterDataOnPage + * @param messageArray + * 根据发送到Kafka中的数据串来绘制界面 + * + */ +void CounterWindowRT::drawCounterDataOnPage(QJsonArray messageArray) { - QString date = QDate::currentDate().toString("yyyy-MM-dd"); - QString time = QTime::currentTime().toString("HH:mm:ss"); - ui->labTime->setText(date + " " + time); + if (messageArray.isEmpty() == false) + { + // 获取设备id属性 + QString deviceId = messageArray.at(0).toObject().value("deviceId").toString(); + + // 如果不是当前显示的计数器 则直接返回 + CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (SettingConfig::getInstance().MASTER != devicePtr->getIsUse()) + { + return; + } + + // 根据设备id 从map中取出列索引的值 + int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); + if (devIdx > SettingConfig::getInstance().DISPLAY_COUNT) + { + return; + } + + // 参考通道 + int refChNo = messageArray.at(0).toObject().value("data").toObject().value("channelRefNo").toInt(); + CounterChannel * refChann = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(refChNo)); + if (refChann != nullptr) + { + if (devIdx < devRefList.size()) { + devRefList.at(devIdx)->setText(QString("REF:%1").arg(refChann->getChannelCode())); + } + } + + updateChannelDataForm(messageArray); + } } + +void CounterWindowRT::onKafkaMessageReceived(QJsonObject message) +{ + qDebug() << message; +} diff --git a/CounterRealTime/CounterWindowRT.h b/CounterRealTime/CounterWindowRT.h index 3f21ce7..c64e07f 100644 --- a/CounterRealTime/CounterWindowRT.h +++ b/CounterRealTime/CounterWindowRT.h @@ -18,6 +18,7 @@ #include "common/utils/SettingConfig.h" #include "common/utils/TimerCounterUtil.h" #include "common/HttpRequestController.h" +#include "common/utils/QKafkaConsumer.h" QT_BEGIN_NAMESPACE namespace Ui { class CounterWindowRT; } @@ -32,10 +33,9 @@ ~CounterWindowRT(); public slots: - void drawCounterDataOnPage(QJsonArray messageArray); + void keyPressEvent(QKeyEvent *event); - void updateDeviceWidget(); - void initChannelForm(); + void onChannelSettingSubmit(QJsonArray channelSettings); private: Ui::CounterWindowRT *ui; @@ -49,24 +49,32 @@ HttpRequestController * httpReq; - void keyPressEvent(QKeyEvent *event); + QKafkaConsumer * kafkaConsumer; int initHttpToken(); void getDeviceList(); void getChannelList(QString deviceId); void initDevAndChannelForm(int count); - void updateChannelDataForm(QJsonArray messageArray); private slots: + void updateDateAndTime(); void on_btnMenuMin_clicked(); void on_btnMenuSetting_clicked(); - void onChannelNameLabelDblClicked(); + void initChannelForm(); + void updateDeviceWidget(); - void updateDateAndTime(); + void onChannelNameLabelDblClicked(); + void drawCounterDataOnPage(QJsonArray messageArray); + + void onKafkaMessageReceived(QJsonObject message); + + + + }; #endif // COUNTERWINDOWRT_H diff --git a/CounterRealTime/CounterChannel.cpp b/CounterRealTime/CounterChannel.cpp index 978b969..0a4b534 100644 --- a/CounterRealTime/CounterChannel.cpp +++ b/CounterRealTime/CounterChannel.cpp @@ -30,6 +30,14 @@ { return delays; } +QString CounterChannel::getIsUse() +{ + return isUse; +} +QString CounterChannel::getName() +{ + return name; +} void CounterChannel::setActive(bool active) { this->active = active; @@ -54,14 +62,13 @@ { this->delays = delays; } - -int CounterChannel::getFormRowIndex() +void CounterChannel::setIsUse(QString isUse) { - return formRowIndex; + this->isUse = isUse; } -void CounterChannel::setFormRowIndex(int rowIndex) +void CounterChannel::setName(QString name) { - this->formRowIndex = rowIndex; + this->name = name; } QList CounterChannel::getHisData() diff --git a/CounterRealTime/CounterChannel.h b/CounterRealTime/CounterChannel.h index afc5dc2..8f2c35e 100644 --- a/CounterRealTime/CounterChannel.h +++ b/CounterRealTime/CounterChannel.h @@ -15,15 +15,16 @@ QString getChannelCode(); QString getChannelNo(); QString getDelays(); + QString getIsUse(); + QString getName(); void setActive(bool active); void setDeviceId(QString deviceId); void setChannelId(QString channelId); void setChannelCode(QString channelCode); void setChannelNo(QString channelNo); void setDelays(QString delays); - - int getFormRowIndex(); - void setFormRowIndex(int rowIndex); + void setIsUse(QString isUse); + void setName(QString name); QList getHisData(); void appendHisData(QString timestampStr, QString valueStr); @@ -35,13 +36,12 @@ QString channelCode; QString channelNo; QString delays; + QString isUse; + QString name; int maxHisDataCount = 3600; QList hisDataList; - // 通道位于界面上的哪一行 列的位置通过计数器来确定 - int formRowIndex; - signals: }; diff --git a/CounterRealTime/CounterDevice.cpp b/CounterRealTime/CounterDevice.cpp index 63200bb..1528373 100644 --- a/CounterRealTime/CounterDevice.cpp +++ b/CounterRealTime/CounterDevice.cpp @@ -1,6 +1,5 @@ #include "CounterDevice.h" -#include #include #include @@ -95,7 +94,7 @@ void CounterDevice::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); -// std::cout << this->dataBuff.toStdString(); +// qDebug() << this->dataBuff; CounterDataDto * counterData = new CounterDataDto(this); if (CounterProtocolXH::checkFrame(this->dataBuff) == true) @@ -117,7 +116,7 @@ } } else if (this->dataBuff.size() > COUNTER_FRAME_MIN_LENGTH) { - std::cout << QString("%1").arg(this->dataBuff.size()).toStdString() << std::endl; + qDebug() << QString("%1").arg(this->dataBuff.size()); this->dataBuff.clear(); } @@ -193,7 +192,7 @@ } // qDebug() << counterData->rawFrame; - qDebug() << messageArray; +// qDebug() << messageArray; // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(messageArray); diff --git a/CounterRealTime/CounterSettingForm.cpp b/CounterRealTime/CounterSettingForm.cpp index c095523..225ec88 100644 --- a/CounterRealTime/CounterSettingForm.cpp +++ b/CounterRealTime/CounterSettingForm.cpp @@ -283,43 +283,68 @@ { QString deviceId = ui->selectCounter->currentData().toString(); - for (int i = 0; i < 8; i++) + QJsonArray channelSettingUpdated; + + for (int i = 0; i < 16; i++) { + QString channelCode; + double delay; + bool active; + if (i < 8) { + channelCode = clockListLeft.at(i)->text(); + delay = delayListLeft.at(i)->value(); + active = activeListLeft.at(i)->isChecked(); + } else if (i < 16) { + channelCode = clockListRight.at(i - 8)->text(); + delay = delayListRight.at(i - 8)->value(); + active = activeListRight.at(i - 8)->isChecked(); + } + CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); if (channelPrt != nullptr) { - QString channelCode = clockListLeft.at(i)->text(); // 通道钟号 - double delay = delayListLeft.at(i)->value(); // 通道时延值 + bool isUpdated = isChannelSettingUpdated(i, channelPrt); + if (isUpdated == true) { + channelPrt->setChannelCode(channelCode); + channelPrt->setDelays(QString::number(delay)); + channelPrt->setActive(active); - channelPrt->setChannelCode(channelCode); - channelPrt->setDelays(QString::number(delay)); - channelPrt->setActive(activeListLeft.at(i)->isChecked()); - } else if (activeListLeft.at(i)->isChecked() == true) { + QJsonObject channelObj; + channelObj.insert("id", channelPrt->getChannelId()); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", channelPrt->getChannelNo()); + channelObj.insert("isUse", channelPrt->getIsUse()); + channelObj.insert("name", channelPrt->getName()); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); + } + } else if (active == true) { CounterChannel * newChannel = new CounterChannel(); newChannel->setActive(true); - newChannel->setChannelCode(clockListLeft.at(i)->text()); - newChannel->setDelays(QString::number(delayListLeft.at(i)->value())); + newChannel->setChannelCode(channelCode); + newChannel->setDelays(QString::number(delay)); ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1), newChannel); + + QJsonObject channelObj; + channelObj.insert("id", ""); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", QString::number(i + 1)); + channelObj.insert("isUse", SettingConfig::getInstance().MASTER); + channelObj.insert("name", QString("钟%1-1PPS").arg(channelCode)); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); } + } - CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr) - { - QString channelCode = clockListRight.at(i)->text(); // 通道钟号 - double delay = delayListRight.at(i)->value(); // 通道时延值 - - channelRightPrt->setChannelCode(channelCode); - channelRightPrt->setDelays(QString::number(delay)); - channelRightPrt->setActive(activeListRight.at(i)->isChecked()); - } else if (activeListRight.at(i)->isChecked() == true) { - CounterChannel * newChannel = new CounterChannel(); - newChannel->setActive(true); - newChannel->setChannelCode(clockListRight.at(i)->text()); - newChannel->setDelays(QString::number(delayListRight.at(i)->value())); - - ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1+8), newChannel); - } + if (channelSettingUpdated.size() > 0) { + emit this->channelSettingSubmit(channelSettingUpdated); } this->close(); @@ -341,10 +366,10 @@ void CounterSettingForm::on_selectCounter_currentIndexChanged(int index) { - if (index == 0) + clearChannelSettingValue(); + + if (index > 0) { - clearChannelSettingValue(); - } else { QString deviceId = ui->selectCounter->currentData().toString(); for (int i = 0; i < 8; i++) @@ -353,45 +378,37 @@ activeListRight.at(i)->setCheckable(true); CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); - if (channelPrt != nullptr && channelPrt->isActive() == true) + if (channelPrt != nullptr) { QString channelCode = channelPrt->getChannelCode(); // 通道钟号 QString delays = channelPrt->getDelays(); // 通道时延值 - activeListLeft.at(i)->setChecked(true); - activeListLeft.at(i)->setText("启用"); clockListLeft.at(i)->setText(channelCode); - clockListLeft.at(i)->setReadOnly(false); delayListLeft.at(i)->setValue(delays.toDouble()); - delayListLeft.at(i)->setReadOnly(false); - } else { - activeListLeft.at(i)->setChecked(false); - activeListLeft.at(i)->setText("未启用"); - clockListLeft.at(i)->setText("-"); - clockListLeft.at(i)->setReadOnly(true); - delayListLeft.at(i)->setValue(0.000); - delayListLeft.at(i)->setReadOnly(true); + + if (channelPrt->isActive() == true) { + activeListLeft.at(i)->setChecked(true); + activeListLeft.at(i)->setText("启用"); + clockListLeft.at(i)->setReadOnly(false); + delayListLeft.at(i)->setReadOnly(false); + } } CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr && channelRightPrt->isActive() == true) + if (channelRightPrt != nullptr) { QString channelCode = channelRightPrt->getChannelCode(); // 通道钟号 QString delays = channelRightPrt->getDelays(); // 通道时延值 - activeListRight.at(i)->setChecked(true); - activeListRight.at(i)->setText("启用"); clockListRight.at(i)->setText(channelCode); - clockListRight.at(i)->setReadOnly(false); delayListRight.at(i)->setValue(delays.toDouble()); - delayListRight.at(i)->setReadOnly(false); - } else { - activeListRight.at(i)->setChecked(false); - activeListRight.at(i)->setText("未启用"); - clockListRight.at(i)->setText("-"); - clockListRight.at(i)->setReadOnly(true); - delayListRight.at(i)->setValue(0.000); - delayListRight.at(i)->setReadOnly(true); + + if (channelRightPrt->isActive() == true) { + activeListRight.at(i)->setChecked(true); + activeListRight.at(i)->setText("启用"); + clockListRight.at(i)->setReadOnly(false); + delayListRight.at(i)->setReadOnly(false); + } } } } @@ -423,3 +440,35 @@ delayListRight.at(index - 8)->setReadOnly(!enable); } } + +bool CounterSettingForm::isChannelSettingUpdated(int index, CounterChannel *channelPtr) +{ + QString channelCode = ""; // 通道钟号 + double delay = 0.0; // 通道时延值 + bool active = false; + + if (index < 8) { + channelCode = clockListLeft.at(index)->text(); // 通道钟号 + delay = delayListLeft.at(index)->value(); // 通道时延值 + active = activeListLeft.at(index)->isChecked(); + } else if (index < 16) { + index = index - 8; + channelCode = clockListRight.at(index)->text(); // 通道钟号 + delay = delayListRight.at(index)->value(); // 通道时延值 + active = activeListRight.at(index)->isChecked(); + } + + if (channelCode != channelPtr->getChannelCode()) { + return true; + } + + if (active != channelPtr->isActive()) { + return true; + } + + if (delay - channelPtr->getDelays().toDouble() != 0) { + return true; + } + + return false; +} diff --git a/CounterRealTime/CounterSettingForm.h b/CounterRealTime/CounterSettingForm.h index b4949fb..9c41362 100644 --- a/CounterRealTime/CounterSettingForm.h +++ b/CounterRealTime/CounterSettingForm.h @@ -55,10 +55,12 @@ void clearChannelSettingValue(); void enableChannel(int index, bool enable); + bool isChannelSettingUpdated(int index, CounterChannel * channelPtr); signals: void swiftDisplayLoop(); void swiftDisplayCount(); + void channelSettingSubmit(QJsonArray channelSettings); }; #endif // COUNTERSETTINGFORM_H diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index 55db2ed..86a7776 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -12,6 +12,7 @@ resize(SettingConfig::getInstance().WINDOW_WIDTH, SettingConfig::getInstance().WINDOW_HEIGHT); settingForm = new CounterSettingForm(); + connect(settingForm, &CounterSettingForm::channelSettingSubmit, this, &CounterWindowRT::onChannelSettingSubmit); QFile file(":/qss/mainClock.css"); if (file.open(QFile::ReadOnly)) { @@ -22,6 +23,14 @@ file.close(); } + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_MSG_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &CounterWindowRT::onKafkaMessageReceived); + // 绘制界面上的表格 initChannelForm(); @@ -31,7 +40,6 @@ // 重新选择显示多少个计数器 connect(settingForm, &CounterSettingForm::swiftDisplayLoop, this, &CounterWindowRT::updateDeviceWidget); - connect(settingForm, &CounterSettingForm::swiftDisplayCount, this, &CounterWindowRT::initChannelForm); httpReq = new HttpRequestController(this); @@ -47,7 +55,7 @@ // 初始化设备和通道 getDeviceList(); -// QThread::msleep(1000 * 3); + QThread::msleep(1000 * 3); for (int i = 0; i < ConstCache::getInstance().deviceIdLoopA.size(); i++) { CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(ConstCache::getInstance().deviceIdLoopA.at(i)); @@ -58,7 +66,6 @@ CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(ConstCache::getInstance().deviceIdLoopB.at(i)); devicePtr->initSerialPort(); } - } CounterWindowRT::~CounterWindowRT() @@ -77,85 +84,94 @@ } } -/** - * @brief CounterWindowRT::drawCounterDataOnPage - * @param messageArray - * 根据发送到Kafka中的数据串来绘制界面 - * - */ -void CounterWindowRT::drawCounterDataOnPage(QJsonArray messageArray) +void CounterWindowRT::onChannelSettingSubmit(QJsonArray channelSettings) { - if (messageArray.isEmpty() == false) - { - // 获取设备id属性 - QString deviceId = messageArray.at(0).toObject().find("deviceId")->toString(); - - // 如果不是当前显示的计数器 则直接返回 - CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); - if (SettingConfig::getInstance().MASTER != devicePtr->getIsUse()) - { - return; - } - - // 根据设备id 从map中取出列索引的值 - int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); - if (devIdx > SettingConfig::getInstance().DISPLAY_COUNT) - { - return; - } - - // 参考通道 - int refChNo = messageArray.at(0).toObject().find("data")->toObject().find("channelRefNo")->toInt(); - CounterChannel * refChann = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(refChNo)); - if (refChann != nullptr) - { - if (devIdx < devRefList.size()) { - devRefList.at(devIdx)->setText(QString("REF:%1").arg(refChann->getChannelCode())); - } - } - - updateChannelDataForm(messageArray); - } + httpReq->updateChannelInfo(channelSettings); } -void CounterWindowRT::updateDeviceWidget() +int CounterWindowRT::initHttpToken() { - QList devIds = SettingConfig::getInstance().MASTER == 1 ? ConstCache::getInstance().deviceIdLoopA : ConstCache::getInstance().deviceIdLoopB; - - for (int i = 0; i < SettingConfig::getInstance().DISPLAY_COUNT; i++) + QJsonObject response = httpReq->getTokenByClientId(SettingConfig::getInstance().CLIENT_ID, + SettingConfig::getInstance().APP_KEY); + return response.value("code").toInt(); +} +void CounterWindowRT::getDeviceList() +{ + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.value("code").toInt() == 200) { - if (i < devIds.size()) + // 将获取到的设备绘制到表格中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) { - CounterDevice * dev = ConstCache::getInstance().deviceMap.value(devIds.at(i)); + // 接口返回的device对象 + QJsonObject devItem = devArray.at(i).toObject(); + QString devName = devItem.value("simpleName").toString(); // 设备名称 + QString deviceId = devItem.value("deviceId").toString(); // 设备ID + QString isUse = devItem.value("isUse").toString(); // 标识A路/B路 - QString devName = dev->getDeviceName(); // 设备名称 - devTitleList.at(i)->setText(devName.right(devName.length() - 8)); - dev->setFormColIndex(i); - } else { - devTitleList.at(i)->setText(""); - devRefList.at(i)->setText(""); + // 构建Device对象 并赋值id、串口、波特率、设备编号等属性 + CounterDevice * device = new CounterDevice(this); + device->setDeviceId(deviceId); + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceName(devName); + device->setIsUse(isUse.toInt()); - for (int j = 0; j < 16; j++) + ConstCache::getInstance().deviceMap.insert(deviceId, device); + if (isUse == "1") { - channelNameList.at(i * 16 + j)->setText(""); - channelValueList.at(i * 16 + j)->setText(""); + ConstCache::getInstance().deviceIdLoopA.append(deviceId); + } else if (isUse == "0") { + ConstCache::getInstance().deviceIdLoopB.append(deviceId); } + + // 绑定显示函数 + connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); + + // 查询计数器的通道 并初始化串口 + this->getChannelList(deviceId); } + + updateDeviceWidget(); } } - -void CounterWindowRT::initChannelForm() +void CounterWindowRT::getChannelList(QString deviceId) { - // 1. 首先清除界面上的所有内容 - qDeleteAll(ui->widgetContent->children()); + // 查询计数器的通道 + QJsonObject response = httpReq->getChannelList(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 + QString channelCode = channelItem.value("channelCode").toString(); // 通道钟号 + QString channelNo = QString::number(channelItem.value("channelNo").toInt()); // 通道序号 + QString delays = channelItem.value("delays").toString(); // 通道时延值 + QString isUse = channelItem.value("isUse").toString(); + QString name = channelItem.value("name").toString(); + bool active = channelItem.value("isActive").toString().toInt(); // 通道是否启用 - // 将lab列表的数量置0 - devTitleList.resize(0); - devRefList.resize(0); - channelNameList.resize(0); - channelValueList.resize(0); + // 构建通道对象 并设置属性 + CounterChannel * channel = new CounterChannel(this); + channel->setDeviceId(deviceId); + channel->setChannelId(channelId); + channel->setChannelCode(channelCode); + channel->setChannelNo(channelNo); + channel->setDelays(delays); + channel->setActive(active); + channel->setIsUse(isUse); + channel->setName(name); - initDevAndChannelForm(SettingConfig::getInstance().DISPLAY_COUNT); + // 添加到通道的集合中 key=deviceId-channelNo + ConstCache::getInstance().channelMap.insert(deviceId + "-" + channelNo, channel); + } + } } void CounterWindowRT::initDevAndChannelForm(int count) @@ -221,7 +237,7 @@ for ( int i = 0; i < 16; i++ ) { QLabelDblClick * labChannelName = new QLabelDblClick(widgetDev); // 通道名 - QLabel * labChannelValue = new QLabel(widgetDev); // 通道测量值 + QLabelDblClick * labChannelValue = new QLabelDblClick(widgetDev); // 通道测量值 // 默认显示内容 labChannelName->setText(QString("CH%1").arg(i+1, 2, 10, QLatin1Char('0'))); @@ -247,6 +263,7 @@ // 添加通道名称的双击事件,显示数据和残差图表 connect(labChannelName, &QLabelDblClick::doubleClicked, this, &CounterWindowRT::onChannelNameLabelDblClicked); + connect(labChannelValue, &QLabelDblClick::doubleClicked, this, &CounterWindowRT::onChannelNameLabelDblClicked); channelNameList.append(labChannelName); channelValueList.append(labChannelValue); @@ -263,14 +280,13 @@ widgetDev->setLayout(layoutChannelPage); } } - void CounterWindowRT::updateChannelDataForm(QJsonArray messageArray) { // 获取设备id属性 - QString deviceId = messageArray.at(0).toObject().find("deviceId")->toString(); + QString deviceId = messageArray.at(0).toObject().value("deviceId").toString(); // 参考通道号 - int refChNo = messageArray.at(0).toObject().find("data")->toObject().find("channelRefNo")->toInt(); + int refChNo = messageArray.at(0).toObject().value("data").toObject().value("channelRefNo").toInt(); // 根据设备id 从map中取出列索引的值 int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); @@ -285,7 +301,7 @@ for (int i = 0; i < messageArray.size(); i++) { QJsonObject channelDataItem = messageArray.at(i).toObject(); - QString channelNo = QString::number(channelDataItem.find("channelNo")->toInt()); // 通道序号 + QString channelNo = QString::number(channelDataItem.value("channelNo").toInt()); // 通道序号 channelDataJsonMap.insert(channelNo, channelDataItem); } @@ -306,13 +322,13 @@ CounterChannel * channel = ConstCache::getInstance().channelMap.value(deviceId + "-" + channelNo); // 从map中找到对应的通道 // 通道Label的序号 - int chanelIdx = devIdx * 16 + channelDataItem.find("channelNo")->toInt() - 1; + int chanelIdx = devIdx * 16 + channelDataItem.value("channelNo").toInt() - 1; if (channel != nullptr && channel->isActive() == true) { // 通道时延值 并计算 减去时延值的测量值 double delay = channel->getDelays().isEmpty() == false ? channel->getDelays().toDouble() : 0.0; - double valueMinusDelay = channelDataItem.find("data")->toObject().find("dataValue")->toString().toDouble() - delay * 1E-9; + double valueMinusDelay = channelDataItem.value("data").toObject().value("dataValue").toString().toDouble() - delay * 1E-9; // 设置通道名和测量值的QLabel channelNameList.at(chanelIdx)->setText(QString("CH%1:%2").arg(channelNoStr).arg(channel->getChannelCode())); @@ -321,6 +337,10 @@ channelNameList.at(chanelIdx)->setProperty("channelId", channel->getChannelId()); channelNameList.at(chanelIdx)->setProperty("deviceId", deviceId); channelNameList.at(chanelIdx)->setProperty("channelNo", channelNo); + + channelValueList.at(chanelIdx)->setProperty("channelId", channel->getChannelId()); + channelValueList.at(chanelIdx)->setProperty("deviceId", deviceId); + channelValueList.at(chanelIdx)->setProperty("channelNo", channelNo); } else { channelNameList.at(chanelIdx)->setText(QString("CH%1:%2").arg(channelNoStr).arg("-")); channelValueList.at(chanelIdx)->setText(""); @@ -333,84 +353,11 @@ } } -int CounterWindowRT::initHttpToken() +void CounterWindowRT::updateDateAndTime() { - QJsonObject response = httpReq->getTokenByClientId(SettingConfig::getInstance().CLIENT_ID, - SettingConfig::getInstance().APP_KEY); - return response.find("code")->toInt(); -} -void CounterWindowRT::getDeviceList() -{ - QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - if (devListRes.find("code")->toInt() == 200) - { - // 将获取到的设备绘制到表格中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - // 接口返回的device对象 - QJsonObject devItem = devArray.at(i).toObject(); - QString devName = devItem.find("deviceName")->toString(); // 设备名称 - QString deviceId = devItem.find("deviceId")->toString(); // 设备ID - QString isUse = devItem.find("isUse")->toString(); // 标识A路/B路 - - // 构建Device对象 并赋值id、串口、波特率、设备编号等属性 - CounterDevice * device = new CounterDevice(this); - device->setDeviceId(deviceId); - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceName(devName); - device->setIsUse(isUse.toInt()); - - ConstCache::getInstance().deviceMap.insert(deviceId, device); - if (isUse == "1") - { - ConstCache::getInstance().deviceIdLoopA.append(deviceId); - } else if (isUse == "0") { - ConstCache::getInstance().deviceIdLoopB.append(deviceId); - } - - // 绑定显示函数 - connect(device, &CounterDevice::sendDataToDraw, - this, &CounterWindowRT::drawCounterDataOnPage); - - // 查询计数器的通道 并初始化串口 - this->getChannelList(deviceId); - } - - updateDeviceWidget(); - } -} -void CounterWindowRT::getChannelList(QString deviceId) -{ - // 查询计数器的通道 - QJsonObject response = httpReq->getChannelList(deviceId); - if (response.find("code")->toInt() == 200) - { - // 将通道绘制在界面上 - QJsonArray channelArray = response.find("data")->toArray(); - for (int i = 0; i < channelArray.size(); i++) - { - // 接口返回的channel对象 - QJsonObject channelItem = channelArray.at(i).toObject(); - QString channelId = channelItem.find("id")->toString(); // 通道ID - QString channelCode = channelItem.find("channelCode")->toString(); // 通道钟号 - QString channelNo = QString::number(channelItem.find("channelNo")->toInt()); // 通道序号 - QString delays = channelItem.find("delays")->toString(); // 通道时延值 - - // 构建通道对象 并设置属性 - CounterChannel * channel = new CounterChannel(this); - channel->setDeviceId(deviceId); - channel->setChannelId(channelId); - channel->setChannelCode(channelCode); - channel->setChannelNo(channelNo); - channel->setDelays(delays); - - // 添加到通道的集合中 key=deviceId-channelNo - ConstCache::getInstance().channelMap.insert(deviceId + "-" + channelNo, channel); - } - } + QString date = QDate::currentDate().toString("yyyy-MM-dd"); + QString time = QTime::currentTime().toString("HH:mm:ss"); + ui->labTime->setText(date + " " + time); } void CounterWindowRT::on_btnMenuSetting_clicked() @@ -425,6 +372,47 @@ setWindowState(Qt::WindowMinimized | windowState()); } +void CounterWindowRT::initChannelForm() +{ + // 1. 首先清除界面上的所有内容 + qDeleteAll(ui->widgetContent->children()); + + // 将lab列表的数量置0 + devTitleList.resize(0); + devRefList.resize(0); + channelNameList.resize(0); + channelValueList.resize(0); + + initDevAndChannelForm(SettingConfig::getInstance().DISPLAY_COUNT); +} + +void CounterWindowRT::updateDeviceWidget() +{ + QList devIds = SettingConfig::getInstance().MASTER == 1 ? ConstCache::getInstance().deviceIdLoopA : ConstCache::getInstance().deviceIdLoopB; + + for (int i = 0; i < SettingConfig::getInstance().DISPLAY_COUNT; i++) + { + if (i < devIds.size()) + { + CounterDevice * dev = ConstCache::getInstance().deviceMap.value(devIds.at(i)); + + if (dev != nullptr) { + devTitleList.at(i)->setText(dev->getDeviceName()); // 设备名称 + dev->setFormColIndex(i); + } + } else { + devTitleList.at(i)->setText(""); + devRefList.at(i)->setText(""); + + for (int j = 0; j < 16; j++) + { + channelNameList.at(i * 16 + j)->setText(""); + channelValueList.at(i * 16 + j)->setText(""); + } + } + } +} + void CounterWindowRT::onChannelNameLabelDblClicked() { QLabelDblClick * channelLab = (QLabelDblClick *) sender(); @@ -445,10 +433,50 @@ } } -void CounterWindowRT::updateDateAndTime() + +/** + * @brief CounterWindowRT::drawCounterDataOnPage + * @param messageArray + * 根据发送到Kafka中的数据串来绘制界面 + * + */ +void CounterWindowRT::drawCounterDataOnPage(QJsonArray messageArray) { - QString date = QDate::currentDate().toString("yyyy-MM-dd"); - QString time = QTime::currentTime().toString("HH:mm:ss"); - ui->labTime->setText(date + " " + time); + if (messageArray.isEmpty() == false) + { + // 获取设备id属性 + QString deviceId = messageArray.at(0).toObject().value("deviceId").toString(); + + // 如果不是当前显示的计数器 则直接返回 + CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (SettingConfig::getInstance().MASTER != devicePtr->getIsUse()) + { + return; + } + + // 根据设备id 从map中取出列索引的值 + int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); + if (devIdx > SettingConfig::getInstance().DISPLAY_COUNT) + { + return; + } + + // 参考通道 + int refChNo = messageArray.at(0).toObject().value("data").toObject().value("channelRefNo").toInt(); + CounterChannel * refChann = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(refChNo)); + if (refChann != nullptr) + { + if (devIdx < devRefList.size()) { + devRefList.at(devIdx)->setText(QString("REF:%1").arg(refChann->getChannelCode())); + } + } + + updateChannelDataForm(messageArray); + } } + +void CounterWindowRT::onKafkaMessageReceived(QJsonObject message) +{ + qDebug() << message; +} diff --git a/CounterRealTime/CounterWindowRT.h b/CounterRealTime/CounterWindowRT.h index 3f21ce7..c64e07f 100644 --- a/CounterRealTime/CounterWindowRT.h +++ b/CounterRealTime/CounterWindowRT.h @@ -18,6 +18,7 @@ #include "common/utils/SettingConfig.h" #include "common/utils/TimerCounterUtil.h" #include "common/HttpRequestController.h" +#include "common/utils/QKafkaConsumer.h" QT_BEGIN_NAMESPACE namespace Ui { class CounterWindowRT; } @@ -32,10 +33,9 @@ ~CounterWindowRT(); public slots: - void drawCounterDataOnPage(QJsonArray messageArray); + void keyPressEvent(QKeyEvent *event); - void updateDeviceWidget(); - void initChannelForm(); + void onChannelSettingSubmit(QJsonArray channelSettings); private: Ui::CounterWindowRT *ui; @@ -49,24 +49,32 @@ HttpRequestController * httpReq; - void keyPressEvent(QKeyEvent *event); + QKafkaConsumer * kafkaConsumer; int initHttpToken(); void getDeviceList(); void getChannelList(QString deviceId); void initDevAndChannelForm(int count); - void updateChannelDataForm(QJsonArray messageArray); private slots: + void updateDateAndTime(); void on_btnMenuMin_clicked(); void on_btnMenuSetting_clicked(); - void onChannelNameLabelDblClicked(); + void initChannelForm(); + void updateDeviceWidget(); - void updateDateAndTime(); + void onChannelNameLabelDblClicked(); + void drawCounterDataOnPage(QJsonArray messageArray); + + void onKafkaMessageReceived(QJsonObject message); + + + + }; #endif // COUNTERWINDOWRT_H diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 20f6177..b287ed5 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -166,7 +166,6 @@ // 获取设备列表的接口地址 QUrl url = baseUrl + "/device/list"; -// QUrl url = baseUrl + "/device/deviceList.js"; QUrlQuery query; query.addQueryItem("type", devType); url.setQuery(query); @@ -201,7 +200,6 @@ // 获取设备列表的接口地址 QUrl url = baseUrl + "/channel/list"; -// QUrl url = baseUrl + "/device/channel-" + deviceId + ".js"; QUrlQuery query; query.addQueryItem("deviceId", deviceId); url.setQuery(query); @@ -224,7 +222,41 @@ resultObj.insert("code", -1); } -// qDebug() << resultObj; + qDebug() << resultObj; + + return resultObj; +} + +QJsonObject HttpRequestController::updateChannelInfo(QJsonArray channelInfos) +{ + QJsonObject resultObj; + + // 获取设备列表的接口地址 + QUrl url = baseUrl + "/channel/qtBatchSave"; + + QNetworkRequest request; + request.setUrl(url); + request.setRawHeader("Content-type", "application/json"); + request.setRawHeader("token", token.toLocal8Bit()); + + QJsonDocument jsonDoc = QJsonDocument(channelInfos); + QByteArray postData = jsonDoc.toJson(QJsonDocument::Compact); + + qDebug() << url; + qDebug() << channelInfos; + + QNetworkReply * reply = httpUtil->sendPostRequest(request, postData); + 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/CounterRealTime/CounterChannel.cpp b/CounterRealTime/CounterChannel.cpp index 978b969..0a4b534 100644 --- a/CounterRealTime/CounterChannel.cpp +++ b/CounterRealTime/CounterChannel.cpp @@ -30,6 +30,14 @@ { return delays; } +QString CounterChannel::getIsUse() +{ + return isUse; +} +QString CounterChannel::getName() +{ + return name; +} void CounterChannel::setActive(bool active) { this->active = active; @@ -54,14 +62,13 @@ { this->delays = delays; } - -int CounterChannel::getFormRowIndex() +void CounterChannel::setIsUse(QString isUse) { - return formRowIndex; + this->isUse = isUse; } -void CounterChannel::setFormRowIndex(int rowIndex) +void CounterChannel::setName(QString name) { - this->formRowIndex = rowIndex; + this->name = name; } QList CounterChannel::getHisData() diff --git a/CounterRealTime/CounterChannel.h b/CounterRealTime/CounterChannel.h index afc5dc2..8f2c35e 100644 --- a/CounterRealTime/CounterChannel.h +++ b/CounterRealTime/CounterChannel.h @@ -15,15 +15,16 @@ QString getChannelCode(); QString getChannelNo(); QString getDelays(); + QString getIsUse(); + QString getName(); void setActive(bool active); void setDeviceId(QString deviceId); void setChannelId(QString channelId); void setChannelCode(QString channelCode); void setChannelNo(QString channelNo); void setDelays(QString delays); - - int getFormRowIndex(); - void setFormRowIndex(int rowIndex); + void setIsUse(QString isUse); + void setName(QString name); QList getHisData(); void appendHisData(QString timestampStr, QString valueStr); @@ -35,13 +36,12 @@ QString channelCode; QString channelNo; QString delays; + QString isUse; + QString name; int maxHisDataCount = 3600; QList hisDataList; - // 通道位于界面上的哪一行 列的位置通过计数器来确定 - int formRowIndex; - signals: }; diff --git a/CounterRealTime/CounterDevice.cpp b/CounterRealTime/CounterDevice.cpp index 63200bb..1528373 100644 --- a/CounterRealTime/CounterDevice.cpp +++ b/CounterRealTime/CounterDevice.cpp @@ -1,6 +1,5 @@ #include "CounterDevice.h" -#include #include #include @@ -95,7 +94,7 @@ void CounterDevice::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); -// std::cout << this->dataBuff.toStdString(); +// qDebug() << this->dataBuff; CounterDataDto * counterData = new CounterDataDto(this); if (CounterProtocolXH::checkFrame(this->dataBuff) == true) @@ -117,7 +116,7 @@ } } else if (this->dataBuff.size() > COUNTER_FRAME_MIN_LENGTH) { - std::cout << QString("%1").arg(this->dataBuff.size()).toStdString() << std::endl; + qDebug() << QString("%1").arg(this->dataBuff.size()); this->dataBuff.clear(); } @@ -193,7 +192,7 @@ } // qDebug() << counterData->rawFrame; - qDebug() << messageArray; +// qDebug() << messageArray; // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(messageArray); diff --git a/CounterRealTime/CounterSettingForm.cpp b/CounterRealTime/CounterSettingForm.cpp index c095523..225ec88 100644 --- a/CounterRealTime/CounterSettingForm.cpp +++ b/CounterRealTime/CounterSettingForm.cpp @@ -283,43 +283,68 @@ { QString deviceId = ui->selectCounter->currentData().toString(); - for (int i = 0; i < 8; i++) + QJsonArray channelSettingUpdated; + + for (int i = 0; i < 16; i++) { + QString channelCode; + double delay; + bool active; + if (i < 8) { + channelCode = clockListLeft.at(i)->text(); + delay = delayListLeft.at(i)->value(); + active = activeListLeft.at(i)->isChecked(); + } else if (i < 16) { + channelCode = clockListRight.at(i - 8)->text(); + delay = delayListRight.at(i - 8)->value(); + active = activeListRight.at(i - 8)->isChecked(); + } + CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); if (channelPrt != nullptr) { - QString channelCode = clockListLeft.at(i)->text(); // 通道钟号 - double delay = delayListLeft.at(i)->value(); // 通道时延值 + bool isUpdated = isChannelSettingUpdated(i, channelPrt); + if (isUpdated == true) { + channelPrt->setChannelCode(channelCode); + channelPrt->setDelays(QString::number(delay)); + channelPrt->setActive(active); - channelPrt->setChannelCode(channelCode); - channelPrt->setDelays(QString::number(delay)); - channelPrt->setActive(activeListLeft.at(i)->isChecked()); - } else if (activeListLeft.at(i)->isChecked() == true) { + QJsonObject channelObj; + channelObj.insert("id", channelPrt->getChannelId()); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", channelPrt->getChannelNo()); + channelObj.insert("isUse", channelPrt->getIsUse()); + channelObj.insert("name", channelPrt->getName()); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); + } + } else if (active == true) { CounterChannel * newChannel = new CounterChannel(); newChannel->setActive(true); - newChannel->setChannelCode(clockListLeft.at(i)->text()); - newChannel->setDelays(QString::number(delayListLeft.at(i)->value())); + newChannel->setChannelCode(channelCode); + newChannel->setDelays(QString::number(delay)); ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1), newChannel); + + QJsonObject channelObj; + channelObj.insert("id", ""); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", QString::number(i + 1)); + channelObj.insert("isUse", SettingConfig::getInstance().MASTER); + channelObj.insert("name", QString("钟%1-1PPS").arg(channelCode)); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); } + } - CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr) - { - QString channelCode = clockListRight.at(i)->text(); // 通道钟号 - double delay = delayListRight.at(i)->value(); // 通道时延值 - - channelRightPrt->setChannelCode(channelCode); - channelRightPrt->setDelays(QString::number(delay)); - channelRightPrt->setActive(activeListRight.at(i)->isChecked()); - } else if (activeListRight.at(i)->isChecked() == true) { - CounterChannel * newChannel = new CounterChannel(); - newChannel->setActive(true); - newChannel->setChannelCode(clockListRight.at(i)->text()); - newChannel->setDelays(QString::number(delayListRight.at(i)->value())); - - ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1+8), newChannel); - } + if (channelSettingUpdated.size() > 0) { + emit this->channelSettingSubmit(channelSettingUpdated); } this->close(); @@ -341,10 +366,10 @@ void CounterSettingForm::on_selectCounter_currentIndexChanged(int index) { - if (index == 0) + clearChannelSettingValue(); + + if (index > 0) { - clearChannelSettingValue(); - } else { QString deviceId = ui->selectCounter->currentData().toString(); for (int i = 0; i < 8; i++) @@ -353,45 +378,37 @@ activeListRight.at(i)->setCheckable(true); CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); - if (channelPrt != nullptr && channelPrt->isActive() == true) + if (channelPrt != nullptr) { QString channelCode = channelPrt->getChannelCode(); // 通道钟号 QString delays = channelPrt->getDelays(); // 通道时延值 - activeListLeft.at(i)->setChecked(true); - activeListLeft.at(i)->setText("启用"); clockListLeft.at(i)->setText(channelCode); - clockListLeft.at(i)->setReadOnly(false); delayListLeft.at(i)->setValue(delays.toDouble()); - delayListLeft.at(i)->setReadOnly(false); - } else { - activeListLeft.at(i)->setChecked(false); - activeListLeft.at(i)->setText("未启用"); - clockListLeft.at(i)->setText("-"); - clockListLeft.at(i)->setReadOnly(true); - delayListLeft.at(i)->setValue(0.000); - delayListLeft.at(i)->setReadOnly(true); + + if (channelPrt->isActive() == true) { + activeListLeft.at(i)->setChecked(true); + activeListLeft.at(i)->setText("启用"); + clockListLeft.at(i)->setReadOnly(false); + delayListLeft.at(i)->setReadOnly(false); + } } CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr && channelRightPrt->isActive() == true) + if (channelRightPrt != nullptr) { QString channelCode = channelRightPrt->getChannelCode(); // 通道钟号 QString delays = channelRightPrt->getDelays(); // 通道时延值 - activeListRight.at(i)->setChecked(true); - activeListRight.at(i)->setText("启用"); clockListRight.at(i)->setText(channelCode); - clockListRight.at(i)->setReadOnly(false); delayListRight.at(i)->setValue(delays.toDouble()); - delayListRight.at(i)->setReadOnly(false); - } else { - activeListRight.at(i)->setChecked(false); - activeListRight.at(i)->setText("未启用"); - clockListRight.at(i)->setText("-"); - clockListRight.at(i)->setReadOnly(true); - delayListRight.at(i)->setValue(0.000); - delayListRight.at(i)->setReadOnly(true); + + if (channelRightPrt->isActive() == true) { + activeListRight.at(i)->setChecked(true); + activeListRight.at(i)->setText("启用"); + clockListRight.at(i)->setReadOnly(false); + delayListRight.at(i)->setReadOnly(false); + } } } } @@ -423,3 +440,35 @@ delayListRight.at(index - 8)->setReadOnly(!enable); } } + +bool CounterSettingForm::isChannelSettingUpdated(int index, CounterChannel *channelPtr) +{ + QString channelCode = ""; // 通道钟号 + double delay = 0.0; // 通道时延值 + bool active = false; + + if (index < 8) { + channelCode = clockListLeft.at(index)->text(); // 通道钟号 + delay = delayListLeft.at(index)->value(); // 通道时延值 + active = activeListLeft.at(index)->isChecked(); + } else if (index < 16) { + index = index - 8; + channelCode = clockListRight.at(index)->text(); // 通道钟号 + delay = delayListRight.at(index)->value(); // 通道时延值 + active = activeListRight.at(index)->isChecked(); + } + + if (channelCode != channelPtr->getChannelCode()) { + return true; + } + + if (active != channelPtr->isActive()) { + return true; + } + + if (delay - channelPtr->getDelays().toDouble() != 0) { + return true; + } + + return false; +} diff --git a/CounterRealTime/CounterSettingForm.h b/CounterRealTime/CounterSettingForm.h index b4949fb..9c41362 100644 --- a/CounterRealTime/CounterSettingForm.h +++ b/CounterRealTime/CounterSettingForm.h @@ -55,10 +55,12 @@ void clearChannelSettingValue(); void enableChannel(int index, bool enable); + bool isChannelSettingUpdated(int index, CounterChannel * channelPtr); signals: void swiftDisplayLoop(); void swiftDisplayCount(); + void channelSettingSubmit(QJsonArray channelSettings); }; #endif // COUNTERSETTINGFORM_H diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index 55db2ed..86a7776 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -12,6 +12,7 @@ resize(SettingConfig::getInstance().WINDOW_WIDTH, SettingConfig::getInstance().WINDOW_HEIGHT); settingForm = new CounterSettingForm(); + connect(settingForm, &CounterSettingForm::channelSettingSubmit, this, &CounterWindowRT::onChannelSettingSubmit); QFile file(":/qss/mainClock.css"); if (file.open(QFile::ReadOnly)) { @@ -22,6 +23,14 @@ file.close(); } + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_MSG_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &CounterWindowRT::onKafkaMessageReceived); + // 绘制界面上的表格 initChannelForm(); @@ -31,7 +40,6 @@ // 重新选择显示多少个计数器 connect(settingForm, &CounterSettingForm::swiftDisplayLoop, this, &CounterWindowRT::updateDeviceWidget); - connect(settingForm, &CounterSettingForm::swiftDisplayCount, this, &CounterWindowRT::initChannelForm); httpReq = new HttpRequestController(this); @@ -47,7 +55,7 @@ // 初始化设备和通道 getDeviceList(); -// QThread::msleep(1000 * 3); + QThread::msleep(1000 * 3); for (int i = 0; i < ConstCache::getInstance().deviceIdLoopA.size(); i++) { CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(ConstCache::getInstance().deviceIdLoopA.at(i)); @@ -58,7 +66,6 @@ CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(ConstCache::getInstance().deviceIdLoopB.at(i)); devicePtr->initSerialPort(); } - } CounterWindowRT::~CounterWindowRT() @@ -77,85 +84,94 @@ } } -/** - * @brief CounterWindowRT::drawCounterDataOnPage - * @param messageArray - * 根据发送到Kafka中的数据串来绘制界面 - * - */ -void CounterWindowRT::drawCounterDataOnPage(QJsonArray messageArray) +void CounterWindowRT::onChannelSettingSubmit(QJsonArray channelSettings) { - if (messageArray.isEmpty() == false) - { - // 获取设备id属性 - QString deviceId = messageArray.at(0).toObject().find("deviceId")->toString(); - - // 如果不是当前显示的计数器 则直接返回 - CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); - if (SettingConfig::getInstance().MASTER != devicePtr->getIsUse()) - { - return; - } - - // 根据设备id 从map中取出列索引的值 - int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); - if (devIdx > SettingConfig::getInstance().DISPLAY_COUNT) - { - return; - } - - // 参考通道 - int refChNo = messageArray.at(0).toObject().find("data")->toObject().find("channelRefNo")->toInt(); - CounterChannel * refChann = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(refChNo)); - if (refChann != nullptr) - { - if (devIdx < devRefList.size()) { - devRefList.at(devIdx)->setText(QString("REF:%1").arg(refChann->getChannelCode())); - } - } - - updateChannelDataForm(messageArray); - } + httpReq->updateChannelInfo(channelSettings); } -void CounterWindowRT::updateDeviceWidget() +int CounterWindowRT::initHttpToken() { - QList devIds = SettingConfig::getInstance().MASTER == 1 ? ConstCache::getInstance().deviceIdLoopA : ConstCache::getInstance().deviceIdLoopB; - - for (int i = 0; i < SettingConfig::getInstance().DISPLAY_COUNT; i++) + QJsonObject response = httpReq->getTokenByClientId(SettingConfig::getInstance().CLIENT_ID, + SettingConfig::getInstance().APP_KEY); + return response.value("code").toInt(); +} +void CounterWindowRT::getDeviceList() +{ + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.value("code").toInt() == 200) { - if (i < devIds.size()) + // 将获取到的设备绘制到表格中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) { - CounterDevice * dev = ConstCache::getInstance().deviceMap.value(devIds.at(i)); + // 接口返回的device对象 + QJsonObject devItem = devArray.at(i).toObject(); + QString devName = devItem.value("simpleName").toString(); // 设备名称 + QString deviceId = devItem.value("deviceId").toString(); // 设备ID + QString isUse = devItem.value("isUse").toString(); // 标识A路/B路 - QString devName = dev->getDeviceName(); // 设备名称 - devTitleList.at(i)->setText(devName.right(devName.length() - 8)); - dev->setFormColIndex(i); - } else { - devTitleList.at(i)->setText(""); - devRefList.at(i)->setText(""); + // 构建Device对象 并赋值id、串口、波特率、设备编号等属性 + CounterDevice * device = new CounterDevice(this); + device->setDeviceId(deviceId); + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceName(devName); + device->setIsUse(isUse.toInt()); - for (int j = 0; j < 16; j++) + ConstCache::getInstance().deviceMap.insert(deviceId, device); + if (isUse == "1") { - channelNameList.at(i * 16 + j)->setText(""); - channelValueList.at(i * 16 + j)->setText(""); + ConstCache::getInstance().deviceIdLoopA.append(deviceId); + } else if (isUse == "0") { + ConstCache::getInstance().deviceIdLoopB.append(deviceId); } + + // 绑定显示函数 + connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); + + // 查询计数器的通道 并初始化串口 + this->getChannelList(deviceId); } + + updateDeviceWidget(); } } - -void CounterWindowRT::initChannelForm() +void CounterWindowRT::getChannelList(QString deviceId) { - // 1. 首先清除界面上的所有内容 - qDeleteAll(ui->widgetContent->children()); + // 查询计数器的通道 + QJsonObject response = httpReq->getChannelList(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 + QString channelCode = channelItem.value("channelCode").toString(); // 通道钟号 + QString channelNo = QString::number(channelItem.value("channelNo").toInt()); // 通道序号 + QString delays = channelItem.value("delays").toString(); // 通道时延值 + QString isUse = channelItem.value("isUse").toString(); + QString name = channelItem.value("name").toString(); + bool active = channelItem.value("isActive").toString().toInt(); // 通道是否启用 - // 将lab列表的数量置0 - devTitleList.resize(0); - devRefList.resize(0); - channelNameList.resize(0); - channelValueList.resize(0); + // 构建通道对象 并设置属性 + CounterChannel * channel = new CounterChannel(this); + channel->setDeviceId(deviceId); + channel->setChannelId(channelId); + channel->setChannelCode(channelCode); + channel->setChannelNo(channelNo); + channel->setDelays(delays); + channel->setActive(active); + channel->setIsUse(isUse); + channel->setName(name); - initDevAndChannelForm(SettingConfig::getInstance().DISPLAY_COUNT); + // 添加到通道的集合中 key=deviceId-channelNo + ConstCache::getInstance().channelMap.insert(deviceId + "-" + channelNo, channel); + } + } } void CounterWindowRT::initDevAndChannelForm(int count) @@ -221,7 +237,7 @@ for ( int i = 0; i < 16; i++ ) { QLabelDblClick * labChannelName = new QLabelDblClick(widgetDev); // 通道名 - QLabel * labChannelValue = new QLabel(widgetDev); // 通道测量值 + QLabelDblClick * labChannelValue = new QLabelDblClick(widgetDev); // 通道测量值 // 默认显示内容 labChannelName->setText(QString("CH%1").arg(i+1, 2, 10, QLatin1Char('0'))); @@ -247,6 +263,7 @@ // 添加通道名称的双击事件,显示数据和残差图表 connect(labChannelName, &QLabelDblClick::doubleClicked, this, &CounterWindowRT::onChannelNameLabelDblClicked); + connect(labChannelValue, &QLabelDblClick::doubleClicked, this, &CounterWindowRT::onChannelNameLabelDblClicked); channelNameList.append(labChannelName); channelValueList.append(labChannelValue); @@ -263,14 +280,13 @@ widgetDev->setLayout(layoutChannelPage); } } - void CounterWindowRT::updateChannelDataForm(QJsonArray messageArray) { // 获取设备id属性 - QString deviceId = messageArray.at(0).toObject().find("deviceId")->toString(); + QString deviceId = messageArray.at(0).toObject().value("deviceId").toString(); // 参考通道号 - int refChNo = messageArray.at(0).toObject().find("data")->toObject().find("channelRefNo")->toInt(); + int refChNo = messageArray.at(0).toObject().value("data").toObject().value("channelRefNo").toInt(); // 根据设备id 从map中取出列索引的值 int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); @@ -285,7 +301,7 @@ for (int i = 0; i < messageArray.size(); i++) { QJsonObject channelDataItem = messageArray.at(i).toObject(); - QString channelNo = QString::number(channelDataItem.find("channelNo")->toInt()); // 通道序号 + QString channelNo = QString::number(channelDataItem.value("channelNo").toInt()); // 通道序号 channelDataJsonMap.insert(channelNo, channelDataItem); } @@ -306,13 +322,13 @@ CounterChannel * channel = ConstCache::getInstance().channelMap.value(deviceId + "-" + channelNo); // 从map中找到对应的通道 // 通道Label的序号 - int chanelIdx = devIdx * 16 + channelDataItem.find("channelNo")->toInt() - 1; + int chanelIdx = devIdx * 16 + channelDataItem.value("channelNo").toInt() - 1; if (channel != nullptr && channel->isActive() == true) { // 通道时延值 并计算 减去时延值的测量值 double delay = channel->getDelays().isEmpty() == false ? channel->getDelays().toDouble() : 0.0; - double valueMinusDelay = channelDataItem.find("data")->toObject().find("dataValue")->toString().toDouble() - delay * 1E-9; + double valueMinusDelay = channelDataItem.value("data").toObject().value("dataValue").toString().toDouble() - delay * 1E-9; // 设置通道名和测量值的QLabel channelNameList.at(chanelIdx)->setText(QString("CH%1:%2").arg(channelNoStr).arg(channel->getChannelCode())); @@ -321,6 +337,10 @@ channelNameList.at(chanelIdx)->setProperty("channelId", channel->getChannelId()); channelNameList.at(chanelIdx)->setProperty("deviceId", deviceId); channelNameList.at(chanelIdx)->setProperty("channelNo", channelNo); + + channelValueList.at(chanelIdx)->setProperty("channelId", channel->getChannelId()); + channelValueList.at(chanelIdx)->setProperty("deviceId", deviceId); + channelValueList.at(chanelIdx)->setProperty("channelNo", channelNo); } else { channelNameList.at(chanelIdx)->setText(QString("CH%1:%2").arg(channelNoStr).arg("-")); channelValueList.at(chanelIdx)->setText(""); @@ -333,84 +353,11 @@ } } -int CounterWindowRT::initHttpToken() +void CounterWindowRT::updateDateAndTime() { - QJsonObject response = httpReq->getTokenByClientId(SettingConfig::getInstance().CLIENT_ID, - SettingConfig::getInstance().APP_KEY); - return response.find("code")->toInt(); -} -void CounterWindowRT::getDeviceList() -{ - QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - if (devListRes.find("code")->toInt() == 200) - { - // 将获取到的设备绘制到表格中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - // 接口返回的device对象 - QJsonObject devItem = devArray.at(i).toObject(); - QString devName = devItem.find("deviceName")->toString(); // 设备名称 - QString deviceId = devItem.find("deviceId")->toString(); // 设备ID - QString isUse = devItem.find("isUse")->toString(); // 标识A路/B路 - - // 构建Device对象 并赋值id、串口、波特率、设备编号等属性 - CounterDevice * device = new CounterDevice(this); - device->setDeviceId(deviceId); - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceName(devName); - device->setIsUse(isUse.toInt()); - - ConstCache::getInstance().deviceMap.insert(deviceId, device); - if (isUse == "1") - { - ConstCache::getInstance().deviceIdLoopA.append(deviceId); - } else if (isUse == "0") { - ConstCache::getInstance().deviceIdLoopB.append(deviceId); - } - - // 绑定显示函数 - connect(device, &CounterDevice::sendDataToDraw, - this, &CounterWindowRT::drawCounterDataOnPage); - - // 查询计数器的通道 并初始化串口 - this->getChannelList(deviceId); - } - - updateDeviceWidget(); - } -} -void CounterWindowRT::getChannelList(QString deviceId) -{ - // 查询计数器的通道 - QJsonObject response = httpReq->getChannelList(deviceId); - if (response.find("code")->toInt() == 200) - { - // 将通道绘制在界面上 - QJsonArray channelArray = response.find("data")->toArray(); - for (int i = 0; i < channelArray.size(); i++) - { - // 接口返回的channel对象 - QJsonObject channelItem = channelArray.at(i).toObject(); - QString channelId = channelItem.find("id")->toString(); // 通道ID - QString channelCode = channelItem.find("channelCode")->toString(); // 通道钟号 - QString channelNo = QString::number(channelItem.find("channelNo")->toInt()); // 通道序号 - QString delays = channelItem.find("delays")->toString(); // 通道时延值 - - // 构建通道对象 并设置属性 - CounterChannel * channel = new CounterChannel(this); - channel->setDeviceId(deviceId); - channel->setChannelId(channelId); - channel->setChannelCode(channelCode); - channel->setChannelNo(channelNo); - channel->setDelays(delays); - - // 添加到通道的集合中 key=deviceId-channelNo - ConstCache::getInstance().channelMap.insert(deviceId + "-" + channelNo, channel); - } - } + QString date = QDate::currentDate().toString("yyyy-MM-dd"); + QString time = QTime::currentTime().toString("HH:mm:ss"); + ui->labTime->setText(date + " " + time); } void CounterWindowRT::on_btnMenuSetting_clicked() @@ -425,6 +372,47 @@ setWindowState(Qt::WindowMinimized | windowState()); } +void CounterWindowRT::initChannelForm() +{ + // 1. 首先清除界面上的所有内容 + qDeleteAll(ui->widgetContent->children()); + + // 将lab列表的数量置0 + devTitleList.resize(0); + devRefList.resize(0); + channelNameList.resize(0); + channelValueList.resize(0); + + initDevAndChannelForm(SettingConfig::getInstance().DISPLAY_COUNT); +} + +void CounterWindowRT::updateDeviceWidget() +{ + QList devIds = SettingConfig::getInstance().MASTER == 1 ? ConstCache::getInstance().deviceIdLoopA : ConstCache::getInstance().deviceIdLoopB; + + for (int i = 0; i < SettingConfig::getInstance().DISPLAY_COUNT; i++) + { + if (i < devIds.size()) + { + CounterDevice * dev = ConstCache::getInstance().deviceMap.value(devIds.at(i)); + + if (dev != nullptr) { + devTitleList.at(i)->setText(dev->getDeviceName()); // 设备名称 + dev->setFormColIndex(i); + } + } else { + devTitleList.at(i)->setText(""); + devRefList.at(i)->setText(""); + + for (int j = 0; j < 16; j++) + { + channelNameList.at(i * 16 + j)->setText(""); + channelValueList.at(i * 16 + j)->setText(""); + } + } + } +} + void CounterWindowRT::onChannelNameLabelDblClicked() { QLabelDblClick * channelLab = (QLabelDblClick *) sender(); @@ -445,10 +433,50 @@ } } -void CounterWindowRT::updateDateAndTime() + +/** + * @brief CounterWindowRT::drawCounterDataOnPage + * @param messageArray + * 根据发送到Kafka中的数据串来绘制界面 + * + */ +void CounterWindowRT::drawCounterDataOnPage(QJsonArray messageArray) { - QString date = QDate::currentDate().toString("yyyy-MM-dd"); - QString time = QTime::currentTime().toString("HH:mm:ss"); - ui->labTime->setText(date + " " + time); + if (messageArray.isEmpty() == false) + { + // 获取设备id属性 + QString deviceId = messageArray.at(0).toObject().value("deviceId").toString(); + + // 如果不是当前显示的计数器 则直接返回 + CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (SettingConfig::getInstance().MASTER != devicePtr->getIsUse()) + { + return; + } + + // 根据设备id 从map中取出列索引的值 + int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); + if (devIdx > SettingConfig::getInstance().DISPLAY_COUNT) + { + return; + } + + // 参考通道 + int refChNo = messageArray.at(0).toObject().value("data").toObject().value("channelRefNo").toInt(); + CounterChannel * refChann = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(refChNo)); + if (refChann != nullptr) + { + if (devIdx < devRefList.size()) { + devRefList.at(devIdx)->setText(QString("REF:%1").arg(refChann->getChannelCode())); + } + } + + updateChannelDataForm(messageArray); + } } + +void CounterWindowRT::onKafkaMessageReceived(QJsonObject message) +{ + qDebug() << message; +} diff --git a/CounterRealTime/CounterWindowRT.h b/CounterRealTime/CounterWindowRT.h index 3f21ce7..c64e07f 100644 --- a/CounterRealTime/CounterWindowRT.h +++ b/CounterRealTime/CounterWindowRT.h @@ -18,6 +18,7 @@ #include "common/utils/SettingConfig.h" #include "common/utils/TimerCounterUtil.h" #include "common/HttpRequestController.h" +#include "common/utils/QKafkaConsumer.h" QT_BEGIN_NAMESPACE namespace Ui { class CounterWindowRT; } @@ -32,10 +33,9 @@ ~CounterWindowRT(); public slots: - void drawCounterDataOnPage(QJsonArray messageArray); + void keyPressEvent(QKeyEvent *event); - void updateDeviceWidget(); - void initChannelForm(); + void onChannelSettingSubmit(QJsonArray channelSettings); private: Ui::CounterWindowRT *ui; @@ -49,24 +49,32 @@ HttpRequestController * httpReq; - void keyPressEvent(QKeyEvent *event); + QKafkaConsumer * kafkaConsumer; int initHttpToken(); void getDeviceList(); void getChannelList(QString deviceId); void initDevAndChannelForm(int count); - void updateChannelDataForm(QJsonArray messageArray); private slots: + void updateDateAndTime(); void on_btnMenuMin_clicked(); void on_btnMenuSetting_clicked(); - void onChannelNameLabelDblClicked(); + void initChannelForm(); + void updateDeviceWidget(); - void updateDateAndTime(); + void onChannelNameLabelDblClicked(); + void drawCounterDataOnPage(QJsonArray messageArray); + + void onKafkaMessageReceived(QJsonObject message); + + + + }; #endif // COUNTERWINDOWRT_H diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 20f6177..b287ed5 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -166,7 +166,6 @@ // 获取设备列表的接口地址 QUrl url = baseUrl + "/device/list"; -// QUrl url = baseUrl + "/device/deviceList.js"; QUrlQuery query; query.addQueryItem("type", devType); url.setQuery(query); @@ -201,7 +200,6 @@ // 获取设备列表的接口地址 QUrl url = baseUrl + "/channel/list"; -// QUrl url = baseUrl + "/device/channel-" + deviceId + ".js"; QUrlQuery query; query.addQueryItem("deviceId", deviceId); url.setQuery(query); @@ -224,7 +222,41 @@ resultObj.insert("code", -1); } -// qDebug() << resultObj; + qDebug() << resultObj; + + return resultObj; +} + +QJsonObject HttpRequestController::updateChannelInfo(QJsonArray channelInfos) +{ + QJsonObject resultObj; + + // 获取设备列表的接口地址 + QUrl url = baseUrl + "/channel/qtBatchSave"; + + QNetworkRequest request; + request.setUrl(url); + request.setRawHeader("Content-type", "application/json"); + request.setRawHeader("token", token.toLocal8Bit()); + + QJsonDocument jsonDoc = QJsonDocument(channelInfos); + QByteArray postData = jsonDoc.toJson(QJsonDocument::Compact); + + qDebug() << url; + qDebug() << channelInfos; + + QNetworkReply * reply = httpUtil->sendPostRequest(request, postData); + 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/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index 5b4e4ef..860aafa 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -2,6 +2,8 @@ #define HTTPREQUESTCONTROLLER_H #include +#include +#include #include #include "utils/HttpRequestUtil.h" @@ -22,6 +24,8 @@ QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); + QJsonObject updateChannelInfo(QJsonArray channelInfos); + private: HttpRequestUtil * httpUtil; diff --git a/CounterRealTime/CounterChannel.cpp b/CounterRealTime/CounterChannel.cpp index 978b969..0a4b534 100644 --- a/CounterRealTime/CounterChannel.cpp +++ b/CounterRealTime/CounterChannel.cpp @@ -30,6 +30,14 @@ { return delays; } +QString CounterChannel::getIsUse() +{ + return isUse; +} +QString CounterChannel::getName() +{ + return name; +} void CounterChannel::setActive(bool active) { this->active = active; @@ -54,14 +62,13 @@ { this->delays = delays; } - -int CounterChannel::getFormRowIndex() +void CounterChannel::setIsUse(QString isUse) { - return formRowIndex; + this->isUse = isUse; } -void CounterChannel::setFormRowIndex(int rowIndex) +void CounterChannel::setName(QString name) { - this->formRowIndex = rowIndex; + this->name = name; } QList CounterChannel::getHisData() diff --git a/CounterRealTime/CounterChannel.h b/CounterRealTime/CounterChannel.h index afc5dc2..8f2c35e 100644 --- a/CounterRealTime/CounterChannel.h +++ b/CounterRealTime/CounterChannel.h @@ -15,15 +15,16 @@ QString getChannelCode(); QString getChannelNo(); QString getDelays(); + QString getIsUse(); + QString getName(); void setActive(bool active); void setDeviceId(QString deviceId); void setChannelId(QString channelId); void setChannelCode(QString channelCode); void setChannelNo(QString channelNo); void setDelays(QString delays); - - int getFormRowIndex(); - void setFormRowIndex(int rowIndex); + void setIsUse(QString isUse); + void setName(QString name); QList getHisData(); void appendHisData(QString timestampStr, QString valueStr); @@ -35,13 +36,12 @@ QString channelCode; QString channelNo; QString delays; + QString isUse; + QString name; int maxHisDataCount = 3600; QList hisDataList; - // 通道位于界面上的哪一行 列的位置通过计数器来确定 - int formRowIndex; - signals: }; diff --git a/CounterRealTime/CounterDevice.cpp b/CounterRealTime/CounterDevice.cpp index 63200bb..1528373 100644 --- a/CounterRealTime/CounterDevice.cpp +++ b/CounterRealTime/CounterDevice.cpp @@ -1,6 +1,5 @@ #include "CounterDevice.h" -#include #include #include @@ -95,7 +94,7 @@ void CounterDevice::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); -// std::cout << this->dataBuff.toStdString(); +// qDebug() << this->dataBuff; CounterDataDto * counterData = new CounterDataDto(this); if (CounterProtocolXH::checkFrame(this->dataBuff) == true) @@ -117,7 +116,7 @@ } } else if (this->dataBuff.size() > COUNTER_FRAME_MIN_LENGTH) { - std::cout << QString("%1").arg(this->dataBuff.size()).toStdString() << std::endl; + qDebug() << QString("%1").arg(this->dataBuff.size()); this->dataBuff.clear(); } @@ -193,7 +192,7 @@ } // qDebug() << counterData->rawFrame; - qDebug() << messageArray; +// qDebug() << messageArray; // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(messageArray); diff --git a/CounterRealTime/CounterSettingForm.cpp b/CounterRealTime/CounterSettingForm.cpp index c095523..225ec88 100644 --- a/CounterRealTime/CounterSettingForm.cpp +++ b/CounterRealTime/CounterSettingForm.cpp @@ -283,43 +283,68 @@ { QString deviceId = ui->selectCounter->currentData().toString(); - for (int i = 0; i < 8; i++) + QJsonArray channelSettingUpdated; + + for (int i = 0; i < 16; i++) { + QString channelCode; + double delay; + bool active; + if (i < 8) { + channelCode = clockListLeft.at(i)->text(); + delay = delayListLeft.at(i)->value(); + active = activeListLeft.at(i)->isChecked(); + } else if (i < 16) { + channelCode = clockListRight.at(i - 8)->text(); + delay = delayListRight.at(i - 8)->value(); + active = activeListRight.at(i - 8)->isChecked(); + } + CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); if (channelPrt != nullptr) { - QString channelCode = clockListLeft.at(i)->text(); // 通道钟号 - double delay = delayListLeft.at(i)->value(); // 通道时延值 + bool isUpdated = isChannelSettingUpdated(i, channelPrt); + if (isUpdated == true) { + channelPrt->setChannelCode(channelCode); + channelPrt->setDelays(QString::number(delay)); + channelPrt->setActive(active); - channelPrt->setChannelCode(channelCode); - channelPrt->setDelays(QString::number(delay)); - channelPrt->setActive(activeListLeft.at(i)->isChecked()); - } else if (activeListLeft.at(i)->isChecked() == true) { + QJsonObject channelObj; + channelObj.insert("id", channelPrt->getChannelId()); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", channelPrt->getChannelNo()); + channelObj.insert("isUse", channelPrt->getIsUse()); + channelObj.insert("name", channelPrt->getName()); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); + } + } else if (active == true) { CounterChannel * newChannel = new CounterChannel(); newChannel->setActive(true); - newChannel->setChannelCode(clockListLeft.at(i)->text()); - newChannel->setDelays(QString::number(delayListLeft.at(i)->value())); + newChannel->setChannelCode(channelCode); + newChannel->setDelays(QString::number(delay)); ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1), newChannel); + + QJsonObject channelObj; + channelObj.insert("id", ""); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", QString::number(i + 1)); + channelObj.insert("isUse", SettingConfig::getInstance().MASTER); + channelObj.insert("name", QString("钟%1-1PPS").arg(channelCode)); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); } + } - CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr) - { - QString channelCode = clockListRight.at(i)->text(); // 通道钟号 - double delay = delayListRight.at(i)->value(); // 通道时延值 - - channelRightPrt->setChannelCode(channelCode); - channelRightPrt->setDelays(QString::number(delay)); - channelRightPrt->setActive(activeListRight.at(i)->isChecked()); - } else if (activeListRight.at(i)->isChecked() == true) { - CounterChannel * newChannel = new CounterChannel(); - newChannel->setActive(true); - newChannel->setChannelCode(clockListRight.at(i)->text()); - newChannel->setDelays(QString::number(delayListRight.at(i)->value())); - - ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1+8), newChannel); - } + if (channelSettingUpdated.size() > 0) { + emit this->channelSettingSubmit(channelSettingUpdated); } this->close(); @@ -341,10 +366,10 @@ void CounterSettingForm::on_selectCounter_currentIndexChanged(int index) { - if (index == 0) + clearChannelSettingValue(); + + if (index > 0) { - clearChannelSettingValue(); - } else { QString deviceId = ui->selectCounter->currentData().toString(); for (int i = 0; i < 8; i++) @@ -353,45 +378,37 @@ activeListRight.at(i)->setCheckable(true); CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); - if (channelPrt != nullptr && channelPrt->isActive() == true) + if (channelPrt != nullptr) { QString channelCode = channelPrt->getChannelCode(); // 通道钟号 QString delays = channelPrt->getDelays(); // 通道时延值 - activeListLeft.at(i)->setChecked(true); - activeListLeft.at(i)->setText("启用"); clockListLeft.at(i)->setText(channelCode); - clockListLeft.at(i)->setReadOnly(false); delayListLeft.at(i)->setValue(delays.toDouble()); - delayListLeft.at(i)->setReadOnly(false); - } else { - activeListLeft.at(i)->setChecked(false); - activeListLeft.at(i)->setText("未启用"); - clockListLeft.at(i)->setText("-"); - clockListLeft.at(i)->setReadOnly(true); - delayListLeft.at(i)->setValue(0.000); - delayListLeft.at(i)->setReadOnly(true); + + if (channelPrt->isActive() == true) { + activeListLeft.at(i)->setChecked(true); + activeListLeft.at(i)->setText("启用"); + clockListLeft.at(i)->setReadOnly(false); + delayListLeft.at(i)->setReadOnly(false); + } } CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr && channelRightPrt->isActive() == true) + if (channelRightPrt != nullptr) { QString channelCode = channelRightPrt->getChannelCode(); // 通道钟号 QString delays = channelRightPrt->getDelays(); // 通道时延值 - activeListRight.at(i)->setChecked(true); - activeListRight.at(i)->setText("启用"); clockListRight.at(i)->setText(channelCode); - clockListRight.at(i)->setReadOnly(false); delayListRight.at(i)->setValue(delays.toDouble()); - delayListRight.at(i)->setReadOnly(false); - } else { - activeListRight.at(i)->setChecked(false); - activeListRight.at(i)->setText("未启用"); - clockListRight.at(i)->setText("-"); - clockListRight.at(i)->setReadOnly(true); - delayListRight.at(i)->setValue(0.000); - delayListRight.at(i)->setReadOnly(true); + + if (channelRightPrt->isActive() == true) { + activeListRight.at(i)->setChecked(true); + activeListRight.at(i)->setText("启用"); + clockListRight.at(i)->setReadOnly(false); + delayListRight.at(i)->setReadOnly(false); + } } } } @@ -423,3 +440,35 @@ delayListRight.at(index - 8)->setReadOnly(!enable); } } + +bool CounterSettingForm::isChannelSettingUpdated(int index, CounterChannel *channelPtr) +{ + QString channelCode = ""; // 通道钟号 + double delay = 0.0; // 通道时延值 + bool active = false; + + if (index < 8) { + channelCode = clockListLeft.at(index)->text(); // 通道钟号 + delay = delayListLeft.at(index)->value(); // 通道时延值 + active = activeListLeft.at(index)->isChecked(); + } else if (index < 16) { + index = index - 8; + channelCode = clockListRight.at(index)->text(); // 通道钟号 + delay = delayListRight.at(index)->value(); // 通道时延值 + active = activeListRight.at(index)->isChecked(); + } + + if (channelCode != channelPtr->getChannelCode()) { + return true; + } + + if (active != channelPtr->isActive()) { + return true; + } + + if (delay - channelPtr->getDelays().toDouble() != 0) { + return true; + } + + return false; +} diff --git a/CounterRealTime/CounterSettingForm.h b/CounterRealTime/CounterSettingForm.h index b4949fb..9c41362 100644 --- a/CounterRealTime/CounterSettingForm.h +++ b/CounterRealTime/CounterSettingForm.h @@ -55,10 +55,12 @@ void clearChannelSettingValue(); void enableChannel(int index, bool enable); + bool isChannelSettingUpdated(int index, CounterChannel * channelPtr); signals: void swiftDisplayLoop(); void swiftDisplayCount(); + void channelSettingSubmit(QJsonArray channelSettings); }; #endif // COUNTERSETTINGFORM_H diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index 55db2ed..86a7776 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -12,6 +12,7 @@ resize(SettingConfig::getInstance().WINDOW_WIDTH, SettingConfig::getInstance().WINDOW_HEIGHT); settingForm = new CounterSettingForm(); + connect(settingForm, &CounterSettingForm::channelSettingSubmit, this, &CounterWindowRT::onChannelSettingSubmit); QFile file(":/qss/mainClock.css"); if (file.open(QFile::ReadOnly)) { @@ -22,6 +23,14 @@ file.close(); } + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_MSG_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &CounterWindowRT::onKafkaMessageReceived); + // 绘制界面上的表格 initChannelForm(); @@ -31,7 +40,6 @@ // 重新选择显示多少个计数器 connect(settingForm, &CounterSettingForm::swiftDisplayLoop, this, &CounterWindowRT::updateDeviceWidget); - connect(settingForm, &CounterSettingForm::swiftDisplayCount, this, &CounterWindowRT::initChannelForm); httpReq = new HttpRequestController(this); @@ -47,7 +55,7 @@ // 初始化设备和通道 getDeviceList(); -// QThread::msleep(1000 * 3); + QThread::msleep(1000 * 3); for (int i = 0; i < ConstCache::getInstance().deviceIdLoopA.size(); i++) { CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(ConstCache::getInstance().deviceIdLoopA.at(i)); @@ -58,7 +66,6 @@ CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(ConstCache::getInstance().deviceIdLoopB.at(i)); devicePtr->initSerialPort(); } - } CounterWindowRT::~CounterWindowRT() @@ -77,85 +84,94 @@ } } -/** - * @brief CounterWindowRT::drawCounterDataOnPage - * @param messageArray - * 根据发送到Kafka中的数据串来绘制界面 - * - */ -void CounterWindowRT::drawCounterDataOnPage(QJsonArray messageArray) +void CounterWindowRT::onChannelSettingSubmit(QJsonArray channelSettings) { - if (messageArray.isEmpty() == false) - { - // 获取设备id属性 - QString deviceId = messageArray.at(0).toObject().find("deviceId")->toString(); - - // 如果不是当前显示的计数器 则直接返回 - CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); - if (SettingConfig::getInstance().MASTER != devicePtr->getIsUse()) - { - return; - } - - // 根据设备id 从map中取出列索引的值 - int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); - if (devIdx > SettingConfig::getInstance().DISPLAY_COUNT) - { - return; - } - - // 参考通道 - int refChNo = messageArray.at(0).toObject().find("data")->toObject().find("channelRefNo")->toInt(); - CounterChannel * refChann = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(refChNo)); - if (refChann != nullptr) - { - if (devIdx < devRefList.size()) { - devRefList.at(devIdx)->setText(QString("REF:%1").arg(refChann->getChannelCode())); - } - } - - updateChannelDataForm(messageArray); - } + httpReq->updateChannelInfo(channelSettings); } -void CounterWindowRT::updateDeviceWidget() +int CounterWindowRT::initHttpToken() { - QList devIds = SettingConfig::getInstance().MASTER == 1 ? ConstCache::getInstance().deviceIdLoopA : ConstCache::getInstance().deviceIdLoopB; - - for (int i = 0; i < SettingConfig::getInstance().DISPLAY_COUNT; i++) + QJsonObject response = httpReq->getTokenByClientId(SettingConfig::getInstance().CLIENT_ID, + SettingConfig::getInstance().APP_KEY); + return response.value("code").toInt(); +} +void CounterWindowRT::getDeviceList() +{ + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.value("code").toInt() == 200) { - if (i < devIds.size()) + // 将获取到的设备绘制到表格中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) { - CounterDevice * dev = ConstCache::getInstance().deviceMap.value(devIds.at(i)); + // 接口返回的device对象 + QJsonObject devItem = devArray.at(i).toObject(); + QString devName = devItem.value("simpleName").toString(); // 设备名称 + QString deviceId = devItem.value("deviceId").toString(); // 设备ID + QString isUse = devItem.value("isUse").toString(); // 标识A路/B路 - QString devName = dev->getDeviceName(); // 设备名称 - devTitleList.at(i)->setText(devName.right(devName.length() - 8)); - dev->setFormColIndex(i); - } else { - devTitleList.at(i)->setText(""); - devRefList.at(i)->setText(""); + // 构建Device对象 并赋值id、串口、波特率、设备编号等属性 + CounterDevice * device = new CounterDevice(this); + device->setDeviceId(deviceId); + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceName(devName); + device->setIsUse(isUse.toInt()); - for (int j = 0; j < 16; j++) + ConstCache::getInstance().deviceMap.insert(deviceId, device); + if (isUse == "1") { - channelNameList.at(i * 16 + j)->setText(""); - channelValueList.at(i * 16 + j)->setText(""); + ConstCache::getInstance().deviceIdLoopA.append(deviceId); + } else if (isUse == "0") { + ConstCache::getInstance().deviceIdLoopB.append(deviceId); } + + // 绑定显示函数 + connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); + + // 查询计数器的通道 并初始化串口 + this->getChannelList(deviceId); } + + updateDeviceWidget(); } } - -void CounterWindowRT::initChannelForm() +void CounterWindowRT::getChannelList(QString deviceId) { - // 1. 首先清除界面上的所有内容 - qDeleteAll(ui->widgetContent->children()); + // 查询计数器的通道 + QJsonObject response = httpReq->getChannelList(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 + QString channelCode = channelItem.value("channelCode").toString(); // 通道钟号 + QString channelNo = QString::number(channelItem.value("channelNo").toInt()); // 通道序号 + QString delays = channelItem.value("delays").toString(); // 通道时延值 + QString isUse = channelItem.value("isUse").toString(); + QString name = channelItem.value("name").toString(); + bool active = channelItem.value("isActive").toString().toInt(); // 通道是否启用 - // 将lab列表的数量置0 - devTitleList.resize(0); - devRefList.resize(0); - channelNameList.resize(0); - channelValueList.resize(0); + // 构建通道对象 并设置属性 + CounterChannel * channel = new CounterChannel(this); + channel->setDeviceId(deviceId); + channel->setChannelId(channelId); + channel->setChannelCode(channelCode); + channel->setChannelNo(channelNo); + channel->setDelays(delays); + channel->setActive(active); + channel->setIsUse(isUse); + channel->setName(name); - initDevAndChannelForm(SettingConfig::getInstance().DISPLAY_COUNT); + // 添加到通道的集合中 key=deviceId-channelNo + ConstCache::getInstance().channelMap.insert(deviceId + "-" + channelNo, channel); + } + } } void CounterWindowRT::initDevAndChannelForm(int count) @@ -221,7 +237,7 @@ for ( int i = 0; i < 16; i++ ) { QLabelDblClick * labChannelName = new QLabelDblClick(widgetDev); // 通道名 - QLabel * labChannelValue = new QLabel(widgetDev); // 通道测量值 + QLabelDblClick * labChannelValue = new QLabelDblClick(widgetDev); // 通道测量值 // 默认显示内容 labChannelName->setText(QString("CH%1").arg(i+1, 2, 10, QLatin1Char('0'))); @@ -247,6 +263,7 @@ // 添加通道名称的双击事件,显示数据和残差图表 connect(labChannelName, &QLabelDblClick::doubleClicked, this, &CounterWindowRT::onChannelNameLabelDblClicked); + connect(labChannelValue, &QLabelDblClick::doubleClicked, this, &CounterWindowRT::onChannelNameLabelDblClicked); channelNameList.append(labChannelName); channelValueList.append(labChannelValue); @@ -263,14 +280,13 @@ widgetDev->setLayout(layoutChannelPage); } } - void CounterWindowRT::updateChannelDataForm(QJsonArray messageArray) { // 获取设备id属性 - QString deviceId = messageArray.at(0).toObject().find("deviceId")->toString(); + QString deviceId = messageArray.at(0).toObject().value("deviceId").toString(); // 参考通道号 - int refChNo = messageArray.at(0).toObject().find("data")->toObject().find("channelRefNo")->toInt(); + int refChNo = messageArray.at(0).toObject().value("data").toObject().value("channelRefNo").toInt(); // 根据设备id 从map中取出列索引的值 int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); @@ -285,7 +301,7 @@ for (int i = 0; i < messageArray.size(); i++) { QJsonObject channelDataItem = messageArray.at(i).toObject(); - QString channelNo = QString::number(channelDataItem.find("channelNo")->toInt()); // 通道序号 + QString channelNo = QString::number(channelDataItem.value("channelNo").toInt()); // 通道序号 channelDataJsonMap.insert(channelNo, channelDataItem); } @@ -306,13 +322,13 @@ CounterChannel * channel = ConstCache::getInstance().channelMap.value(deviceId + "-" + channelNo); // 从map中找到对应的通道 // 通道Label的序号 - int chanelIdx = devIdx * 16 + channelDataItem.find("channelNo")->toInt() - 1; + int chanelIdx = devIdx * 16 + channelDataItem.value("channelNo").toInt() - 1; if (channel != nullptr && channel->isActive() == true) { // 通道时延值 并计算 减去时延值的测量值 double delay = channel->getDelays().isEmpty() == false ? channel->getDelays().toDouble() : 0.0; - double valueMinusDelay = channelDataItem.find("data")->toObject().find("dataValue")->toString().toDouble() - delay * 1E-9; + double valueMinusDelay = channelDataItem.value("data").toObject().value("dataValue").toString().toDouble() - delay * 1E-9; // 设置通道名和测量值的QLabel channelNameList.at(chanelIdx)->setText(QString("CH%1:%2").arg(channelNoStr).arg(channel->getChannelCode())); @@ -321,6 +337,10 @@ channelNameList.at(chanelIdx)->setProperty("channelId", channel->getChannelId()); channelNameList.at(chanelIdx)->setProperty("deviceId", deviceId); channelNameList.at(chanelIdx)->setProperty("channelNo", channelNo); + + channelValueList.at(chanelIdx)->setProperty("channelId", channel->getChannelId()); + channelValueList.at(chanelIdx)->setProperty("deviceId", deviceId); + channelValueList.at(chanelIdx)->setProperty("channelNo", channelNo); } else { channelNameList.at(chanelIdx)->setText(QString("CH%1:%2").arg(channelNoStr).arg("-")); channelValueList.at(chanelIdx)->setText(""); @@ -333,84 +353,11 @@ } } -int CounterWindowRT::initHttpToken() +void CounterWindowRT::updateDateAndTime() { - QJsonObject response = httpReq->getTokenByClientId(SettingConfig::getInstance().CLIENT_ID, - SettingConfig::getInstance().APP_KEY); - return response.find("code")->toInt(); -} -void CounterWindowRT::getDeviceList() -{ - QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - if (devListRes.find("code")->toInt() == 200) - { - // 将获取到的设备绘制到表格中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - // 接口返回的device对象 - QJsonObject devItem = devArray.at(i).toObject(); - QString devName = devItem.find("deviceName")->toString(); // 设备名称 - QString deviceId = devItem.find("deviceId")->toString(); // 设备ID - QString isUse = devItem.find("isUse")->toString(); // 标识A路/B路 - - // 构建Device对象 并赋值id、串口、波特率、设备编号等属性 - CounterDevice * device = new CounterDevice(this); - device->setDeviceId(deviceId); - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceName(devName); - device->setIsUse(isUse.toInt()); - - ConstCache::getInstance().deviceMap.insert(deviceId, device); - if (isUse == "1") - { - ConstCache::getInstance().deviceIdLoopA.append(deviceId); - } else if (isUse == "0") { - ConstCache::getInstance().deviceIdLoopB.append(deviceId); - } - - // 绑定显示函数 - connect(device, &CounterDevice::sendDataToDraw, - this, &CounterWindowRT::drawCounterDataOnPage); - - // 查询计数器的通道 并初始化串口 - this->getChannelList(deviceId); - } - - updateDeviceWidget(); - } -} -void CounterWindowRT::getChannelList(QString deviceId) -{ - // 查询计数器的通道 - QJsonObject response = httpReq->getChannelList(deviceId); - if (response.find("code")->toInt() == 200) - { - // 将通道绘制在界面上 - QJsonArray channelArray = response.find("data")->toArray(); - for (int i = 0; i < channelArray.size(); i++) - { - // 接口返回的channel对象 - QJsonObject channelItem = channelArray.at(i).toObject(); - QString channelId = channelItem.find("id")->toString(); // 通道ID - QString channelCode = channelItem.find("channelCode")->toString(); // 通道钟号 - QString channelNo = QString::number(channelItem.find("channelNo")->toInt()); // 通道序号 - QString delays = channelItem.find("delays")->toString(); // 通道时延值 - - // 构建通道对象 并设置属性 - CounterChannel * channel = new CounterChannel(this); - channel->setDeviceId(deviceId); - channel->setChannelId(channelId); - channel->setChannelCode(channelCode); - channel->setChannelNo(channelNo); - channel->setDelays(delays); - - // 添加到通道的集合中 key=deviceId-channelNo - ConstCache::getInstance().channelMap.insert(deviceId + "-" + channelNo, channel); - } - } + QString date = QDate::currentDate().toString("yyyy-MM-dd"); + QString time = QTime::currentTime().toString("HH:mm:ss"); + ui->labTime->setText(date + " " + time); } void CounterWindowRT::on_btnMenuSetting_clicked() @@ -425,6 +372,47 @@ setWindowState(Qt::WindowMinimized | windowState()); } +void CounterWindowRT::initChannelForm() +{ + // 1. 首先清除界面上的所有内容 + qDeleteAll(ui->widgetContent->children()); + + // 将lab列表的数量置0 + devTitleList.resize(0); + devRefList.resize(0); + channelNameList.resize(0); + channelValueList.resize(0); + + initDevAndChannelForm(SettingConfig::getInstance().DISPLAY_COUNT); +} + +void CounterWindowRT::updateDeviceWidget() +{ + QList devIds = SettingConfig::getInstance().MASTER == 1 ? ConstCache::getInstance().deviceIdLoopA : ConstCache::getInstance().deviceIdLoopB; + + for (int i = 0; i < SettingConfig::getInstance().DISPLAY_COUNT; i++) + { + if (i < devIds.size()) + { + CounterDevice * dev = ConstCache::getInstance().deviceMap.value(devIds.at(i)); + + if (dev != nullptr) { + devTitleList.at(i)->setText(dev->getDeviceName()); // 设备名称 + dev->setFormColIndex(i); + } + } else { + devTitleList.at(i)->setText(""); + devRefList.at(i)->setText(""); + + for (int j = 0; j < 16; j++) + { + channelNameList.at(i * 16 + j)->setText(""); + channelValueList.at(i * 16 + j)->setText(""); + } + } + } +} + void CounterWindowRT::onChannelNameLabelDblClicked() { QLabelDblClick * channelLab = (QLabelDblClick *) sender(); @@ -445,10 +433,50 @@ } } -void CounterWindowRT::updateDateAndTime() + +/** + * @brief CounterWindowRT::drawCounterDataOnPage + * @param messageArray + * 根据发送到Kafka中的数据串来绘制界面 + * + */ +void CounterWindowRT::drawCounterDataOnPage(QJsonArray messageArray) { - QString date = QDate::currentDate().toString("yyyy-MM-dd"); - QString time = QTime::currentTime().toString("HH:mm:ss"); - ui->labTime->setText(date + " " + time); + if (messageArray.isEmpty() == false) + { + // 获取设备id属性 + QString deviceId = messageArray.at(0).toObject().value("deviceId").toString(); + + // 如果不是当前显示的计数器 则直接返回 + CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (SettingConfig::getInstance().MASTER != devicePtr->getIsUse()) + { + return; + } + + // 根据设备id 从map中取出列索引的值 + int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); + if (devIdx > SettingConfig::getInstance().DISPLAY_COUNT) + { + return; + } + + // 参考通道 + int refChNo = messageArray.at(0).toObject().value("data").toObject().value("channelRefNo").toInt(); + CounterChannel * refChann = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(refChNo)); + if (refChann != nullptr) + { + if (devIdx < devRefList.size()) { + devRefList.at(devIdx)->setText(QString("REF:%1").arg(refChann->getChannelCode())); + } + } + + updateChannelDataForm(messageArray); + } } + +void CounterWindowRT::onKafkaMessageReceived(QJsonObject message) +{ + qDebug() << message; +} diff --git a/CounterRealTime/CounterWindowRT.h b/CounterRealTime/CounterWindowRT.h index 3f21ce7..c64e07f 100644 --- a/CounterRealTime/CounterWindowRT.h +++ b/CounterRealTime/CounterWindowRT.h @@ -18,6 +18,7 @@ #include "common/utils/SettingConfig.h" #include "common/utils/TimerCounterUtil.h" #include "common/HttpRequestController.h" +#include "common/utils/QKafkaConsumer.h" QT_BEGIN_NAMESPACE namespace Ui { class CounterWindowRT; } @@ -32,10 +33,9 @@ ~CounterWindowRT(); public slots: - void drawCounterDataOnPage(QJsonArray messageArray); + void keyPressEvent(QKeyEvent *event); - void updateDeviceWidget(); - void initChannelForm(); + void onChannelSettingSubmit(QJsonArray channelSettings); private: Ui::CounterWindowRT *ui; @@ -49,24 +49,32 @@ HttpRequestController * httpReq; - void keyPressEvent(QKeyEvent *event); + QKafkaConsumer * kafkaConsumer; int initHttpToken(); void getDeviceList(); void getChannelList(QString deviceId); void initDevAndChannelForm(int count); - void updateChannelDataForm(QJsonArray messageArray); private slots: + void updateDateAndTime(); void on_btnMenuMin_clicked(); void on_btnMenuSetting_clicked(); - void onChannelNameLabelDblClicked(); + void initChannelForm(); + void updateDeviceWidget(); - void updateDateAndTime(); + void onChannelNameLabelDblClicked(); + void drawCounterDataOnPage(QJsonArray messageArray); + + void onKafkaMessageReceived(QJsonObject message); + + + + }; #endif // COUNTERWINDOWRT_H diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 20f6177..b287ed5 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -166,7 +166,6 @@ // 获取设备列表的接口地址 QUrl url = baseUrl + "/device/list"; -// QUrl url = baseUrl + "/device/deviceList.js"; QUrlQuery query; query.addQueryItem("type", devType); url.setQuery(query); @@ -201,7 +200,6 @@ // 获取设备列表的接口地址 QUrl url = baseUrl + "/channel/list"; -// QUrl url = baseUrl + "/device/channel-" + deviceId + ".js"; QUrlQuery query; query.addQueryItem("deviceId", deviceId); url.setQuery(query); @@ -224,7 +222,41 @@ resultObj.insert("code", -1); } -// qDebug() << resultObj; + qDebug() << resultObj; + + return resultObj; +} + +QJsonObject HttpRequestController::updateChannelInfo(QJsonArray channelInfos) +{ + QJsonObject resultObj; + + // 获取设备列表的接口地址 + QUrl url = baseUrl + "/channel/qtBatchSave"; + + QNetworkRequest request; + request.setUrl(url); + request.setRawHeader("Content-type", "application/json"); + request.setRawHeader("token", token.toLocal8Bit()); + + QJsonDocument jsonDoc = QJsonDocument(channelInfos); + QByteArray postData = jsonDoc.toJson(QJsonDocument::Compact); + + qDebug() << url; + qDebug() << channelInfos; + + QNetworkReply * reply = httpUtil->sendPostRequest(request, postData); + 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/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index 5b4e4ef..860aafa 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -2,6 +2,8 @@ #define HTTPREQUESTCONTROLLER_H #include +#include +#include #include #include "utils/HttpRequestUtil.h" @@ -22,6 +24,8 @@ QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); + QJsonObject updateChannelInfo(QJsonArray channelInfos); + private: HttpRequestUtil * httpUtil; diff --git a/CounterRealTime/common/common.pri b/CounterRealTime/common/common.pri index 12bdd90..aa3f245 100644 --- a/CounterRealTime/common/common.pri +++ b/CounterRealTime/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/LinearRegression.cpp @@ -16,6 +17,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/CounterRealTime/CounterChannel.cpp b/CounterRealTime/CounterChannel.cpp index 978b969..0a4b534 100644 --- a/CounterRealTime/CounterChannel.cpp +++ b/CounterRealTime/CounterChannel.cpp @@ -30,6 +30,14 @@ { return delays; } +QString CounterChannel::getIsUse() +{ + return isUse; +} +QString CounterChannel::getName() +{ + return name; +} void CounterChannel::setActive(bool active) { this->active = active; @@ -54,14 +62,13 @@ { this->delays = delays; } - -int CounterChannel::getFormRowIndex() +void CounterChannel::setIsUse(QString isUse) { - return formRowIndex; + this->isUse = isUse; } -void CounterChannel::setFormRowIndex(int rowIndex) +void CounterChannel::setName(QString name) { - this->formRowIndex = rowIndex; + this->name = name; } QList CounterChannel::getHisData() diff --git a/CounterRealTime/CounterChannel.h b/CounterRealTime/CounterChannel.h index afc5dc2..8f2c35e 100644 --- a/CounterRealTime/CounterChannel.h +++ b/CounterRealTime/CounterChannel.h @@ -15,15 +15,16 @@ QString getChannelCode(); QString getChannelNo(); QString getDelays(); + QString getIsUse(); + QString getName(); void setActive(bool active); void setDeviceId(QString deviceId); void setChannelId(QString channelId); void setChannelCode(QString channelCode); void setChannelNo(QString channelNo); void setDelays(QString delays); - - int getFormRowIndex(); - void setFormRowIndex(int rowIndex); + void setIsUse(QString isUse); + void setName(QString name); QList getHisData(); void appendHisData(QString timestampStr, QString valueStr); @@ -35,13 +36,12 @@ QString channelCode; QString channelNo; QString delays; + QString isUse; + QString name; int maxHisDataCount = 3600; QList hisDataList; - // 通道位于界面上的哪一行 列的位置通过计数器来确定 - int formRowIndex; - signals: }; diff --git a/CounterRealTime/CounterDevice.cpp b/CounterRealTime/CounterDevice.cpp index 63200bb..1528373 100644 --- a/CounterRealTime/CounterDevice.cpp +++ b/CounterRealTime/CounterDevice.cpp @@ -1,6 +1,5 @@ #include "CounterDevice.h" -#include #include #include @@ -95,7 +94,7 @@ void CounterDevice::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); -// std::cout << this->dataBuff.toStdString(); +// qDebug() << this->dataBuff; CounterDataDto * counterData = new CounterDataDto(this); if (CounterProtocolXH::checkFrame(this->dataBuff) == true) @@ -117,7 +116,7 @@ } } else if (this->dataBuff.size() > COUNTER_FRAME_MIN_LENGTH) { - std::cout << QString("%1").arg(this->dataBuff.size()).toStdString() << std::endl; + qDebug() << QString("%1").arg(this->dataBuff.size()); this->dataBuff.clear(); } @@ -193,7 +192,7 @@ } // qDebug() << counterData->rawFrame; - qDebug() << messageArray; +// qDebug() << messageArray; // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(messageArray); diff --git a/CounterRealTime/CounterSettingForm.cpp b/CounterRealTime/CounterSettingForm.cpp index c095523..225ec88 100644 --- a/CounterRealTime/CounterSettingForm.cpp +++ b/CounterRealTime/CounterSettingForm.cpp @@ -283,43 +283,68 @@ { QString deviceId = ui->selectCounter->currentData().toString(); - for (int i = 0; i < 8; i++) + QJsonArray channelSettingUpdated; + + for (int i = 0; i < 16; i++) { + QString channelCode; + double delay; + bool active; + if (i < 8) { + channelCode = clockListLeft.at(i)->text(); + delay = delayListLeft.at(i)->value(); + active = activeListLeft.at(i)->isChecked(); + } else if (i < 16) { + channelCode = clockListRight.at(i - 8)->text(); + delay = delayListRight.at(i - 8)->value(); + active = activeListRight.at(i - 8)->isChecked(); + } + CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); if (channelPrt != nullptr) { - QString channelCode = clockListLeft.at(i)->text(); // 通道钟号 - double delay = delayListLeft.at(i)->value(); // 通道时延值 + bool isUpdated = isChannelSettingUpdated(i, channelPrt); + if (isUpdated == true) { + channelPrt->setChannelCode(channelCode); + channelPrt->setDelays(QString::number(delay)); + channelPrt->setActive(active); - channelPrt->setChannelCode(channelCode); - channelPrt->setDelays(QString::number(delay)); - channelPrt->setActive(activeListLeft.at(i)->isChecked()); - } else if (activeListLeft.at(i)->isChecked() == true) { + QJsonObject channelObj; + channelObj.insert("id", channelPrt->getChannelId()); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", channelPrt->getChannelNo()); + channelObj.insert("isUse", channelPrt->getIsUse()); + channelObj.insert("name", channelPrt->getName()); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); + } + } else if (active == true) { CounterChannel * newChannel = new CounterChannel(); newChannel->setActive(true); - newChannel->setChannelCode(clockListLeft.at(i)->text()); - newChannel->setDelays(QString::number(delayListLeft.at(i)->value())); + newChannel->setChannelCode(channelCode); + newChannel->setDelays(QString::number(delay)); ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1), newChannel); + + QJsonObject channelObj; + channelObj.insert("id", ""); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", QString::number(i + 1)); + channelObj.insert("isUse", SettingConfig::getInstance().MASTER); + channelObj.insert("name", QString("钟%1-1PPS").arg(channelCode)); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); } + } - CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr) - { - QString channelCode = clockListRight.at(i)->text(); // 通道钟号 - double delay = delayListRight.at(i)->value(); // 通道时延值 - - channelRightPrt->setChannelCode(channelCode); - channelRightPrt->setDelays(QString::number(delay)); - channelRightPrt->setActive(activeListRight.at(i)->isChecked()); - } else if (activeListRight.at(i)->isChecked() == true) { - CounterChannel * newChannel = new CounterChannel(); - newChannel->setActive(true); - newChannel->setChannelCode(clockListRight.at(i)->text()); - newChannel->setDelays(QString::number(delayListRight.at(i)->value())); - - ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1+8), newChannel); - } + if (channelSettingUpdated.size() > 0) { + emit this->channelSettingSubmit(channelSettingUpdated); } this->close(); @@ -341,10 +366,10 @@ void CounterSettingForm::on_selectCounter_currentIndexChanged(int index) { - if (index == 0) + clearChannelSettingValue(); + + if (index > 0) { - clearChannelSettingValue(); - } else { QString deviceId = ui->selectCounter->currentData().toString(); for (int i = 0; i < 8; i++) @@ -353,45 +378,37 @@ activeListRight.at(i)->setCheckable(true); CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); - if (channelPrt != nullptr && channelPrt->isActive() == true) + if (channelPrt != nullptr) { QString channelCode = channelPrt->getChannelCode(); // 通道钟号 QString delays = channelPrt->getDelays(); // 通道时延值 - activeListLeft.at(i)->setChecked(true); - activeListLeft.at(i)->setText("启用"); clockListLeft.at(i)->setText(channelCode); - clockListLeft.at(i)->setReadOnly(false); delayListLeft.at(i)->setValue(delays.toDouble()); - delayListLeft.at(i)->setReadOnly(false); - } else { - activeListLeft.at(i)->setChecked(false); - activeListLeft.at(i)->setText("未启用"); - clockListLeft.at(i)->setText("-"); - clockListLeft.at(i)->setReadOnly(true); - delayListLeft.at(i)->setValue(0.000); - delayListLeft.at(i)->setReadOnly(true); + + if (channelPrt->isActive() == true) { + activeListLeft.at(i)->setChecked(true); + activeListLeft.at(i)->setText("启用"); + clockListLeft.at(i)->setReadOnly(false); + delayListLeft.at(i)->setReadOnly(false); + } } CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr && channelRightPrt->isActive() == true) + if (channelRightPrt != nullptr) { QString channelCode = channelRightPrt->getChannelCode(); // 通道钟号 QString delays = channelRightPrt->getDelays(); // 通道时延值 - activeListRight.at(i)->setChecked(true); - activeListRight.at(i)->setText("启用"); clockListRight.at(i)->setText(channelCode); - clockListRight.at(i)->setReadOnly(false); delayListRight.at(i)->setValue(delays.toDouble()); - delayListRight.at(i)->setReadOnly(false); - } else { - activeListRight.at(i)->setChecked(false); - activeListRight.at(i)->setText("未启用"); - clockListRight.at(i)->setText("-"); - clockListRight.at(i)->setReadOnly(true); - delayListRight.at(i)->setValue(0.000); - delayListRight.at(i)->setReadOnly(true); + + if (channelRightPrt->isActive() == true) { + activeListRight.at(i)->setChecked(true); + activeListRight.at(i)->setText("启用"); + clockListRight.at(i)->setReadOnly(false); + delayListRight.at(i)->setReadOnly(false); + } } } } @@ -423,3 +440,35 @@ delayListRight.at(index - 8)->setReadOnly(!enable); } } + +bool CounterSettingForm::isChannelSettingUpdated(int index, CounterChannel *channelPtr) +{ + QString channelCode = ""; // 通道钟号 + double delay = 0.0; // 通道时延值 + bool active = false; + + if (index < 8) { + channelCode = clockListLeft.at(index)->text(); // 通道钟号 + delay = delayListLeft.at(index)->value(); // 通道时延值 + active = activeListLeft.at(index)->isChecked(); + } else if (index < 16) { + index = index - 8; + channelCode = clockListRight.at(index)->text(); // 通道钟号 + delay = delayListRight.at(index)->value(); // 通道时延值 + active = activeListRight.at(index)->isChecked(); + } + + if (channelCode != channelPtr->getChannelCode()) { + return true; + } + + if (active != channelPtr->isActive()) { + return true; + } + + if (delay - channelPtr->getDelays().toDouble() != 0) { + return true; + } + + return false; +} diff --git a/CounterRealTime/CounterSettingForm.h b/CounterRealTime/CounterSettingForm.h index b4949fb..9c41362 100644 --- a/CounterRealTime/CounterSettingForm.h +++ b/CounterRealTime/CounterSettingForm.h @@ -55,10 +55,12 @@ void clearChannelSettingValue(); void enableChannel(int index, bool enable); + bool isChannelSettingUpdated(int index, CounterChannel * channelPtr); signals: void swiftDisplayLoop(); void swiftDisplayCount(); + void channelSettingSubmit(QJsonArray channelSettings); }; #endif // COUNTERSETTINGFORM_H diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index 55db2ed..86a7776 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -12,6 +12,7 @@ resize(SettingConfig::getInstance().WINDOW_WIDTH, SettingConfig::getInstance().WINDOW_HEIGHT); settingForm = new CounterSettingForm(); + connect(settingForm, &CounterSettingForm::channelSettingSubmit, this, &CounterWindowRT::onChannelSettingSubmit); QFile file(":/qss/mainClock.css"); if (file.open(QFile::ReadOnly)) { @@ -22,6 +23,14 @@ file.close(); } + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_MSG_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &CounterWindowRT::onKafkaMessageReceived); + // 绘制界面上的表格 initChannelForm(); @@ -31,7 +40,6 @@ // 重新选择显示多少个计数器 connect(settingForm, &CounterSettingForm::swiftDisplayLoop, this, &CounterWindowRT::updateDeviceWidget); - connect(settingForm, &CounterSettingForm::swiftDisplayCount, this, &CounterWindowRT::initChannelForm); httpReq = new HttpRequestController(this); @@ -47,7 +55,7 @@ // 初始化设备和通道 getDeviceList(); -// QThread::msleep(1000 * 3); + QThread::msleep(1000 * 3); for (int i = 0; i < ConstCache::getInstance().deviceIdLoopA.size(); i++) { CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(ConstCache::getInstance().deviceIdLoopA.at(i)); @@ -58,7 +66,6 @@ CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(ConstCache::getInstance().deviceIdLoopB.at(i)); devicePtr->initSerialPort(); } - } CounterWindowRT::~CounterWindowRT() @@ -77,85 +84,94 @@ } } -/** - * @brief CounterWindowRT::drawCounterDataOnPage - * @param messageArray - * 根据发送到Kafka中的数据串来绘制界面 - * - */ -void CounterWindowRT::drawCounterDataOnPage(QJsonArray messageArray) +void CounterWindowRT::onChannelSettingSubmit(QJsonArray channelSettings) { - if (messageArray.isEmpty() == false) - { - // 获取设备id属性 - QString deviceId = messageArray.at(0).toObject().find("deviceId")->toString(); - - // 如果不是当前显示的计数器 则直接返回 - CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); - if (SettingConfig::getInstance().MASTER != devicePtr->getIsUse()) - { - return; - } - - // 根据设备id 从map中取出列索引的值 - int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); - if (devIdx > SettingConfig::getInstance().DISPLAY_COUNT) - { - return; - } - - // 参考通道 - int refChNo = messageArray.at(0).toObject().find("data")->toObject().find("channelRefNo")->toInt(); - CounterChannel * refChann = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(refChNo)); - if (refChann != nullptr) - { - if (devIdx < devRefList.size()) { - devRefList.at(devIdx)->setText(QString("REF:%1").arg(refChann->getChannelCode())); - } - } - - updateChannelDataForm(messageArray); - } + httpReq->updateChannelInfo(channelSettings); } -void CounterWindowRT::updateDeviceWidget() +int CounterWindowRT::initHttpToken() { - QList devIds = SettingConfig::getInstance().MASTER == 1 ? ConstCache::getInstance().deviceIdLoopA : ConstCache::getInstance().deviceIdLoopB; - - for (int i = 0; i < SettingConfig::getInstance().DISPLAY_COUNT; i++) + QJsonObject response = httpReq->getTokenByClientId(SettingConfig::getInstance().CLIENT_ID, + SettingConfig::getInstance().APP_KEY); + return response.value("code").toInt(); +} +void CounterWindowRT::getDeviceList() +{ + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.value("code").toInt() == 200) { - if (i < devIds.size()) + // 将获取到的设备绘制到表格中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) { - CounterDevice * dev = ConstCache::getInstance().deviceMap.value(devIds.at(i)); + // 接口返回的device对象 + QJsonObject devItem = devArray.at(i).toObject(); + QString devName = devItem.value("simpleName").toString(); // 设备名称 + QString deviceId = devItem.value("deviceId").toString(); // 设备ID + QString isUse = devItem.value("isUse").toString(); // 标识A路/B路 - QString devName = dev->getDeviceName(); // 设备名称 - devTitleList.at(i)->setText(devName.right(devName.length() - 8)); - dev->setFormColIndex(i); - } else { - devTitleList.at(i)->setText(""); - devRefList.at(i)->setText(""); + // 构建Device对象 并赋值id、串口、波特率、设备编号等属性 + CounterDevice * device = new CounterDevice(this); + device->setDeviceId(deviceId); + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceName(devName); + device->setIsUse(isUse.toInt()); - for (int j = 0; j < 16; j++) + ConstCache::getInstance().deviceMap.insert(deviceId, device); + if (isUse == "1") { - channelNameList.at(i * 16 + j)->setText(""); - channelValueList.at(i * 16 + j)->setText(""); + ConstCache::getInstance().deviceIdLoopA.append(deviceId); + } else if (isUse == "0") { + ConstCache::getInstance().deviceIdLoopB.append(deviceId); } + + // 绑定显示函数 + connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); + + // 查询计数器的通道 并初始化串口 + this->getChannelList(deviceId); } + + updateDeviceWidget(); } } - -void CounterWindowRT::initChannelForm() +void CounterWindowRT::getChannelList(QString deviceId) { - // 1. 首先清除界面上的所有内容 - qDeleteAll(ui->widgetContent->children()); + // 查询计数器的通道 + QJsonObject response = httpReq->getChannelList(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 + QString channelCode = channelItem.value("channelCode").toString(); // 通道钟号 + QString channelNo = QString::number(channelItem.value("channelNo").toInt()); // 通道序号 + QString delays = channelItem.value("delays").toString(); // 通道时延值 + QString isUse = channelItem.value("isUse").toString(); + QString name = channelItem.value("name").toString(); + bool active = channelItem.value("isActive").toString().toInt(); // 通道是否启用 - // 将lab列表的数量置0 - devTitleList.resize(0); - devRefList.resize(0); - channelNameList.resize(0); - channelValueList.resize(0); + // 构建通道对象 并设置属性 + CounterChannel * channel = new CounterChannel(this); + channel->setDeviceId(deviceId); + channel->setChannelId(channelId); + channel->setChannelCode(channelCode); + channel->setChannelNo(channelNo); + channel->setDelays(delays); + channel->setActive(active); + channel->setIsUse(isUse); + channel->setName(name); - initDevAndChannelForm(SettingConfig::getInstance().DISPLAY_COUNT); + // 添加到通道的集合中 key=deviceId-channelNo + ConstCache::getInstance().channelMap.insert(deviceId + "-" + channelNo, channel); + } + } } void CounterWindowRT::initDevAndChannelForm(int count) @@ -221,7 +237,7 @@ for ( int i = 0; i < 16; i++ ) { QLabelDblClick * labChannelName = new QLabelDblClick(widgetDev); // 通道名 - QLabel * labChannelValue = new QLabel(widgetDev); // 通道测量值 + QLabelDblClick * labChannelValue = new QLabelDblClick(widgetDev); // 通道测量值 // 默认显示内容 labChannelName->setText(QString("CH%1").arg(i+1, 2, 10, QLatin1Char('0'))); @@ -247,6 +263,7 @@ // 添加通道名称的双击事件,显示数据和残差图表 connect(labChannelName, &QLabelDblClick::doubleClicked, this, &CounterWindowRT::onChannelNameLabelDblClicked); + connect(labChannelValue, &QLabelDblClick::doubleClicked, this, &CounterWindowRT::onChannelNameLabelDblClicked); channelNameList.append(labChannelName); channelValueList.append(labChannelValue); @@ -263,14 +280,13 @@ widgetDev->setLayout(layoutChannelPage); } } - void CounterWindowRT::updateChannelDataForm(QJsonArray messageArray) { // 获取设备id属性 - QString deviceId = messageArray.at(0).toObject().find("deviceId")->toString(); + QString deviceId = messageArray.at(0).toObject().value("deviceId").toString(); // 参考通道号 - int refChNo = messageArray.at(0).toObject().find("data")->toObject().find("channelRefNo")->toInt(); + int refChNo = messageArray.at(0).toObject().value("data").toObject().value("channelRefNo").toInt(); // 根据设备id 从map中取出列索引的值 int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); @@ -285,7 +301,7 @@ for (int i = 0; i < messageArray.size(); i++) { QJsonObject channelDataItem = messageArray.at(i).toObject(); - QString channelNo = QString::number(channelDataItem.find("channelNo")->toInt()); // 通道序号 + QString channelNo = QString::number(channelDataItem.value("channelNo").toInt()); // 通道序号 channelDataJsonMap.insert(channelNo, channelDataItem); } @@ -306,13 +322,13 @@ CounterChannel * channel = ConstCache::getInstance().channelMap.value(deviceId + "-" + channelNo); // 从map中找到对应的通道 // 通道Label的序号 - int chanelIdx = devIdx * 16 + channelDataItem.find("channelNo")->toInt() - 1; + int chanelIdx = devIdx * 16 + channelDataItem.value("channelNo").toInt() - 1; if (channel != nullptr && channel->isActive() == true) { // 通道时延值 并计算 减去时延值的测量值 double delay = channel->getDelays().isEmpty() == false ? channel->getDelays().toDouble() : 0.0; - double valueMinusDelay = channelDataItem.find("data")->toObject().find("dataValue")->toString().toDouble() - delay * 1E-9; + double valueMinusDelay = channelDataItem.value("data").toObject().value("dataValue").toString().toDouble() - delay * 1E-9; // 设置通道名和测量值的QLabel channelNameList.at(chanelIdx)->setText(QString("CH%1:%2").arg(channelNoStr).arg(channel->getChannelCode())); @@ -321,6 +337,10 @@ channelNameList.at(chanelIdx)->setProperty("channelId", channel->getChannelId()); channelNameList.at(chanelIdx)->setProperty("deviceId", deviceId); channelNameList.at(chanelIdx)->setProperty("channelNo", channelNo); + + channelValueList.at(chanelIdx)->setProperty("channelId", channel->getChannelId()); + channelValueList.at(chanelIdx)->setProperty("deviceId", deviceId); + channelValueList.at(chanelIdx)->setProperty("channelNo", channelNo); } else { channelNameList.at(chanelIdx)->setText(QString("CH%1:%2").arg(channelNoStr).arg("-")); channelValueList.at(chanelIdx)->setText(""); @@ -333,84 +353,11 @@ } } -int CounterWindowRT::initHttpToken() +void CounterWindowRT::updateDateAndTime() { - QJsonObject response = httpReq->getTokenByClientId(SettingConfig::getInstance().CLIENT_ID, - SettingConfig::getInstance().APP_KEY); - return response.find("code")->toInt(); -} -void CounterWindowRT::getDeviceList() -{ - QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - if (devListRes.find("code")->toInt() == 200) - { - // 将获取到的设备绘制到表格中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - // 接口返回的device对象 - QJsonObject devItem = devArray.at(i).toObject(); - QString devName = devItem.find("deviceName")->toString(); // 设备名称 - QString deviceId = devItem.find("deviceId")->toString(); // 设备ID - QString isUse = devItem.find("isUse")->toString(); // 标识A路/B路 - - // 构建Device对象 并赋值id、串口、波特率、设备编号等属性 - CounterDevice * device = new CounterDevice(this); - device->setDeviceId(deviceId); - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceName(devName); - device->setIsUse(isUse.toInt()); - - ConstCache::getInstance().deviceMap.insert(deviceId, device); - if (isUse == "1") - { - ConstCache::getInstance().deviceIdLoopA.append(deviceId); - } else if (isUse == "0") { - ConstCache::getInstance().deviceIdLoopB.append(deviceId); - } - - // 绑定显示函数 - connect(device, &CounterDevice::sendDataToDraw, - this, &CounterWindowRT::drawCounterDataOnPage); - - // 查询计数器的通道 并初始化串口 - this->getChannelList(deviceId); - } - - updateDeviceWidget(); - } -} -void CounterWindowRT::getChannelList(QString deviceId) -{ - // 查询计数器的通道 - QJsonObject response = httpReq->getChannelList(deviceId); - if (response.find("code")->toInt() == 200) - { - // 将通道绘制在界面上 - QJsonArray channelArray = response.find("data")->toArray(); - for (int i = 0; i < channelArray.size(); i++) - { - // 接口返回的channel对象 - QJsonObject channelItem = channelArray.at(i).toObject(); - QString channelId = channelItem.find("id")->toString(); // 通道ID - QString channelCode = channelItem.find("channelCode")->toString(); // 通道钟号 - QString channelNo = QString::number(channelItem.find("channelNo")->toInt()); // 通道序号 - QString delays = channelItem.find("delays")->toString(); // 通道时延值 - - // 构建通道对象 并设置属性 - CounterChannel * channel = new CounterChannel(this); - channel->setDeviceId(deviceId); - channel->setChannelId(channelId); - channel->setChannelCode(channelCode); - channel->setChannelNo(channelNo); - channel->setDelays(delays); - - // 添加到通道的集合中 key=deviceId-channelNo - ConstCache::getInstance().channelMap.insert(deviceId + "-" + channelNo, channel); - } - } + QString date = QDate::currentDate().toString("yyyy-MM-dd"); + QString time = QTime::currentTime().toString("HH:mm:ss"); + ui->labTime->setText(date + " " + time); } void CounterWindowRT::on_btnMenuSetting_clicked() @@ -425,6 +372,47 @@ setWindowState(Qt::WindowMinimized | windowState()); } +void CounterWindowRT::initChannelForm() +{ + // 1. 首先清除界面上的所有内容 + qDeleteAll(ui->widgetContent->children()); + + // 将lab列表的数量置0 + devTitleList.resize(0); + devRefList.resize(0); + channelNameList.resize(0); + channelValueList.resize(0); + + initDevAndChannelForm(SettingConfig::getInstance().DISPLAY_COUNT); +} + +void CounterWindowRT::updateDeviceWidget() +{ + QList devIds = SettingConfig::getInstance().MASTER == 1 ? ConstCache::getInstance().deviceIdLoopA : ConstCache::getInstance().deviceIdLoopB; + + for (int i = 0; i < SettingConfig::getInstance().DISPLAY_COUNT; i++) + { + if (i < devIds.size()) + { + CounterDevice * dev = ConstCache::getInstance().deviceMap.value(devIds.at(i)); + + if (dev != nullptr) { + devTitleList.at(i)->setText(dev->getDeviceName()); // 设备名称 + dev->setFormColIndex(i); + } + } else { + devTitleList.at(i)->setText(""); + devRefList.at(i)->setText(""); + + for (int j = 0; j < 16; j++) + { + channelNameList.at(i * 16 + j)->setText(""); + channelValueList.at(i * 16 + j)->setText(""); + } + } + } +} + void CounterWindowRT::onChannelNameLabelDblClicked() { QLabelDblClick * channelLab = (QLabelDblClick *) sender(); @@ -445,10 +433,50 @@ } } -void CounterWindowRT::updateDateAndTime() + +/** + * @brief CounterWindowRT::drawCounterDataOnPage + * @param messageArray + * 根据发送到Kafka中的数据串来绘制界面 + * + */ +void CounterWindowRT::drawCounterDataOnPage(QJsonArray messageArray) { - QString date = QDate::currentDate().toString("yyyy-MM-dd"); - QString time = QTime::currentTime().toString("HH:mm:ss"); - ui->labTime->setText(date + " " + time); + if (messageArray.isEmpty() == false) + { + // 获取设备id属性 + QString deviceId = messageArray.at(0).toObject().value("deviceId").toString(); + + // 如果不是当前显示的计数器 则直接返回 + CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (SettingConfig::getInstance().MASTER != devicePtr->getIsUse()) + { + return; + } + + // 根据设备id 从map中取出列索引的值 + int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); + if (devIdx > SettingConfig::getInstance().DISPLAY_COUNT) + { + return; + } + + // 参考通道 + int refChNo = messageArray.at(0).toObject().value("data").toObject().value("channelRefNo").toInt(); + CounterChannel * refChann = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(refChNo)); + if (refChann != nullptr) + { + if (devIdx < devRefList.size()) { + devRefList.at(devIdx)->setText(QString("REF:%1").arg(refChann->getChannelCode())); + } + } + + updateChannelDataForm(messageArray); + } } + +void CounterWindowRT::onKafkaMessageReceived(QJsonObject message) +{ + qDebug() << message; +} diff --git a/CounterRealTime/CounterWindowRT.h b/CounterRealTime/CounterWindowRT.h index 3f21ce7..c64e07f 100644 --- a/CounterRealTime/CounterWindowRT.h +++ b/CounterRealTime/CounterWindowRT.h @@ -18,6 +18,7 @@ #include "common/utils/SettingConfig.h" #include "common/utils/TimerCounterUtil.h" #include "common/HttpRequestController.h" +#include "common/utils/QKafkaConsumer.h" QT_BEGIN_NAMESPACE namespace Ui { class CounterWindowRT; } @@ -32,10 +33,9 @@ ~CounterWindowRT(); public slots: - void drawCounterDataOnPage(QJsonArray messageArray); + void keyPressEvent(QKeyEvent *event); - void updateDeviceWidget(); - void initChannelForm(); + void onChannelSettingSubmit(QJsonArray channelSettings); private: Ui::CounterWindowRT *ui; @@ -49,24 +49,32 @@ HttpRequestController * httpReq; - void keyPressEvent(QKeyEvent *event); + QKafkaConsumer * kafkaConsumer; int initHttpToken(); void getDeviceList(); void getChannelList(QString deviceId); void initDevAndChannelForm(int count); - void updateChannelDataForm(QJsonArray messageArray); private slots: + void updateDateAndTime(); void on_btnMenuMin_clicked(); void on_btnMenuSetting_clicked(); - void onChannelNameLabelDblClicked(); + void initChannelForm(); + void updateDeviceWidget(); - void updateDateAndTime(); + void onChannelNameLabelDblClicked(); + void drawCounterDataOnPage(QJsonArray messageArray); + + void onKafkaMessageReceived(QJsonObject message); + + + + }; #endif // COUNTERWINDOWRT_H diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 20f6177..b287ed5 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -166,7 +166,6 @@ // 获取设备列表的接口地址 QUrl url = baseUrl + "/device/list"; -// QUrl url = baseUrl + "/device/deviceList.js"; QUrlQuery query; query.addQueryItem("type", devType); url.setQuery(query); @@ -201,7 +200,6 @@ // 获取设备列表的接口地址 QUrl url = baseUrl + "/channel/list"; -// QUrl url = baseUrl + "/device/channel-" + deviceId + ".js"; QUrlQuery query; query.addQueryItem("deviceId", deviceId); url.setQuery(query); @@ -224,7 +222,41 @@ resultObj.insert("code", -1); } -// qDebug() << resultObj; + qDebug() << resultObj; + + return resultObj; +} + +QJsonObject HttpRequestController::updateChannelInfo(QJsonArray channelInfos) +{ + QJsonObject resultObj; + + // 获取设备列表的接口地址 + QUrl url = baseUrl + "/channel/qtBatchSave"; + + QNetworkRequest request; + request.setUrl(url); + request.setRawHeader("Content-type", "application/json"); + request.setRawHeader("token", token.toLocal8Bit()); + + QJsonDocument jsonDoc = QJsonDocument(channelInfos); + QByteArray postData = jsonDoc.toJson(QJsonDocument::Compact); + + qDebug() << url; + qDebug() << channelInfos; + + QNetworkReply * reply = httpUtil->sendPostRequest(request, postData); + 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/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index 5b4e4ef..860aafa 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -2,6 +2,8 @@ #define HTTPREQUESTCONTROLLER_H #include +#include +#include #include #include "utils/HttpRequestUtil.h" @@ -22,6 +24,8 @@ QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); + QJsonObject updateChannelInfo(QJsonArray channelInfos); + private: HttpRequestUtil * httpUtil; diff --git a/CounterRealTime/common/common.pri b/CounterRealTime/common/common.pri index 12bdd90..aa3f245 100644 --- a/CounterRealTime/common/common.pri +++ b/CounterRealTime/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/LinearRegression.cpp @@ -16,6 +17,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/CounterRealTime/common/utils/QKafkaConsumer.cpp b/CounterRealTime/common/utils/QKafkaConsumer.cpp new file mode 100644 index 0000000..cb4fe37 --- /dev/null +++ b/CounterRealTime/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/CounterRealTime/CounterChannel.cpp b/CounterRealTime/CounterChannel.cpp index 978b969..0a4b534 100644 --- a/CounterRealTime/CounterChannel.cpp +++ b/CounterRealTime/CounterChannel.cpp @@ -30,6 +30,14 @@ { return delays; } +QString CounterChannel::getIsUse() +{ + return isUse; +} +QString CounterChannel::getName() +{ + return name; +} void CounterChannel::setActive(bool active) { this->active = active; @@ -54,14 +62,13 @@ { this->delays = delays; } - -int CounterChannel::getFormRowIndex() +void CounterChannel::setIsUse(QString isUse) { - return formRowIndex; + this->isUse = isUse; } -void CounterChannel::setFormRowIndex(int rowIndex) +void CounterChannel::setName(QString name) { - this->formRowIndex = rowIndex; + this->name = name; } QList CounterChannel::getHisData() diff --git a/CounterRealTime/CounterChannel.h b/CounterRealTime/CounterChannel.h index afc5dc2..8f2c35e 100644 --- a/CounterRealTime/CounterChannel.h +++ b/CounterRealTime/CounterChannel.h @@ -15,15 +15,16 @@ QString getChannelCode(); QString getChannelNo(); QString getDelays(); + QString getIsUse(); + QString getName(); void setActive(bool active); void setDeviceId(QString deviceId); void setChannelId(QString channelId); void setChannelCode(QString channelCode); void setChannelNo(QString channelNo); void setDelays(QString delays); - - int getFormRowIndex(); - void setFormRowIndex(int rowIndex); + void setIsUse(QString isUse); + void setName(QString name); QList getHisData(); void appendHisData(QString timestampStr, QString valueStr); @@ -35,13 +36,12 @@ QString channelCode; QString channelNo; QString delays; + QString isUse; + QString name; int maxHisDataCount = 3600; QList hisDataList; - // 通道位于界面上的哪一行 列的位置通过计数器来确定 - int formRowIndex; - signals: }; diff --git a/CounterRealTime/CounterDevice.cpp b/CounterRealTime/CounterDevice.cpp index 63200bb..1528373 100644 --- a/CounterRealTime/CounterDevice.cpp +++ b/CounterRealTime/CounterDevice.cpp @@ -1,6 +1,5 @@ #include "CounterDevice.h" -#include #include #include @@ -95,7 +94,7 @@ void CounterDevice::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); -// std::cout << this->dataBuff.toStdString(); +// qDebug() << this->dataBuff; CounterDataDto * counterData = new CounterDataDto(this); if (CounterProtocolXH::checkFrame(this->dataBuff) == true) @@ -117,7 +116,7 @@ } } else if (this->dataBuff.size() > COUNTER_FRAME_MIN_LENGTH) { - std::cout << QString("%1").arg(this->dataBuff.size()).toStdString() << std::endl; + qDebug() << QString("%1").arg(this->dataBuff.size()); this->dataBuff.clear(); } @@ -193,7 +192,7 @@ } // qDebug() << counterData->rawFrame; - qDebug() << messageArray; +// qDebug() << messageArray; // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(messageArray); diff --git a/CounterRealTime/CounterSettingForm.cpp b/CounterRealTime/CounterSettingForm.cpp index c095523..225ec88 100644 --- a/CounterRealTime/CounterSettingForm.cpp +++ b/CounterRealTime/CounterSettingForm.cpp @@ -283,43 +283,68 @@ { QString deviceId = ui->selectCounter->currentData().toString(); - for (int i = 0; i < 8; i++) + QJsonArray channelSettingUpdated; + + for (int i = 0; i < 16; i++) { + QString channelCode; + double delay; + bool active; + if (i < 8) { + channelCode = clockListLeft.at(i)->text(); + delay = delayListLeft.at(i)->value(); + active = activeListLeft.at(i)->isChecked(); + } else if (i < 16) { + channelCode = clockListRight.at(i - 8)->text(); + delay = delayListRight.at(i - 8)->value(); + active = activeListRight.at(i - 8)->isChecked(); + } + CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); if (channelPrt != nullptr) { - QString channelCode = clockListLeft.at(i)->text(); // 通道钟号 - double delay = delayListLeft.at(i)->value(); // 通道时延值 + bool isUpdated = isChannelSettingUpdated(i, channelPrt); + if (isUpdated == true) { + channelPrt->setChannelCode(channelCode); + channelPrt->setDelays(QString::number(delay)); + channelPrt->setActive(active); - channelPrt->setChannelCode(channelCode); - channelPrt->setDelays(QString::number(delay)); - channelPrt->setActive(activeListLeft.at(i)->isChecked()); - } else if (activeListLeft.at(i)->isChecked() == true) { + QJsonObject channelObj; + channelObj.insert("id", channelPrt->getChannelId()); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", channelPrt->getChannelNo()); + channelObj.insert("isUse", channelPrt->getIsUse()); + channelObj.insert("name", channelPrt->getName()); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); + } + } else if (active == true) { CounterChannel * newChannel = new CounterChannel(); newChannel->setActive(true); - newChannel->setChannelCode(clockListLeft.at(i)->text()); - newChannel->setDelays(QString::number(delayListLeft.at(i)->value())); + newChannel->setChannelCode(channelCode); + newChannel->setDelays(QString::number(delay)); ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1), newChannel); + + QJsonObject channelObj; + channelObj.insert("id", ""); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", QString::number(i + 1)); + channelObj.insert("isUse", SettingConfig::getInstance().MASTER); + channelObj.insert("name", QString("钟%1-1PPS").arg(channelCode)); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); } + } - CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr) - { - QString channelCode = clockListRight.at(i)->text(); // 通道钟号 - double delay = delayListRight.at(i)->value(); // 通道时延值 - - channelRightPrt->setChannelCode(channelCode); - channelRightPrt->setDelays(QString::number(delay)); - channelRightPrt->setActive(activeListRight.at(i)->isChecked()); - } else if (activeListRight.at(i)->isChecked() == true) { - CounterChannel * newChannel = new CounterChannel(); - newChannel->setActive(true); - newChannel->setChannelCode(clockListRight.at(i)->text()); - newChannel->setDelays(QString::number(delayListRight.at(i)->value())); - - ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1+8), newChannel); - } + if (channelSettingUpdated.size() > 0) { + emit this->channelSettingSubmit(channelSettingUpdated); } this->close(); @@ -341,10 +366,10 @@ void CounterSettingForm::on_selectCounter_currentIndexChanged(int index) { - if (index == 0) + clearChannelSettingValue(); + + if (index > 0) { - clearChannelSettingValue(); - } else { QString deviceId = ui->selectCounter->currentData().toString(); for (int i = 0; i < 8; i++) @@ -353,45 +378,37 @@ activeListRight.at(i)->setCheckable(true); CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); - if (channelPrt != nullptr && channelPrt->isActive() == true) + if (channelPrt != nullptr) { QString channelCode = channelPrt->getChannelCode(); // 通道钟号 QString delays = channelPrt->getDelays(); // 通道时延值 - activeListLeft.at(i)->setChecked(true); - activeListLeft.at(i)->setText("启用"); clockListLeft.at(i)->setText(channelCode); - clockListLeft.at(i)->setReadOnly(false); delayListLeft.at(i)->setValue(delays.toDouble()); - delayListLeft.at(i)->setReadOnly(false); - } else { - activeListLeft.at(i)->setChecked(false); - activeListLeft.at(i)->setText("未启用"); - clockListLeft.at(i)->setText("-"); - clockListLeft.at(i)->setReadOnly(true); - delayListLeft.at(i)->setValue(0.000); - delayListLeft.at(i)->setReadOnly(true); + + if (channelPrt->isActive() == true) { + activeListLeft.at(i)->setChecked(true); + activeListLeft.at(i)->setText("启用"); + clockListLeft.at(i)->setReadOnly(false); + delayListLeft.at(i)->setReadOnly(false); + } } CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr && channelRightPrt->isActive() == true) + if (channelRightPrt != nullptr) { QString channelCode = channelRightPrt->getChannelCode(); // 通道钟号 QString delays = channelRightPrt->getDelays(); // 通道时延值 - activeListRight.at(i)->setChecked(true); - activeListRight.at(i)->setText("启用"); clockListRight.at(i)->setText(channelCode); - clockListRight.at(i)->setReadOnly(false); delayListRight.at(i)->setValue(delays.toDouble()); - delayListRight.at(i)->setReadOnly(false); - } else { - activeListRight.at(i)->setChecked(false); - activeListRight.at(i)->setText("未启用"); - clockListRight.at(i)->setText("-"); - clockListRight.at(i)->setReadOnly(true); - delayListRight.at(i)->setValue(0.000); - delayListRight.at(i)->setReadOnly(true); + + if (channelRightPrt->isActive() == true) { + activeListRight.at(i)->setChecked(true); + activeListRight.at(i)->setText("启用"); + clockListRight.at(i)->setReadOnly(false); + delayListRight.at(i)->setReadOnly(false); + } } } } @@ -423,3 +440,35 @@ delayListRight.at(index - 8)->setReadOnly(!enable); } } + +bool CounterSettingForm::isChannelSettingUpdated(int index, CounterChannel *channelPtr) +{ + QString channelCode = ""; // 通道钟号 + double delay = 0.0; // 通道时延值 + bool active = false; + + if (index < 8) { + channelCode = clockListLeft.at(index)->text(); // 通道钟号 + delay = delayListLeft.at(index)->value(); // 通道时延值 + active = activeListLeft.at(index)->isChecked(); + } else if (index < 16) { + index = index - 8; + channelCode = clockListRight.at(index)->text(); // 通道钟号 + delay = delayListRight.at(index)->value(); // 通道时延值 + active = activeListRight.at(index)->isChecked(); + } + + if (channelCode != channelPtr->getChannelCode()) { + return true; + } + + if (active != channelPtr->isActive()) { + return true; + } + + if (delay - channelPtr->getDelays().toDouble() != 0) { + return true; + } + + return false; +} diff --git a/CounterRealTime/CounterSettingForm.h b/CounterRealTime/CounterSettingForm.h index b4949fb..9c41362 100644 --- a/CounterRealTime/CounterSettingForm.h +++ b/CounterRealTime/CounterSettingForm.h @@ -55,10 +55,12 @@ void clearChannelSettingValue(); void enableChannel(int index, bool enable); + bool isChannelSettingUpdated(int index, CounterChannel * channelPtr); signals: void swiftDisplayLoop(); void swiftDisplayCount(); + void channelSettingSubmit(QJsonArray channelSettings); }; #endif // COUNTERSETTINGFORM_H diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index 55db2ed..86a7776 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -12,6 +12,7 @@ resize(SettingConfig::getInstance().WINDOW_WIDTH, SettingConfig::getInstance().WINDOW_HEIGHT); settingForm = new CounterSettingForm(); + connect(settingForm, &CounterSettingForm::channelSettingSubmit, this, &CounterWindowRT::onChannelSettingSubmit); QFile file(":/qss/mainClock.css"); if (file.open(QFile::ReadOnly)) { @@ -22,6 +23,14 @@ file.close(); } + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_MSG_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &CounterWindowRT::onKafkaMessageReceived); + // 绘制界面上的表格 initChannelForm(); @@ -31,7 +40,6 @@ // 重新选择显示多少个计数器 connect(settingForm, &CounterSettingForm::swiftDisplayLoop, this, &CounterWindowRT::updateDeviceWidget); - connect(settingForm, &CounterSettingForm::swiftDisplayCount, this, &CounterWindowRT::initChannelForm); httpReq = new HttpRequestController(this); @@ -47,7 +55,7 @@ // 初始化设备和通道 getDeviceList(); -// QThread::msleep(1000 * 3); + QThread::msleep(1000 * 3); for (int i = 0; i < ConstCache::getInstance().deviceIdLoopA.size(); i++) { CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(ConstCache::getInstance().deviceIdLoopA.at(i)); @@ -58,7 +66,6 @@ CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(ConstCache::getInstance().deviceIdLoopB.at(i)); devicePtr->initSerialPort(); } - } CounterWindowRT::~CounterWindowRT() @@ -77,85 +84,94 @@ } } -/** - * @brief CounterWindowRT::drawCounterDataOnPage - * @param messageArray - * 根据发送到Kafka中的数据串来绘制界面 - * - */ -void CounterWindowRT::drawCounterDataOnPage(QJsonArray messageArray) +void CounterWindowRT::onChannelSettingSubmit(QJsonArray channelSettings) { - if (messageArray.isEmpty() == false) - { - // 获取设备id属性 - QString deviceId = messageArray.at(0).toObject().find("deviceId")->toString(); - - // 如果不是当前显示的计数器 则直接返回 - CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); - if (SettingConfig::getInstance().MASTER != devicePtr->getIsUse()) - { - return; - } - - // 根据设备id 从map中取出列索引的值 - int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); - if (devIdx > SettingConfig::getInstance().DISPLAY_COUNT) - { - return; - } - - // 参考通道 - int refChNo = messageArray.at(0).toObject().find("data")->toObject().find("channelRefNo")->toInt(); - CounterChannel * refChann = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(refChNo)); - if (refChann != nullptr) - { - if (devIdx < devRefList.size()) { - devRefList.at(devIdx)->setText(QString("REF:%1").arg(refChann->getChannelCode())); - } - } - - updateChannelDataForm(messageArray); - } + httpReq->updateChannelInfo(channelSettings); } -void CounterWindowRT::updateDeviceWidget() +int CounterWindowRT::initHttpToken() { - QList devIds = SettingConfig::getInstance().MASTER == 1 ? ConstCache::getInstance().deviceIdLoopA : ConstCache::getInstance().deviceIdLoopB; - - for (int i = 0; i < SettingConfig::getInstance().DISPLAY_COUNT; i++) + QJsonObject response = httpReq->getTokenByClientId(SettingConfig::getInstance().CLIENT_ID, + SettingConfig::getInstance().APP_KEY); + return response.value("code").toInt(); +} +void CounterWindowRT::getDeviceList() +{ + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.value("code").toInt() == 200) { - if (i < devIds.size()) + // 将获取到的设备绘制到表格中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) { - CounterDevice * dev = ConstCache::getInstance().deviceMap.value(devIds.at(i)); + // 接口返回的device对象 + QJsonObject devItem = devArray.at(i).toObject(); + QString devName = devItem.value("simpleName").toString(); // 设备名称 + QString deviceId = devItem.value("deviceId").toString(); // 设备ID + QString isUse = devItem.value("isUse").toString(); // 标识A路/B路 - QString devName = dev->getDeviceName(); // 设备名称 - devTitleList.at(i)->setText(devName.right(devName.length() - 8)); - dev->setFormColIndex(i); - } else { - devTitleList.at(i)->setText(""); - devRefList.at(i)->setText(""); + // 构建Device对象 并赋值id、串口、波特率、设备编号等属性 + CounterDevice * device = new CounterDevice(this); + device->setDeviceId(deviceId); + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceName(devName); + device->setIsUse(isUse.toInt()); - for (int j = 0; j < 16; j++) + ConstCache::getInstance().deviceMap.insert(deviceId, device); + if (isUse == "1") { - channelNameList.at(i * 16 + j)->setText(""); - channelValueList.at(i * 16 + j)->setText(""); + ConstCache::getInstance().deviceIdLoopA.append(deviceId); + } else if (isUse == "0") { + ConstCache::getInstance().deviceIdLoopB.append(deviceId); } + + // 绑定显示函数 + connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); + + // 查询计数器的通道 并初始化串口 + this->getChannelList(deviceId); } + + updateDeviceWidget(); } } - -void CounterWindowRT::initChannelForm() +void CounterWindowRT::getChannelList(QString deviceId) { - // 1. 首先清除界面上的所有内容 - qDeleteAll(ui->widgetContent->children()); + // 查询计数器的通道 + QJsonObject response = httpReq->getChannelList(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 + QString channelCode = channelItem.value("channelCode").toString(); // 通道钟号 + QString channelNo = QString::number(channelItem.value("channelNo").toInt()); // 通道序号 + QString delays = channelItem.value("delays").toString(); // 通道时延值 + QString isUse = channelItem.value("isUse").toString(); + QString name = channelItem.value("name").toString(); + bool active = channelItem.value("isActive").toString().toInt(); // 通道是否启用 - // 将lab列表的数量置0 - devTitleList.resize(0); - devRefList.resize(0); - channelNameList.resize(0); - channelValueList.resize(0); + // 构建通道对象 并设置属性 + CounterChannel * channel = new CounterChannel(this); + channel->setDeviceId(deviceId); + channel->setChannelId(channelId); + channel->setChannelCode(channelCode); + channel->setChannelNo(channelNo); + channel->setDelays(delays); + channel->setActive(active); + channel->setIsUse(isUse); + channel->setName(name); - initDevAndChannelForm(SettingConfig::getInstance().DISPLAY_COUNT); + // 添加到通道的集合中 key=deviceId-channelNo + ConstCache::getInstance().channelMap.insert(deviceId + "-" + channelNo, channel); + } + } } void CounterWindowRT::initDevAndChannelForm(int count) @@ -221,7 +237,7 @@ for ( int i = 0; i < 16; i++ ) { QLabelDblClick * labChannelName = new QLabelDblClick(widgetDev); // 通道名 - QLabel * labChannelValue = new QLabel(widgetDev); // 通道测量值 + QLabelDblClick * labChannelValue = new QLabelDblClick(widgetDev); // 通道测量值 // 默认显示内容 labChannelName->setText(QString("CH%1").arg(i+1, 2, 10, QLatin1Char('0'))); @@ -247,6 +263,7 @@ // 添加通道名称的双击事件,显示数据和残差图表 connect(labChannelName, &QLabelDblClick::doubleClicked, this, &CounterWindowRT::onChannelNameLabelDblClicked); + connect(labChannelValue, &QLabelDblClick::doubleClicked, this, &CounterWindowRT::onChannelNameLabelDblClicked); channelNameList.append(labChannelName); channelValueList.append(labChannelValue); @@ -263,14 +280,13 @@ widgetDev->setLayout(layoutChannelPage); } } - void CounterWindowRT::updateChannelDataForm(QJsonArray messageArray) { // 获取设备id属性 - QString deviceId = messageArray.at(0).toObject().find("deviceId")->toString(); + QString deviceId = messageArray.at(0).toObject().value("deviceId").toString(); // 参考通道号 - int refChNo = messageArray.at(0).toObject().find("data")->toObject().find("channelRefNo")->toInt(); + int refChNo = messageArray.at(0).toObject().value("data").toObject().value("channelRefNo").toInt(); // 根据设备id 从map中取出列索引的值 int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); @@ -285,7 +301,7 @@ for (int i = 0; i < messageArray.size(); i++) { QJsonObject channelDataItem = messageArray.at(i).toObject(); - QString channelNo = QString::number(channelDataItem.find("channelNo")->toInt()); // 通道序号 + QString channelNo = QString::number(channelDataItem.value("channelNo").toInt()); // 通道序号 channelDataJsonMap.insert(channelNo, channelDataItem); } @@ -306,13 +322,13 @@ CounterChannel * channel = ConstCache::getInstance().channelMap.value(deviceId + "-" + channelNo); // 从map中找到对应的通道 // 通道Label的序号 - int chanelIdx = devIdx * 16 + channelDataItem.find("channelNo")->toInt() - 1; + int chanelIdx = devIdx * 16 + channelDataItem.value("channelNo").toInt() - 1; if (channel != nullptr && channel->isActive() == true) { // 通道时延值 并计算 减去时延值的测量值 double delay = channel->getDelays().isEmpty() == false ? channel->getDelays().toDouble() : 0.0; - double valueMinusDelay = channelDataItem.find("data")->toObject().find("dataValue")->toString().toDouble() - delay * 1E-9; + double valueMinusDelay = channelDataItem.value("data").toObject().value("dataValue").toString().toDouble() - delay * 1E-9; // 设置通道名和测量值的QLabel channelNameList.at(chanelIdx)->setText(QString("CH%1:%2").arg(channelNoStr).arg(channel->getChannelCode())); @@ -321,6 +337,10 @@ channelNameList.at(chanelIdx)->setProperty("channelId", channel->getChannelId()); channelNameList.at(chanelIdx)->setProperty("deviceId", deviceId); channelNameList.at(chanelIdx)->setProperty("channelNo", channelNo); + + channelValueList.at(chanelIdx)->setProperty("channelId", channel->getChannelId()); + channelValueList.at(chanelIdx)->setProperty("deviceId", deviceId); + channelValueList.at(chanelIdx)->setProperty("channelNo", channelNo); } else { channelNameList.at(chanelIdx)->setText(QString("CH%1:%2").arg(channelNoStr).arg("-")); channelValueList.at(chanelIdx)->setText(""); @@ -333,84 +353,11 @@ } } -int CounterWindowRT::initHttpToken() +void CounterWindowRT::updateDateAndTime() { - QJsonObject response = httpReq->getTokenByClientId(SettingConfig::getInstance().CLIENT_ID, - SettingConfig::getInstance().APP_KEY); - return response.find("code")->toInt(); -} -void CounterWindowRT::getDeviceList() -{ - QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - if (devListRes.find("code")->toInt() == 200) - { - // 将获取到的设备绘制到表格中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - // 接口返回的device对象 - QJsonObject devItem = devArray.at(i).toObject(); - QString devName = devItem.find("deviceName")->toString(); // 设备名称 - QString deviceId = devItem.find("deviceId")->toString(); // 设备ID - QString isUse = devItem.find("isUse")->toString(); // 标识A路/B路 - - // 构建Device对象 并赋值id、串口、波特率、设备编号等属性 - CounterDevice * device = new CounterDevice(this); - device->setDeviceId(deviceId); - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceName(devName); - device->setIsUse(isUse.toInt()); - - ConstCache::getInstance().deviceMap.insert(deviceId, device); - if (isUse == "1") - { - ConstCache::getInstance().deviceIdLoopA.append(deviceId); - } else if (isUse == "0") { - ConstCache::getInstance().deviceIdLoopB.append(deviceId); - } - - // 绑定显示函数 - connect(device, &CounterDevice::sendDataToDraw, - this, &CounterWindowRT::drawCounterDataOnPage); - - // 查询计数器的通道 并初始化串口 - this->getChannelList(deviceId); - } - - updateDeviceWidget(); - } -} -void CounterWindowRT::getChannelList(QString deviceId) -{ - // 查询计数器的通道 - QJsonObject response = httpReq->getChannelList(deviceId); - if (response.find("code")->toInt() == 200) - { - // 将通道绘制在界面上 - QJsonArray channelArray = response.find("data")->toArray(); - for (int i = 0; i < channelArray.size(); i++) - { - // 接口返回的channel对象 - QJsonObject channelItem = channelArray.at(i).toObject(); - QString channelId = channelItem.find("id")->toString(); // 通道ID - QString channelCode = channelItem.find("channelCode")->toString(); // 通道钟号 - QString channelNo = QString::number(channelItem.find("channelNo")->toInt()); // 通道序号 - QString delays = channelItem.find("delays")->toString(); // 通道时延值 - - // 构建通道对象 并设置属性 - CounterChannel * channel = new CounterChannel(this); - channel->setDeviceId(deviceId); - channel->setChannelId(channelId); - channel->setChannelCode(channelCode); - channel->setChannelNo(channelNo); - channel->setDelays(delays); - - // 添加到通道的集合中 key=deviceId-channelNo - ConstCache::getInstance().channelMap.insert(deviceId + "-" + channelNo, channel); - } - } + QString date = QDate::currentDate().toString("yyyy-MM-dd"); + QString time = QTime::currentTime().toString("HH:mm:ss"); + ui->labTime->setText(date + " " + time); } void CounterWindowRT::on_btnMenuSetting_clicked() @@ -425,6 +372,47 @@ setWindowState(Qt::WindowMinimized | windowState()); } +void CounterWindowRT::initChannelForm() +{ + // 1. 首先清除界面上的所有内容 + qDeleteAll(ui->widgetContent->children()); + + // 将lab列表的数量置0 + devTitleList.resize(0); + devRefList.resize(0); + channelNameList.resize(0); + channelValueList.resize(0); + + initDevAndChannelForm(SettingConfig::getInstance().DISPLAY_COUNT); +} + +void CounterWindowRT::updateDeviceWidget() +{ + QList devIds = SettingConfig::getInstance().MASTER == 1 ? ConstCache::getInstance().deviceIdLoopA : ConstCache::getInstance().deviceIdLoopB; + + for (int i = 0; i < SettingConfig::getInstance().DISPLAY_COUNT; i++) + { + if (i < devIds.size()) + { + CounterDevice * dev = ConstCache::getInstance().deviceMap.value(devIds.at(i)); + + if (dev != nullptr) { + devTitleList.at(i)->setText(dev->getDeviceName()); // 设备名称 + dev->setFormColIndex(i); + } + } else { + devTitleList.at(i)->setText(""); + devRefList.at(i)->setText(""); + + for (int j = 0; j < 16; j++) + { + channelNameList.at(i * 16 + j)->setText(""); + channelValueList.at(i * 16 + j)->setText(""); + } + } + } +} + void CounterWindowRT::onChannelNameLabelDblClicked() { QLabelDblClick * channelLab = (QLabelDblClick *) sender(); @@ -445,10 +433,50 @@ } } -void CounterWindowRT::updateDateAndTime() + +/** + * @brief CounterWindowRT::drawCounterDataOnPage + * @param messageArray + * 根据发送到Kafka中的数据串来绘制界面 + * + */ +void CounterWindowRT::drawCounterDataOnPage(QJsonArray messageArray) { - QString date = QDate::currentDate().toString("yyyy-MM-dd"); - QString time = QTime::currentTime().toString("HH:mm:ss"); - ui->labTime->setText(date + " " + time); + if (messageArray.isEmpty() == false) + { + // 获取设备id属性 + QString deviceId = messageArray.at(0).toObject().value("deviceId").toString(); + + // 如果不是当前显示的计数器 则直接返回 + CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (SettingConfig::getInstance().MASTER != devicePtr->getIsUse()) + { + return; + } + + // 根据设备id 从map中取出列索引的值 + int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); + if (devIdx > SettingConfig::getInstance().DISPLAY_COUNT) + { + return; + } + + // 参考通道 + int refChNo = messageArray.at(0).toObject().value("data").toObject().value("channelRefNo").toInt(); + CounterChannel * refChann = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(refChNo)); + if (refChann != nullptr) + { + if (devIdx < devRefList.size()) { + devRefList.at(devIdx)->setText(QString("REF:%1").arg(refChann->getChannelCode())); + } + } + + updateChannelDataForm(messageArray); + } } + +void CounterWindowRT::onKafkaMessageReceived(QJsonObject message) +{ + qDebug() << message; +} diff --git a/CounterRealTime/CounterWindowRT.h b/CounterRealTime/CounterWindowRT.h index 3f21ce7..c64e07f 100644 --- a/CounterRealTime/CounterWindowRT.h +++ b/CounterRealTime/CounterWindowRT.h @@ -18,6 +18,7 @@ #include "common/utils/SettingConfig.h" #include "common/utils/TimerCounterUtil.h" #include "common/HttpRequestController.h" +#include "common/utils/QKafkaConsumer.h" QT_BEGIN_NAMESPACE namespace Ui { class CounterWindowRT; } @@ -32,10 +33,9 @@ ~CounterWindowRT(); public slots: - void drawCounterDataOnPage(QJsonArray messageArray); + void keyPressEvent(QKeyEvent *event); - void updateDeviceWidget(); - void initChannelForm(); + void onChannelSettingSubmit(QJsonArray channelSettings); private: Ui::CounterWindowRT *ui; @@ -49,24 +49,32 @@ HttpRequestController * httpReq; - void keyPressEvent(QKeyEvent *event); + QKafkaConsumer * kafkaConsumer; int initHttpToken(); void getDeviceList(); void getChannelList(QString deviceId); void initDevAndChannelForm(int count); - void updateChannelDataForm(QJsonArray messageArray); private slots: + void updateDateAndTime(); void on_btnMenuMin_clicked(); void on_btnMenuSetting_clicked(); - void onChannelNameLabelDblClicked(); + void initChannelForm(); + void updateDeviceWidget(); - void updateDateAndTime(); + void onChannelNameLabelDblClicked(); + void drawCounterDataOnPage(QJsonArray messageArray); + + void onKafkaMessageReceived(QJsonObject message); + + + + }; #endif // COUNTERWINDOWRT_H diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 20f6177..b287ed5 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -166,7 +166,6 @@ // 获取设备列表的接口地址 QUrl url = baseUrl + "/device/list"; -// QUrl url = baseUrl + "/device/deviceList.js"; QUrlQuery query; query.addQueryItem("type", devType); url.setQuery(query); @@ -201,7 +200,6 @@ // 获取设备列表的接口地址 QUrl url = baseUrl + "/channel/list"; -// QUrl url = baseUrl + "/device/channel-" + deviceId + ".js"; QUrlQuery query; query.addQueryItem("deviceId", deviceId); url.setQuery(query); @@ -224,7 +222,41 @@ resultObj.insert("code", -1); } -// qDebug() << resultObj; + qDebug() << resultObj; + + return resultObj; +} + +QJsonObject HttpRequestController::updateChannelInfo(QJsonArray channelInfos) +{ + QJsonObject resultObj; + + // 获取设备列表的接口地址 + QUrl url = baseUrl + "/channel/qtBatchSave"; + + QNetworkRequest request; + request.setUrl(url); + request.setRawHeader("Content-type", "application/json"); + request.setRawHeader("token", token.toLocal8Bit()); + + QJsonDocument jsonDoc = QJsonDocument(channelInfos); + QByteArray postData = jsonDoc.toJson(QJsonDocument::Compact); + + qDebug() << url; + qDebug() << channelInfos; + + QNetworkReply * reply = httpUtil->sendPostRequest(request, postData); + 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/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index 5b4e4ef..860aafa 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -2,6 +2,8 @@ #define HTTPREQUESTCONTROLLER_H #include +#include +#include #include #include "utils/HttpRequestUtil.h" @@ -22,6 +24,8 @@ QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); + QJsonObject updateChannelInfo(QJsonArray channelInfos); + private: HttpRequestUtil * httpUtil; diff --git a/CounterRealTime/common/common.pri b/CounterRealTime/common/common.pri index 12bdd90..aa3f245 100644 --- a/CounterRealTime/common/common.pri +++ b/CounterRealTime/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/LinearRegression.cpp @@ -16,6 +17,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/CounterRealTime/common/utils/QKafkaConsumer.cpp b/CounterRealTime/common/utils/QKafkaConsumer.cpp new file mode 100644 index 0000000..cb4fe37 --- /dev/null +++ b/CounterRealTime/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/CounterRealTime/common/utils/QKafkaConsumer.h b/CounterRealTime/common/utils/QKafkaConsumer.h new file mode 100644 index 0000000..84f4702 --- /dev/null +++ b/CounterRealTime/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/CounterRealTime/CounterChannel.cpp b/CounterRealTime/CounterChannel.cpp index 978b969..0a4b534 100644 --- a/CounterRealTime/CounterChannel.cpp +++ b/CounterRealTime/CounterChannel.cpp @@ -30,6 +30,14 @@ { return delays; } +QString CounterChannel::getIsUse() +{ + return isUse; +} +QString CounterChannel::getName() +{ + return name; +} void CounterChannel::setActive(bool active) { this->active = active; @@ -54,14 +62,13 @@ { this->delays = delays; } - -int CounterChannel::getFormRowIndex() +void CounterChannel::setIsUse(QString isUse) { - return formRowIndex; + this->isUse = isUse; } -void CounterChannel::setFormRowIndex(int rowIndex) +void CounterChannel::setName(QString name) { - this->formRowIndex = rowIndex; + this->name = name; } QList CounterChannel::getHisData() diff --git a/CounterRealTime/CounterChannel.h b/CounterRealTime/CounterChannel.h index afc5dc2..8f2c35e 100644 --- a/CounterRealTime/CounterChannel.h +++ b/CounterRealTime/CounterChannel.h @@ -15,15 +15,16 @@ QString getChannelCode(); QString getChannelNo(); QString getDelays(); + QString getIsUse(); + QString getName(); void setActive(bool active); void setDeviceId(QString deviceId); void setChannelId(QString channelId); void setChannelCode(QString channelCode); void setChannelNo(QString channelNo); void setDelays(QString delays); - - int getFormRowIndex(); - void setFormRowIndex(int rowIndex); + void setIsUse(QString isUse); + void setName(QString name); QList getHisData(); void appendHisData(QString timestampStr, QString valueStr); @@ -35,13 +36,12 @@ QString channelCode; QString channelNo; QString delays; + QString isUse; + QString name; int maxHisDataCount = 3600; QList hisDataList; - // 通道位于界面上的哪一行 列的位置通过计数器来确定 - int formRowIndex; - signals: }; diff --git a/CounterRealTime/CounterDevice.cpp b/CounterRealTime/CounterDevice.cpp index 63200bb..1528373 100644 --- a/CounterRealTime/CounterDevice.cpp +++ b/CounterRealTime/CounterDevice.cpp @@ -1,6 +1,5 @@ #include "CounterDevice.h" -#include #include #include @@ -95,7 +94,7 @@ void CounterDevice::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); -// std::cout << this->dataBuff.toStdString(); +// qDebug() << this->dataBuff; CounterDataDto * counterData = new CounterDataDto(this); if (CounterProtocolXH::checkFrame(this->dataBuff) == true) @@ -117,7 +116,7 @@ } } else if (this->dataBuff.size() > COUNTER_FRAME_MIN_LENGTH) { - std::cout << QString("%1").arg(this->dataBuff.size()).toStdString() << std::endl; + qDebug() << QString("%1").arg(this->dataBuff.size()); this->dataBuff.clear(); } @@ -193,7 +192,7 @@ } // qDebug() << counterData->rawFrame; - qDebug() << messageArray; +// qDebug() << messageArray; // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(messageArray); diff --git a/CounterRealTime/CounterSettingForm.cpp b/CounterRealTime/CounterSettingForm.cpp index c095523..225ec88 100644 --- a/CounterRealTime/CounterSettingForm.cpp +++ b/CounterRealTime/CounterSettingForm.cpp @@ -283,43 +283,68 @@ { QString deviceId = ui->selectCounter->currentData().toString(); - for (int i = 0; i < 8; i++) + QJsonArray channelSettingUpdated; + + for (int i = 0; i < 16; i++) { + QString channelCode; + double delay; + bool active; + if (i < 8) { + channelCode = clockListLeft.at(i)->text(); + delay = delayListLeft.at(i)->value(); + active = activeListLeft.at(i)->isChecked(); + } else if (i < 16) { + channelCode = clockListRight.at(i - 8)->text(); + delay = delayListRight.at(i - 8)->value(); + active = activeListRight.at(i - 8)->isChecked(); + } + CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); if (channelPrt != nullptr) { - QString channelCode = clockListLeft.at(i)->text(); // 通道钟号 - double delay = delayListLeft.at(i)->value(); // 通道时延值 + bool isUpdated = isChannelSettingUpdated(i, channelPrt); + if (isUpdated == true) { + channelPrt->setChannelCode(channelCode); + channelPrt->setDelays(QString::number(delay)); + channelPrt->setActive(active); - channelPrt->setChannelCode(channelCode); - channelPrt->setDelays(QString::number(delay)); - channelPrt->setActive(activeListLeft.at(i)->isChecked()); - } else if (activeListLeft.at(i)->isChecked() == true) { + QJsonObject channelObj; + channelObj.insert("id", channelPrt->getChannelId()); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", channelPrt->getChannelNo()); + channelObj.insert("isUse", channelPrt->getIsUse()); + channelObj.insert("name", channelPrt->getName()); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); + } + } else if (active == true) { CounterChannel * newChannel = new CounterChannel(); newChannel->setActive(true); - newChannel->setChannelCode(clockListLeft.at(i)->text()); - newChannel->setDelays(QString::number(delayListLeft.at(i)->value())); + newChannel->setChannelCode(channelCode); + newChannel->setDelays(QString::number(delay)); ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1), newChannel); + + QJsonObject channelObj; + channelObj.insert("id", ""); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", QString::number(i + 1)); + channelObj.insert("isUse", SettingConfig::getInstance().MASTER); + channelObj.insert("name", QString("钟%1-1PPS").arg(channelCode)); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); } + } - CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr) - { - QString channelCode = clockListRight.at(i)->text(); // 通道钟号 - double delay = delayListRight.at(i)->value(); // 通道时延值 - - channelRightPrt->setChannelCode(channelCode); - channelRightPrt->setDelays(QString::number(delay)); - channelRightPrt->setActive(activeListRight.at(i)->isChecked()); - } else if (activeListRight.at(i)->isChecked() == true) { - CounterChannel * newChannel = new CounterChannel(); - newChannel->setActive(true); - newChannel->setChannelCode(clockListRight.at(i)->text()); - newChannel->setDelays(QString::number(delayListRight.at(i)->value())); - - ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1+8), newChannel); - } + if (channelSettingUpdated.size() > 0) { + emit this->channelSettingSubmit(channelSettingUpdated); } this->close(); @@ -341,10 +366,10 @@ void CounterSettingForm::on_selectCounter_currentIndexChanged(int index) { - if (index == 0) + clearChannelSettingValue(); + + if (index > 0) { - clearChannelSettingValue(); - } else { QString deviceId = ui->selectCounter->currentData().toString(); for (int i = 0; i < 8; i++) @@ -353,45 +378,37 @@ activeListRight.at(i)->setCheckable(true); CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); - if (channelPrt != nullptr && channelPrt->isActive() == true) + if (channelPrt != nullptr) { QString channelCode = channelPrt->getChannelCode(); // 通道钟号 QString delays = channelPrt->getDelays(); // 通道时延值 - activeListLeft.at(i)->setChecked(true); - activeListLeft.at(i)->setText("启用"); clockListLeft.at(i)->setText(channelCode); - clockListLeft.at(i)->setReadOnly(false); delayListLeft.at(i)->setValue(delays.toDouble()); - delayListLeft.at(i)->setReadOnly(false); - } else { - activeListLeft.at(i)->setChecked(false); - activeListLeft.at(i)->setText("未启用"); - clockListLeft.at(i)->setText("-"); - clockListLeft.at(i)->setReadOnly(true); - delayListLeft.at(i)->setValue(0.000); - delayListLeft.at(i)->setReadOnly(true); + + if (channelPrt->isActive() == true) { + activeListLeft.at(i)->setChecked(true); + activeListLeft.at(i)->setText("启用"); + clockListLeft.at(i)->setReadOnly(false); + delayListLeft.at(i)->setReadOnly(false); + } } CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr && channelRightPrt->isActive() == true) + if (channelRightPrt != nullptr) { QString channelCode = channelRightPrt->getChannelCode(); // 通道钟号 QString delays = channelRightPrt->getDelays(); // 通道时延值 - activeListRight.at(i)->setChecked(true); - activeListRight.at(i)->setText("启用"); clockListRight.at(i)->setText(channelCode); - clockListRight.at(i)->setReadOnly(false); delayListRight.at(i)->setValue(delays.toDouble()); - delayListRight.at(i)->setReadOnly(false); - } else { - activeListRight.at(i)->setChecked(false); - activeListRight.at(i)->setText("未启用"); - clockListRight.at(i)->setText("-"); - clockListRight.at(i)->setReadOnly(true); - delayListRight.at(i)->setValue(0.000); - delayListRight.at(i)->setReadOnly(true); + + if (channelRightPrt->isActive() == true) { + activeListRight.at(i)->setChecked(true); + activeListRight.at(i)->setText("启用"); + clockListRight.at(i)->setReadOnly(false); + delayListRight.at(i)->setReadOnly(false); + } } } } @@ -423,3 +440,35 @@ delayListRight.at(index - 8)->setReadOnly(!enable); } } + +bool CounterSettingForm::isChannelSettingUpdated(int index, CounterChannel *channelPtr) +{ + QString channelCode = ""; // 通道钟号 + double delay = 0.0; // 通道时延值 + bool active = false; + + if (index < 8) { + channelCode = clockListLeft.at(index)->text(); // 通道钟号 + delay = delayListLeft.at(index)->value(); // 通道时延值 + active = activeListLeft.at(index)->isChecked(); + } else if (index < 16) { + index = index - 8; + channelCode = clockListRight.at(index)->text(); // 通道钟号 + delay = delayListRight.at(index)->value(); // 通道时延值 + active = activeListRight.at(index)->isChecked(); + } + + if (channelCode != channelPtr->getChannelCode()) { + return true; + } + + if (active != channelPtr->isActive()) { + return true; + } + + if (delay - channelPtr->getDelays().toDouble() != 0) { + return true; + } + + return false; +} diff --git a/CounterRealTime/CounterSettingForm.h b/CounterRealTime/CounterSettingForm.h index b4949fb..9c41362 100644 --- a/CounterRealTime/CounterSettingForm.h +++ b/CounterRealTime/CounterSettingForm.h @@ -55,10 +55,12 @@ void clearChannelSettingValue(); void enableChannel(int index, bool enable); + bool isChannelSettingUpdated(int index, CounterChannel * channelPtr); signals: void swiftDisplayLoop(); void swiftDisplayCount(); + void channelSettingSubmit(QJsonArray channelSettings); }; #endif // COUNTERSETTINGFORM_H diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index 55db2ed..86a7776 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -12,6 +12,7 @@ resize(SettingConfig::getInstance().WINDOW_WIDTH, SettingConfig::getInstance().WINDOW_HEIGHT); settingForm = new CounterSettingForm(); + connect(settingForm, &CounterSettingForm::channelSettingSubmit, this, &CounterWindowRT::onChannelSettingSubmit); QFile file(":/qss/mainClock.css"); if (file.open(QFile::ReadOnly)) { @@ -22,6 +23,14 @@ file.close(); } + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_MSG_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &CounterWindowRT::onKafkaMessageReceived); + // 绘制界面上的表格 initChannelForm(); @@ -31,7 +40,6 @@ // 重新选择显示多少个计数器 connect(settingForm, &CounterSettingForm::swiftDisplayLoop, this, &CounterWindowRT::updateDeviceWidget); - connect(settingForm, &CounterSettingForm::swiftDisplayCount, this, &CounterWindowRT::initChannelForm); httpReq = new HttpRequestController(this); @@ -47,7 +55,7 @@ // 初始化设备和通道 getDeviceList(); -// QThread::msleep(1000 * 3); + QThread::msleep(1000 * 3); for (int i = 0; i < ConstCache::getInstance().deviceIdLoopA.size(); i++) { CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(ConstCache::getInstance().deviceIdLoopA.at(i)); @@ -58,7 +66,6 @@ CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(ConstCache::getInstance().deviceIdLoopB.at(i)); devicePtr->initSerialPort(); } - } CounterWindowRT::~CounterWindowRT() @@ -77,85 +84,94 @@ } } -/** - * @brief CounterWindowRT::drawCounterDataOnPage - * @param messageArray - * 根据发送到Kafka中的数据串来绘制界面 - * - */ -void CounterWindowRT::drawCounterDataOnPage(QJsonArray messageArray) +void CounterWindowRT::onChannelSettingSubmit(QJsonArray channelSettings) { - if (messageArray.isEmpty() == false) - { - // 获取设备id属性 - QString deviceId = messageArray.at(0).toObject().find("deviceId")->toString(); - - // 如果不是当前显示的计数器 则直接返回 - CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); - if (SettingConfig::getInstance().MASTER != devicePtr->getIsUse()) - { - return; - } - - // 根据设备id 从map中取出列索引的值 - int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); - if (devIdx > SettingConfig::getInstance().DISPLAY_COUNT) - { - return; - } - - // 参考通道 - int refChNo = messageArray.at(0).toObject().find("data")->toObject().find("channelRefNo")->toInt(); - CounterChannel * refChann = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(refChNo)); - if (refChann != nullptr) - { - if (devIdx < devRefList.size()) { - devRefList.at(devIdx)->setText(QString("REF:%1").arg(refChann->getChannelCode())); - } - } - - updateChannelDataForm(messageArray); - } + httpReq->updateChannelInfo(channelSettings); } -void CounterWindowRT::updateDeviceWidget() +int CounterWindowRT::initHttpToken() { - QList devIds = SettingConfig::getInstance().MASTER == 1 ? ConstCache::getInstance().deviceIdLoopA : ConstCache::getInstance().deviceIdLoopB; - - for (int i = 0; i < SettingConfig::getInstance().DISPLAY_COUNT; i++) + QJsonObject response = httpReq->getTokenByClientId(SettingConfig::getInstance().CLIENT_ID, + SettingConfig::getInstance().APP_KEY); + return response.value("code").toInt(); +} +void CounterWindowRT::getDeviceList() +{ + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.value("code").toInt() == 200) { - if (i < devIds.size()) + // 将获取到的设备绘制到表格中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) { - CounterDevice * dev = ConstCache::getInstance().deviceMap.value(devIds.at(i)); + // 接口返回的device对象 + QJsonObject devItem = devArray.at(i).toObject(); + QString devName = devItem.value("simpleName").toString(); // 设备名称 + QString deviceId = devItem.value("deviceId").toString(); // 设备ID + QString isUse = devItem.value("isUse").toString(); // 标识A路/B路 - QString devName = dev->getDeviceName(); // 设备名称 - devTitleList.at(i)->setText(devName.right(devName.length() - 8)); - dev->setFormColIndex(i); - } else { - devTitleList.at(i)->setText(""); - devRefList.at(i)->setText(""); + // 构建Device对象 并赋值id、串口、波特率、设备编号等属性 + CounterDevice * device = new CounterDevice(this); + device->setDeviceId(deviceId); + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceName(devName); + device->setIsUse(isUse.toInt()); - for (int j = 0; j < 16; j++) + ConstCache::getInstance().deviceMap.insert(deviceId, device); + if (isUse == "1") { - channelNameList.at(i * 16 + j)->setText(""); - channelValueList.at(i * 16 + j)->setText(""); + ConstCache::getInstance().deviceIdLoopA.append(deviceId); + } else if (isUse == "0") { + ConstCache::getInstance().deviceIdLoopB.append(deviceId); } + + // 绑定显示函数 + connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); + + // 查询计数器的通道 并初始化串口 + this->getChannelList(deviceId); } + + updateDeviceWidget(); } } - -void CounterWindowRT::initChannelForm() +void CounterWindowRT::getChannelList(QString deviceId) { - // 1. 首先清除界面上的所有内容 - qDeleteAll(ui->widgetContent->children()); + // 查询计数器的通道 + QJsonObject response = httpReq->getChannelList(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 + QString channelCode = channelItem.value("channelCode").toString(); // 通道钟号 + QString channelNo = QString::number(channelItem.value("channelNo").toInt()); // 通道序号 + QString delays = channelItem.value("delays").toString(); // 通道时延值 + QString isUse = channelItem.value("isUse").toString(); + QString name = channelItem.value("name").toString(); + bool active = channelItem.value("isActive").toString().toInt(); // 通道是否启用 - // 将lab列表的数量置0 - devTitleList.resize(0); - devRefList.resize(0); - channelNameList.resize(0); - channelValueList.resize(0); + // 构建通道对象 并设置属性 + CounterChannel * channel = new CounterChannel(this); + channel->setDeviceId(deviceId); + channel->setChannelId(channelId); + channel->setChannelCode(channelCode); + channel->setChannelNo(channelNo); + channel->setDelays(delays); + channel->setActive(active); + channel->setIsUse(isUse); + channel->setName(name); - initDevAndChannelForm(SettingConfig::getInstance().DISPLAY_COUNT); + // 添加到通道的集合中 key=deviceId-channelNo + ConstCache::getInstance().channelMap.insert(deviceId + "-" + channelNo, channel); + } + } } void CounterWindowRT::initDevAndChannelForm(int count) @@ -221,7 +237,7 @@ for ( int i = 0; i < 16; i++ ) { QLabelDblClick * labChannelName = new QLabelDblClick(widgetDev); // 通道名 - QLabel * labChannelValue = new QLabel(widgetDev); // 通道测量值 + QLabelDblClick * labChannelValue = new QLabelDblClick(widgetDev); // 通道测量值 // 默认显示内容 labChannelName->setText(QString("CH%1").arg(i+1, 2, 10, QLatin1Char('0'))); @@ -247,6 +263,7 @@ // 添加通道名称的双击事件,显示数据和残差图表 connect(labChannelName, &QLabelDblClick::doubleClicked, this, &CounterWindowRT::onChannelNameLabelDblClicked); + connect(labChannelValue, &QLabelDblClick::doubleClicked, this, &CounterWindowRT::onChannelNameLabelDblClicked); channelNameList.append(labChannelName); channelValueList.append(labChannelValue); @@ -263,14 +280,13 @@ widgetDev->setLayout(layoutChannelPage); } } - void CounterWindowRT::updateChannelDataForm(QJsonArray messageArray) { // 获取设备id属性 - QString deviceId = messageArray.at(0).toObject().find("deviceId")->toString(); + QString deviceId = messageArray.at(0).toObject().value("deviceId").toString(); // 参考通道号 - int refChNo = messageArray.at(0).toObject().find("data")->toObject().find("channelRefNo")->toInt(); + int refChNo = messageArray.at(0).toObject().value("data").toObject().value("channelRefNo").toInt(); // 根据设备id 从map中取出列索引的值 int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); @@ -285,7 +301,7 @@ for (int i = 0; i < messageArray.size(); i++) { QJsonObject channelDataItem = messageArray.at(i).toObject(); - QString channelNo = QString::number(channelDataItem.find("channelNo")->toInt()); // 通道序号 + QString channelNo = QString::number(channelDataItem.value("channelNo").toInt()); // 通道序号 channelDataJsonMap.insert(channelNo, channelDataItem); } @@ -306,13 +322,13 @@ CounterChannel * channel = ConstCache::getInstance().channelMap.value(deviceId + "-" + channelNo); // 从map中找到对应的通道 // 通道Label的序号 - int chanelIdx = devIdx * 16 + channelDataItem.find("channelNo")->toInt() - 1; + int chanelIdx = devIdx * 16 + channelDataItem.value("channelNo").toInt() - 1; if (channel != nullptr && channel->isActive() == true) { // 通道时延值 并计算 减去时延值的测量值 double delay = channel->getDelays().isEmpty() == false ? channel->getDelays().toDouble() : 0.0; - double valueMinusDelay = channelDataItem.find("data")->toObject().find("dataValue")->toString().toDouble() - delay * 1E-9; + double valueMinusDelay = channelDataItem.value("data").toObject().value("dataValue").toString().toDouble() - delay * 1E-9; // 设置通道名和测量值的QLabel channelNameList.at(chanelIdx)->setText(QString("CH%1:%2").arg(channelNoStr).arg(channel->getChannelCode())); @@ -321,6 +337,10 @@ channelNameList.at(chanelIdx)->setProperty("channelId", channel->getChannelId()); channelNameList.at(chanelIdx)->setProperty("deviceId", deviceId); channelNameList.at(chanelIdx)->setProperty("channelNo", channelNo); + + channelValueList.at(chanelIdx)->setProperty("channelId", channel->getChannelId()); + channelValueList.at(chanelIdx)->setProperty("deviceId", deviceId); + channelValueList.at(chanelIdx)->setProperty("channelNo", channelNo); } else { channelNameList.at(chanelIdx)->setText(QString("CH%1:%2").arg(channelNoStr).arg("-")); channelValueList.at(chanelIdx)->setText(""); @@ -333,84 +353,11 @@ } } -int CounterWindowRT::initHttpToken() +void CounterWindowRT::updateDateAndTime() { - QJsonObject response = httpReq->getTokenByClientId(SettingConfig::getInstance().CLIENT_ID, - SettingConfig::getInstance().APP_KEY); - return response.find("code")->toInt(); -} -void CounterWindowRT::getDeviceList() -{ - QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - if (devListRes.find("code")->toInt() == 200) - { - // 将获取到的设备绘制到表格中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - // 接口返回的device对象 - QJsonObject devItem = devArray.at(i).toObject(); - QString devName = devItem.find("deviceName")->toString(); // 设备名称 - QString deviceId = devItem.find("deviceId")->toString(); // 设备ID - QString isUse = devItem.find("isUse")->toString(); // 标识A路/B路 - - // 构建Device对象 并赋值id、串口、波特率、设备编号等属性 - CounterDevice * device = new CounterDevice(this); - device->setDeviceId(deviceId); - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceName(devName); - device->setIsUse(isUse.toInt()); - - ConstCache::getInstance().deviceMap.insert(deviceId, device); - if (isUse == "1") - { - ConstCache::getInstance().deviceIdLoopA.append(deviceId); - } else if (isUse == "0") { - ConstCache::getInstance().deviceIdLoopB.append(deviceId); - } - - // 绑定显示函数 - connect(device, &CounterDevice::sendDataToDraw, - this, &CounterWindowRT::drawCounterDataOnPage); - - // 查询计数器的通道 并初始化串口 - this->getChannelList(deviceId); - } - - updateDeviceWidget(); - } -} -void CounterWindowRT::getChannelList(QString deviceId) -{ - // 查询计数器的通道 - QJsonObject response = httpReq->getChannelList(deviceId); - if (response.find("code")->toInt() == 200) - { - // 将通道绘制在界面上 - QJsonArray channelArray = response.find("data")->toArray(); - for (int i = 0; i < channelArray.size(); i++) - { - // 接口返回的channel对象 - QJsonObject channelItem = channelArray.at(i).toObject(); - QString channelId = channelItem.find("id")->toString(); // 通道ID - QString channelCode = channelItem.find("channelCode")->toString(); // 通道钟号 - QString channelNo = QString::number(channelItem.find("channelNo")->toInt()); // 通道序号 - QString delays = channelItem.find("delays")->toString(); // 通道时延值 - - // 构建通道对象 并设置属性 - CounterChannel * channel = new CounterChannel(this); - channel->setDeviceId(deviceId); - channel->setChannelId(channelId); - channel->setChannelCode(channelCode); - channel->setChannelNo(channelNo); - channel->setDelays(delays); - - // 添加到通道的集合中 key=deviceId-channelNo - ConstCache::getInstance().channelMap.insert(deviceId + "-" + channelNo, channel); - } - } + QString date = QDate::currentDate().toString("yyyy-MM-dd"); + QString time = QTime::currentTime().toString("HH:mm:ss"); + ui->labTime->setText(date + " " + time); } void CounterWindowRT::on_btnMenuSetting_clicked() @@ -425,6 +372,47 @@ setWindowState(Qt::WindowMinimized | windowState()); } +void CounterWindowRT::initChannelForm() +{ + // 1. 首先清除界面上的所有内容 + qDeleteAll(ui->widgetContent->children()); + + // 将lab列表的数量置0 + devTitleList.resize(0); + devRefList.resize(0); + channelNameList.resize(0); + channelValueList.resize(0); + + initDevAndChannelForm(SettingConfig::getInstance().DISPLAY_COUNT); +} + +void CounterWindowRT::updateDeviceWidget() +{ + QList devIds = SettingConfig::getInstance().MASTER == 1 ? ConstCache::getInstance().deviceIdLoopA : ConstCache::getInstance().deviceIdLoopB; + + for (int i = 0; i < SettingConfig::getInstance().DISPLAY_COUNT; i++) + { + if (i < devIds.size()) + { + CounterDevice * dev = ConstCache::getInstance().deviceMap.value(devIds.at(i)); + + if (dev != nullptr) { + devTitleList.at(i)->setText(dev->getDeviceName()); // 设备名称 + dev->setFormColIndex(i); + } + } else { + devTitleList.at(i)->setText(""); + devRefList.at(i)->setText(""); + + for (int j = 0; j < 16; j++) + { + channelNameList.at(i * 16 + j)->setText(""); + channelValueList.at(i * 16 + j)->setText(""); + } + } + } +} + void CounterWindowRT::onChannelNameLabelDblClicked() { QLabelDblClick * channelLab = (QLabelDblClick *) sender(); @@ -445,10 +433,50 @@ } } -void CounterWindowRT::updateDateAndTime() + +/** + * @brief CounterWindowRT::drawCounterDataOnPage + * @param messageArray + * 根据发送到Kafka中的数据串来绘制界面 + * + */ +void CounterWindowRT::drawCounterDataOnPage(QJsonArray messageArray) { - QString date = QDate::currentDate().toString("yyyy-MM-dd"); - QString time = QTime::currentTime().toString("HH:mm:ss"); - ui->labTime->setText(date + " " + time); + if (messageArray.isEmpty() == false) + { + // 获取设备id属性 + QString deviceId = messageArray.at(0).toObject().value("deviceId").toString(); + + // 如果不是当前显示的计数器 则直接返回 + CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (SettingConfig::getInstance().MASTER != devicePtr->getIsUse()) + { + return; + } + + // 根据设备id 从map中取出列索引的值 + int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); + if (devIdx > SettingConfig::getInstance().DISPLAY_COUNT) + { + return; + } + + // 参考通道 + int refChNo = messageArray.at(0).toObject().value("data").toObject().value("channelRefNo").toInt(); + CounterChannel * refChann = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(refChNo)); + if (refChann != nullptr) + { + if (devIdx < devRefList.size()) { + devRefList.at(devIdx)->setText(QString("REF:%1").arg(refChann->getChannelCode())); + } + } + + updateChannelDataForm(messageArray); + } } + +void CounterWindowRT::onKafkaMessageReceived(QJsonObject message) +{ + qDebug() << message; +} diff --git a/CounterRealTime/CounterWindowRT.h b/CounterRealTime/CounterWindowRT.h index 3f21ce7..c64e07f 100644 --- a/CounterRealTime/CounterWindowRT.h +++ b/CounterRealTime/CounterWindowRT.h @@ -18,6 +18,7 @@ #include "common/utils/SettingConfig.h" #include "common/utils/TimerCounterUtil.h" #include "common/HttpRequestController.h" +#include "common/utils/QKafkaConsumer.h" QT_BEGIN_NAMESPACE namespace Ui { class CounterWindowRT; } @@ -32,10 +33,9 @@ ~CounterWindowRT(); public slots: - void drawCounterDataOnPage(QJsonArray messageArray); + void keyPressEvent(QKeyEvent *event); - void updateDeviceWidget(); - void initChannelForm(); + void onChannelSettingSubmit(QJsonArray channelSettings); private: Ui::CounterWindowRT *ui; @@ -49,24 +49,32 @@ HttpRequestController * httpReq; - void keyPressEvent(QKeyEvent *event); + QKafkaConsumer * kafkaConsumer; int initHttpToken(); void getDeviceList(); void getChannelList(QString deviceId); void initDevAndChannelForm(int count); - void updateChannelDataForm(QJsonArray messageArray); private slots: + void updateDateAndTime(); void on_btnMenuMin_clicked(); void on_btnMenuSetting_clicked(); - void onChannelNameLabelDblClicked(); + void initChannelForm(); + void updateDeviceWidget(); - void updateDateAndTime(); + void onChannelNameLabelDblClicked(); + void drawCounterDataOnPage(QJsonArray messageArray); + + void onKafkaMessageReceived(QJsonObject message); + + + + }; #endif // COUNTERWINDOWRT_H diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 20f6177..b287ed5 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -166,7 +166,6 @@ // 获取设备列表的接口地址 QUrl url = baseUrl + "/device/list"; -// QUrl url = baseUrl + "/device/deviceList.js"; QUrlQuery query; query.addQueryItem("type", devType); url.setQuery(query); @@ -201,7 +200,6 @@ // 获取设备列表的接口地址 QUrl url = baseUrl + "/channel/list"; -// QUrl url = baseUrl + "/device/channel-" + deviceId + ".js"; QUrlQuery query; query.addQueryItem("deviceId", deviceId); url.setQuery(query); @@ -224,7 +222,41 @@ resultObj.insert("code", -1); } -// qDebug() << resultObj; + qDebug() << resultObj; + + return resultObj; +} + +QJsonObject HttpRequestController::updateChannelInfo(QJsonArray channelInfos) +{ + QJsonObject resultObj; + + // 获取设备列表的接口地址 + QUrl url = baseUrl + "/channel/qtBatchSave"; + + QNetworkRequest request; + request.setUrl(url); + request.setRawHeader("Content-type", "application/json"); + request.setRawHeader("token", token.toLocal8Bit()); + + QJsonDocument jsonDoc = QJsonDocument(channelInfos); + QByteArray postData = jsonDoc.toJson(QJsonDocument::Compact); + + qDebug() << url; + qDebug() << channelInfos; + + QNetworkReply * reply = httpUtil->sendPostRequest(request, postData); + 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/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index 5b4e4ef..860aafa 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -2,6 +2,8 @@ #define HTTPREQUESTCONTROLLER_H #include +#include +#include #include #include "utils/HttpRequestUtil.h" @@ -22,6 +24,8 @@ QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); + QJsonObject updateChannelInfo(QJsonArray channelInfos); + private: HttpRequestUtil * httpUtil; diff --git a/CounterRealTime/common/common.pri b/CounterRealTime/common/common.pri index 12bdd90..aa3f245 100644 --- a/CounterRealTime/common/common.pri +++ b/CounterRealTime/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/LinearRegression.cpp @@ -16,6 +17,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/CounterRealTime/common/utils/QKafkaConsumer.cpp b/CounterRealTime/common/utils/QKafkaConsumer.cpp new file mode 100644 index 0000000..cb4fe37 --- /dev/null +++ b/CounterRealTime/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/CounterRealTime/common/utils/QKafkaConsumer.h b/CounterRealTime/common/utils/QKafkaConsumer.h new file mode 100644 index 0000000..84f4702 --- /dev/null +++ b/CounterRealTime/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/CounterRealTime/common/utils/SettingConfig.cpp b/CounterRealTime/common/utils/SettingConfig.cpp index df08aed..ccfa7ac 100644 --- a/CounterRealTime/common/utils/SettingConfig.cpp +++ b/CounterRealTime/common/utils/SettingConfig.cpp @@ -13,6 +13,10 @@ QVariant var = this->setting->value(QString("/%1/%2").arg(nodeName).arg(keyName)); return var; } +QVariant SettingConfig::getProperty(QString nodeName, QString keyName, QString defaultValue) { + QVariant var = this->setting->value(QString("/%1/%2").arg(nodeName).arg(keyName), defaultValue); + return var; +} void SettingConfig::setProperty(QString nodeName, QString keyName, QString value) { this->setting->setValue(QString("/%1/%2").arg(nodeName).arg(keyName), value); @@ -27,6 +31,7 @@ NEED_KAFKA = getProperty("kafka", "needKafka").toInt(); KAFKA_BROKERS = getProperty("kafka", "brokers").toString(); KAFKA_DATA_TOPIC = getProperty("kafka", "dataTopic").toString(); + KAFKA_MSG_TOPIC = getProperty("kafka", "msgTopic").toString(); KAFKA_STATUS_TOPIC = getProperty("kafka", "statusTopic").toString(); NEED_SASL = getProperty("kafka", "needSasl").toInt(); SASL_USERNAME = getProperty("kafka", "saslUsername").toString(); @@ -38,7 +43,7 @@ SYSTEM = getProperty("client", "system").toString(); WORK_TYPE = getProperty("client", "workMode").toString(); MASTER = getProperty("client", "master").toInt(); - DISPLAY_COUNT = getProperty("client", "displayCount").toInt(); + DISPLAY_COUNT = getProperty("client", "displayCount", "4").toInt(); SERVER_PORT = getProperty("client", "serverPort").toInt(); BASE_URL = getProperty("http", "baseUrl").toString(); diff --git a/CounterRealTime/CounterChannel.cpp b/CounterRealTime/CounterChannel.cpp index 978b969..0a4b534 100644 --- a/CounterRealTime/CounterChannel.cpp +++ b/CounterRealTime/CounterChannel.cpp @@ -30,6 +30,14 @@ { return delays; } +QString CounterChannel::getIsUse() +{ + return isUse; +} +QString CounterChannel::getName() +{ + return name; +} void CounterChannel::setActive(bool active) { this->active = active; @@ -54,14 +62,13 @@ { this->delays = delays; } - -int CounterChannel::getFormRowIndex() +void CounterChannel::setIsUse(QString isUse) { - return formRowIndex; + this->isUse = isUse; } -void CounterChannel::setFormRowIndex(int rowIndex) +void CounterChannel::setName(QString name) { - this->formRowIndex = rowIndex; + this->name = name; } QList CounterChannel::getHisData() diff --git a/CounterRealTime/CounterChannel.h b/CounterRealTime/CounterChannel.h index afc5dc2..8f2c35e 100644 --- a/CounterRealTime/CounterChannel.h +++ b/CounterRealTime/CounterChannel.h @@ -15,15 +15,16 @@ QString getChannelCode(); QString getChannelNo(); QString getDelays(); + QString getIsUse(); + QString getName(); void setActive(bool active); void setDeviceId(QString deviceId); void setChannelId(QString channelId); void setChannelCode(QString channelCode); void setChannelNo(QString channelNo); void setDelays(QString delays); - - int getFormRowIndex(); - void setFormRowIndex(int rowIndex); + void setIsUse(QString isUse); + void setName(QString name); QList getHisData(); void appendHisData(QString timestampStr, QString valueStr); @@ -35,13 +36,12 @@ QString channelCode; QString channelNo; QString delays; + QString isUse; + QString name; int maxHisDataCount = 3600; QList hisDataList; - // 通道位于界面上的哪一行 列的位置通过计数器来确定 - int formRowIndex; - signals: }; diff --git a/CounterRealTime/CounterDevice.cpp b/CounterRealTime/CounterDevice.cpp index 63200bb..1528373 100644 --- a/CounterRealTime/CounterDevice.cpp +++ b/CounterRealTime/CounterDevice.cpp @@ -1,6 +1,5 @@ #include "CounterDevice.h" -#include #include #include @@ -95,7 +94,7 @@ void CounterDevice::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); -// std::cout << this->dataBuff.toStdString(); +// qDebug() << this->dataBuff; CounterDataDto * counterData = new CounterDataDto(this); if (CounterProtocolXH::checkFrame(this->dataBuff) == true) @@ -117,7 +116,7 @@ } } else if (this->dataBuff.size() > COUNTER_FRAME_MIN_LENGTH) { - std::cout << QString("%1").arg(this->dataBuff.size()).toStdString() << std::endl; + qDebug() << QString("%1").arg(this->dataBuff.size()); this->dataBuff.clear(); } @@ -193,7 +192,7 @@ } // qDebug() << counterData->rawFrame; - qDebug() << messageArray; +// qDebug() << messageArray; // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(messageArray); diff --git a/CounterRealTime/CounterSettingForm.cpp b/CounterRealTime/CounterSettingForm.cpp index c095523..225ec88 100644 --- a/CounterRealTime/CounterSettingForm.cpp +++ b/CounterRealTime/CounterSettingForm.cpp @@ -283,43 +283,68 @@ { QString deviceId = ui->selectCounter->currentData().toString(); - for (int i = 0; i < 8; i++) + QJsonArray channelSettingUpdated; + + for (int i = 0; i < 16; i++) { + QString channelCode; + double delay; + bool active; + if (i < 8) { + channelCode = clockListLeft.at(i)->text(); + delay = delayListLeft.at(i)->value(); + active = activeListLeft.at(i)->isChecked(); + } else if (i < 16) { + channelCode = clockListRight.at(i - 8)->text(); + delay = delayListRight.at(i - 8)->value(); + active = activeListRight.at(i - 8)->isChecked(); + } + CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); if (channelPrt != nullptr) { - QString channelCode = clockListLeft.at(i)->text(); // 通道钟号 - double delay = delayListLeft.at(i)->value(); // 通道时延值 + bool isUpdated = isChannelSettingUpdated(i, channelPrt); + if (isUpdated == true) { + channelPrt->setChannelCode(channelCode); + channelPrt->setDelays(QString::number(delay)); + channelPrt->setActive(active); - channelPrt->setChannelCode(channelCode); - channelPrt->setDelays(QString::number(delay)); - channelPrt->setActive(activeListLeft.at(i)->isChecked()); - } else if (activeListLeft.at(i)->isChecked() == true) { + QJsonObject channelObj; + channelObj.insert("id", channelPrt->getChannelId()); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", channelPrt->getChannelNo()); + channelObj.insert("isUse", channelPrt->getIsUse()); + channelObj.insert("name", channelPrt->getName()); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); + } + } else if (active == true) { CounterChannel * newChannel = new CounterChannel(); newChannel->setActive(true); - newChannel->setChannelCode(clockListLeft.at(i)->text()); - newChannel->setDelays(QString::number(delayListLeft.at(i)->value())); + newChannel->setChannelCode(channelCode); + newChannel->setDelays(QString::number(delay)); ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1), newChannel); + + QJsonObject channelObj; + channelObj.insert("id", ""); + channelObj.insert("deviceId", deviceId); + channelObj.insert("channelNo", QString::number(i + 1)); + channelObj.insert("isUse", SettingConfig::getInstance().MASTER); + channelObj.insert("name", QString("钟%1-1PPS").arg(channelCode)); + + channelObj.insert("delays", QString::number(delay)); + channelObj.insert("isActive", active == true ? "1" : "0"); + channelObj.insert("channelCode", channelCode); + channelSettingUpdated.append(channelObj); } + } - CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr) - { - QString channelCode = clockListRight.at(i)->text(); // 通道钟号 - double delay = delayListRight.at(i)->value(); // 通道时延值 - - channelRightPrt->setChannelCode(channelCode); - channelRightPrt->setDelays(QString::number(delay)); - channelRightPrt->setActive(activeListRight.at(i)->isChecked()); - } else if (activeListRight.at(i)->isChecked() == true) { - CounterChannel * newChannel = new CounterChannel(); - newChannel->setActive(true); - newChannel->setChannelCode(clockListRight.at(i)->text()); - newChannel->setDelays(QString::number(delayListRight.at(i)->value())); - - ConstCache::getInstance().channelMap.insert(QString("%1-%2").arg(deviceId).arg(i+1+8), newChannel); - } + if (channelSettingUpdated.size() > 0) { + emit this->channelSettingSubmit(channelSettingUpdated); } this->close(); @@ -341,10 +366,10 @@ void CounterSettingForm::on_selectCounter_currentIndexChanged(int index) { - if (index == 0) + clearChannelSettingValue(); + + if (index > 0) { - clearChannelSettingValue(); - } else { QString deviceId = ui->selectCounter->currentData().toString(); for (int i = 0; i < 8; i++) @@ -353,45 +378,37 @@ activeListRight.at(i)->setCheckable(true); CounterChannel * channelPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1)); - if (channelPrt != nullptr && channelPrt->isActive() == true) + if (channelPrt != nullptr) { QString channelCode = channelPrt->getChannelCode(); // 通道钟号 QString delays = channelPrt->getDelays(); // 通道时延值 - activeListLeft.at(i)->setChecked(true); - activeListLeft.at(i)->setText("启用"); clockListLeft.at(i)->setText(channelCode); - clockListLeft.at(i)->setReadOnly(false); delayListLeft.at(i)->setValue(delays.toDouble()); - delayListLeft.at(i)->setReadOnly(false); - } else { - activeListLeft.at(i)->setChecked(false); - activeListLeft.at(i)->setText("未启用"); - clockListLeft.at(i)->setText("-"); - clockListLeft.at(i)->setReadOnly(true); - delayListLeft.at(i)->setValue(0.000); - delayListLeft.at(i)->setReadOnly(true); + + if (channelPrt->isActive() == true) { + activeListLeft.at(i)->setChecked(true); + activeListLeft.at(i)->setText("启用"); + clockListLeft.at(i)->setReadOnly(false); + delayListLeft.at(i)->setReadOnly(false); + } } CounterChannel * channelRightPrt = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(i+1+8)); - if (channelRightPrt != nullptr && channelRightPrt->isActive() == true) + if (channelRightPrt != nullptr) { QString channelCode = channelRightPrt->getChannelCode(); // 通道钟号 QString delays = channelRightPrt->getDelays(); // 通道时延值 - activeListRight.at(i)->setChecked(true); - activeListRight.at(i)->setText("启用"); clockListRight.at(i)->setText(channelCode); - clockListRight.at(i)->setReadOnly(false); delayListRight.at(i)->setValue(delays.toDouble()); - delayListRight.at(i)->setReadOnly(false); - } else { - activeListRight.at(i)->setChecked(false); - activeListRight.at(i)->setText("未启用"); - clockListRight.at(i)->setText("-"); - clockListRight.at(i)->setReadOnly(true); - delayListRight.at(i)->setValue(0.000); - delayListRight.at(i)->setReadOnly(true); + + if (channelRightPrt->isActive() == true) { + activeListRight.at(i)->setChecked(true); + activeListRight.at(i)->setText("启用"); + clockListRight.at(i)->setReadOnly(false); + delayListRight.at(i)->setReadOnly(false); + } } } } @@ -423,3 +440,35 @@ delayListRight.at(index - 8)->setReadOnly(!enable); } } + +bool CounterSettingForm::isChannelSettingUpdated(int index, CounterChannel *channelPtr) +{ + QString channelCode = ""; // 通道钟号 + double delay = 0.0; // 通道时延值 + bool active = false; + + if (index < 8) { + channelCode = clockListLeft.at(index)->text(); // 通道钟号 + delay = delayListLeft.at(index)->value(); // 通道时延值 + active = activeListLeft.at(index)->isChecked(); + } else if (index < 16) { + index = index - 8; + channelCode = clockListRight.at(index)->text(); // 通道钟号 + delay = delayListRight.at(index)->value(); // 通道时延值 + active = activeListRight.at(index)->isChecked(); + } + + if (channelCode != channelPtr->getChannelCode()) { + return true; + } + + if (active != channelPtr->isActive()) { + return true; + } + + if (delay - channelPtr->getDelays().toDouble() != 0) { + return true; + } + + return false; +} diff --git a/CounterRealTime/CounterSettingForm.h b/CounterRealTime/CounterSettingForm.h index b4949fb..9c41362 100644 --- a/CounterRealTime/CounterSettingForm.h +++ b/CounterRealTime/CounterSettingForm.h @@ -55,10 +55,12 @@ void clearChannelSettingValue(); void enableChannel(int index, bool enable); + bool isChannelSettingUpdated(int index, CounterChannel * channelPtr); signals: void swiftDisplayLoop(); void swiftDisplayCount(); + void channelSettingSubmit(QJsonArray channelSettings); }; #endif // COUNTERSETTINGFORM_H diff --git a/CounterRealTime/CounterWindowRT.cpp b/CounterRealTime/CounterWindowRT.cpp index 55db2ed..86a7776 100644 --- a/CounterRealTime/CounterWindowRT.cpp +++ b/CounterRealTime/CounterWindowRT.cpp @@ -12,6 +12,7 @@ resize(SettingConfig::getInstance().WINDOW_WIDTH, SettingConfig::getInstance().WINDOW_HEIGHT); settingForm = new CounterSettingForm(); + connect(settingForm, &CounterSettingForm::channelSettingSubmit, this, &CounterWindowRT::onChannelSettingSubmit); QFile file(":/qss/mainClock.css"); if (file.open(QFile::ReadOnly)) { @@ -22,6 +23,14 @@ file.close(); } + // kafka consumer + kafkaConsumer = new QKafkaConsumer(this); + kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaConsumer->setTopic(SettingConfig::getInstance().KAFKA_MSG_TOPIC); + kafkaConsumer->createConsumer(); + kafkaConsumer->start(); + connect(kafkaConsumer, &QKafkaConsumer::messageRecieved, this, &CounterWindowRT::onKafkaMessageReceived); + // 绘制界面上的表格 initChannelForm(); @@ -31,7 +40,6 @@ // 重新选择显示多少个计数器 connect(settingForm, &CounterSettingForm::swiftDisplayLoop, this, &CounterWindowRT::updateDeviceWidget); - connect(settingForm, &CounterSettingForm::swiftDisplayCount, this, &CounterWindowRT::initChannelForm); httpReq = new HttpRequestController(this); @@ -47,7 +55,7 @@ // 初始化设备和通道 getDeviceList(); -// QThread::msleep(1000 * 3); + QThread::msleep(1000 * 3); for (int i = 0; i < ConstCache::getInstance().deviceIdLoopA.size(); i++) { CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(ConstCache::getInstance().deviceIdLoopA.at(i)); @@ -58,7 +66,6 @@ CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(ConstCache::getInstance().deviceIdLoopB.at(i)); devicePtr->initSerialPort(); } - } CounterWindowRT::~CounterWindowRT() @@ -77,85 +84,94 @@ } } -/** - * @brief CounterWindowRT::drawCounterDataOnPage - * @param messageArray - * 根据发送到Kafka中的数据串来绘制界面 - * - */ -void CounterWindowRT::drawCounterDataOnPage(QJsonArray messageArray) +void CounterWindowRT::onChannelSettingSubmit(QJsonArray channelSettings) { - if (messageArray.isEmpty() == false) - { - // 获取设备id属性 - QString deviceId = messageArray.at(0).toObject().find("deviceId")->toString(); - - // 如果不是当前显示的计数器 则直接返回 - CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); - if (SettingConfig::getInstance().MASTER != devicePtr->getIsUse()) - { - return; - } - - // 根据设备id 从map中取出列索引的值 - int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); - if (devIdx > SettingConfig::getInstance().DISPLAY_COUNT) - { - return; - } - - // 参考通道 - int refChNo = messageArray.at(0).toObject().find("data")->toObject().find("channelRefNo")->toInt(); - CounterChannel * refChann = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(refChNo)); - if (refChann != nullptr) - { - if (devIdx < devRefList.size()) { - devRefList.at(devIdx)->setText(QString("REF:%1").arg(refChann->getChannelCode())); - } - } - - updateChannelDataForm(messageArray); - } + httpReq->updateChannelInfo(channelSettings); } -void CounterWindowRT::updateDeviceWidget() +int CounterWindowRT::initHttpToken() { - QList devIds = SettingConfig::getInstance().MASTER == 1 ? ConstCache::getInstance().deviceIdLoopA : ConstCache::getInstance().deviceIdLoopB; - - for (int i = 0; i < SettingConfig::getInstance().DISPLAY_COUNT; i++) + QJsonObject response = httpReq->getTokenByClientId(SettingConfig::getInstance().CLIENT_ID, + SettingConfig::getInstance().APP_KEY); + return response.value("code").toInt(); +} +void CounterWindowRT::getDeviceList() +{ + QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); + if (devListRes.value("code").toInt() == 200) { - if (i < devIds.size()) + // 将获取到的设备绘制到表格中 + QJsonArray devArray = devListRes.value("data").toArray(); + for (int i = 0; i < devArray.size(); i++) { - CounterDevice * dev = ConstCache::getInstance().deviceMap.value(devIds.at(i)); + // 接口返回的device对象 + QJsonObject devItem = devArray.at(i).toObject(); + QString devName = devItem.value("simpleName").toString(); // 设备名称 + QString deviceId = devItem.value("deviceId").toString(); // 设备ID + QString isUse = devItem.value("isUse").toString(); // 标识A路/B路 - QString devName = dev->getDeviceName(); // 设备名称 - devTitleList.at(i)->setText(devName.right(devName.length() - 8)); - dev->setFormColIndex(i); - } else { - devTitleList.at(i)->setText(""); - devRefList.at(i)->setText(""); + // 构建Device对象 并赋值id、串口、波特率、设备编号等属性 + CounterDevice * device = new CounterDevice(this); + device->setDeviceId(deviceId); + device->setComName(devItem.value("linkComName").toString()); + device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); + device->setDevCode(devItem.value("deviceNo").toString()); + device->setDeviceName(devName); + device->setIsUse(isUse.toInt()); - for (int j = 0; j < 16; j++) + ConstCache::getInstance().deviceMap.insert(deviceId, device); + if (isUse == "1") { - channelNameList.at(i * 16 + j)->setText(""); - channelValueList.at(i * 16 + j)->setText(""); + ConstCache::getInstance().deviceIdLoopA.append(deviceId); + } else if (isUse == "0") { + ConstCache::getInstance().deviceIdLoopB.append(deviceId); } + + // 绑定显示函数 + connect(device, &CounterDevice::sendDataToDraw, this, &CounterWindowRT::drawCounterDataOnPage); + + // 查询计数器的通道 并初始化串口 + this->getChannelList(deviceId); } + + updateDeviceWidget(); } } - -void CounterWindowRT::initChannelForm() +void CounterWindowRT::getChannelList(QString deviceId) { - // 1. 首先清除界面上的所有内容 - qDeleteAll(ui->widgetContent->children()); + // 查询计数器的通道 + QJsonObject response = httpReq->getChannelList(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 + QString channelCode = channelItem.value("channelCode").toString(); // 通道钟号 + QString channelNo = QString::number(channelItem.value("channelNo").toInt()); // 通道序号 + QString delays = channelItem.value("delays").toString(); // 通道时延值 + QString isUse = channelItem.value("isUse").toString(); + QString name = channelItem.value("name").toString(); + bool active = channelItem.value("isActive").toString().toInt(); // 通道是否启用 - // 将lab列表的数量置0 - devTitleList.resize(0); - devRefList.resize(0); - channelNameList.resize(0); - channelValueList.resize(0); + // 构建通道对象 并设置属性 + CounterChannel * channel = new CounterChannel(this); + channel->setDeviceId(deviceId); + channel->setChannelId(channelId); + channel->setChannelCode(channelCode); + channel->setChannelNo(channelNo); + channel->setDelays(delays); + channel->setActive(active); + channel->setIsUse(isUse); + channel->setName(name); - initDevAndChannelForm(SettingConfig::getInstance().DISPLAY_COUNT); + // 添加到通道的集合中 key=deviceId-channelNo + ConstCache::getInstance().channelMap.insert(deviceId + "-" + channelNo, channel); + } + } } void CounterWindowRT::initDevAndChannelForm(int count) @@ -221,7 +237,7 @@ for ( int i = 0; i < 16; i++ ) { QLabelDblClick * labChannelName = new QLabelDblClick(widgetDev); // 通道名 - QLabel * labChannelValue = new QLabel(widgetDev); // 通道测量值 + QLabelDblClick * labChannelValue = new QLabelDblClick(widgetDev); // 通道测量值 // 默认显示内容 labChannelName->setText(QString("CH%1").arg(i+1, 2, 10, QLatin1Char('0'))); @@ -247,6 +263,7 @@ // 添加通道名称的双击事件,显示数据和残差图表 connect(labChannelName, &QLabelDblClick::doubleClicked, this, &CounterWindowRT::onChannelNameLabelDblClicked); + connect(labChannelValue, &QLabelDblClick::doubleClicked, this, &CounterWindowRT::onChannelNameLabelDblClicked); channelNameList.append(labChannelName); channelValueList.append(labChannelValue); @@ -263,14 +280,13 @@ widgetDev->setLayout(layoutChannelPage); } } - void CounterWindowRT::updateChannelDataForm(QJsonArray messageArray) { // 获取设备id属性 - QString deviceId = messageArray.at(0).toObject().find("deviceId")->toString(); + QString deviceId = messageArray.at(0).toObject().value("deviceId").toString(); // 参考通道号 - int refChNo = messageArray.at(0).toObject().find("data")->toObject().find("channelRefNo")->toInt(); + int refChNo = messageArray.at(0).toObject().value("data").toObject().value("channelRefNo").toInt(); // 根据设备id 从map中取出列索引的值 int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); @@ -285,7 +301,7 @@ for (int i = 0; i < messageArray.size(); i++) { QJsonObject channelDataItem = messageArray.at(i).toObject(); - QString channelNo = QString::number(channelDataItem.find("channelNo")->toInt()); // 通道序号 + QString channelNo = QString::number(channelDataItem.value("channelNo").toInt()); // 通道序号 channelDataJsonMap.insert(channelNo, channelDataItem); } @@ -306,13 +322,13 @@ CounterChannel * channel = ConstCache::getInstance().channelMap.value(deviceId + "-" + channelNo); // 从map中找到对应的通道 // 通道Label的序号 - int chanelIdx = devIdx * 16 + channelDataItem.find("channelNo")->toInt() - 1; + int chanelIdx = devIdx * 16 + channelDataItem.value("channelNo").toInt() - 1; if (channel != nullptr && channel->isActive() == true) { // 通道时延值 并计算 减去时延值的测量值 double delay = channel->getDelays().isEmpty() == false ? channel->getDelays().toDouble() : 0.0; - double valueMinusDelay = channelDataItem.find("data")->toObject().find("dataValue")->toString().toDouble() - delay * 1E-9; + double valueMinusDelay = channelDataItem.value("data").toObject().value("dataValue").toString().toDouble() - delay * 1E-9; // 设置通道名和测量值的QLabel channelNameList.at(chanelIdx)->setText(QString("CH%1:%2").arg(channelNoStr).arg(channel->getChannelCode())); @@ -321,6 +337,10 @@ channelNameList.at(chanelIdx)->setProperty("channelId", channel->getChannelId()); channelNameList.at(chanelIdx)->setProperty("deviceId", deviceId); channelNameList.at(chanelIdx)->setProperty("channelNo", channelNo); + + channelValueList.at(chanelIdx)->setProperty("channelId", channel->getChannelId()); + channelValueList.at(chanelIdx)->setProperty("deviceId", deviceId); + channelValueList.at(chanelIdx)->setProperty("channelNo", channelNo); } else { channelNameList.at(chanelIdx)->setText(QString("CH%1:%2").arg(channelNoStr).arg("-")); channelValueList.at(chanelIdx)->setText(""); @@ -333,84 +353,11 @@ } } -int CounterWindowRT::initHttpToken() +void CounterWindowRT::updateDateAndTime() { - QJsonObject response = httpReq->getTokenByClientId(SettingConfig::getInstance().CLIENT_ID, - SettingConfig::getInstance().APP_KEY); - return response.find("code")->toInt(); -} -void CounterWindowRT::getDeviceList() -{ - QJsonObject devListRes = httpReq->initDeviceList(SettingConfig::getInstance().DEV_TYPES, SettingConfig::getInstance().SYSTEM); - if (devListRes.find("code")->toInt() == 200) - { - // 将获取到的设备绘制到表格中 - QJsonArray devArray = devListRes.find("data")->toArray(); - for (int i = 0; i < devArray.size(); i++) - { - // 接口返回的device对象 - QJsonObject devItem = devArray.at(i).toObject(); - QString devName = devItem.find("deviceName")->toString(); // 设备名称 - QString deviceId = devItem.find("deviceId")->toString(); // 设备ID - QString isUse = devItem.find("isUse")->toString(); // 标识A路/B路 - - // 构建Device对象 并赋值id、串口、波特率、设备编号等属性 - CounterDevice * device = new CounterDevice(this); - device->setDeviceId(deviceId); - device->setComName(devItem.find("linkComName")->toString()); - device->setBaudRate(SettingConfig::getInstance().BAUD_RATE); - device->setDevCode(devItem.find("deviceNo")->toString()); - device->setDeviceName(devName); - device->setIsUse(isUse.toInt()); - - ConstCache::getInstance().deviceMap.insert(deviceId, device); - if (isUse == "1") - { - ConstCache::getInstance().deviceIdLoopA.append(deviceId); - } else if (isUse == "0") { - ConstCache::getInstance().deviceIdLoopB.append(deviceId); - } - - // 绑定显示函数 - connect(device, &CounterDevice::sendDataToDraw, - this, &CounterWindowRT::drawCounterDataOnPage); - - // 查询计数器的通道 并初始化串口 - this->getChannelList(deviceId); - } - - updateDeviceWidget(); - } -} -void CounterWindowRT::getChannelList(QString deviceId) -{ - // 查询计数器的通道 - QJsonObject response = httpReq->getChannelList(deviceId); - if (response.find("code")->toInt() == 200) - { - // 将通道绘制在界面上 - QJsonArray channelArray = response.find("data")->toArray(); - for (int i = 0; i < channelArray.size(); i++) - { - // 接口返回的channel对象 - QJsonObject channelItem = channelArray.at(i).toObject(); - QString channelId = channelItem.find("id")->toString(); // 通道ID - QString channelCode = channelItem.find("channelCode")->toString(); // 通道钟号 - QString channelNo = QString::number(channelItem.find("channelNo")->toInt()); // 通道序号 - QString delays = channelItem.find("delays")->toString(); // 通道时延值 - - // 构建通道对象 并设置属性 - CounterChannel * channel = new CounterChannel(this); - channel->setDeviceId(deviceId); - channel->setChannelId(channelId); - channel->setChannelCode(channelCode); - channel->setChannelNo(channelNo); - channel->setDelays(delays); - - // 添加到通道的集合中 key=deviceId-channelNo - ConstCache::getInstance().channelMap.insert(deviceId + "-" + channelNo, channel); - } - } + QString date = QDate::currentDate().toString("yyyy-MM-dd"); + QString time = QTime::currentTime().toString("HH:mm:ss"); + ui->labTime->setText(date + " " + time); } void CounterWindowRT::on_btnMenuSetting_clicked() @@ -425,6 +372,47 @@ setWindowState(Qt::WindowMinimized | windowState()); } +void CounterWindowRT::initChannelForm() +{ + // 1. 首先清除界面上的所有内容 + qDeleteAll(ui->widgetContent->children()); + + // 将lab列表的数量置0 + devTitleList.resize(0); + devRefList.resize(0); + channelNameList.resize(0); + channelValueList.resize(0); + + initDevAndChannelForm(SettingConfig::getInstance().DISPLAY_COUNT); +} + +void CounterWindowRT::updateDeviceWidget() +{ + QList devIds = SettingConfig::getInstance().MASTER == 1 ? ConstCache::getInstance().deviceIdLoopA : ConstCache::getInstance().deviceIdLoopB; + + for (int i = 0; i < SettingConfig::getInstance().DISPLAY_COUNT; i++) + { + if (i < devIds.size()) + { + CounterDevice * dev = ConstCache::getInstance().deviceMap.value(devIds.at(i)); + + if (dev != nullptr) { + devTitleList.at(i)->setText(dev->getDeviceName()); // 设备名称 + dev->setFormColIndex(i); + } + } else { + devTitleList.at(i)->setText(""); + devRefList.at(i)->setText(""); + + for (int j = 0; j < 16; j++) + { + channelNameList.at(i * 16 + j)->setText(""); + channelValueList.at(i * 16 + j)->setText(""); + } + } + } +} + void CounterWindowRT::onChannelNameLabelDblClicked() { QLabelDblClick * channelLab = (QLabelDblClick *) sender(); @@ -445,10 +433,50 @@ } } -void CounterWindowRT::updateDateAndTime() + +/** + * @brief CounterWindowRT::drawCounterDataOnPage + * @param messageArray + * 根据发送到Kafka中的数据串来绘制界面 + * + */ +void CounterWindowRT::drawCounterDataOnPage(QJsonArray messageArray) { - QString date = QDate::currentDate().toString("yyyy-MM-dd"); - QString time = QTime::currentTime().toString("HH:mm:ss"); - ui->labTime->setText(date + " " + time); + if (messageArray.isEmpty() == false) + { + // 获取设备id属性 + QString deviceId = messageArray.at(0).toObject().value("deviceId").toString(); + + // 如果不是当前显示的计数器 则直接返回 + CounterDevice * devicePtr = ConstCache::getInstance().deviceMap.value(deviceId); + if (SettingConfig::getInstance().MASTER != devicePtr->getIsUse()) + { + return; + } + + // 根据设备id 从map中取出列索引的值 + int devIdx = ConstCache::getInstance().deviceMap.value(deviceId)->getFormColIndex(); + if (devIdx > SettingConfig::getInstance().DISPLAY_COUNT) + { + return; + } + + // 参考通道 + int refChNo = messageArray.at(0).toObject().value("data").toObject().value("channelRefNo").toInt(); + CounterChannel * refChann = ConstCache::getInstance().channelMap.value(QString("%1-%2").arg(deviceId).arg(refChNo)); + if (refChann != nullptr) + { + if (devIdx < devRefList.size()) { + devRefList.at(devIdx)->setText(QString("REF:%1").arg(refChann->getChannelCode())); + } + } + + updateChannelDataForm(messageArray); + } } + +void CounterWindowRT::onKafkaMessageReceived(QJsonObject message) +{ + qDebug() << message; +} diff --git a/CounterRealTime/CounterWindowRT.h b/CounterRealTime/CounterWindowRT.h index 3f21ce7..c64e07f 100644 --- a/CounterRealTime/CounterWindowRT.h +++ b/CounterRealTime/CounterWindowRT.h @@ -18,6 +18,7 @@ #include "common/utils/SettingConfig.h" #include "common/utils/TimerCounterUtil.h" #include "common/HttpRequestController.h" +#include "common/utils/QKafkaConsumer.h" QT_BEGIN_NAMESPACE namespace Ui { class CounterWindowRT; } @@ -32,10 +33,9 @@ ~CounterWindowRT(); public slots: - void drawCounterDataOnPage(QJsonArray messageArray); + void keyPressEvent(QKeyEvent *event); - void updateDeviceWidget(); - void initChannelForm(); + void onChannelSettingSubmit(QJsonArray channelSettings); private: Ui::CounterWindowRT *ui; @@ -49,24 +49,32 @@ HttpRequestController * httpReq; - void keyPressEvent(QKeyEvent *event); + QKafkaConsumer * kafkaConsumer; int initHttpToken(); void getDeviceList(); void getChannelList(QString deviceId); void initDevAndChannelForm(int count); - void updateChannelDataForm(QJsonArray messageArray); private slots: + void updateDateAndTime(); void on_btnMenuMin_clicked(); void on_btnMenuSetting_clicked(); - void onChannelNameLabelDblClicked(); + void initChannelForm(); + void updateDeviceWidget(); - void updateDateAndTime(); + void onChannelNameLabelDblClicked(); + void drawCounterDataOnPage(QJsonArray messageArray); + + void onKafkaMessageReceived(QJsonObject message); + + + + }; #endif // COUNTERWINDOWRT_H diff --git a/CounterRealTime/common/HttpRequestController.cpp b/CounterRealTime/common/HttpRequestController.cpp index 20f6177..b287ed5 100644 --- a/CounterRealTime/common/HttpRequestController.cpp +++ b/CounterRealTime/common/HttpRequestController.cpp @@ -166,7 +166,6 @@ // 获取设备列表的接口地址 QUrl url = baseUrl + "/device/list"; -// QUrl url = baseUrl + "/device/deviceList.js"; QUrlQuery query; query.addQueryItem("type", devType); url.setQuery(query); @@ -201,7 +200,6 @@ // 获取设备列表的接口地址 QUrl url = baseUrl + "/channel/list"; -// QUrl url = baseUrl + "/device/channel-" + deviceId + ".js"; QUrlQuery query; query.addQueryItem("deviceId", deviceId); url.setQuery(query); @@ -224,7 +222,41 @@ resultObj.insert("code", -1); } -// qDebug() << resultObj; + qDebug() << resultObj; + + return resultObj; +} + +QJsonObject HttpRequestController::updateChannelInfo(QJsonArray channelInfos) +{ + QJsonObject resultObj; + + // 获取设备列表的接口地址 + QUrl url = baseUrl + "/channel/qtBatchSave"; + + QNetworkRequest request; + request.setUrl(url); + request.setRawHeader("Content-type", "application/json"); + request.setRawHeader("token", token.toLocal8Bit()); + + QJsonDocument jsonDoc = QJsonDocument(channelInfos); + QByteArray postData = jsonDoc.toJson(QJsonDocument::Compact); + + qDebug() << url; + qDebug() << channelInfos; + + QNetworkReply * reply = httpUtil->sendPostRequest(request, postData); + 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/CounterRealTime/common/HttpRequestController.h b/CounterRealTime/common/HttpRequestController.h index 5b4e4ef..860aafa 100644 --- a/CounterRealTime/common/HttpRequestController.h +++ b/CounterRealTime/common/HttpRequestController.h @@ -2,6 +2,8 @@ #define HTTPREQUESTCONTROLLER_H #include +#include +#include #include #include "utils/HttpRequestUtil.h" @@ -22,6 +24,8 @@ QJsonObject initDeviceList(QString devType, QString system); QJsonObject getChannelList(QString deviceId); + QJsonObject updateChannelInfo(QJsonArray channelInfos); + private: HttpRequestUtil * httpUtil; diff --git a/CounterRealTime/common/common.pri b/CounterRealTime/common/common.pri index 12bdd90..aa3f245 100644 --- a/CounterRealTime/common/common.pri +++ b/CounterRealTime/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/LinearRegression.cpp @@ -16,6 +17,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/CounterRealTime/common/utils/QKafkaConsumer.cpp b/CounterRealTime/common/utils/QKafkaConsumer.cpp new file mode 100644 index 0000000..cb4fe37 --- /dev/null +++ b/CounterRealTime/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/CounterRealTime/common/utils/QKafkaConsumer.h b/CounterRealTime/common/utils/QKafkaConsumer.h new file mode 100644 index 0000000..84f4702 --- /dev/null +++ b/CounterRealTime/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/CounterRealTime/common/utils/SettingConfig.cpp b/CounterRealTime/common/utils/SettingConfig.cpp index df08aed..ccfa7ac 100644 --- a/CounterRealTime/common/utils/SettingConfig.cpp +++ b/CounterRealTime/common/utils/SettingConfig.cpp @@ -13,6 +13,10 @@ QVariant var = this->setting->value(QString("/%1/%2").arg(nodeName).arg(keyName)); return var; } +QVariant SettingConfig::getProperty(QString nodeName, QString keyName, QString defaultValue) { + QVariant var = this->setting->value(QString("/%1/%2").arg(nodeName).arg(keyName), defaultValue); + return var; +} void SettingConfig::setProperty(QString nodeName, QString keyName, QString value) { this->setting->setValue(QString("/%1/%2").arg(nodeName).arg(keyName), value); @@ -27,6 +31,7 @@ NEED_KAFKA = getProperty("kafka", "needKafka").toInt(); KAFKA_BROKERS = getProperty("kafka", "brokers").toString(); KAFKA_DATA_TOPIC = getProperty("kafka", "dataTopic").toString(); + KAFKA_MSG_TOPIC = getProperty("kafka", "msgTopic").toString(); KAFKA_STATUS_TOPIC = getProperty("kafka", "statusTopic").toString(); NEED_SASL = getProperty("kafka", "needSasl").toInt(); SASL_USERNAME = getProperty("kafka", "saslUsername").toString(); @@ -38,7 +43,7 @@ SYSTEM = getProperty("client", "system").toString(); WORK_TYPE = getProperty("client", "workMode").toString(); MASTER = getProperty("client", "master").toInt(); - DISPLAY_COUNT = getProperty("client", "displayCount").toInt(); + DISPLAY_COUNT = getProperty("client", "displayCount", "4").toInt(); SERVER_PORT = getProperty("client", "serverPort").toInt(); BASE_URL = getProperty("http", "baseUrl").toString(); diff --git a/CounterRealTime/common/utils/SettingConfig.h b/CounterRealTime/common/utils/SettingConfig.h index 0ed91ea..93f40ce 100644 --- a/CounterRealTime/common/utils/SettingConfig.h +++ b/CounterRealTime/common/utils/SettingConfig.h @@ -25,6 +25,7 @@ * @title */ QVariant getProperty(QString nodeName, QString keyName); + QVariant getProperty(QString nodeName, QString keyName, QString defaultValue); void setProperty(QString nodeName, QString keyName, QString value); void init(); @@ -37,6 +38,7 @@ qint8 NEED_KAFKA; QString KAFKA_BROKERS; QString KAFKA_DATA_TOPIC; + QString KAFKA_MSG_TOPIC; QString KAFKA_STATUS_TOPIC; qint8 NEED_SASL; QString SASL_USERNAME;