diff --git a/DeviceHub/BCodeTerminalForm.cpp b/DeviceHub/BCodeTerminalForm.cpp new file mode 100644 index 0000000..2c8bc26 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.cpp @@ -0,0 +1,63 @@ +#include "BCodeTerminalForm.h" +#include "ui_BCodeTerminalForm.h" +#include "DeviceHubWindow.h" + +BCodeTerminalForm::BCodeTerminalForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::BCodeTerminalForm) +{ + ui->setupUi(this); +} + +BCodeTerminalForm::~BCodeTerminalForm() +{ + delete ui; +} + +void BCodeTerminalForm::on_bctButt_clicked() +{ + // ������������ + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + BCodeTerminal * device = (BCodeTerminal *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("07").at(devIndex); + device->mockReceivData(); +} + +void BCodeTerminalForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // ������������������ + if (frameData->frameType == "0701") + { + BCodeTerminalStatusDto * statusFrameDto = (BCodeTerminalStatusDto *) frameData; + + } +} + +void BCodeTerminalForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("04"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "04") + { + ((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/BCodeTerminalForm.cpp b/DeviceHub/BCodeTerminalForm.cpp new file mode 100644 index 0000000..2c8bc26 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.cpp @@ -0,0 +1,63 @@ +#include "BCodeTerminalForm.h" +#include "ui_BCodeTerminalForm.h" +#include "DeviceHubWindow.h" + +BCodeTerminalForm::BCodeTerminalForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::BCodeTerminalForm) +{ + ui->setupUi(this); +} + +BCodeTerminalForm::~BCodeTerminalForm() +{ + delete ui; +} + +void BCodeTerminalForm::on_bctButt_clicked() +{ + // ������������ + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + BCodeTerminal * device = (BCodeTerminal *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("07").at(devIndex); + device->mockReceivData(); +} + +void BCodeTerminalForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // ������������������ + if (frameData->frameType == "0701") + { + BCodeTerminalStatusDto * statusFrameDto = (BCodeTerminalStatusDto *) frameData; + + } +} + +void BCodeTerminalForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("04"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "04") + { + ((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/BCodeTerminalForm.h b/DeviceHub/BCodeTerminalForm.h new file mode 100644 index 0000000..0ae7601 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.h @@ -0,0 +1,30 @@ +#ifndef BCODETERMINALFORM_H +#define BCODETERMINALFORM_H + +#include +#include "device/BCodeTerminal.h" + +namespace Ui { +class BCodeTerminalForm; +} + +class BCodeTerminalForm : public QWidget +{ + Q_OBJECT + +public: + explicit BCodeTerminalForm(QWidget *parent = nullptr); + ~BCodeTerminalForm(); + +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + +private slots: + void on_bctButt_clicked(); + +private: + Ui::BCodeTerminalForm *ui; +}; + +#endif // BCODETERMINALFORM_H diff --git a/DeviceHub/BCodeTerminalForm.cpp b/DeviceHub/BCodeTerminalForm.cpp new file mode 100644 index 0000000..2c8bc26 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.cpp @@ -0,0 +1,63 @@ +#include "BCodeTerminalForm.h" +#include "ui_BCodeTerminalForm.h" +#include "DeviceHubWindow.h" + +BCodeTerminalForm::BCodeTerminalForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::BCodeTerminalForm) +{ + ui->setupUi(this); +} + +BCodeTerminalForm::~BCodeTerminalForm() +{ + delete ui; +} + +void BCodeTerminalForm::on_bctButt_clicked() +{ + // ������������ + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + BCodeTerminal * device = (BCodeTerminal *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("07").at(devIndex); + device->mockReceivData(); +} + +void BCodeTerminalForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // ������������������ + if (frameData->frameType == "0701") + { + BCodeTerminalStatusDto * statusFrameDto = (BCodeTerminalStatusDto *) frameData; + + } +} + +void BCodeTerminalForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("04"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "04") + { + ((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/BCodeTerminalForm.h b/DeviceHub/BCodeTerminalForm.h new file mode 100644 index 0000000..0ae7601 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.h @@ -0,0 +1,30 @@ +#ifndef BCODETERMINALFORM_H +#define BCODETERMINALFORM_H + +#include +#include "device/BCodeTerminal.h" + +namespace Ui { +class BCodeTerminalForm; +} + +class BCodeTerminalForm : public QWidget +{ + Q_OBJECT + +public: + explicit BCodeTerminalForm(QWidget *parent = nullptr); + ~BCodeTerminalForm(); + +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + +private slots: + void on_bctButt_clicked(); + +private: + Ui::BCodeTerminalForm *ui; +}; + +#endif // BCODETERMINALFORM_H diff --git a/DeviceHub/BCodeTerminalForm.ui b/DeviceHub/BCodeTerminalForm.ui new file mode 100644 index 0000000..9504c94 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.ui @@ -0,0 +1,34 @@ + + + + + BCodeTerminalForm + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + 50 + 20 + 180 + 40 + + + + Mock BCodeTerminal + + + + + + diff --git a/DeviceHub/BCodeTerminalForm.cpp b/DeviceHub/BCodeTerminalForm.cpp new file mode 100644 index 0000000..2c8bc26 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.cpp @@ -0,0 +1,63 @@ +#include "BCodeTerminalForm.h" +#include "ui_BCodeTerminalForm.h" +#include "DeviceHubWindow.h" + +BCodeTerminalForm::BCodeTerminalForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::BCodeTerminalForm) +{ + ui->setupUi(this); +} + +BCodeTerminalForm::~BCodeTerminalForm() +{ + delete ui; +} + +void BCodeTerminalForm::on_bctButt_clicked() +{ + // ������������ + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + BCodeTerminal * device = (BCodeTerminal *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("07").at(devIndex); + device->mockReceivData(); +} + +void BCodeTerminalForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // ������������������ + if (frameData->frameType == "0701") + { + BCodeTerminalStatusDto * statusFrameDto = (BCodeTerminalStatusDto *) frameData; + + } +} + +void BCodeTerminalForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("04"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "04") + { + ((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/BCodeTerminalForm.h b/DeviceHub/BCodeTerminalForm.h new file mode 100644 index 0000000..0ae7601 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.h @@ -0,0 +1,30 @@ +#ifndef BCODETERMINALFORM_H +#define BCODETERMINALFORM_H + +#include +#include "device/BCodeTerminal.h" + +namespace Ui { +class BCodeTerminalForm; +} + +class BCodeTerminalForm : public QWidget +{ + Q_OBJECT + +public: + explicit BCodeTerminalForm(QWidget *parent = nullptr); + ~BCodeTerminalForm(); + +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + +private slots: + void on_bctButt_clicked(); + +private: + Ui::BCodeTerminalForm *ui; +}; + +#endif // BCODETERMINALFORM_H diff --git a/DeviceHub/BCodeTerminalForm.ui b/DeviceHub/BCodeTerminalForm.ui new file mode 100644 index 0000000..9504c94 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.ui @@ -0,0 +1,34 @@ + + + + + BCodeTerminalForm + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + 50 + 20 + 180 + 40 + + + + Mock BCodeTerminal + + + + + + diff --git a/DeviceHub/DeviceHub.pro b/DeviceHub/DeviceHub.pro index 3f17e33..6dc7b34 100644 --- a/DeviceHub/DeviceHub.pro +++ b/DeviceHub/DeviceHub.pro @@ -15,20 +15,23 @@ # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 -SOURCES += main.cpp +SOURCES += main.cpp \ + BCodeTerminalForm.cpp SOURCES += DeviceHubWindow.cpp SOURCES += FrequencyTuningForm.cpp SOURCES += SignalGeneratorForm.cpp SOURCES += TimeSwitcherForm.cpp SOURCES += FreqSwitcherForm.cpp -HEADERS += DeviceHubWindow.h +HEADERS += DeviceHubWindow.h \ + BCodeTerminalForm.h HEADERS += FrequencyTuningForm.h HEADERS += SignalGeneratorForm.h HEADERS += TimeSwitcherForm.h HEADERS += FreqSwitcherForm.h -FORMS += DeviceHubWindow.ui +FORMS += DeviceHubWindow.ui \ + BCodeTerminalForm.ui FORMS += FrequencyTuningForm.ui FORMS += SignalGeneratorForm.ui FORMS += TimeSwitcherForm.ui diff --git a/DeviceHub/BCodeTerminalForm.cpp b/DeviceHub/BCodeTerminalForm.cpp new file mode 100644 index 0000000..2c8bc26 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.cpp @@ -0,0 +1,63 @@ +#include "BCodeTerminalForm.h" +#include "ui_BCodeTerminalForm.h" +#include "DeviceHubWindow.h" + +BCodeTerminalForm::BCodeTerminalForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::BCodeTerminalForm) +{ + ui->setupUi(this); +} + +BCodeTerminalForm::~BCodeTerminalForm() +{ + delete ui; +} + +void BCodeTerminalForm::on_bctButt_clicked() +{ + // ������������ + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + BCodeTerminal * device = (BCodeTerminal *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("07").at(devIndex); + device->mockReceivData(); +} + +void BCodeTerminalForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // ������������������ + if (frameData->frameType == "0701") + { + BCodeTerminalStatusDto * statusFrameDto = (BCodeTerminalStatusDto *) frameData; + + } +} + +void BCodeTerminalForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("04"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "04") + { + ((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/BCodeTerminalForm.h b/DeviceHub/BCodeTerminalForm.h new file mode 100644 index 0000000..0ae7601 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.h @@ -0,0 +1,30 @@ +#ifndef BCODETERMINALFORM_H +#define BCODETERMINALFORM_H + +#include +#include "device/BCodeTerminal.h" + +namespace Ui { +class BCodeTerminalForm; +} + +class BCodeTerminalForm : public QWidget +{ + Q_OBJECT + +public: + explicit BCodeTerminalForm(QWidget *parent = nullptr); + ~BCodeTerminalForm(); + +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + +private slots: + void on_bctButt_clicked(); + +private: + Ui::BCodeTerminalForm *ui; +}; + +#endif // BCODETERMINALFORM_H diff --git a/DeviceHub/BCodeTerminalForm.ui b/DeviceHub/BCodeTerminalForm.ui new file mode 100644 index 0000000..9504c94 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.ui @@ -0,0 +1,34 @@ + + + + + BCodeTerminalForm + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + 50 + 20 + 180 + 40 + + + + Mock BCodeTerminal + + + + + + diff --git a/DeviceHub/DeviceHub.pro b/DeviceHub/DeviceHub.pro index 3f17e33..6dc7b34 100644 --- a/DeviceHub/DeviceHub.pro +++ b/DeviceHub/DeviceHub.pro @@ -15,20 +15,23 @@ # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 -SOURCES += main.cpp +SOURCES += main.cpp \ + BCodeTerminalForm.cpp SOURCES += DeviceHubWindow.cpp SOURCES += FrequencyTuningForm.cpp SOURCES += SignalGeneratorForm.cpp SOURCES += TimeSwitcherForm.cpp SOURCES += FreqSwitcherForm.cpp -HEADERS += DeviceHubWindow.h +HEADERS += DeviceHubWindow.h \ + BCodeTerminalForm.h HEADERS += FrequencyTuningForm.h HEADERS += SignalGeneratorForm.h HEADERS += TimeSwitcherForm.h HEADERS += FreqSwitcherForm.h -FORMS += DeviceHubWindow.ui +FORMS += DeviceHubWindow.ui \ + BCodeTerminalForm.ui FORMS += FrequencyTuningForm.ui FORMS += SignalGeneratorForm.ui FORMS += TimeSwitcherForm.ui diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 8050848..d2b0ac2 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -41,6 +41,9 @@ freqSwitForm = new FreqSwitcherForm(this); ui->stackedWidget->addWidget(freqSwitForm); + bCodeTermForm = new BCodeTerminalForm(this); + ui->stackedWidget->addWidget(bCodeTermForm); + // kafka consumer kafkaConsumer = new QKafkaConsumer(this); kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); diff --git a/DeviceHub/BCodeTerminalForm.cpp b/DeviceHub/BCodeTerminalForm.cpp new file mode 100644 index 0000000..2c8bc26 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.cpp @@ -0,0 +1,63 @@ +#include "BCodeTerminalForm.h" +#include "ui_BCodeTerminalForm.h" +#include "DeviceHubWindow.h" + +BCodeTerminalForm::BCodeTerminalForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::BCodeTerminalForm) +{ + ui->setupUi(this); +} + +BCodeTerminalForm::~BCodeTerminalForm() +{ + delete ui; +} + +void BCodeTerminalForm::on_bctButt_clicked() +{ + // ������������ + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + BCodeTerminal * device = (BCodeTerminal *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("07").at(devIndex); + device->mockReceivData(); +} + +void BCodeTerminalForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // ������������������ + if (frameData->frameType == "0701") + { + BCodeTerminalStatusDto * statusFrameDto = (BCodeTerminalStatusDto *) frameData; + + } +} + +void BCodeTerminalForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("04"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "04") + { + ((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/BCodeTerminalForm.h b/DeviceHub/BCodeTerminalForm.h new file mode 100644 index 0000000..0ae7601 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.h @@ -0,0 +1,30 @@ +#ifndef BCODETERMINALFORM_H +#define BCODETERMINALFORM_H + +#include +#include "device/BCodeTerminal.h" + +namespace Ui { +class BCodeTerminalForm; +} + +class BCodeTerminalForm : public QWidget +{ + Q_OBJECT + +public: + explicit BCodeTerminalForm(QWidget *parent = nullptr); + ~BCodeTerminalForm(); + +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + +private slots: + void on_bctButt_clicked(); + +private: + Ui::BCodeTerminalForm *ui; +}; + +#endif // BCODETERMINALFORM_H diff --git a/DeviceHub/BCodeTerminalForm.ui b/DeviceHub/BCodeTerminalForm.ui new file mode 100644 index 0000000..9504c94 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.ui @@ -0,0 +1,34 @@ + + + + + BCodeTerminalForm + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + 50 + 20 + 180 + 40 + + + + Mock BCodeTerminal + + + + + + diff --git a/DeviceHub/DeviceHub.pro b/DeviceHub/DeviceHub.pro index 3f17e33..6dc7b34 100644 --- a/DeviceHub/DeviceHub.pro +++ b/DeviceHub/DeviceHub.pro @@ -15,20 +15,23 @@ # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 -SOURCES += main.cpp +SOURCES += main.cpp \ + BCodeTerminalForm.cpp SOURCES += DeviceHubWindow.cpp SOURCES += FrequencyTuningForm.cpp SOURCES += SignalGeneratorForm.cpp SOURCES += TimeSwitcherForm.cpp SOURCES += FreqSwitcherForm.cpp -HEADERS += DeviceHubWindow.h +HEADERS += DeviceHubWindow.h \ + BCodeTerminalForm.h HEADERS += FrequencyTuningForm.h HEADERS += SignalGeneratorForm.h HEADERS += TimeSwitcherForm.h HEADERS += FreqSwitcherForm.h -FORMS += DeviceHubWindow.ui +FORMS += DeviceHubWindow.ui \ + BCodeTerminalForm.ui FORMS += FrequencyTuningForm.ui FORMS += SignalGeneratorForm.ui FORMS += TimeSwitcherForm.ui diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 8050848..d2b0ac2 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -41,6 +41,9 @@ freqSwitForm = new FreqSwitcherForm(this); ui->stackedWidget->addWidget(freqSwitForm); + bCodeTermForm = new BCodeTerminalForm(this); + ui->stackedWidget->addWidget(bCodeTermForm); + // kafka consumer kafkaConsumer = new QKafkaConsumer(this); kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index bdc0749..9e2700e 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -11,6 +11,7 @@ #include "SignalGeneratorForm.h" #include "TimeSwitcherForm.h" #include "FreqSwitcherForm.h" +#include "BCodeTerminalForm.h" QT_BEGIN_NAMESPACE namespace Ui { class DeviceHubWindow; } @@ -38,6 +39,7 @@ SignalGeneratorForm * signGenForm; TimeSwitcherForm * tmSwitForm; FreqSwitcherForm * freqSwitForm; + BCodeTerminalForm * bCodeTermForm; private slots: void on_minButt_clicked(); diff --git a/DeviceHub/BCodeTerminalForm.cpp b/DeviceHub/BCodeTerminalForm.cpp new file mode 100644 index 0000000..2c8bc26 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.cpp @@ -0,0 +1,63 @@ +#include "BCodeTerminalForm.h" +#include "ui_BCodeTerminalForm.h" +#include "DeviceHubWindow.h" + +BCodeTerminalForm::BCodeTerminalForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::BCodeTerminalForm) +{ + ui->setupUi(this); +} + +BCodeTerminalForm::~BCodeTerminalForm() +{ + delete ui; +} + +void BCodeTerminalForm::on_bctButt_clicked() +{ + // ������������ + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + BCodeTerminal * device = (BCodeTerminal *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("07").at(devIndex); + device->mockReceivData(); +} + +void BCodeTerminalForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // ������������������ + if (frameData->frameType == "0701") + { + BCodeTerminalStatusDto * statusFrameDto = (BCodeTerminalStatusDto *) frameData; + + } +} + +void BCodeTerminalForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("04"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "04") + { + ((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/BCodeTerminalForm.h b/DeviceHub/BCodeTerminalForm.h new file mode 100644 index 0000000..0ae7601 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.h @@ -0,0 +1,30 @@ +#ifndef BCODETERMINALFORM_H +#define BCODETERMINALFORM_H + +#include +#include "device/BCodeTerminal.h" + +namespace Ui { +class BCodeTerminalForm; +} + +class BCodeTerminalForm : public QWidget +{ + Q_OBJECT + +public: + explicit BCodeTerminalForm(QWidget *parent = nullptr); + ~BCodeTerminalForm(); + +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + +private slots: + void on_bctButt_clicked(); + +private: + Ui::BCodeTerminalForm *ui; +}; + +#endif // BCODETERMINALFORM_H diff --git a/DeviceHub/BCodeTerminalForm.ui b/DeviceHub/BCodeTerminalForm.ui new file mode 100644 index 0000000..9504c94 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.ui @@ -0,0 +1,34 @@ + + + + + BCodeTerminalForm + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + 50 + 20 + 180 + 40 + + + + Mock BCodeTerminal + + + + + + diff --git a/DeviceHub/DeviceHub.pro b/DeviceHub/DeviceHub.pro index 3f17e33..6dc7b34 100644 --- a/DeviceHub/DeviceHub.pro +++ b/DeviceHub/DeviceHub.pro @@ -15,20 +15,23 @@ # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 -SOURCES += main.cpp +SOURCES += main.cpp \ + BCodeTerminalForm.cpp SOURCES += DeviceHubWindow.cpp SOURCES += FrequencyTuningForm.cpp SOURCES += SignalGeneratorForm.cpp SOURCES += TimeSwitcherForm.cpp SOURCES += FreqSwitcherForm.cpp -HEADERS += DeviceHubWindow.h +HEADERS += DeviceHubWindow.h \ + BCodeTerminalForm.h HEADERS += FrequencyTuningForm.h HEADERS += SignalGeneratorForm.h HEADERS += TimeSwitcherForm.h HEADERS += FreqSwitcherForm.h -FORMS += DeviceHubWindow.ui +FORMS += DeviceHubWindow.ui \ + BCodeTerminalForm.ui FORMS += FrequencyTuningForm.ui FORMS += SignalGeneratorForm.ui FORMS += TimeSwitcherForm.ui diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 8050848..d2b0ac2 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -41,6 +41,9 @@ freqSwitForm = new FreqSwitcherForm(this); ui->stackedWidget->addWidget(freqSwitForm); + bCodeTermForm = new BCodeTerminalForm(this); + ui->stackedWidget->addWidget(bCodeTermForm); + // kafka consumer kafkaConsumer = new QKafkaConsumer(this); kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index bdc0749..9e2700e 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -11,6 +11,7 @@ #include "SignalGeneratorForm.h" #include "TimeSwitcherForm.h" #include "FreqSwitcherForm.h" +#include "BCodeTerminalForm.h" QT_BEGIN_NAMESPACE namespace Ui { class DeviceHubWindow; } @@ -38,6 +39,7 @@ SignalGeneratorForm * signGenForm; TimeSwitcherForm * tmSwitForm; FreqSwitcherForm * freqSwitForm; + BCodeTerminalForm * bCodeTermForm; private slots: void on_minButt_clicked(); diff --git a/DeviceHub/device/BCodeTerminal.cpp b/DeviceHub/device/BCodeTerminal.cpp index 19d33fd..b792982 100644 --- a/DeviceHub/device/BCodeTerminal.cpp +++ b/DeviceHub/device/BCodeTerminal.cpp @@ -1,18 +1,27 @@ #include "BCodeTerminal.h" - +#include "DeviceHubWindow.h" #include #include BCodeTerminal::BCodeTerminal(QObject* parent) : DeviceBase(parent) { + this->devType = "07"; connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &BCodeTerminal::dataReceivedHandler); - kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); - kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); - kafkaUtil.createProducer(); + connect(this, &BCodeTerminal::sendDataToDraw, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::drawDeviceFrameOnForm); + connect(this, &BCodeTerminal::sendCommandToDisplay, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::displayDeviceCommandOnForm); - this->protocol = DeviceStatusProtocolBase::deviceStatusProtocolFactory(typeid (this).name()); + connect(((DeviceHubWindow *)this->parent())->kafkaConsumer, &QKafkaConsumer::messageRecieved, + this, &BCodeTerminal::commandReceivedHandler); + + kafkaProducer.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaProducer.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaProducer.createProducer(); + + this->protocol = DeviceProtocolBase::deviceProtocolFactory(devType); } BCodeTerminal::~BCodeTerminal() @@ -21,6 +30,17 @@ this, &BCodeTerminal::dataReceivedHandler); } +void BCodeTerminal::mockReceivData() +{ + QByteArray buffer; + + // b-code terminal + buffer.append("$2621304-20 210100112100f90210.035422*"); + buffer.append("$3e21304-20 2101001111304-20 2101001210801H.1.00S.1.030008432*"); + + this->dataReceivedHandler(buffer); +} + void BCodeTerminal::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); @@ -73,6 +93,60 @@ 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))); } } + +void BCodeTerminal::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 BCodeTerminal::commandReceivedHandler(QJsonObject command) +{ + if (command.contains("deviceType") == false || command.value("deviceType").toString() != "07") + { + 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/BCodeTerminalForm.cpp b/DeviceHub/BCodeTerminalForm.cpp new file mode 100644 index 0000000..2c8bc26 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.cpp @@ -0,0 +1,63 @@ +#include "BCodeTerminalForm.h" +#include "ui_BCodeTerminalForm.h" +#include "DeviceHubWindow.h" + +BCodeTerminalForm::BCodeTerminalForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::BCodeTerminalForm) +{ + ui->setupUi(this); +} + +BCodeTerminalForm::~BCodeTerminalForm() +{ + delete ui; +} + +void BCodeTerminalForm::on_bctButt_clicked() +{ + // ������������ + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + BCodeTerminal * device = (BCodeTerminal *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("07").at(devIndex); + device->mockReceivData(); +} + +void BCodeTerminalForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // ������������������ + if (frameData->frameType == "0701") + { + BCodeTerminalStatusDto * statusFrameDto = (BCodeTerminalStatusDto *) frameData; + + } +} + +void BCodeTerminalForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("04"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "04") + { + ((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/BCodeTerminalForm.h b/DeviceHub/BCodeTerminalForm.h new file mode 100644 index 0000000..0ae7601 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.h @@ -0,0 +1,30 @@ +#ifndef BCODETERMINALFORM_H +#define BCODETERMINALFORM_H + +#include +#include "device/BCodeTerminal.h" + +namespace Ui { +class BCodeTerminalForm; +} + +class BCodeTerminalForm : public QWidget +{ + Q_OBJECT + +public: + explicit BCodeTerminalForm(QWidget *parent = nullptr); + ~BCodeTerminalForm(); + +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + +private slots: + void on_bctButt_clicked(); + +private: + Ui::BCodeTerminalForm *ui; +}; + +#endif // BCODETERMINALFORM_H diff --git a/DeviceHub/BCodeTerminalForm.ui b/DeviceHub/BCodeTerminalForm.ui new file mode 100644 index 0000000..9504c94 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.ui @@ -0,0 +1,34 @@ + + + + + BCodeTerminalForm + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + 50 + 20 + 180 + 40 + + + + Mock BCodeTerminal + + + + + + diff --git a/DeviceHub/DeviceHub.pro b/DeviceHub/DeviceHub.pro index 3f17e33..6dc7b34 100644 --- a/DeviceHub/DeviceHub.pro +++ b/DeviceHub/DeviceHub.pro @@ -15,20 +15,23 @@ # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 -SOURCES += main.cpp +SOURCES += main.cpp \ + BCodeTerminalForm.cpp SOURCES += DeviceHubWindow.cpp SOURCES += FrequencyTuningForm.cpp SOURCES += SignalGeneratorForm.cpp SOURCES += TimeSwitcherForm.cpp SOURCES += FreqSwitcherForm.cpp -HEADERS += DeviceHubWindow.h +HEADERS += DeviceHubWindow.h \ + BCodeTerminalForm.h HEADERS += FrequencyTuningForm.h HEADERS += SignalGeneratorForm.h HEADERS += TimeSwitcherForm.h HEADERS += FreqSwitcherForm.h -FORMS += DeviceHubWindow.ui +FORMS += DeviceHubWindow.ui \ + BCodeTerminalForm.ui FORMS += FrequencyTuningForm.ui FORMS += SignalGeneratorForm.ui FORMS += TimeSwitcherForm.ui diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 8050848..d2b0ac2 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -41,6 +41,9 @@ freqSwitForm = new FreqSwitcherForm(this); ui->stackedWidget->addWidget(freqSwitForm); + bCodeTermForm = new BCodeTerminalForm(this); + ui->stackedWidget->addWidget(bCodeTermForm); + // kafka consumer kafkaConsumer = new QKafkaConsumer(this); kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index bdc0749..9e2700e 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -11,6 +11,7 @@ #include "SignalGeneratorForm.h" #include "TimeSwitcherForm.h" #include "FreqSwitcherForm.h" +#include "BCodeTerminalForm.h" QT_BEGIN_NAMESPACE namespace Ui { class DeviceHubWindow; } @@ -38,6 +39,7 @@ SignalGeneratorForm * signGenForm; TimeSwitcherForm * tmSwitForm; FreqSwitcherForm * freqSwitForm; + BCodeTerminalForm * bCodeTermForm; private slots: void on_minButt_clicked(); diff --git a/DeviceHub/device/BCodeTerminal.cpp b/DeviceHub/device/BCodeTerminal.cpp index 19d33fd..b792982 100644 --- a/DeviceHub/device/BCodeTerminal.cpp +++ b/DeviceHub/device/BCodeTerminal.cpp @@ -1,18 +1,27 @@ #include "BCodeTerminal.h" - +#include "DeviceHubWindow.h" #include #include BCodeTerminal::BCodeTerminal(QObject* parent) : DeviceBase(parent) { + this->devType = "07"; connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &BCodeTerminal::dataReceivedHandler); - kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); - kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); - kafkaUtil.createProducer(); + connect(this, &BCodeTerminal::sendDataToDraw, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::drawDeviceFrameOnForm); + connect(this, &BCodeTerminal::sendCommandToDisplay, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::displayDeviceCommandOnForm); - this->protocol = DeviceStatusProtocolBase::deviceStatusProtocolFactory(typeid (this).name()); + connect(((DeviceHubWindow *)this->parent())->kafkaConsumer, &QKafkaConsumer::messageRecieved, + this, &BCodeTerminal::commandReceivedHandler); + + kafkaProducer.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaProducer.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaProducer.createProducer(); + + this->protocol = DeviceProtocolBase::deviceProtocolFactory(devType); } BCodeTerminal::~BCodeTerminal() @@ -21,6 +30,17 @@ this, &BCodeTerminal::dataReceivedHandler); } +void BCodeTerminal::mockReceivData() +{ + QByteArray buffer; + + // b-code terminal + buffer.append("$2621304-20 210100112100f90210.035422*"); + buffer.append("$3e21304-20 2101001111304-20 2101001210801H.1.00S.1.030008432*"); + + this->dataReceivedHandler(buffer); +} + void BCodeTerminal::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); @@ -73,6 +93,60 @@ 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))); } } + +void BCodeTerminal::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 BCodeTerminal::commandReceivedHandler(QJsonObject command) +{ + if (command.contains("deviceType") == false || command.value("deviceType").toString() != "07") + { + 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/BCodeTerminal.h b/DeviceHub/device/BCodeTerminal.h index b3aa5cb..f39ab6e 100644 --- a/DeviceHub/device/BCodeTerminal.h +++ b/DeviceHub/device/BCodeTerminal.h @@ -4,6 +4,7 @@ #include #include "device/DeviceBase.h" +#include "protocol/dto/BCodeTerminalStatusDto.h" class BCodeTerminal : public DeviceBase { @@ -12,13 +13,18 @@ explicit BCodeTerminal(QObject *parent = nullptr); ~BCodeTerminal(); + void mockReceivData() override; + void afterFramePhase(DeviceFrameBaseDto * frameDto); + void sendDataToSerial(QByteArray data) override; signals: void sendDataToDraw(DeviceFrameBaseDto * frameData); + void sendCommandToDisplay(QJsonObject command); public slots: void dataReceivedHandler(QByteArray data); + void commandReceivedHandler(QJsonObject command); }; #endif // BCODETERMINAL_H diff --git a/DeviceHub/BCodeTerminalForm.cpp b/DeviceHub/BCodeTerminalForm.cpp new file mode 100644 index 0000000..2c8bc26 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.cpp @@ -0,0 +1,63 @@ +#include "BCodeTerminalForm.h" +#include "ui_BCodeTerminalForm.h" +#include "DeviceHubWindow.h" + +BCodeTerminalForm::BCodeTerminalForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::BCodeTerminalForm) +{ + ui->setupUi(this); +} + +BCodeTerminalForm::~BCodeTerminalForm() +{ + delete ui; +} + +void BCodeTerminalForm::on_bctButt_clicked() +{ + // ������������ + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + BCodeTerminal * device = (BCodeTerminal *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("07").at(devIndex); + device->mockReceivData(); +} + +void BCodeTerminalForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // ������������������ + if (frameData->frameType == "0701") + { + BCodeTerminalStatusDto * statusFrameDto = (BCodeTerminalStatusDto *) frameData; + + } +} + +void BCodeTerminalForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("04"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "04") + { + ((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/BCodeTerminalForm.h b/DeviceHub/BCodeTerminalForm.h new file mode 100644 index 0000000..0ae7601 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.h @@ -0,0 +1,30 @@ +#ifndef BCODETERMINALFORM_H +#define BCODETERMINALFORM_H + +#include +#include "device/BCodeTerminal.h" + +namespace Ui { +class BCodeTerminalForm; +} + +class BCodeTerminalForm : public QWidget +{ + Q_OBJECT + +public: + explicit BCodeTerminalForm(QWidget *parent = nullptr); + ~BCodeTerminalForm(); + +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + +private slots: + void on_bctButt_clicked(); + +private: + Ui::BCodeTerminalForm *ui; +}; + +#endif // BCODETERMINALFORM_H diff --git a/DeviceHub/BCodeTerminalForm.ui b/DeviceHub/BCodeTerminalForm.ui new file mode 100644 index 0000000..9504c94 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.ui @@ -0,0 +1,34 @@ + + + + + BCodeTerminalForm + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + 50 + 20 + 180 + 40 + + + + Mock BCodeTerminal + + + + + + diff --git a/DeviceHub/DeviceHub.pro b/DeviceHub/DeviceHub.pro index 3f17e33..6dc7b34 100644 --- a/DeviceHub/DeviceHub.pro +++ b/DeviceHub/DeviceHub.pro @@ -15,20 +15,23 @@ # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 -SOURCES += main.cpp +SOURCES += main.cpp \ + BCodeTerminalForm.cpp SOURCES += DeviceHubWindow.cpp SOURCES += FrequencyTuningForm.cpp SOURCES += SignalGeneratorForm.cpp SOURCES += TimeSwitcherForm.cpp SOURCES += FreqSwitcherForm.cpp -HEADERS += DeviceHubWindow.h +HEADERS += DeviceHubWindow.h \ + BCodeTerminalForm.h HEADERS += FrequencyTuningForm.h HEADERS += SignalGeneratorForm.h HEADERS += TimeSwitcherForm.h HEADERS += FreqSwitcherForm.h -FORMS += DeviceHubWindow.ui +FORMS += DeviceHubWindow.ui \ + BCodeTerminalForm.ui FORMS += FrequencyTuningForm.ui FORMS += SignalGeneratorForm.ui FORMS += TimeSwitcherForm.ui diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 8050848..d2b0ac2 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -41,6 +41,9 @@ freqSwitForm = new FreqSwitcherForm(this); ui->stackedWidget->addWidget(freqSwitForm); + bCodeTermForm = new BCodeTerminalForm(this); + ui->stackedWidget->addWidget(bCodeTermForm); + // kafka consumer kafkaConsumer = new QKafkaConsumer(this); kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index bdc0749..9e2700e 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -11,6 +11,7 @@ #include "SignalGeneratorForm.h" #include "TimeSwitcherForm.h" #include "FreqSwitcherForm.h" +#include "BCodeTerminalForm.h" QT_BEGIN_NAMESPACE namespace Ui { class DeviceHubWindow; } @@ -38,6 +39,7 @@ SignalGeneratorForm * signGenForm; TimeSwitcherForm * tmSwitForm; FreqSwitcherForm * freqSwitForm; + BCodeTerminalForm * bCodeTermForm; private slots: void on_minButt_clicked(); diff --git a/DeviceHub/device/BCodeTerminal.cpp b/DeviceHub/device/BCodeTerminal.cpp index 19d33fd..b792982 100644 --- a/DeviceHub/device/BCodeTerminal.cpp +++ b/DeviceHub/device/BCodeTerminal.cpp @@ -1,18 +1,27 @@ #include "BCodeTerminal.h" - +#include "DeviceHubWindow.h" #include #include BCodeTerminal::BCodeTerminal(QObject* parent) : DeviceBase(parent) { + this->devType = "07"; connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &BCodeTerminal::dataReceivedHandler); - kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); - kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); - kafkaUtil.createProducer(); + connect(this, &BCodeTerminal::sendDataToDraw, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::drawDeviceFrameOnForm); + connect(this, &BCodeTerminal::sendCommandToDisplay, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::displayDeviceCommandOnForm); - this->protocol = DeviceStatusProtocolBase::deviceStatusProtocolFactory(typeid (this).name()); + connect(((DeviceHubWindow *)this->parent())->kafkaConsumer, &QKafkaConsumer::messageRecieved, + this, &BCodeTerminal::commandReceivedHandler); + + kafkaProducer.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaProducer.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaProducer.createProducer(); + + this->protocol = DeviceProtocolBase::deviceProtocolFactory(devType); } BCodeTerminal::~BCodeTerminal() @@ -21,6 +30,17 @@ this, &BCodeTerminal::dataReceivedHandler); } +void BCodeTerminal::mockReceivData() +{ + QByteArray buffer; + + // b-code terminal + buffer.append("$2621304-20 210100112100f90210.035422*"); + buffer.append("$3e21304-20 2101001111304-20 2101001210801H.1.00S.1.030008432*"); + + this->dataReceivedHandler(buffer); +} + void BCodeTerminal::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); @@ -73,6 +93,60 @@ 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))); } } + +void BCodeTerminal::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 BCodeTerminal::commandReceivedHandler(QJsonObject command) +{ + if (command.contains("deviceType") == false || command.value("deviceType").toString() != "07") + { + 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/BCodeTerminal.h b/DeviceHub/device/BCodeTerminal.h index b3aa5cb..f39ab6e 100644 --- a/DeviceHub/device/BCodeTerminal.h +++ b/DeviceHub/device/BCodeTerminal.h @@ -4,6 +4,7 @@ #include #include "device/DeviceBase.h" +#include "protocol/dto/BCodeTerminalStatusDto.h" class BCodeTerminal : public DeviceBase { @@ -12,13 +13,18 @@ explicit BCodeTerminal(QObject *parent = nullptr); ~BCodeTerminal(); + void mockReceivData() override; + void afterFramePhase(DeviceFrameBaseDto * frameDto); + void sendDataToSerial(QByteArray data) override; signals: void sendDataToDraw(DeviceFrameBaseDto * frameData); + void sendCommandToDisplay(QJsonObject command); public slots: void dataReceivedHandler(QByteArray data); + void commandReceivedHandler(QJsonObject command); }; #endif // BCODETERMINAL_H diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index 2d9d345..f1c6c59 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -3,6 +3,7 @@ #include "SignalGenerator.h" #include "TimeSwitcher.h" #include "FreqSwitcher.h" +#include "BCodeTerminal.h" #include @@ -51,6 +52,9 @@ } else if (deviceType == "06") { base = new FreqSwitcher(parent); + } else if (deviceType == "07") + { + base = new BCodeTerminal(parent); } return base; diff --git a/DeviceHub/BCodeTerminalForm.cpp b/DeviceHub/BCodeTerminalForm.cpp new file mode 100644 index 0000000..2c8bc26 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.cpp @@ -0,0 +1,63 @@ +#include "BCodeTerminalForm.h" +#include "ui_BCodeTerminalForm.h" +#include "DeviceHubWindow.h" + +BCodeTerminalForm::BCodeTerminalForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::BCodeTerminalForm) +{ + ui->setupUi(this); +} + +BCodeTerminalForm::~BCodeTerminalForm() +{ + delete ui; +} + +void BCodeTerminalForm::on_bctButt_clicked() +{ + // ������������ + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + BCodeTerminal * device = (BCodeTerminal *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("07").at(devIndex); + device->mockReceivData(); +} + +void BCodeTerminalForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // ������������������ + if (frameData->frameType == "0701") + { + BCodeTerminalStatusDto * statusFrameDto = (BCodeTerminalStatusDto *) frameData; + + } +} + +void BCodeTerminalForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("04"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "04") + { + ((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/BCodeTerminalForm.h b/DeviceHub/BCodeTerminalForm.h new file mode 100644 index 0000000..0ae7601 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.h @@ -0,0 +1,30 @@ +#ifndef BCODETERMINALFORM_H +#define BCODETERMINALFORM_H + +#include +#include "device/BCodeTerminal.h" + +namespace Ui { +class BCodeTerminalForm; +} + +class BCodeTerminalForm : public QWidget +{ + Q_OBJECT + +public: + explicit BCodeTerminalForm(QWidget *parent = nullptr); + ~BCodeTerminalForm(); + +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + +private slots: + void on_bctButt_clicked(); + +private: + Ui::BCodeTerminalForm *ui; +}; + +#endif // BCODETERMINALFORM_H diff --git a/DeviceHub/BCodeTerminalForm.ui b/DeviceHub/BCodeTerminalForm.ui new file mode 100644 index 0000000..9504c94 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.ui @@ -0,0 +1,34 @@ + + + + + BCodeTerminalForm + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + 50 + 20 + 180 + 40 + + + + Mock BCodeTerminal + + + + + + diff --git a/DeviceHub/DeviceHub.pro b/DeviceHub/DeviceHub.pro index 3f17e33..6dc7b34 100644 --- a/DeviceHub/DeviceHub.pro +++ b/DeviceHub/DeviceHub.pro @@ -15,20 +15,23 @@ # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 -SOURCES += main.cpp +SOURCES += main.cpp \ + BCodeTerminalForm.cpp SOURCES += DeviceHubWindow.cpp SOURCES += FrequencyTuningForm.cpp SOURCES += SignalGeneratorForm.cpp SOURCES += TimeSwitcherForm.cpp SOURCES += FreqSwitcherForm.cpp -HEADERS += DeviceHubWindow.h +HEADERS += DeviceHubWindow.h \ + BCodeTerminalForm.h HEADERS += FrequencyTuningForm.h HEADERS += SignalGeneratorForm.h HEADERS += TimeSwitcherForm.h HEADERS += FreqSwitcherForm.h -FORMS += DeviceHubWindow.ui +FORMS += DeviceHubWindow.ui \ + BCodeTerminalForm.ui FORMS += FrequencyTuningForm.ui FORMS += SignalGeneratorForm.ui FORMS += TimeSwitcherForm.ui diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 8050848..d2b0ac2 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -41,6 +41,9 @@ freqSwitForm = new FreqSwitcherForm(this); ui->stackedWidget->addWidget(freqSwitForm); + bCodeTermForm = new BCodeTerminalForm(this); + ui->stackedWidget->addWidget(bCodeTermForm); + // kafka consumer kafkaConsumer = new QKafkaConsumer(this); kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index bdc0749..9e2700e 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -11,6 +11,7 @@ #include "SignalGeneratorForm.h" #include "TimeSwitcherForm.h" #include "FreqSwitcherForm.h" +#include "BCodeTerminalForm.h" QT_BEGIN_NAMESPACE namespace Ui { class DeviceHubWindow; } @@ -38,6 +39,7 @@ SignalGeneratorForm * signGenForm; TimeSwitcherForm * tmSwitForm; FreqSwitcherForm * freqSwitForm; + BCodeTerminalForm * bCodeTermForm; private slots: void on_minButt_clicked(); diff --git a/DeviceHub/device/BCodeTerminal.cpp b/DeviceHub/device/BCodeTerminal.cpp index 19d33fd..b792982 100644 --- a/DeviceHub/device/BCodeTerminal.cpp +++ b/DeviceHub/device/BCodeTerminal.cpp @@ -1,18 +1,27 @@ #include "BCodeTerminal.h" - +#include "DeviceHubWindow.h" #include #include BCodeTerminal::BCodeTerminal(QObject* parent) : DeviceBase(parent) { + this->devType = "07"; connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &BCodeTerminal::dataReceivedHandler); - kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); - kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); - kafkaUtil.createProducer(); + connect(this, &BCodeTerminal::sendDataToDraw, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::drawDeviceFrameOnForm); + connect(this, &BCodeTerminal::sendCommandToDisplay, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::displayDeviceCommandOnForm); - this->protocol = DeviceStatusProtocolBase::deviceStatusProtocolFactory(typeid (this).name()); + connect(((DeviceHubWindow *)this->parent())->kafkaConsumer, &QKafkaConsumer::messageRecieved, + this, &BCodeTerminal::commandReceivedHandler); + + kafkaProducer.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaProducer.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaProducer.createProducer(); + + this->protocol = DeviceProtocolBase::deviceProtocolFactory(devType); } BCodeTerminal::~BCodeTerminal() @@ -21,6 +30,17 @@ this, &BCodeTerminal::dataReceivedHandler); } +void BCodeTerminal::mockReceivData() +{ + QByteArray buffer; + + // b-code terminal + buffer.append("$2621304-20 210100112100f90210.035422*"); + buffer.append("$3e21304-20 2101001111304-20 2101001210801H.1.00S.1.030008432*"); + + this->dataReceivedHandler(buffer); +} + void BCodeTerminal::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); @@ -73,6 +93,60 @@ 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))); } } + +void BCodeTerminal::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 BCodeTerminal::commandReceivedHandler(QJsonObject command) +{ + if (command.contains("deviceType") == false || command.value("deviceType").toString() != "07") + { + 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/BCodeTerminal.h b/DeviceHub/device/BCodeTerminal.h index b3aa5cb..f39ab6e 100644 --- a/DeviceHub/device/BCodeTerminal.h +++ b/DeviceHub/device/BCodeTerminal.h @@ -4,6 +4,7 @@ #include #include "device/DeviceBase.h" +#include "protocol/dto/BCodeTerminalStatusDto.h" class BCodeTerminal : public DeviceBase { @@ -12,13 +13,18 @@ explicit BCodeTerminal(QObject *parent = nullptr); ~BCodeTerminal(); + void mockReceivData() override; + void afterFramePhase(DeviceFrameBaseDto * frameDto); + void sendDataToSerial(QByteArray data) override; signals: void sendDataToDraw(DeviceFrameBaseDto * frameData); + void sendCommandToDisplay(QJsonObject command); public slots: void dataReceivedHandler(QByteArray data); + void commandReceivedHandler(QJsonObject command); }; #endif // BCODETERMINAL_H diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index 2d9d345..f1c6c59 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -3,6 +3,7 @@ #include "SignalGenerator.h" #include "TimeSwitcher.h" #include "FreqSwitcher.h" +#include "BCodeTerminal.h" #include @@ -51,6 +52,9 @@ } else if (deviceType == "06") { base = new FreqSwitcher(parent); + } else if (deviceType == "07") + { + base = new BCodeTerminal(parent); } return base; diff --git a/DeviceHub/device/device.pri b/DeviceHub/device/device.pri index 5ad8604..5c6c099 100644 --- a/DeviceHub/device/device.pri +++ b/DeviceHub/device/device.pri @@ -6,7 +6,7 @@ HEADERS += $$PWD/FreqSwitcher.h #HEADERS += $$PWD/TimeReplicator.h #HEADERS += $$PWD/FreqReplicator.h -#HEADERS += $$PWD/BCodeTerminal.h +HEADERS += $$PWD/BCodeTerminal.h SOURCES += $$PWD/DeviceBase.cpp SOURCES += $$PWD/FrequencyTuning.cpp @@ -15,4 +15,4 @@ SOURCES += $$PWD/FreqSwitcher.cpp #SOURCES += $$PWD/TimeReplicator.cpp #SOURCES += $$PWD/FreqReplicator.cpp -#SOURCES += $$PWD/BCodeTerminal.cpp +SOURCES += $$PWD/BCodeTerminal.cpp diff --git a/DeviceHub/BCodeTerminalForm.cpp b/DeviceHub/BCodeTerminalForm.cpp new file mode 100644 index 0000000..2c8bc26 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.cpp @@ -0,0 +1,63 @@ +#include "BCodeTerminalForm.h" +#include "ui_BCodeTerminalForm.h" +#include "DeviceHubWindow.h" + +BCodeTerminalForm::BCodeTerminalForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::BCodeTerminalForm) +{ + ui->setupUi(this); +} + +BCodeTerminalForm::~BCodeTerminalForm() +{ + delete ui; +} + +void BCodeTerminalForm::on_bctButt_clicked() +{ + // ������������ + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + BCodeTerminal * device = (BCodeTerminal *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("07").at(devIndex); + device->mockReceivData(); +} + +void BCodeTerminalForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // ������������������ + if (frameData->frameType == "0701") + { + BCodeTerminalStatusDto * statusFrameDto = (BCodeTerminalStatusDto *) frameData; + + } +} + +void BCodeTerminalForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("04"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "04") + { + ((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/BCodeTerminalForm.h b/DeviceHub/BCodeTerminalForm.h new file mode 100644 index 0000000..0ae7601 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.h @@ -0,0 +1,30 @@ +#ifndef BCODETERMINALFORM_H +#define BCODETERMINALFORM_H + +#include +#include "device/BCodeTerminal.h" + +namespace Ui { +class BCodeTerminalForm; +} + +class BCodeTerminalForm : public QWidget +{ + Q_OBJECT + +public: + explicit BCodeTerminalForm(QWidget *parent = nullptr); + ~BCodeTerminalForm(); + +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + +private slots: + void on_bctButt_clicked(); + +private: + Ui::BCodeTerminalForm *ui; +}; + +#endif // BCODETERMINALFORM_H diff --git a/DeviceHub/BCodeTerminalForm.ui b/DeviceHub/BCodeTerminalForm.ui new file mode 100644 index 0000000..9504c94 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.ui @@ -0,0 +1,34 @@ + + + + + BCodeTerminalForm + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + 50 + 20 + 180 + 40 + + + + Mock BCodeTerminal + + + + + + diff --git a/DeviceHub/DeviceHub.pro b/DeviceHub/DeviceHub.pro index 3f17e33..6dc7b34 100644 --- a/DeviceHub/DeviceHub.pro +++ b/DeviceHub/DeviceHub.pro @@ -15,20 +15,23 @@ # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 -SOURCES += main.cpp +SOURCES += main.cpp \ + BCodeTerminalForm.cpp SOURCES += DeviceHubWindow.cpp SOURCES += FrequencyTuningForm.cpp SOURCES += SignalGeneratorForm.cpp SOURCES += TimeSwitcherForm.cpp SOURCES += FreqSwitcherForm.cpp -HEADERS += DeviceHubWindow.h +HEADERS += DeviceHubWindow.h \ + BCodeTerminalForm.h HEADERS += FrequencyTuningForm.h HEADERS += SignalGeneratorForm.h HEADERS += TimeSwitcherForm.h HEADERS += FreqSwitcherForm.h -FORMS += DeviceHubWindow.ui +FORMS += DeviceHubWindow.ui \ + BCodeTerminalForm.ui FORMS += FrequencyTuningForm.ui FORMS += SignalGeneratorForm.ui FORMS += TimeSwitcherForm.ui diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 8050848..d2b0ac2 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -41,6 +41,9 @@ freqSwitForm = new FreqSwitcherForm(this); ui->stackedWidget->addWidget(freqSwitForm); + bCodeTermForm = new BCodeTerminalForm(this); + ui->stackedWidget->addWidget(bCodeTermForm); + // kafka consumer kafkaConsumer = new QKafkaConsumer(this); kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index bdc0749..9e2700e 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -11,6 +11,7 @@ #include "SignalGeneratorForm.h" #include "TimeSwitcherForm.h" #include "FreqSwitcherForm.h" +#include "BCodeTerminalForm.h" QT_BEGIN_NAMESPACE namespace Ui { class DeviceHubWindow; } @@ -38,6 +39,7 @@ SignalGeneratorForm * signGenForm; TimeSwitcherForm * tmSwitForm; FreqSwitcherForm * freqSwitForm; + BCodeTerminalForm * bCodeTermForm; private slots: void on_minButt_clicked(); diff --git a/DeviceHub/device/BCodeTerminal.cpp b/DeviceHub/device/BCodeTerminal.cpp index 19d33fd..b792982 100644 --- a/DeviceHub/device/BCodeTerminal.cpp +++ b/DeviceHub/device/BCodeTerminal.cpp @@ -1,18 +1,27 @@ #include "BCodeTerminal.h" - +#include "DeviceHubWindow.h" #include #include BCodeTerminal::BCodeTerminal(QObject* parent) : DeviceBase(parent) { + this->devType = "07"; connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &BCodeTerminal::dataReceivedHandler); - kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); - kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); - kafkaUtil.createProducer(); + connect(this, &BCodeTerminal::sendDataToDraw, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::drawDeviceFrameOnForm); + connect(this, &BCodeTerminal::sendCommandToDisplay, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::displayDeviceCommandOnForm); - this->protocol = DeviceStatusProtocolBase::deviceStatusProtocolFactory(typeid (this).name()); + connect(((DeviceHubWindow *)this->parent())->kafkaConsumer, &QKafkaConsumer::messageRecieved, + this, &BCodeTerminal::commandReceivedHandler); + + kafkaProducer.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaProducer.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaProducer.createProducer(); + + this->protocol = DeviceProtocolBase::deviceProtocolFactory(devType); } BCodeTerminal::~BCodeTerminal() @@ -21,6 +30,17 @@ this, &BCodeTerminal::dataReceivedHandler); } +void BCodeTerminal::mockReceivData() +{ + QByteArray buffer; + + // b-code terminal + buffer.append("$2621304-20 210100112100f90210.035422*"); + buffer.append("$3e21304-20 2101001111304-20 2101001210801H.1.00S.1.030008432*"); + + this->dataReceivedHandler(buffer); +} + void BCodeTerminal::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); @@ -73,6 +93,60 @@ 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))); } } + +void BCodeTerminal::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 BCodeTerminal::commandReceivedHandler(QJsonObject command) +{ + if (command.contains("deviceType") == false || command.value("deviceType").toString() != "07") + { + 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/BCodeTerminal.h b/DeviceHub/device/BCodeTerminal.h index b3aa5cb..f39ab6e 100644 --- a/DeviceHub/device/BCodeTerminal.h +++ b/DeviceHub/device/BCodeTerminal.h @@ -4,6 +4,7 @@ #include #include "device/DeviceBase.h" +#include "protocol/dto/BCodeTerminalStatusDto.h" class BCodeTerminal : public DeviceBase { @@ -12,13 +13,18 @@ explicit BCodeTerminal(QObject *parent = nullptr); ~BCodeTerminal(); + void mockReceivData() override; + void afterFramePhase(DeviceFrameBaseDto * frameDto); + void sendDataToSerial(QByteArray data) override; signals: void sendDataToDraw(DeviceFrameBaseDto * frameData); + void sendCommandToDisplay(QJsonObject command); public slots: void dataReceivedHandler(QByteArray data); + void commandReceivedHandler(QJsonObject command); }; #endif // BCODETERMINAL_H diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index 2d9d345..f1c6c59 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -3,6 +3,7 @@ #include "SignalGenerator.h" #include "TimeSwitcher.h" #include "FreqSwitcher.h" +#include "BCodeTerminal.h" #include @@ -51,6 +52,9 @@ } else if (deviceType == "06") { base = new FreqSwitcher(parent); + } else if (deviceType == "07") + { + base = new BCodeTerminal(parent); } return base; diff --git a/DeviceHub/device/device.pri b/DeviceHub/device/device.pri index 5ad8604..5c6c099 100644 --- a/DeviceHub/device/device.pri +++ b/DeviceHub/device/device.pri @@ -6,7 +6,7 @@ HEADERS += $$PWD/FreqSwitcher.h #HEADERS += $$PWD/TimeReplicator.h #HEADERS += $$PWD/FreqReplicator.h -#HEADERS += $$PWD/BCodeTerminal.h +HEADERS += $$PWD/BCodeTerminal.h SOURCES += $$PWD/DeviceBase.cpp SOURCES += $$PWD/FrequencyTuning.cpp @@ -15,4 +15,4 @@ SOURCES += $$PWD/FreqSwitcher.cpp #SOURCES += $$PWD/TimeReplicator.cpp #SOURCES += $$PWD/FreqReplicator.cpp -#SOURCES += $$PWD/BCodeTerminal.cpp +SOURCES += $$PWD/BCodeTerminal.cpp diff --git a/DeviceHub/protocol/BCodeTerminalProtocolBM.cpp b/DeviceHub/protocol/BCodeTerminalProtocolBM.cpp new file mode 100644 index 0000000..1dfe733 --- /dev/null +++ b/DeviceHub/protocol/BCodeTerminalProtocolBM.cpp @@ -0,0 +1,107 @@ +#include "BCodeTerminalProtocolBM.h" + +BCodeTerminalProtocolBM::BCodeTerminalProtocolBM(QObject* parent) : DeviceProtocolBase(parent) +{ + +} + +BCodeTerminalProtocolBM::~BCodeTerminalProtocolBM() +{ + +} + +// 解析数据 +bool BCodeTerminalProtocolBM::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); + int outBdcByte = 0; + int outBacByte = 0; + QString outStr = ""; + + ((BCodeTerminalStatusDto *) frameData)->devStatus = content.at(0); + ((BCodeTerminalStatusDto *) frameData)->bdcInStatus = content.at(2); + ((BCodeTerminalStatusDto *) frameData)->bacInStatus = content.at(3); + ((BCodeTerminalStatusDto *) frameData)->fiveInStatus = content.at(4); + + outBdcByte = content.mid(6, 1).toInt(0, 16); + outBacByte = content.mid(5, 1).toInt(0, 16); + for (int i = 0; i < 4; i++) + { + ((BCodeTerminalStatusDto *) frameData)->bdcOutStatusList.append(QString("%1").arg(outBdcByte % 2)); + ((BCodeTerminalStatusDto *) frameData)->bacOutStatusList.append(QString("%1").arg(outBacByte % 2)); + + outBdcByte = outBdcByte / 2; + outBacByte = outBacByte / 2; + } + + ((BCodeTerminalStatusDto *) frameData)->ref = content.at(7); + ((BCodeTerminalStatusDto *) frameData)->track = content.at(8); + ((BCodeTerminalStatusDto *) frameData)->bacRange = content.mid(9, 4).toFloat(); + ((BCodeTerminalStatusDto *) frameData)->bacRatio = content.mid(13, 1).toInt(); + + ((BCodeTerminalStatusDto *)frameData)->frameType = B_CODE_TERMINAL_STATUS_FRAME_TYPE; + + return true; +} + +QByteArray BCodeTerminalProtocolBM::generateSettingCommand(QString commandType, QString valueSet) +{ + QByteArray commandBytes; + + commandBytes.append("3"); + commandBytes.append("1304-20").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; +} + +DeviceFrameBaseDto * BCodeTerminalProtocolBM::frameFactory(int frameType) +{ + DeviceFrameBaseDto * frameData = new BCodeTerminalStatusDto(); + + return frameData; +} + +// +QList BCodeTerminalProtocolBM::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; +} + +// 检测帧格式,帧头帧尾 +int BCodeTerminalProtocolBM::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; +} diff --git a/DeviceHub/BCodeTerminalForm.cpp b/DeviceHub/BCodeTerminalForm.cpp new file mode 100644 index 0000000..2c8bc26 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.cpp @@ -0,0 +1,63 @@ +#include "BCodeTerminalForm.h" +#include "ui_BCodeTerminalForm.h" +#include "DeviceHubWindow.h" + +BCodeTerminalForm::BCodeTerminalForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::BCodeTerminalForm) +{ + ui->setupUi(this); +} + +BCodeTerminalForm::~BCodeTerminalForm() +{ + delete ui; +} + +void BCodeTerminalForm::on_bctButt_clicked() +{ + // ������������ + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + BCodeTerminal * device = (BCodeTerminal *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("07").at(devIndex); + device->mockReceivData(); +} + +void BCodeTerminalForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // ������������������ + if (frameData->frameType == "0701") + { + BCodeTerminalStatusDto * statusFrameDto = (BCodeTerminalStatusDto *) frameData; + + } +} + +void BCodeTerminalForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("04"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "04") + { + ((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/BCodeTerminalForm.h b/DeviceHub/BCodeTerminalForm.h new file mode 100644 index 0000000..0ae7601 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.h @@ -0,0 +1,30 @@ +#ifndef BCODETERMINALFORM_H +#define BCODETERMINALFORM_H + +#include +#include "device/BCodeTerminal.h" + +namespace Ui { +class BCodeTerminalForm; +} + +class BCodeTerminalForm : public QWidget +{ + Q_OBJECT + +public: + explicit BCodeTerminalForm(QWidget *parent = nullptr); + ~BCodeTerminalForm(); + +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + +private slots: + void on_bctButt_clicked(); + +private: + Ui::BCodeTerminalForm *ui; +}; + +#endif // BCODETERMINALFORM_H diff --git a/DeviceHub/BCodeTerminalForm.ui b/DeviceHub/BCodeTerminalForm.ui new file mode 100644 index 0000000..9504c94 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.ui @@ -0,0 +1,34 @@ + + + + + BCodeTerminalForm + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + 50 + 20 + 180 + 40 + + + + Mock BCodeTerminal + + + + + + diff --git a/DeviceHub/DeviceHub.pro b/DeviceHub/DeviceHub.pro index 3f17e33..6dc7b34 100644 --- a/DeviceHub/DeviceHub.pro +++ b/DeviceHub/DeviceHub.pro @@ -15,20 +15,23 @@ # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 -SOURCES += main.cpp +SOURCES += main.cpp \ + BCodeTerminalForm.cpp SOURCES += DeviceHubWindow.cpp SOURCES += FrequencyTuningForm.cpp SOURCES += SignalGeneratorForm.cpp SOURCES += TimeSwitcherForm.cpp SOURCES += FreqSwitcherForm.cpp -HEADERS += DeviceHubWindow.h +HEADERS += DeviceHubWindow.h \ + BCodeTerminalForm.h HEADERS += FrequencyTuningForm.h HEADERS += SignalGeneratorForm.h HEADERS += TimeSwitcherForm.h HEADERS += FreqSwitcherForm.h -FORMS += DeviceHubWindow.ui +FORMS += DeviceHubWindow.ui \ + BCodeTerminalForm.ui FORMS += FrequencyTuningForm.ui FORMS += SignalGeneratorForm.ui FORMS += TimeSwitcherForm.ui diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 8050848..d2b0ac2 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -41,6 +41,9 @@ freqSwitForm = new FreqSwitcherForm(this); ui->stackedWidget->addWidget(freqSwitForm); + bCodeTermForm = new BCodeTerminalForm(this); + ui->stackedWidget->addWidget(bCodeTermForm); + // kafka consumer kafkaConsumer = new QKafkaConsumer(this); kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index bdc0749..9e2700e 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -11,6 +11,7 @@ #include "SignalGeneratorForm.h" #include "TimeSwitcherForm.h" #include "FreqSwitcherForm.h" +#include "BCodeTerminalForm.h" QT_BEGIN_NAMESPACE namespace Ui { class DeviceHubWindow; } @@ -38,6 +39,7 @@ SignalGeneratorForm * signGenForm; TimeSwitcherForm * tmSwitForm; FreqSwitcherForm * freqSwitForm; + BCodeTerminalForm * bCodeTermForm; private slots: void on_minButt_clicked(); diff --git a/DeviceHub/device/BCodeTerminal.cpp b/DeviceHub/device/BCodeTerminal.cpp index 19d33fd..b792982 100644 --- a/DeviceHub/device/BCodeTerminal.cpp +++ b/DeviceHub/device/BCodeTerminal.cpp @@ -1,18 +1,27 @@ #include "BCodeTerminal.h" - +#include "DeviceHubWindow.h" #include #include BCodeTerminal::BCodeTerminal(QObject* parent) : DeviceBase(parent) { + this->devType = "07"; connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &BCodeTerminal::dataReceivedHandler); - kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); - kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); - kafkaUtil.createProducer(); + connect(this, &BCodeTerminal::sendDataToDraw, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::drawDeviceFrameOnForm); + connect(this, &BCodeTerminal::sendCommandToDisplay, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::displayDeviceCommandOnForm); - this->protocol = DeviceStatusProtocolBase::deviceStatusProtocolFactory(typeid (this).name()); + connect(((DeviceHubWindow *)this->parent())->kafkaConsumer, &QKafkaConsumer::messageRecieved, + this, &BCodeTerminal::commandReceivedHandler); + + kafkaProducer.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaProducer.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaProducer.createProducer(); + + this->protocol = DeviceProtocolBase::deviceProtocolFactory(devType); } BCodeTerminal::~BCodeTerminal() @@ -21,6 +30,17 @@ this, &BCodeTerminal::dataReceivedHandler); } +void BCodeTerminal::mockReceivData() +{ + QByteArray buffer; + + // b-code terminal + buffer.append("$2621304-20 210100112100f90210.035422*"); + buffer.append("$3e21304-20 2101001111304-20 2101001210801H.1.00S.1.030008432*"); + + this->dataReceivedHandler(buffer); +} + void BCodeTerminal::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); @@ -73,6 +93,60 @@ 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))); } } + +void BCodeTerminal::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 BCodeTerminal::commandReceivedHandler(QJsonObject command) +{ + if (command.contains("deviceType") == false || command.value("deviceType").toString() != "07") + { + 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/BCodeTerminal.h b/DeviceHub/device/BCodeTerminal.h index b3aa5cb..f39ab6e 100644 --- a/DeviceHub/device/BCodeTerminal.h +++ b/DeviceHub/device/BCodeTerminal.h @@ -4,6 +4,7 @@ #include #include "device/DeviceBase.h" +#include "protocol/dto/BCodeTerminalStatusDto.h" class BCodeTerminal : public DeviceBase { @@ -12,13 +13,18 @@ explicit BCodeTerminal(QObject *parent = nullptr); ~BCodeTerminal(); + void mockReceivData() override; + void afterFramePhase(DeviceFrameBaseDto * frameDto); + void sendDataToSerial(QByteArray data) override; signals: void sendDataToDraw(DeviceFrameBaseDto * frameData); + void sendCommandToDisplay(QJsonObject command); public slots: void dataReceivedHandler(QByteArray data); + void commandReceivedHandler(QJsonObject command); }; #endif // BCODETERMINAL_H diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index 2d9d345..f1c6c59 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -3,6 +3,7 @@ #include "SignalGenerator.h" #include "TimeSwitcher.h" #include "FreqSwitcher.h" +#include "BCodeTerminal.h" #include @@ -51,6 +52,9 @@ } else if (deviceType == "06") { base = new FreqSwitcher(parent); + } else if (deviceType == "07") + { + base = new BCodeTerminal(parent); } return base; diff --git a/DeviceHub/device/device.pri b/DeviceHub/device/device.pri index 5ad8604..5c6c099 100644 --- a/DeviceHub/device/device.pri +++ b/DeviceHub/device/device.pri @@ -6,7 +6,7 @@ HEADERS += $$PWD/FreqSwitcher.h #HEADERS += $$PWD/TimeReplicator.h #HEADERS += $$PWD/FreqReplicator.h -#HEADERS += $$PWD/BCodeTerminal.h +HEADERS += $$PWD/BCodeTerminal.h SOURCES += $$PWD/DeviceBase.cpp SOURCES += $$PWD/FrequencyTuning.cpp @@ -15,4 +15,4 @@ SOURCES += $$PWD/FreqSwitcher.cpp #SOURCES += $$PWD/TimeReplicator.cpp #SOURCES += $$PWD/FreqReplicator.cpp -#SOURCES += $$PWD/BCodeTerminal.cpp +SOURCES += $$PWD/BCodeTerminal.cpp diff --git a/DeviceHub/protocol/BCodeTerminalProtocolBM.cpp b/DeviceHub/protocol/BCodeTerminalProtocolBM.cpp new file mode 100644 index 0000000..1dfe733 --- /dev/null +++ b/DeviceHub/protocol/BCodeTerminalProtocolBM.cpp @@ -0,0 +1,107 @@ +#include "BCodeTerminalProtocolBM.h" + +BCodeTerminalProtocolBM::BCodeTerminalProtocolBM(QObject* parent) : DeviceProtocolBase(parent) +{ + +} + +BCodeTerminalProtocolBM::~BCodeTerminalProtocolBM() +{ + +} + +// 解析数据 +bool BCodeTerminalProtocolBM::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); + int outBdcByte = 0; + int outBacByte = 0; + QString outStr = ""; + + ((BCodeTerminalStatusDto *) frameData)->devStatus = content.at(0); + ((BCodeTerminalStatusDto *) frameData)->bdcInStatus = content.at(2); + ((BCodeTerminalStatusDto *) frameData)->bacInStatus = content.at(3); + ((BCodeTerminalStatusDto *) frameData)->fiveInStatus = content.at(4); + + outBdcByte = content.mid(6, 1).toInt(0, 16); + outBacByte = content.mid(5, 1).toInt(0, 16); + for (int i = 0; i < 4; i++) + { + ((BCodeTerminalStatusDto *) frameData)->bdcOutStatusList.append(QString("%1").arg(outBdcByte % 2)); + ((BCodeTerminalStatusDto *) frameData)->bacOutStatusList.append(QString("%1").arg(outBacByte % 2)); + + outBdcByte = outBdcByte / 2; + outBacByte = outBacByte / 2; + } + + ((BCodeTerminalStatusDto *) frameData)->ref = content.at(7); + ((BCodeTerminalStatusDto *) frameData)->track = content.at(8); + ((BCodeTerminalStatusDto *) frameData)->bacRange = content.mid(9, 4).toFloat(); + ((BCodeTerminalStatusDto *) frameData)->bacRatio = content.mid(13, 1).toInt(); + + ((BCodeTerminalStatusDto *)frameData)->frameType = B_CODE_TERMINAL_STATUS_FRAME_TYPE; + + return true; +} + +QByteArray BCodeTerminalProtocolBM::generateSettingCommand(QString commandType, QString valueSet) +{ + QByteArray commandBytes; + + commandBytes.append("3"); + commandBytes.append("1304-20").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; +} + +DeviceFrameBaseDto * BCodeTerminalProtocolBM::frameFactory(int frameType) +{ + DeviceFrameBaseDto * frameData = new BCodeTerminalStatusDto(); + + return frameData; +} + +// +QList BCodeTerminalProtocolBM::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; +} + +// 检测帧格式,帧头帧尾 +int BCodeTerminalProtocolBM::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; +} diff --git a/DeviceHub/protocol/BCodeTerminalProtocolBM.h b/DeviceHub/protocol/BCodeTerminalProtocolBM.h new file mode 100644 index 0000000..dcd5212 --- /dev/null +++ b/DeviceHub/protocol/BCodeTerminalProtocolBM.h @@ -0,0 +1,29 @@ +#ifndef BCODETERMINALPROTOCOLBM_H +#define BCODETERMINALPROTOCOLBM_H + +#include + +#include "common/utils/QByteUtil.h" +#include "DeviceProtocolBase.h" +#include "dto/BCodeTerminalStatusDto.h" + +class BCodeTerminalProtocolBM : public DeviceProtocolBase +{ + Q_OBJECT +public: + explicit BCodeTerminalProtocolBM(QObject *parent = nullptr); + ~BCodeTerminalProtocolBM(); + + // 解析数据 + 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 // BCODETERMINALPROTOCOLBM_H diff --git a/DeviceHub/BCodeTerminalForm.cpp b/DeviceHub/BCodeTerminalForm.cpp new file mode 100644 index 0000000..2c8bc26 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.cpp @@ -0,0 +1,63 @@ +#include "BCodeTerminalForm.h" +#include "ui_BCodeTerminalForm.h" +#include "DeviceHubWindow.h" + +BCodeTerminalForm::BCodeTerminalForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::BCodeTerminalForm) +{ + ui->setupUi(this); +} + +BCodeTerminalForm::~BCodeTerminalForm() +{ + delete ui; +} + +void BCodeTerminalForm::on_bctButt_clicked() +{ + // ������������ + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + BCodeTerminal * device = (BCodeTerminal *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("07").at(devIndex); + device->mockReceivData(); +} + +void BCodeTerminalForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // ������������������ + if (frameData->frameType == "0701") + { + BCodeTerminalStatusDto * statusFrameDto = (BCodeTerminalStatusDto *) frameData; + + } +} + +void BCodeTerminalForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("04"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "04") + { + ((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/BCodeTerminalForm.h b/DeviceHub/BCodeTerminalForm.h new file mode 100644 index 0000000..0ae7601 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.h @@ -0,0 +1,30 @@ +#ifndef BCODETERMINALFORM_H +#define BCODETERMINALFORM_H + +#include +#include "device/BCodeTerminal.h" + +namespace Ui { +class BCodeTerminalForm; +} + +class BCodeTerminalForm : public QWidget +{ + Q_OBJECT + +public: + explicit BCodeTerminalForm(QWidget *parent = nullptr); + ~BCodeTerminalForm(); + +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + +private slots: + void on_bctButt_clicked(); + +private: + Ui::BCodeTerminalForm *ui; +}; + +#endif // BCODETERMINALFORM_H diff --git a/DeviceHub/BCodeTerminalForm.ui b/DeviceHub/BCodeTerminalForm.ui new file mode 100644 index 0000000..9504c94 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.ui @@ -0,0 +1,34 @@ + + + + + BCodeTerminalForm + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + 50 + 20 + 180 + 40 + + + + Mock BCodeTerminal + + + + + + diff --git a/DeviceHub/DeviceHub.pro b/DeviceHub/DeviceHub.pro index 3f17e33..6dc7b34 100644 --- a/DeviceHub/DeviceHub.pro +++ b/DeviceHub/DeviceHub.pro @@ -15,20 +15,23 @@ # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 -SOURCES += main.cpp +SOURCES += main.cpp \ + BCodeTerminalForm.cpp SOURCES += DeviceHubWindow.cpp SOURCES += FrequencyTuningForm.cpp SOURCES += SignalGeneratorForm.cpp SOURCES += TimeSwitcherForm.cpp SOURCES += FreqSwitcherForm.cpp -HEADERS += DeviceHubWindow.h +HEADERS += DeviceHubWindow.h \ + BCodeTerminalForm.h HEADERS += FrequencyTuningForm.h HEADERS += SignalGeneratorForm.h HEADERS += TimeSwitcherForm.h HEADERS += FreqSwitcherForm.h -FORMS += DeviceHubWindow.ui +FORMS += DeviceHubWindow.ui \ + BCodeTerminalForm.ui FORMS += FrequencyTuningForm.ui FORMS += SignalGeneratorForm.ui FORMS += TimeSwitcherForm.ui diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 8050848..d2b0ac2 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -41,6 +41,9 @@ freqSwitForm = new FreqSwitcherForm(this); ui->stackedWidget->addWidget(freqSwitForm); + bCodeTermForm = new BCodeTerminalForm(this); + ui->stackedWidget->addWidget(bCodeTermForm); + // kafka consumer kafkaConsumer = new QKafkaConsumer(this); kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index bdc0749..9e2700e 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -11,6 +11,7 @@ #include "SignalGeneratorForm.h" #include "TimeSwitcherForm.h" #include "FreqSwitcherForm.h" +#include "BCodeTerminalForm.h" QT_BEGIN_NAMESPACE namespace Ui { class DeviceHubWindow; } @@ -38,6 +39,7 @@ SignalGeneratorForm * signGenForm; TimeSwitcherForm * tmSwitForm; FreqSwitcherForm * freqSwitForm; + BCodeTerminalForm * bCodeTermForm; private slots: void on_minButt_clicked(); diff --git a/DeviceHub/device/BCodeTerminal.cpp b/DeviceHub/device/BCodeTerminal.cpp index 19d33fd..b792982 100644 --- a/DeviceHub/device/BCodeTerminal.cpp +++ b/DeviceHub/device/BCodeTerminal.cpp @@ -1,18 +1,27 @@ #include "BCodeTerminal.h" - +#include "DeviceHubWindow.h" #include #include BCodeTerminal::BCodeTerminal(QObject* parent) : DeviceBase(parent) { + this->devType = "07"; connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &BCodeTerminal::dataReceivedHandler); - kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); - kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); - kafkaUtil.createProducer(); + connect(this, &BCodeTerminal::sendDataToDraw, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::drawDeviceFrameOnForm); + connect(this, &BCodeTerminal::sendCommandToDisplay, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::displayDeviceCommandOnForm); - this->protocol = DeviceStatusProtocolBase::deviceStatusProtocolFactory(typeid (this).name()); + connect(((DeviceHubWindow *)this->parent())->kafkaConsumer, &QKafkaConsumer::messageRecieved, + this, &BCodeTerminal::commandReceivedHandler); + + kafkaProducer.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaProducer.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaProducer.createProducer(); + + this->protocol = DeviceProtocolBase::deviceProtocolFactory(devType); } BCodeTerminal::~BCodeTerminal() @@ -21,6 +30,17 @@ this, &BCodeTerminal::dataReceivedHandler); } +void BCodeTerminal::mockReceivData() +{ + QByteArray buffer; + + // b-code terminal + buffer.append("$2621304-20 210100112100f90210.035422*"); + buffer.append("$3e21304-20 2101001111304-20 2101001210801H.1.00S.1.030008432*"); + + this->dataReceivedHandler(buffer); +} + void BCodeTerminal::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); @@ -73,6 +93,60 @@ 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))); } } + +void BCodeTerminal::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 BCodeTerminal::commandReceivedHandler(QJsonObject command) +{ + if (command.contains("deviceType") == false || command.value("deviceType").toString() != "07") + { + 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/BCodeTerminal.h b/DeviceHub/device/BCodeTerminal.h index b3aa5cb..f39ab6e 100644 --- a/DeviceHub/device/BCodeTerminal.h +++ b/DeviceHub/device/BCodeTerminal.h @@ -4,6 +4,7 @@ #include #include "device/DeviceBase.h" +#include "protocol/dto/BCodeTerminalStatusDto.h" class BCodeTerminal : public DeviceBase { @@ -12,13 +13,18 @@ explicit BCodeTerminal(QObject *parent = nullptr); ~BCodeTerminal(); + void mockReceivData() override; + void afterFramePhase(DeviceFrameBaseDto * frameDto); + void sendDataToSerial(QByteArray data) override; signals: void sendDataToDraw(DeviceFrameBaseDto * frameData); + void sendCommandToDisplay(QJsonObject command); public slots: void dataReceivedHandler(QByteArray data); + void commandReceivedHandler(QJsonObject command); }; #endif // BCODETERMINAL_H diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index 2d9d345..f1c6c59 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -3,6 +3,7 @@ #include "SignalGenerator.h" #include "TimeSwitcher.h" #include "FreqSwitcher.h" +#include "BCodeTerminal.h" #include @@ -51,6 +52,9 @@ } else if (deviceType == "06") { base = new FreqSwitcher(parent); + } else if (deviceType == "07") + { + base = new BCodeTerminal(parent); } return base; diff --git a/DeviceHub/device/device.pri b/DeviceHub/device/device.pri index 5ad8604..5c6c099 100644 --- a/DeviceHub/device/device.pri +++ b/DeviceHub/device/device.pri @@ -6,7 +6,7 @@ HEADERS += $$PWD/FreqSwitcher.h #HEADERS += $$PWD/TimeReplicator.h #HEADERS += $$PWD/FreqReplicator.h -#HEADERS += $$PWD/BCodeTerminal.h +HEADERS += $$PWD/BCodeTerminal.h SOURCES += $$PWD/DeviceBase.cpp SOURCES += $$PWD/FrequencyTuning.cpp @@ -15,4 +15,4 @@ SOURCES += $$PWD/FreqSwitcher.cpp #SOURCES += $$PWD/TimeReplicator.cpp #SOURCES += $$PWD/FreqReplicator.cpp -#SOURCES += $$PWD/BCodeTerminal.cpp +SOURCES += $$PWD/BCodeTerminal.cpp diff --git a/DeviceHub/protocol/BCodeTerminalProtocolBM.cpp b/DeviceHub/protocol/BCodeTerminalProtocolBM.cpp new file mode 100644 index 0000000..1dfe733 --- /dev/null +++ b/DeviceHub/protocol/BCodeTerminalProtocolBM.cpp @@ -0,0 +1,107 @@ +#include "BCodeTerminalProtocolBM.h" + +BCodeTerminalProtocolBM::BCodeTerminalProtocolBM(QObject* parent) : DeviceProtocolBase(parent) +{ + +} + +BCodeTerminalProtocolBM::~BCodeTerminalProtocolBM() +{ + +} + +// 解析数据 +bool BCodeTerminalProtocolBM::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); + int outBdcByte = 0; + int outBacByte = 0; + QString outStr = ""; + + ((BCodeTerminalStatusDto *) frameData)->devStatus = content.at(0); + ((BCodeTerminalStatusDto *) frameData)->bdcInStatus = content.at(2); + ((BCodeTerminalStatusDto *) frameData)->bacInStatus = content.at(3); + ((BCodeTerminalStatusDto *) frameData)->fiveInStatus = content.at(4); + + outBdcByte = content.mid(6, 1).toInt(0, 16); + outBacByte = content.mid(5, 1).toInt(0, 16); + for (int i = 0; i < 4; i++) + { + ((BCodeTerminalStatusDto *) frameData)->bdcOutStatusList.append(QString("%1").arg(outBdcByte % 2)); + ((BCodeTerminalStatusDto *) frameData)->bacOutStatusList.append(QString("%1").arg(outBacByte % 2)); + + outBdcByte = outBdcByte / 2; + outBacByte = outBacByte / 2; + } + + ((BCodeTerminalStatusDto *) frameData)->ref = content.at(7); + ((BCodeTerminalStatusDto *) frameData)->track = content.at(8); + ((BCodeTerminalStatusDto *) frameData)->bacRange = content.mid(9, 4).toFloat(); + ((BCodeTerminalStatusDto *) frameData)->bacRatio = content.mid(13, 1).toInt(); + + ((BCodeTerminalStatusDto *)frameData)->frameType = B_CODE_TERMINAL_STATUS_FRAME_TYPE; + + return true; +} + +QByteArray BCodeTerminalProtocolBM::generateSettingCommand(QString commandType, QString valueSet) +{ + QByteArray commandBytes; + + commandBytes.append("3"); + commandBytes.append("1304-20").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; +} + +DeviceFrameBaseDto * BCodeTerminalProtocolBM::frameFactory(int frameType) +{ + DeviceFrameBaseDto * frameData = new BCodeTerminalStatusDto(); + + return frameData; +} + +// +QList BCodeTerminalProtocolBM::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; +} + +// 检测帧格式,帧头帧尾 +int BCodeTerminalProtocolBM::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; +} diff --git a/DeviceHub/protocol/BCodeTerminalProtocolBM.h b/DeviceHub/protocol/BCodeTerminalProtocolBM.h new file mode 100644 index 0000000..dcd5212 --- /dev/null +++ b/DeviceHub/protocol/BCodeTerminalProtocolBM.h @@ -0,0 +1,29 @@ +#ifndef BCODETERMINALPROTOCOLBM_H +#define BCODETERMINALPROTOCOLBM_H + +#include + +#include "common/utils/QByteUtil.h" +#include "DeviceProtocolBase.h" +#include "dto/BCodeTerminalStatusDto.h" + +class BCodeTerminalProtocolBM : public DeviceProtocolBase +{ + Q_OBJECT +public: + explicit BCodeTerminalProtocolBM(QObject *parent = nullptr); + ~BCodeTerminalProtocolBM(); + + // 解析数据 + 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 // BCODETERMINALPROTOCOLBM_H diff --git a/DeviceHub/protocol/DeviceProtocolBase.cpp b/DeviceHub/protocol/DeviceProtocolBase.cpp index c7d5238..9c46989 100644 --- a/DeviceHub/protocol/DeviceProtocolBase.cpp +++ b/DeviceHub/protocol/DeviceProtocolBase.cpp @@ -5,7 +5,7 @@ #include "FreqSwitcherProtocolBM.h" //#include "TimeReplicatorProtocolBM.h" //#include "FreqReplicatorProtocolTX.h" -//#include "BCodeTerminalProtocolBM.h" +#include "BCodeTerminalProtocolBM.h" #include @@ -28,6 +28,9 @@ } else if (deviceType == "06") { return new FreqSwitcherProtocolBM(); + } else if (deviceType == "07") + { + return new BCodeTerminalProtocolBM(); } // if (deviceType.contains("SignalGenerator") == true) @@ -84,3 +87,21 @@ str.sprintf("%02X", xorValue); return str; } + +QString DeviceProtocolBase::buildCRC16(QByteArray byteArray) +{ + qint16 crc = 0; + for (int i = 0; i < byteArray.size(); i++) { + crc = crc ^ byteArray[i] << 8; + for (int j = 0; j < 8; j++) { + if ((crc & 0x8000) > 0) { + crc = crc << 1 ^ 0x1021; + } else { + crc = crc << 1; + } + } + } + + QByteArray crcBytes = QByteUtil::ULongToBytes(crc, 2); + return QByteUtil::binToHexString(crcBytes); +} diff --git a/DeviceHub/BCodeTerminalForm.cpp b/DeviceHub/BCodeTerminalForm.cpp new file mode 100644 index 0000000..2c8bc26 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.cpp @@ -0,0 +1,63 @@ +#include "BCodeTerminalForm.h" +#include "ui_BCodeTerminalForm.h" +#include "DeviceHubWindow.h" + +BCodeTerminalForm::BCodeTerminalForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::BCodeTerminalForm) +{ + ui->setupUi(this); +} + +BCodeTerminalForm::~BCodeTerminalForm() +{ + delete ui; +} + +void BCodeTerminalForm::on_bctButt_clicked() +{ + // ������������ + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + BCodeTerminal * device = (BCodeTerminal *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("07").at(devIndex); + device->mockReceivData(); +} + +void BCodeTerminalForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // ������������������ + if (frameData->frameType == "0701") + { + BCodeTerminalStatusDto * statusFrameDto = (BCodeTerminalStatusDto *) frameData; + + } +} + +void BCodeTerminalForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("04"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "04") + { + ((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/BCodeTerminalForm.h b/DeviceHub/BCodeTerminalForm.h new file mode 100644 index 0000000..0ae7601 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.h @@ -0,0 +1,30 @@ +#ifndef BCODETERMINALFORM_H +#define BCODETERMINALFORM_H + +#include +#include "device/BCodeTerminal.h" + +namespace Ui { +class BCodeTerminalForm; +} + +class BCodeTerminalForm : public QWidget +{ + Q_OBJECT + +public: + explicit BCodeTerminalForm(QWidget *parent = nullptr); + ~BCodeTerminalForm(); + +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + +private slots: + void on_bctButt_clicked(); + +private: + Ui::BCodeTerminalForm *ui; +}; + +#endif // BCODETERMINALFORM_H diff --git a/DeviceHub/BCodeTerminalForm.ui b/DeviceHub/BCodeTerminalForm.ui new file mode 100644 index 0000000..9504c94 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.ui @@ -0,0 +1,34 @@ + + + + + BCodeTerminalForm + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + 50 + 20 + 180 + 40 + + + + Mock BCodeTerminal + + + + + + diff --git a/DeviceHub/DeviceHub.pro b/DeviceHub/DeviceHub.pro index 3f17e33..6dc7b34 100644 --- a/DeviceHub/DeviceHub.pro +++ b/DeviceHub/DeviceHub.pro @@ -15,20 +15,23 @@ # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 -SOURCES += main.cpp +SOURCES += main.cpp \ + BCodeTerminalForm.cpp SOURCES += DeviceHubWindow.cpp SOURCES += FrequencyTuningForm.cpp SOURCES += SignalGeneratorForm.cpp SOURCES += TimeSwitcherForm.cpp SOURCES += FreqSwitcherForm.cpp -HEADERS += DeviceHubWindow.h +HEADERS += DeviceHubWindow.h \ + BCodeTerminalForm.h HEADERS += FrequencyTuningForm.h HEADERS += SignalGeneratorForm.h HEADERS += TimeSwitcherForm.h HEADERS += FreqSwitcherForm.h -FORMS += DeviceHubWindow.ui +FORMS += DeviceHubWindow.ui \ + BCodeTerminalForm.ui FORMS += FrequencyTuningForm.ui FORMS += SignalGeneratorForm.ui FORMS += TimeSwitcherForm.ui diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 8050848..d2b0ac2 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -41,6 +41,9 @@ freqSwitForm = new FreqSwitcherForm(this); ui->stackedWidget->addWidget(freqSwitForm); + bCodeTermForm = new BCodeTerminalForm(this); + ui->stackedWidget->addWidget(bCodeTermForm); + // kafka consumer kafkaConsumer = new QKafkaConsumer(this); kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index bdc0749..9e2700e 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -11,6 +11,7 @@ #include "SignalGeneratorForm.h" #include "TimeSwitcherForm.h" #include "FreqSwitcherForm.h" +#include "BCodeTerminalForm.h" QT_BEGIN_NAMESPACE namespace Ui { class DeviceHubWindow; } @@ -38,6 +39,7 @@ SignalGeneratorForm * signGenForm; TimeSwitcherForm * tmSwitForm; FreqSwitcherForm * freqSwitForm; + BCodeTerminalForm * bCodeTermForm; private slots: void on_minButt_clicked(); diff --git a/DeviceHub/device/BCodeTerminal.cpp b/DeviceHub/device/BCodeTerminal.cpp index 19d33fd..b792982 100644 --- a/DeviceHub/device/BCodeTerminal.cpp +++ b/DeviceHub/device/BCodeTerminal.cpp @@ -1,18 +1,27 @@ #include "BCodeTerminal.h" - +#include "DeviceHubWindow.h" #include #include BCodeTerminal::BCodeTerminal(QObject* parent) : DeviceBase(parent) { + this->devType = "07"; connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &BCodeTerminal::dataReceivedHandler); - kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); - kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); - kafkaUtil.createProducer(); + connect(this, &BCodeTerminal::sendDataToDraw, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::drawDeviceFrameOnForm); + connect(this, &BCodeTerminal::sendCommandToDisplay, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::displayDeviceCommandOnForm); - this->protocol = DeviceStatusProtocolBase::deviceStatusProtocolFactory(typeid (this).name()); + connect(((DeviceHubWindow *)this->parent())->kafkaConsumer, &QKafkaConsumer::messageRecieved, + this, &BCodeTerminal::commandReceivedHandler); + + kafkaProducer.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaProducer.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaProducer.createProducer(); + + this->protocol = DeviceProtocolBase::deviceProtocolFactory(devType); } BCodeTerminal::~BCodeTerminal() @@ -21,6 +30,17 @@ this, &BCodeTerminal::dataReceivedHandler); } +void BCodeTerminal::mockReceivData() +{ + QByteArray buffer; + + // b-code terminal + buffer.append("$2621304-20 210100112100f90210.035422*"); + buffer.append("$3e21304-20 2101001111304-20 2101001210801H.1.00S.1.030008432*"); + + this->dataReceivedHandler(buffer); +} + void BCodeTerminal::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); @@ -73,6 +93,60 @@ 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))); } } + +void BCodeTerminal::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 BCodeTerminal::commandReceivedHandler(QJsonObject command) +{ + if (command.contains("deviceType") == false || command.value("deviceType").toString() != "07") + { + 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/BCodeTerminal.h b/DeviceHub/device/BCodeTerminal.h index b3aa5cb..f39ab6e 100644 --- a/DeviceHub/device/BCodeTerminal.h +++ b/DeviceHub/device/BCodeTerminal.h @@ -4,6 +4,7 @@ #include #include "device/DeviceBase.h" +#include "protocol/dto/BCodeTerminalStatusDto.h" class BCodeTerminal : public DeviceBase { @@ -12,13 +13,18 @@ explicit BCodeTerminal(QObject *parent = nullptr); ~BCodeTerminal(); + void mockReceivData() override; + void afterFramePhase(DeviceFrameBaseDto * frameDto); + void sendDataToSerial(QByteArray data) override; signals: void sendDataToDraw(DeviceFrameBaseDto * frameData); + void sendCommandToDisplay(QJsonObject command); public slots: void dataReceivedHandler(QByteArray data); + void commandReceivedHandler(QJsonObject command); }; #endif // BCODETERMINAL_H diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index 2d9d345..f1c6c59 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -3,6 +3,7 @@ #include "SignalGenerator.h" #include "TimeSwitcher.h" #include "FreqSwitcher.h" +#include "BCodeTerminal.h" #include @@ -51,6 +52,9 @@ } else if (deviceType == "06") { base = new FreqSwitcher(parent); + } else if (deviceType == "07") + { + base = new BCodeTerminal(parent); } return base; diff --git a/DeviceHub/device/device.pri b/DeviceHub/device/device.pri index 5ad8604..5c6c099 100644 --- a/DeviceHub/device/device.pri +++ b/DeviceHub/device/device.pri @@ -6,7 +6,7 @@ HEADERS += $$PWD/FreqSwitcher.h #HEADERS += $$PWD/TimeReplicator.h #HEADERS += $$PWD/FreqReplicator.h -#HEADERS += $$PWD/BCodeTerminal.h +HEADERS += $$PWD/BCodeTerminal.h SOURCES += $$PWD/DeviceBase.cpp SOURCES += $$PWD/FrequencyTuning.cpp @@ -15,4 +15,4 @@ SOURCES += $$PWD/FreqSwitcher.cpp #SOURCES += $$PWD/TimeReplicator.cpp #SOURCES += $$PWD/FreqReplicator.cpp -#SOURCES += $$PWD/BCodeTerminal.cpp +SOURCES += $$PWD/BCodeTerminal.cpp diff --git a/DeviceHub/protocol/BCodeTerminalProtocolBM.cpp b/DeviceHub/protocol/BCodeTerminalProtocolBM.cpp new file mode 100644 index 0000000..1dfe733 --- /dev/null +++ b/DeviceHub/protocol/BCodeTerminalProtocolBM.cpp @@ -0,0 +1,107 @@ +#include "BCodeTerminalProtocolBM.h" + +BCodeTerminalProtocolBM::BCodeTerminalProtocolBM(QObject* parent) : DeviceProtocolBase(parent) +{ + +} + +BCodeTerminalProtocolBM::~BCodeTerminalProtocolBM() +{ + +} + +// 解析数据 +bool BCodeTerminalProtocolBM::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); + int outBdcByte = 0; + int outBacByte = 0; + QString outStr = ""; + + ((BCodeTerminalStatusDto *) frameData)->devStatus = content.at(0); + ((BCodeTerminalStatusDto *) frameData)->bdcInStatus = content.at(2); + ((BCodeTerminalStatusDto *) frameData)->bacInStatus = content.at(3); + ((BCodeTerminalStatusDto *) frameData)->fiveInStatus = content.at(4); + + outBdcByte = content.mid(6, 1).toInt(0, 16); + outBacByte = content.mid(5, 1).toInt(0, 16); + for (int i = 0; i < 4; i++) + { + ((BCodeTerminalStatusDto *) frameData)->bdcOutStatusList.append(QString("%1").arg(outBdcByte % 2)); + ((BCodeTerminalStatusDto *) frameData)->bacOutStatusList.append(QString("%1").arg(outBacByte % 2)); + + outBdcByte = outBdcByte / 2; + outBacByte = outBacByte / 2; + } + + ((BCodeTerminalStatusDto *) frameData)->ref = content.at(7); + ((BCodeTerminalStatusDto *) frameData)->track = content.at(8); + ((BCodeTerminalStatusDto *) frameData)->bacRange = content.mid(9, 4).toFloat(); + ((BCodeTerminalStatusDto *) frameData)->bacRatio = content.mid(13, 1).toInt(); + + ((BCodeTerminalStatusDto *)frameData)->frameType = B_CODE_TERMINAL_STATUS_FRAME_TYPE; + + return true; +} + +QByteArray BCodeTerminalProtocolBM::generateSettingCommand(QString commandType, QString valueSet) +{ + QByteArray commandBytes; + + commandBytes.append("3"); + commandBytes.append("1304-20").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; +} + +DeviceFrameBaseDto * BCodeTerminalProtocolBM::frameFactory(int frameType) +{ + DeviceFrameBaseDto * frameData = new BCodeTerminalStatusDto(); + + return frameData; +} + +// +QList BCodeTerminalProtocolBM::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; +} + +// 检测帧格式,帧头帧尾 +int BCodeTerminalProtocolBM::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; +} diff --git a/DeviceHub/protocol/BCodeTerminalProtocolBM.h b/DeviceHub/protocol/BCodeTerminalProtocolBM.h new file mode 100644 index 0000000..dcd5212 --- /dev/null +++ b/DeviceHub/protocol/BCodeTerminalProtocolBM.h @@ -0,0 +1,29 @@ +#ifndef BCODETERMINALPROTOCOLBM_H +#define BCODETERMINALPROTOCOLBM_H + +#include + +#include "common/utils/QByteUtil.h" +#include "DeviceProtocolBase.h" +#include "dto/BCodeTerminalStatusDto.h" + +class BCodeTerminalProtocolBM : public DeviceProtocolBase +{ + Q_OBJECT +public: + explicit BCodeTerminalProtocolBM(QObject *parent = nullptr); + ~BCodeTerminalProtocolBM(); + + // 解析数据 + 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 // BCODETERMINALPROTOCOLBM_H diff --git a/DeviceHub/protocol/DeviceProtocolBase.cpp b/DeviceHub/protocol/DeviceProtocolBase.cpp index c7d5238..9c46989 100644 --- a/DeviceHub/protocol/DeviceProtocolBase.cpp +++ b/DeviceHub/protocol/DeviceProtocolBase.cpp @@ -5,7 +5,7 @@ #include "FreqSwitcherProtocolBM.h" //#include "TimeReplicatorProtocolBM.h" //#include "FreqReplicatorProtocolTX.h" -//#include "BCodeTerminalProtocolBM.h" +#include "BCodeTerminalProtocolBM.h" #include @@ -28,6 +28,9 @@ } else if (deviceType == "06") { return new FreqSwitcherProtocolBM(); + } else if (deviceType == "07") + { + return new BCodeTerminalProtocolBM(); } // if (deviceType.contains("SignalGenerator") == true) @@ -84,3 +87,21 @@ str.sprintf("%02X", xorValue); return str; } + +QString DeviceProtocolBase::buildCRC16(QByteArray byteArray) +{ + qint16 crc = 0; + for (int i = 0; i < byteArray.size(); i++) { + crc = crc ^ byteArray[i] << 8; + for (int j = 0; j < 8; j++) { + if ((crc & 0x8000) > 0) { + crc = crc << 1 ^ 0x1021; + } else { + crc = crc << 1; + } + } + } + + QByteArray crcBytes = QByteUtil::ULongToBytes(crc, 2); + return QByteUtil::binToHexString(crcBytes); +} diff --git a/DeviceHub/protocol/DeviceProtocolBase.h b/DeviceHub/protocol/DeviceProtocolBase.h index 8946995..396bda6 100644 --- a/DeviceHub/protocol/DeviceProtocolBase.h +++ b/DeviceHub/protocol/DeviceProtocolBase.h @@ -110,6 +110,7 @@ static DeviceProtocolBase * deviceProtocolFactory(QString deviceType); QString calculateXOR(QByteArray byteArray); + QString buildCRC16(QByteArray byteArray); virtual DeviceFrameBaseDto * frameFactory(int frameType) = 0; virtual int checkFrame(QByteArray rawData) = 0; diff --git a/DeviceHub/BCodeTerminalForm.cpp b/DeviceHub/BCodeTerminalForm.cpp new file mode 100644 index 0000000..2c8bc26 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.cpp @@ -0,0 +1,63 @@ +#include "BCodeTerminalForm.h" +#include "ui_BCodeTerminalForm.h" +#include "DeviceHubWindow.h" + +BCodeTerminalForm::BCodeTerminalForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::BCodeTerminalForm) +{ + ui->setupUi(this); +} + +BCodeTerminalForm::~BCodeTerminalForm() +{ + delete ui; +} + +void BCodeTerminalForm::on_bctButt_clicked() +{ + // ������������ + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + BCodeTerminal * device = (BCodeTerminal *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("07").at(devIndex); + device->mockReceivData(); +} + +void BCodeTerminalForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // ������������������ + if (frameData->frameType == "0701") + { + BCodeTerminalStatusDto * statusFrameDto = (BCodeTerminalStatusDto *) frameData; + + } +} + +void BCodeTerminalForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("04"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "04") + { + ((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/BCodeTerminalForm.h b/DeviceHub/BCodeTerminalForm.h new file mode 100644 index 0000000..0ae7601 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.h @@ -0,0 +1,30 @@ +#ifndef BCODETERMINALFORM_H +#define BCODETERMINALFORM_H + +#include +#include "device/BCodeTerminal.h" + +namespace Ui { +class BCodeTerminalForm; +} + +class BCodeTerminalForm : public QWidget +{ + Q_OBJECT + +public: + explicit BCodeTerminalForm(QWidget *parent = nullptr); + ~BCodeTerminalForm(); + +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + +private slots: + void on_bctButt_clicked(); + +private: + Ui::BCodeTerminalForm *ui; +}; + +#endif // BCODETERMINALFORM_H diff --git a/DeviceHub/BCodeTerminalForm.ui b/DeviceHub/BCodeTerminalForm.ui new file mode 100644 index 0000000..9504c94 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.ui @@ -0,0 +1,34 @@ + + + + + BCodeTerminalForm + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + 50 + 20 + 180 + 40 + + + + Mock BCodeTerminal + + + + + + diff --git a/DeviceHub/DeviceHub.pro b/DeviceHub/DeviceHub.pro index 3f17e33..6dc7b34 100644 --- a/DeviceHub/DeviceHub.pro +++ b/DeviceHub/DeviceHub.pro @@ -15,20 +15,23 @@ # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 -SOURCES += main.cpp +SOURCES += main.cpp \ + BCodeTerminalForm.cpp SOURCES += DeviceHubWindow.cpp SOURCES += FrequencyTuningForm.cpp SOURCES += SignalGeneratorForm.cpp SOURCES += TimeSwitcherForm.cpp SOURCES += FreqSwitcherForm.cpp -HEADERS += DeviceHubWindow.h +HEADERS += DeviceHubWindow.h \ + BCodeTerminalForm.h HEADERS += FrequencyTuningForm.h HEADERS += SignalGeneratorForm.h HEADERS += TimeSwitcherForm.h HEADERS += FreqSwitcherForm.h -FORMS += DeviceHubWindow.ui +FORMS += DeviceHubWindow.ui \ + BCodeTerminalForm.ui FORMS += FrequencyTuningForm.ui FORMS += SignalGeneratorForm.ui FORMS += TimeSwitcherForm.ui diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 8050848..d2b0ac2 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -41,6 +41,9 @@ freqSwitForm = new FreqSwitcherForm(this); ui->stackedWidget->addWidget(freqSwitForm); + bCodeTermForm = new BCodeTerminalForm(this); + ui->stackedWidget->addWidget(bCodeTermForm); + // kafka consumer kafkaConsumer = new QKafkaConsumer(this); kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index bdc0749..9e2700e 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -11,6 +11,7 @@ #include "SignalGeneratorForm.h" #include "TimeSwitcherForm.h" #include "FreqSwitcherForm.h" +#include "BCodeTerminalForm.h" QT_BEGIN_NAMESPACE namespace Ui { class DeviceHubWindow; } @@ -38,6 +39,7 @@ SignalGeneratorForm * signGenForm; TimeSwitcherForm * tmSwitForm; FreqSwitcherForm * freqSwitForm; + BCodeTerminalForm * bCodeTermForm; private slots: void on_minButt_clicked(); diff --git a/DeviceHub/device/BCodeTerminal.cpp b/DeviceHub/device/BCodeTerminal.cpp index 19d33fd..b792982 100644 --- a/DeviceHub/device/BCodeTerminal.cpp +++ b/DeviceHub/device/BCodeTerminal.cpp @@ -1,18 +1,27 @@ #include "BCodeTerminal.h" - +#include "DeviceHubWindow.h" #include #include BCodeTerminal::BCodeTerminal(QObject* parent) : DeviceBase(parent) { + this->devType = "07"; connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &BCodeTerminal::dataReceivedHandler); - kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); - kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); - kafkaUtil.createProducer(); + connect(this, &BCodeTerminal::sendDataToDraw, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::drawDeviceFrameOnForm); + connect(this, &BCodeTerminal::sendCommandToDisplay, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::displayDeviceCommandOnForm); - this->protocol = DeviceStatusProtocolBase::deviceStatusProtocolFactory(typeid (this).name()); + connect(((DeviceHubWindow *)this->parent())->kafkaConsumer, &QKafkaConsumer::messageRecieved, + this, &BCodeTerminal::commandReceivedHandler); + + kafkaProducer.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaProducer.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaProducer.createProducer(); + + this->protocol = DeviceProtocolBase::deviceProtocolFactory(devType); } BCodeTerminal::~BCodeTerminal() @@ -21,6 +30,17 @@ this, &BCodeTerminal::dataReceivedHandler); } +void BCodeTerminal::mockReceivData() +{ + QByteArray buffer; + + // b-code terminal + buffer.append("$2621304-20 210100112100f90210.035422*"); + buffer.append("$3e21304-20 2101001111304-20 2101001210801H.1.00S.1.030008432*"); + + this->dataReceivedHandler(buffer); +} + void BCodeTerminal::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); @@ -73,6 +93,60 @@ 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))); } } + +void BCodeTerminal::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 BCodeTerminal::commandReceivedHandler(QJsonObject command) +{ + if (command.contains("deviceType") == false || command.value("deviceType").toString() != "07") + { + 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/BCodeTerminal.h b/DeviceHub/device/BCodeTerminal.h index b3aa5cb..f39ab6e 100644 --- a/DeviceHub/device/BCodeTerminal.h +++ b/DeviceHub/device/BCodeTerminal.h @@ -4,6 +4,7 @@ #include #include "device/DeviceBase.h" +#include "protocol/dto/BCodeTerminalStatusDto.h" class BCodeTerminal : public DeviceBase { @@ -12,13 +13,18 @@ explicit BCodeTerminal(QObject *parent = nullptr); ~BCodeTerminal(); + void mockReceivData() override; + void afterFramePhase(DeviceFrameBaseDto * frameDto); + void sendDataToSerial(QByteArray data) override; signals: void sendDataToDraw(DeviceFrameBaseDto * frameData); + void sendCommandToDisplay(QJsonObject command); public slots: void dataReceivedHandler(QByteArray data); + void commandReceivedHandler(QJsonObject command); }; #endif // BCODETERMINAL_H diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index 2d9d345..f1c6c59 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -3,6 +3,7 @@ #include "SignalGenerator.h" #include "TimeSwitcher.h" #include "FreqSwitcher.h" +#include "BCodeTerminal.h" #include @@ -51,6 +52,9 @@ } else if (deviceType == "06") { base = new FreqSwitcher(parent); + } else if (deviceType == "07") + { + base = new BCodeTerminal(parent); } return base; diff --git a/DeviceHub/device/device.pri b/DeviceHub/device/device.pri index 5ad8604..5c6c099 100644 --- a/DeviceHub/device/device.pri +++ b/DeviceHub/device/device.pri @@ -6,7 +6,7 @@ HEADERS += $$PWD/FreqSwitcher.h #HEADERS += $$PWD/TimeReplicator.h #HEADERS += $$PWD/FreqReplicator.h -#HEADERS += $$PWD/BCodeTerminal.h +HEADERS += $$PWD/BCodeTerminal.h SOURCES += $$PWD/DeviceBase.cpp SOURCES += $$PWD/FrequencyTuning.cpp @@ -15,4 +15,4 @@ SOURCES += $$PWD/FreqSwitcher.cpp #SOURCES += $$PWD/TimeReplicator.cpp #SOURCES += $$PWD/FreqReplicator.cpp -#SOURCES += $$PWD/BCodeTerminal.cpp +SOURCES += $$PWD/BCodeTerminal.cpp diff --git a/DeviceHub/protocol/BCodeTerminalProtocolBM.cpp b/DeviceHub/protocol/BCodeTerminalProtocolBM.cpp new file mode 100644 index 0000000..1dfe733 --- /dev/null +++ b/DeviceHub/protocol/BCodeTerminalProtocolBM.cpp @@ -0,0 +1,107 @@ +#include "BCodeTerminalProtocolBM.h" + +BCodeTerminalProtocolBM::BCodeTerminalProtocolBM(QObject* parent) : DeviceProtocolBase(parent) +{ + +} + +BCodeTerminalProtocolBM::~BCodeTerminalProtocolBM() +{ + +} + +// 解析数据 +bool BCodeTerminalProtocolBM::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); + int outBdcByte = 0; + int outBacByte = 0; + QString outStr = ""; + + ((BCodeTerminalStatusDto *) frameData)->devStatus = content.at(0); + ((BCodeTerminalStatusDto *) frameData)->bdcInStatus = content.at(2); + ((BCodeTerminalStatusDto *) frameData)->bacInStatus = content.at(3); + ((BCodeTerminalStatusDto *) frameData)->fiveInStatus = content.at(4); + + outBdcByte = content.mid(6, 1).toInt(0, 16); + outBacByte = content.mid(5, 1).toInt(0, 16); + for (int i = 0; i < 4; i++) + { + ((BCodeTerminalStatusDto *) frameData)->bdcOutStatusList.append(QString("%1").arg(outBdcByte % 2)); + ((BCodeTerminalStatusDto *) frameData)->bacOutStatusList.append(QString("%1").arg(outBacByte % 2)); + + outBdcByte = outBdcByte / 2; + outBacByte = outBacByte / 2; + } + + ((BCodeTerminalStatusDto *) frameData)->ref = content.at(7); + ((BCodeTerminalStatusDto *) frameData)->track = content.at(8); + ((BCodeTerminalStatusDto *) frameData)->bacRange = content.mid(9, 4).toFloat(); + ((BCodeTerminalStatusDto *) frameData)->bacRatio = content.mid(13, 1).toInt(); + + ((BCodeTerminalStatusDto *)frameData)->frameType = B_CODE_TERMINAL_STATUS_FRAME_TYPE; + + return true; +} + +QByteArray BCodeTerminalProtocolBM::generateSettingCommand(QString commandType, QString valueSet) +{ + QByteArray commandBytes; + + commandBytes.append("3"); + commandBytes.append("1304-20").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; +} + +DeviceFrameBaseDto * BCodeTerminalProtocolBM::frameFactory(int frameType) +{ + DeviceFrameBaseDto * frameData = new BCodeTerminalStatusDto(); + + return frameData; +} + +// +QList BCodeTerminalProtocolBM::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; +} + +// 检测帧格式,帧头帧尾 +int BCodeTerminalProtocolBM::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; +} diff --git a/DeviceHub/protocol/BCodeTerminalProtocolBM.h b/DeviceHub/protocol/BCodeTerminalProtocolBM.h new file mode 100644 index 0000000..dcd5212 --- /dev/null +++ b/DeviceHub/protocol/BCodeTerminalProtocolBM.h @@ -0,0 +1,29 @@ +#ifndef BCODETERMINALPROTOCOLBM_H +#define BCODETERMINALPROTOCOLBM_H + +#include + +#include "common/utils/QByteUtil.h" +#include "DeviceProtocolBase.h" +#include "dto/BCodeTerminalStatusDto.h" + +class BCodeTerminalProtocolBM : public DeviceProtocolBase +{ + Q_OBJECT +public: + explicit BCodeTerminalProtocolBM(QObject *parent = nullptr); + ~BCodeTerminalProtocolBM(); + + // 解析数据 + 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 // BCODETERMINALPROTOCOLBM_H diff --git a/DeviceHub/protocol/DeviceProtocolBase.cpp b/DeviceHub/protocol/DeviceProtocolBase.cpp index c7d5238..9c46989 100644 --- a/DeviceHub/protocol/DeviceProtocolBase.cpp +++ b/DeviceHub/protocol/DeviceProtocolBase.cpp @@ -5,7 +5,7 @@ #include "FreqSwitcherProtocolBM.h" //#include "TimeReplicatorProtocolBM.h" //#include "FreqReplicatorProtocolTX.h" -//#include "BCodeTerminalProtocolBM.h" +#include "BCodeTerminalProtocolBM.h" #include @@ -28,6 +28,9 @@ } else if (deviceType == "06") { return new FreqSwitcherProtocolBM(); + } else if (deviceType == "07") + { + return new BCodeTerminalProtocolBM(); } // if (deviceType.contains("SignalGenerator") == true) @@ -84,3 +87,21 @@ str.sprintf("%02X", xorValue); return str; } + +QString DeviceProtocolBase::buildCRC16(QByteArray byteArray) +{ + qint16 crc = 0; + for (int i = 0; i < byteArray.size(); i++) { + crc = crc ^ byteArray[i] << 8; + for (int j = 0; j < 8; j++) { + if ((crc & 0x8000) > 0) { + crc = crc << 1 ^ 0x1021; + } else { + crc = crc << 1; + } + } + } + + QByteArray crcBytes = QByteUtil::ULongToBytes(crc, 2); + return QByteUtil::binToHexString(crcBytes); +} diff --git a/DeviceHub/protocol/DeviceProtocolBase.h b/DeviceHub/protocol/DeviceProtocolBase.h index 8946995..396bda6 100644 --- a/DeviceHub/protocol/DeviceProtocolBase.h +++ b/DeviceHub/protocol/DeviceProtocolBase.h @@ -110,6 +110,7 @@ static DeviceProtocolBase * deviceProtocolFactory(QString deviceType); QString calculateXOR(QByteArray byteArray); + QString buildCRC16(QByteArray byteArray); virtual DeviceFrameBaseDto * frameFactory(int frameType) = 0; virtual int checkFrame(QByteArray rawData) = 0; diff --git a/DeviceHub/protocol/dto/BCodeTerminalStatusDto.cpp b/DeviceHub/protocol/dto/BCodeTerminalStatusDto.cpp new file mode 100644 index 0000000..44c3c38 --- /dev/null +++ b/DeviceHub/protocol/dto/BCodeTerminalStatusDto.cpp @@ -0,0 +1,37 @@ +#include "BCodeTerminalStatusDto.h" + +BCodeTerminalStatusDto::BCodeTerminalStatusDto(QObject* parent) : DeviceFrameBaseDto(parent) +{ + +} + +QJsonObject BCodeTerminalStatusDto::toJSON() +{ + QJsonObject jsonObj; + + QJsonObject dataObj; + dataObj.insert("devStatus", this->devStatus); + dataObj.insert("bdcInStatus", bdcInStatus); + dataObj.insert("bacInStatus", bacInStatus); + dataObj.insert("fiveInStatus", fiveInStatus); + dataObj.insert("ref", ref); + dataObj.insert("track", track); + dataObj.insert("bacRange", bacRange); + dataObj.insert("bacRatio", bacRatio); + + QJsonArray bdcOutStatusArray; + QJsonArray bacOutStatusArray; + for (int i = 0; i < 4; i++) + { + bdcOutStatusArray.append(bdcOutStatusList.at(i)); + bacOutStatusArray.append(bacOutStatusList.at(i)); + } + dataObj.insert("bdcOutStatusArray", bdcOutStatusArray); + dataObj.insert("bacOutStatusArray", bacOutStatusArray); + + jsonObj.insert("ts", this->milisecond); + jsonObj.insert("frameType", this->frameType); + jsonObj.insert("data", dataObj); + + return jsonObj; +} diff --git a/DeviceHub/BCodeTerminalForm.cpp b/DeviceHub/BCodeTerminalForm.cpp new file mode 100644 index 0000000..2c8bc26 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.cpp @@ -0,0 +1,63 @@ +#include "BCodeTerminalForm.h" +#include "ui_BCodeTerminalForm.h" +#include "DeviceHubWindow.h" + +BCodeTerminalForm::BCodeTerminalForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::BCodeTerminalForm) +{ + ui->setupUi(this); +} + +BCodeTerminalForm::~BCodeTerminalForm() +{ + delete ui; +} + +void BCodeTerminalForm::on_bctButt_clicked() +{ + // ������������ + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + BCodeTerminal * device = (BCodeTerminal *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("07").at(devIndex); + device->mockReceivData(); +} + +void BCodeTerminalForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // ������������������ + if (frameData->frameType == "0701") + { + BCodeTerminalStatusDto * statusFrameDto = (BCodeTerminalStatusDto *) frameData; + + } +} + +void BCodeTerminalForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("04"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "04") + { + ((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/BCodeTerminalForm.h b/DeviceHub/BCodeTerminalForm.h new file mode 100644 index 0000000..0ae7601 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.h @@ -0,0 +1,30 @@ +#ifndef BCODETERMINALFORM_H +#define BCODETERMINALFORM_H + +#include +#include "device/BCodeTerminal.h" + +namespace Ui { +class BCodeTerminalForm; +} + +class BCodeTerminalForm : public QWidget +{ + Q_OBJECT + +public: + explicit BCodeTerminalForm(QWidget *parent = nullptr); + ~BCodeTerminalForm(); + +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + +private slots: + void on_bctButt_clicked(); + +private: + Ui::BCodeTerminalForm *ui; +}; + +#endif // BCODETERMINALFORM_H diff --git a/DeviceHub/BCodeTerminalForm.ui b/DeviceHub/BCodeTerminalForm.ui new file mode 100644 index 0000000..9504c94 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.ui @@ -0,0 +1,34 @@ + + + + + BCodeTerminalForm + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + 50 + 20 + 180 + 40 + + + + Mock BCodeTerminal + + + + + + diff --git a/DeviceHub/DeviceHub.pro b/DeviceHub/DeviceHub.pro index 3f17e33..6dc7b34 100644 --- a/DeviceHub/DeviceHub.pro +++ b/DeviceHub/DeviceHub.pro @@ -15,20 +15,23 @@ # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 -SOURCES += main.cpp +SOURCES += main.cpp \ + BCodeTerminalForm.cpp SOURCES += DeviceHubWindow.cpp SOURCES += FrequencyTuningForm.cpp SOURCES += SignalGeneratorForm.cpp SOURCES += TimeSwitcherForm.cpp SOURCES += FreqSwitcherForm.cpp -HEADERS += DeviceHubWindow.h +HEADERS += DeviceHubWindow.h \ + BCodeTerminalForm.h HEADERS += FrequencyTuningForm.h HEADERS += SignalGeneratorForm.h HEADERS += TimeSwitcherForm.h HEADERS += FreqSwitcherForm.h -FORMS += DeviceHubWindow.ui +FORMS += DeviceHubWindow.ui \ + BCodeTerminalForm.ui FORMS += FrequencyTuningForm.ui FORMS += SignalGeneratorForm.ui FORMS += TimeSwitcherForm.ui diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 8050848..d2b0ac2 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -41,6 +41,9 @@ freqSwitForm = new FreqSwitcherForm(this); ui->stackedWidget->addWidget(freqSwitForm); + bCodeTermForm = new BCodeTerminalForm(this); + ui->stackedWidget->addWidget(bCodeTermForm); + // kafka consumer kafkaConsumer = new QKafkaConsumer(this); kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index bdc0749..9e2700e 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -11,6 +11,7 @@ #include "SignalGeneratorForm.h" #include "TimeSwitcherForm.h" #include "FreqSwitcherForm.h" +#include "BCodeTerminalForm.h" QT_BEGIN_NAMESPACE namespace Ui { class DeviceHubWindow; } @@ -38,6 +39,7 @@ SignalGeneratorForm * signGenForm; TimeSwitcherForm * tmSwitForm; FreqSwitcherForm * freqSwitForm; + BCodeTerminalForm * bCodeTermForm; private slots: void on_minButt_clicked(); diff --git a/DeviceHub/device/BCodeTerminal.cpp b/DeviceHub/device/BCodeTerminal.cpp index 19d33fd..b792982 100644 --- a/DeviceHub/device/BCodeTerminal.cpp +++ b/DeviceHub/device/BCodeTerminal.cpp @@ -1,18 +1,27 @@ #include "BCodeTerminal.h" - +#include "DeviceHubWindow.h" #include #include BCodeTerminal::BCodeTerminal(QObject* parent) : DeviceBase(parent) { + this->devType = "07"; connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &BCodeTerminal::dataReceivedHandler); - kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); - kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); - kafkaUtil.createProducer(); + connect(this, &BCodeTerminal::sendDataToDraw, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::drawDeviceFrameOnForm); + connect(this, &BCodeTerminal::sendCommandToDisplay, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::displayDeviceCommandOnForm); - this->protocol = DeviceStatusProtocolBase::deviceStatusProtocolFactory(typeid (this).name()); + connect(((DeviceHubWindow *)this->parent())->kafkaConsumer, &QKafkaConsumer::messageRecieved, + this, &BCodeTerminal::commandReceivedHandler); + + kafkaProducer.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaProducer.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaProducer.createProducer(); + + this->protocol = DeviceProtocolBase::deviceProtocolFactory(devType); } BCodeTerminal::~BCodeTerminal() @@ -21,6 +30,17 @@ this, &BCodeTerminal::dataReceivedHandler); } +void BCodeTerminal::mockReceivData() +{ + QByteArray buffer; + + // b-code terminal + buffer.append("$2621304-20 210100112100f90210.035422*"); + buffer.append("$3e21304-20 2101001111304-20 2101001210801H.1.00S.1.030008432*"); + + this->dataReceivedHandler(buffer); +} + void BCodeTerminal::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); @@ -73,6 +93,60 @@ 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))); } } + +void BCodeTerminal::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 BCodeTerminal::commandReceivedHandler(QJsonObject command) +{ + if (command.contains("deviceType") == false || command.value("deviceType").toString() != "07") + { + 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/BCodeTerminal.h b/DeviceHub/device/BCodeTerminal.h index b3aa5cb..f39ab6e 100644 --- a/DeviceHub/device/BCodeTerminal.h +++ b/DeviceHub/device/BCodeTerminal.h @@ -4,6 +4,7 @@ #include #include "device/DeviceBase.h" +#include "protocol/dto/BCodeTerminalStatusDto.h" class BCodeTerminal : public DeviceBase { @@ -12,13 +13,18 @@ explicit BCodeTerminal(QObject *parent = nullptr); ~BCodeTerminal(); + void mockReceivData() override; + void afterFramePhase(DeviceFrameBaseDto * frameDto); + void sendDataToSerial(QByteArray data) override; signals: void sendDataToDraw(DeviceFrameBaseDto * frameData); + void sendCommandToDisplay(QJsonObject command); public slots: void dataReceivedHandler(QByteArray data); + void commandReceivedHandler(QJsonObject command); }; #endif // BCODETERMINAL_H diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index 2d9d345..f1c6c59 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -3,6 +3,7 @@ #include "SignalGenerator.h" #include "TimeSwitcher.h" #include "FreqSwitcher.h" +#include "BCodeTerminal.h" #include @@ -51,6 +52,9 @@ } else if (deviceType == "06") { base = new FreqSwitcher(parent); + } else if (deviceType == "07") + { + base = new BCodeTerminal(parent); } return base; diff --git a/DeviceHub/device/device.pri b/DeviceHub/device/device.pri index 5ad8604..5c6c099 100644 --- a/DeviceHub/device/device.pri +++ b/DeviceHub/device/device.pri @@ -6,7 +6,7 @@ HEADERS += $$PWD/FreqSwitcher.h #HEADERS += $$PWD/TimeReplicator.h #HEADERS += $$PWD/FreqReplicator.h -#HEADERS += $$PWD/BCodeTerminal.h +HEADERS += $$PWD/BCodeTerminal.h SOURCES += $$PWD/DeviceBase.cpp SOURCES += $$PWD/FrequencyTuning.cpp @@ -15,4 +15,4 @@ SOURCES += $$PWD/FreqSwitcher.cpp #SOURCES += $$PWD/TimeReplicator.cpp #SOURCES += $$PWD/FreqReplicator.cpp -#SOURCES += $$PWD/BCodeTerminal.cpp +SOURCES += $$PWD/BCodeTerminal.cpp diff --git a/DeviceHub/protocol/BCodeTerminalProtocolBM.cpp b/DeviceHub/protocol/BCodeTerminalProtocolBM.cpp new file mode 100644 index 0000000..1dfe733 --- /dev/null +++ b/DeviceHub/protocol/BCodeTerminalProtocolBM.cpp @@ -0,0 +1,107 @@ +#include "BCodeTerminalProtocolBM.h" + +BCodeTerminalProtocolBM::BCodeTerminalProtocolBM(QObject* parent) : DeviceProtocolBase(parent) +{ + +} + +BCodeTerminalProtocolBM::~BCodeTerminalProtocolBM() +{ + +} + +// 解析数据 +bool BCodeTerminalProtocolBM::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); + int outBdcByte = 0; + int outBacByte = 0; + QString outStr = ""; + + ((BCodeTerminalStatusDto *) frameData)->devStatus = content.at(0); + ((BCodeTerminalStatusDto *) frameData)->bdcInStatus = content.at(2); + ((BCodeTerminalStatusDto *) frameData)->bacInStatus = content.at(3); + ((BCodeTerminalStatusDto *) frameData)->fiveInStatus = content.at(4); + + outBdcByte = content.mid(6, 1).toInt(0, 16); + outBacByte = content.mid(5, 1).toInt(0, 16); + for (int i = 0; i < 4; i++) + { + ((BCodeTerminalStatusDto *) frameData)->bdcOutStatusList.append(QString("%1").arg(outBdcByte % 2)); + ((BCodeTerminalStatusDto *) frameData)->bacOutStatusList.append(QString("%1").arg(outBacByte % 2)); + + outBdcByte = outBdcByte / 2; + outBacByte = outBacByte / 2; + } + + ((BCodeTerminalStatusDto *) frameData)->ref = content.at(7); + ((BCodeTerminalStatusDto *) frameData)->track = content.at(8); + ((BCodeTerminalStatusDto *) frameData)->bacRange = content.mid(9, 4).toFloat(); + ((BCodeTerminalStatusDto *) frameData)->bacRatio = content.mid(13, 1).toInt(); + + ((BCodeTerminalStatusDto *)frameData)->frameType = B_CODE_TERMINAL_STATUS_FRAME_TYPE; + + return true; +} + +QByteArray BCodeTerminalProtocolBM::generateSettingCommand(QString commandType, QString valueSet) +{ + QByteArray commandBytes; + + commandBytes.append("3"); + commandBytes.append("1304-20").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; +} + +DeviceFrameBaseDto * BCodeTerminalProtocolBM::frameFactory(int frameType) +{ + DeviceFrameBaseDto * frameData = new BCodeTerminalStatusDto(); + + return frameData; +} + +// +QList BCodeTerminalProtocolBM::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; +} + +// 检测帧格式,帧头帧尾 +int BCodeTerminalProtocolBM::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; +} diff --git a/DeviceHub/protocol/BCodeTerminalProtocolBM.h b/DeviceHub/protocol/BCodeTerminalProtocolBM.h new file mode 100644 index 0000000..dcd5212 --- /dev/null +++ b/DeviceHub/protocol/BCodeTerminalProtocolBM.h @@ -0,0 +1,29 @@ +#ifndef BCODETERMINALPROTOCOLBM_H +#define BCODETERMINALPROTOCOLBM_H + +#include + +#include "common/utils/QByteUtil.h" +#include "DeviceProtocolBase.h" +#include "dto/BCodeTerminalStatusDto.h" + +class BCodeTerminalProtocolBM : public DeviceProtocolBase +{ + Q_OBJECT +public: + explicit BCodeTerminalProtocolBM(QObject *parent = nullptr); + ~BCodeTerminalProtocolBM(); + + // 解析数据 + 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 // BCODETERMINALPROTOCOLBM_H diff --git a/DeviceHub/protocol/DeviceProtocolBase.cpp b/DeviceHub/protocol/DeviceProtocolBase.cpp index c7d5238..9c46989 100644 --- a/DeviceHub/protocol/DeviceProtocolBase.cpp +++ b/DeviceHub/protocol/DeviceProtocolBase.cpp @@ -5,7 +5,7 @@ #include "FreqSwitcherProtocolBM.h" //#include "TimeReplicatorProtocolBM.h" //#include "FreqReplicatorProtocolTX.h" -//#include "BCodeTerminalProtocolBM.h" +#include "BCodeTerminalProtocolBM.h" #include @@ -28,6 +28,9 @@ } else if (deviceType == "06") { return new FreqSwitcherProtocolBM(); + } else if (deviceType == "07") + { + return new BCodeTerminalProtocolBM(); } // if (deviceType.contains("SignalGenerator") == true) @@ -84,3 +87,21 @@ str.sprintf("%02X", xorValue); return str; } + +QString DeviceProtocolBase::buildCRC16(QByteArray byteArray) +{ + qint16 crc = 0; + for (int i = 0; i < byteArray.size(); i++) { + crc = crc ^ byteArray[i] << 8; + for (int j = 0; j < 8; j++) { + if ((crc & 0x8000) > 0) { + crc = crc << 1 ^ 0x1021; + } else { + crc = crc << 1; + } + } + } + + QByteArray crcBytes = QByteUtil::ULongToBytes(crc, 2); + return QByteUtil::binToHexString(crcBytes); +} diff --git a/DeviceHub/protocol/DeviceProtocolBase.h b/DeviceHub/protocol/DeviceProtocolBase.h index 8946995..396bda6 100644 --- a/DeviceHub/protocol/DeviceProtocolBase.h +++ b/DeviceHub/protocol/DeviceProtocolBase.h @@ -110,6 +110,7 @@ static DeviceProtocolBase * deviceProtocolFactory(QString deviceType); QString calculateXOR(QByteArray byteArray); + QString buildCRC16(QByteArray byteArray); virtual DeviceFrameBaseDto * frameFactory(int frameType) = 0; virtual int checkFrame(QByteArray rawData) = 0; diff --git a/DeviceHub/protocol/dto/BCodeTerminalStatusDto.cpp b/DeviceHub/protocol/dto/BCodeTerminalStatusDto.cpp new file mode 100644 index 0000000..44c3c38 --- /dev/null +++ b/DeviceHub/protocol/dto/BCodeTerminalStatusDto.cpp @@ -0,0 +1,37 @@ +#include "BCodeTerminalStatusDto.h" + +BCodeTerminalStatusDto::BCodeTerminalStatusDto(QObject* parent) : DeviceFrameBaseDto(parent) +{ + +} + +QJsonObject BCodeTerminalStatusDto::toJSON() +{ + QJsonObject jsonObj; + + QJsonObject dataObj; + dataObj.insert("devStatus", this->devStatus); + dataObj.insert("bdcInStatus", bdcInStatus); + dataObj.insert("bacInStatus", bacInStatus); + dataObj.insert("fiveInStatus", fiveInStatus); + dataObj.insert("ref", ref); + dataObj.insert("track", track); + dataObj.insert("bacRange", bacRange); + dataObj.insert("bacRatio", bacRatio); + + QJsonArray bdcOutStatusArray; + QJsonArray bacOutStatusArray; + for (int i = 0; i < 4; i++) + { + bdcOutStatusArray.append(bdcOutStatusList.at(i)); + bacOutStatusArray.append(bacOutStatusList.at(i)); + } + dataObj.insert("bdcOutStatusArray", bdcOutStatusArray); + dataObj.insert("bacOutStatusArray", bacOutStatusArray); + + jsonObj.insert("ts", this->milisecond); + jsonObj.insert("frameType", this->frameType); + jsonObj.insert("data", dataObj); + + return jsonObj; +} diff --git a/DeviceHub/protocol/dto/BCodeTerminalStatusDto.h b/DeviceHub/protocol/dto/BCodeTerminalStatusDto.h new file mode 100644 index 0000000..db54b55 --- /dev/null +++ b/DeviceHub/protocol/dto/BCodeTerminalStatusDto.h @@ -0,0 +1,28 @@ +#ifndef BCODETERMINALSTATUSDTO_H +#define BCODETERMINALSTATUSDTO_H + +#include + +#include "DeviceFrameBaseDto.h" + +class BCodeTerminalStatusDto : public DeviceFrameBaseDto +{ + Q_OBJECT +public: + explicit BCodeTerminalStatusDto(QObject * parent = nullptr); + + QString devStatus; + QString bdcInStatus; + QString bacInStatus; + QString fiveInStatus; + QList bdcOutStatusList; + QList bacOutStatusList; + QString ref; + QString track; + float bacRange; + qint8 bacRatio; + + QJsonObject toJSON() override; +}; + +#endif // BCODETERMINALSTATUSDTO_H diff --git a/DeviceHub/BCodeTerminalForm.cpp b/DeviceHub/BCodeTerminalForm.cpp new file mode 100644 index 0000000..2c8bc26 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.cpp @@ -0,0 +1,63 @@ +#include "BCodeTerminalForm.h" +#include "ui_BCodeTerminalForm.h" +#include "DeviceHubWindow.h" + +BCodeTerminalForm::BCodeTerminalForm(QWidget *parent) : + QWidget(parent), + ui(new Ui::BCodeTerminalForm) +{ + ui->setupUi(this); +} + +BCodeTerminalForm::~BCodeTerminalForm() +{ + delete ui; +} + +void BCodeTerminalForm::on_bctButt_clicked() +{ + // ������������ + int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; + BCodeTerminal * device = (BCodeTerminal *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("07").at(devIndex); + device->mockReceivData(); +} + +void BCodeTerminalForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) +{ + // ������������������ + if (frameData->frameType == "0701") + { + BCodeTerminalStatusDto * statusFrameDto = (BCodeTerminalStatusDto *) frameData; + + } +} + +void BCodeTerminalForm::displayDeviceCommandOnForm(QJsonObject command) +{ + QString deviceId = command.value("deviceId").toString(); + QList typeDevList = ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("04"); + for (int i = 0; i < ((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->count(); i++) + { + if (((DeviceHubWindow *)this->parent()->parent())->getDevTypeSelect()->itemData(i) == "04") + { + ((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/BCodeTerminalForm.h b/DeviceHub/BCodeTerminalForm.h new file mode 100644 index 0000000..0ae7601 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.h @@ -0,0 +1,30 @@ +#ifndef BCODETERMINALFORM_H +#define BCODETERMINALFORM_H + +#include +#include "device/BCodeTerminal.h" + +namespace Ui { +class BCodeTerminalForm; +} + +class BCodeTerminalForm : public QWidget +{ + Q_OBJECT + +public: + explicit BCodeTerminalForm(QWidget *parent = nullptr); + ~BCodeTerminalForm(); + +public slots: + void drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData); + void displayDeviceCommandOnForm(QJsonObject command); + +private slots: + void on_bctButt_clicked(); + +private: + Ui::BCodeTerminalForm *ui; +}; + +#endif // BCODETERMINALFORM_H diff --git a/DeviceHub/BCodeTerminalForm.ui b/DeviceHub/BCodeTerminalForm.ui new file mode 100644 index 0000000..9504c94 --- /dev/null +++ b/DeviceHub/BCodeTerminalForm.ui @@ -0,0 +1,34 @@ + + + + + BCodeTerminalForm + + + + 0 + 0 + 400 + 300 + + + + Form + + + + + 50 + 20 + 180 + 40 + + + + Mock BCodeTerminal + + + + + + diff --git a/DeviceHub/DeviceHub.pro b/DeviceHub/DeviceHub.pro index 3f17e33..6dc7b34 100644 --- a/DeviceHub/DeviceHub.pro +++ b/DeviceHub/DeviceHub.pro @@ -15,20 +15,23 @@ # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 -SOURCES += main.cpp +SOURCES += main.cpp \ + BCodeTerminalForm.cpp SOURCES += DeviceHubWindow.cpp SOURCES += FrequencyTuningForm.cpp SOURCES += SignalGeneratorForm.cpp SOURCES += TimeSwitcherForm.cpp SOURCES += FreqSwitcherForm.cpp -HEADERS += DeviceHubWindow.h +HEADERS += DeviceHubWindow.h \ + BCodeTerminalForm.h HEADERS += FrequencyTuningForm.h HEADERS += SignalGeneratorForm.h HEADERS += TimeSwitcherForm.h HEADERS += FreqSwitcherForm.h -FORMS += DeviceHubWindow.ui +FORMS += DeviceHubWindow.ui \ + BCodeTerminalForm.ui FORMS += FrequencyTuningForm.ui FORMS += SignalGeneratorForm.ui FORMS += TimeSwitcherForm.ui diff --git a/DeviceHub/DeviceHubWindow.cpp b/DeviceHub/DeviceHubWindow.cpp index 8050848..d2b0ac2 100644 --- a/DeviceHub/DeviceHubWindow.cpp +++ b/DeviceHub/DeviceHubWindow.cpp @@ -41,6 +41,9 @@ freqSwitForm = new FreqSwitcherForm(this); ui->stackedWidget->addWidget(freqSwitForm); + bCodeTermForm = new BCodeTerminalForm(this); + ui->stackedWidget->addWidget(bCodeTermForm); + // kafka consumer kafkaConsumer = new QKafkaConsumer(this); kafkaConsumer->setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); diff --git a/DeviceHub/DeviceHubWindow.h b/DeviceHub/DeviceHubWindow.h index bdc0749..9e2700e 100644 --- a/DeviceHub/DeviceHubWindow.h +++ b/DeviceHub/DeviceHubWindow.h @@ -11,6 +11,7 @@ #include "SignalGeneratorForm.h" #include "TimeSwitcherForm.h" #include "FreqSwitcherForm.h" +#include "BCodeTerminalForm.h" QT_BEGIN_NAMESPACE namespace Ui { class DeviceHubWindow; } @@ -38,6 +39,7 @@ SignalGeneratorForm * signGenForm; TimeSwitcherForm * tmSwitForm; FreqSwitcherForm * freqSwitForm; + BCodeTerminalForm * bCodeTermForm; private slots: void on_minButt_clicked(); diff --git a/DeviceHub/device/BCodeTerminal.cpp b/DeviceHub/device/BCodeTerminal.cpp index 19d33fd..b792982 100644 --- a/DeviceHub/device/BCodeTerminal.cpp +++ b/DeviceHub/device/BCodeTerminal.cpp @@ -1,18 +1,27 @@ #include "BCodeTerminal.h" - +#include "DeviceHubWindow.h" #include #include BCodeTerminal::BCodeTerminal(QObject* parent) : DeviceBase(parent) { + this->devType = "07"; connect(&this->serialUtil, &QSerialPortUtil::dataRecieved, this, &BCodeTerminal::dataReceivedHandler); - kafkaUtil.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); - kafkaUtil.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); - kafkaUtil.createProducer(); + connect(this, &BCodeTerminal::sendDataToDraw, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::drawDeviceFrameOnForm); + connect(this, &BCodeTerminal::sendCommandToDisplay, + ((DeviceHubWindow *)this->parent())->bCodeTermForm, &BCodeTerminalForm::displayDeviceCommandOnForm); - this->protocol = DeviceStatusProtocolBase::deviceStatusProtocolFactory(typeid (this).name()); + connect(((DeviceHubWindow *)this->parent())->kafkaConsumer, &QKafkaConsumer::messageRecieved, + this, &BCodeTerminal::commandReceivedHandler); + + kafkaProducer.setBrokers(SettingConfig::getInstance().KAFKA_BROKERS); + kafkaProducer.setTopic(SettingConfig::getInstance().KAFKA_DATA_TOPIC); + kafkaProducer.createProducer(); + + this->protocol = DeviceProtocolBase::deviceProtocolFactory(devType); } BCodeTerminal::~BCodeTerminal() @@ -21,6 +30,17 @@ this, &BCodeTerminal::dataReceivedHandler); } +void BCodeTerminal::mockReceivData() +{ + QByteArray buffer; + + // b-code terminal + buffer.append("$2621304-20 210100112100f90210.035422*"); + buffer.append("$3e21304-20 2101001111304-20 2101001210801H.1.00S.1.030008432*"); + + this->dataReceivedHandler(buffer); +} + void BCodeTerminal::dataReceivedHandler(QByteArray data) { this->dataBuff.append(data); @@ -73,6 +93,60 @@ 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))); } } + +void BCodeTerminal::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 BCodeTerminal::commandReceivedHandler(QJsonObject command) +{ + if (command.contains("deviceType") == false || command.value("deviceType").toString() != "07") + { + 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/BCodeTerminal.h b/DeviceHub/device/BCodeTerminal.h index b3aa5cb..f39ab6e 100644 --- a/DeviceHub/device/BCodeTerminal.h +++ b/DeviceHub/device/BCodeTerminal.h @@ -4,6 +4,7 @@ #include #include "device/DeviceBase.h" +#include "protocol/dto/BCodeTerminalStatusDto.h" class BCodeTerminal : public DeviceBase { @@ -12,13 +13,18 @@ explicit BCodeTerminal(QObject *parent = nullptr); ~BCodeTerminal(); + void mockReceivData() override; + void afterFramePhase(DeviceFrameBaseDto * frameDto); + void sendDataToSerial(QByteArray data) override; signals: void sendDataToDraw(DeviceFrameBaseDto * frameData); + void sendCommandToDisplay(QJsonObject command); public slots: void dataReceivedHandler(QByteArray data); + void commandReceivedHandler(QJsonObject command); }; #endif // BCODETERMINAL_H diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index 2d9d345..f1c6c59 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -3,6 +3,7 @@ #include "SignalGenerator.h" #include "TimeSwitcher.h" #include "FreqSwitcher.h" +#include "BCodeTerminal.h" #include @@ -51,6 +52,9 @@ } else if (deviceType == "06") { base = new FreqSwitcher(parent); + } else if (deviceType == "07") + { + base = new BCodeTerminal(parent); } return base; diff --git a/DeviceHub/device/device.pri b/DeviceHub/device/device.pri index 5ad8604..5c6c099 100644 --- a/DeviceHub/device/device.pri +++ b/DeviceHub/device/device.pri @@ -6,7 +6,7 @@ HEADERS += $$PWD/FreqSwitcher.h #HEADERS += $$PWD/TimeReplicator.h #HEADERS += $$PWD/FreqReplicator.h -#HEADERS += $$PWD/BCodeTerminal.h +HEADERS += $$PWD/BCodeTerminal.h SOURCES += $$PWD/DeviceBase.cpp SOURCES += $$PWD/FrequencyTuning.cpp @@ -15,4 +15,4 @@ SOURCES += $$PWD/FreqSwitcher.cpp #SOURCES += $$PWD/TimeReplicator.cpp #SOURCES += $$PWD/FreqReplicator.cpp -#SOURCES += $$PWD/BCodeTerminal.cpp +SOURCES += $$PWD/BCodeTerminal.cpp diff --git a/DeviceHub/protocol/BCodeTerminalProtocolBM.cpp b/DeviceHub/protocol/BCodeTerminalProtocolBM.cpp new file mode 100644 index 0000000..1dfe733 --- /dev/null +++ b/DeviceHub/protocol/BCodeTerminalProtocolBM.cpp @@ -0,0 +1,107 @@ +#include "BCodeTerminalProtocolBM.h" + +BCodeTerminalProtocolBM::BCodeTerminalProtocolBM(QObject* parent) : DeviceProtocolBase(parent) +{ + +} + +BCodeTerminalProtocolBM::~BCodeTerminalProtocolBM() +{ + +} + +// 解析数据 +bool BCodeTerminalProtocolBM::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); + int outBdcByte = 0; + int outBacByte = 0; + QString outStr = ""; + + ((BCodeTerminalStatusDto *) frameData)->devStatus = content.at(0); + ((BCodeTerminalStatusDto *) frameData)->bdcInStatus = content.at(2); + ((BCodeTerminalStatusDto *) frameData)->bacInStatus = content.at(3); + ((BCodeTerminalStatusDto *) frameData)->fiveInStatus = content.at(4); + + outBdcByte = content.mid(6, 1).toInt(0, 16); + outBacByte = content.mid(5, 1).toInt(0, 16); + for (int i = 0; i < 4; i++) + { + ((BCodeTerminalStatusDto *) frameData)->bdcOutStatusList.append(QString("%1").arg(outBdcByte % 2)); + ((BCodeTerminalStatusDto *) frameData)->bacOutStatusList.append(QString("%1").arg(outBacByte % 2)); + + outBdcByte = outBdcByte / 2; + outBacByte = outBacByte / 2; + } + + ((BCodeTerminalStatusDto *) frameData)->ref = content.at(7); + ((BCodeTerminalStatusDto *) frameData)->track = content.at(8); + ((BCodeTerminalStatusDto *) frameData)->bacRange = content.mid(9, 4).toFloat(); + ((BCodeTerminalStatusDto *) frameData)->bacRatio = content.mid(13, 1).toInt(); + + ((BCodeTerminalStatusDto *)frameData)->frameType = B_CODE_TERMINAL_STATUS_FRAME_TYPE; + + return true; +} + +QByteArray BCodeTerminalProtocolBM::generateSettingCommand(QString commandType, QString valueSet) +{ + QByteArray commandBytes; + + commandBytes.append("3"); + commandBytes.append("1304-20").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; +} + +DeviceFrameBaseDto * BCodeTerminalProtocolBM::frameFactory(int frameType) +{ + DeviceFrameBaseDto * frameData = new BCodeTerminalStatusDto(); + + return frameData; +} + +// +QList BCodeTerminalProtocolBM::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; +} + +// 检测帧格式,帧头帧尾 +int BCodeTerminalProtocolBM::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; +} diff --git a/DeviceHub/protocol/BCodeTerminalProtocolBM.h b/DeviceHub/protocol/BCodeTerminalProtocolBM.h new file mode 100644 index 0000000..dcd5212 --- /dev/null +++ b/DeviceHub/protocol/BCodeTerminalProtocolBM.h @@ -0,0 +1,29 @@ +#ifndef BCODETERMINALPROTOCOLBM_H +#define BCODETERMINALPROTOCOLBM_H + +#include + +#include "common/utils/QByteUtil.h" +#include "DeviceProtocolBase.h" +#include "dto/BCodeTerminalStatusDto.h" + +class BCodeTerminalProtocolBM : public DeviceProtocolBase +{ + Q_OBJECT +public: + explicit BCodeTerminalProtocolBM(QObject *parent = nullptr); + ~BCodeTerminalProtocolBM(); + + // 解析数据 + 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 // BCODETERMINALPROTOCOLBM_H diff --git a/DeviceHub/protocol/DeviceProtocolBase.cpp b/DeviceHub/protocol/DeviceProtocolBase.cpp index c7d5238..9c46989 100644 --- a/DeviceHub/protocol/DeviceProtocolBase.cpp +++ b/DeviceHub/protocol/DeviceProtocolBase.cpp @@ -5,7 +5,7 @@ #include "FreqSwitcherProtocolBM.h" //#include "TimeReplicatorProtocolBM.h" //#include "FreqReplicatorProtocolTX.h" -//#include "BCodeTerminalProtocolBM.h" +#include "BCodeTerminalProtocolBM.h" #include @@ -28,6 +28,9 @@ } else if (deviceType == "06") { return new FreqSwitcherProtocolBM(); + } else if (deviceType == "07") + { + return new BCodeTerminalProtocolBM(); } // if (deviceType.contains("SignalGenerator") == true) @@ -84,3 +87,21 @@ str.sprintf("%02X", xorValue); return str; } + +QString DeviceProtocolBase::buildCRC16(QByteArray byteArray) +{ + qint16 crc = 0; + for (int i = 0; i < byteArray.size(); i++) { + crc = crc ^ byteArray[i] << 8; + for (int j = 0; j < 8; j++) { + if ((crc & 0x8000) > 0) { + crc = crc << 1 ^ 0x1021; + } else { + crc = crc << 1; + } + } + } + + QByteArray crcBytes = QByteUtil::ULongToBytes(crc, 2); + return QByteUtil::binToHexString(crcBytes); +} diff --git a/DeviceHub/protocol/DeviceProtocolBase.h b/DeviceHub/protocol/DeviceProtocolBase.h index 8946995..396bda6 100644 --- a/DeviceHub/protocol/DeviceProtocolBase.h +++ b/DeviceHub/protocol/DeviceProtocolBase.h @@ -110,6 +110,7 @@ static DeviceProtocolBase * deviceProtocolFactory(QString deviceType); QString calculateXOR(QByteArray byteArray); + QString buildCRC16(QByteArray byteArray); virtual DeviceFrameBaseDto * frameFactory(int frameType) = 0; virtual int checkFrame(QByteArray rawData) = 0; diff --git a/DeviceHub/protocol/dto/BCodeTerminalStatusDto.cpp b/DeviceHub/protocol/dto/BCodeTerminalStatusDto.cpp new file mode 100644 index 0000000..44c3c38 --- /dev/null +++ b/DeviceHub/protocol/dto/BCodeTerminalStatusDto.cpp @@ -0,0 +1,37 @@ +#include "BCodeTerminalStatusDto.h" + +BCodeTerminalStatusDto::BCodeTerminalStatusDto(QObject* parent) : DeviceFrameBaseDto(parent) +{ + +} + +QJsonObject BCodeTerminalStatusDto::toJSON() +{ + QJsonObject jsonObj; + + QJsonObject dataObj; + dataObj.insert("devStatus", this->devStatus); + dataObj.insert("bdcInStatus", bdcInStatus); + dataObj.insert("bacInStatus", bacInStatus); + dataObj.insert("fiveInStatus", fiveInStatus); + dataObj.insert("ref", ref); + dataObj.insert("track", track); + dataObj.insert("bacRange", bacRange); + dataObj.insert("bacRatio", bacRatio); + + QJsonArray bdcOutStatusArray; + QJsonArray bacOutStatusArray; + for (int i = 0; i < 4; i++) + { + bdcOutStatusArray.append(bdcOutStatusList.at(i)); + bacOutStatusArray.append(bacOutStatusList.at(i)); + } + dataObj.insert("bdcOutStatusArray", bdcOutStatusArray); + dataObj.insert("bacOutStatusArray", bacOutStatusArray); + + jsonObj.insert("ts", this->milisecond); + jsonObj.insert("frameType", this->frameType); + jsonObj.insert("data", dataObj); + + return jsonObj; +} diff --git a/DeviceHub/protocol/dto/BCodeTerminalStatusDto.h b/DeviceHub/protocol/dto/BCodeTerminalStatusDto.h new file mode 100644 index 0000000..db54b55 --- /dev/null +++ b/DeviceHub/protocol/dto/BCodeTerminalStatusDto.h @@ -0,0 +1,28 @@ +#ifndef BCODETERMINALSTATUSDTO_H +#define BCODETERMINALSTATUSDTO_H + +#include + +#include "DeviceFrameBaseDto.h" + +class BCodeTerminalStatusDto : public DeviceFrameBaseDto +{ + Q_OBJECT +public: + explicit BCodeTerminalStatusDto(QObject * parent = nullptr); + + QString devStatus; + QString bdcInStatus; + QString bacInStatus; + QString fiveInStatus; + QList bdcOutStatusList; + QList bacOutStatusList; + QString ref; + QString track; + float bacRange; + qint8 bacRatio; + + QJsonObject toJSON() override; +}; + +#endif // BCODETERMINALSTATUSDTO_H diff --git a/DeviceHub/protocol/protocol.pri b/DeviceHub/protocol/protocol.pri index 791654b..245a746 100644 --- a/DeviceHub/protocol/protocol.pri +++ b/DeviceHub/protocol/protocol.pri @@ -10,21 +10,23 @@ HEADERS += $$PWD/dto/TimeSwitcherInterfaceDto.h HEADERS += $$PWD/dto/FreqSwitcherInterfaceDto.h HEADERS += $$PWD/dto/FreqSwitcherStatusDto.h +HEADERS += $$PWD/dto/BCodeTerminalStatusDto.h HEADERS += $$PWD/DeviceProtocolBase.h HEADERS += $$PWD/FrequencyTuningProtocolBM.h HEADERS += $$PWD/SignalGeneratorProtocolBM.h HEADERS += $$PWD/TimeSwitcherProtocolBM.h HEADERS += $$PWD/FreqSwitcherProtocolBM.h +HEADERS += $$PWD/BCodeTerminalProtocolBM.h #HEADERS += $$PWD/dto/TimeReplicatorStatusDto.h #HEADERS += $$PWD/dto/FreqReplicatorStatusDto.h -#HEADERS += $$PWD/dto/BCodeTerminalStatusDto.h + #HEADERS += $$PWD/TimeReplicatorProtocolBM.h #HEADERS += $$PWD/FreqReplicatorProtocolTX.h -#HEADERS += $$PWD/BCodeTerminalProtocolBM.h + SOURCES += $$PWD/dto/SignalGeneratorStatusDto.cpp SOURCES += $$PWD/dto/SignalGeneratorInterfaceDto.cpp @@ -41,9 +43,9 @@ SOURCES += $$PWD/FrequencyTuningProtocolBM.cpp SOURCES += $$PWD/SignalGeneratorProtocolBM.cpp #SOURCES += $$PWD/dto/FreqReplicatorStatusDto.cpp -#SOURCES += $$PWD/dto/BCodeTerminalStatusDto.cpp +SOURCES += $$PWD/dto/BCodeTerminalStatusDto.cpp SOURCES += $$PWD/TimeSwitcherProtocolBM.cpp SOURCES += $$PWD/FreqSwitcherProtocolBM.cpp #SOURCES += $$PWD/TimeReplicatorProtocolBM.cpp #SOURCES += $$PWD/FreqReplicatorProtocolTX.cpp -#SOURCES += $$PWD/BCodeTerminalProtocolBM.cpp +SOURCES += $$PWD/BCodeTerminalProtocolBM.cpp