diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index a3729d2..133c113 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -10,6 +10,7 @@ #include #include #include +#include CounterWindow::CounterWindow(QWidget *parent) : QWidget(parent), @@ -17,6 +18,9 @@ { ui->setupUi(this); + QRect screenRect = QApplication::desktop()->screenGeometry(); + resize(screenRect.width(), 700); + QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index a3729d2..133c113 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -10,6 +10,7 @@ #include #include #include +#include CounterWindow::CounterWindow(QWidget *parent) : QWidget(parent), @@ -17,6 +18,9 @@ { ui->setupUi(this); + QRect screenRect = QApplication::desktop()->screenGeometry(); + resize(screenRect.width(), 700); + QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 3a75d94..5c84cd5 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -6,6 +6,10 @@ { connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &PhaseDevice::dataReceivedHandler); + + kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaUtil.createProducer(); } PhaseDevice::~PhaseDevice() @@ -56,6 +60,8 @@ { this->dataBuff.append(data); + std::cout << QByteUtil::binToHexString(this->dataBuff).toStdString() << std::endl; + PhaseDataDto * phaseData = new PhaseDataDto(this); if (PhaseProtocolBM::checkFrame(this->dataBuff) == true) { @@ -112,11 +118,18 @@ QString channelDataStr = QString("%1 %2 %3").arg(phaseData->timestamp).arg(phaseData->channelData.at(i-1)).arg(phaseData->frameId); QLogUtil::writeChannelDataLog(chFilename, channelDataStr); + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + QJsonObject jsonObj = phaseData->toJSON(i - 1); + jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); + jsonObj.insert("deviceId", devCode); + kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + } } } - // 3. 输出到中间件,执行后续处理过程 - // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(phaseData); } diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index a3729d2..133c113 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -10,6 +10,7 @@ #include #include #include +#include CounterWindow::CounterWindow(QWidget *parent) : QWidget(parent), @@ -17,6 +18,9 @@ { ui->setupUi(this); + QRect screenRect = QApplication::desktop()->screenGeometry(); + resize(screenRect.width(), 700); + QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 3a75d94..5c84cd5 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -6,6 +6,10 @@ { connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &PhaseDevice::dataReceivedHandler); + + kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaUtil.createProducer(); } PhaseDevice::~PhaseDevice() @@ -56,6 +60,8 @@ { this->dataBuff.append(data); + std::cout << QByteUtil::binToHexString(this->dataBuff).toStdString() << std::endl; + PhaseDataDto * phaseData = new PhaseDataDto(this); if (PhaseProtocolBM::checkFrame(this->dataBuff) == true) { @@ -112,11 +118,18 @@ QString channelDataStr = QString("%1 %2 %3").arg(phaseData->timestamp).arg(phaseData->channelData.at(i-1)).arg(phaseData->frameId); QLogUtil::writeChannelDataLog(chFilename, channelDataStr); + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + QJsonObject jsonObj = phaseData->toJSON(i - 1); + jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); + jsonObj.insert("deviceId", devCode); + kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + } } } - // 3. 输出到中间件,执行后续处理过程 - // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(phaseData); } diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 630a0e7..92182d4 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -4,6 +4,8 @@ #include #include "common/utils/QSerialPortUtil.h" #include "common/utils/QLogUtil.h" +#include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaUtil.h" #include "protocol/PhaseProtocolBM.h" class PhaseDevice : public QObject @@ -31,6 +33,7 @@ int baudRate; QSerialPortUtil serialUtil; + QKafkaUtil kafkaUtil; QByteArray dataBuff; signals: diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index a3729d2..133c113 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -10,6 +10,7 @@ #include #include #include +#include CounterWindow::CounterWindow(QWidget *parent) : QWidget(parent), @@ -17,6 +18,9 @@ { ui->setupUi(this); + QRect screenRect = QApplication::desktop()->screenGeometry(); + resize(screenRect.width(), 700); + QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 3a75d94..5c84cd5 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -6,6 +6,10 @@ { connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &PhaseDevice::dataReceivedHandler); + + kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaUtil.createProducer(); } PhaseDevice::~PhaseDevice() @@ -56,6 +60,8 @@ { this->dataBuff.append(data); + std::cout << QByteUtil::binToHexString(this->dataBuff).toStdString() << std::endl; + PhaseDataDto * phaseData = new PhaseDataDto(this); if (PhaseProtocolBM::checkFrame(this->dataBuff) == true) { @@ -112,11 +118,18 @@ QString channelDataStr = QString("%1 %2 %3").arg(phaseData->timestamp).arg(phaseData->channelData.at(i-1)).arg(phaseData->frameId); QLogUtil::writeChannelDataLog(chFilename, channelDataStr); + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + QJsonObject jsonObj = phaseData->toJSON(i - 1); + jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); + jsonObj.insert("deviceId", devCode); + kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + } } } - // 3. 输出到中间件,执行后续处理过程 - // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(phaseData); } diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 630a0e7..92182d4 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -4,6 +4,8 @@ #include #include "common/utils/QSerialPortUtil.h" #include "common/utils/QLogUtil.h" +#include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaUtil.h" #include "protocol/PhaseProtocolBM.h" class PhaseDevice : public QObject @@ -31,6 +33,7 @@ int baudRate; QSerialPortUtil serialUtil; + QKafkaUtil kafkaUtil; QByteArray dataBuff; signals: diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 459dcaf..b3cab89 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -18,12 +18,6 @@ { ui->setupUi(this); -// QKafkaUtil * kafkaTest = new QKafkaUtil(this); -// kafkaTest->setBrokers("111.198.10.15:12502"); -// kafkaTest->setTopic("cppTest"); -// int kaf = kafkaTest->initKafkaConf(); -// std::cout << "init kafka cppTest: " << kaf << std::endl; - QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; @@ -61,8 +55,6 @@ // 创建设备的widget this->generateWidgetForDevice(devCodeList.at(i), i); } - - this->createKafkaMessage(); } PhaseWindow::~PhaseWindow() @@ -75,7 +67,7 @@ // 1. 判断数据属于哪个设备,显示在不同的widget上 // 2. 循环设置各个tableView - QList devChannels = this->tableModelList.at(2); // 暂时写死 + QList devChannels = this->tableModelList.at(0); // 暂时写死 for (int i = 0; i < phaseData->channelData.size(); i++) { if (phaseData->channelActive.at(i).toInt() == 1) diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index a3729d2..133c113 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -10,6 +10,7 @@ #include #include #include +#include CounterWindow::CounterWindow(QWidget *parent) : QWidget(parent), @@ -17,6 +18,9 @@ { ui->setupUi(this); + QRect screenRect = QApplication::desktop()->screenGeometry(); + resize(screenRect.width(), 700); + QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 3a75d94..5c84cd5 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -6,6 +6,10 @@ { connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &PhaseDevice::dataReceivedHandler); + + kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaUtil.createProducer(); } PhaseDevice::~PhaseDevice() @@ -56,6 +60,8 @@ { this->dataBuff.append(data); + std::cout << QByteUtil::binToHexString(this->dataBuff).toStdString() << std::endl; + PhaseDataDto * phaseData = new PhaseDataDto(this); if (PhaseProtocolBM::checkFrame(this->dataBuff) == true) { @@ -112,11 +118,18 @@ QString channelDataStr = QString("%1 %2 %3").arg(phaseData->timestamp).arg(phaseData->channelData.at(i-1)).arg(phaseData->frameId); QLogUtil::writeChannelDataLog(chFilename, channelDataStr); + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + QJsonObject jsonObj = phaseData->toJSON(i - 1); + jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); + jsonObj.insert("deviceId", devCode); + kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + } } } - // 3. 输出到中间件,执行后续处理过程 - // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(phaseData); } diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 630a0e7..92182d4 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -4,6 +4,8 @@ #include #include "common/utils/QSerialPortUtil.h" #include "common/utils/QLogUtil.h" +#include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaUtil.h" #include "protocol/PhaseProtocolBM.h" class PhaseDevice : public QObject @@ -31,6 +33,7 @@ int baudRate; QSerialPortUtil serialUtil; + QKafkaUtil kafkaUtil; QByteArray dataBuff; signals: diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 459dcaf..b3cab89 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -18,12 +18,6 @@ { ui->setupUi(this); -// QKafkaUtil * kafkaTest = new QKafkaUtil(this); -// kafkaTest->setBrokers("111.198.10.15:12502"); -// kafkaTest->setTopic("cppTest"); -// int kaf = kafkaTest->initKafkaConf(); -// std::cout << "init kafka cppTest: " << kaf << std::endl; - QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; @@ -61,8 +55,6 @@ // 创建设备的widget this->generateWidgetForDevice(devCodeList.at(i), i); } - - this->createKafkaMessage(); } PhaseWindow::~PhaseWindow() @@ -75,7 +67,7 @@ // 1. 判断数据属于哪个设备,显示在不同的widget上 // 2. 循环设置各个tableView - QList devChannels = this->tableModelList.at(2); // 暂时写死 + QList devChannels = this->tableModelList.at(0); // 暂时写死 for (int i = 0; i < phaseData->channelData.size(); i++) { if (phaseData->channelActive.at(i).toInt() == 1) diff --git a/PhaseCompAcq/common/utils/QByteUtil.cpp b/PhaseCompAcq/common/utils/QByteUtil.cpp index b26c69b..1d49a51 100644 --- a/PhaseCompAcq/common/utils/QByteUtil.cpp +++ b/PhaseCompAcq/common/utils/QByteUtil.cpp @@ -58,6 +58,19 @@ return value; } +QByteArray QByteUtil::ULongToBytes(qulonglong value, qint8 length) +{ + QByteArray ba; + + for (int i = 0; i < length; i++) + { + ba.prepend(value % 256); + value = value / 256; + } + + return ba; +} + float QByteUtil::binToFloat(QByteArray bytes) { diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index a3729d2..133c113 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -10,6 +10,7 @@ #include #include #include +#include CounterWindow::CounterWindow(QWidget *parent) : QWidget(parent), @@ -17,6 +18,9 @@ { ui->setupUi(this); + QRect screenRect = QApplication::desktop()->screenGeometry(); + resize(screenRect.width(), 700); + QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 3a75d94..5c84cd5 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -6,6 +6,10 @@ { connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &PhaseDevice::dataReceivedHandler); + + kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaUtil.createProducer(); } PhaseDevice::~PhaseDevice() @@ -56,6 +60,8 @@ { this->dataBuff.append(data); + std::cout << QByteUtil::binToHexString(this->dataBuff).toStdString() << std::endl; + PhaseDataDto * phaseData = new PhaseDataDto(this); if (PhaseProtocolBM::checkFrame(this->dataBuff) == true) { @@ -112,11 +118,18 @@ QString channelDataStr = QString("%1 %2 %3").arg(phaseData->timestamp).arg(phaseData->channelData.at(i-1)).arg(phaseData->frameId); QLogUtil::writeChannelDataLog(chFilename, channelDataStr); + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + QJsonObject jsonObj = phaseData->toJSON(i - 1); + jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); + jsonObj.insert("deviceId", devCode); + kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + } } } - // 3. 输出到中间件,执行后续处理过程 - // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(phaseData); } diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 630a0e7..92182d4 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -4,6 +4,8 @@ #include #include "common/utils/QSerialPortUtil.h" #include "common/utils/QLogUtil.h" +#include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaUtil.h" #include "protocol/PhaseProtocolBM.h" class PhaseDevice : public QObject @@ -31,6 +33,7 @@ int baudRate; QSerialPortUtil serialUtil; + QKafkaUtil kafkaUtil; QByteArray dataBuff; signals: diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 459dcaf..b3cab89 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -18,12 +18,6 @@ { ui->setupUi(this); -// QKafkaUtil * kafkaTest = new QKafkaUtil(this); -// kafkaTest->setBrokers("111.198.10.15:12502"); -// kafkaTest->setTopic("cppTest"); -// int kaf = kafkaTest->initKafkaConf(); -// std::cout << "init kafka cppTest: " << kaf << std::endl; - QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; @@ -61,8 +55,6 @@ // 创建设备的widget this->generateWidgetForDevice(devCodeList.at(i), i); } - - this->createKafkaMessage(); } PhaseWindow::~PhaseWindow() @@ -75,7 +67,7 @@ // 1. 判断数据属于哪个设备,显示在不同的widget上 // 2. 循环设置各个tableView - QList devChannels = this->tableModelList.at(2); // 暂时写死 + QList devChannels = this->tableModelList.at(0); // 暂时写死 for (int i = 0; i < phaseData->channelData.size(); i++) { if (phaseData->channelActive.at(i).toInt() == 1) diff --git a/PhaseCompAcq/common/utils/QByteUtil.cpp b/PhaseCompAcq/common/utils/QByteUtil.cpp index b26c69b..1d49a51 100644 --- a/PhaseCompAcq/common/utils/QByteUtil.cpp +++ b/PhaseCompAcq/common/utils/QByteUtil.cpp @@ -58,6 +58,19 @@ return value; } +QByteArray QByteUtil::ULongToBytes(qulonglong value, qint8 length) +{ + QByteArray ba; + + for (int i = 0; i < length; i++) + { + ba.prepend(value % 256); + value = value / 256; + } + + return ba; +} + float QByteUtil::binToFloat(QByteArray bytes) { diff --git a/PhaseCompAcq/common/utils/QByteUtil.h b/PhaseCompAcq/common/utils/QByteUtil.h index 78fe0dd..f02d2f9 100644 --- a/PhaseCompAcq/common/utils/QByteUtil.h +++ b/PhaseCompAcq/common/utils/QByteUtil.h @@ -35,7 +35,7 @@ * @return */ static qulonglong binToULong(QByteArray bytes, quint8 length); - static QByteArray ULongToBytes(qulonglong value); + static QByteArray ULongToBytes(qulonglong value, qint8 length); /******** 字节数组与float互转 ********/ /** diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index a3729d2..133c113 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -10,6 +10,7 @@ #include #include #include +#include CounterWindow::CounterWindow(QWidget *parent) : QWidget(parent), @@ -17,6 +18,9 @@ { ui->setupUi(this); + QRect screenRect = QApplication::desktop()->screenGeometry(); + resize(screenRect.width(), 700); + QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 3a75d94..5c84cd5 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -6,6 +6,10 @@ { connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &PhaseDevice::dataReceivedHandler); + + kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaUtil.createProducer(); } PhaseDevice::~PhaseDevice() @@ -56,6 +60,8 @@ { this->dataBuff.append(data); + std::cout << QByteUtil::binToHexString(this->dataBuff).toStdString() << std::endl; + PhaseDataDto * phaseData = new PhaseDataDto(this); if (PhaseProtocolBM::checkFrame(this->dataBuff) == true) { @@ -112,11 +118,18 @@ QString channelDataStr = QString("%1 %2 %3").arg(phaseData->timestamp).arg(phaseData->channelData.at(i-1)).arg(phaseData->frameId); QLogUtil::writeChannelDataLog(chFilename, channelDataStr); + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + QJsonObject jsonObj = phaseData->toJSON(i - 1); + jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); + jsonObj.insert("deviceId", devCode); + kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + } } } - // 3. 输出到中间件,执行后续处理过程 - // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(phaseData); } diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 630a0e7..92182d4 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -4,6 +4,8 @@ #include #include "common/utils/QSerialPortUtil.h" #include "common/utils/QLogUtil.h" +#include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaUtil.h" #include "protocol/PhaseProtocolBM.h" class PhaseDevice : public QObject @@ -31,6 +33,7 @@ int baudRate; QSerialPortUtil serialUtil; + QKafkaUtil kafkaUtil; QByteArray dataBuff; signals: diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 459dcaf..b3cab89 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -18,12 +18,6 @@ { ui->setupUi(this); -// QKafkaUtil * kafkaTest = new QKafkaUtil(this); -// kafkaTest->setBrokers("111.198.10.15:12502"); -// kafkaTest->setTopic("cppTest"); -// int kaf = kafkaTest->initKafkaConf(); -// std::cout << "init kafka cppTest: " << kaf << std::endl; - QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; @@ -61,8 +55,6 @@ // 创建设备的widget this->generateWidgetForDevice(devCodeList.at(i), i); } - - this->createKafkaMessage(); } PhaseWindow::~PhaseWindow() @@ -75,7 +67,7 @@ // 1. 判断数据属于哪个设备,显示在不同的widget上 // 2. 循环设置各个tableView - QList devChannels = this->tableModelList.at(2); // 暂时写死 + QList devChannels = this->tableModelList.at(0); // 暂时写死 for (int i = 0; i < phaseData->channelData.size(); i++) { if (phaseData->channelActive.at(i).toInt() == 1) diff --git a/PhaseCompAcq/common/utils/QByteUtil.cpp b/PhaseCompAcq/common/utils/QByteUtil.cpp index b26c69b..1d49a51 100644 --- a/PhaseCompAcq/common/utils/QByteUtil.cpp +++ b/PhaseCompAcq/common/utils/QByteUtil.cpp @@ -58,6 +58,19 @@ return value; } +QByteArray QByteUtil::ULongToBytes(qulonglong value, qint8 length) +{ + QByteArray ba; + + for (int i = 0; i < length; i++) + { + ba.prepend(value % 256); + value = value / 256; + } + + return ba; +} + float QByteUtil::binToFloat(QByteArray bytes) { diff --git a/PhaseCompAcq/common/utils/QByteUtil.h b/PhaseCompAcq/common/utils/QByteUtil.h index 78fe0dd..f02d2f9 100644 --- a/PhaseCompAcq/common/utils/QByteUtil.h +++ b/PhaseCompAcq/common/utils/QByteUtil.h @@ -35,7 +35,7 @@ * @return */ static qulonglong binToULong(QByteArray bytes, quint8 length); - static QByteArray ULongToBytes(qulonglong value); + static QByteArray ULongToBytes(qulonglong value, qint8 length); /******** 字节数组与float互转 ********/ /** diff --git a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp index 2d54019..ba0ffb5 100644 --- a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp +++ b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp @@ -1,6 +1,9 @@ #include "QSerialPortUtil.h" #include +#include +#include +#include "common/utils/QByteUtil.h" QSerialPortUtil::QSerialPortUtil(QObject *parent) : QObject(parent) { @@ -18,12 +21,18 @@ open = serial.open(QIODevice::ReadWrite); - if (open == true) - { +// if (open == true) +// { // 绑定信号与槽 connect(&serial, &QSerialPort::readyRead, this, &QSerialPortUtil::readData); - } + + // mock data received per second + QTimer * timer = new QTimer(this); + connect(timer, &QTimer::timeout, + this, &QSerialPortUtil::mockReceivData); + timer->start(1000); +// } } void QSerialPortUtil::sendData(QByteArray data) @@ -46,3 +55,23 @@ { return this->open; } + +void QSerialPortUtil::mockReceivData() +{ + QDateTime now = QDateTime::currentDateTime(); + + QString frameId = now.toString("HHmmsszzz"); + + QByteArray buffer; + buffer.append(QByteUtil::hexStringToBytes(frameId.mid(0, 8))); + + for (int i = 1; i <= 16; i++) + { + int value = qrand() % 10000; + buffer.append(QByteUtil::hexStringToBytes("00800000")).append(QByteUtil::ULongToBytes(value, 4)); + } + + buffer.append(QByteUtil::hexStringToBytes("EEEEEEEE")); + + emit dataRecieved(buffer); +} diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index a3729d2..133c113 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -10,6 +10,7 @@ #include #include #include +#include CounterWindow::CounterWindow(QWidget *parent) : QWidget(parent), @@ -17,6 +18,9 @@ { ui->setupUi(this); + QRect screenRect = QApplication::desktop()->screenGeometry(); + resize(screenRect.width(), 700); + QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 3a75d94..5c84cd5 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -6,6 +6,10 @@ { connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &PhaseDevice::dataReceivedHandler); + + kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaUtil.createProducer(); } PhaseDevice::~PhaseDevice() @@ -56,6 +60,8 @@ { this->dataBuff.append(data); + std::cout << QByteUtil::binToHexString(this->dataBuff).toStdString() << std::endl; + PhaseDataDto * phaseData = new PhaseDataDto(this); if (PhaseProtocolBM::checkFrame(this->dataBuff) == true) { @@ -112,11 +118,18 @@ QString channelDataStr = QString("%1 %2 %3").arg(phaseData->timestamp).arg(phaseData->channelData.at(i-1)).arg(phaseData->frameId); QLogUtil::writeChannelDataLog(chFilename, channelDataStr); + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + QJsonObject jsonObj = phaseData->toJSON(i - 1); + jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); + jsonObj.insert("deviceId", devCode); + kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + } } } - // 3. 输出到中间件,执行后续处理过程 - // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(phaseData); } diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 630a0e7..92182d4 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -4,6 +4,8 @@ #include #include "common/utils/QSerialPortUtil.h" #include "common/utils/QLogUtil.h" +#include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaUtil.h" #include "protocol/PhaseProtocolBM.h" class PhaseDevice : public QObject @@ -31,6 +33,7 @@ int baudRate; QSerialPortUtil serialUtil; + QKafkaUtil kafkaUtil; QByteArray dataBuff; signals: diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 459dcaf..b3cab89 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -18,12 +18,6 @@ { ui->setupUi(this); -// QKafkaUtil * kafkaTest = new QKafkaUtil(this); -// kafkaTest->setBrokers("111.198.10.15:12502"); -// kafkaTest->setTopic("cppTest"); -// int kaf = kafkaTest->initKafkaConf(); -// std::cout << "init kafka cppTest: " << kaf << std::endl; - QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; @@ -61,8 +55,6 @@ // 创建设备的widget this->generateWidgetForDevice(devCodeList.at(i), i); } - - this->createKafkaMessage(); } PhaseWindow::~PhaseWindow() @@ -75,7 +67,7 @@ // 1. 判断数据属于哪个设备,显示在不同的widget上 // 2. 循环设置各个tableView - QList devChannels = this->tableModelList.at(2); // 暂时写死 + QList devChannels = this->tableModelList.at(0); // 暂时写死 for (int i = 0; i < phaseData->channelData.size(); i++) { if (phaseData->channelActive.at(i).toInt() == 1) diff --git a/PhaseCompAcq/common/utils/QByteUtil.cpp b/PhaseCompAcq/common/utils/QByteUtil.cpp index b26c69b..1d49a51 100644 --- a/PhaseCompAcq/common/utils/QByteUtil.cpp +++ b/PhaseCompAcq/common/utils/QByteUtil.cpp @@ -58,6 +58,19 @@ return value; } +QByteArray QByteUtil::ULongToBytes(qulonglong value, qint8 length) +{ + QByteArray ba; + + for (int i = 0; i < length; i++) + { + ba.prepend(value % 256); + value = value / 256; + } + + return ba; +} + float QByteUtil::binToFloat(QByteArray bytes) { diff --git a/PhaseCompAcq/common/utils/QByteUtil.h b/PhaseCompAcq/common/utils/QByteUtil.h index 78fe0dd..f02d2f9 100644 --- a/PhaseCompAcq/common/utils/QByteUtil.h +++ b/PhaseCompAcq/common/utils/QByteUtil.h @@ -35,7 +35,7 @@ * @return */ static qulonglong binToULong(QByteArray bytes, quint8 length); - static QByteArray ULongToBytes(qulonglong value); + static QByteArray ULongToBytes(qulonglong value, qint8 length); /******** 字节数组与float互转 ********/ /** diff --git a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp index 2d54019..ba0ffb5 100644 --- a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp +++ b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp @@ -1,6 +1,9 @@ #include "QSerialPortUtil.h" #include +#include +#include +#include "common/utils/QByteUtil.h" QSerialPortUtil::QSerialPortUtil(QObject *parent) : QObject(parent) { @@ -18,12 +21,18 @@ open = serial.open(QIODevice::ReadWrite); - if (open == true) - { +// if (open == true) +// { // 绑定信号与槽 connect(&serial, &QSerialPort::readyRead, this, &QSerialPortUtil::readData); - } + + // mock data received per second + QTimer * timer = new QTimer(this); + connect(timer, &QTimer::timeout, + this, &QSerialPortUtil::mockReceivData); + timer->start(1000); +// } } void QSerialPortUtil::sendData(QByteArray data) @@ -46,3 +55,23 @@ { return this->open; } + +void QSerialPortUtil::mockReceivData() +{ + QDateTime now = QDateTime::currentDateTime(); + + QString frameId = now.toString("HHmmsszzz"); + + QByteArray buffer; + buffer.append(QByteUtil::hexStringToBytes(frameId.mid(0, 8))); + + for (int i = 1; i <= 16; i++) + { + int value = qrand() % 10000; + buffer.append(QByteUtil::hexStringToBytes("00800000")).append(QByteUtil::ULongToBytes(value, 4)); + } + + buffer.append(QByteUtil::hexStringToBytes("EEEEEEEE")); + + emit dataRecieved(buffer); +} diff --git a/PhaseCompAcq/common/utils/QSerialPortUtil.h b/PhaseCompAcq/common/utils/QSerialPortUtil.h index 1a7f62f..accf403 100644 --- a/PhaseCompAcq/common/utils/QSerialPortUtil.h +++ b/PhaseCompAcq/common/utils/QSerialPortUtil.h @@ -21,6 +21,8 @@ bool open; + void mockReceivData(); + signals: void dataRecieved(QByteArray data); // 收到数据的信号 }; diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index a3729d2..133c113 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -10,6 +10,7 @@ #include #include #include +#include CounterWindow::CounterWindow(QWidget *parent) : QWidget(parent), @@ -17,6 +18,9 @@ { ui->setupUi(this); + QRect screenRect = QApplication::desktop()->screenGeometry(); + resize(screenRect.width(), 700); + QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 3a75d94..5c84cd5 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -6,6 +6,10 @@ { connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &PhaseDevice::dataReceivedHandler); + + kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaUtil.createProducer(); } PhaseDevice::~PhaseDevice() @@ -56,6 +60,8 @@ { this->dataBuff.append(data); + std::cout << QByteUtil::binToHexString(this->dataBuff).toStdString() << std::endl; + PhaseDataDto * phaseData = new PhaseDataDto(this); if (PhaseProtocolBM::checkFrame(this->dataBuff) == true) { @@ -112,11 +118,18 @@ QString channelDataStr = QString("%1 %2 %3").arg(phaseData->timestamp).arg(phaseData->channelData.at(i-1)).arg(phaseData->frameId); QLogUtil::writeChannelDataLog(chFilename, channelDataStr); + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + QJsonObject jsonObj = phaseData->toJSON(i - 1); + jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); + jsonObj.insert("deviceId", devCode); + kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + } } } - // 3. 输出到中间件,执行后续处理过程 - // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(phaseData); } diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 630a0e7..92182d4 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -4,6 +4,8 @@ #include #include "common/utils/QSerialPortUtil.h" #include "common/utils/QLogUtil.h" +#include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaUtil.h" #include "protocol/PhaseProtocolBM.h" class PhaseDevice : public QObject @@ -31,6 +33,7 @@ int baudRate; QSerialPortUtil serialUtil; + QKafkaUtil kafkaUtil; QByteArray dataBuff; signals: diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 459dcaf..b3cab89 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -18,12 +18,6 @@ { ui->setupUi(this); -// QKafkaUtil * kafkaTest = new QKafkaUtil(this); -// kafkaTest->setBrokers("111.198.10.15:12502"); -// kafkaTest->setTopic("cppTest"); -// int kaf = kafkaTest->initKafkaConf(); -// std::cout << "init kafka cppTest: " << kaf << std::endl; - QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; @@ -61,8 +55,6 @@ // 创建设备的widget this->generateWidgetForDevice(devCodeList.at(i), i); } - - this->createKafkaMessage(); } PhaseWindow::~PhaseWindow() @@ -75,7 +67,7 @@ // 1. 判断数据属于哪个设备,显示在不同的widget上 // 2. 循环设置各个tableView - QList devChannels = this->tableModelList.at(2); // 暂时写死 + QList devChannels = this->tableModelList.at(0); // 暂时写死 for (int i = 0; i < phaseData->channelData.size(); i++) { if (phaseData->channelActive.at(i).toInt() == 1) diff --git a/PhaseCompAcq/common/utils/QByteUtil.cpp b/PhaseCompAcq/common/utils/QByteUtil.cpp index b26c69b..1d49a51 100644 --- a/PhaseCompAcq/common/utils/QByteUtil.cpp +++ b/PhaseCompAcq/common/utils/QByteUtil.cpp @@ -58,6 +58,19 @@ return value; } +QByteArray QByteUtil::ULongToBytes(qulonglong value, qint8 length) +{ + QByteArray ba; + + for (int i = 0; i < length; i++) + { + ba.prepend(value % 256); + value = value / 256; + } + + return ba; +} + float QByteUtil::binToFloat(QByteArray bytes) { diff --git a/PhaseCompAcq/common/utils/QByteUtil.h b/PhaseCompAcq/common/utils/QByteUtil.h index 78fe0dd..f02d2f9 100644 --- a/PhaseCompAcq/common/utils/QByteUtil.h +++ b/PhaseCompAcq/common/utils/QByteUtil.h @@ -35,7 +35,7 @@ * @return */ static qulonglong binToULong(QByteArray bytes, quint8 length); - static QByteArray ULongToBytes(qulonglong value); + static QByteArray ULongToBytes(qulonglong value, qint8 length); /******** 字节数组与float互转 ********/ /** diff --git a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp index 2d54019..ba0ffb5 100644 --- a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp +++ b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp @@ -1,6 +1,9 @@ #include "QSerialPortUtil.h" #include +#include +#include +#include "common/utils/QByteUtil.h" QSerialPortUtil::QSerialPortUtil(QObject *parent) : QObject(parent) { @@ -18,12 +21,18 @@ open = serial.open(QIODevice::ReadWrite); - if (open == true) - { +// if (open == true) +// { // 绑定信号与槽 connect(&serial, &QSerialPort::readyRead, this, &QSerialPortUtil::readData); - } + + // mock data received per second + QTimer * timer = new QTimer(this); + connect(timer, &QTimer::timeout, + this, &QSerialPortUtil::mockReceivData); + timer->start(1000); +// } } void QSerialPortUtil::sendData(QByteArray data) @@ -46,3 +55,23 @@ { return this->open; } + +void QSerialPortUtil::mockReceivData() +{ + QDateTime now = QDateTime::currentDateTime(); + + QString frameId = now.toString("HHmmsszzz"); + + QByteArray buffer; + buffer.append(QByteUtil::hexStringToBytes(frameId.mid(0, 8))); + + for (int i = 1; i <= 16; i++) + { + int value = qrand() % 10000; + buffer.append(QByteUtil::hexStringToBytes("00800000")).append(QByteUtil::ULongToBytes(value, 4)); + } + + buffer.append(QByteUtil::hexStringToBytes("EEEEEEEE")); + + emit dataRecieved(buffer); +} diff --git a/PhaseCompAcq/common/utils/QSerialPortUtil.h b/PhaseCompAcq/common/utils/QSerialPortUtil.h index 1a7f62f..accf403 100644 --- a/PhaseCompAcq/common/utils/QSerialPortUtil.h +++ b/PhaseCompAcq/common/utils/QSerialPortUtil.h @@ -21,6 +21,8 @@ bool open; + void mockReceivData(); + signals: void dataRecieved(QByteArray data); // 收到数据的信号 }; diff --git a/PhaseCompAcq/common/utils/SettingConfig.cpp b/PhaseCompAcq/common/utils/SettingConfig.cpp index c664a21..67a8dd0 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.cpp +++ b/PhaseCompAcq/common/utils/SettingConfig.cpp @@ -9,8 +9,11 @@ BAUD_RATE = getProperty("com", "baudRate").toUInt(); DEV_CODES = getProperty("com", "devCodes").toString(); + NEED_KAFKA = getProperty("kafka", "needKafka").toUInt(); KAFKA_BROKERS = getProperty("kafka", "brokers").toString(); KAFKA_DATA_TOPIC = getProperty("kafka", "dataTopic").toString(); + + CLIENT_ID = getProperty("client", "clientId").toString(); } diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index a3729d2..133c113 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -10,6 +10,7 @@ #include #include #include +#include CounterWindow::CounterWindow(QWidget *parent) : QWidget(parent), @@ -17,6 +18,9 @@ { ui->setupUi(this); + QRect screenRect = QApplication::desktop()->screenGeometry(); + resize(screenRect.width(), 700); + QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 3a75d94..5c84cd5 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -6,6 +6,10 @@ { connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &PhaseDevice::dataReceivedHandler); + + kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaUtil.createProducer(); } PhaseDevice::~PhaseDevice() @@ -56,6 +60,8 @@ { this->dataBuff.append(data); + std::cout << QByteUtil::binToHexString(this->dataBuff).toStdString() << std::endl; + PhaseDataDto * phaseData = new PhaseDataDto(this); if (PhaseProtocolBM::checkFrame(this->dataBuff) == true) { @@ -112,11 +118,18 @@ QString channelDataStr = QString("%1 %2 %3").arg(phaseData->timestamp).arg(phaseData->channelData.at(i-1)).arg(phaseData->frameId); QLogUtil::writeChannelDataLog(chFilename, channelDataStr); + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + QJsonObject jsonObj = phaseData->toJSON(i - 1); + jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); + jsonObj.insert("deviceId", devCode); + kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + } } } - // 3. 输出到中间件,执行后续处理过程 - // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(phaseData); } diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 630a0e7..92182d4 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -4,6 +4,8 @@ #include #include "common/utils/QSerialPortUtil.h" #include "common/utils/QLogUtil.h" +#include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaUtil.h" #include "protocol/PhaseProtocolBM.h" class PhaseDevice : public QObject @@ -31,6 +33,7 @@ int baudRate; QSerialPortUtil serialUtil; + QKafkaUtil kafkaUtil; QByteArray dataBuff; signals: diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 459dcaf..b3cab89 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -18,12 +18,6 @@ { ui->setupUi(this); -// QKafkaUtil * kafkaTest = new QKafkaUtil(this); -// kafkaTest->setBrokers("111.198.10.15:12502"); -// kafkaTest->setTopic("cppTest"); -// int kaf = kafkaTest->initKafkaConf(); -// std::cout << "init kafka cppTest: " << kaf << std::endl; - QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; @@ -61,8 +55,6 @@ // 创建设备的widget this->generateWidgetForDevice(devCodeList.at(i), i); } - - this->createKafkaMessage(); } PhaseWindow::~PhaseWindow() @@ -75,7 +67,7 @@ // 1. 判断数据属于哪个设备,显示在不同的widget上 // 2. 循环设置各个tableView - QList devChannels = this->tableModelList.at(2); // 暂时写死 + QList devChannels = this->tableModelList.at(0); // 暂时写死 for (int i = 0; i < phaseData->channelData.size(); i++) { if (phaseData->channelActive.at(i).toInt() == 1) diff --git a/PhaseCompAcq/common/utils/QByteUtil.cpp b/PhaseCompAcq/common/utils/QByteUtil.cpp index b26c69b..1d49a51 100644 --- a/PhaseCompAcq/common/utils/QByteUtil.cpp +++ b/PhaseCompAcq/common/utils/QByteUtil.cpp @@ -58,6 +58,19 @@ return value; } +QByteArray QByteUtil::ULongToBytes(qulonglong value, qint8 length) +{ + QByteArray ba; + + for (int i = 0; i < length; i++) + { + ba.prepend(value % 256); + value = value / 256; + } + + return ba; +} + float QByteUtil::binToFloat(QByteArray bytes) { diff --git a/PhaseCompAcq/common/utils/QByteUtil.h b/PhaseCompAcq/common/utils/QByteUtil.h index 78fe0dd..f02d2f9 100644 --- a/PhaseCompAcq/common/utils/QByteUtil.h +++ b/PhaseCompAcq/common/utils/QByteUtil.h @@ -35,7 +35,7 @@ * @return */ static qulonglong binToULong(QByteArray bytes, quint8 length); - static QByteArray ULongToBytes(qulonglong value); + static QByteArray ULongToBytes(qulonglong value, qint8 length); /******** 字节数组与float互转 ********/ /** diff --git a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp index 2d54019..ba0ffb5 100644 --- a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp +++ b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp @@ -1,6 +1,9 @@ #include "QSerialPortUtil.h" #include +#include +#include +#include "common/utils/QByteUtil.h" QSerialPortUtil::QSerialPortUtil(QObject *parent) : QObject(parent) { @@ -18,12 +21,18 @@ open = serial.open(QIODevice::ReadWrite); - if (open == true) - { +// if (open == true) +// { // 绑定信号与槽 connect(&serial, &QSerialPort::readyRead, this, &QSerialPortUtil::readData); - } + + // mock data received per second + QTimer * timer = new QTimer(this); + connect(timer, &QTimer::timeout, + this, &QSerialPortUtil::mockReceivData); + timer->start(1000); +// } } void QSerialPortUtil::sendData(QByteArray data) @@ -46,3 +55,23 @@ { return this->open; } + +void QSerialPortUtil::mockReceivData() +{ + QDateTime now = QDateTime::currentDateTime(); + + QString frameId = now.toString("HHmmsszzz"); + + QByteArray buffer; + buffer.append(QByteUtil::hexStringToBytes(frameId.mid(0, 8))); + + for (int i = 1; i <= 16; i++) + { + int value = qrand() % 10000; + buffer.append(QByteUtil::hexStringToBytes("00800000")).append(QByteUtil::ULongToBytes(value, 4)); + } + + buffer.append(QByteUtil::hexStringToBytes("EEEEEEEE")); + + emit dataRecieved(buffer); +} diff --git a/PhaseCompAcq/common/utils/QSerialPortUtil.h b/PhaseCompAcq/common/utils/QSerialPortUtil.h index 1a7f62f..accf403 100644 --- a/PhaseCompAcq/common/utils/QSerialPortUtil.h +++ b/PhaseCompAcq/common/utils/QSerialPortUtil.h @@ -21,6 +21,8 @@ bool open; + void mockReceivData(); + signals: void dataRecieved(QByteArray data); // 收到数据的信号 }; diff --git a/PhaseCompAcq/common/utils/SettingConfig.cpp b/PhaseCompAcq/common/utils/SettingConfig.cpp index c664a21..67a8dd0 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.cpp +++ b/PhaseCompAcq/common/utils/SettingConfig.cpp @@ -9,8 +9,11 @@ BAUD_RATE = getProperty("com", "baudRate").toUInt(); DEV_CODES = getProperty("com", "devCodes").toString(); + NEED_KAFKA = getProperty("kafka", "needKafka").toUInt(); KAFKA_BROKERS = getProperty("kafka", "brokers").toString(); KAFKA_DATA_TOPIC = getProperty("kafka", "dataTopic").toString(); + + CLIENT_ID = getProperty("client", "clientId").toString(); } diff --git a/PhaseCompAcq/common/utils/SettingConfig.h b/PhaseCompAcq/common/utils/SettingConfig.h index 08ab277..2d10a4d 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.h +++ b/PhaseCompAcq/common/utils/SettingConfig.h @@ -31,9 +31,12 @@ int BAUD_RATE; QString DEV_CODES; + int NEED_KAFKA; QString KAFKA_BROKERS; QString KAFKA_DATA_TOPIC; + QString CLIENT_ID; + private: SettingConfig(); diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index a3729d2..133c113 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -10,6 +10,7 @@ #include #include #include +#include CounterWindow::CounterWindow(QWidget *parent) : QWidget(parent), @@ -17,6 +18,9 @@ { ui->setupUi(this); + QRect screenRect = QApplication::desktop()->screenGeometry(); + resize(screenRect.width(), 700); + QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 3a75d94..5c84cd5 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -6,6 +6,10 @@ { connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &PhaseDevice::dataReceivedHandler); + + kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaUtil.createProducer(); } PhaseDevice::~PhaseDevice() @@ -56,6 +60,8 @@ { this->dataBuff.append(data); + std::cout << QByteUtil::binToHexString(this->dataBuff).toStdString() << std::endl; + PhaseDataDto * phaseData = new PhaseDataDto(this); if (PhaseProtocolBM::checkFrame(this->dataBuff) == true) { @@ -112,11 +118,18 @@ QString channelDataStr = QString("%1 %2 %3").arg(phaseData->timestamp).arg(phaseData->channelData.at(i-1)).arg(phaseData->frameId); QLogUtil::writeChannelDataLog(chFilename, channelDataStr); + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + QJsonObject jsonObj = phaseData->toJSON(i - 1); + jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); + jsonObj.insert("deviceId", devCode); + kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + } } } - // 3. 输出到中间件,执行后续处理过程 - // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(phaseData); } diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 630a0e7..92182d4 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -4,6 +4,8 @@ #include #include "common/utils/QSerialPortUtil.h" #include "common/utils/QLogUtil.h" +#include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaUtil.h" #include "protocol/PhaseProtocolBM.h" class PhaseDevice : public QObject @@ -31,6 +33,7 @@ int baudRate; QSerialPortUtil serialUtil; + QKafkaUtil kafkaUtil; QByteArray dataBuff; signals: diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 459dcaf..b3cab89 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -18,12 +18,6 @@ { ui->setupUi(this); -// QKafkaUtil * kafkaTest = new QKafkaUtil(this); -// kafkaTest->setBrokers("111.198.10.15:12502"); -// kafkaTest->setTopic("cppTest"); -// int kaf = kafkaTest->initKafkaConf(); -// std::cout << "init kafka cppTest: " << kaf << std::endl; - QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; @@ -61,8 +55,6 @@ // 创建设备的widget this->generateWidgetForDevice(devCodeList.at(i), i); } - - this->createKafkaMessage(); } PhaseWindow::~PhaseWindow() @@ -75,7 +67,7 @@ // 1. 判断数据属于哪个设备,显示在不同的widget上 // 2. 循环设置各个tableView - QList devChannels = this->tableModelList.at(2); // 暂时写死 + QList devChannels = this->tableModelList.at(0); // 暂时写死 for (int i = 0; i < phaseData->channelData.size(); i++) { if (phaseData->channelActive.at(i).toInt() == 1) diff --git a/PhaseCompAcq/common/utils/QByteUtil.cpp b/PhaseCompAcq/common/utils/QByteUtil.cpp index b26c69b..1d49a51 100644 --- a/PhaseCompAcq/common/utils/QByteUtil.cpp +++ b/PhaseCompAcq/common/utils/QByteUtil.cpp @@ -58,6 +58,19 @@ return value; } +QByteArray QByteUtil::ULongToBytes(qulonglong value, qint8 length) +{ + QByteArray ba; + + for (int i = 0; i < length; i++) + { + ba.prepend(value % 256); + value = value / 256; + } + + return ba; +} + float QByteUtil::binToFloat(QByteArray bytes) { diff --git a/PhaseCompAcq/common/utils/QByteUtil.h b/PhaseCompAcq/common/utils/QByteUtil.h index 78fe0dd..f02d2f9 100644 --- a/PhaseCompAcq/common/utils/QByteUtil.h +++ b/PhaseCompAcq/common/utils/QByteUtil.h @@ -35,7 +35,7 @@ * @return */ static qulonglong binToULong(QByteArray bytes, quint8 length); - static QByteArray ULongToBytes(qulonglong value); + static QByteArray ULongToBytes(qulonglong value, qint8 length); /******** 字节数组与float互转 ********/ /** diff --git a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp index 2d54019..ba0ffb5 100644 --- a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp +++ b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp @@ -1,6 +1,9 @@ #include "QSerialPortUtil.h" #include +#include +#include +#include "common/utils/QByteUtil.h" QSerialPortUtil::QSerialPortUtil(QObject *parent) : QObject(parent) { @@ -18,12 +21,18 @@ open = serial.open(QIODevice::ReadWrite); - if (open == true) - { +// if (open == true) +// { // 绑定信号与槽 connect(&serial, &QSerialPort::readyRead, this, &QSerialPortUtil::readData); - } + + // mock data received per second + QTimer * timer = new QTimer(this); + connect(timer, &QTimer::timeout, + this, &QSerialPortUtil::mockReceivData); + timer->start(1000); +// } } void QSerialPortUtil::sendData(QByteArray data) @@ -46,3 +55,23 @@ { return this->open; } + +void QSerialPortUtil::mockReceivData() +{ + QDateTime now = QDateTime::currentDateTime(); + + QString frameId = now.toString("HHmmsszzz"); + + QByteArray buffer; + buffer.append(QByteUtil::hexStringToBytes(frameId.mid(0, 8))); + + for (int i = 1; i <= 16; i++) + { + int value = qrand() % 10000; + buffer.append(QByteUtil::hexStringToBytes("00800000")).append(QByteUtil::ULongToBytes(value, 4)); + } + + buffer.append(QByteUtil::hexStringToBytes("EEEEEEEE")); + + emit dataRecieved(buffer); +} diff --git a/PhaseCompAcq/common/utils/QSerialPortUtil.h b/PhaseCompAcq/common/utils/QSerialPortUtil.h index 1a7f62f..accf403 100644 --- a/PhaseCompAcq/common/utils/QSerialPortUtil.h +++ b/PhaseCompAcq/common/utils/QSerialPortUtil.h @@ -21,6 +21,8 @@ bool open; + void mockReceivData(); + signals: void dataRecieved(QByteArray data); // 收到数据的信号 }; diff --git a/PhaseCompAcq/common/utils/SettingConfig.cpp b/PhaseCompAcq/common/utils/SettingConfig.cpp index c664a21..67a8dd0 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.cpp +++ b/PhaseCompAcq/common/utils/SettingConfig.cpp @@ -9,8 +9,11 @@ BAUD_RATE = getProperty("com", "baudRate").toUInt(); DEV_CODES = getProperty("com", "devCodes").toString(); + NEED_KAFKA = getProperty("kafka", "needKafka").toUInt(); KAFKA_BROKERS = getProperty("kafka", "brokers").toString(); KAFKA_DATA_TOPIC = getProperty("kafka", "dataTopic").toString(); + + CLIENT_ID = getProperty("client", "clientId").toString(); } diff --git a/PhaseCompAcq/common/utils/SettingConfig.h b/PhaseCompAcq/common/utils/SettingConfig.h index 08ab277..2d10a4d 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.h +++ b/PhaseCompAcq/common/utils/SettingConfig.h @@ -31,9 +31,12 @@ int BAUD_RATE; QString DEV_CODES; + int NEED_KAFKA; QString KAFKA_BROKERS; QString KAFKA_DATA_TOPIC; + QString CLIENT_ID; + private: SettingConfig(); diff --git a/PhaseCompAcq/conf/config.ini b/PhaseCompAcq/conf/config.ini index 631f014..ed326b2 100644 --- a/PhaseCompAcq/conf/config.ini +++ b/PhaseCompAcq/conf/config.ini @@ -1,8 +1,12 @@ [com] -portNames="COM1,COM2,COM3" -devCodes="9101,9102,9103" +portNames="COM3" +devCodes="9103" baudRate=115200 [kafka] +needKafka=1 brokers="111.198.10.15:12502" dataTopic="cppTest" + +[client] +clientId="112233445566" diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index a3729d2..133c113 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -10,6 +10,7 @@ #include #include #include +#include CounterWindow::CounterWindow(QWidget *parent) : QWidget(parent), @@ -17,6 +18,9 @@ { ui->setupUi(this); + QRect screenRect = QApplication::desktop()->screenGeometry(); + resize(screenRect.width(), 700); + QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 3a75d94..5c84cd5 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -6,6 +6,10 @@ { connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &PhaseDevice::dataReceivedHandler); + + kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaUtil.createProducer(); } PhaseDevice::~PhaseDevice() @@ -56,6 +60,8 @@ { this->dataBuff.append(data); + std::cout << QByteUtil::binToHexString(this->dataBuff).toStdString() << std::endl; + PhaseDataDto * phaseData = new PhaseDataDto(this); if (PhaseProtocolBM::checkFrame(this->dataBuff) == true) { @@ -112,11 +118,18 @@ QString channelDataStr = QString("%1 %2 %3").arg(phaseData->timestamp).arg(phaseData->channelData.at(i-1)).arg(phaseData->frameId); QLogUtil::writeChannelDataLog(chFilename, channelDataStr); + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + QJsonObject jsonObj = phaseData->toJSON(i - 1); + jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); + jsonObj.insert("deviceId", devCode); + kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + } } } - // 3. 输出到中间件,执行后续处理过程 - // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(phaseData); } diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 630a0e7..92182d4 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -4,6 +4,8 @@ #include #include "common/utils/QSerialPortUtil.h" #include "common/utils/QLogUtil.h" +#include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaUtil.h" #include "protocol/PhaseProtocolBM.h" class PhaseDevice : public QObject @@ -31,6 +33,7 @@ int baudRate; QSerialPortUtil serialUtil; + QKafkaUtil kafkaUtil; QByteArray dataBuff; signals: diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 459dcaf..b3cab89 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -18,12 +18,6 @@ { ui->setupUi(this); -// QKafkaUtil * kafkaTest = new QKafkaUtil(this); -// kafkaTest->setBrokers("111.198.10.15:12502"); -// kafkaTest->setTopic("cppTest"); -// int kaf = kafkaTest->initKafkaConf(); -// std::cout << "init kafka cppTest: " << kaf << std::endl; - QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; @@ -61,8 +55,6 @@ // 创建设备的widget this->generateWidgetForDevice(devCodeList.at(i), i); } - - this->createKafkaMessage(); } PhaseWindow::~PhaseWindow() @@ -75,7 +67,7 @@ // 1. 判断数据属于哪个设备,显示在不同的widget上 // 2. 循环设置各个tableView - QList devChannels = this->tableModelList.at(2); // 暂时写死 + QList devChannels = this->tableModelList.at(0); // 暂时写死 for (int i = 0; i < phaseData->channelData.size(); i++) { if (phaseData->channelActive.at(i).toInt() == 1) diff --git a/PhaseCompAcq/common/utils/QByteUtil.cpp b/PhaseCompAcq/common/utils/QByteUtil.cpp index b26c69b..1d49a51 100644 --- a/PhaseCompAcq/common/utils/QByteUtil.cpp +++ b/PhaseCompAcq/common/utils/QByteUtil.cpp @@ -58,6 +58,19 @@ return value; } +QByteArray QByteUtil::ULongToBytes(qulonglong value, qint8 length) +{ + QByteArray ba; + + for (int i = 0; i < length; i++) + { + ba.prepend(value % 256); + value = value / 256; + } + + return ba; +} + float QByteUtil::binToFloat(QByteArray bytes) { diff --git a/PhaseCompAcq/common/utils/QByteUtil.h b/PhaseCompAcq/common/utils/QByteUtil.h index 78fe0dd..f02d2f9 100644 --- a/PhaseCompAcq/common/utils/QByteUtil.h +++ b/PhaseCompAcq/common/utils/QByteUtil.h @@ -35,7 +35,7 @@ * @return */ static qulonglong binToULong(QByteArray bytes, quint8 length); - static QByteArray ULongToBytes(qulonglong value); + static QByteArray ULongToBytes(qulonglong value, qint8 length); /******** 字节数组与float互转 ********/ /** diff --git a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp index 2d54019..ba0ffb5 100644 --- a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp +++ b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp @@ -1,6 +1,9 @@ #include "QSerialPortUtil.h" #include +#include +#include +#include "common/utils/QByteUtil.h" QSerialPortUtil::QSerialPortUtil(QObject *parent) : QObject(parent) { @@ -18,12 +21,18 @@ open = serial.open(QIODevice::ReadWrite); - if (open == true) - { +// if (open == true) +// { // 绑定信号与槽 connect(&serial, &QSerialPort::readyRead, this, &QSerialPortUtil::readData); - } + + // mock data received per second + QTimer * timer = new QTimer(this); + connect(timer, &QTimer::timeout, + this, &QSerialPortUtil::mockReceivData); + timer->start(1000); +// } } void QSerialPortUtil::sendData(QByteArray data) @@ -46,3 +55,23 @@ { return this->open; } + +void QSerialPortUtil::mockReceivData() +{ + QDateTime now = QDateTime::currentDateTime(); + + QString frameId = now.toString("HHmmsszzz"); + + QByteArray buffer; + buffer.append(QByteUtil::hexStringToBytes(frameId.mid(0, 8))); + + for (int i = 1; i <= 16; i++) + { + int value = qrand() % 10000; + buffer.append(QByteUtil::hexStringToBytes("00800000")).append(QByteUtil::ULongToBytes(value, 4)); + } + + buffer.append(QByteUtil::hexStringToBytes("EEEEEEEE")); + + emit dataRecieved(buffer); +} diff --git a/PhaseCompAcq/common/utils/QSerialPortUtil.h b/PhaseCompAcq/common/utils/QSerialPortUtil.h index 1a7f62f..accf403 100644 --- a/PhaseCompAcq/common/utils/QSerialPortUtil.h +++ b/PhaseCompAcq/common/utils/QSerialPortUtil.h @@ -21,6 +21,8 @@ bool open; + void mockReceivData(); + signals: void dataRecieved(QByteArray data); // 收到数据的信号 }; diff --git a/PhaseCompAcq/common/utils/SettingConfig.cpp b/PhaseCompAcq/common/utils/SettingConfig.cpp index c664a21..67a8dd0 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.cpp +++ b/PhaseCompAcq/common/utils/SettingConfig.cpp @@ -9,8 +9,11 @@ BAUD_RATE = getProperty("com", "baudRate").toUInt(); DEV_CODES = getProperty("com", "devCodes").toString(); + NEED_KAFKA = getProperty("kafka", "needKafka").toUInt(); KAFKA_BROKERS = getProperty("kafka", "brokers").toString(); KAFKA_DATA_TOPIC = getProperty("kafka", "dataTopic").toString(); + + CLIENT_ID = getProperty("client", "clientId").toString(); } diff --git a/PhaseCompAcq/common/utils/SettingConfig.h b/PhaseCompAcq/common/utils/SettingConfig.h index 08ab277..2d10a4d 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.h +++ b/PhaseCompAcq/common/utils/SettingConfig.h @@ -31,9 +31,12 @@ int BAUD_RATE; QString DEV_CODES; + int NEED_KAFKA; QString KAFKA_BROKERS; QString KAFKA_DATA_TOPIC; + QString CLIENT_ID; + private: SettingConfig(); diff --git a/PhaseCompAcq/conf/config.ini b/PhaseCompAcq/conf/config.ini index 631f014..ed326b2 100644 --- a/PhaseCompAcq/conf/config.ini +++ b/PhaseCompAcq/conf/config.ini @@ -1,8 +1,12 @@ [com] -portNames="COM1,COM2,COM3" -devCodes="9101,9102,9103" +portNames="COM3" +devCodes="9103" baudRate=115200 [kafka] +needKafka=1 brokers="111.198.10.15:12502" dataTopic="cppTest" + +[client] +clientId="112233445566" diff --git a/PhaseCompAcq/protocol/dto/PhaseDataDto.cpp b/PhaseCompAcq/protocol/dto/PhaseDataDto.cpp index 604700e..0627d34 100644 --- a/PhaseCompAcq/protocol/dto/PhaseDataDto.cpp +++ b/PhaseCompAcq/protocol/dto/PhaseDataDto.cpp @@ -2,12 +2,20 @@ PhaseDataDto::PhaseDataDto(QObject *parent) : QObject(parent) { - this->frameId = ""; - this->timestamp = ""; - this->milisecond = 0; - this->devCode = ""; - this->devStatus = ""; - this->channelActive = QList(); - this->channelData = QList(); +} + +QJsonObject PhaseDataDto::toJSON(int i) +{ + QJsonObject jsonObj; + + QJsonObject dataObj; + dataObj.insert("dataValue", QString("%1").arg(this->channelData.at(i))); + dataObj.insert("frameId", this->frameId); + + jsonObj.insert("channelNo", (i + 1)); + jsonObj.insert("ts", this->milisecond); + jsonObj.insert("data", dataObj); + + return jsonObj; } diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index a3729d2..133c113 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -10,6 +10,7 @@ #include #include #include +#include CounterWindow::CounterWindow(QWidget *parent) : QWidget(parent), @@ -17,6 +18,9 @@ { ui->setupUi(this); + QRect screenRect = QApplication::desktop()->screenGeometry(); + resize(screenRect.width(), 700); + QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 3a75d94..5c84cd5 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -6,6 +6,10 @@ { connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &PhaseDevice::dataReceivedHandler); + + kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaUtil.createProducer(); } PhaseDevice::~PhaseDevice() @@ -56,6 +60,8 @@ { this->dataBuff.append(data); + std::cout << QByteUtil::binToHexString(this->dataBuff).toStdString() << std::endl; + PhaseDataDto * phaseData = new PhaseDataDto(this); if (PhaseProtocolBM::checkFrame(this->dataBuff) == true) { @@ -112,11 +118,18 @@ QString channelDataStr = QString("%1 %2 %3").arg(phaseData->timestamp).arg(phaseData->channelData.at(i-1)).arg(phaseData->frameId); QLogUtil::writeChannelDataLog(chFilename, channelDataStr); + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + QJsonObject jsonObj = phaseData->toJSON(i - 1); + jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); + jsonObj.insert("deviceId", devCode); + kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + } } } - // 3. 输出到中间件,执行后续处理过程 - // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(phaseData); } diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 630a0e7..92182d4 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -4,6 +4,8 @@ #include #include "common/utils/QSerialPortUtil.h" #include "common/utils/QLogUtil.h" +#include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaUtil.h" #include "protocol/PhaseProtocolBM.h" class PhaseDevice : public QObject @@ -31,6 +33,7 @@ int baudRate; QSerialPortUtil serialUtil; + QKafkaUtil kafkaUtil; QByteArray dataBuff; signals: diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 459dcaf..b3cab89 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -18,12 +18,6 @@ { ui->setupUi(this); -// QKafkaUtil * kafkaTest = new QKafkaUtil(this); -// kafkaTest->setBrokers("111.198.10.15:12502"); -// kafkaTest->setTopic("cppTest"); -// int kaf = kafkaTest->initKafkaConf(); -// std::cout << "init kafka cppTest: " << kaf << std::endl; - QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; @@ -61,8 +55,6 @@ // 创建设备的widget this->generateWidgetForDevice(devCodeList.at(i), i); } - - this->createKafkaMessage(); } PhaseWindow::~PhaseWindow() @@ -75,7 +67,7 @@ // 1. 判断数据属于哪个设备,显示在不同的widget上 // 2. 循环设置各个tableView - QList devChannels = this->tableModelList.at(2); // 暂时写死 + QList devChannels = this->tableModelList.at(0); // 暂时写死 for (int i = 0; i < phaseData->channelData.size(); i++) { if (phaseData->channelActive.at(i).toInt() == 1) diff --git a/PhaseCompAcq/common/utils/QByteUtil.cpp b/PhaseCompAcq/common/utils/QByteUtil.cpp index b26c69b..1d49a51 100644 --- a/PhaseCompAcq/common/utils/QByteUtil.cpp +++ b/PhaseCompAcq/common/utils/QByteUtil.cpp @@ -58,6 +58,19 @@ return value; } +QByteArray QByteUtil::ULongToBytes(qulonglong value, qint8 length) +{ + QByteArray ba; + + for (int i = 0; i < length; i++) + { + ba.prepend(value % 256); + value = value / 256; + } + + return ba; +} + float QByteUtil::binToFloat(QByteArray bytes) { diff --git a/PhaseCompAcq/common/utils/QByteUtil.h b/PhaseCompAcq/common/utils/QByteUtil.h index 78fe0dd..f02d2f9 100644 --- a/PhaseCompAcq/common/utils/QByteUtil.h +++ b/PhaseCompAcq/common/utils/QByteUtil.h @@ -35,7 +35,7 @@ * @return */ static qulonglong binToULong(QByteArray bytes, quint8 length); - static QByteArray ULongToBytes(qulonglong value); + static QByteArray ULongToBytes(qulonglong value, qint8 length); /******** 字节数组与float互转 ********/ /** diff --git a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp index 2d54019..ba0ffb5 100644 --- a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp +++ b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp @@ -1,6 +1,9 @@ #include "QSerialPortUtil.h" #include +#include +#include +#include "common/utils/QByteUtil.h" QSerialPortUtil::QSerialPortUtil(QObject *parent) : QObject(parent) { @@ -18,12 +21,18 @@ open = serial.open(QIODevice::ReadWrite); - if (open == true) - { +// if (open == true) +// { // 绑定信号与槽 connect(&serial, &QSerialPort::readyRead, this, &QSerialPortUtil::readData); - } + + // mock data received per second + QTimer * timer = new QTimer(this); + connect(timer, &QTimer::timeout, + this, &QSerialPortUtil::mockReceivData); + timer->start(1000); +// } } void QSerialPortUtil::sendData(QByteArray data) @@ -46,3 +55,23 @@ { return this->open; } + +void QSerialPortUtil::mockReceivData() +{ + QDateTime now = QDateTime::currentDateTime(); + + QString frameId = now.toString("HHmmsszzz"); + + QByteArray buffer; + buffer.append(QByteUtil::hexStringToBytes(frameId.mid(0, 8))); + + for (int i = 1; i <= 16; i++) + { + int value = qrand() % 10000; + buffer.append(QByteUtil::hexStringToBytes("00800000")).append(QByteUtil::ULongToBytes(value, 4)); + } + + buffer.append(QByteUtil::hexStringToBytes("EEEEEEEE")); + + emit dataRecieved(buffer); +} diff --git a/PhaseCompAcq/common/utils/QSerialPortUtil.h b/PhaseCompAcq/common/utils/QSerialPortUtil.h index 1a7f62f..accf403 100644 --- a/PhaseCompAcq/common/utils/QSerialPortUtil.h +++ b/PhaseCompAcq/common/utils/QSerialPortUtil.h @@ -21,6 +21,8 @@ bool open; + void mockReceivData(); + signals: void dataRecieved(QByteArray data); // 收到数据的信号 }; diff --git a/PhaseCompAcq/common/utils/SettingConfig.cpp b/PhaseCompAcq/common/utils/SettingConfig.cpp index c664a21..67a8dd0 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.cpp +++ b/PhaseCompAcq/common/utils/SettingConfig.cpp @@ -9,8 +9,11 @@ BAUD_RATE = getProperty("com", "baudRate").toUInt(); DEV_CODES = getProperty("com", "devCodes").toString(); + NEED_KAFKA = getProperty("kafka", "needKafka").toUInt(); KAFKA_BROKERS = getProperty("kafka", "brokers").toString(); KAFKA_DATA_TOPIC = getProperty("kafka", "dataTopic").toString(); + + CLIENT_ID = getProperty("client", "clientId").toString(); } diff --git a/PhaseCompAcq/common/utils/SettingConfig.h b/PhaseCompAcq/common/utils/SettingConfig.h index 08ab277..2d10a4d 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.h +++ b/PhaseCompAcq/common/utils/SettingConfig.h @@ -31,9 +31,12 @@ int BAUD_RATE; QString DEV_CODES; + int NEED_KAFKA; QString KAFKA_BROKERS; QString KAFKA_DATA_TOPIC; + QString CLIENT_ID; + private: SettingConfig(); diff --git a/PhaseCompAcq/conf/config.ini b/PhaseCompAcq/conf/config.ini index 631f014..ed326b2 100644 --- a/PhaseCompAcq/conf/config.ini +++ b/PhaseCompAcq/conf/config.ini @@ -1,8 +1,12 @@ [com] -portNames="COM1,COM2,COM3" -devCodes="9101,9102,9103" +portNames="COM3" +devCodes="9103" baudRate=115200 [kafka] +needKafka=1 brokers="111.198.10.15:12502" dataTopic="cppTest" + +[client] +clientId="112233445566" diff --git a/PhaseCompAcq/protocol/dto/PhaseDataDto.cpp b/PhaseCompAcq/protocol/dto/PhaseDataDto.cpp index 604700e..0627d34 100644 --- a/PhaseCompAcq/protocol/dto/PhaseDataDto.cpp +++ b/PhaseCompAcq/protocol/dto/PhaseDataDto.cpp @@ -2,12 +2,20 @@ PhaseDataDto::PhaseDataDto(QObject *parent) : QObject(parent) { - this->frameId = ""; - this->timestamp = ""; - this->milisecond = 0; - this->devCode = ""; - this->devStatus = ""; - this->channelActive = QList(); - this->channelData = QList(); +} + +QJsonObject PhaseDataDto::toJSON(int i) +{ + QJsonObject jsonObj; + + QJsonObject dataObj; + dataObj.insert("dataValue", QString("%1").arg(this->channelData.at(i))); + dataObj.insert("frameId", this->frameId); + + jsonObj.insert("channelNo", (i + 1)); + jsonObj.insert("ts", this->milisecond); + jsonObj.insert("data", dataObj); + + return jsonObj; } diff --git a/PhaseCompAcq/protocol/dto/PhaseDataDto.h b/PhaseCompAcq/protocol/dto/PhaseDataDto.h index 75af3ee..05924fb 100644 --- a/PhaseCompAcq/protocol/dto/PhaseDataDto.h +++ b/PhaseCompAcq/protocol/dto/PhaseDataDto.h @@ -2,6 +2,8 @@ #define PHASEDATADTO_H #include +#include +#include class PhaseDataDto : public QObject { @@ -11,7 +13,7 @@ QString frameId; // 帧id QString timestamp; // 时间戳字符串 - qulonglong milisecond; // 毫秒计数 + qlonglong milisecond; // 毫秒计数 QByteArray rawFrame; // 原始帧字节数组 QString devCode; @@ -19,6 +21,8 @@ QList channelActive; QList channelData; + QJsonObject toJSON(int i); + signals: }; diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index a3729d2..133c113 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -10,6 +10,7 @@ #include #include #include +#include CounterWindow::CounterWindow(QWidget *parent) : QWidget(parent), @@ -17,6 +18,9 @@ { ui->setupUi(this); + QRect screenRect = QApplication::desktop()->screenGeometry(); + resize(screenRect.width(), 700); + QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; diff --git a/PhaseCompAcq/PhaseDevice.cpp b/PhaseCompAcq/PhaseDevice.cpp index 3a75d94..5c84cd5 100644 --- a/PhaseCompAcq/PhaseDevice.cpp +++ b/PhaseCompAcq/PhaseDevice.cpp @@ -6,6 +6,10 @@ { connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &PhaseDevice::dataReceivedHandler); + + kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaUtil.createProducer(); } PhaseDevice::~PhaseDevice() @@ -56,6 +60,8 @@ { this->dataBuff.append(data); + std::cout << QByteUtil::binToHexString(this->dataBuff).toStdString() << std::endl; + PhaseDataDto * phaseData = new PhaseDataDto(this); if (PhaseProtocolBM::checkFrame(this->dataBuff) == true) { @@ -112,11 +118,18 @@ QString channelDataStr = QString("%1 %2 %3").arg(phaseData->timestamp).arg(phaseData->channelData.at(i-1)).arg(phaseData->frameId); QLogUtil::writeChannelDataLog(chFilename, channelDataStr); + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + QJsonObject jsonObj = phaseData->toJSON(i - 1); + jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); + jsonObj.insert("deviceId", devCode); + kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + } } } - // 3. 输出到中间件,执行后续处理过程 - // 4. 在界面上简单显示相差数据结果 emit this->sendDataToDraw(phaseData); } diff --git a/PhaseCompAcq/PhaseDevice.h b/PhaseCompAcq/PhaseDevice.h index 630a0e7..92182d4 100644 --- a/PhaseCompAcq/PhaseDevice.h +++ b/PhaseCompAcq/PhaseDevice.h @@ -4,6 +4,8 @@ #include #include "common/utils/QSerialPortUtil.h" #include "common/utils/QLogUtil.h" +#include "common/utils/SettingConfig.h" +#include "common/utils/QKafkaUtil.h" #include "protocol/PhaseProtocolBM.h" class PhaseDevice : public QObject @@ -31,6 +33,7 @@ int baudRate; QSerialPortUtil serialUtil; + QKafkaUtil kafkaUtil; QByteArray dataBuff; signals: diff --git a/PhaseCompAcq/PhaseWindow.cpp b/PhaseCompAcq/PhaseWindow.cpp index 459dcaf..b3cab89 100644 --- a/PhaseCompAcq/PhaseWindow.cpp +++ b/PhaseCompAcq/PhaseWindow.cpp @@ -18,12 +18,6 @@ { ui->setupUi(this); -// QKafkaUtil * kafkaTest = new QKafkaUtil(this); -// kafkaTest->setBrokers("111.198.10.15:12502"); -// kafkaTest->setTopic("cppTest"); -// int kaf = kafkaTest->initKafkaConf(); -// std::cout << "init kafka cppTest: " << kaf << std::endl; - QString portNames = SettingConfig::getInstance().PORT_NAMES; int baudRate = SettingConfig::getInstance().BAUD_RATE; QString devCodes = SettingConfig::getInstance().DEV_CODES; @@ -61,8 +55,6 @@ // 创建设备的widget this->generateWidgetForDevice(devCodeList.at(i), i); } - - this->createKafkaMessage(); } PhaseWindow::~PhaseWindow() @@ -75,7 +67,7 @@ // 1. 判断数据属于哪个设备,显示在不同的widget上 // 2. 循环设置各个tableView - QList devChannels = this->tableModelList.at(2); // 暂时写死 + QList devChannels = this->tableModelList.at(0); // 暂时写死 for (int i = 0; i < phaseData->channelData.size(); i++) { if (phaseData->channelActive.at(i).toInt() == 1) diff --git a/PhaseCompAcq/common/utils/QByteUtil.cpp b/PhaseCompAcq/common/utils/QByteUtil.cpp index b26c69b..1d49a51 100644 --- a/PhaseCompAcq/common/utils/QByteUtil.cpp +++ b/PhaseCompAcq/common/utils/QByteUtil.cpp @@ -58,6 +58,19 @@ return value; } +QByteArray QByteUtil::ULongToBytes(qulonglong value, qint8 length) +{ + QByteArray ba; + + for (int i = 0; i < length; i++) + { + ba.prepend(value % 256); + value = value / 256; + } + + return ba; +} + float QByteUtil::binToFloat(QByteArray bytes) { diff --git a/PhaseCompAcq/common/utils/QByteUtil.h b/PhaseCompAcq/common/utils/QByteUtil.h index 78fe0dd..f02d2f9 100644 --- a/PhaseCompAcq/common/utils/QByteUtil.h +++ b/PhaseCompAcq/common/utils/QByteUtil.h @@ -35,7 +35,7 @@ * @return */ static qulonglong binToULong(QByteArray bytes, quint8 length); - static QByteArray ULongToBytes(qulonglong value); + static QByteArray ULongToBytes(qulonglong value, qint8 length); /******** 字节数组与float互转 ********/ /** diff --git a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp index 2d54019..ba0ffb5 100644 --- a/PhaseCompAcq/common/utils/QSerialPortUtil.cpp +++ b/PhaseCompAcq/common/utils/QSerialPortUtil.cpp @@ -1,6 +1,9 @@ #include "QSerialPortUtil.h" #include +#include +#include +#include "common/utils/QByteUtil.h" QSerialPortUtil::QSerialPortUtil(QObject *parent) : QObject(parent) { @@ -18,12 +21,18 @@ open = serial.open(QIODevice::ReadWrite); - if (open == true) - { +// if (open == true) +// { // 绑定信号与槽 connect(&serial, &QSerialPort::readyRead, this, &QSerialPortUtil::readData); - } + + // mock data received per second + QTimer * timer = new QTimer(this); + connect(timer, &QTimer::timeout, + this, &QSerialPortUtil::mockReceivData); + timer->start(1000); +// } } void QSerialPortUtil::sendData(QByteArray data) @@ -46,3 +55,23 @@ { return this->open; } + +void QSerialPortUtil::mockReceivData() +{ + QDateTime now = QDateTime::currentDateTime(); + + QString frameId = now.toString("HHmmsszzz"); + + QByteArray buffer; + buffer.append(QByteUtil::hexStringToBytes(frameId.mid(0, 8))); + + for (int i = 1; i <= 16; i++) + { + int value = qrand() % 10000; + buffer.append(QByteUtil::hexStringToBytes("00800000")).append(QByteUtil::ULongToBytes(value, 4)); + } + + buffer.append(QByteUtil::hexStringToBytes("EEEEEEEE")); + + emit dataRecieved(buffer); +} diff --git a/PhaseCompAcq/common/utils/QSerialPortUtil.h b/PhaseCompAcq/common/utils/QSerialPortUtil.h index 1a7f62f..accf403 100644 --- a/PhaseCompAcq/common/utils/QSerialPortUtil.h +++ b/PhaseCompAcq/common/utils/QSerialPortUtil.h @@ -21,6 +21,8 @@ bool open; + void mockReceivData(); + signals: void dataRecieved(QByteArray data); // 收到数据的信号 }; diff --git a/PhaseCompAcq/common/utils/SettingConfig.cpp b/PhaseCompAcq/common/utils/SettingConfig.cpp index c664a21..67a8dd0 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.cpp +++ b/PhaseCompAcq/common/utils/SettingConfig.cpp @@ -9,8 +9,11 @@ BAUD_RATE = getProperty("com", "baudRate").toUInt(); DEV_CODES = getProperty("com", "devCodes").toString(); + NEED_KAFKA = getProperty("kafka", "needKafka").toUInt(); KAFKA_BROKERS = getProperty("kafka", "brokers").toString(); KAFKA_DATA_TOPIC = getProperty("kafka", "dataTopic").toString(); + + CLIENT_ID = getProperty("client", "clientId").toString(); } diff --git a/PhaseCompAcq/common/utils/SettingConfig.h b/PhaseCompAcq/common/utils/SettingConfig.h index 08ab277..2d10a4d 100644 --- a/PhaseCompAcq/common/utils/SettingConfig.h +++ b/PhaseCompAcq/common/utils/SettingConfig.h @@ -31,9 +31,12 @@ int BAUD_RATE; QString DEV_CODES; + int NEED_KAFKA; QString KAFKA_BROKERS; QString KAFKA_DATA_TOPIC; + QString CLIENT_ID; + private: SettingConfig(); diff --git a/PhaseCompAcq/conf/config.ini b/PhaseCompAcq/conf/config.ini index 631f014..ed326b2 100644 --- a/PhaseCompAcq/conf/config.ini +++ b/PhaseCompAcq/conf/config.ini @@ -1,8 +1,12 @@ [com] -portNames="COM1,COM2,COM3" -devCodes="9101,9102,9103" +portNames="COM3" +devCodes="9103" baudRate=115200 [kafka] +needKafka=1 brokers="111.198.10.15:12502" dataTopic="cppTest" + +[client] +clientId="112233445566" diff --git a/PhaseCompAcq/protocol/dto/PhaseDataDto.cpp b/PhaseCompAcq/protocol/dto/PhaseDataDto.cpp index 604700e..0627d34 100644 --- a/PhaseCompAcq/protocol/dto/PhaseDataDto.cpp +++ b/PhaseCompAcq/protocol/dto/PhaseDataDto.cpp @@ -2,12 +2,20 @@ PhaseDataDto::PhaseDataDto(QObject *parent) : QObject(parent) { - this->frameId = ""; - this->timestamp = ""; - this->milisecond = 0; - this->devCode = ""; - this->devStatus = ""; - this->channelActive = QList(); - this->channelData = QList(); +} + +QJsonObject PhaseDataDto::toJSON(int i) +{ + QJsonObject jsonObj; + + QJsonObject dataObj; + dataObj.insert("dataValue", QString("%1").arg(this->channelData.at(i))); + dataObj.insert("frameId", this->frameId); + + jsonObj.insert("channelNo", (i + 1)); + jsonObj.insert("ts", this->milisecond); + jsonObj.insert("data", dataObj); + + return jsonObj; } diff --git a/PhaseCompAcq/protocol/dto/PhaseDataDto.h b/PhaseCompAcq/protocol/dto/PhaseDataDto.h index 75af3ee..05924fb 100644 --- a/PhaseCompAcq/protocol/dto/PhaseDataDto.h +++ b/PhaseCompAcq/protocol/dto/PhaseDataDto.h @@ -2,6 +2,8 @@ #define PHASEDATADTO_H #include +#include +#include class PhaseDataDto : public QObject { @@ -11,7 +13,7 @@ QString frameId; // 帧id QString timestamp; // 时间戳字符串 - qulonglong milisecond; // 毫秒计数 + qlonglong milisecond; // 毫秒计数 QByteArray rawFrame; // 原始帧字节数组 QString devCode; @@ -19,6 +21,8 @@ QList channelActive; QList channelData; + QJsonObject toJSON(int i); + signals: }; diff --git a/ZXSSCJ.pro b/ZXSSCJ.pro index 5d8d1e2..87b1bd3 100644 --- a/ZXSSCJ.pro +++ b/ZXSSCJ.pro @@ -4,6 +4,6 @@ #CONFIG += ordered SUBDIRS += CounterAcq #计数器数据采集 -#SUBDIRS += PhaseCompAcq #比相仪数据采集 +SUBDIRS += PhaseCompAcq #比相仪数据采集 #SUBDIRS += DevStatusAcq #SUBDIRS += HClockAcq #氢钟状态数据采集