diff --git a/DeviceHub/TimeReplicatorForm.cpp b/DeviceHub/TimeReplicatorForm.cpp index eb83370..0a6c833 100644 --- a/DeviceHub/TimeReplicatorForm.cpp +++ b/DeviceHub/TimeReplicatorForm.cpp @@ -1,5 +1,6 @@ #include "TimeReplicatorForm.h" #include "ui_TimeReplicatorForm.h" +#include "DeviceHubWindow.h" TimeReplicatorForm::TimeReplicatorForm(QWidget *parent) : QWidget(parent), @@ -15,5 +16,59 @@ void TimeReplicatorForm::on_tmRepButt_clicked() { - + // 获取设备对象 + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + TimeReplicator * device = (TimeReplicator *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09").at(devIndex); + device->mockReceivData(); } + + +void TimeReplicatorForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // 当前显示的设备编号 + if (frameData->frameType == "0901") + { + TimeReplicatorStatusDto * statusFrameDto = (TimeReplicatorStatusDto *) frameData; + ui->trDevStatus->setText(statusFrameDto->devStatus == "1" ? "正常" : "异常"); + ui->trInCount->setText(QString("%1").arg(statusFrameDto->inCount)); + + QJsonObject dataObj = statusFrameDto->toJSON().find("data")->toObject(); + QJsonDocument doc; + + doc.setArray(dataObj.find("inValid")->toArray()); + ui->trInValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + + doc.setArray(dataObj.find("outValid")->toArray()); + ui->trOutValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + } +} + +void TimeReplicatorForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "09") + { + ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->setCurrentIndex(i); + } + } + for (int i = 0; i < typeDevList.size(); i++) + { + if (typeDevList.at(i)->getDeviceId() == command.value("deviceId").toString()) + { + ((DeviceHubWindow *)this->parent()->parent())->getDevSelect()->setCurrentIndex(i); + } + } + + QString commandType = command.value("command").toString(); + if (commandType == "1") + { + + } else if (commandType == "2") + { + + } +} + diff --git a/DeviceHub/TimeReplicatorForm.cpp b/DeviceHub/TimeReplicatorForm.cpp index eb83370..0a6c833 100644 --- a/DeviceHub/TimeReplicatorForm.cpp +++ b/DeviceHub/TimeReplicatorForm.cpp @@ -1,5 +1,6 @@ #include "TimeReplicatorForm.h" #include "ui_TimeReplicatorForm.h" +#include "DeviceHubWindow.h" TimeReplicatorForm::TimeReplicatorForm(QWidget *parent) : QWidget(parent), @@ -15,5 +16,59 @@ void TimeReplicatorForm::on_tmRepButt_clicked() { - + // 获取设备对象 + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + TimeReplicator * device = (TimeReplicator *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09").at(devIndex); + device->mockReceivData(); } + + +void TimeReplicatorForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // 当前显示的设备编号 + if (frameData->frameType == "0901") + { + TimeReplicatorStatusDto * statusFrameDto = (TimeReplicatorStatusDto *) frameData; + ui->trDevStatus->setText(statusFrameDto->devStatus == "1" ? "正常" : "异常"); + ui->trInCount->setText(QString("%1").arg(statusFrameDto->inCount)); + + QJsonObject dataObj = statusFrameDto->toJSON().find("data")->toObject(); + QJsonDocument doc; + + doc.setArray(dataObj.find("inValid")->toArray()); + ui->trInValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + + doc.setArray(dataObj.find("outValid")->toArray()); + ui->trOutValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + } +} + +void TimeReplicatorForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "09") + { + ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->setCurrentIndex(i); + } + } + for (int i = 0; i < typeDevList.size(); i++) + { + if (typeDevList.at(i)->getDeviceId() == command.value("deviceId").toString()) + { + ((DeviceHubWindow *)this->parent()->parent())->getDevSelect()->setCurrentIndex(i); + } + } + + QString commandType = command.value("command").toString(); + if (commandType == "1") + { + + } else if (commandType == "2") + { + + } +} + diff --git a/DeviceHub/TimeReplicatorForm.h b/DeviceHub/TimeReplicatorForm.h index e6c9dc8..dc73d85 100644 --- a/DeviceHub/TimeReplicatorForm.h +++ b/DeviceHub/TimeReplicatorForm.h @@ -2,6 +2,7 @@ #define TIMEREPLICATORFORM_H #include +#include "device/TimeReplicator.h" namespace Ui { class TimeReplicatorForm; @@ -15,6 +16,10 @@ explicit TimeReplicatorForm(QWidget *parent = nullptr); ~TimeReplicatorForm(); +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + private slots: void on_tmRepButt_clicked(); diff --git a/DeviceHub/TimeReplicatorForm.cpp b/DeviceHub/TimeReplicatorForm.cpp index eb83370..0a6c833 100644 --- a/DeviceHub/TimeReplicatorForm.cpp +++ b/DeviceHub/TimeReplicatorForm.cpp @@ -1,5 +1,6 @@ #include "TimeReplicatorForm.h" #include "ui_TimeReplicatorForm.h" +#include "DeviceHubWindow.h" TimeReplicatorForm::TimeReplicatorForm(QWidget *parent) : QWidget(parent), @@ -15,5 +16,59 @@ void TimeReplicatorForm::on_tmRepButt_clicked() { - + // 获取设备对象 + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + TimeReplicator * device = (TimeReplicator *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09").at(devIndex); + device->mockReceivData(); } + + +void TimeReplicatorForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // 当前显示的设备编号 + if (frameData->frameType == "0901") + { + TimeReplicatorStatusDto * statusFrameDto = (TimeReplicatorStatusDto *) frameData; + ui->trDevStatus->setText(statusFrameDto->devStatus == "1" ? "正常" : "异常"); + ui->trInCount->setText(QString("%1").arg(statusFrameDto->inCount)); + + QJsonObject dataObj = statusFrameDto->toJSON().find("data")->toObject(); + QJsonDocument doc; + + doc.setArray(dataObj.find("inValid")->toArray()); + ui->trInValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + + doc.setArray(dataObj.find("outValid")->toArray()); + ui->trOutValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + } +} + +void TimeReplicatorForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "09") + { + ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->setCurrentIndex(i); + } + } + for (int i = 0; i < typeDevList.size(); i++) + { + if (typeDevList.at(i)->getDeviceId() == command.value("deviceId").toString()) + { + ((DeviceHubWindow *)this->parent()->parent())->getDevSelect()->setCurrentIndex(i); + } + } + + QString commandType = command.value("command").toString(); + if (commandType == "1") + { + + } else if (commandType == "2") + { + + } +} + diff --git a/DeviceHub/TimeReplicatorForm.h b/DeviceHub/TimeReplicatorForm.h index e6c9dc8..dc73d85 100644 --- a/DeviceHub/TimeReplicatorForm.h +++ b/DeviceHub/TimeReplicatorForm.h @@ -2,6 +2,7 @@ #define TIMEREPLICATORFORM_H #include +#include "device/TimeReplicator.h" namespace Ui { class TimeReplicatorForm; @@ -15,6 +16,10 @@ explicit TimeReplicatorForm(QWidget *parent = nullptr); ~TimeReplicatorForm(); +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + private slots: void on_tmRepButt_clicked(); diff --git a/DeviceHub/TimeReplicatorForm.ui b/DeviceHub/TimeReplicatorForm.ui index bf39894..c2f6ef4 100644 --- a/DeviceHub/TimeReplicatorForm.ui +++ b/DeviceHub/TimeReplicatorForm.ui @@ -1,34 +1,186 @@ + - - - TimeReplicatorForm - + 0 0 - 400 - 300 + 1200 + 600 Form - - - 50 - 20 - 180 - 40 - - - - Mock TimeReplicator - - + + + 50 + 20 + 180 + 40 + + + + Mock TimeReplicator + + + + + + 20 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 设备工作状态 + + + + + + 100 + 120 + 50 + 30 + + + + + + + 20 + 80 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 工作状态 + + + + + + 20 + 290 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 参数设置 + + + + + + 170 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 输入路数量 + + + + + + 260 + 120 + 50 + 30 + + + + + + + 350 + 120 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输入状态 + + + + + + 410 + 120 + 200 + 30 + + + + + + + 20 + 180 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输出状态 + + + + + + 100 + 180 + 500 + 30 + + + - + diff --git a/DeviceHub/TimeReplicatorForm.cpp b/DeviceHub/TimeReplicatorForm.cpp index eb83370..0a6c833 100644 --- a/DeviceHub/TimeReplicatorForm.cpp +++ b/DeviceHub/TimeReplicatorForm.cpp @@ -1,5 +1,6 @@ #include "TimeReplicatorForm.h" #include "ui_TimeReplicatorForm.h" +#include "DeviceHubWindow.h" TimeReplicatorForm::TimeReplicatorForm(QWidget *parent) : QWidget(parent), @@ -15,5 +16,59 @@ void TimeReplicatorForm::on_tmRepButt_clicked() { - + // 获取设备对象 + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + TimeReplicator * device = (TimeReplicator *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09").at(devIndex); + device->mockReceivData(); } + + +void TimeReplicatorForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // 当前显示的设备编号 + if (frameData->frameType == "0901") + { + TimeReplicatorStatusDto * statusFrameDto = (TimeReplicatorStatusDto *) frameData; + ui->trDevStatus->setText(statusFrameDto->devStatus == "1" ? "正常" : "异常"); + ui->trInCount->setText(QString("%1").arg(statusFrameDto->inCount)); + + QJsonObject dataObj = statusFrameDto->toJSON().find("data")->toObject(); + QJsonDocument doc; + + doc.setArray(dataObj.find("inValid")->toArray()); + ui->trInValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + + doc.setArray(dataObj.find("outValid")->toArray()); + ui->trOutValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + } +} + +void TimeReplicatorForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "09") + { + ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->setCurrentIndex(i); + } + } + for (int i = 0; i < typeDevList.size(); i++) + { + if (typeDevList.at(i)->getDeviceId() == command.value("deviceId").toString()) + { + ((DeviceHubWindow *)this->parent()->parent())->getDevSelect()->setCurrentIndex(i); + } + } + + QString commandType = command.value("command").toString(); + if (commandType == "1") + { + + } else if (commandType == "2") + { + + } +} + diff --git a/DeviceHub/TimeReplicatorForm.h b/DeviceHub/TimeReplicatorForm.h index e6c9dc8..dc73d85 100644 --- a/DeviceHub/TimeReplicatorForm.h +++ b/DeviceHub/TimeReplicatorForm.h @@ -2,6 +2,7 @@ #define TIMEREPLICATORFORM_H #include +#include "device/TimeReplicator.h" namespace Ui { class TimeReplicatorForm; @@ -15,6 +16,10 @@ explicit TimeReplicatorForm(QWidget *parent = nullptr); ~TimeReplicatorForm(); +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + private slots: void on_tmRepButt_clicked(); diff --git a/DeviceHub/TimeReplicatorForm.ui b/DeviceHub/TimeReplicatorForm.ui index bf39894..c2f6ef4 100644 --- a/DeviceHub/TimeReplicatorForm.ui +++ b/DeviceHub/TimeReplicatorForm.ui @@ -1,34 +1,186 @@ + - - - TimeReplicatorForm - + 0 0 - 400 - 300 + 1200 + 600 Form - - - 50 - 20 - 180 - 40 - - - - Mock TimeReplicator - - + + + 50 + 20 + 180 + 40 + + + + Mock TimeReplicator + + + + + + 20 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 设备工作状态 + + + + + + 100 + 120 + 50 + 30 + + + + + + + 20 + 80 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 工作状态 + + + + + + 20 + 290 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 参数设置 + + + + + + 170 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 输入路数量 + + + + + + 260 + 120 + 50 + 30 + + + + + + + 350 + 120 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输入状态 + + + + + + 410 + 120 + 200 + 30 + + + + + + + 20 + 180 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输出状态 + + + + + + 100 + 180 + 500 + 30 + + + - + diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index 289beb5..4b69272 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -4,6 +4,7 @@ #include "TimeSwitcher.h" #include "FreqSwitcher.h" #include "BCodeTerminal.h" +#include "TimeReplicator.h" #include "FreqReplicator.h" #include @@ -56,6 +57,9 @@ } else if (deviceType == "07") { base = new BCodeTerminal(parent); + } else if (deviceType == "09") + { + base = new TimeReplicator(parent); } else if (deviceType == "10") { base = new FreqReplicator(parent); diff --git a/DeviceHub/TimeReplicatorForm.cpp b/DeviceHub/TimeReplicatorForm.cpp index eb83370..0a6c833 100644 --- a/DeviceHub/TimeReplicatorForm.cpp +++ b/DeviceHub/TimeReplicatorForm.cpp @@ -1,5 +1,6 @@ #include "TimeReplicatorForm.h" #include "ui_TimeReplicatorForm.h" +#include "DeviceHubWindow.h" TimeReplicatorForm::TimeReplicatorForm(QWidget *parent) : QWidget(parent), @@ -15,5 +16,59 @@ void TimeReplicatorForm::on_tmRepButt_clicked() { - + // 获取设备对象 + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + TimeReplicator * device = (TimeReplicator *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09").at(devIndex); + device->mockReceivData(); } + + +void TimeReplicatorForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // 当前显示的设备编号 + if (frameData->frameType == "0901") + { + TimeReplicatorStatusDto * statusFrameDto = (TimeReplicatorStatusDto *) frameData; + ui->trDevStatus->setText(statusFrameDto->devStatus == "1" ? "正常" : "异常"); + ui->trInCount->setText(QString("%1").arg(statusFrameDto->inCount)); + + QJsonObject dataObj = statusFrameDto->toJSON().find("data")->toObject(); + QJsonDocument doc; + + doc.setArray(dataObj.find("inValid")->toArray()); + ui->trInValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + + doc.setArray(dataObj.find("outValid")->toArray()); + ui->trOutValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + } +} + +void TimeReplicatorForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "09") + { + ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->setCurrentIndex(i); + } + } + for (int i = 0; i < typeDevList.size(); i++) + { + if (typeDevList.at(i)->getDeviceId() == command.value("deviceId").toString()) + { + ((DeviceHubWindow *)this->parent()->parent())->getDevSelect()->setCurrentIndex(i); + } + } + + QString commandType = command.value("command").toString(); + if (commandType == "1") + { + + } else if (commandType == "2") + { + + } +} + diff --git a/DeviceHub/TimeReplicatorForm.h b/DeviceHub/TimeReplicatorForm.h index e6c9dc8..dc73d85 100644 --- a/DeviceHub/TimeReplicatorForm.h +++ b/DeviceHub/TimeReplicatorForm.h @@ -2,6 +2,7 @@ #define TIMEREPLICATORFORM_H #include +#include "device/TimeReplicator.h" namespace Ui { class TimeReplicatorForm; @@ -15,6 +16,10 @@ explicit TimeReplicatorForm(QWidget *parent = nullptr); ~TimeReplicatorForm(); +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + private slots: void on_tmRepButt_clicked(); diff --git a/DeviceHub/TimeReplicatorForm.ui b/DeviceHub/TimeReplicatorForm.ui index bf39894..c2f6ef4 100644 --- a/DeviceHub/TimeReplicatorForm.ui +++ b/DeviceHub/TimeReplicatorForm.ui @@ -1,34 +1,186 @@ + - - - TimeReplicatorForm - + 0 0 - 400 - 300 + 1200 + 600 Form - - - 50 - 20 - 180 - 40 - - - - Mock TimeReplicator - - + + + 50 + 20 + 180 + 40 + + + + Mock TimeReplicator + + + + + + 20 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 设备工作状态 + + + + + + 100 + 120 + 50 + 30 + + + + + + + 20 + 80 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 工作状态 + + + + + + 20 + 290 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 参数设置 + + + + + + 170 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 输入路数量 + + + + + + 260 + 120 + 50 + 30 + + + + + + + 350 + 120 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输入状态 + + + + + + 410 + 120 + 200 + 30 + + + + + + + 20 + 180 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输出状态 + + + + + + 100 + 180 + 500 + 30 + + + - + diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index 289beb5..4b69272 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -4,6 +4,7 @@ #include "TimeSwitcher.h" #include "FreqSwitcher.h" #include "BCodeTerminal.h" +#include "TimeReplicator.h" #include "FreqReplicator.h" #include @@ -56,6 +57,9 @@ } else if (deviceType == "07") { base = new BCodeTerminal(parent); + } else if (deviceType == "09") + { + base = new TimeReplicator(parent); } else if (deviceType == "10") { base = new FreqReplicator(parent); diff --git a/DeviceHub/device/TimeReplicator.cpp b/DeviceHub/device/TimeReplicator.cpp index 1ba0db4..4ca4547 100644 --- a/DeviceHub/device/TimeReplicator.cpp +++ b/DeviceHub/device/TimeReplicator.cpp @@ -1,18 +1,27 @@ #include "TimeReplicator.h" - +#include "DeviceHubWindow.h" #include #include TimeReplicator::TimeReplicator(QObject *parent) : DeviceBase(parent) { + this->devType = "09"; connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &TimeReplicator::dataReceivedHandler); - kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); - kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); - kafkaUtil.createProducer(); + connect(this, &TimeReplicator::sendDataToDraw, + ((DeviceHubWindow *)this->parent())->timeRepForm, &TimeReplicatorForm::drawDeviceFrameOnForm); + connect(this, &TimeReplicator::sendCommandToDisplay, + ((DeviceHubWindow *)this->parent())->timeRepForm, &TimeReplicatorForm::displayDeviceCommandOnForm); - this->protocol = DeviceStatusProtocolBase::deviceStatusProtocolFactory(typeid (this).name()); + connect(((DeviceHubWindow *)this->parent())->kafkaConsumer, &QKafkaConsumer::messageRecieved, + this, &TimeReplicator::commandReceivedHandler); + + kafkaProducer.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaProducer.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaProducer.createProducer(); + + this->protocol = DeviceProtocolBase::deviceProtocolFactory(devType); } TimeReplicator::~TimeReplicator() @@ -21,6 +30,20 @@ this, &TimeReplicator::dataReceivedHandler); } +void TimeReplicator::mockReceivData() +{ + QByteArray buffer; + + // time replicator + buffer.append("$2B21308-13 21010012200000000000000000faf0*"); + buffer.append("$3C21308-13 2101008220322222222111111110000000000000000ca24*"); + buffer.append("$3C21308-13 21010052207111111111111111111111111000000005984*"); +// buffer.append("$3C21308-13 2101005121511111111111111111111111111111111bcfe*"); + buffer.append("$3E21308-13 2101001211308-13 2101001210929H.1.00S.1.00000292b*"); + + this->dataReceivedHandler(buffer); +} + void TimeReplicator::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); @@ -50,6 +73,8 @@ frameDto->milisecond = now.toMSecsSinceEpoch(); frameDto->rawFrame = frameByte; + frameDto->devCode = devCode; + this->afterFramePhase(frameDto); } @@ -67,12 +92,83 @@ std::cout << "frame type: " << typeid(* frameDto).name() << std::endl; std::cout << frameDto->rawFrame.toStdString() << std::endl; + // 0. 输出到日志文件中 + QString date = frameDto->timestamp.mid(0, 10); + + // 1. 原始字节数组数据 + QString filename = "raw_" + devCode + ".log"; + QString content = frameDto->timestamp + " [recv] " + frameDto->rawFrame.left(frameDto->rawFrame.size() - FRAME_TAIL.size()); + QLogUtil::writeRawDataLogByDate(date, filename, content); + + // 2. 解析后的json数据 + QString frameFilename = "frame_" + devCode + ".log"; + QString frameContent = frameDto->timestamp + " [recv] " + QJsonDocument(frameDto->toJSON()).toJson(QJsonDocument::Compact); + QLogUtil::writeChannelDataLogByDate(date, frameFilename, frameContent); + // 3. 输出到中间件,执行后续处理过程 if (SettingConfig::getInstance().NEED_KAFKA == 1) { QJsonObject jsonObj = frameDto->toJSON(); jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); jsonObj.insert("deviceId", deviceId); - kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + kafkaProducer.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); } + + // 4. 在界面上简单显示相差数据结果 + emit this->sendDataToDraw(frameDto); } + +void TimeReplicator::sendDataToSerial(QByteArray data) +{ + data.append(FRAME_TAIL); + this->serialUtil.sendData(data); + + // 记录日志 + // 0. 输出到日志文件中 + QDateTime now = QDateTime::currentDateTime(); + QString date = now.toString("yyyy-MM-dd"); + + // 1. 原始字节数组数据 + QString filename = "raw_" + devCode + ".log"; + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + data.left(data.size() - FRAME_TAIL.size()); + QLogUtil::writeRawDataLogByDate(date, filename, content); +} + +void TimeReplicator::commandReceivedHandler(QJsonObject command) +{ + if (command.contains("deviceType") == false || command.value("deviceType").toString() != "09") + { + std::cout << "device type [" << command.value("deviceType").toString().toStdString() << "] not matched. return" << std::endl; + return; + } + + if (command.contains("deviceId") == false || command.value("deviceId").toString() != deviceId) + { + std::cout << "deviceId [" << command.value("deviceId").toString().toStdString() << "] not matched. return" << std::endl; + return; + } + + // 记录日志 + // 0. 输出到日志文件中 + QDateTime now = QDateTime::currentDateTime(); + QString date = now.toString("yyyy-MM-dd"); + + // 1. 原始字节数组数据 + QString filename = "frame_" + devCode + ".log"; + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + command.find("cmdStr")->toString(); + QLogUtil::writeChannelDataLogByDate(date, filename, content); + + QByteArray commandBytes = protocol->generateSettingCommand(command.value("command").toString(), command.value("params").toString()); + this->sendDataToSerial(commandBytes); + command.insert("rawCommand", QString::fromUtf8(commandBytes)); + + QJsonObject cmdcb; + cmdcb.insert("commandId", command.value("commandId").toString()); + cmdcb.insert("rawCommand", QString::fromUtf8(commandBytes)); + cmdcb.insert("status", "1"); + this->kafkaProducer.produceMessage(SettingConfig::getInstance().KAFKA_CMDCB_TOPIC, QString(QJsonDocument(cmdcb).toJson(QJsonDocument::Compact))); + + // display on page + emit sendCommandToDisplay(command); +} + diff --git a/DeviceHub/TimeReplicatorForm.cpp b/DeviceHub/TimeReplicatorForm.cpp index eb83370..0a6c833 100644 --- a/DeviceHub/TimeReplicatorForm.cpp +++ b/DeviceHub/TimeReplicatorForm.cpp @@ -1,5 +1,6 @@ #include "TimeReplicatorForm.h" #include "ui_TimeReplicatorForm.h" +#include "DeviceHubWindow.h" TimeReplicatorForm::TimeReplicatorForm(QWidget *parent) : QWidget(parent), @@ -15,5 +16,59 @@ void TimeReplicatorForm::on_tmRepButt_clicked() { - + // 获取设备对象 + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + TimeReplicator * device = (TimeReplicator *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09").at(devIndex); + device->mockReceivData(); } + + +void TimeReplicatorForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // 当前显示的设备编号 + if (frameData->frameType == "0901") + { + TimeReplicatorStatusDto * statusFrameDto = (TimeReplicatorStatusDto *) frameData; + ui->trDevStatus->setText(statusFrameDto->devStatus == "1" ? "正常" : "异常"); + ui->trInCount->setText(QString("%1").arg(statusFrameDto->inCount)); + + QJsonObject dataObj = statusFrameDto->toJSON().find("data")->toObject(); + QJsonDocument doc; + + doc.setArray(dataObj.find("inValid")->toArray()); + ui->trInValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + + doc.setArray(dataObj.find("outValid")->toArray()); + ui->trOutValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + } +} + +void TimeReplicatorForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "09") + { + ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->setCurrentIndex(i); + } + } + for (int i = 0; i < typeDevList.size(); i++) + { + if (typeDevList.at(i)->getDeviceId() == command.value("deviceId").toString()) + { + ((DeviceHubWindow *)this->parent()->parent())->getDevSelect()->setCurrentIndex(i); + } + } + + QString commandType = command.value("command").toString(); + if (commandType == "1") + { + + } else if (commandType == "2") + { + + } +} + diff --git a/DeviceHub/TimeReplicatorForm.h b/DeviceHub/TimeReplicatorForm.h index e6c9dc8..dc73d85 100644 --- a/DeviceHub/TimeReplicatorForm.h +++ b/DeviceHub/TimeReplicatorForm.h @@ -2,6 +2,7 @@ #define TIMEREPLICATORFORM_H #include +#include "device/TimeReplicator.h" namespace Ui { class TimeReplicatorForm; @@ -15,6 +16,10 @@ explicit TimeReplicatorForm(QWidget *parent = nullptr); ~TimeReplicatorForm(); +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + private slots: void on_tmRepButt_clicked(); diff --git a/DeviceHub/TimeReplicatorForm.ui b/DeviceHub/TimeReplicatorForm.ui index bf39894..c2f6ef4 100644 --- a/DeviceHub/TimeReplicatorForm.ui +++ b/DeviceHub/TimeReplicatorForm.ui @@ -1,34 +1,186 @@ + - - - TimeReplicatorForm - + 0 0 - 400 - 300 + 1200 + 600 Form - - - 50 - 20 - 180 - 40 - - - - Mock TimeReplicator - - + + + 50 + 20 + 180 + 40 + + + + Mock TimeReplicator + + + + + + 20 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 设备工作状态 + + + + + + 100 + 120 + 50 + 30 + + + + + + + 20 + 80 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 工作状态 + + + + + + 20 + 290 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 参数设置 + + + + + + 170 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 输入路数量 + + + + + + 260 + 120 + 50 + 30 + + + + + + + 350 + 120 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输入状态 + + + + + + 410 + 120 + 200 + 30 + + + + + + + 20 + 180 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输出状态 + + + + + + 100 + 180 + 500 + 30 + + + - + diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index 289beb5..4b69272 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -4,6 +4,7 @@ #include "TimeSwitcher.h" #include "FreqSwitcher.h" #include "BCodeTerminal.h" +#include "TimeReplicator.h" #include "FreqReplicator.h" #include @@ -56,6 +57,9 @@ } else if (deviceType == "07") { base = new BCodeTerminal(parent); + } else if (deviceType == "09") + { + base = new TimeReplicator(parent); } else if (deviceType == "10") { base = new FreqReplicator(parent); diff --git a/DeviceHub/device/TimeReplicator.cpp b/DeviceHub/device/TimeReplicator.cpp index 1ba0db4..4ca4547 100644 --- a/DeviceHub/device/TimeReplicator.cpp +++ b/DeviceHub/device/TimeReplicator.cpp @@ -1,18 +1,27 @@ #include "TimeReplicator.h" - +#include "DeviceHubWindow.h" #include #include TimeReplicator::TimeReplicator(QObject *parent) : DeviceBase(parent) { + this->devType = "09"; connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &TimeReplicator::dataReceivedHandler); - kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); - kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); - kafkaUtil.createProducer(); + connect(this, &TimeReplicator::sendDataToDraw, + ((DeviceHubWindow *)this->parent())->timeRepForm, &TimeReplicatorForm::drawDeviceFrameOnForm); + connect(this, &TimeReplicator::sendCommandToDisplay, + ((DeviceHubWindow *)this->parent())->timeRepForm, &TimeReplicatorForm::displayDeviceCommandOnForm); - this->protocol = DeviceStatusProtocolBase::deviceStatusProtocolFactory(typeid (this).name()); + connect(((DeviceHubWindow *)this->parent())->kafkaConsumer, &QKafkaConsumer::messageRecieved, + this, &TimeReplicator::commandReceivedHandler); + + kafkaProducer.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaProducer.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaProducer.createProducer(); + + this->protocol = DeviceProtocolBase::deviceProtocolFactory(devType); } TimeReplicator::~TimeReplicator() @@ -21,6 +30,20 @@ this, &TimeReplicator::dataReceivedHandler); } +void TimeReplicator::mockReceivData() +{ + QByteArray buffer; + + // time replicator + buffer.append("$2B21308-13 21010012200000000000000000faf0*"); + buffer.append("$3C21308-13 2101008220322222222111111110000000000000000ca24*"); + buffer.append("$3C21308-13 21010052207111111111111111111111111000000005984*"); +// buffer.append("$3C21308-13 2101005121511111111111111111111111111111111bcfe*"); + buffer.append("$3E21308-13 2101001211308-13 2101001210929H.1.00S.1.00000292b*"); + + this->dataReceivedHandler(buffer); +} + void TimeReplicator::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); @@ -50,6 +73,8 @@ frameDto->milisecond = now.toMSecsSinceEpoch(); frameDto->rawFrame = frameByte; + frameDto->devCode = devCode; + this->afterFramePhase(frameDto); } @@ -67,12 +92,83 @@ std::cout << "frame type: " << typeid(* frameDto).name() << std::endl; std::cout << frameDto->rawFrame.toStdString() << std::endl; + // 0. 输出到日志文件中 + QString date = frameDto->timestamp.mid(0, 10); + + // 1. 原始字节数组数据 + QString filename = "raw_" + devCode + ".log"; + QString content = frameDto->timestamp + " [recv] " + frameDto->rawFrame.left(frameDto->rawFrame.size() - FRAME_TAIL.size()); + QLogUtil::writeRawDataLogByDate(date, filename, content); + + // 2. 解析后的json数据 + QString frameFilename = "frame_" + devCode + ".log"; + QString frameContent = frameDto->timestamp + " [recv] " + QJsonDocument(frameDto->toJSON()).toJson(QJsonDocument::Compact); + QLogUtil::writeChannelDataLogByDate(date, frameFilename, frameContent); + // 3. 输出到中间件,执行后续处理过程 if (SettingConfig::getInstance().NEED_KAFKA == 1) { QJsonObject jsonObj = frameDto->toJSON(); jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); jsonObj.insert("deviceId", deviceId); - kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + kafkaProducer.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); } + + // 4. 在界面上简单显示相差数据结果 + emit this->sendDataToDraw(frameDto); } + +void TimeReplicator::sendDataToSerial(QByteArray data) +{ + data.append(FRAME_TAIL); + this->serialUtil.sendData(data); + + // 记录日志 + // 0. 输出到日志文件中 + QDateTime now = QDateTime::currentDateTime(); + QString date = now.toString("yyyy-MM-dd"); + + // 1. 原始字节数组数据 + QString filename = "raw_" + devCode + ".log"; + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + data.left(data.size() - FRAME_TAIL.size()); + QLogUtil::writeRawDataLogByDate(date, filename, content); +} + +void TimeReplicator::commandReceivedHandler(QJsonObject command) +{ + if (command.contains("deviceType") == false || command.value("deviceType").toString() != "09") + { + std::cout << "device type [" << command.value("deviceType").toString().toStdString() << "] not matched. return" << std::endl; + return; + } + + if (command.contains("deviceId") == false || command.value("deviceId").toString() != deviceId) + { + std::cout << "deviceId [" << command.value("deviceId").toString().toStdString() << "] not matched. return" << std::endl; + return; + } + + // 记录日志 + // 0. 输出到日志文件中 + QDateTime now = QDateTime::currentDateTime(); + QString date = now.toString("yyyy-MM-dd"); + + // 1. 原始字节数组数据 + QString filename = "frame_" + devCode + ".log"; + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + command.find("cmdStr")->toString(); + QLogUtil::writeChannelDataLogByDate(date, filename, content); + + QByteArray commandBytes = protocol->generateSettingCommand(command.value("command").toString(), command.value("params").toString()); + this->sendDataToSerial(commandBytes); + command.insert("rawCommand", QString::fromUtf8(commandBytes)); + + QJsonObject cmdcb; + cmdcb.insert("commandId", command.value("commandId").toString()); + cmdcb.insert("rawCommand", QString::fromUtf8(commandBytes)); + cmdcb.insert("status", "1"); + this->kafkaProducer.produceMessage(SettingConfig::getInstance().KAFKA_CMDCB_TOPIC, QString(QJsonDocument(cmdcb).toJson(QJsonDocument::Compact))); + + // display on page + emit sendCommandToDisplay(command); +} + diff --git a/DeviceHub/device/TimeReplicator.h b/DeviceHub/device/TimeReplicator.h index 5acf6d9..12b5ff4 100644 --- a/DeviceHub/device/TimeReplicator.h +++ b/DeviceHub/device/TimeReplicator.h @@ -4,6 +4,7 @@ #include #include "device/DeviceBase.h" +#include "protocol/dto/TimeReplicatorStatusDto.h" class TimeReplicator : public DeviceBase { @@ -12,13 +13,19 @@ explicit TimeReplicator(QObject *parent = nullptr); ~TimeReplicator(); + void mockReceivData(); + void afterFramePhase(DeviceFrameBaseDto * frameDto); + void sendDataToSerial(QByteArray data); signals: void sendDataToDraw(DeviceFrameBaseDto * frameData); + void sendCommandToDisplay(QJsonObject command); public slots: void dataReceivedHandler(QByteArray data); + void commandReceivedHandler(QJsonObject command); + }; #endif // TIMEREPLICATOR_H diff --git a/DeviceHub/TimeReplicatorForm.cpp b/DeviceHub/TimeReplicatorForm.cpp index eb83370..0a6c833 100644 --- a/DeviceHub/TimeReplicatorForm.cpp +++ b/DeviceHub/TimeReplicatorForm.cpp @@ -1,5 +1,6 @@ #include "TimeReplicatorForm.h" #include "ui_TimeReplicatorForm.h" +#include "DeviceHubWindow.h" TimeReplicatorForm::TimeReplicatorForm(QWidget *parent) : QWidget(parent), @@ -15,5 +16,59 @@ void TimeReplicatorForm::on_tmRepButt_clicked() { - + // 获取设备对象 + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + TimeReplicator * device = (TimeReplicator *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09").at(devIndex); + device->mockReceivData(); } + + +void TimeReplicatorForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // 当前显示的设备编号 + if (frameData->frameType == "0901") + { + TimeReplicatorStatusDto * statusFrameDto = (TimeReplicatorStatusDto *) frameData; + ui->trDevStatus->setText(statusFrameDto->devStatus == "1" ? "正常" : "异常"); + ui->trInCount->setText(QString("%1").arg(statusFrameDto->inCount)); + + QJsonObject dataObj = statusFrameDto->toJSON().find("data")->toObject(); + QJsonDocument doc; + + doc.setArray(dataObj.find("inValid")->toArray()); + ui->trInValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + + doc.setArray(dataObj.find("outValid")->toArray()); + ui->trOutValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + } +} + +void TimeReplicatorForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "09") + { + ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->setCurrentIndex(i); + } + } + for (int i = 0; i < typeDevList.size(); i++) + { + if (typeDevList.at(i)->getDeviceId() == command.value("deviceId").toString()) + { + ((DeviceHubWindow *)this->parent()->parent())->getDevSelect()->setCurrentIndex(i); + } + } + + QString commandType = command.value("command").toString(); + if (commandType == "1") + { + + } else if (commandType == "2") + { + + } +} + diff --git a/DeviceHub/TimeReplicatorForm.h b/DeviceHub/TimeReplicatorForm.h index e6c9dc8..dc73d85 100644 --- a/DeviceHub/TimeReplicatorForm.h +++ b/DeviceHub/TimeReplicatorForm.h @@ -2,6 +2,7 @@ #define TIMEREPLICATORFORM_H #include +#include "device/TimeReplicator.h" namespace Ui { class TimeReplicatorForm; @@ -15,6 +16,10 @@ explicit TimeReplicatorForm(QWidget *parent = nullptr); ~TimeReplicatorForm(); +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + private slots: void on_tmRepButt_clicked(); diff --git a/DeviceHub/TimeReplicatorForm.ui b/DeviceHub/TimeReplicatorForm.ui index bf39894..c2f6ef4 100644 --- a/DeviceHub/TimeReplicatorForm.ui +++ b/DeviceHub/TimeReplicatorForm.ui @@ -1,34 +1,186 @@ + - - - TimeReplicatorForm - + 0 0 - 400 - 300 + 1200 + 600 Form - - - 50 - 20 - 180 - 40 - - - - Mock TimeReplicator - - + + + 50 + 20 + 180 + 40 + + + + Mock TimeReplicator + + + + + + 20 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 设备工作状态 + + + + + + 100 + 120 + 50 + 30 + + + + + + + 20 + 80 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 工作状态 + + + + + + 20 + 290 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 参数设置 + + + + + + 170 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 输入路数量 + + + + + + 260 + 120 + 50 + 30 + + + + + + + 350 + 120 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输入状态 + + + + + + 410 + 120 + 200 + 30 + + + + + + + 20 + 180 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输出状态 + + + + + + 100 + 180 + 500 + 30 + + + - + diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index 289beb5..4b69272 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -4,6 +4,7 @@ #include "TimeSwitcher.h" #include "FreqSwitcher.h" #include "BCodeTerminal.h" +#include "TimeReplicator.h" #include "FreqReplicator.h" #include @@ -56,6 +57,9 @@ } else if (deviceType == "07") { base = new BCodeTerminal(parent); + } else if (deviceType == "09") + { + base = new TimeReplicator(parent); } else if (deviceType == "10") { base = new FreqReplicator(parent); diff --git a/DeviceHub/device/TimeReplicator.cpp b/DeviceHub/device/TimeReplicator.cpp index 1ba0db4..4ca4547 100644 --- a/DeviceHub/device/TimeReplicator.cpp +++ b/DeviceHub/device/TimeReplicator.cpp @@ -1,18 +1,27 @@ #include "TimeReplicator.h" - +#include "DeviceHubWindow.h" #include #include TimeReplicator::TimeReplicator(QObject *parent) : DeviceBase(parent) { + this->devType = "09"; connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &TimeReplicator::dataReceivedHandler); - kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); - kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); - kafkaUtil.createProducer(); + connect(this, &TimeReplicator::sendDataToDraw, + ((DeviceHubWindow *)this->parent())->timeRepForm, &TimeReplicatorForm::drawDeviceFrameOnForm); + connect(this, &TimeReplicator::sendCommandToDisplay, + ((DeviceHubWindow *)this->parent())->timeRepForm, &TimeReplicatorForm::displayDeviceCommandOnForm); - this->protocol = DeviceStatusProtocolBase::deviceStatusProtocolFactory(typeid (this).name()); + connect(((DeviceHubWindow *)this->parent())->kafkaConsumer, &QKafkaConsumer::messageRecieved, + this, &TimeReplicator::commandReceivedHandler); + + kafkaProducer.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaProducer.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaProducer.createProducer(); + + this->protocol = DeviceProtocolBase::deviceProtocolFactory(devType); } TimeReplicator::~TimeReplicator() @@ -21,6 +30,20 @@ this, &TimeReplicator::dataReceivedHandler); } +void TimeReplicator::mockReceivData() +{ + QByteArray buffer; + + // time replicator + buffer.append("$2B21308-13 21010012200000000000000000faf0*"); + buffer.append("$3C21308-13 2101008220322222222111111110000000000000000ca24*"); + buffer.append("$3C21308-13 21010052207111111111111111111111111000000005984*"); +// buffer.append("$3C21308-13 2101005121511111111111111111111111111111111bcfe*"); + buffer.append("$3E21308-13 2101001211308-13 2101001210929H.1.00S.1.00000292b*"); + + this->dataReceivedHandler(buffer); +} + void TimeReplicator::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); @@ -50,6 +73,8 @@ frameDto->milisecond = now.toMSecsSinceEpoch(); frameDto->rawFrame = frameByte; + frameDto->devCode = devCode; + this->afterFramePhase(frameDto); } @@ -67,12 +92,83 @@ std::cout << "frame type: " << typeid(* frameDto).name() << std::endl; std::cout << frameDto->rawFrame.toStdString() << std::endl; + // 0. 输出到日志文件中 + QString date = frameDto->timestamp.mid(0, 10); + + // 1. 原始字节数组数据 + QString filename = "raw_" + devCode + ".log"; + QString content = frameDto->timestamp + " [recv] " + frameDto->rawFrame.left(frameDto->rawFrame.size() - FRAME_TAIL.size()); + QLogUtil::writeRawDataLogByDate(date, filename, content); + + // 2. 解析后的json数据 + QString frameFilename = "frame_" + devCode + ".log"; + QString frameContent = frameDto->timestamp + " [recv] " + QJsonDocument(frameDto->toJSON()).toJson(QJsonDocument::Compact); + QLogUtil::writeChannelDataLogByDate(date, frameFilename, frameContent); + // 3. 输出到中间件,执行后续处理过程 if (SettingConfig::getInstance().NEED_KAFKA == 1) { QJsonObject jsonObj = frameDto->toJSON(); jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); jsonObj.insert("deviceId", deviceId); - kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + kafkaProducer.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); } + + // 4. 在界面上简单显示相差数据结果 + emit this->sendDataToDraw(frameDto); } + +void TimeReplicator::sendDataToSerial(QByteArray data) +{ + data.append(FRAME_TAIL); + this->serialUtil.sendData(data); + + // 记录日志 + // 0. 输出到日志文件中 + QDateTime now = QDateTime::currentDateTime(); + QString date = now.toString("yyyy-MM-dd"); + + // 1. 原始字节数组数据 + QString filename = "raw_" + devCode + ".log"; + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + data.left(data.size() - FRAME_TAIL.size()); + QLogUtil::writeRawDataLogByDate(date, filename, content); +} + +void TimeReplicator::commandReceivedHandler(QJsonObject command) +{ + if (command.contains("deviceType") == false || command.value("deviceType").toString() != "09") + { + std::cout << "device type [" << command.value("deviceType").toString().toStdString() << "] not matched. return" << std::endl; + return; + } + + if (command.contains("deviceId") == false || command.value("deviceId").toString() != deviceId) + { + std::cout << "deviceId [" << command.value("deviceId").toString().toStdString() << "] not matched. return" << std::endl; + return; + } + + // 记录日志 + // 0. 输出到日志文件中 + QDateTime now = QDateTime::currentDateTime(); + QString date = now.toString("yyyy-MM-dd"); + + // 1. 原始字节数组数据 + QString filename = "frame_" + devCode + ".log"; + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + command.find("cmdStr")->toString(); + QLogUtil::writeChannelDataLogByDate(date, filename, content); + + QByteArray commandBytes = protocol->generateSettingCommand(command.value("command").toString(), command.value("params").toString()); + this->sendDataToSerial(commandBytes); + command.insert("rawCommand", QString::fromUtf8(commandBytes)); + + QJsonObject cmdcb; + cmdcb.insert("commandId", command.value("commandId").toString()); + cmdcb.insert("rawCommand", QString::fromUtf8(commandBytes)); + cmdcb.insert("status", "1"); + this->kafkaProducer.produceMessage(SettingConfig::getInstance().KAFKA_CMDCB_TOPIC, QString(QJsonDocument(cmdcb).toJson(QJsonDocument::Compact))); + + // display on page + emit sendCommandToDisplay(command); +} + diff --git a/DeviceHub/device/TimeReplicator.h b/DeviceHub/device/TimeReplicator.h index 5acf6d9..12b5ff4 100644 --- a/DeviceHub/device/TimeReplicator.h +++ b/DeviceHub/device/TimeReplicator.h @@ -4,6 +4,7 @@ #include #include "device/DeviceBase.h" +#include "protocol/dto/TimeReplicatorStatusDto.h" class TimeReplicator : public DeviceBase { @@ -12,13 +13,19 @@ explicit TimeReplicator(QObject *parent = nullptr); ~TimeReplicator(); + void mockReceivData(); + void afterFramePhase(DeviceFrameBaseDto * frameDto); + void sendDataToSerial(QByteArray data); signals: void sendDataToDraw(DeviceFrameBaseDto * frameData); + void sendCommandToDisplay(QJsonObject command); public slots: void dataReceivedHandler(QByteArray data); + void commandReceivedHandler(QJsonObject command); + }; #endif // TIMEREPLICATOR_H diff --git a/DeviceHub/device/device.pri b/DeviceHub/device/device.pri index 645dbcb..fedab85 100644 --- a/DeviceHub/device/device.pri +++ b/DeviceHub/device/device.pri @@ -4,7 +4,7 @@ HEADERS += $$PWD/SignalGenerator.h HEADERS += $$PWD/TimeSwitcher.h HEADERS += $$PWD/FreqSwitcher.h -#HEADERS += $$PWD/TimeReplicator.h +HEADERS += $$PWD/TimeReplicator.h HEADERS += $$PWD/FreqReplicator.h HEADERS += $$PWD/BCodeTerminal.h @@ -13,6 +13,6 @@ SOURCES += $$PWD/SignalGenerator.cpp SOURCES += $$PWD/TimeSwitcher.cpp SOURCES += $$PWD/FreqSwitcher.cpp -#SOURCES += $$PWD/TimeReplicator.cpp +SOURCES += $$PWD/TimeReplicator.cpp SOURCES += $$PWD/FreqReplicator.cpp SOURCES += $$PWD/BCodeTerminal.cpp diff --git a/DeviceHub/TimeReplicatorForm.cpp b/DeviceHub/TimeReplicatorForm.cpp index eb83370..0a6c833 100644 --- a/DeviceHub/TimeReplicatorForm.cpp +++ b/DeviceHub/TimeReplicatorForm.cpp @@ -1,5 +1,6 @@ #include "TimeReplicatorForm.h" #include "ui_TimeReplicatorForm.h" +#include "DeviceHubWindow.h" TimeReplicatorForm::TimeReplicatorForm(QWidget *parent) : QWidget(parent), @@ -15,5 +16,59 @@ void TimeReplicatorForm::on_tmRepButt_clicked() { - + // 获取设备对象 + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + TimeReplicator * device = (TimeReplicator *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09").at(devIndex); + device->mockReceivData(); } + + +void TimeReplicatorForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // 当前显示的设备编号 + if (frameData->frameType == "0901") + { + TimeReplicatorStatusDto * statusFrameDto = (TimeReplicatorStatusDto *) frameData; + ui->trDevStatus->setText(statusFrameDto->devStatus == "1" ? "正常" : "异常"); + ui->trInCount->setText(QString("%1").arg(statusFrameDto->inCount)); + + QJsonObject dataObj = statusFrameDto->toJSON().find("data")->toObject(); + QJsonDocument doc; + + doc.setArray(dataObj.find("inValid")->toArray()); + ui->trInValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + + doc.setArray(dataObj.find("outValid")->toArray()); + ui->trOutValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + } +} + +void TimeReplicatorForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "09") + { + ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->setCurrentIndex(i); + } + } + for (int i = 0; i < typeDevList.size(); i++) + { + if (typeDevList.at(i)->getDeviceId() == command.value("deviceId").toString()) + { + ((DeviceHubWindow *)this->parent()->parent())->getDevSelect()->setCurrentIndex(i); + } + } + + QString commandType = command.value("command").toString(); + if (commandType == "1") + { + + } else if (commandType == "2") + { + + } +} + diff --git a/DeviceHub/TimeReplicatorForm.h b/DeviceHub/TimeReplicatorForm.h index e6c9dc8..dc73d85 100644 --- a/DeviceHub/TimeReplicatorForm.h +++ b/DeviceHub/TimeReplicatorForm.h @@ -2,6 +2,7 @@ #define TIMEREPLICATORFORM_H #include +#include "device/TimeReplicator.h" namespace Ui { class TimeReplicatorForm; @@ -15,6 +16,10 @@ explicit TimeReplicatorForm(QWidget *parent = nullptr); ~TimeReplicatorForm(); +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + private slots: void on_tmRepButt_clicked(); diff --git a/DeviceHub/TimeReplicatorForm.ui b/DeviceHub/TimeReplicatorForm.ui index bf39894..c2f6ef4 100644 --- a/DeviceHub/TimeReplicatorForm.ui +++ b/DeviceHub/TimeReplicatorForm.ui @@ -1,34 +1,186 @@ + - - - TimeReplicatorForm - + 0 0 - 400 - 300 + 1200 + 600 Form - - - 50 - 20 - 180 - 40 - - - - Mock TimeReplicator - - + + + 50 + 20 + 180 + 40 + + + + Mock TimeReplicator + + + + + + 20 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 设备工作状态 + + + + + + 100 + 120 + 50 + 30 + + + + + + + 20 + 80 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 工作状态 + + + + + + 20 + 290 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 参数设置 + + + + + + 170 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 输入路数量 + + + + + + 260 + 120 + 50 + 30 + + + + + + + 350 + 120 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输入状态 + + + + + + 410 + 120 + 200 + 30 + + + + + + + 20 + 180 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输出状态 + + + + + + 100 + 180 + 500 + 30 + + + - + diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index 289beb5..4b69272 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -4,6 +4,7 @@ #include "TimeSwitcher.h" #include "FreqSwitcher.h" #include "BCodeTerminal.h" +#include "TimeReplicator.h" #include "FreqReplicator.h" #include @@ -56,6 +57,9 @@ } else if (deviceType == "07") { base = new BCodeTerminal(parent); + } else if (deviceType == "09") + { + base = new TimeReplicator(parent); } else if (deviceType == "10") { base = new FreqReplicator(parent); diff --git a/DeviceHub/device/TimeReplicator.cpp b/DeviceHub/device/TimeReplicator.cpp index 1ba0db4..4ca4547 100644 --- a/DeviceHub/device/TimeReplicator.cpp +++ b/DeviceHub/device/TimeReplicator.cpp @@ -1,18 +1,27 @@ #include "TimeReplicator.h" - +#include "DeviceHubWindow.h" #include #include TimeReplicator::TimeReplicator(QObject *parent) : DeviceBase(parent) { + this->devType = "09"; connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &TimeReplicator::dataReceivedHandler); - kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); - kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); - kafkaUtil.createProducer(); + connect(this, &TimeReplicator::sendDataToDraw, + ((DeviceHubWindow *)this->parent())->timeRepForm, &TimeReplicatorForm::drawDeviceFrameOnForm); + connect(this, &TimeReplicator::sendCommandToDisplay, + ((DeviceHubWindow *)this->parent())->timeRepForm, &TimeReplicatorForm::displayDeviceCommandOnForm); - this->protocol = DeviceStatusProtocolBase::deviceStatusProtocolFactory(typeid (this).name()); + connect(((DeviceHubWindow *)this->parent())->kafkaConsumer, &QKafkaConsumer::messageRecieved, + this, &TimeReplicator::commandReceivedHandler); + + kafkaProducer.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaProducer.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaProducer.createProducer(); + + this->protocol = DeviceProtocolBase::deviceProtocolFactory(devType); } TimeReplicator::~TimeReplicator() @@ -21,6 +30,20 @@ this, &TimeReplicator::dataReceivedHandler); } +void TimeReplicator::mockReceivData() +{ + QByteArray buffer; + + // time replicator + buffer.append("$2B21308-13 21010012200000000000000000faf0*"); + buffer.append("$3C21308-13 2101008220322222222111111110000000000000000ca24*"); + buffer.append("$3C21308-13 21010052207111111111111111111111111000000005984*"); +// buffer.append("$3C21308-13 2101005121511111111111111111111111111111111bcfe*"); + buffer.append("$3E21308-13 2101001211308-13 2101001210929H.1.00S.1.00000292b*"); + + this->dataReceivedHandler(buffer); +} + void TimeReplicator::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); @@ -50,6 +73,8 @@ frameDto->milisecond = now.toMSecsSinceEpoch(); frameDto->rawFrame = frameByte; + frameDto->devCode = devCode; + this->afterFramePhase(frameDto); } @@ -67,12 +92,83 @@ std::cout << "frame type: " << typeid(* frameDto).name() << std::endl; std::cout << frameDto->rawFrame.toStdString() << std::endl; + // 0. 输出到日志文件中 + QString date = frameDto->timestamp.mid(0, 10); + + // 1. 原始字节数组数据 + QString filename = "raw_" + devCode + ".log"; + QString content = frameDto->timestamp + " [recv] " + frameDto->rawFrame.left(frameDto->rawFrame.size() - FRAME_TAIL.size()); + QLogUtil::writeRawDataLogByDate(date, filename, content); + + // 2. 解析后的json数据 + QString frameFilename = "frame_" + devCode + ".log"; + QString frameContent = frameDto->timestamp + " [recv] " + QJsonDocument(frameDto->toJSON()).toJson(QJsonDocument::Compact); + QLogUtil::writeChannelDataLogByDate(date, frameFilename, frameContent); + // 3. 输出到中间件,执行后续处理过程 if (SettingConfig::getInstance().NEED_KAFKA == 1) { QJsonObject jsonObj = frameDto->toJSON(); jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); jsonObj.insert("deviceId", deviceId); - kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + kafkaProducer.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); } + + // 4. 在界面上简单显示相差数据结果 + emit this->sendDataToDraw(frameDto); } + +void TimeReplicator::sendDataToSerial(QByteArray data) +{ + data.append(FRAME_TAIL); + this->serialUtil.sendData(data); + + // 记录日志 + // 0. 输出到日志文件中 + QDateTime now = QDateTime::currentDateTime(); + QString date = now.toString("yyyy-MM-dd"); + + // 1. 原始字节数组数据 + QString filename = "raw_" + devCode + ".log"; + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + data.left(data.size() - FRAME_TAIL.size()); + QLogUtil::writeRawDataLogByDate(date, filename, content); +} + +void TimeReplicator::commandReceivedHandler(QJsonObject command) +{ + if (command.contains("deviceType") == false || command.value("deviceType").toString() != "09") + { + std::cout << "device type [" << command.value("deviceType").toString().toStdString() << "] not matched. return" << std::endl; + return; + } + + if (command.contains("deviceId") == false || command.value("deviceId").toString() != deviceId) + { + std::cout << "deviceId [" << command.value("deviceId").toString().toStdString() << "] not matched. return" << std::endl; + return; + } + + // 记录日志 + // 0. 输出到日志文件中 + QDateTime now = QDateTime::currentDateTime(); + QString date = now.toString("yyyy-MM-dd"); + + // 1. 原始字节数组数据 + QString filename = "frame_" + devCode + ".log"; + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + command.find("cmdStr")->toString(); + QLogUtil::writeChannelDataLogByDate(date, filename, content); + + QByteArray commandBytes = protocol->generateSettingCommand(command.value("command").toString(), command.value("params").toString()); + this->sendDataToSerial(commandBytes); + command.insert("rawCommand", QString::fromUtf8(commandBytes)); + + QJsonObject cmdcb; + cmdcb.insert("commandId", command.value("commandId").toString()); + cmdcb.insert("rawCommand", QString::fromUtf8(commandBytes)); + cmdcb.insert("status", "1"); + this->kafkaProducer.produceMessage(SettingConfig::getInstance().KAFKA_CMDCB_TOPIC, QString(QJsonDocument(cmdcb).toJson(QJsonDocument::Compact))); + + // display on page + emit sendCommandToDisplay(command); +} + diff --git a/DeviceHub/device/TimeReplicator.h b/DeviceHub/device/TimeReplicator.h index 5acf6d9..12b5ff4 100644 --- a/DeviceHub/device/TimeReplicator.h +++ b/DeviceHub/device/TimeReplicator.h @@ -4,6 +4,7 @@ #include #include "device/DeviceBase.h" +#include "protocol/dto/TimeReplicatorStatusDto.h" class TimeReplicator : public DeviceBase { @@ -12,13 +13,19 @@ explicit TimeReplicator(QObject *parent = nullptr); ~TimeReplicator(); + void mockReceivData(); + void afterFramePhase(DeviceFrameBaseDto * frameDto); + void sendDataToSerial(QByteArray data); signals: void sendDataToDraw(DeviceFrameBaseDto * frameData); + void sendCommandToDisplay(QJsonObject command); public slots: void dataReceivedHandler(QByteArray data); + void commandReceivedHandler(QJsonObject command); + }; #endif // TIMEREPLICATOR_H diff --git a/DeviceHub/device/device.pri b/DeviceHub/device/device.pri index 645dbcb..fedab85 100644 --- a/DeviceHub/device/device.pri +++ b/DeviceHub/device/device.pri @@ -4,7 +4,7 @@ HEADERS += $$PWD/SignalGenerator.h HEADERS += $$PWD/TimeSwitcher.h HEADERS += $$PWD/FreqSwitcher.h -#HEADERS += $$PWD/TimeReplicator.h +HEADERS += $$PWD/TimeReplicator.h HEADERS += $$PWD/FreqReplicator.h HEADERS += $$PWD/BCodeTerminal.h @@ -13,6 +13,6 @@ SOURCES += $$PWD/SignalGenerator.cpp SOURCES += $$PWD/TimeSwitcher.cpp SOURCES += $$PWD/FreqSwitcher.cpp -#SOURCES += $$PWD/TimeReplicator.cpp +SOURCES += $$PWD/TimeReplicator.cpp SOURCES += $$PWD/FreqReplicator.cpp SOURCES += $$PWD/BCodeTerminal.cpp diff --git a/DeviceHub/protocol/DeviceProtocolBase.cpp b/DeviceHub/protocol/DeviceProtocolBase.cpp index e7b26ca..4109e8d 100644 --- a/DeviceHub/protocol/DeviceProtocolBase.cpp +++ b/DeviceHub/protocol/DeviceProtocolBase.cpp @@ -3,7 +3,7 @@ #include "SignalGeneratorProtocolBM.h" #include "TimeSwitcherProtocolBM.h" #include "FreqSwitcherProtocolBM.h" -//#include "TimeReplicatorProtocolBM.h" +#include "TimeReplicatorProtocolBM.h" #include "FreqReplicatorProtocolTX.h" #include "BCodeTerminalProtocolBM.h" @@ -16,22 +16,25 @@ DeviceProtocolBase * DeviceProtocolBase::deviceProtocolFactory(QString deviceType) { - if (deviceType == "03") + if (deviceType == "03" || deviceType.contains("FrequencyTuning") == true) { return new FrequencyTuningProtocolBM(); - } else if (deviceType == "04") + } else if (deviceType == "04" || deviceType.contains("SignalGenerator") == true) { return new SignalGeneratorProtocolBM(); - } else if (deviceType == "05") + } else if (deviceType == "05" || deviceType.contains("TimeSwitcher") == true) { return new TimeSwitcherProtocolBM(); - } else if (deviceType == "06") + } else if (deviceType == "06" || deviceType.contains("FreqSwitcher") == true) { return new FreqSwitcherProtocolBM(); - } else if (deviceType == "07") + } else if (deviceType == "07" || deviceType.contains("BCodeTerminal") == true) { return new BCodeTerminalProtocolBM(); - } else if (deviceType == "10") + } else if (deviceType == "09" || deviceType.contains("TimeReplicator") == true) + { + return new TimeReplicatorProtocolBM(); + } else if (deviceType == "10" || deviceType.contains("FreqReplicator") == true) { return new FreqReplicatorProtocolTX(); } diff --git a/DeviceHub/TimeReplicatorForm.cpp b/DeviceHub/TimeReplicatorForm.cpp index eb83370..0a6c833 100644 --- a/DeviceHub/TimeReplicatorForm.cpp +++ b/DeviceHub/TimeReplicatorForm.cpp @@ -1,5 +1,6 @@ #include "TimeReplicatorForm.h" #include "ui_TimeReplicatorForm.h" +#include "DeviceHubWindow.h" TimeReplicatorForm::TimeReplicatorForm(QWidget *parent) : QWidget(parent), @@ -15,5 +16,59 @@ void TimeReplicatorForm::on_tmRepButt_clicked() { - + // 获取设备对象 + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + TimeReplicator * device = (TimeReplicator *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09").at(devIndex); + device->mockReceivData(); } + + +void TimeReplicatorForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // 当前显示的设备编号 + if (frameData->frameType == "0901") + { + TimeReplicatorStatusDto * statusFrameDto = (TimeReplicatorStatusDto *) frameData; + ui->trDevStatus->setText(statusFrameDto->devStatus == "1" ? "正常" : "异常"); + ui->trInCount->setText(QString("%1").arg(statusFrameDto->inCount)); + + QJsonObject dataObj = statusFrameDto->toJSON().find("data")->toObject(); + QJsonDocument doc; + + doc.setArray(dataObj.find("inValid")->toArray()); + ui->trInValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + + doc.setArray(dataObj.find("outValid")->toArray()); + ui->trOutValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + } +} + +void TimeReplicatorForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "09") + { + ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->setCurrentIndex(i); + } + } + for (int i = 0; i < typeDevList.size(); i++) + { + if (typeDevList.at(i)->getDeviceId() == command.value("deviceId").toString()) + { + ((DeviceHubWindow *)this->parent()->parent())->getDevSelect()->setCurrentIndex(i); + } + } + + QString commandType = command.value("command").toString(); + if (commandType == "1") + { + + } else if (commandType == "2") + { + + } +} + diff --git a/DeviceHub/TimeReplicatorForm.h b/DeviceHub/TimeReplicatorForm.h index e6c9dc8..dc73d85 100644 --- a/DeviceHub/TimeReplicatorForm.h +++ b/DeviceHub/TimeReplicatorForm.h @@ -2,6 +2,7 @@ #define TIMEREPLICATORFORM_H #include +#include "device/TimeReplicator.h" namespace Ui { class TimeReplicatorForm; @@ -15,6 +16,10 @@ explicit TimeReplicatorForm(QWidget *parent = nullptr); ~TimeReplicatorForm(); +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + private slots: void on_tmRepButt_clicked(); diff --git a/DeviceHub/TimeReplicatorForm.ui b/DeviceHub/TimeReplicatorForm.ui index bf39894..c2f6ef4 100644 --- a/DeviceHub/TimeReplicatorForm.ui +++ b/DeviceHub/TimeReplicatorForm.ui @@ -1,34 +1,186 @@ + - - - TimeReplicatorForm - + 0 0 - 400 - 300 + 1200 + 600 Form - - - 50 - 20 - 180 - 40 - - - - Mock TimeReplicator - - + + + 50 + 20 + 180 + 40 + + + + Mock TimeReplicator + + + + + + 20 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 设备工作状态 + + + + + + 100 + 120 + 50 + 30 + + + + + + + 20 + 80 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 工作状态 + + + + + + 20 + 290 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 参数设置 + + + + + + 170 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 输入路数量 + + + + + + 260 + 120 + 50 + 30 + + + + + + + 350 + 120 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输入状态 + + + + + + 410 + 120 + 200 + 30 + + + + + + + 20 + 180 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输出状态 + + + + + + 100 + 180 + 500 + 30 + + + - + diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index 289beb5..4b69272 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -4,6 +4,7 @@ #include "TimeSwitcher.h" #include "FreqSwitcher.h" #include "BCodeTerminal.h" +#include "TimeReplicator.h" #include "FreqReplicator.h" #include @@ -56,6 +57,9 @@ } else if (deviceType == "07") { base = new BCodeTerminal(parent); + } else if (deviceType == "09") + { + base = new TimeReplicator(parent); } else if (deviceType == "10") { base = new FreqReplicator(parent); diff --git a/DeviceHub/device/TimeReplicator.cpp b/DeviceHub/device/TimeReplicator.cpp index 1ba0db4..4ca4547 100644 --- a/DeviceHub/device/TimeReplicator.cpp +++ b/DeviceHub/device/TimeReplicator.cpp @@ -1,18 +1,27 @@ #include "TimeReplicator.h" - +#include "DeviceHubWindow.h" #include #include TimeReplicator::TimeReplicator(QObject *parent) : DeviceBase(parent) { + this->devType = "09"; connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &TimeReplicator::dataReceivedHandler); - kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); - kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); - kafkaUtil.createProducer(); + connect(this, &TimeReplicator::sendDataToDraw, + ((DeviceHubWindow *)this->parent())->timeRepForm, &TimeReplicatorForm::drawDeviceFrameOnForm); + connect(this, &TimeReplicator::sendCommandToDisplay, + ((DeviceHubWindow *)this->parent())->timeRepForm, &TimeReplicatorForm::displayDeviceCommandOnForm); - this->protocol = DeviceStatusProtocolBase::deviceStatusProtocolFactory(typeid (this).name()); + connect(((DeviceHubWindow *)this->parent())->kafkaConsumer, &QKafkaConsumer::messageRecieved, + this, &TimeReplicator::commandReceivedHandler); + + kafkaProducer.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaProducer.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaProducer.createProducer(); + + this->protocol = DeviceProtocolBase::deviceProtocolFactory(devType); } TimeReplicator::~TimeReplicator() @@ -21,6 +30,20 @@ this, &TimeReplicator::dataReceivedHandler); } +void TimeReplicator::mockReceivData() +{ + QByteArray buffer; + + // time replicator + buffer.append("$2B21308-13 21010012200000000000000000faf0*"); + buffer.append("$3C21308-13 2101008220322222222111111110000000000000000ca24*"); + buffer.append("$3C21308-13 21010052207111111111111111111111111000000005984*"); +// buffer.append("$3C21308-13 2101005121511111111111111111111111111111111bcfe*"); + buffer.append("$3E21308-13 2101001211308-13 2101001210929H.1.00S.1.00000292b*"); + + this->dataReceivedHandler(buffer); +} + void TimeReplicator::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); @@ -50,6 +73,8 @@ frameDto->milisecond = now.toMSecsSinceEpoch(); frameDto->rawFrame = frameByte; + frameDto->devCode = devCode; + this->afterFramePhase(frameDto); } @@ -67,12 +92,83 @@ std::cout << "frame type: " << typeid(* frameDto).name() << std::endl; std::cout << frameDto->rawFrame.toStdString() << std::endl; + // 0. 输出到日志文件中 + QString date = frameDto->timestamp.mid(0, 10); + + // 1. 原始字节数组数据 + QString filename = "raw_" + devCode + ".log"; + QString content = frameDto->timestamp + " [recv] " + frameDto->rawFrame.left(frameDto->rawFrame.size() - FRAME_TAIL.size()); + QLogUtil::writeRawDataLogByDate(date, filename, content); + + // 2. 解析后的json数据 + QString frameFilename = "frame_" + devCode + ".log"; + QString frameContent = frameDto->timestamp + " [recv] " + QJsonDocument(frameDto->toJSON()).toJson(QJsonDocument::Compact); + QLogUtil::writeChannelDataLogByDate(date, frameFilename, frameContent); + // 3. 输出到中间件,执行后续处理过程 if (SettingConfig::getInstance().NEED_KAFKA == 1) { QJsonObject jsonObj = frameDto->toJSON(); jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); jsonObj.insert("deviceId", deviceId); - kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + kafkaProducer.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); } + + // 4. 在界面上简单显示相差数据结果 + emit this->sendDataToDraw(frameDto); } + +void TimeReplicator::sendDataToSerial(QByteArray data) +{ + data.append(FRAME_TAIL); + this->serialUtil.sendData(data); + + // 记录日志 + // 0. 输出到日志文件中 + QDateTime now = QDateTime::currentDateTime(); + QString date = now.toString("yyyy-MM-dd"); + + // 1. 原始字节数组数据 + QString filename = "raw_" + devCode + ".log"; + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + data.left(data.size() - FRAME_TAIL.size()); + QLogUtil::writeRawDataLogByDate(date, filename, content); +} + +void TimeReplicator::commandReceivedHandler(QJsonObject command) +{ + if (command.contains("deviceType") == false || command.value("deviceType").toString() != "09") + { + std::cout << "device type [" << command.value("deviceType").toString().toStdString() << "] not matched. return" << std::endl; + return; + } + + if (command.contains("deviceId") == false || command.value("deviceId").toString() != deviceId) + { + std::cout << "deviceId [" << command.value("deviceId").toString().toStdString() << "] not matched. return" << std::endl; + return; + } + + // 记录日志 + // 0. 输出到日志文件中 + QDateTime now = QDateTime::currentDateTime(); + QString date = now.toString("yyyy-MM-dd"); + + // 1. 原始字节数组数据 + QString filename = "frame_" + devCode + ".log"; + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + command.find("cmdStr")->toString(); + QLogUtil::writeChannelDataLogByDate(date, filename, content); + + QByteArray commandBytes = protocol->generateSettingCommand(command.value("command").toString(), command.value("params").toString()); + this->sendDataToSerial(commandBytes); + command.insert("rawCommand", QString::fromUtf8(commandBytes)); + + QJsonObject cmdcb; + cmdcb.insert("commandId", command.value("commandId").toString()); + cmdcb.insert("rawCommand", QString::fromUtf8(commandBytes)); + cmdcb.insert("status", "1"); + this->kafkaProducer.produceMessage(SettingConfig::getInstance().KAFKA_CMDCB_TOPIC, QString(QJsonDocument(cmdcb).toJson(QJsonDocument::Compact))); + + // display on page + emit sendCommandToDisplay(command); +} + diff --git a/DeviceHub/device/TimeReplicator.h b/DeviceHub/device/TimeReplicator.h index 5acf6d9..12b5ff4 100644 --- a/DeviceHub/device/TimeReplicator.h +++ b/DeviceHub/device/TimeReplicator.h @@ -4,6 +4,7 @@ #include #include "device/DeviceBase.h" +#include "protocol/dto/TimeReplicatorStatusDto.h" class TimeReplicator : public DeviceBase { @@ -12,13 +13,19 @@ explicit TimeReplicator(QObject *parent = nullptr); ~TimeReplicator(); + void mockReceivData(); + void afterFramePhase(DeviceFrameBaseDto * frameDto); + void sendDataToSerial(QByteArray data); signals: void sendDataToDraw(DeviceFrameBaseDto * frameData); + void sendCommandToDisplay(QJsonObject command); public slots: void dataReceivedHandler(QByteArray data); + void commandReceivedHandler(QJsonObject command); + }; #endif // TIMEREPLICATOR_H diff --git a/DeviceHub/device/device.pri b/DeviceHub/device/device.pri index 645dbcb..fedab85 100644 --- a/DeviceHub/device/device.pri +++ b/DeviceHub/device/device.pri @@ -4,7 +4,7 @@ HEADERS += $$PWD/SignalGenerator.h HEADERS += $$PWD/TimeSwitcher.h HEADERS += $$PWD/FreqSwitcher.h -#HEADERS += $$PWD/TimeReplicator.h +HEADERS += $$PWD/TimeReplicator.h HEADERS += $$PWD/FreqReplicator.h HEADERS += $$PWD/BCodeTerminal.h @@ -13,6 +13,6 @@ SOURCES += $$PWD/SignalGenerator.cpp SOURCES += $$PWD/TimeSwitcher.cpp SOURCES += $$PWD/FreqSwitcher.cpp -#SOURCES += $$PWD/TimeReplicator.cpp +SOURCES += $$PWD/TimeReplicator.cpp SOURCES += $$PWD/FreqReplicator.cpp SOURCES += $$PWD/BCodeTerminal.cpp diff --git a/DeviceHub/protocol/DeviceProtocolBase.cpp b/DeviceHub/protocol/DeviceProtocolBase.cpp index e7b26ca..4109e8d 100644 --- a/DeviceHub/protocol/DeviceProtocolBase.cpp +++ b/DeviceHub/protocol/DeviceProtocolBase.cpp @@ -3,7 +3,7 @@ #include "SignalGeneratorProtocolBM.h" #include "TimeSwitcherProtocolBM.h" #include "FreqSwitcherProtocolBM.h" -//#include "TimeReplicatorProtocolBM.h" +#include "TimeReplicatorProtocolBM.h" #include "FreqReplicatorProtocolTX.h" #include "BCodeTerminalProtocolBM.h" @@ -16,22 +16,25 @@ DeviceProtocolBase * DeviceProtocolBase::deviceProtocolFactory(QString deviceType) { - if (deviceType == "03") + if (deviceType == "03" || deviceType.contains("FrequencyTuning") == true) { return new FrequencyTuningProtocolBM(); - } else if (deviceType == "04") + } else if (deviceType == "04" || deviceType.contains("SignalGenerator") == true) { return new SignalGeneratorProtocolBM(); - } else if (deviceType == "05") + } else if (deviceType == "05" || deviceType.contains("TimeSwitcher") == true) { return new TimeSwitcherProtocolBM(); - } else if (deviceType == "06") + } else if (deviceType == "06" || deviceType.contains("FreqSwitcher") == true) { return new FreqSwitcherProtocolBM(); - } else if (deviceType == "07") + } else if (deviceType == "07" || deviceType.contains("BCodeTerminal") == true) { return new BCodeTerminalProtocolBM(); - } else if (deviceType == "10") + } else if (deviceType == "09" || deviceType.contains("TimeReplicator") == true) + { + return new TimeReplicatorProtocolBM(); + } else if (deviceType == "10" || deviceType.contains("FreqReplicator") == true) { return new FreqReplicatorProtocolTX(); } diff --git a/DeviceHub/protocol/TimeReplicatorProtocolBM.cpp b/DeviceHub/protocol/TimeReplicatorProtocolBM.cpp new file mode 100644 index 0000000..dbd6914 --- /dev/null +++ b/DeviceHub/protocol/TimeReplicatorProtocolBM.cpp @@ -0,0 +1,111 @@ +#include "TimeReplicatorProtocolBM.h" + +TimeReplicatorProtocolBM::TimeReplicatorProtocolBM(QObject *parent) : DeviceProtocolBase(parent) +{ + +} + +TimeReplicatorProtocolBM::~TimeReplicatorProtocolBM() +{ + +} + +bool TimeReplicatorProtocolBM::parseDeviceFrameData(QByteArray rawData, DeviceFrameBaseDto *frameData, int frameType) +{ + if (frameType != 1) + return false; + + qint8 length = rawData.mid(1, 2).toInt(0, 16); + QByteArray content = rawData.mid(19, length - 24); + qint8 count; + int inputByte = 0; + QString outStr = ""; + + ((TimeReplicatorStatusDto *) frameData)->devStatus = content.at(0); + if (length < 0x30) + { + count = 2; + inputByte = content.mid(2, 1).toInt(); + outStr = content.mid(3, 8 * count); + } else + { + count = 4; + inputByte = content.mid(2, 2).toInt(); + outStr = content.mid(4, 8 * count); + } + + ((TimeReplicatorStatusDto *) frameData)->inCount = count; + for (int i = 0; i < count; i++) + { + qint8 in = inputByte % 2; + ((TimeReplicatorStatusDto *) frameData)->inValid.append(in == 1 ? "1" : "0"); + inputByte = inputByte / 2; + + QString output = ""; + for (int j = 0; j < 8; j++) + { + output += outStr.at(i * 8 + j); + output += ","; + } + ((TimeReplicatorStatusDto *) frameData)->outValid.append(output.mid(0, output.size() - 1)); + } + + ((TimeReplicatorStatusDto *)frameData)->frameType = TIME_REPLICATOR_STATUS_FRAME_TYPE; + + return true; +} + +QByteArray TimeReplicatorProtocolBM::generateSettingCommand(QString commandType, QString valueSet) +{ + QByteArray commandBytes; + + commandBytes.append("1308-13").append(0x20); + commandBytes.append("2101001"); + commandBytes.append(commandType); + commandBytes.append(valueSet.replace(",", "")); + commandBytes.append(this->buildCRC16(commandBytes)); + commandBytes.append("*"); + + commandBytes.prepend(QString::number(commandBytes.size() + 3, 16).toUpper().toLocal8Bit()); + commandBytes.prepend("$"); + + return commandBytes; +} + +int TimeReplicatorProtocolBM::checkFrame(QByteArray rawData) +{ + if (rawData.at(0) == '$' && rawData.at(rawData.size() - 1) == '*' && + rawData.at(3) == '2' && rawData.at(20) == '2' && + rawData.size() == rawData.mid(1, 2).toInt(0, 16)) + return 1; + return 0; +} + +DeviceFrameBaseDto * TimeReplicatorProtocolBM::frameFactory(int frameType) +{ + DeviceFrameBaseDto * frameData = new TimeReplicatorStatusDto(); + + return frameData; +} + +QList TimeReplicatorProtocolBM::extractFrameList(QByteArray rawData) +{ + QList resultList; + while (rawData.endsWith(QByteArray("*")) == true && rawData.startsWith("$") == true) + { + QByteArray ba; + qint8 length = rawData.mid(1, 2).toInt(0, 16); + if (rawData.at(0) == '$' && rawData.at(length - 1) == '*') + { + ba.append(rawData.mid(0, length)); + resultList.append(ba); + ba.clear(); + rawData = rawData.mid(length, rawData.size() - length); + } else + { + break; + } + } + + return resultList; +} diff --git a/DeviceHub/TimeReplicatorForm.cpp b/DeviceHub/TimeReplicatorForm.cpp index eb83370..0a6c833 100644 --- a/DeviceHub/TimeReplicatorForm.cpp +++ b/DeviceHub/TimeReplicatorForm.cpp @@ -1,5 +1,6 @@ #include "TimeReplicatorForm.h" #include "ui_TimeReplicatorForm.h" +#include "DeviceHubWindow.h" TimeReplicatorForm::TimeReplicatorForm(QWidget *parent) : QWidget(parent), @@ -15,5 +16,59 @@ void TimeReplicatorForm::on_tmRepButt_clicked() { - + // 获取设备对象 + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + TimeReplicator * device = (TimeReplicator *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09").at(devIndex); + device->mockReceivData(); } + + +void TimeReplicatorForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // 当前显示的设备编号 + if (frameData->frameType == "0901") + { + TimeReplicatorStatusDto * statusFrameDto = (TimeReplicatorStatusDto *) frameData; + ui->trDevStatus->setText(statusFrameDto->devStatus == "1" ? "正常" : "异常"); + ui->trInCount->setText(QString("%1").arg(statusFrameDto->inCount)); + + QJsonObject dataObj = statusFrameDto->toJSON().find("data")->toObject(); + QJsonDocument doc; + + doc.setArray(dataObj.find("inValid")->toArray()); + ui->trInValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + + doc.setArray(dataObj.find("outValid")->toArray()); + ui->trOutValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + } +} + +void TimeReplicatorForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "09") + { + ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->setCurrentIndex(i); + } + } + for (int i = 0; i < typeDevList.size(); i++) + { + if (typeDevList.at(i)->getDeviceId() == command.value("deviceId").toString()) + { + ((DeviceHubWindow *)this->parent()->parent())->getDevSelect()->setCurrentIndex(i); + } + } + + QString commandType = command.value("command").toString(); + if (commandType == "1") + { + + } else if (commandType == "2") + { + + } +} + diff --git a/DeviceHub/TimeReplicatorForm.h b/DeviceHub/TimeReplicatorForm.h index e6c9dc8..dc73d85 100644 --- a/DeviceHub/TimeReplicatorForm.h +++ b/DeviceHub/TimeReplicatorForm.h @@ -2,6 +2,7 @@ #define TIMEREPLICATORFORM_H #include +#include "device/TimeReplicator.h" namespace Ui { class TimeReplicatorForm; @@ -15,6 +16,10 @@ explicit TimeReplicatorForm(QWidget *parent = nullptr); ~TimeReplicatorForm(); +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + private slots: void on_tmRepButt_clicked(); diff --git a/DeviceHub/TimeReplicatorForm.ui b/DeviceHub/TimeReplicatorForm.ui index bf39894..c2f6ef4 100644 --- a/DeviceHub/TimeReplicatorForm.ui +++ b/DeviceHub/TimeReplicatorForm.ui @@ -1,34 +1,186 @@ + - - - TimeReplicatorForm - + 0 0 - 400 - 300 + 1200 + 600 Form - - - 50 - 20 - 180 - 40 - - - - Mock TimeReplicator - - + + + 50 + 20 + 180 + 40 + + + + Mock TimeReplicator + + + + + + 20 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 设备工作状态 + + + + + + 100 + 120 + 50 + 30 + + + + + + + 20 + 80 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 工作状态 + + + + + + 20 + 290 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 参数设置 + + + + + + 170 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 输入路数量 + + + + + + 260 + 120 + 50 + 30 + + + + + + + 350 + 120 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输入状态 + + + + + + 410 + 120 + 200 + 30 + + + + + + + 20 + 180 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输出状态 + + + + + + 100 + 180 + 500 + 30 + + + - + diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index 289beb5..4b69272 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -4,6 +4,7 @@ #include "TimeSwitcher.h" #include "FreqSwitcher.h" #include "BCodeTerminal.h" +#include "TimeReplicator.h" #include "FreqReplicator.h" #include @@ -56,6 +57,9 @@ } else if (deviceType == "07") { base = new BCodeTerminal(parent); + } else if (deviceType == "09") + { + base = new TimeReplicator(parent); } else if (deviceType == "10") { base = new FreqReplicator(parent); diff --git a/DeviceHub/device/TimeReplicator.cpp b/DeviceHub/device/TimeReplicator.cpp index 1ba0db4..4ca4547 100644 --- a/DeviceHub/device/TimeReplicator.cpp +++ b/DeviceHub/device/TimeReplicator.cpp @@ -1,18 +1,27 @@ #include "TimeReplicator.h" - +#include "DeviceHubWindow.h" #include #include TimeReplicator::TimeReplicator(QObject *parent) : DeviceBase(parent) { + this->devType = "09"; connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &TimeReplicator::dataReceivedHandler); - kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); - kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); - kafkaUtil.createProducer(); + connect(this, &TimeReplicator::sendDataToDraw, + ((DeviceHubWindow *)this->parent())->timeRepForm, &TimeReplicatorForm::drawDeviceFrameOnForm); + connect(this, &TimeReplicator::sendCommandToDisplay, + ((DeviceHubWindow *)this->parent())->timeRepForm, &TimeReplicatorForm::displayDeviceCommandOnForm); - this->protocol = DeviceStatusProtocolBase::deviceStatusProtocolFactory(typeid (this).name()); + connect(((DeviceHubWindow *)this->parent())->kafkaConsumer, &QKafkaConsumer::messageRecieved, + this, &TimeReplicator::commandReceivedHandler); + + kafkaProducer.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaProducer.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaProducer.createProducer(); + + this->protocol = DeviceProtocolBase::deviceProtocolFactory(devType); } TimeReplicator::~TimeReplicator() @@ -21,6 +30,20 @@ this, &TimeReplicator::dataReceivedHandler); } +void TimeReplicator::mockReceivData() +{ + QByteArray buffer; + + // time replicator + buffer.append("$2B21308-13 21010012200000000000000000faf0*"); + buffer.append("$3C21308-13 2101008220322222222111111110000000000000000ca24*"); + buffer.append("$3C21308-13 21010052207111111111111111111111111000000005984*"); +// buffer.append("$3C21308-13 2101005121511111111111111111111111111111111bcfe*"); + buffer.append("$3E21308-13 2101001211308-13 2101001210929H.1.00S.1.00000292b*"); + + this->dataReceivedHandler(buffer); +} + void TimeReplicator::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); @@ -50,6 +73,8 @@ frameDto->milisecond = now.toMSecsSinceEpoch(); frameDto->rawFrame = frameByte; + frameDto->devCode = devCode; + this->afterFramePhase(frameDto); } @@ -67,12 +92,83 @@ std::cout << "frame type: " << typeid(* frameDto).name() << std::endl; std::cout << frameDto->rawFrame.toStdString() << std::endl; + // 0. 输出到日志文件中 + QString date = frameDto->timestamp.mid(0, 10); + + // 1. 原始字节数组数据 + QString filename = "raw_" + devCode + ".log"; + QString content = frameDto->timestamp + " [recv] " + frameDto->rawFrame.left(frameDto->rawFrame.size() - FRAME_TAIL.size()); + QLogUtil::writeRawDataLogByDate(date, filename, content); + + // 2. 解析后的json数据 + QString frameFilename = "frame_" + devCode + ".log"; + QString frameContent = frameDto->timestamp + " [recv] " + QJsonDocument(frameDto->toJSON()).toJson(QJsonDocument::Compact); + QLogUtil::writeChannelDataLogByDate(date, frameFilename, frameContent); + // 3. 输出到中间件,执行后续处理过程 if (SettingConfig::getInstance().NEED_KAFKA == 1) { QJsonObject jsonObj = frameDto->toJSON(); jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); jsonObj.insert("deviceId", deviceId); - kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + kafkaProducer.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); } + + // 4. 在界面上简单显示相差数据结果 + emit this->sendDataToDraw(frameDto); } + +void TimeReplicator::sendDataToSerial(QByteArray data) +{ + data.append(FRAME_TAIL); + this->serialUtil.sendData(data); + + // 记录日志 + // 0. 输出到日志文件中 + QDateTime now = QDateTime::currentDateTime(); + QString date = now.toString("yyyy-MM-dd"); + + // 1. 原始字节数组数据 + QString filename = "raw_" + devCode + ".log"; + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + data.left(data.size() - FRAME_TAIL.size()); + QLogUtil::writeRawDataLogByDate(date, filename, content); +} + +void TimeReplicator::commandReceivedHandler(QJsonObject command) +{ + if (command.contains("deviceType") == false || command.value("deviceType").toString() != "09") + { + std::cout << "device type [" << command.value("deviceType").toString().toStdString() << "] not matched. return" << std::endl; + return; + } + + if (command.contains("deviceId") == false || command.value("deviceId").toString() != deviceId) + { + std::cout << "deviceId [" << command.value("deviceId").toString().toStdString() << "] not matched. return" << std::endl; + return; + } + + // 记录日志 + // 0. 输出到日志文件中 + QDateTime now = QDateTime::currentDateTime(); + QString date = now.toString("yyyy-MM-dd"); + + // 1. 原始字节数组数据 + QString filename = "frame_" + devCode + ".log"; + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + command.find("cmdStr")->toString(); + QLogUtil::writeChannelDataLogByDate(date, filename, content); + + QByteArray commandBytes = protocol->generateSettingCommand(command.value("command").toString(), command.value("params").toString()); + this->sendDataToSerial(commandBytes); + command.insert("rawCommand", QString::fromUtf8(commandBytes)); + + QJsonObject cmdcb; + cmdcb.insert("commandId", command.value("commandId").toString()); + cmdcb.insert("rawCommand", QString::fromUtf8(commandBytes)); + cmdcb.insert("status", "1"); + this->kafkaProducer.produceMessage(SettingConfig::getInstance().KAFKA_CMDCB_TOPIC, QString(QJsonDocument(cmdcb).toJson(QJsonDocument::Compact))); + + // display on page + emit sendCommandToDisplay(command); +} + diff --git a/DeviceHub/device/TimeReplicator.h b/DeviceHub/device/TimeReplicator.h index 5acf6d9..12b5ff4 100644 --- a/DeviceHub/device/TimeReplicator.h +++ b/DeviceHub/device/TimeReplicator.h @@ -4,6 +4,7 @@ #include #include "device/DeviceBase.h" +#include "protocol/dto/TimeReplicatorStatusDto.h" class TimeReplicator : public DeviceBase { @@ -12,13 +13,19 @@ explicit TimeReplicator(QObject *parent = nullptr); ~TimeReplicator(); + void mockReceivData(); + void afterFramePhase(DeviceFrameBaseDto * frameDto); + void sendDataToSerial(QByteArray data); signals: void sendDataToDraw(DeviceFrameBaseDto * frameData); + void sendCommandToDisplay(QJsonObject command); public slots: void dataReceivedHandler(QByteArray data); + void commandReceivedHandler(QJsonObject command); + }; #endif // TIMEREPLICATOR_H diff --git a/DeviceHub/device/device.pri b/DeviceHub/device/device.pri index 645dbcb..fedab85 100644 --- a/DeviceHub/device/device.pri +++ b/DeviceHub/device/device.pri @@ -4,7 +4,7 @@ HEADERS += $$PWD/SignalGenerator.h HEADERS += $$PWD/TimeSwitcher.h HEADERS += $$PWD/FreqSwitcher.h -#HEADERS += $$PWD/TimeReplicator.h +HEADERS += $$PWD/TimeReplicator.h HEADERS += $$PWD/FreqReplicator.h HEADERS += $$PWD/BCodeTerminal.h @@ -13,6 +13,6 @@ SOURCES += $$PWD/SignalGenerator.cpp SOURCES += $$PWD/TimeSwitcher.cpp SOURCES += $$PWD/FreqSwitcher.cpp -#SOURCES += $$PWD/TimeReplicator.cpp +SOURCES += $$PWD/TimeReplicator.cpp SOURCES += $$PWD/FreqReplicator.cpp SOURCES += $$PWD/BCodeTerminal.cpp diff --git a/DeviceHub/protocol/DeviceProtocolBase.cpp b/DeviceHub/protocol/DeviceProtocolBase.cpp index e7b26ca..4109e8d 100644 --- a/DeviceHub/protocol/DeviceProtocolBase.cpp +++ b/DeviceHub/protocol/DeviceProtocolBase.cpp @@ -3,7 +3,7 @@ #include "SignalGeneratorProtocolBM.h" #include "TimeSwitcherProtocolBM.h" #include "FreqSwitcherProtocolBM.h" -//#include "TimeReplicatorProtocolBM.h" +#include "TimeReplicatorProtocolBM.h" #include "FreqReplicatorProtocolTX.h" #include "BCodeTerminalProtocolBM.h" @@ -16,22 +16,25 @@ DeviceProtocolBase * DeviceProtocolBase::deviceProtocolFactory(QString deviceType) { - if (deviceType == "03") + if (deviceType == "03" || deviceType.contains("FrequencyTuning") == true) { return new FrequencyTuningProtocolBM(); - } else if (deviceType == "04") + } else if (deviceType == "04" || deviceType.contains("SignalGenerator") == true) { return new SignalGeneratorProtocolBM(); - } else if (deviceType == "05") + } else if (deviceType == "05" || deviceType.contains("TimeSwitcher") == true) { return new TimeSwitcherProtocolBM(); - } else if (deviceType == "06") + } else if (deviceType == "06" || deviceType.contains("FreqSwitcher") == true) { return new FreqSwitcherProtocolBM(); - } else if (deviceType == "07") + } else if (deviceType == "07" || deviceType.contains("BCodeTerminal") == true) { return new BCodeTerminalProtocolBM(); - } else if (deviceType == "10") + } else if (deviceType == "09" || deviceType.contains("TimeReplicator") == true) + { + return new TimeReplicatorProtocolBM(); + } else if (deviceType == "10" || deviceType.contains("FreqReplicator") == true) { return new FreqReplicatorProtocolTX(); } diff --git a/DeviceHub/protocol/TimeReplicatorProtocolBM.cpp b/DeviceHub/protocol/TimeReplicatorProtocolBM.cpp new file mode 100644 index 0000000..dbd6914 --- /dev/null +++ b/DeviceHub/protocol/TimeReplicatorProtocolBM.cpp @@ -0,0 +1,111 @@ +#include "TimeReplicatorProtocolBM.h" + +TimeReplicatorProtocolBM::TimeReplicatorProtocolBM(QObject *parent) : DeviceProtocolBase(parent) +{ + +} + +TimeReplicatorProtocolBM::~TimeReplicatorProtocolBM() +{ + +} + +bool TimeReplicatorProtocolBM::parseDeviceFrameData(QByteArray rawData, DeviceFrameBaseDto *frameData, int frameType) +{ + if (frameType != 1) + return false; + + qint8 length = rawData.mid(1, 2).toInt(0, 16); + QByteArray content = rawData.mid(19, length - 24); + qint8 count; + int inputByte = 0; + QString outStr = ""; + + ((TimeReplicatorStatusDto *) frameData)->devStatus = content.at(0); + if (length < 0x30) + { + count = 2; + inputByte = content.mid(2, 1).toInt(); + outStr = content.mid(3, 8 * count); + } else + { + count = 4; + inputByte = content.mid(2, 2).toInt(); + outStr = content.mid(4, 8 * count); + } + + ((TimeReplicatorStatusDto *) frameData)->inCount = count; + for (int i = 0; i < count; i++) + { + qint8 in = inputByte % 2; + ((TimeReplicatorStatusDto *) frameData)->inValid.append(in == 1 ? "1" : "0"); + inputByte = inputByte / 2; + + QString output = ""; + for (int j = 0; j < 8; j++) + { + output += outStr.at(i * 8 + j); + output += ","; + } + ((TimeReplicatorStatusDto *) frameData)->outValid.append(output.mid(0, output.size() - 1)); + } + + ((TimeReplicatorStatusDto *)frameData)->frameType = TIME_REPLICATOR_STATUS_FRAME_TYPE; + + return true; +} + +QByteArray TimeReplicatorProtocolBM::generateSettingCommand(QString commandType, QString valueSet) +{ + QByteArray commandBytes; + + commandBytes.append("1308-13").append(0x20); + commandBytes.append("2101001"); + commandBytes.append(commandType); + commandBytes.append(valueSet.replace(",", "")); + commandBytes.append(this->buildCRC16(commandBytes)); + commandBytes.append("*"); + + commandBytes.prepend(QString::number(commandBytes.size() + 3, 16).toUpper().toLocal8Bit()); + commandBytes.prepend("$"); + + return commandBytes; +} + +int TimeReplicatorProtocolBM::checkFrame(QByteArray rawData) +{ + if (rawData.at(0) == '$' && rawData.at(rawData.size() - 1) == '*' && + rawData.at(3) == '2' && rawData.at(20) == '2' && + rawData.size() == rawData.mid(1, 2).toInt(0, 16)) + return 1; + return 0; +} + +DeviceFrameBaseDto * TimeReplicatorProtocolBM::frameFactory(int frameType) +{ + DeviceFrameBaseDto * frameData = new TimeReplicatorStatusDto(); + + return frameData; +} + +QList TimeReplicatorProtocolBM::extractFrameList(QByteArray rawData) +{ + QList resultList; + while (rawData.endsWith(QByteArray("*")) == true && rawData.startsWith("$") == true) + { + QByteArray ba; + qint8 length = rawData.mid(1, 2).toInt(0, 16); + if (rawData.at(0) == '$' && rawData.at(length - 1) == '*') + { + ba.append(rawData.mid(0, length)); + resultList.append(ba); + ba.clear(); + rawData = rawData.mid(length, rawData.size() - length); + } else + { + break; + } + } + + return resultList; +} diff --git a/DeviceHub/protocol/TimeReplicatorProtocolBM.h b/DeviceHub/protocol/TimeReplicatorProtocolBM.h new file mode 100644 index 0000000..a310e61 --- /dev/null +++ b/DeviceHub/protocol/TimeReplicatorProtocolBM.h @@ -0,0 +1,29 @@ +#ifndef TIMEREPLICATORPROTOCOLBM_H +#define TIMEREPLICATORPROTOCOLBM_H + +#include + +#include "common/utils/QByteUtil.h" +#include "DeviceProtocolBase.h" +#include "dto/TimeReplicatorStatusDto.h" + +class TimeReplicatorProtocolBM : public DeviceProtocolBase +{ + Q_OBJECT +public: + explicit TimeReplicatorProtocolBM(QObject *parent = nullptr); + ~TimeReplicatorProtocolBM(); + + // 解析数据 + bool parseDeviceFrameData(QByteArray rawData, DeviceFrameBaseDto * frameData, int frameType); + QByteArray generateSettingCommand(QString commandType, QString valueSet); + DeviceFrameBaseDto * frameFactory(int frameType); + + // + QList extractFrameList(QByteArray rawData); + + // 检测帧格式,帧头帧尾 + int checkFrame(QByteArray rawData); +}; + +#endif // TIMEREPLICATORPROTOCOLBM_H diff --git a/DeviceHub/TimeReplicatorForm.cpp b/DeviceHub/TimeReplicatorForm.cpp index eb83370..0a6c833 100644 --- a/DeviceHub/TimeReplicatorForm.cpp +++ b/DeviceHub/TimeReplicatorForm.cpp @@ -1,5 +1,6 @@ #include "TimeReplicatorForm.h" #include "ui_TimeReplicatorForm.h" +#include "DeviceHubWindow.h" TimeReplicatorForm::TimeReplicatorForm(QWidget *parent) : QWidget(parent), @@ -15,5 +16,59 @@ void TimeReplicatorForm::on_tmRepButt_clicked() { - + // 获取设备对象 + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + TimeReplicator * device = (TimeReplicator *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09").at(devIndex); + device->mockReceivData(); } + + +void TimeReplicatorForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // 当前显示的设备编号 + if (frameData->frameType == "0901") + { + TimeReplicatorStatusDto * statusFrameDto = (TimeReplicatorStatusDto *) frameData; + ui->trDevStatus->setText(statusFrameDto->devStatus == "1" ? "正常" : "异常"); + ui->trInCount->setText(QString("%1").arg(statusFrameDto->inCount)); + + QJsonObject dataObj = statusFrameDto->toJSON().find("data")->toObject(); + QJsonDocument doc; + + doc.setArray(dataObj.find("inValid")->toArray()); + ui->trInValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + + doc.setArray(dataObj.find("outValid")->toArray()); + ui->trOutValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + } +} + +void TimeReplicatorForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "09") + { + ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->setCurrentIndex(i); + } + } + for (int i = 0; i < typeDevList.size(); i++) + { + if (typeDevList.at(i)->getDeviceId() == command.value("deviceId").toString()) + { + ((DeviceHubWindow *)this->parent()->parent())->getDevSelect()->setCurrentIndex(i); + } + } + + QString commandType = command.value("command").toString(); + if (commandType == "1") + { + + } else if (commandType == "2") + { + + } +} + diff --git a/DeviceHub/TimeReplicatorForm.h b/DeviceHub/TimeReplicatorForm.h index e6c9dc8..dc73d85 100644 --- a/DeviceHub/TimeReplicatorForm.h +++ b/DeviceHub/TimeReplicatorForm.h @@ -2,6 +2,7 @@ #define TIMEREPLICATORFORM_H #include +#include "device/TimeReplicator.h" namespace Ui { class TimeReplicatorForm; @@ -15,6 +16,10 @@ explicit TimeReplicatorForm(QWidget *parent = nullptr); ~TimeReplicatorForm(); +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + private slots: void on_tmRepButt_clicked(); diff --git a/DeviceHub/TimeReplicatorForm.ui b/DeviceHub/TimeReplicatorForm.ui index bf39894..c2f6ef4 100644 --- a/DeviceHub/TimeReplicatorForm.ui +++ b/DeviceHub/TimeReplicatorForm.ui @@ -1,34 +1,186 @@ + - - - TimeReplicatorForm - + 0 0 - 400 - 300 + 1200 + 600 Form - - - 50 - 20 - 180 - 40 - - - - Mock TimeReplicator - - + + + 50 + 20 + 180 + 40 + + + + Mock TimeReplicator + + + + + + 20 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 设备工作状态 + + + + + + 100 + 120 + 50 + 30 + + + + + + + 20 + 80 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 工作状态 + + + + + + 20 + 290 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 参数设置 + + + + + + 170 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 输入路数量 + + + + + + 260 + 120 + 50 + 30 + + + + + + + 350 + 120 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输入状态 + + + + + + 410 + 120 + 200 + 30 + + + + + + + 20 + 180 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输出状态 + + + + + + 100 + 180 + 500 + 30 + + + - + diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index 289beb5..4b69272 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -4,6 +4,7 @@ #include "TimeSwitcher.h" #include "FreqSwitcher.h" #include "BCodeTerminal.h" +#include "TimeReplicator.h" #include "FreqReplicator.h" #include @@ -56,6 +57,9 @@ } else if (deviceType == "07") { base = new BCodeTerminal(parent); + } else if (deviceType == "09") + { + base = new TimeReplicator(parent); } else if (deviceType == "10") { base = new FreqReplicator(parent); diff --git a/DeviceHub/device/TimeReplicator.cpp b/DeviceHub/device/TimeReplicator.cpp index 1ba0db4..4ca4547 100644 --- a/DeviceHub/device/TimeReplicator.cpp +++ b/DeviceHub/device/TimeReplicator.cpp @@ -1,18 +1,27 @@ #include "TimeReplicator.h" - +#include "DeviceHubWindow.h" #include #include TimeReplicator::TimeReplicator(QObject *parent) : DeviceBase(parent) { + this->devType = "09"; connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &TimeReplicator::dataReceivedHandler); - kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); - kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); - kafkaUtil.createProducer(); + connect(this, &TimeReplicator::sendDataToDraw, + ((DeviceHubWindow *)this->parent())->timeRepForm, &TimeReplicatorForm::drawDeviceFrameOnForm); + connect(this, &TimeReplicator::sendCommandToDisplay, + ((DeviceHubWindow *)this->parent())->timeRepForm, &TimeReplicatorForm::displayDeviceCommandOnForm); - this->protocol = DeviceStatusProtocolBase::deviceStatusProtocolFactory(typeid (this).name()); + connect(((DeviceHubWindow *)this->parent())->kafkaConsumer, &QKafkaConsumer::messageRecieved, + this, &TimeReplicator::commandReceivedHandler); + + kafkaProducer.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaProducer.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaProducer.createProducer(); + + this->protocol = DeviceProtocolBase::deviceProtocolFactory(devType); } TimeReplicator::~TimeReplicator() @@ -21,6 +30,20 @@ this, &TimeReplicator::dataReceivedHandler); } +void TimeReplicator::mockReceivData() +{ + QByteArray buffer; + + // time replicator + buffer.append("$2B21308-13 21010012200000000000000000faf0*"); + buffer.append("$3C21308-13 2101008220322222222111111110000000000000000ca24*"); + buffer.append("$3C21308-13 21010052207111111111111111111111111000000005984*"); +// buffer.append("$3C21308-13 2101005121511111111111111111111111111111111bcfe*"); + buffer.append("$3E21308-13 2101001211308-13 2101001210929H.1.00S.1.00000292b*"); + + this->dataReceivedHandler(buffer); +} + void TimeReplicator::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); @@ -50,6 +73,8 @@ frameDto->milisecond = now.toMSecsSinceEpoch(); frameDto->rawFrame = frameByte; + frameDto->devCode = devCode; + this->afterFramePhase(frameDto); } @@ -67,12 +92,83 @@ std::cout << "frame type: " << typeid(* frameDto).name() << std::endl; std::cout << frameDto->rawFrame.toStdString() << std::endl; + // 0. 输出到日志文件中 + QString date = frameDto->timestamp.mid(0, 10); + + // 1. 原始字节数组数据 + QString filename = "raw_" + devCode + ".log"; + QString content = frameDto->timestamp + " [recv] " + frameDto->rawFrame.left(frameDto->rawFrame.size() - FRAME_TAIL.size()); + QLogUtil::writeRawDataLogByDate(date, filename, content); + + // 2. 解析后的json数据 + QString frameFilename = "frame_" + devCode + ".log"; + QString frameContent = frameDto->timestamp + " [recv] " + QJsonDocument(frameDto->toJSON()).toJson(QJsonDocument::Compact); + QLogUtil::writeChannelDataLogByDate(date, frameFilename, frameContent); + // 3. 输出到中间件,执行后续处理过程 if (SettingConfig::getInstance().NEED_KAFKA == 1) { QJsonObject jsonObj = frameDto->toJSON(); jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); jsonObj.insert("deviceId", deviceId); - kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + kafkaProducer.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); } + + // 4. 在界面上简单显示相差数据结果 + emit this->sendDataToDraw(frameDto); } + +void TimeReplicator::sendDataToSerial(QByteArray data) +{ + data.append(FRAME_TAIL); + this->serialUtil.sendData(data); + + // 记录日志 + // 0. 输出到日志文件中 + QDateTime now = QDateTime::currentDateTime(); + QString date = now.toString("yyyy-MM-dd"); + + // 1. 原始字节数组数据 + QString filename = "raw_" + devCode + ".log"; + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + data.left(data.size() - FRAME_TAIL.size()); + QLogUtil::writeRawDataLogByDate(date, filename, content); +} + +void TimeReplicator::commandReceivedHandler(QJsonObject command) +{ + if (command.contains("deviceType") == false || command.value("deviceType").toString() != "09") + { + std::cout << "device type [" << command.value("deviceType").toString().toStdString() << "] not matched. return" << std::endl; + return; + } + + if (command.contains("deviceId") == false || command.value("deviceId").toString() != deviceId) + { + std::cout << "deviceId [" << command.value("deviceId").toString().toStdString() << "] not matched. return" << std::endl; + return; + } + + // 记录日志 + // 0. 输出到日志文件中 + QDateTime now = QDateTime::currentDateTime(); + QString date = now.toString("yyyy-MM-dd"); + + // 1. 原始字节数组数据 + QString filename = "frame_" + devCode + ".log"; + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + command.find("cmdStr")->toString(); + QLogUtil::writeChannelDataLogByDate(date, filename, content); + + QByteArray commandBytes = protocol->generateSettingCommand(command.value("command").toString(), command.value("params").toString()); + this->sendDataToSerial(commandBytes); + command.insert("rawCommand", QString::fromUtf8(commandBytes)); + + QJsonObject cmdcb; + cmdcb.insert("commandId", command.value("commandId").toString()); + cmdcb.insert("rawCommand", QString::fromUtf8(commandBytes)); + cmdcb.insert("status", "1"); + this->kafkaProducer.produceMessage(SettingConfig::getInstance().KAFKA_CMDCB_TOPIC, QString(QJsonDocument(cmdcb).toJson(QJsonDocument::Compact))); + + // display on page + emit sendCommandToDisplay(command); +} + diff --git a/DeviceHub/device/TimeReplicator.h b/DeviceHub/device/TimeReplicator.h index 5acf6d9..12b5ff4 100644 --- a/DeviceHub/device/TimeReplicator.h +++ b/DeviceHub/device/TimeReplicator.h @@ -4,6 +4,7 @@ #include #include "device/DeviceBase.h" +#include "protocol/dto/TimeReplicatorStatusDto.h" class TimeReplicator : public DeviceBase { @@ -12,13 +13,19 @@ explicit TimeReplicator(QObject *parent = nullptr); ~TimeReplicator(); + void mockReceivData(); + void afterFramePhase(DeviceFrameBaseDto * frameDto); + void sendDataToSerial(QByteArray data); signals: void sendDataToDraw(DeviceFrameBaseDto * frameData); + void sendCommandToDisplay(QJsonObject command); public slots: void dataReceivedHandler(QByteArray data); + void commandReceivedHandler(QJsonObject command); + }; #endif // TIMEREPLICATOR_H diff --git a/DeviceHub/device/device.pri b/DeviceHub/device/device.pri index 645dbcb..fedab85 100644 --- a/DeviceHub/device/device.pri +++ b/DeviceHub/device/device.pri @@ -4,7 +4,7 @@ HEADERS += $$PWD/SignalGenerator.h HEADERS += $$PWD/TimeSwitcher.h HEADERS += $$PWD/FreqSwitcher.h -#HEADERS += $$PWD/TimeReplicator.h +HEADERS += $$PWD/TimeReplicator.h HEADERS += $$PWD/FreqReplicator.h HEADERS += $$PWD/BCodeTerminal.h @@ -13,6 +13,6 @@ SOURCES += $$PWD/SignalGenerator.cpp SOURCES += $$PWD/TimeSwitcher.cpp SOURCES += $$PWD/FreqSwitcher.cpp -#SOURCES += $$PWD/TimeReplicator.cpp +SOURCES += $$PWD/TimeReplicator.cpp SOURCES += $$PWD/FreqReplicator.cpp SOURCES += $$PWD/BCodeTerminal.cpp diff --git a/DeviceHub/protocol/DeviceProtocolBase.cpp b/DeviceHub/protocol/DeviceProtocolBase.cpp index e7b26ca..4109e8d 100644 --- a/DeviceHub/protocol/DeviceProtocolBase.cpp +++ b/DeviceHub/protocol/DeviceProtocolBase.cpp @@ -3,7 +3,7 @@ #include "SignalGeneratorProtocolBM.h" #include "TimeSwitcherProtocolBM.h" #include "FreqSwitcherProtocolBM.h" -//#include "TimeReplicatorProtocolBM.h" +#include "TimeReplicatorProtocolBM.h" #include "FreqReplicatorProtocolTX.h" #include "BCodeTerminalProtocolBM.h" @@ -16,22 +16,25 @@ DeviceProtocolBase * DeviceProtocolBase::deviceProtocolFactory(QString deviceType) { - if (deviceType == "03") + if (deviceType == "03" || deviceType.contains("FrequencyTuning") == true) { return new FrequencyTuningProtocolBM(); - } else if (deviceType == "04") + } else if (deviceType == "04" || deviceType.contains("SignalGenerator") == true) { return new SignalGeneratorProtocolBM(); - } else if (deviceType == "05") + } else if (deviceType == "05" || deviceType.contains("TimeSwitcher") == true) { return new TimeSwitcherProtocolBM(); - } else if (deviceType == "06") + } else if (deviceType == "06" || deviceType.contains("FreqSwitcher") == true) { return new FreqSwitcherProtocolBM(); - } else if (deviceType == "07") + } else if (deviceType == "07" || deviceType.contains("BCodeTerminal") == true) { return new BCodeTerminalProtocolBM(); - } else if (deviceType == "10") + } else if (deviceType == "09" || deviceType.contains("TimeReplicator") == true) + { + return new TimeReplicatorProtocolBM(); + } else if (deviceType == "10" || deviceType.contains("FreqReplicator") == true) { return new FreqReplicatorProtocolTX(); } diff --git a/DeviceHub/protocol/TimeReplicatorProtocolBM.cpp b/DeviceHub/protocol/TimeReplicatorProtocolBM.cpp new file mode 100644 index 0000000..dbd6914 --- /dev/null +++ b/DeviceHub/protocol/TimeReplicatorProtocolBM.cpp @@ -0,0 +1,111 @@ +#include "TimeReplicatorProtocolBM.h" + +TimeReplicatorProtocolBM::TimeReplicatorProtocolBM(QObject *parent) : DeviceProtocolBase(parent) +{ + +} + +TimeReplicatorProtocolBM::~TimeReplicatorProtocolBM() +{ + +} + +bool TimeReplicatorProtocolBM::parseDeviceFrameData(QByteArray rawData, DeviceFrameBaseDto *frameData, int frameType) +{ + if (frameType != 1) + return false; + + qint8 length = rawData.mid(1, 2).toInt(0, 16); + QByteArray content = rawData.mid(19, length - 24); + qint8 count; + int inputByte = 0; + QString outStr = ""; + + ((TimeReplicatorStatusDto *) frameData)->devStatus = content.at(0); + if (length < 0x30) + { + count = 2; + inputByte = content.mid(2, 1).toInt(); + outStr = content.mid(3, 8 * count); + } else + { + count = 4; + inputByte = content.mid(2, 2).toInt(); + outStr = content.mid(4, 8 * count); + } + + ((TimeReplicatorStatusDto *) frameData)->inCount = count; + for (int i = 0; i < count; i++) + { + qint8 in = inputByte % 2; + ((TimeReplicatorStatusDto *) frameData)->inValid.append(in == 1 ? "1" : "0"); + inputByte = inputByte / 2; + + QString output = ""; + for (int j = 0; j < 8; j++) + { + output += outStr.at(i * 8 + j); + output += ","; + } + ((TimeReplicatorStatusDto *) frameData)->outValid.append(output.mid(0, output.size() - 1)); + } + + ((TimeReplicatorStatusDto *)frameData)->frameType = TIME_REPLICATOR_STATUS_FRAME_TYPE; + + return true; +} + +QByteArray TimeReplicatorProtocolBM::generateSettingCommand(QString commandType, QString valueSet) +{ + QByteArray commandBytes; + + commandBytes.append("1308-13").append(0x20); + commandBytes.append("2101001"); + commandBytes.append(commandType); + commandBytes.append(valueSet.replace(",", "")); + commandBytes.append(this->buildCRC16(commandBytes)); + commandBytes.append("*"); + + commandBytes.prepend(QString::number(commandBytes.size() + 3, 16).toUpper().toLocal8Bit()); + commandBytes.prepend("$"); + + return commandBytes; +} + +int TimeReplicatorProtocolBM::checkFrame(QByteArray rawData) +{ + if (rawData.at(0) == '$' && rawData.at(rawData.size() - 1) == '*' && + rawData.at(3) == '2' && rawData.at(20) == '2' && + rawData.size() == rawData.mid(1, 2).toInt(0, 16)) + return 1; + return 0; +} + +DeviceFrameBaseDto * TimeReplicatorProtocolBM::frameFactory(int frameType) +{ + DeviceFrameBaseDto * frameData = new TimeReplicatorStatusDto(); + + return frameData; +} + +QList TimeReplicatorProtocolBM::extractFrameList(QByteArray rawData) +{ + QList resultList; + while (rawData.endsWith(QByteArray("*")) == true && rawData.startsWith("$") == true) + { + QByteArray ba; + qint8 length = rawData.mid(1, 2).toInt(0, 16); + if (rawData.at(0) == '$' && rawData.at(length - 1) == '*') + { + ba.append(rawData.mid(0, length)); + resultList.append(ba); + ba.clear(); + rawData = rawData.mid(length, rawData.size() - length); + } else + { + break; + } + } + + return resultList; +} diff --git a/DeviceHub/protocol/TimeReplicatorProtocolBM.h b/DeviceHub/protocol/TimeReplicatorProtocolBM.h new file mode 100644 index 0000000..a310e61 --- /dev/null +++ b/DeviceHub/protocol/TimeReplicatorProtocolBM.h @@ -0,0 +1,29 @@ +#ifndef TIMEREPLICATORPROTOCOLBM_H +#define TIMEREPLICATORPROTOCOLBM_H + +#include + +#include "common/utils/QByteUtil.h" +#include "DeviceProtocolBase.h" +#include "dto/TimeReplicatorStatusDto.h" + +class TimeReplicatorProtocolBM : public DeviceProtocolBase +{ + Q_OBJECT +public: + explicit TimeReplicatorProtocolBM(QObject *parent = nullptr); + ~TimeReplicatorProtocolBM(); + + // 解析数据 + bool parseDeviceFrameData(QByteArray rawData, DeviceFrameBaseDto * frameData, int frameType); + QByteArray generateSettingCommand(QString commandType, QString valueSet); + DeviceFrameBaseDto * frameFactory(int frameType); + + // + QList extractFrameList(QByteArray rawData); + + // 检测帧格式,帧头帧尾 + int checkFrame(QByteArray rawData); +}; + +#endif // TIMEREPLICATORPROTOCOLBM_H diff --git a/DeviceHub/protocol/dto/TimeReplicatorStatusDto.cpp b/DeviceHub/protocol/dto/TimeReplicatorStatusDto.cpp new file mode 100644 index 0000000..8f0dba7 --- /dev/null +++ b/DeviceHub/protocol/dto/TimeReplicatorStatusDto.cpp @@ -0,0 +1,32 @@ +#include "TimeReplicatorStatusDto.h" + +TimeReplicatorStatusDto::TimeReplicatorStatusDto(QObject *parent) : DeviceFrameBaseDto(parent) +{ + +} + +QJsonObject TimeReplicatorStatusDto::toJSON() +{ + QJsonObject jsonObj; + + QJsonObject dataObj; + dataObj.insert("devStatus", this->devStatus); + dataObj.insert("inCount", inCount); + + QJsonArray inputArray; + QJsonArray outputArray; + + for (int i = 0; i < inCount; i++) + { + inputArray.append(this->inValid.at(i)); + outputArray.append(outValid.at(i)); + } + dataObj.insert("inValid", inputArray); + dataObj.insert("outValid", outputArray); + + jsonObj.insert("ts", this->milisecond); + jsonObj.insert("frameType", this->frameType); + jsonObj.insert("data", dataObj); + + return jsonObj; +} diff --git a/DeviceHub/TimeReplicatorForm.cpp b/DeviceHub/TimeReplicatorForm.cpp index eb83370..0a6c833 100644 --- a/DeviceHub/TimeReplicatorForm.cpp +++ b/DeviceHub/TimeReplicatorForm.cpp @@ -1,5 +1,6 @@ #include "TimeReplicatorForm.h" #include "ui_TimeReplicatorForm.h" +#include "DeviceHubWindow.h" TimeReplicatorForm::TimeReplicatorForm(QWidget *parent) : QWidget(parent), @@ -15,5 +16,59 @@ void TimeReplicatorForm::on_tmRepButt_clicked() { - + // 获取设备对象 + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + TimeReplicator * device = (TimeReplicator *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09").at(devIndex); + device->mockReceivData(); } + + +void TimeReplicatorForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // 当前显示的设备编号 + if (frameData->frameType == "0901") + { + TimeReplicatorStatusDto * statusFrameDto = (TimeReplicatorStatusDto *) frameData; + ui->trDevStatus->setText(statusFrameDto->devStatus == "1" ? "正常" : "异常"); + ui->trInCount->setText(QString("%1").arg(statusFrameDto->inCount)); + + QJsonObject dataObj = statusFrameDto->toJSON().find("data")->toObject(); + QJsonDocument doc; + + doc.setArray(dataObj.find("inValid")->toArray()); + ui->trInValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + + doc.setArray(dataObj.find("outValid")->toArray()); + ui->trOutValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + } +} + +void TimeReplicatorForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "09") + { + ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->setCurrentIndex(i); + } + } + for (int i = 0; i < typeDevList.size(); i++) + { + if (typeDevList.at(i)->getDeviceId() == command.value("deviceId").toString()) + { + ((DeviceHubWindow *)this->parent()->parent())->getDevSelect()->setCurrentIndex(i); + } + } + + QString commandType = command.value("command").toString(); + if (commandType == "1") + { + + } else if (commandType == "2") + { + + } +} + diff --git a/DeviceHub/TimeReplicatorForm.h b/DeviceHub/TimeReplicatorForm.h index e6c9dc8..dc73d85 100644 --- a/DeviceHub/TimeReplicatorForm.h +++ b/DeviceHub/TimeReplicatorForm.h @@ -2,6 +2,7 @@ #define TIMEREPLICATORFORM_H #include +#include "device/TimeReplicator.h" namespace Ui { class TimeReplicatorForm; @@ -15,6 +16,10 @@ explicit TimeReplicatorForm(QWidget *parent = nullptr); ~TimeReplicatorForm(); +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + private slots: void on_tmRepButt_clicked(); diff --git a/DeviceHub/TimeReplicatorForm.ui b/DeviceHub/TimeReplicatorForm.ui index bf39894..c2f6ef4 100644 --- a/DeviceHub/TimeReplicatorForm.ui +++ b/DeviceHub/TimeReplicatorForm.ui @@ -1,34 +1,186 @@ + - - - TimeReplicatorForm - + 0 0 - 400 - 300 + 1200 + 600 Form - - - 50 - 20 - 180 - 40 - - - - Mock TimeReplicator - - + + + 50 + 20 + 180 + 40 + + + + Mock TimeReplicator + + + + + + 20 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 设备工作状态 + + + + + + 100 + 120 + 50 + 30 + + + + + + + 20 + 80 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 工作状态 + + + + + + 20 + 290 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 参数设置 + + + + + + 170 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 输入路数量 + + + + + + 260 + 120 + 50 + 30 + + + + + + + 350 + 120 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输入状态 + + + + + + 410 + 120 + 200 + 30 + + + + + + + 20 + 180 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输出状态 + + + + + + 100 + 180 + 500 + 30 + + + - + diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index 289beb5..4b69272 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -4,6 +4,7 @@ #include "TimeSwitcher.h" #include "FreqSwitcher.h" #include "BCodeTerminal.h" +#include "TimeReplicator.h" #include "FreqReplicator.h" #include @@ -56,6 +57,9 @@ } else if (deviceType == "07") { base = new BCodeTerminal(parent); + } else if (deviceType == "09") + { + base = new TimeReplicator(parent); } else if (deviceType == "10") { base = new FreqReplicator(parent); diff --git a/DeviceHub/device/TimeReplicator.cpp b/DeviceHub/device/TimeReplicator.cpp index 1ba0db4..4ca4547 100644 --- a/DeviceHub/device/TimeReplicator.cpp +++ b/DeviceHub/device/TimeReplicator.cpp @@ -1,18 +1,27 @@ #include "TimeReplicator.h" - +#include "DeviceHubWindow.h" #include #include TimeReplicator::TimeReplicator(QObject *parent) : DeviceBase(parent) { + this->devType = "09"; connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &TimeReplicator::dataReceivedHandler); - kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); - kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); - kafkaUtil.createProducer(); + connect(this, &TimeReplicator::sendDataToDraw, + ((DeviceHubWindow *)this->parent())->timeRepForm, &TimeReplicatorForm::drawDeviceFrameOnForm); + connect(this, &TimeReplicator::sendCommandToDisplay, + ((DeviceHubWindow *)this->parent())->timeRepForm, &TimeReplicatorForm::displayDeviceCommandOnForm); - this->protocol = DeviceStatusProtocolBase::deviceStatusProtocolFactory(typeid (this).name()); + connect(((DeviceHubWindow *)this->parent())->kafkaConsumer, &QKafkaConsumer::messageRecieved, + this, &TimeReplicator::commandReceivedHandler); + + kafkaProducer.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaProducer.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaProducer.createProducer(); + + this->protocol = DeviceProtocolBase::deviceProtocolFactory(devType); } TimeReplicator::~TimeReplicator() @@ -21,6 +30,20 @@ this, &TimeReplicator::dataReceivedHandler); } +void TimeReplicator::mockReceivData() +{ + QByteArray buffer; + + // time replicator + buffer.append("$2B21308-13 21010012200000000000000000faf0*"); + buffer.append("$3C21308-13 2101008220322222222111111110000000000000000ca24*"); + buffer.append("$3C21308-13 21010052207111111111111111111111111000000005984*"); +// buffer.append("$3C21308-13 2101005121511111111111111111111111111111111bcfe*"); + buffer.append("$3E21308-13 2101001211308-13 2101001210929H.1.00S.1.00000292b*"); + + this->dataReceivedHandler(buffer); +} + void TimeReplicator::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); @@ -50,6 +73,8 @@ frameDto->milisecond = now.toMSecsSinceEpoch(); frameDto->rawFrame = frameByte; + frameDto->devCode = devCode; + this->afterFramePhase(frameDto); } @@ -67,12 +92,83 @@ std::cout << "frame type: " << typeid(* frameDto).name() << std::endl; std::cout << frameDto->rawFrame.toStdString() << std::endl; + // 0. 输出到日志文件中 + QString date = frameDto->timestamp.mid(0, 10); + + // 1. 原始字节数组数据 + QString filename = "raw_" + devCode + ".log"; + QString content = frameDto->timestamp + " [recv] " + frameDto->rawFrame.left(frameDto->rawFrame.size() - FRAME_TAIL.size()); + QLogUtil::writeRawDataLogByDate(date, filename, content); + + // 2. 解析后的json数据 + QString frameFilename = "frame_" + devCode + ".log"; + QString frameContent = frameDto->timestamp + " [recv] " + QJsonDocument(frameDto->toJSON()).toJson(QJsonDocument::Compact); + QLogUtil::writeChannelDataLogByDate(date, frameFilename, frameContent); + // 3. 输出到中间件,执行后续处理过程 if (SettingConfig::getInstance().NEED_KAFKA == 1) { QJsonObject jsonObj = frameDto->toJSON(); jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); jsonObj.insert("deviceId", deviceId); - kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + kafkaProducer.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); } + + // 4. 在界面上简单显示相差数据结果 + emit this->sendDataToDraw(frameDto); } + +void TimeReplicator::sendDataToSerial(QByteArray data) +{ + data.append(FRAME_TAIL); + this->serialUtil.sendData(data); + + // 记录日志 + // 0. 输出到日志文件中 + QDateTime now = QDateTime::currentDateTime(); + QString date = now.toString("yyyy-MM-dd"); + + // 1. 原始字节数组数据 + QString filename = "raw_" + devCode + ".log"; + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + data.left(data.size() - FRAME_TAIL.size()); + QLogUtil::writeRawDataLogByDate(date, filename, content); +} + +void TimeReplicator::commandReceivedHandler(QJsonObject command) +{ + if (command.contains("deviceType") == false || command.value("deviceType").toString() != "09") + { + std::cout << "device type [" << command.value("deviceType").toString().toStdString() << "] not matched. return" << std::endl; + return; + } + + if (command.contains("deviceId") == false || command.value("deviceId").toString() != deviceId) + { + std::cout << "deviceId [" << command.value("deviceId").toString().toStdString() << "] not matched. return" << std::endl; + return; + } + + // 记录日志 + // 0. 输出到日志文件中 + QDateTime now = QDateTime::currentDateTime(); + QString date = now.toString("yyyy-MM-dd"); + + // 1. 原始字节数组数据 + QString filename = "frame_" + devCode + ".log"; + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + command.find("cmdStr")->toString(); + QLogUtil::writeChannelDataLogByDate(date, filename, content); + + QByteArray commandBytes = protocol->generateSettingCommand(command.value("command").toString(), command.value("params").toString()); + this->sendDataToSerial(commandBytes); + command.insert("rawCommand", QString::fromUtf8(commandBytes)); + + QJsonObject cmdcb; + cmdcb.insert("commandId", command.value("commandId").toString()); + cmdcb.insert("rawCommand", QString::fromUtf8(commandBytes)); + cmdcb.insert("status", "1"); + this->kafkaProducer.produceMessage(SettingConfig::getInstance().KAFKA_CMDCB_TOPIC, QString(QJsonDocument(cmdcb).toJson(QJsonDocument::Compact))); + + // display on page + emit sendCommandToDisplay(command); +} + diff --git a/DeviceHub/device/TimeReplicator.h b/DeviceHub/device/TimeReplicator.h index 5acf6d9..12b5ff4 100644 --- a/DeviceHub/device/TimeReplicator.h +++ b/DeviceHub/device/TimeReplicator.h @@ -4,6 +4,7 @@ #include #include "device/DeviceBase.h" +#include "protocol/dto/TimeReplicatorStatusDto.h" class TimeReplicator : public DeviceBase { @@ -12,13 +13,19 @@ explicit TimeReplicator(QObject *parent = nullptr); ~TimeReplicator(); + void mockReceivData(); + void afterFramePhase(DeviceFrameBaseDto * frameDto); + void sendDataToSerial(QByteArray data); signals: void sendDataToDraw(DeviceFrameBaseDto * frameData); + void sendCommandToDisplay(QJsonObject command); public slots: void dataReceivedHandler(QByteArray data); + void commandReceivedHandler(QJsonObject command); + }; #endif // TIMEREPLICATOR_H diff --git a/DeviceHub/device/device.pri b/DeviceHub/device/device.pri index 645dbcb..fedab85 100644 --- a/DeviceHub/device/device.pri +++ b/DeviceHub/device/device.pri @@ -4,7 +4,7 @@ HEADERS += $$PWD/SignalGenerator.h HEADERS += $$PWD/TimeSwitcher.h HEADERS += $$PWD/FreqSwitcher.h -#HEADERS += $$PWD/TimeReplicator.h +HEADERS += $$PWD/TimeReplicator.h HEADERS += $$PWD/FreqReplicator.h HEADERS += $$PWD/BCodeTerminal.h @@ -13,6 +13,6 @@ SOURCES += $$PWD/SignalGenerator.cpp SOURCES += $$PWD/TimeSwitcher.cpp SOURCES += $$PWD/FreqSwitcher.cpp -#SOURCES += $$PWD/TimeReplicator.cpp +SOURCES += $$PWD/TimeReplicator.cpp SOURCES += $$PWD/FreqReplicator.cpp SOURCES += $$PWD/BCodeTerminal.cpp diff --git a/DeviceHub/protocol/DeviceProtocolBase.cpp b/DeviceHub/protocol/DeviceProtocolBase.cpp index e7b26ca..4109e8d 100644 --- a/DeviceHub/protocol/DeviceProtocolBase.cpp +++ b/DeviceHub/protocol/DeviceProtocolBase.cpp @@ -3,7 +3,7 @@ #include "SignalGeneratorProtocolBM.h" #include "TimeSwitcherProtocolBM.h" #include "FreqSwitcherProtocolBM.h" -//#include "TimeReplicatorProtocolBM.h" +#include "TimeReplicatorProtocolBM.h" #include "FreqReplicatorProtocolTX.h" #include "BCodeTerminalProtocolBM.h" @@ -16,22 +16,25 @@ DeviceProtocolBase * DeviceProtocolBase::deviceProtocolFactory(QString deviceType) { - if (deviceType == "03") + if (deviceType == "03" || deviceType.contains("FrequencyTuning") == true) { return new FrequencyTuningProtocolBM(); - } else if (deviceType == "04") + } else if (deviceType == "04" || deviceType.contains("SignalGenerator") == true) { return new SignalGeneratorProtocolBM(); - } else if (deviceType == "05") + } else if (deviceType == "05" || deviceType.contains("TimeSwitcher") == true) { return new TimeSwitcherProtocolBM(); - } else if (deviceType == "06") + } else if (deviceType == "06" || deviceType.contains("FreqSwitcher") == true) { return new FreqSwitcherProtocolBM(); - } else if (deviceType == "07") + } else if (deviceType == "07" || deviceType.contains("BCodeTerminal") == true) { return new BCodeTerminalProtocolBM(); - } else if (deviceType == "10") + } else if (deviceType == "09" || deviceType.contains("TimeReplicator") == true) + { + return new TimeReplicatorProtocolBM(); + } else if (deviceType == "10" || deviceType.contains("FreqReplicator") == true) { return new FreqReplicatorProtocolTX(); } diff --git a/DeviceHub/protocol/TimeReplicatorProtocolBM.cpp b/DeviceHub/protocol/TimeReplicatorProtocolBM.cpp new file mode 100644 index 0000000..dbd6914 --- /dev/null +++ b/DeviceHub/protocol/TimeReplicatorProtocolBM.cpp @@ -0,0 +1,111 @@ +#include "TimeReplicatorProtocolBM.h" + +TimeReplicatorProtocolBM::TimeReplicatorProtocolBM(QObject *parent) : DeviceProtocolBase(parent) +{ + +} + +TimeReplicatorProtocolBM::~TimeReplicatorProtocolBM() +{ + +} + +bool TimeReplicatorProtocolBM::parseDeviceFrameData(QByteArray rawData, DeviceFrameBaseDto *frameData, int frameType) +{ + if (frameType != 1) + return false; + + qint8 length = rawData.mid(1, 2).toInt(0, 16); + QByteArray content = rawData.mid(19, length - 24); + qint8 count; + int inputByte = 0; + QString outStr = ""; + + ((TimeReplicatorStatusDto *) frameData)->devStatus = content.at(0); + if (length < 0x30) + { + count = 2; + inputByte = content.mid(2, 1).toInt(); + outStr = content.mid(3, 8 * count); + } else + { + count = 4; + inputByte = content.mid(2, 2).toInt(); + outStr = content.mid(4, 8 * count); + } + + ((TimeReplicatorStatusDto *) frameData)->inCount = count; + for (int i = 0; i < count; i++) + { + qint8 in = inputByte % 2; + ((TimeReplicatorStatusDto *) frameData)->inValid.append(in == 1 ? "1" : "0"); + inputByte = inputByte / 2; + + QString output = ""; + for (int j = 0; j < 8; j++) + { + output += outStr.at(i * 8 + j); + output += ","; + } + ((TimeReplicatorStatusDto *) frameData)->outValid.append(output.mid(0, output.size() - 1)); + } + + ((TimeReplicatorStatusDto *)frameData)->frameType = TIME_REPLICATOR_STATUS_FRAME_TYPE; + + return true; +} + +QByteArray TimeReplicatorProtocolBM::generateSettingCommand(QString commandType, QString valueSet) +{ + QByteArray commandBytes; + + commandBytes.append("1308-13").append(0x20); + commandBytes.append("2101001"); + commandBytes.append(commandType); + commandBytes.append(valueSet.replace(",", "")); + commandBytes.append(this->buildCRC16(commandBytes)); + commandBytes.append("*"); + + commandBytes.prepend(QString::number(commandBytes.size() + 3, 16).toUpper().toLocal8Bit()); + commandBytes.prepend("$"); + + return commandBytes; +} + +int TimeReplicatorProtocolBM::checkFrame(QByteArray rawData) +{ + if (rawData.at(0) == '$' && rawData.at(rawData.size() - 1) == '*' && + rawData.at(3) == '2' && rawData.at(20) == '2' && + rawData.size() == rawData.mid(1, 2).toInt(0, 16)) + return 1; + return 0; +} + +DeviceFrameBaseDto * TimeReplicatorProtocolBM::frameFactory(int frameType) +{ + DeviceFrameBaseDto * frameData = new TimeReplicatorStatusDto(); + + return frameData; +} + +QList TimeReplicatorProtocolBM::extractFrameList(QByteArray rawData) +{ + QList resultList; + while (rawData.endsWith(QByteArray("*")) == true && rawData.startsWith("$") == true) + { + QByteArray ba; + qint8 length = rawData.mid(1, 2).toInt(0, 16); + if (rawData.at(0) == '$' && rawData.at(length - 1) == '*') + { + ba.append(rawData.mid(0, length)); + resultList.append(ba); + ba.clear(); + rawData = rawData.mid(length, rawData.size() - length); + } else + { + break; + } + } + + return resultList; +} diff --git a/DeviceHub/protocol/TimeReplicatorProtocolBM.h b/DeviceHub/protocol/TimeReplicatorProtocolBM.h new file mode 100644 index 0000000..a310e61 --- /dev/null +++ b/DeviceHub/protocol/TimeReplicatorProtocolBM.h @@ -0,0 +1,29 @@ +#ifndef TIMEREPLICATORPROTOCOLBM_H +#define TIMEREPLICATORPROTOCOLBM_H + +#include + +#include "common/utils/QByteUtil.h" +#include "DeviceProtocolBase.h" +#include "dto/TimeReplicatorStatusDto.h" + +class TimeReplicatorProtocolBM : public DeviceProtocolBase +{ + Q_OBJECT +public: + explicit TimeReplicatorProtocolBM(QObject *parent = nullptr); + ~TimeReplicatorProtocolBM(); + + // 解析数据 + bool parseDeviceFrameData(QByteArray rawData, DeviceFrameBaseDto * frameData, int frameType); + QByteArray generateSettingCommand(QString commandType, QString valueSet); + DeviceFrameBaseDto * frameFactory(int frameType); + + // + QList extractFrameList(QByteArray rawData); + + // 检测帧格式,帧头帧尾 + int checkFrame(QByteArray rawData); +}; + +#endif // TIMEREPLICATORPROTOCOLBM_H diff --git a/DeviceHub/protocol/dto/TimeReplicatorStatusDto.cpp b/DeviceHub/protocol/dto/TimeReplicatorStatusDto.cpp new file mode 100644 index 0000000..8f0dba7 --- /dev/null +++ b/DeviceHub/protocol/dto/TimeReplicatorStatusDto.cpp @@ -0,0 +1,32 @@ +#include "TimeReplicatorStatusDto.h" + +TimeReplicatorStatusDto::TimeReplicatorStatusDto(QObject *parent) : DeviceFrameBaseDto(parent) +{ + +} + +QJsonObject TimeReplicatorStatusDto::toJSON() +{ + QJsonObject jsonObj; + + QJsonObject dataObj; + dataObj.insert("devStatus", this->devStatus); + dataObj.insert("inCount", inCount); + + QJsonArray inputArray; + QJsonArray outputArray; + + for (int i = 0; i < inCount; i++) + { + inputArray.append(this->inValid.at(i)); + outputArray.append(outValid.at(i)); + } + dataObj.insert("inValid", inputArray); + dataObj.insert("outValid", outputArray); + + jsonObj.insert("ts", this->milisecond); + jsonObj.insert("frameType", this->frameType); + jsonObj.insert("data", dataObj); + + return jsonObj; +} diff --git a/DeviceHub/protocol/dto/TimeReplicatorStatusDto.h b/DeviceHub/protocol/dto/TimeReplicatorStatusDto.h new file mode 100644 index 0000000..0a227cf --- /dev/null +++ b/DeviceHub/protocol/dto/TimeReplicatorStatusDto.h @@ -0,0 +1,22 @@ +#ifndef TIMEREPLICATORSTATUSDTO_H +#define TIMEREPLICATORSTATUSDTO_H + +#include + +#include "DeviceFrameBaseDto.h" + +class TimeReplicatorStatusDto : public DeviceFrameBaseDto +{ + Q_OBJECT +public: + explicit TimeReplicatorStatusDto(QObject * parent = nullptr); + + QString devStatus; + qint8 inCount; + QList inValid; + QList outValid; + + QJsonObject toJSON() override; +}; + +#endif // TIMEREPLICATORSTATUSDTO_H diff --git a/DeviceHub/TimeReplicatorForm.cpp b/DeviceHub/TimeReplicatorForm.cpp index eb83370..0a6c833 100644 --- a/DeviceHub/TimeReplicatorForm.cpp +++ b/DeviceHub/TimeReplicatorForm.cpp @@ -1,5 +1,6 @@ #include "TimeReplicatorForm.h" #include "ui_TimeReplicatorForm.h" +#include "DeviceHubWindow.h" TimeReplicatorForm::TimeReplicatorForm(QWidget *parent) : QWidget(parent), @@ -15,5 +16,59 @@ void TimeReplicatorForm::on_tmRepButt_clicked() { - + // 获取设备对象 + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + TimeReplicator * device = (TimeReplicator *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09").at(devIndex); + device->mockReceivData(); } + + +void TimeReplicatorForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // 当前显示的设备编号 + if (frameData->frameType == "0901") + { + TimeReplicatorStatusDto * statusFrameDto = (TimeReplicatorStatusDto *) frameData; + ui->trDevStatus->setText(statusFrameDto->devStatus == "1" ? "正常" : "异常"); + ui->trInCount->setText(QString("%1").arg(statusFrameDto->inCount)); + + QJsonObject dataObj = statusFrameDto->toJSON().find("data")->toObject(); + QJsonDocument doc; + + doc.setArray(dataObj.find("inValid")->toArray()); + ui->trInValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + + doc.setArray(dataObj.find("outValid")->toArray()); + ui->trOutValid->setText(QString::fromUtf8(doc.toJson(QJsonDocument::Compact).constData())); + } +} + +void TimeReplicatorForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("09"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "09") + { + ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->setCurrentIndex(i); + } + } + for (int i = 0; i < typeDevList.size(); i++) + { + if (typeDevList.at(i)->getDeviceId() == command.value("deviceId").toString()) + { + ((DeviceHubWindow *)this->parent()->parent())->getDevSelect()->setCurrentIndex(i); + } + } + + QString commandType = command.value("command").toString(); + if (commandType == "1") + { + + } else if (commandType == "2") + { + + } +} + diff --git a/DeviceHub/TimeReplicatorForm.h b/DeviceHub/TimeReplicatorForm.h index e6c9dc8..dc73d85 100644 --- a/DeviceHub/TimeReplicatorForm.h +++ b/DeviceHub/TimeReplicatorForm.h @@ -2,6 +2,7 @@ #define TIMEREPLICATORFORM_H #include +#include "device/TimeReplicator.h" namespace Ui { class TimeReplicatorForm; @@ -15,6 +16,10 @@ explicit TimeReplicatorForm(QWidget *parent = nullptr); ~TimeReplicatorForm(); +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + private slots: void on_tmRepButt_clicked(); diff --git a/DeviceHub/TimeReplicatorForm.ui b/DeviceHub/TimeReplicatorForm.ui index bf39894..c2f6ef4 100644 --- a/DeviceHub/TimeReplicatorForm.ui +++ b/DeviceHub/TimeReplicatorForm.ui @@ -1,34 +1,186 @@ + - - - TimeReplicatorForm - + 0 0 - 400 - 300 + 1200 + 600 Form - - - 50 - 20 - 180 - 40 - - - - Mock TimeReplicator - - + + + 50 + 20 + 180 + 40 + + + + Mock TimeReplicator + + + + + + 20 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 设备工作状态 + + + + + + 100 + 120 + 50 + 30 + + + + + + + 20 + 80 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 工作状态 + + + + + + 20 + 290 + 100 + 30 + + + + + 微软雅黑 + 12 + + + + 参数设置 + + + + + + 170 + 120 + 80 + 30 + + + + + 微软雅黑 + 10 + + + + 输入路数量 + + + + + + 260 + 120 + 50 + 30 + + + + + + + 350 + 120 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输入状态 + + + + + + 410 + 120 + 200 + 30 + + + + + + + 20 + 180 + 60 + 30 + + + + + 微软雅黑 + 10 + + + + 输出状态 + + + + + + 100 + 180 + 500 + 30 + + + - + diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index 289beb5..4b69272 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -4,6 +4,7 @@ #include "TimeSwitcher.h" #include "FreqSwitcher.h" #include "BCodeTerminal.h" +#include "TimeReplicator.h" #include "FreqReplicator.h" #include @@ -56,6 +57,9 @@ } else if (deviceType == "07") { base = new BCodeTerminal(parent); + } else if (deviceType == "09") + { + base = new TimeReplicator(parent); } else if (deviceType == "10") { base = new FreqReplicator(parent); diff --git a/DeviceHub/device/TimeReplicator.cpp b/DeviceHub/device/TimeReplicator.cpp index 1ba0db4..4ca4547 100644 --- a/DeviceHub/device/TimeReplicator.cpp +++ b/DeviceHub/device/TimeReplicator.cpp @@ -1,18 +1,27 @@ #include "TimeReplicator.h" - +#include "DeviceHubWindow.h" #include #include TimeReplicator::TimeReplicator(QObject *parent) : DeviceBase(parent) { + this->devType = "09"; connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &TimeReplicator::dataReceivedHandler); - kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); - kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); - kafkaUtil.createProducer(); + connect(this, &TimeReplicator::sendDataToDraw, + ((DeviceHubWindow *)this->parent())->timeRepForm, &TimeReplicatorForm::drawDeviceFrameOnForm); + connect(this, &TimeReplicator::sendCommandToDisplay, + ((DeviceHubWindow *)this->parent())->timeRepForm, &TimeReplicatorForm::displayDeviceCommandOnForm); - this->protocol = DeviceStatusProtocolBase::deviceStatusProtocolFactory(typeid (this).name()); + connect(((DeviceHubWindow *)this->parent())->kafkaConsumer, &QKafkaConsumer::messageRecieved, + this, &TimeReplicator::commandReceivedHandler); + + kafkaProducer.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaProducer.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaProducer.createProducer(); + + this->protocol = DeviceProtocolBase::deviceProtocolFactory(devType); } TimeReplicator::~TimeReplicator() @@ -21,6 +30,20 @@ this, &TimeReplicator::dataReceivedHandler); } +void TimeReplicator::mockReceivData() +{ + QByteArray buffer; + + // time replicator + buffer.append("$2B21308-13 21010012200000000000000000faf0*"); + buffer.append("$3C21308-13 2101008220322222222111111110000000000000000ca24*"); + buffer.append("$3C21308-13 21010052207111111111111111111111111000000005984*"); +// buffer.append("$3C21308-13 2101005121511111111111111111111111111111111bcfe*"); + buffer.append("$3E21308-13 2101001211308-13 2101001210929H.1.00S.1.00000292b*"); + + this->dataReceivedHandler(buffer); +} + void TimeReplicator::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); @@ -50,6 +73,8 @@ frameDto->milisecond = now.toMSecsSinceEpoch(); frameDto->rawFrame = frameByte; + frameDto->devCode = devCode; + this->afterFramePhase(frameDto); } @@ -67,12 +92,83 @@ std::cout << "frame type: " << typeid(* frameDto).name() << std::endl; std::cout << frameDto->rawFrame.toStdString() << std::endl; + // 0. 输出到日志文件中 + QString date = frameDto->timestamp.mid(0, 10); + + // 1. 原始字节数组数据 + QString filename = "raw_" + devCode + ".log"; + QString content = frameDto->timestamp + " [recv] " + frameDto->rawFrame.left(frameDto->rawFrame.size() - FRAME_TAIL.size()); + QLogUtil::writeRawDataLogByDate(date, filename, content); + + // 2. 解析后的json数据 + QString frameFilename = "frame_" + devCode + ".log"; + QString frameContent = frameDto->timestamp + " [recv] " + QJsonDocument(frameDto->toJSON()).toJson(QJsonDocument::Compact); + QLogUtil::writeChannelDataLogByDate(date, frameFilename, frameContent); + // 3. 输出到中间件,执行后续处理过程 if (SettingConfig::getInstance().NEED_KAFKA == 1) { QJsonObject jsonObj = frameDto->toJSON(); jsonObj.insert("clientId", SettingConfig::getInstance().CLIENT_ID); jsonObj.insert("deviceId", deviceId); - kafkaUtil.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); + kafkaProducer.produceMessage(QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact))); } + + // 4. 在界面上简单显示相差数据结果 + emit this->sendDataToDraw(frameDto); } + +void TimeReplicator::sendDataToSerial(QByteArray data) +{ + data.append(FRAME_TAIL); + this->serialUtil.sendData(data); + + // 记录日志 + // 0. 输出到日志文件中 + QDateTime now = QDateTime::currentDateTime(); + QString date = now.toString("yyyy-MM-dd"); + + // 1. 原始字节数组数据 + QString filename = "raw_" + devCode + ".log"; + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + data.left(data.size() - FRAME_TAIL.size()); + QLogUtil::writeRawDataLogByDate(date, filename, content); +} + +void TimeReplicator::commandReceivedHandler(QJsonObject command) +{ + if (command.contains("deviceType") == false || command.value("deviceType").toString() != "09") + { + std::cout << "device type [" << command.value("deviceType").toString().toStdString() << "] not matched. return" << std::endl; + return; + } + + if (command.contains("deviceId") == false || command.value("deviceId").toString() != deviceId) + { + std::cout << "deviceId [" << command.value("deviceId").toString().toStdString() << "] not matched. return" << std::endl; + return; + } + + // 记录日志 + // 0. 输出到日志文件中 + QDateTime now = QDateTime::currentDateTime(); + QString date = now.toString("yyyy-MM-dd"); + + // 1. 原始字节数组数据 + QString filename = "frame_" + devCode + ".log"; + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + command.find("cmdStr")->toString(); + QLogUtil::writeChannelDataLogByDate(date, filename, content); + + QByteArray commandBytes = protocol->generateSettingCommand(command.value("command").toString(), command.value("params").toString()); + this->sendDataToSerial(commandBytes); + command.insert("rawCommand", QString::fromUtf8(commandBytes)); + + QJsonObject cmdcb; + cmdcb.insert("commandId", command.value("commandId").toString()); + cmdcb.insert("rawCommand", QString::fromUtf8(commandBytes)); + cmdcb.insert("status", "1"); + this->kafkaProducer.produceMessage(SettingConfig::getInstance().KAFKA_CMDCB_TOPIC, QString(QJsonDocument(cmdcb).toJson(QJsonDocument::Compact))); + + // display on page + emit sendCommandToDisplay(command); +} + diff --git a/DeviceHub/device/TimeReplicator.h b/DeviceHub/device/TimeReplicator.h index 5acf6d9..12b5ff4 100644 --- a/DeviceHub/device/TimeReplicator.h +++ b/DeviceHub/device/TimeReplicator.h @@ -4,6 +4,7 @@ #include #include "device/DeviceBase.h" +#include "protocol/dto/TimeReplicatorStatusDto.h" class TimeReplicator : public DeviceBase { @@ -12,13 +13,19 @@ explicit TimeReplicator(QObject *parent = nullptr); ~TimeReplicator(); + void mockReceivData(); + void afterFramePhase(DeviceFrameBaseDto * frameDto); + void sendDataToSerial(QByteArray data); signals: void sendDataToDraw(DeviceFrameBaseDto * frameData); + void sendCommandToDisplay(QJsonObject command); public slots: void dataReceivedHandler(QByteArray data); + void commandReceivedHandler(QJsonObject command); + }; #endif // TIMEREPLICATOR_H diff --git a/DeviceHub/device/device.pri b/DeviceHub/device/device.pri index 645dbcb..fedab85 100644 --- a/DeviceHub/device/device.pri +++ b/DeviceHub/device/device.pri @@ -4,7 +4,7 @@ HEADERS += $$PWD/SignalGenerator.h HEADERS += $$PWD/TimeSwitcher.h HEADERS += $$PWD/FreqSwitcher.h -#HEADERS += $$PWD/TimeReplicator.h +HEADERS += $$PWD/TimeReplicator.h HEADERS += $$PWD/FreqReplicator.h HEADERS += $$PWD/BCodeTerminal.h @@ -13,6 +13,6 @@ SOURCES += $$PWD/SignalGenerator.cpp SOURCES += $$PWD/TimeSwitcher.cpp SOURCES += $$PWD/FreqSwitcher.cpp -#SOURCES += $$PWD/TimeReplicator.cpp +SOURCES += $$PWD/TimeReplicator.cpp SOURCES += $$PWD/FreqReplicator.cpp SOURCES += $$PWD/BCodeTerminal.cpp diff --git a/DeviceHub/protocol/DeviceProtocolBase.cpp b/DeviceHub/protocol/DeviceProtocolBase.cpp index e7b26ca..4109e8d 100644 --- a/DeviceHub/protocol/DeviceProtocolBase.cpp +++ b/DeviceHub/protocol/DeviceProtocolBase.cpp @@ -3,7 +3,7 @@ #include "SignalGeneratorProtocolBM.h" #include "TimeSwitcherProtocolBM.h" #include "FreqSwitcherProtocolBM.h" -//#include "TimeReplicatorProtocolBM.h" +#include "TimeReplicatorProtocolBM.h" #include "FreqReplicatorProtocolTX.h" #include "BCodeTerminalProtocolBM.h" @@ -16,22 +16,25 @@ DeviceProtocolBase * DeviceProtocolBase::deviceProtocolFactory(QString deviceType) { - if (deviceType == "03") + if (deviceType == "03" || deviceType.contains("FrequencyTuning") == true) { return new FrequencyTuningProtocolBM(); - } else if (deviceType == "04") + } else if (deviceType == "04" || deviceType.contains("SignalGenerator") == true) { return new SignalGeneratorProtocolBM(); - } else if (deviceType == "05") + } else if (deviceType == "05" || deviceType.contains("TimeSwitcher") == true) { return new TimeSwitcherProtocolBM(); - } else if (deviceType == "06") + } else if (deviceType == "06" || deviceType.contains("FreqSwitcher") == true) { return new FreqSwitcherProtocolBM(); - } else if (deviceType == "07") + } else if (deviceType == "07" || deviceType.contains("BCodeTerminal") == true) { return new BCodeTerminalProtocolBM(); - } else if (deviceType == "10") + } else if (deviceType == "09" || deviceType.contains("TimeReplicator") == true) + { + return new TimeReplicatorProtocolBM(); + } else if (deviceType == "10" || deviceType.contains("FreqReplicator") == true) { return new FreqReplicatorProtocolTX(); } diff --git a/DeviceHub/protocol/TimeReplicatorProtocolBM.cpp b/DeviceHub/protocol/TimeReplicatorProtocolBM.cpp new file mode 100644 index 0000000..dbd6914 --- /dev/null +++ b/DeviceHub/protocol/TimeReplicatorProtocolBM.cpp @@ -0,0 +1,111 @@ +#include "TimeReplicatorProtocolBM.h" + +TimeReplicatorProtocolBM::TimeReplicatorProtocolBM(QObject *parent) : DeviceProtocolBase(parent) +{ + +} + +TimeReplicatorProtocolBM::~TimeReplicatorProtocolBM() +{ + +} + +bool TimeReplicatorProtocolBM::parseDeviceFrameData(QByteArray rawData, DeviceFrameBaseDto *frameData, int frameType) +{ + if (frameType != 1) + return false; + + qint8 length = rawData.mid(1, 2).toInt(0, 16); + QByteArray content = rawData.mid(19, length - 24); + qint8 count; + int inputByte = 0; + QString outStr = ""; + + ((TimeReplicatorStatusDto *) frameData)->devStatus = content.at(0); + if (length < 0x30) + { + count = 2; + inputByte = content.mid(2, 1).toInt(); + outStr = content.mid(3, 8 * count); + } else + { + count = 4; + inputByte = content.mid(2, 2).toInt(); + outStr = content.mid(4, 8 * count); + } + + ((TimeReplicatorStatusDto *) frameData)->inCount = count; + for (int i = 0; i < count; i++) + { + qint8 in = inputByte % 2; + ((TimeReplicatorStatusDto *) frameData)->inValid.append(in == 1 ? "1" : "0"); + inputByte = inputByte / 2; + + QString output = ""; + for (int j = 0; j < 8; j++) + { + output += outStr.at(i * 8 + j); + output += ","; + } + ((TimeReplicatorStatusDto *) frameData)->outValid.append(output.mid(0, output.size() - 1)); + } + + ((TimeReplicatorStatusDto *)frameData)->frameType = TIME_REPLICATOR_STATUS_FRAME_TYPE; + + return true; +} + +QByteArray TimeReplicatorProtocolBM::generateSettingCommand(QString commandType, QString valueSet) +{ + QByteArray commandBytes; + + commandBytes.append("1308-13").append(0x20); + commandBytes.append("2101001"); + commandBytes.append(commandType); + commandBytes.append(valueSet.replace(",", "")); + commandBytes.append(this->buildCRC16(commandBytes)); + commandBytes.append("*"); + + commandBytes.prepend(QString::number(commandBytes.size() + 3, 16).toUpper().toLocal8Bit()); + commandBytes.prepend("$"); + + return commandBytes; +} + +int TimeReplicatorProtocolBM::checkFrame(QByteArray rawData) +{ + if (rawData.at(0) == '$' && rawData.at(rawData.size() - 1) == '*' && + rawData.at(3) == '2' && rawData.at(20) == '2' && + rawData.size() == rawData.mid(1, 2).toInt(0, 16)) + return 1; + return 0; +} + +DeviceFrameBaseDto * TimeReplicatorProtocolBM::frameFactory(int frameType) +{ + DeviceFrameBaseDto * frameData = new TimeReplicatorStatusDto(); + + return frameData; +} + +QList TimeReplicatorProtocolBM::extractFrameList(QByteArray rawData) +{ + QList resultList; + while (rawData.endsWith(QByteArray("*")) == true && rawData.startsWith("$") == true) + { + QByteArray ba; + qint8 length = rawData.mid(1, 2).toInt(0, 16); + if (rawData.at(0) == '$' && rawData.at(length - 1) == '*') + { + ba.append(rawData.mid(0, length)); + resultList.append(ba); + ba.clear(); + rawData = rawData.mid(length, rawData.size() - length); + } else + { + break; + } + } + + return resultList; +} diff --git a/DeviceHub/protocol/TimeReplicatorProtocolBM.h b/DeviceHub/protocol/TimeReplicatorProtocolBM.h new file mode 100644 index 0000000..a310e61 --- /dev/null +++ b/DeviceHub/protocol/TimeReplicatorProtocolBM.h @@ -0,0 +1,29 @@ +#ifndef TIMEREPLICATORPROTOCOLBM_H +#define TIMEREPLICATORPROTOCOLBM_H + +#include + +#include "common/utils/QByteUtil.h" +#include "DeviceProtocolBase.h" +#include "dto/TimeReplicatorStatusDto.h" + +class TimeReplicatorProtocolBM : public DeviceProtocolBase +{ + Q_OBJECT +public: + explicit TimeReplicatorProtocolBM(QObject *parent = nullptr); + ~TimeReplicatorProtocolBM(); + + // 解析数据 + bool parseDeviceFrameData(QByteArray rawData, DeviceFrameBaseDto * frameData, int frameType); + QByteArray generateSettingCommand(QString commandType, QString valueSet); + DeviceFrameBaseDto * frameFactory(int frameType); + + // + QList extractFrameList(QByteArray rawData); + + // 检测帧格式,帧头帧尾 + int checkFrame(QByteArray rawData); +}; + +#endif // TIMEREPLICATORPROTOCOLBM_H diff --git a/DeviceHub/protocol/dto/TimeReplicatorStatusDto.cpp b/DeviceHub/protocol/dto/TimeReplicatorStatusDto.cpp new file mode 100644 index 0000000..8f0dba7 --- /dev/null +++ b/DeviceHub/protocol/dto/TimeReplicatorStatusDto.cpp @@ -0,0 +1,32 @@ +#include "TimeReplicatorStatusDto.h" + +TimeReplicatorStatusDto::TimeReplicatorStatusDto(QObject *parent) : DeviceFrameBaseDto(parent) +{ + +} + +QJsonObject TimeReplicatorStatusDto::toJSON() +{ + QJsonObject jsonObj; + + QJsonObject dataObj; + dataObj.insert("devStatus", this->devStatus); + dataObj.insert("inCount", inCount); + + QJsonArray inputArray; + QJsonArray outputArray; + + for (int i = 0; i < inCount; i++) + { + inputArray.append(this->inValid.at(i)); + outputArray.append(outValid.at(i)); + } + dataObj.insert("inValid", inputArray); + dataObj.insert("outValid", outputArray); + + jsonObj.insert("ts", this->milisecond); + jsonObj.insert("frameType", this->frameType); + jsonObj.insert("data", dataObj); + + return jsonObj; +} diff --git a/DeviceHub/protocol/dto/TimeReplicatorStatusDto.h b/DeviceHub/protocol/dto/TimeReplicatorStatusDto.h new file mode 100644 index 0000000..0a227cf --- /dev/null +++ b/DeviceHub/protocol/dto/TimeReplicatorStatusDto.h @@ -0,0 +1,22 @@ +#ifndef TIMEREPLICATORSTATUSDTO_H +#define TIMEREPLICATORSTATUSDTO_H + +#include + +#include "DeviceFrameBaseDto.h" + +class TimeReplicatorStatusDto : public DeviceFrameBaseDto +{ + Q_OBJECT +public: + explicit TimeReplicatorStatusDto(QObject * parent = nullptr); + + QString devStatus; + qint8 inCount; + QList inValid; + QList outValid; + + QJsonObject toJSON() override; +}; + +#endif // TIMEREPLICATORSTATUSDTO_H diff --git a/DeviceHub/protocol/protocol.pri b/DeviceHub/protocol/protocol.pri index 1318181..ddf5dcc 100644 --- a/DeviceHub/protocol/protocol.pri +++ b/DeviceHub/protocol/protocol.pri @@ -11,7 +11,7 @@ HEADERS += $$PWD/dto/FreqSwitcherInterfaceDto.h HEADERS += $$PWD/dto/FreqSwitcherStatusDto.h HEADERS += $$PWD/dto/BCodeTerminalStatusDto.h -#HEADERS += $$PWD/dto/TimeReplicatorStatusDto.h +HEADERS += $$PWD/dto/TimeReplicatorStatusDto.h HEADERS += $$PWD/dto/FreqReplicatorStatusDto.h HEADERS += $$PWD/DeviceProtocolBase.h @@ -20,7 +20,7 @@ HEADERS += $$PWD/TimeSwitcherProtocolBM.h HEADERS += $$PWD/FreqSwitcherProtocolBM.h HEADERS += $$PWD/BCodeTerminalProtocolBM.h -#HEADERS += $$PWD/TimeReplicatorProtocolBM.h +HEADERS += $$PWD/TimeReplicatorProtocolBM.h HEADERS += $$PWD/FreqReplicatorProtocolTX.h @@ -33,7 +33,7 @@ SOURCES += $$PWD/dto/TimeSwitcherStatusDto.cpp SOURCES += $$PWD/dto/TimeSwitcherInterfaceDto.cpp SOURCES += $$PWD/dto/FreqSwitcherInterfaceDto.cpp -#SOURCES += $$PWD/dto/TimeReplicatorStatusDto.cpp +SOURCES += $$PWD/dto/TimeReplicatorStatusDto.cpp SOURCES += $$PWD/dto/FreqSwitcherStatusDto.cpp SOURCES += $$PWD/DeviceProtocolBase.cpp SOURCES += $$PWD/FrequencyTuningProtocolBM.cpp @@ -42,6 +42,6 @@ SOURCES += $$PWD/dto/BCodeTerminalStatusDto.cpp SOURCES += $$PWD/TimeSwitcherProtocolBM.cpp SOURCES += $$PWD/FreqSwitcherProtocolBM.cpp -#SOURCES += $$PWD/TimeReplicatorProtocolBM.cpp +SOURCES += $$PWD/TimeReplicatorProtocolBM.cpp SOURCES += $$PWD/FreqReplicatorProtocolTX.cpp SOURCES += $$PWD/BCodeTerminalProtocolBM.cpp