diff --git a/CounterAcq/CounterDevice.cpp b/CounterAcq/CounterDevice.cpp index 9de69d6..46c14df 100644 --- a/CounterAcq/CounterDevice.cpp +++ b/CounterAcq/CounterDevice.cpp @@ -58,40 +58,49 @@ void CounterDevice::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); - std::cout << dataBuff.toStdString() << std::endl; - CounterDataDto * counterData = new CounterDataDto(this); - if (CounterProtocolXH::checkFrame(this->dataBuff) == true) + QList frameList = CounterProtocolXH::extractFrameList(data); + if (frameList.size() > 0) { - counterData->rawFrame = this->dataBuff; -// std::cout << counterData->rawFrame.toStdString() << std::endl; - - // ★解析成数据对象 - bool parse = CounterProtocolXH::parseMessureData(this->dataBuff, counterData); - - // 解析成功 - if (parse == true) + this->dataBuff.clear(); + for (int i = 0; i < frameList.size(); i++) { - // 1. 清空dataBuff,等待下一帧的数据 - this->dataBuff.clear(); + QByteArray frameByte = frameList.at(i); + if (CounterProtocolXH::checkFrame(frameByte) == true) + { + CounterDataDto * counterData = new CounterDataDto(this); - // 2. 补充其他字段 - QDateTime now = QDateTime::currentDateTime(); - counterData->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); - counterData->milisecond = now.toMSecsSinceEpoch(); - counterData->devCode = devCode; + counterData->rawFrame = frameByte; - this->afterFramePhase(counterData); + // ★解析成数据对象 + bool parse = CounterProtocolXH::parseMessureData(frameByte, counterData); - // 3 计算数据ID一样的测量数据的时差值,通道值-参考通道值 -// this->pushChannelRawFrame(counterData); + // 解析成功 + if (parse == true) + { + // 1. 清空dataBuff,等待下一帧的数据 +// this->dataBuff.clear(); + std::cout << "+++" << frameByte.toStdString() << std::endl; + + // 2. 补充其他字段 + QDateTime now = QDateTime::currentDateTime(); + counterData->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); + counterData->milisecond = now.toMSecsSinceEpoch(); + counterData->devCode = devCode; + + this->afterFramePhase(counterData); + + // 3 计算数据ID一样的测量数据的时差值,通道值-参考通道值 + // this->pushChannelRawFrame(counterData); + } + + // 在此处释放内存,不影响后续显示 + // 不在此处释放内存则会导致内存持续增加 + delete counterData; + } } } - - // 在此处释放内存,不影响后续显示 - // 不在此处释放内存则会导致内存持续增加 - delete counterData; } void CounterDevice::dataReceivedHandlerByUDP() @@ -144,38 +153,11 @@ jsonObj.insert("deviceId", deviceId); // kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); } + + // 4. 在界面上简单显示相差数据结果 + emit this->sendDataToDraw(counterData); } } - - /* - QString chFilename("%1_CH_%2.log"); - chFilename = chFilename.arg(devCode); - if (counterData->channelId < 10) - { - chFilename = chFilename.arg(QString("0%1").arg(counterData->channelId)); - } else - { - chFilename = chFilename.arg(counterData->channelId); - } - QString channelDataStr = QString("%1 [%2] %3 %4") - .arg(counterData->timestamp) - .arg(counterData->frameId) - .arg(counterData->channelData) - .arg(counterData->channelClockValue); - QLogUtil::writeChannelDataLogByDate(date, chFilename, channelDataStr); - - // 3. 输出到中间件,执行后续处理过程 - if (SettingConfig::getInstance().NEED_KAFKA == 1) - { - QJsonObject jsonObj = counterData->toJSON(); - jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); - jsonObj.insert("deviceId", deviceId); - kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); - } - */ - - // 4. 在界面上简单显示相差数据结果 - emit this->sendDataToDraw(counterData); } void CounterDevice::pushChannelRawFrame(CounterDataDto * counterData) diff --git a/CounterAcq/CounterDevice.cpp b/CounterAcq/CounterDevice.cpp index 9de69d6..46c14df 100644 --- a/CounterAcq/CounterDevice.cpp +++ b/CounterAcq/CounterDevice.cpp @@ -58,40 +58,49 @@ void CounterDevice::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); - std::cout << dataBuff.toStdString() << std::endl; - CounterDataDto * counterData = new CounterDataDto(this); - if (CounterProtocolXH::checkFrame(this->dataBuff) == true) + QList frameList = CounterProtocolXH::extractFrameList(data); + if (frameList.size() > 0) { - counterData->rawFrame = this->dataBuff; -// std::cout << counterData->rawFrame.toStdString() << std::endl; - - // ★解析成数据对象 - bool parse = CounterProtocolXH::parseMessureData(this->dataBuff, counterData); - - // 解析成功 - if (parse == true) + this->dataBuff.clear(); + for (int i = 0; i < frameList.size(); i++) { - // 1. 清空dataBuff,等待下一帧的数据 - this->dataBuff.clear(); + QByteArray frameByte = frameList.at(i); + if (CounterProtocolXH::checkFrame(frameByte) == true) + { + CounterDataDto * counterData = new CounterDataDto(this); - // 2. 补充其他字段 - QDateTime now = QDateTime::currentDateTime(); - counterData->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); - counterData->milisecond = now.toMSecsSinceEpoch(); - counterData->devCode = devCode; + counterData->rawFrame = frameByte; - this->afterFramePhase(counterData); + // ★解析成数据对象 + bool parse = CounterProtocolXH::parseMessureData(frameByte, counterData); - // 3 计算数据ID一样的测量数据的时差值,通道值-参考通道值 -// this->pushChannelRawFrame(counterData); + // 解析成功 + if (parse == true) + { + // 1. 清空dataBuff,等待下一帧的数据 +// this->dataBuff.clear(); + std::cout << "+++" << frameByte.toStdString() << std::endl; + + // 2. 补充其他字段 + QDateTime now = QDateTime::currentDateTime(); + counterData->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); + counterData->milisecond = now.toMSecsSinceEpoch(); + counterData->devCode = devCode; + + this->afterFramePhase(counterData); + + // 3 计算数据ID一样的测量数据的时差值,通道值-参考通道值 + // this->pushChannelRawFrame(counterData); + } + + // 在此处释放内存,不影响后续显示 + // 不在此处释放内存则会导致内存持续增加 + delete counterData; + } } } - - // 在此处释放内存,不影响后续显示 - // 不在此处释放内存则会导致内存持续增加 - delete counterData; } void CounterDevice::dataReceivedHandlerByUDP() @@ -144,38 +153,11 @@ jsonObj.insert("deviceId", deviceId); // kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); } + + // 4. 在界面上简单显示相差数据结果 + emit this->sendDataToDraw(counterData); } } - - /* - QString chFilename("%1_CH_%2.log"); - chFilename = chFilename.arg(devCode); - if (counterData->channelId < 10) - { - chFilename = chFilename.arg(QString("0%1").arg(counterData->channelId)); - } else - { - chFilename = chFilename.arg(counterData->channelId); - } - QString channelDataStr = QString("%1 [%2] %3 %4") - .arg(counterData->timestamp) - .arg(counterData->frameId) - .arg(counterData->channelData) - .arg(counterData->channelClockValue); - QLogUtil::writeChannelDataLogByDate(date, chFilename, channelDataStr); - - // 3. 输出到中间件,执行后续处理过程 - if (SettingConfig::getInstance().NEED_KAFKA == 1) - { - QJsonObject jsonObj = counterData->toJSON(); - jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); - jsonObj.insert("deviceId", deviceId); - kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); - } - */ - - // 4. 在界面上简单显示相差数据结果 - emit this->sendDataToDraw(counterData); } void CounterDevice::pushChannelRawFrame(CounterDataDto * counterData) diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 0840fb2..63262dc 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -130,7 +130,7 @@ dataLabel->setStyleSheet("margin-left:20;"); vbox->addWidget(dataLabel); QLineEdit * dataValue = new QLineEdit(); - dataValue->setFixedWidth(75); + dataValue->setFixedWidth(200); dataValue->setFont(labelFont); vbox->addWidget(dataValue); @@ -202,7 +202,7 @@ } // 1. 判断数据属于哪个设备,显示在不同的widget上 -// qDebug() << counterData->toJSON() << counterData->devCode << "---" << counterData->frameId; + qDebug() << counterData->toJSON() << counterData->devCode << "---" << counterData->frameId; // 更新所有通道BOX的值 for (int i = 0; i < counterData->channelDataArray.size(); i++) diff --git a/CounterAcq/CounterDevice.cpp b/CounterAcq/CounterDevice.cpp index 9de69d6..46c14df 100644 --- a/CounterAcq/CounterDevice.cpp +++ b/CounterAcq/CounterDevice.cpp @@ -58,40 +58,49 @@ void CounterDevice::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); - std::cout << dataBuff.toStdString() << std::endl; - CounterDataDto * counterData = new CounterDataDto(this); - if (CounterProtocolXH::checkFrame(this->dataBuff) == true) + QList frameList = CounterProtocolXH::extractFrameList(data); + if (frameList.size() > 0) { - counterData->rawFrame = this->dataBuff; -// std::cout << counterData->rawFrame.toStdString() << std::endl; - - // ★解析成数据对象 - bool parse = CounterProtocolXH::parseMessureData(this->dataBuff, counterData); - - // 解析成功 - if (parse == true) + this->dataBuff.clear(); + for (int i = 0; i < frameList.size(); i++) { - // 1. 清空dataBuff,等待下一帧的数据 - this->dataBuff.clear(); + QByteArray frameByte = frameList.at(i); + if (CounterProtocolXH::checkFrame(frameByte) == true) + { + CounterDataDto * counterData = new CounterDataDto(this); - // 2. 补充其他字段 - QDateTime now = QDateTime::currentDateTime(); - counterData->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); - counterData->milisecond = now.toMSecsSinceEpoch(); - counterData->devCode = devCode; + counterData->rawFrame = frameByte; - this->afterFramePhase(counterData); + // ★解析成数据对象 + bool parse = CounterProtocolXH::parseMessureData(frameByte, counterData); - // 3 计算数据ID一样的测量数据的时差值,通道值-参考通道值 -// this->pushChannelRawFrame(counterData); + // 解析成功 + if (parse == true) + { + // 1. 清空dataBuff,等待下一帧的数据 +// this->dataBuff.clear(); + std::cout << "+++" << frameByte.toStdString() << std::endl; + + // 2. 补充其他字段 + QDateTime now = QDateTime::currentDateTime(); + counterData->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); + counterData->milisecond = now.toMSecsSinceEpoch(); + counterData->devCode = devCode; + + this->afterFramePhase(counterData); + + // 3 计算数据ID一样的测量数据的时差值,通道值-参考通道值 + // this->pushChannelRawFrame(counterData); + } + + // 在此处释放内存,不影响后续显示 + // 不在此处释放内存则会导致内存持续增加 + delete counterData; + } } } - - // 在此处释放内存,不影响后续显示 - // 不在此处释放内存则会导致内存持续增加 - delete counterData; } void CounterDevice::dataReceivedHandlerByUDP() @@ -144,38 +153,11 @@ jsonObj.insert("deviceId", deviceId); // kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); } + + // 4. 在界面上简单显示相差数据结果 + emit this->sendDataToDraw(counterData); } } - - /* - QString chFilename("%1_CH_%2.log"); - chFilename = chFilename.arg(devCode); - if (counterData->channelId < 10) - { - chFilename = chFilename.arg(QString("0%1").arg(counterData->channelId)); - } else - { - chFilename = chFilename.arg(counterData->channelId); - } - QString channelDataStr = QString("%1 [%2] %3 %4") - .arg(counterData->timestamp) - .arg(counterData->frameId) - .arg(counterData->channelData) - .arg(counterData->channelClockValue); - QLogUtil::writeChannelDataLogByDate(date, chFilename, channelDataStr); - - // 3. 输出到中间件,执行后续处理过程 - if (SettingConfig::getInstance().NEED_KAFKA == 1) - { - QJsonObject jsonObj = counterData->toJSON(); - jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); - jsonObj.insert("deviceId", deviceId); - kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); - } - */ - - // 4. 在界面上简单显示相差数据结果 - emit this->sendDataToDraw(counterData); } void CounterDevice::pushChannelRawFrame(CounterDataDto * counterData) diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 0840fb2..63262dc 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -130,7 +130,7 @@ dataLabel->setStyleSheet("margin-left:20;"); vbox->addWidget(dataLabel); QLineEdit * dataValue = new QLineEdit(); - dataValue->setFixedWidth(75); + dataValue->setFixedWidth(200); dataValue->setFont(labelFont); vbox->addWidget(dataValue); @@ -202,7 +202,7 @@ } // 1. 判断数据属于哪个设备,显示在不同的widget上 -// qDebug() << counterData->toJSON() << counterData->devCode << "---" << counterData->frameId; + qDebug() << counterData->toJSON() << counterData->devCode << "---" << counterData->frameId; // 更新所有通道BOX的值 for (int i = 0; i < counterData->channelDataArray.size(); i++) diff --git a/CounterAcq/common/utils/QKafkaUtil.cpp b/CounterAcq/common/utils/QKafkaUtil.cpp index 3f00452..b30cf45 100644 --- a/CounterAcq/common/utils/QKafkaUtil.cpp +++ b/CounterAcq/common/utils/QKafkaUtil.cpp @@ -1,4 +1,5 @@ #include "QKafkaUtil.h" +#include "SettingConfig.h" #include QKafkaUtil::QKafkaUtil(QObject *parent) : QObject(parent) @@ -20,6 +21,13 @@ { int result; result = this->conf->set("bootstrap.servers", this->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 (result != RdKafka::Conf::CONF_OK) { diff --git a/CounterAcq/CounterDevice.cpp b/CounterAcq/CounterDevice.cpp index 9de69d6..46c14df 100644 --- a/CounterAcq/CounterDevice.cpp +++ b/CounterAcq/CounterDevice.cpp @@ -58,40 +58,49 @@ void CounterDevice::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); - std::cout << dataBuff.toStdString() << std::endl; - CounterDataDto * counterData = new CounterDataDto(this); - if (CounterProtocolXH::checkFrame(this->dataBuff) == true) + QList frameList = CounterProtocolXH::extractFrameList(data); + if (frameList.size() > 0) { - counterData->rawFrame = this->dataBuff; -// std::cout << counterData->rawFrame.toStdString() << std::endl; - - // ★解析成数据对象 - bool parse = CounterProtocolXH::parseMessureData(this->dataBuff, counterData); - - // 解析成功 - if (parse == true) + this->dataBuff.clear(); + for (int i = 0; i < frameList.size(); i++) { - // 1. 清空dataBuff,等待下一帧的数据 - this->dataBuff.clear(); + QByteArray frameByte = frameList.at(i); + if (CounterProtocolXH::checkFrame(frameByte) == true) + { + CounterDataDto * counterData = new CounterDataDto(this); - // 2. 补充其他字段 - QDateTime now = QDateTime::currentDateTime(); - counterData->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); - counterData->milisecond = now.toMSecsSinceEpoch(); - counterData->devCode = devCode; + counterData->rawFrame = frameByte; - this->afterFramePhase(counterData); + // ★解析成数据对象 + bool parse = CounterProtocolXH::parseMessureData(frameByte, counterData); - // 3 计算数据ID一样的测量数据的时差值,通道值-参考通道值 -// this->pushChannelRawFrame(counterData); + // 解析成功 + if (parse == true) + { + // 1. 清空dataBuff,等待下一帧的数据 +// this->dataBuff.clear(); + std::cout << "+++" << frameByte.toStdString() << std::endl; + + // 2. 补充其他字段 + QDateTime now = QDateTime::currentDateTime(); + counterData->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); + counterData->milisecond = now.toMSecsSinceEpoch(); + counterData->devCode = devCode; + + this->afterFramePhase(counterData); + + // 3 计算数据ID一样的测量数据的时差值,通道值-参考通道值 + // this->pushChannelRawFrame(counterData); + } + + // 在此处释放内存,不影响后续显示 + // 不在此处释放内存则会导致内存持续增加 + delete counterData; + } } } - - // 在此处释放内存,不影响后续显示 - // 不在此处释放内存则会导致内存持续增加 - delete counterData; } void CounterDevice::dataReceivedHandlerByUDP() @@ -144,38 +153,11 @@ jsonObj.insert("deviceId", deviceId); // kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); } + + // 4. 在界面上简单显示相差数据结果 + emit this->sendDataToDraw(counterData); } } - - /* - QString chFilename("%1_CH_%2.log"); - chFilename = chFilename.arg(devCode); - if (counterData->channelId < 10) - { - chFilename = chFilename.arg(QString("0%1").arg(counterData->channelId)); - } else - { - chFilename = chFilename.arg(counterData->channelId); - } - QString channelDataStr = QString("%1 [%2] %3 %4") - .arg(counterData->timestamp) - .arg(counterData->frameId) - .arg(counterData->channelData) - .arg(counterData->channelClockValue); - QLogUtil::writeChannelDataLogByDate(date, chFilename, channelDataStr); - - // 3. 输出到中间件,执行后续处理过程 - if (SettingConfig::getInstance().NEED_KAFKA == 1) - { - QJsonObject jsonObj = counterData->toJSON(); - jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); - jsonObj.insert("deviceId", deviceId); - kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); - } - */ - - // 4. 在界面上简单显示相差数据结果 - emit this->sendDataToDraw(counterData); } void CounterDevice::pushChannelRawFrame(CounterDataDto * counterData) diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 0840fb2..63262dc 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -130,7 +130,7 @@ dataLabel->setStyleSheet("margin-left:20;"); vbox->addWidget(dataLabel); QLineEdit * dataValue = new QLineEdit(); - dataValue->setFixedWidth(75); + dataValue->setFixedWidth(200); dataValue->setFont(labelFont); vbox->addWidget(dataValue); @@ -202,7 +202,7 @@ } // 1. 判断数据属于哪个设备,显示在不同的widget上 -// qDebug() << counterData->toJSON() << counterData->devCode << "---" << counterData->frameId; + qDebug() << counterData->toJSON() << counterData->devCode << "---" << counterData->frameId; // 更新所有通道BOX的值 for (int i = 0; i < counterData->channelDataArray.size(); i++) diff --git a/CounterAcq/common/utils/QKafkaUtil.cpp b/CounterAcq/common/utils/QKafkaUtil.cpp index 3f00452..b30cf45 100644 --- a/CounterAcq/common/utils/QKafkaUtil.cpp +++ b/CounterAcq/common/utils/QKafkaUtil.cpp @@ -1,4 +1,5 @@ #include "QKafkaUtil.h" +#include "SettingConfig.h" #include QKafkaUtil::QKafkaUtil(QObject *parent) : QObject(parent) @@ -20,6 +21,13 @@ { int result; result = this->conf->set("bootstrap.servers", this->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 (result != RdKafka::Conf::CONF_OK) { diff --git a/CounterAcq/common/utils/SettingConfig.cpp b/CounterAcq/common/utils/SettingConfig.cpp index 1f2ebb2..9a84a43 100644 --- a/CounterAcq/common/utils/SettingConfig.cpp +++ b/CounterAcq/common/utils/SettingConfig.cpp @@ -5,11 +5,14 @@ filename = QApplication::applicationDirPath() + "/conf/config.ini"; setting = new QSettings(this->filename, QSettings::IniFormat); - BAUD_RATE = getProperty("com", "baudRate").toUInt(); + BAUD_RATE = getProperty("com", "baudRate").toInt(); - NEED_KAFKA = getProperty("kafka", "needKafka").toUInt(); + NEED_KAFKA = getProperty("kafka", "needKafka").toInt(); KAFKA_BROKERS = getProperty("kafka", "brokers").toString(); KAFKA_DATA_TOPIC = getProperty("kafka", "dataTopic").toString(); + NEED_SASL = getProperty("kafka", "needSasl").toInt(); + SASL_USERNAME = getProperty("kafka", "saslUsername").toString(); + SASL_PASSWORD = getProperty("kafka", "saslPassword").toString(); CLIENT_ID = getProperty("client", "clientId").toString(); APP_KEY = getProperty("client", "appKey").toString(); diff --git a/CounterAcq/CounterDevice.cpp b/CounterAcq/CounterDevice.cpp index 9de69d6..46c14df 100644 --- a/CounterAcq/CounterDevice.cpp +++ b/CounterAcq/CounterDevice.cpp @@ -58,40 +58,49 @@ void CounterDevice::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); - std::cout << dataBuff.toStdString() << std::endl; - CounterDataDto * counterData = new CounterDataDto(this); - if (CounterProtocolXH::checkFrame(this->dataBuff) == true) + QList frameList = CounterProtocolXH::extractFrameList(data); + if (frameList.size() > 0) { - counterData->rawFrame = this->dataBuff; -// std::cout << counterData->rawFrame.toStdString() << std::endl; - - // ★解析成数据对象 - bool parse = CounterProtocolXH::parseMessureData(this->dataBuff, counterData); - - // 解析成功 - if (parse == true) + this->dataBuff.clear(); + for (int i = 0; i < frameList.size(); i++) { - // 1. 清空dataBuff,等待下一帧的数据 - this->dataBuff.clear(); + QByteArray frameByte = frameList.at(i); + if (CounterProtocolXH::checkFrame(frameByte) == true) + { + CounterDataDto * counterData = new CounterDataDto(this); - // 2. 补充其他字段 - QDateTime now = QDateTime::currentDateTime(); - counterData->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); - counterData->milisecond = now.toMSecsSinceEpoch(); - counterData->devCode = devCode; + counterData->rawFrame = frameByte; - this->afterFramePhase(counterData); + // ★解析成数据对象 + bool parse = CounterProtocolXH::parseMessureData(frameByte, counterData); - // 3 计算数据ID一样的测量数据的时差值,通道值-参考通道值 -// this->pushChannelRawFrame(counterData); + // 解析成功 + if (parse == true) + { + // 1. 清空dataBuff,等待下一帧的数据 +// this->dataBuff.clear(); + std::cout << "+++" << frameByte.toStdString() << std::endl; + + // 2. 补充其他字段 + QDateTime now = QDateTime::currentDateTime(); + counterData->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); + counterData->milisecond = now.toMSecsSinceEpoch(); + counterData->devCode = devCode; + + this->afterFramePhase(counterData); + + // 3 计算数据ID一样的测量数据的时差值,通道值-参考通道值 + // this->pushChannelRawFrame(counterData); + } + + // 在此处释放内存,不影响后续显示 + // 不在此处释放内存则会导致内存持续增加 + delete counterData; + } } } - - // 在此处释放内存,不影响后续显示 - // 不在此处释放内存则会导致内存持续增加 - delete counterData; } void CounterDevice::dataReceivedHandlerByUDP() @@ -144,38 +153,11 @@ jsonObj.insert("deviceId", deviceId); // kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); } + + // 4. 在界面上简单显示相差数据结果 + emit this->sendDataToDraw(counterData); } } - - /* - QString chFilename("%1_CH_%2.log"); - chFilename = chFilename.arg(devCode); - if (counterData->channelId < 10) - { - chFilename = chFilename.arg(QString("0%1").arg(counterData->channelId)); - } else - { - chFilename = chFilename.arg(counterData->channelId); - } - QString channelDataStr = QString("%1 [%2] %3 %4") - .arg(counterData->timestamp) - .arg(counterData->frameId) - .arg(counterData->channelData) - .arg(counterData->channelClockValue); - QLogUtil::writeChannelDataLogByDate(date, chFilename, channelDataStr); - - // 3. 输出到中间件,执行后续处理过程 - if (SettingConfig::getInstance().NEED_KAFKA == 1) - { - QJsonObject jsonObj = counterData->toJSON(); - jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); - jsonObj.insert("deviceId", deviceId); - kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); - } - */ - - // 4. 在界面上简单显示相差数据结果 - emit this->sendDataToDraw(counterData); } void CounterDevice::pushChannelRawFrame(CounterDataDto * counterData) diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 0840fb2..63262dc 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -130,7 +130,7 @@ dataLabel->setStyleSheet("margin-left:20;"); vbox->addWidget(dataLabel); QLineEdit * dataValue = new QLineEdit(); - dataValue->setFixedWidth(75); + dataValue->setFixedWidth(200); dataValue->setFont(labelFont); vbox->addWidget(dataValue); @@ -202,7 +202,7 @@ } // 1. 判断数据属于哪个设备,显示在不同的widget上 -// qDebug() << counterData->toJSON() << counterData->devCode << "---" << counterData->frameId; + qDebug() << counterData->toJSON() << counterData->devCode << "---" << counterData->frameId; // 更新所有通道BOX的值 for (int i = 0; i < counterData->channelDataArray.size(); i++) diff --git a/CounterAcq/common/utils/QKafkaUtil.cpp b/CounterAcq/common/utils/QKafkaUtil.cpp index 3f00452..b30cf45 100644 --- a/CounterAcq/common/utils/QKafkaUtil.cpp +++ b/CounterAcq/common/utils/QKafkaUtil.cpp @@ -1,4 +1,5 @@ #include "QKafkaUtil.h" +#include "SettingConfig.h" #include QKafkaUtil::QKafkaUtil(QObject *parent) : QObject(parent) @@ -20,6 +21,13 @@ { int result; result = this->conf->set("bootstrap.servers", this->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 (result != RdKafka::Conf::CONF_OK) { diff --git a/CounterAcq/common/utils/SettingConfig.cpp b/CounterAcq/common/utils/SettingConfig.cpp index 1f2ebb2..9a84a43 100644 --- a/CounterAcq/common/utils/SettingConfig.cpp +++ b/CounterAcq/common/utils/SettingConfig.cpp @@ -5,11 +5,14 @@ filename = QApplication::applicationDirPath() + "/conf/config.ini"; setting = new QSettings(this->filename, QSettings::IniFormat); - BAUD_RATE = getProperty("com", "baudRate").toUInt(); + BAUD_RATE = getProperty("com", "baudRate").toInt(); - NEED_KAFKA = getProperty("kafka", "needKafka").toUInt(); + NEED_KAFKA = getProperty("kafka", "needKafka").toInt(); KAFKA_BROKERS = getProperty("kafka", "brokers").toString(); KAFKA_DATA_TOPIC = getProperty("kafka", "dataTopic").toString(); + NEED_SASL = getProperty("kafka", "needSasl").toInt(); + SASL_USERNAME = getProperty("kafka", "saslUsername").toString(); + SASL_PASSWORD = getProperty("kafka", "saslPassword").toString(); CLIENT_ID = getProperty("client", "clientId").toString(); APP_KEY = getProperty("client", "appKey").toString(); diff --git a/CounterAcq/common/utils/SettingConfig.h b/CounterAcq/common/utils/SettingConfig.h index ce1e933..0038ecc 100644 --- a/CounterAcq/common/utils/SettingConfig.h +++ b/CounterAcq/common/utils/SettingConfig.h @@ -32,6 +32,9 @@ int NEED_KAFKA; QString KAFKA_BROKERS; QString KAFKA_DATA_TOPIC; + int NEED_SASL; + QString SASL_USERNAME; + QString SASL_PASSWORD; QString CLIENT_ID; QString APP_KEY; diff --git a/CounterAcq/CounterDevice.cpp b/CounterAcq/CounterDevice.cpp index 9de69d6..46c14df 100644 --- a/CounterAcq/CounterDevice.cpp +++ b/CounterAcq/CounterDevice.cpp @@ -58,40 +58,49 @@ void CounterDevice::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); - std::cout << dataBuff.toStdString() << std::endl; - CounterDataDto * counterData = new CounterDataDto(this); - if (CounterProtocolXH::checkFrame(this->dataBuff) == true) + QList frameList = CounterProtocolXH::extractFrameList(data); + if (frameList.size() > 0) { - counterData->rawFrame = this->dataBuff; -// std::cout << counterData->rawFrame.toStdString() << std::endl; - - // ★解析成数据对象 - bool parse = CounterProtocolXH::parseMessureData(this->dataBuff, counterData); - - // 解析成功 - if (parse == true) + this->dataBuff.clear(); + for (int i = 0; i < frameList.size(); i++) { - // 1. 清空dataBuff,等待下一帧的数据 - this->dataBuff.clear(); + QByteArray frameByte = frameList.at(i); + if (CounterProtocolXH::checkFrame(frameByte) == true) + { + CounterDataDto * counterData = new CounterDataDto(this); - // 2. 补充其他字段 - QDateTime now = QDateTime::currentDateTime(); - counterData->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); - counterData->milisecond = now.toMSecsSinceEpoch(); - counterData->devCode = devCode; + counterData->rawFrame = frameByte; - this->afterFramePhase(counterData); + // ★解析成数据对象 + bool parse = CounterProtocolXH::parseMessureData(frameByte, counterData); - // 3 计算数据ID一样的测量数据的时差值,通道值-参考通道值 -// this->pushChannelRawFrame(counterData); + // 解析成功 + if (parse == true) + { + // 1. 清空dataBuff,等待下一帧的数据 +// this->dataBuff.clear(); + std::cout << "+++" << frameByte.toStdString() << std::endl; + + // 2. 补充其他字段 + QDateTime now = QDateTime::currentDateTime(); + counterData->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); + counterData->milisecond = now.toMSecsSinceEpoch(); + counterData->devCode = devCode; + + this->afterFramePhase(counterData); + + // 3 计算数据ID一样的测量数据的时差值,通道值-参考通道值 + // this->pushChannelRawFrame(counterData); + } + + // 在此处释放内存,不影响后续显示 + // 不在此处释放内存则会导致内存持续增加 + delete counterData; + } } } - - // 在此处释放内存,不影响后续显示 - // 不在此处释放内存则会导致内存持续增加 - delete counterData; } void CounterDevice::dataReceivedHandlerByUDP() @@ -144,38 +153,11 @@ jsonObj.insert("deviceId", deviceId); // kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); } + + // 4. 在界面上简单显示相差数据结果 + emit this->sendDataToDraw(counterData); } } - - /* - QString chFilename("%1_CH_%2.log"); - chFilename = chFilename.arg(devCode); - if (counterData->channelId < 10) - { - chFilename = chFilename.arg(QString("0%1").arg(counterData->channelId)); - } else - { - chFilename = chFilename.arg(counterData->channelId); - } - QString channelDataStr = QString("%1 [%2] %3 %4") - .arg(counterData->timestamp) - .arg(counterData->frameId) - .arg(counterData->channelData) - .arg(counterData->channelClockValue); - QLogUtil::writeChannelDataLogByDate(date, chFilename, channelDataStr); - - // 3. 输出到中间件,执行后续处理过程 - if (SettingConfig::getInstance().NEED_KAFKA == 1) - { - QJsonObject jsonObj = counterData->toJSON(); - jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); - jsonObj.insert("deviceId", deviceId); - kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); - } - */ - - // 4. 在界面上简单显示相差数据结果 - emit this->sendDataToDraw(counterData); } void CounterDevice::pushChannelRawFrame(CounterDataDto * counterData) diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 0840fb2..63262dc 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -130,7 +130,7 @@ dataLabel->setStyleSheet("margin-left:20;"); vbox->addWidget(dataLabel); QLineEdit * dataValue = new QLineEdit(); - dataValue->setFixedWidth(75); + dataValue->setFixedWidth(200); dataValue->setFont(labelFont); vbox->addWidget(dataValue); @@ -202,7 +202,7 @@ } // 1. 判断数据属于哪个设备,显示在不同的widget上 -// qDebug() << counterData->toJSON() << counterData->devCode << "---" << counterData->frameId; + qDebug() << counterData->toJSON() << counterData->devCode << "---" << counterData->frameId; // 更新所有通道BOX的值 for (int i = 0; i < counterData->channelDataArray.size(); i++) diff --git a/CounterAcq/common/utils/QKafkaUtil.cpp b/CounterAcq/common/utils/QKafkaUtil.cpp index 3f00452..b30cf45 100644 --- a/CounterAcq/common/utils/QKafkaUtil.cpp +++ b/CounterAcq/common/utils/QKafkaUtil.cpp @@ -1,4 +1,5 @@ #include "QKafkaUtil.h" +#include "SettingConfig.h" #include QKafkaUtil::QKafkaUtil(QObject *parent) : QObject(parent) @@ -20,6 +21,13 @@ { int result; result = this->conf->set("bootstrap.servers", this->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 (result != RdKafka::Conf::CONF_OK) { diff --git a/CounterAcq/common/utils/SettingConfig.cpp b/CounterAcq/common/utils/SettingConfig.cpp index 1f2ebb2..9a84a43 100644 --- a/CounterAcq/common/utils/SettingConfig.cpp +++ b/CounterAcq/common/utils/SettingConfig.cpp @@ -5,11 +5,14 @@ filename = QApplication::applicationDirPath() + "/conf/config.ini"; setting = new QSettings(this->filename, QSettings::IniFormat); - BAUD_RATE = getProperty("com", "baudRate").toUInt(); + BAUD_RATE = getProperty("com", "baudRate").toInt(); - NEED_KAFKA = getProperty("kafka", "needKafka").toUInt(); + NEED_KAFKA = getProperty("kafka", "needKafka").toInt(); KAFKA_BROKERS = getProperty("kafka", "brokers").toString(); KAFKA_DATA_TOPIC = getProperty("kafka", "dataTopic").toString(); + NEED_SASL = getProperty("kafka", "needSasl").toInt(); + SASL_USERNAME = getProperty("kafka", "saslUsername").toString(); + SASL_PASSWORD = getProperty("kafka", "saslPassword").toString(); CLIENT_ID = getProperty("client", "clientId").toString(); APP_KEY = getProperty("client", "appKey").toString(); diff --git a/CounterAcq/common/utils/SettingConfig.h b/CounterAcq/common/utils/SettingConfig.h index ce1e933..0038ecc 100644 --- a/CounterAcq/common/utils/SettingConfig.h +++ b/CounterAcq/common/utils/SettingConfig.h @@ -32,6 +32,9 @@ int NEED_KAFKA; QString KAFKA_BROKERS; QString KAFKA_DATA_TOPIC; + int NEED_SASL; + QString SASL_USERNAME; + QString SASL_PASSWORD; QString CLIENT_ID; QString APP_KEY; diff --git a/CounterAcq/protocol/CounterProtocolXH.cpp b/CounterAcq/protocol/CounterProtocolXH.cpp index 45d164c..6ba3e50 100644 --- a/CounterAcq/protocol/CounterProtocolXH.cpp +++ b/CounterAcq/protocol/CounterProtocolXH.cpp @@ -1,4 +1,5 @@ #include "CounterProtocolXH.h" +#include CounterProtocolXH::CounterProtocolXH(QObject *parent) : QObject(parent) { @@ -26,19 +27,58 @@ for (int i = 0; i < COUNTER_MESSURE_CHANNEL; i++) { // 通道1的有效性在最后 - counterData->channelActiveArray.append(channelStatus.mid(COUNTER_MESSURE_CHANNEL - i, 1)); - if (channelStatus.mid(COUNTER_MESSURE_CHANNEL - i, 1) == "0") + counterData->channelActiveArray.append(channelStatus.mid(COUNTER_MESSURE_CHANNEL - i - 1, 1)); + if (channelStatus.mid(COUNTER_MESSURE_CHANNEL - i - 1, 1) == "0") { - counterData->channelDataArray.append(0.00); + counterData->channelDataArray.append("0.00"); } else { - counterData->channelDataArray.append(subList.at(i + 1).toDouble()); + counterData->channelDataArray.append(subList.at(i + 1)); } } return true; } +QList CounterProtocolXH::extractFrameList(QByteArray rawData) +{ + QList resultList; + int head = rawData.indexOf(COUNTER_FRAME_HEAD); + if (head < 0) + { + return resultList; + } + + rawData = rawData.mid(head, rawData.size() - head + 2); + std::cout << rawData.toStdString() << std::endl; + + if (rawData.startsWith("$XHTS") == true) + { + QByteArray ba; + for (int i = 0; i < rawData.size() - 1; i++) + { + if (rawData.at(i) != '\r') + { + ba.append(rawData.at(i)); + } else + { + if (rawData.at(i + 1) == '\n') + { + ba.append("\r\n"); + resultList.append(ba); + ba.clear(); + i++; + } else + { + ba.append(rawData.at(i)); + } + } + } + } + + return resultList; +} + bool CounterProtocolXH::checkFrame(QByteArray rawData) { @@ -48,7 +88,7 @@ return false; } - // 帧头不是$GL + // 帧头不是$XHTS if (COUNTER_FRAME_HEAD.toLocal8Bit() != rawData.mid(0, COUNTER_FRAME_HEAD.size())) { return false; diff --git a/CounterAcq/CounterDevice.cpp b/CounterAcq/CounterDevice.cpp index 9de69d6..46c14df 100644 --- a/CounterAcq/CounterDevice.cpp +++ b/CounterAcq/CounterDevice.cpp @@ -58,40 +58,49 @@ void CounterDevice::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); - std::cout << dataBuff.toStdString() << std::endl; - CounterDataDto * counterData = new CounterDataDto(this); - if (CounterProtocolXH::checkFrame(this->dataBuff) == true) + QList frameList = CounterProtocolXH::extractFrameList(data); + if (frameList.size() > 0) { - counterData->rawFrame = this->dataBuff; -// std::cout << counterData->rawFrame.toStdString() << std::endl; - - // ★解析成数据对象 - bool parse = CounterProtocolXH::parseMessureData(this->dataBuff, counterData); - - // 解析成功 - if (parse == true) + this->dataBuff.clear(); + for (int i = 0; i < frameList.size(); i++) { - // 1. 清空dataBuff,等待下一帧的数据 - this->dataBuff.clear(); + QByteArray frameByte = frameList.at(i); + if (CounterProtocolXH::checkFrame(frameByte) == true) + { + CounterDataDto * counterData = new CounterDataDto(this); - // 2. 补充其他字段 - QDateTime now = QDateTime::currentDateTime(); - counterData->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); - counterData->milisecond = now.toMSecsSinceEpoch(); - counterData->devCode = devCode; + counterData->rawFrame = frameByte; - this->afterFramePhase(counterData); + // ★解析成数据对象 + bool parse = CounterProtocolXH::parseMessureData(frameByte, counterData); - // 3 计算数据ID一样的测量数据的时差值,通道值-参考通道值 -// this->pushChannelRawFrame(counterData); + // 解析成功 + if (parse == true) + { + // 1. 清空dataBuff,等待下一帧的数据 +// this->dataBuff.clear(); + std::cout << "+++" << frameByte.toStdString() << std::endl; + + // 2. 补充其他字段 + QDateTime now = QDateTime::currentDateTime(); + counterData->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); + counterData->milisecond = now.toMSecsSinceEpoch(); + counterData->devCode = devCode; + + this->afterFramePhase(counterData); + + // 3 计算数据ID一样的测量数据的时差值,通道值-参考通道值 + // this->pushChannelRawFrame(counterData); + } + + // 在此处释放内存,不影响后续显示 + // 不在此处释放内存则会导致内存持续增加 + delete counterData; + } } } - - // 在此处释放内存,不影响后续显示 - // 不在此处释放内存则会导致内存持续增加 - delete counterData; } void CounterDevice::dataReceivedHandlerByUDP() @@ -144,38 +153,11 @@ jsonObj.insert("deviceId", deviceId); // kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); } + + // 4. 在界面上简单显示相差数据结果 + emit this->sendDataToDraw(counterData); } } - - /* - QString chFilename("%1_CH_%2.log"); - chFilename = chFilename.arg(devCode); - if (counterData->channelId < 10) - { - chFilename = chFilename.arg(QString("0%1").arg(counterData->channelId)); - } else - { - chFilename = chFilename.arg(counterData->channelId); - } - QString channelDataStr = QString("%1 [%2] %3 %4") - .arg(counterData->timestamp) - .arg(counterData->frameId) - .arg(counterData->channelData) - .arg(counterData->channelClockValue); - QLogUtil::writeChannelDataLogByDate(date, chFilename, channelDataStr); - - // 3. 输出到中间件,执行后续处理过程 - if (SettingConfig::getInstance().NEED_KAFKA == 1) - { - QJsonObject jsonObj = counterData->toJSON(); - jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); - jsonObj.insert("deviceId", deviceId); - kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); - } - */ - - // 4. 在界面上简单显示相差数据结果 - emit this->sendDataToDraw(counterData); } void CounterDevice::pushChannelRawFrame(CounterDataDto * counterData) diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 0840fb2..63262dc 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -130,7 +130,7 @@ dataLabel->setStyleSheet("margin-left:20;"); vbox->addWidget(dataLabel); QLineEdit * dataValue = new QLineEdit(); - dataValue->setFixedWidth(75); + dataValue->setFixedWidth(200); dataValue->setFont(labelFont); vbox->addWidget(dataValue); @@ -202,7 +202,7 @@ } // 1. 判断数据属于哪个设备,显示在不同的widget上 -// qDebug() << counterData->toJSON() << counterData->devCode << "---" << counterData->frameId; + qDebug() << counterData->toJSON() << counterData->devCode << "---" << counterData->frameId; // 更新所有通道BOX的值 for (int i = 0; i < counterData->channelDataArray.size(); i++) diff --git a/CounterAcq/common/utils/QKafkaUtil.cpp b/CounterAcq/common/utils/QKafkaUtil.cpp index 3f00452..b30cf45 100644 --- a/CounterAcq/common/utils/QKafkaUtil.cpp +++ b/CounterAcq/common/utils/QKafkaUtil.cpp @@ -1,4 +1,5 @@ #include "QKafkaUtil.h" +#include "SettingConfig.h" #include QKafkaUtil::QKafkaUtil(QObject *parent) : QObject(parent) @@ -20,6 +21,13 @@ { int result; result = this->conf->set("bootstrap.servers", this->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 (result != RdKafka::Conf::CONF_OK) { diff --git a/CounterAcq/common/utils/SettingConfig.cpp b/CounterAcq/common/utils/SettingConfig.cpp index 1f2ebb2..9a84a43 100644 --- a/CounterAcq/common/utils/SettingConfig.cpp +++ b/CounterAcq/common/utils/SettingConfig.cpp @@ -5,11 +5,14 @@ filename = QApplication::applicationDirPath() + "/conf/config.ini"; setting = new QSettings(this->filename, QSettings::IniFormat); - BAUD_RATE = getProperty("com", "baudRate").toUInt(); + BAUD_RATE = getProperty("com", "baudRate").toInt(); - NEED_KAFKA = getProperty("kafka", "needKafka").toUInt(); + NEED_KAFKA = getProperty("kafka", "needKafka").toInt(); KAFKA_BROKERS = getProperty("kafka", "brokers").toString(); KAFKA_DATA_TOPIC = getProperty("kafka", "dataTopic").toString(); + NEED_SASL = getProperty("kafka", "needSasl").toInt(); + SASL_USERNAME = getProperty("kafka", "saslUsername").toString(); + SASL_PASSWORD = getProperty("kafka", "saslPassword").toString(); CLIENT_ID = getProperty("client", "clientId").toString(); APP_KEY = getProperty("client", "appKey").toString(); diff --git a/CounterAcq/common/utils/SettingConfig.h b/CounterAcq/common/utils/SettingConfig.h index ce1e933..0038ecc 100644 --- a/CounterAcq/common/utils/SettingConfig.h +++ b/CounterAcq/common/utils/SettingConfig.h @@ -32,6 +32,9 @@ int NEED_KAFKA; QString KAFKA_BROKERS; QString KAFKA_DATA_TOPIC; + int NEED_SASL; + QString SASL_USERNAME; + QString SASL_PASSWORD; QString CLIENT_ID; QString APP_KEY; diff --git a/CounterAcq/protocol/CounterProtocolXH.cpp b/CounterAcq/protocol/CounterProtocolXH.cpp index 45d164c..6ba3e50 100644 --- a/CounterAcq/protocol/CounterProtocolXH.cpp +++ b/CounterAcq/protocol/CounterProtocolXH.cpp @@ -1,4 +1,5 @@ #include "CounterProtocolXH.h" +#include CounterProtocolXH::CounterProtocolXH(QObject *parent) : QObject(parent) { @@ -26,19 +27,58 @@ for (int i = 0; i < COUNTER_MESSURE_CHANNEL; i++) { // 通道1的有效性在最后 - counterData->channelActiveArray.append(channelStatus.mid(COUNTER_MESSURE_CHANNEL - i, 1)); - if (channelStatus.mid(COUNTER_MESSURE_CHANNEL - i, 1) == "0") + counterData->channelActiveArray.append(channelStatus.mid(COUNTER_MESSURE_CHANNEL - i - 1, 1)); + if (channelStatus.mid(COUNTER_MESSURE_CHANNEL - i - 1, 1) == "0") { - counterData->channelDataArray.append(0.00); + counterData->channelDataArray.append("0.00"); } else { - counterData->channelDataArray.append(subList.at(i + 1).toDouble()); + counterData->channelDataArray.append(subList.at(i + 1)); } } return true; } +QList CounterProtocolXH::extractFrameList(QByteArray rawData) +{ + QList resultList; + int head = rawData.indexOf(COUNTER_FRAME_HEAD); + if (head < 0) + { + return resultList; + } + + rawData = rawData.mid(head, rawData.size() - head + 2); + std::cout << rawData.toStdString() << std::endl; + + if (rawData.startsWith("$XHTS") == true) + { + QByteArray ba; + for (int i = 0; i < rawData.size() - 1; i++) + { + if (rawData.at(i) != '\r') + { + ba.append(rawData.at(i)); + } else + { + if (rawData.at(i + 1) == '\n') + { + ba.append("\r\n"); + resultList.append(ba); + ba.clear(); + i++; + } else + { + ba.append(rawData.at(i)); + } + } + } + } + + return resultList; +} + bool CounterProtocolXH::checkFrame(QByteArray rawData) { @@ -48,7 +88,7 @@ return false; } - // 帧头不是$GL + // 帧头不是$XHTS if (COUNTER_FRAME_HEAD.toLocal8Bit() != rawData.mid(0, COUNTER_FRAME_HEAD.size())) { return false; diff --git a/CounterAcq/protocol/CounterProtocolXH.h b/CounterAcq/protocol/CounterProtocolXH.h index 9af82f5..7c9ccd4 100644 --- a/CounterAcq/protocol/CounterProtocolXH.h +++ b/CounterAcq/protocol/CounterProtocolXH.h @@ -1,4 +1,4 @@ -#ifndef COUNTERPROTOCOLXH_H +#ifndef COUNTERPROTOCOLXH_H #define COUNTERPROTOCOLXH_H #include @@ -28,6 +28,7 @@ // 解析计数器测量数据 static bool parseMessureData(QByteArray rawData, CounterDataDto * counterData); + static QList extractFrameList(QByteArray rawData); // 检测帧格式,帧头帧尾 static bool checkFrame(QByteArray rawData); diff --git a/CounterAcq/CounterDevice.cpp b/CounterAcq/CounterDevice.cpp index 9de69d6..46c14df 100644 --- a/CounterAcq/CounterDevice.cpp +++ b/CounterAcq/CounterDevice.cpp @@ -58,40 +58,49 @@ void CounterDevice::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); - std::cout << dataBuff.toStdString() << std::endl; - CounterDataDto * counterData = new CounterDataDto(this); - if (CounterProtocolXH::checkFrame(this->dataBuff) == true) + QList frameList = CounterProtocolXH::extractFrameList(data); + if (frameList.size() > 0) { - counterData->rawFrame = this->dataBuff; -// std::cout << counterData->rawFrame.toStdString() << std::endl; - - // ★解析成数据对象 - bool parse = CounterProtocolXH::parseMessureData(this->dataBuff, counterData); - - // 解析成功 - if (parse == true) + this->dataBuff.clear(); + for (int i = 0; i < frameList.size(); i++) { - // 1. 清空dataBuff,等待下一帧的数据 - this->dataBuff.clear(); + QByteArray frameByte = frameList.at(i); + if (CounterProtocolXH::checkFrame(frameByte) == true) + { + CounterDataDto * counterData = new CounterDataDto(this); - // 2. 补充其他字段 - QDateTime now = QDateTime::currentDateTime(); - counterData->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); - counterData->milisecond = now.toMSecsSinceEpoch(); - counterData->devCode = devCode; + counterData->rawFrame = frameByte; - this->afterFramePhase(counterData); + // ★解析成数据对象 + bool parse = CounterProtocolXH::parseMessureData(frameByte, counterData); - // 3 计算数据ID一样的测量数据的时差值,通道值-参考通道值 -// this->pushChannelRawFrame(counterData); + // 解析成功 + if (parse == true) + { + // 1. 清空dataBuff,等待下一帧的数据 +// this->dataBuff.clear(); + std::cout << "+++" << frameByte.toStdString() << std::endl; + + // 2. 补充其他字段 + QDateTime now = QDateTime::currentDateTime(); + counterData->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); + counterData->milisecond = now.toMSecsSinceEpoch(); + counterData->devCode = devCode; + + this->afterFramePhase(counterData); + + // 3 计算数据ID一样的测量数据的时差值,通道值-参考通道值 + // this->pushChannelRawFrame(counterData); + } + + // 在此处释放内存,不影响后续显示 + // 不在此处释放内存则会导致内存持续增加 + delete counterData; + } } } - - // 在此处释放内存,不影响后续显示 - // 不在此处释放内存则会导致内存持续增加 - delete counterData; } void CounterDevice::dataReceivedHandlerByUDP() @@ -144,38 +153,11 @@ jsonObj.insert("deviceId", deviceId); // kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); } + + // 4. 在界面上简单显示相差数据结果 + emit this->sendDataToDraw(counterData); } } - - /* - QString chFilename("%1_CH_%2.log"); - chFilename = chFilename.arg(devCode); - if (counterData->channelId < 10) - { - chFilename = chFilename.arg(QString("0%1").arg(counterData->channelId)); - } else - { - chFilename = chFilename.arg(counterData->channelId); - } - QString channelDataStr = QString("%1 [%2] %3 %4") - .arg(counterData->timestamp) - .arg(counterData->frameId) - .arg(counterData->channelData) - .arg(counterData->channelClockValue); - QLogUtil::writeChannelDataLogByDate(date, chFilename, channelDataStr); - - // 3. 输出到中间件,执行后续处理过程 - if (SettingConfig::getInstance().NEED_KAFKA == 1) - { - QJsonObject jsonObj = counterData->toJSON(); - jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); - jsonObj.insert("deviceId", deviceId); - kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); - } - */ - - // 4. 在界面上简单显示相差数据结果 - emit this->sendDataToDraw(counterData); } void CounterDevice::pushChannelRawFrame(CounterDataDto * counterData) diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 0840fb2..63262dc 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -130,7 +130,7 @@ dataLabel->setStyleSheet("margin-left:20;"); vbox->addWidget(dataLabel); QLineEdit * dataValue = new QLineEdit(); - dataValue->setFixedWidth(75); + dataValue->setFixedWidth(200); dataValue->setFont(labelFont); vbox->addWidget(dataValue); @@ -202,7 +202,7 @@ } // 1. 判断数据属于哪个设备,显示在不同的widget上 -// qDebug() << counterData->toJSON() << counterData->devCode << "---" << counterData->frameId; + qDebug() << counterData->toJSON() << counterData->devCode << "---" << counterData->frameId; // 更新所有通道BOX的值 for (int i = 0; i < counterData->channelDataArray.size(); i++) diff --git a/CounterAcq/common/utils/QKafkaUtil.cpp b/CounterAcq/common/utils/QKafkaUtil.cpp index 3f00452..b30cf45 100644 --- a/CounterAcq/common/utils/QKafkaUtil.cpp +++ b/CounterAcq/common/utils/QKafkaUtil.cpp @@ -1,4 +1,5 @@ #include "QKafkaUtil.h" +#include "SettingConfig.h" #include QKafkaUtil::QKafkaUtil(QObject *parent) : QObject(parent) @@ -20,6 +21,13 @@ { int result; result = this->conf->set("bootstrap.servers", this->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 (result != RdKafka::Conf::CONF_OK) { diff --git a/CounterAcq/common/utils/SettingConfig.cpp b/CounterAcq/common/utils/SettingConfig.cpp index 1f2ebb2..9a84a43 100644 --- a/CounterAcq/common/utils/SettingConfig.cpp +++ b/CounterAcq/common/utils/SettingConfig.cpp @@ -5,11 +5,14 @@ filename = QApplication::applicationDirPath() + "/conf/config.ini"; setting = new QSettings(this->filename, QSettings::IniFormat); - BAUD_RATE = getProperty("com", "baudRate").toUInt(); + BAUD_RATE = getProperty("com", "baudRate").toInt(); - NEED_KAFKA = getProperty("kafka", "needKafka").toUInt(); + NEED_KAFKA = getProperty("kafka", "needKafka").toInt(); KAFKA_BROKERS = getProperty("kafka", "brokers").toString(); KAFKA_DATA_TOPIC = getProperty("kafka", "dataTopic").toString(); + NEED_SASL = getProperty("kafka", "needSasl").toInt(); + SASL_USERNAME = getProperty("kafka", "saslUsername").toString(); + SASL_PASSWORD = getProperty("kafka", "saslPassword").toString(); CLIENT_ID = getProperty("client", "clientId").toString(); APP_KEY = getProperty("client", "appKey").toString(); diff --git a/CounterAcq/common/utils/SettingConfig.h b/CounterAcq/common/utils/SettingConfig.h index ce1e933..0038ecc 100644 --- a/CounterAcq/common/utils/SettingConfig.h +++ b/CounterAcq/common/utils/SettingConfig.h @@ -32,6 +32,9 @@ int NEED_KAFKA; QString KAFKA_BROKERS; QString KAFKA_DATA_TOPIC; + int NEED_SASL; + QString SASL_USERNAME; + QString SASL_PASSWORD; QString CLIENT_ID; QString APP_KEY; diff --git a/CounterAcq/protocol/CounterProtocolXH.cpp b/CounterAcq/protocol/CounterProtocolXH.cpp index 45d164c..6ba3e50 100644 --- a/CounterAcq/protocol/CounterProtocolXH.cpp +++ b/CounterAcq/protocol/CounterProtocolXH.cpp @@ -1,4 +1,5 @@ #include "CounterProtocolXH.h" +#include CounterProtocolXH::CounterProtocolXH(QObject *parent) : QObject(parent) { @@ -26,19 +27,58 @@ for (int i = 0; i < COUNTER_MESSURE_CHANNEL; i++) { // 通道1的有效性在最后 - counterData->channelActiveArray.append(channelStatus.mid(COUNTER_MESSURE_CHANNEL - i, 1)); - if (channelStatus.mid(COUNTER_MESSURE_CHANNEL - i, 1) == "0") + counterData->channelActiveArray.append(channelStatus.mid(COUNTER_MESSURE_CHANNEL - i - 1, 1)); + if (channelStatus.mid(COUNTER_MESSURE_CHANNEL - i - 1, 1) == "0") { - counterData->channelDataArray.append(0.00); + counterData->channelDataArray.append("0.00"); } else { - counterData->channelDataArray.append(subList.at(i + 1).toDouble()); + counterData->channelDataArray.append(subList.at(i + 1)); } } return true; } +QList CounterProtocolXH::extractFrameList(QByteArray rawData) +{ + QList resultList; + int head = rawData.indexOf(COUNTER_FRAME_HEAD); + if (head < 0) + { + return resultList; + } + + rawData = rawData.mid(head, rawData.size() - head + 2); + std::cout << rawData.toStdString() << std::endl; + + if (rawData.startsWith("$XHTS") == true) + { + QByteArray ba; + for (int i = 0; i < rawData.size() - 1; i++) + { + if (rawData.at(i) != '\r') + { + ba.append(rawData.at(i)); + } else + { + if (rawData.at(i + 1) == '\n') + { + ba.append("\r\n"); + resultList.append(ba); + ba.clear(); + i++; + } else + { + ba.append(rawData.at(i)); + } + } + } + } + + return resultList; +} + bool CounterProtocolXH::checkFrame(QByteArray rawData) { @@ -48,7 +88,7 @@ return false; } - // 帧头不是$GL + // 帧头不是$XHTS if (COUNTER_FRAME_HEAD.toLocal8Bit() != rawData.mid(0, COUNTER_FRAME_HEAD.size())) { return false; diff --git a/CounterAcq/protocol/CounterProtocolXH.h b/CounterAcq/protocol/CounterProtocolXH.h index 9af82f5..7c9ccd4 100644 --- a/CounterAcq/protocol/CounterProtocolXH.h +++ b/CounterAcq/protocol/CounterProtocolXH.h @@ -1,4 +1,4 @@ -#ifndef COUNTERPROTOCOLXH_H +#ifndef COUNTERPROTOCOLXH_H #define COUNTERPROTOCOLXH_H #include @@ -28,6 +28,7 @@ // 解析计数器测量数据 static bool parseMessureData(QByteArray rawData, CounterDataDto * counterData); + static QList extractFrameList(QByteArray rawData); // 检测帧格式,帧头帧尾 static bool checkFrame(QByteArray rawData); diff --git a/CounterAcq/protocol/dto/CounterDataDto.h b/CounterAcq/protocol/dto/CounterDataDto.h index ff19375..60aaaae 100644 --- a/CounterAcq/protocol/dto/CounterDataDto.h +++ b/CounterAcq/protocol/dto/CounterDataDto.h @@ -23,7 +23,7 @@ double channelClockValue; // QList channelActiveArray; // xhts - QList channelDataArray; // xhts + QList channelDataArray; // xhts QByteArray rawFrame; // 原始帧字节数组 diff --git a/CounterAcq/CounterDevice.cpp b/CounterAcq/CounterDevice.cpp index 9de69d6..46c14df 100644 --- a/CounterAcq/CounterDevice.cpp +++ b/CounterAcq/CounterDevice.cpp @@ -58,40 +58,49 @@ void CounterDevice::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); - std::cout << dataBuff.toStdString() << std::endl; - CounterDataDto * counterData = new CounterDataDto(this); - if (CounterProtocolXH::checkFrame(this->dataBuff) == true) + QList frameList = CounterProtocolXH::extractFrameList(data); + if (frameList.size() > 0) { - counterData->rawFrame = this->dataBuff; -// std::cout << counterData->rawFrame.toStdString() << std::endl; - - // ★解析成数据对象 - bool parse = CounterProtocolXH::parseMessureData(this->dataBuff, counterData); - - // 解析成功 - if (parse == true) + this->dataBuff.clear(); + for (int i = 0; i < frameList.size(); i++) { - // 1. 清空dataBuff,等待下一帧的数据 - this->dataBuff.clear(); + QByteArray frameByte = frameList.at(i); + if (CounterProtocolXH::checkFrame(frameByte) == true) + { + CounterDataDto * counterData = new CounterDataDto(this); - // 2. 补充其他字段 - QDateTime now = QDateTime::currentDateTime(); - counterData->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); - counterData->milisecond = now.toMSecsSinceEpoch(); - counterData->devCode = devCode; + counterData->rawFrame = frameByte; - this->afterFramePhase(counterData); + // ★解析成数据对象 + bool parse = CounterProtocolXH::parseMessureData(frameByte, counterData); - // 3 计算数据ID一样的测量数据的时差值,通道值-参考通道值 -// this->pushChannelRawFrame(counterData); + // 解析成功 + if (parse == true) + { + // 1. 清空dataBuff,等待下一帧的数据 +// this->dataBuff.clear(); + std::cout << "+++" << frameByte.toStdString() << std::endl; + + // 2. 补充其他字段 + QDateTime now = QDateTime::currentDateTime(); + counterData->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); + counterData->milisecond = now.toMSecsSinceEpoch(); + counterData->devCode = devCode; + + this->afterFramePhase(counterData); + + // 3 计算数据ID一样的测量数据的时差值,通道值-参考通道值 + // this->pushChannelRawFrame(counterData); + } + + // 在此处释放内存,不影响后续显示 + // 不在此处释放内存则会导致内存持续增加 + delete counterData; + } } } - - // 在此处释放内存,不影响后续显示 - // 不在此处释放内存则会导致内存持续增加 - delete counterData; } void CounterDevice::dataReceivedHandlerByUDP() @@ -144,38 +153,11 @@ jsonObj.insert("deviceId", deviceId); // kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); } + + // 4. 在界面上简单显示相差数据结果 + emit this->sendDataToDraw(counterData); } } - - /* - QString chFilename("%1_CH_%2.log"); - chFilename = chFilename.arg(devCode); - if (counterData->channelId < 10) - { - chFilename = chFilename.arg(QString("0%1").arg(counterData->channelId)); - } else - { - chFilename = chFilename.arg(counterData->channelId); - } - QString channelDataStr = QString("%1 [%2] %3 %4") - .arg(counterData->timestamp) - .arg(counterData->frameId) - .arg(counterData->channelData) - .arg(counterData->channelClockValue); - QLogUtil::writeChannelDataLogByDate(date, chFilename, channelDataStr); - - // 3. 输出到中间件,执行后续处理过程 - if (SettingConfig::getInstance().NEED_KAFKA == 1) - { - QJsonObject jsonObj = counterData->toJSON(); - jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); - jsonObj.insert("deviceId", deviceId); - kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); - } - */ - - // 4. 在界面上简单显示相差数据结果 - emit this->sendDataToDraw(counterData); } void CounterDevice::pushChannelRawFrame(CounterDataDto * counterData) diff --git a/CounterAcq/CounterWindow.cpp b/CounterAcq/CounterWindow.cpp index 0840fb2..63262dc 100644 --- a/CounterAcq/CounterWindow.cpp +++ b/CounterAcq/CounterWindow.cpp @@ -130,7 +130,7 @@ dataLabel->setStyleSheet("margin-left:20;"); vbox->addWidget(dataLabel); QLineEdit * dataValue = new QLineEdit(); - dataValue->setFixedWidth(75); + dataValue->setFixedWidth(200); dataValue->setFont(labelFont); vbox->addWidget(dataValue); @@ -202,7 +202,7 @@ } // 1. 判断数据属于哪个设备,显示在不同的widget上 -// qDebug() << counterData->toJSON() << counterData->devCode << "---" << counterData->frameId; + qDebug() << counterData->toJSON() << counterData->devCode << "---" << counterData->frameId; // 更新所有通道BOX的值 for (int i = 0; i < counterData->channelDataArray.size(); i++) diff --git a/CounterAcq/common/utils/QKafkaUtil.cpp b/CounterAcq/common/utils/QKafkaUtil.cpp index 3f00452..b30cf45 100644 --- a/CounterAcq/common/utils/QKafkaUtil.cpp +++ b/CounterAcq/common/utils/QKafkaUtil.cpp @@ -1,4 +1,5 @@ #include "QKafkaUtil.h" +#include "SettingConfig.h" #include QKafkaUtil::QKafkaUtil(QObject *parent) : QObject(parent) @@ -20,6 +21,13 @@ { int result; result = this->conf->set("bootstrap.servers", this->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 (result != RdKafka::Conf::CONF_OK) { diff --git a/CounterAcq/common/utils/SettingConfig.cpp b/CounterAcq/common/utils/SettingConfig.cpp index 1f2ebb2..9a84a43 100644 --- a/CounterAcq/common/utils/SettingConfig.cpp +++ b/CounterAcq/common/utils/SettingConfig.cpp @@ -5,11 +5,14 @@ filename = QApplication::applicationDirPath() + "/conf/config.ini"; setting = new QSettings(this->filename, QSettings::IniFormat); - BAUD_RATE = getProperty("com", "baudRate").toUInt(); + BAUD_RATE = getProperty("com", "baudRate").toInt(); - NEED_KAFKA = getProperty("kafka", "needKafka").toUInt(); + NEED_KAFKA = getProperty("kafka", "needKafka").toInt(); KAFKA_BROKERS = getProperty("kafka", "brokers").toString(); KAFKA_DATA_TOPIC = getProperty("kafka", "dataTopic").toString(); + NEED_SASL = getProperty("kafka", "needSasl").toInt(); + SASL_USERNAME = getProperty("kafka", "saslUsername").toString(); + SASL_PASSWORD = getProperty("kafka", "saslPassword").toString(); CLIENT_ID = getProperty("client", "clientId").toString(); APP_KEY = getProperty("client", "appKey").toString(); diff --git a/CounterAcq/common/utils/SettingConfig.h b/CounterAcq/common/utils/SettingConfig.h index ce1e933..0038ecc 100644 --- a/CounterAcq/common/utils/SettingConfig.h +++ b/CounterAcq/common/utils/SettingConfig.h @@ -32,6 +32,9 @@ int NEED_KAFKA; QString KAFKA_BROKERS; QString KAFKA_DATA_TOPIC; + int NEED_SASL; + QString SASL_USERNAME; + QString SASL_PASSWORD; QString CLIENT_ID; QString APP_KEY; diff --git a/CounterAcq/protocol/CounterProtocolXH.cpp b/CounterAcq/protocol/CounterProtocolXH.cpp index 45d164c..6ba3e50 100644 --- a/CounterAcq/protocol/CounterProtocolXH.cpp +++ b/CounterAcq/protocol/CounterProtocolXH.cpp @@ -1,4 +1,5 @@ #include "CounterProtocolXH.h" +#include CounterProtocolXH::CounterProtocolXH(QObject *parent) : QObject(parent) { @@ -26,19 +27,58 @@ for (int i = 0; i < COUNTER_MESSURE_CHANNEL; i++) { // 通道1的有效性在最后 - counterData->channelActiveArray.append(channelStatus.mid(COUNTER_MESSURE_CHANNEL - i, 1)); - if (channelStatus.mid(COUNTER_MESSURE_CHANNEL - i, 1) == "0") + counterData->channelActiveArray.append(channelStatus.mid(COUNTER_MESSURE_CHANNEL - i - 1, 1)); + if (channelStatus.mid(COUNTER_MESSURE_CHANNEL - i - 1, 1) == "0") { - counterData->channelDataArray.append(0.00); + counterData->channelDataArray.append("0.00"); } else { - counterData->channelDataArray.append(subList.at(i + 1).toDouble()); + counterData->channelDataArray.append(subList.at(i + 1)); } } return true; } +QList CounterProtocolXH::extractFrameList(QByteArray rawData) +{ + QList resultList; + int head = rawData.indexOf(COUNTER_FRAME_HEAD); + if (head < 0) + { + return resultList; + } + + rawData = rawData.mid(head, rawData.size() - head + 2); + std::cout << rawData.toStdString() << std::endl; + + if (rawData.startsWith("$XHTS") == true) + { + QByteArray ba; + for (int i = 0; i < rawData.size() - 1; i++) + { + if (rawData.at(i) != '\r') + { + ba.append(rawData.at(i)); + } else + { + if (rawData.at(i + 1) == '\n') + { + ba.append("\r\n"); + resultList.append(ba); + ba.clear(); + i++; + } else + { + ba.append(rawData.at(i)); + } + } + } + } + + return resultList; +} + bool CounterProtocolXH::checkFrame(QByteArray rawData) { @@ -48,7 +88,7 @@ return false; } - // 帧头不是$GL + // 帧头不是$XHTS if (COUNTER_FRAME_HEAD.toLocal8Bit() != rawData.mid(0, COUNTER_FRAME_HEAD.size())) { return false; diff --git a/CounterAcq/protocol/CounterProtocolXH.h b/CounterAcq/protocol/CounterProtocolXH.h index 9af82f5..7c9ccd4 100644 --- a/CounterAcq/protocol/CounterProtocolXH.h +++ b/CounterAcq/protocol/CounterProtocolXH.h @@ -1,4 +1,4 @@ -#ifndef COUNTERPROTOCOLXH_H +#ifndef COUNTERPROTOCOLXH_H #define COUNTERPROTOCOLXH_H #include @@ -28,6 +28,7 @@ // 解析计数器测量数据 static bool parseMessureData(QByteArray rawData, CounterDataDto * counterData); + static QList extractFrameList(QByteArray rawData); // 检测帧格式,帧头帧尾 static bool checkFrame(QByteArray rawData); diff --git a/CounterAcq/protocol/dto/CounterDataDto.h b/CounterAcq/protocol/dto/CounterDataDto.h index ff19375..60aaaae 100644 --- a/CounterAcq/protocol/dto/CounterDataDto.h +++ b/CounterAcq/protocol/dto/CounterDataDto.h @@ -23,7 +23,7 @@ double channelClockValue; // QList channelActiveArray; // xhts - QList channelDataArray; // xhts + QList channelDataArray; // xhts QByteArray rawFrame; // 原始帧字节数组 diff --git a/CounterAcq/protocol/protocol.pri b/CounterAcq/protocol/protocol.pri index 9194ddc..8aa7808 100644 --- a/CounterAcq/protocol/protocol.pri +++ b/CounterAcq/protocol/protocol.pri @@ -1,8 +1,8 @@ -SOURCES += $$PWD/CounterProtocolBM.cpp +#SOURCES += $$PWD/CounterProtocolBM.cpp SOURCES += $$PWD/CounterProtocolXH.cpp SOURCES += $$PWD/dto/CounterDataDto.cpp -HEADERS += $$PWD/CounterProtocolBM.h +#HEADERS += $$PWD/CounterProtocolBM.h HEADERS += $$PWD/CounterProtocolXH.h HEADERS += $$PWD/dto/CounterDataDto.h