diff --git a/DeviceHub/TimeSwitcherForm.cpp b/DeviceHub/TimeSwitcherForm.cpp index 83278db..4d18f0c 100644 --- a/DeviceHub/TimeSwitcherForm.cpp +++ b/DeviceHub/TimeSwitcherForm.cpp @@ -19,7 +19,12 @@ // 获取设备对象 int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; TimeSwitcher * device = (TimeSwitcher *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("05").at(devIndex); - device->mockReceivData(); + + // mock测试工作模式则产生一条mock数据 + if (SettingConfig::getInstance().WORK_MODE == "mock") + { + device->mockReceivData(); + } } void TimeSwitcherForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) diff --git a/DeviceHub/TimeSwitcherForm.cpp b/DeviceHub/TimeSwitcherForm.cpp index 83278db..4d18f0c 100644 --- a/DeviceHub/TimeSwitcherForm.cpp +++ b/DeviceHub/TimeSwitcherForm.cpp @@ -19,7 +19,12 @@ // 获取设备对象 int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; TimeSwitcher * device = (TimeSwitcher *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("05").at(devIndex); - device->mockReceivData(); + + // mock测试工作模式则产生一条mock数据 + if (SettingConfig::getInstance().WORK_MODE == "mock") + { + device->mockReceivData(); + } } void TimeSwitcherForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) diff --git a/DeviceHub/common/utils/QSerialPortUtil.cpp b/DeviceHub/common/utils/QSerialPortUtil.cpp index a5e07ac..2331e2a 100644 --- a/DeviceHub/common/utils/QSerialPortUtil.cpp +++ b/DeviceHub/common/utils/QSerialPortUtil.cpp @@ -25,22 +25,16 @@ open = serial.open(QIODevice::ReadWrite); - std::cout << portName.toStdString() << " " << QString::number(baudRate).toStdString() << " " << open << std::endl; - if (open == true) { - // mock data received per second -// QTimer * timer = new QTimer(this); -// connect(timer, &QTimer::timeout, -// this, &QSerialPortUtil::mockReceivData); -// timer->start(1000 * 10); + std::cout << portName.toStdString() << " " << QString::number(baudRate).toStdString() << " " << open << std::endl; } } void QSerialPortUtil::sendData(QByteArray data) { - std::cout << data.toStdString() << std::endl; + std::cout << "[send]" << data.toStdString() << std::endl; if (this->open == true) { serial.write(data); @@ -50,7 +44,7 @@ void QSerialPortUtil::readData() { QByteArray buffer = serial.readAll(); - + std::cout << "[recv]" << buffer.toStdString() << std::endl; emit dataRecieved(buffer); } @@ -58,61 +52,3 @@ { return this->open; } -/* -void QSerialPortUtil::mockReceivData(QString portName) -{ - QByteArray buffer; - buffer.clear(); - - if (portName == "SignalGenerator") - { - - // signal generator - buffer.append("$GLN,0,192.168.000.126,255.255.255.000,192.168.000.001,192.168.001.126,255.255.255.000,192.168.001.001,255.255.255.255,3000,2000,2001*75").append("\r\n"); - buffer.append("$GLF,1,0,20210929,1,1,1,-0.01,20000,0,2,50,1*48").append("\r\n"); - buffer.append("$GPZDA,192157.00,01,01,2000,0,01*5C").append("\r\n"); - buffer.append("$GPMJD,192157.00,51544,0,01*73").append("\r\n"); - buffer.append("$GLC,0,0*48").append("\r\n"); - - } else if (portName == "FrequencyTuning") - { - // frequency tuning - buffer.append("$GLN,0,192.168.1.126,255.255.255.0,192.168.1.1,255.255.255.255,3000,2000,2001*71").append("\r\n"); - buffer.append("$GLF,0,0,0,0,00000,0,0,0,0,0,0,0,0,0,0,0,1900,2355,0,0,0,0,0,1,1,1*4C").append("\r\n"); - buffer.append("$GLP,0,1000,0,100,20000*45").append("\r\n"); - } else if (portName == "TimeSwitcher") - { - // time switcher - buffer.append("$GPZDA,211219.00,01,01,2000,0,01*5D").append("\r\n"); - buffer.append("$GLC,0*54").append("\r\n"); - buffer.append("$GLF,1,1,1,010,100,100,000,000,0,-235136964.75,-235136959.89,0,0,0,5,50,-16390,-16710,-15660,-16700,-17300,11111,11111,11111*51").append("\r\n"); - buffer.append("$GLN,0,192.168.000.126,255.255.255.000,192.168.000.001,192.168.001.126,255.255.255.000,192.168.001.001,255.255.255.255,3000,2000,2001*75").append("\r\n"); - } else if (portName == "FreqSwitcher") - { - // freq switcher - buffer.append("$GLF,0,4,0,0,22000,-745,-776,0,0,0,0,0,100,100,0,0,0,1,00000,111111,111111*54").append("\r\n"); - buffer.append("$GLC,0*54").append("\r\n"); - buffer.append("$GLN,0,192.168.000.123,255.255.255.000,192.168.000.001,192.168.001.123,255.255.255.000,192.168.001.001,255.255.255.255,3000,2000,2001*75").append("\r\n"); - } else if (portName == "BCodeTerminal") - { - // b-code terminal - buffer.append("$2621304-20 210100112100f90210.035422*"); - buffer.append("$3e21304-20 2101001111304-20 2101001210801H.1.00S.1.030008432*"); - } else if (portName == "TimeReplicator") - { - // time replicator - buffer.append("$2B21308-13 21010012200000000000000000faf0*"); - buffer.append("$3C21308-13 2101008220322222222111111110000000000000000ca24*"); - buffer.append("$3C21308-13 21010052207111111111111111111111111000000005984*"); - buffer.append("$3C21308-13 2101005121511111111111111111111111111111111bcfe*"); - buffer.append("$3E21308-13 2101001211308-13 2101001210929H.1.00S.1.00000292b*"); - } else if (portName == "FreqReplicator") - { - // freq replicator - buffer = QByteUtil::hexStringToBytes("AA550015010001000101010101010101010101010101010101EB"); - buffer.append(QByteUtil::hexStringToBytes("AA550015020001000101010101010101010101010101010101E8")); - } - - emit dataRecieved(buffer); -} -*/ diff --git a/DeviceHub/TimeSwitcherForm.cpp b/DeviceHub/TimeSwitcherForm.cpp index 83278db..4d18f0c 100644 --- a/DeviceHub/TimeSwitcherForm.cpp +++ b/DeviceHub/TimeSwitcherForm.cpp @@ -19,7 +19,12 @@ // 获取设备对象 int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; TimeSwitcher * device = (TimeSwitcher *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("05").at(devIndex); - device->mockReceivData(); + + // mock测试工作模式则产生一条mock数据 + if (SettingConfig::getInstance().WORK_MODE == "mock") + { + device->mockReceivData(); + } } void TimeSwitcherForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) diff --git a/DeviceHub/common/utils/QSerialPortUtil.cpp b/DeviceHub/common/utils/QSerialPortUtil.cpp index a5e07ac..2331e2a 100644 --- a/DeviceHub/common/utils/QSerialPortUtil.cpp +++ b/DeviceHub/common/utils/QSerialPortUtil.cpp @@ -25,22 +25,16 @@ open = serial.open(QIODevice::ReadWrite); - std::cout << portName.toStdString() << " " << QString::number(baudRate).toStdString() << " " << open << std::endl; - if (open == true) { - // mock data received per second -// QTimer * timer = new QTimer(this); -// connect(timer, &QTimer::timeout, -// this, &QSerialPortUtil::mockReceivData); -// timer->start(1000 * 10); + std::cout << portName.toStdString() << " " << QString::number(baudRate).toStdString() << " " << open << std::endl; } } void QSerialPortUtil::sendData(QByteArray data) { - std::cout << data.toStdString() << std::endl; + std::cout << "[send]" << data.toStdString() << std::endl; if (this->open == true) { serial.write(data); @@ -50,7 +44,7 @@ void QSerialPortUtil::readData() { QByteArray buffer = serial.readAll(); - + std::cout << "[recv]" << buffer.toStdString() << std::endl; emit dataRecieved(buffer); } @@ -58,61 +52,3 @@ { return this->open; } -/* -void QSerialPortUtil::mockReceivData(QString portName) -{ - QByteArray buffer; - buffer.clear(); - - if (portName == "SignalGenerator") - { - - // signal generator - buffer.append("$GLN,0,192.168.000.126,255.255.255.000,192.168.000.001,192.168.001.126,255.255.255.000,192.168.001.001,255.255.255.255,3000,2000,2001*75").append("\r\n"); - buffer.append("$GLF,1,0,20210929,1,1,1,-0.01,20000,0,2,50,1*48").append("\r\n"); - buffer.append("$GPZDA,192157.00,01,01,2000,0,01*5C").append("\r\n"); - buffer.append("$GPMJD,192157.00,51544,0,01*73").append("\r\n"); - buffer.append("$GLC,0,0*48").append("\r\n"); - - } else if (portName == "FrequencyTuning") - { - // frequency tuning - buffer.append("$GLN,0,192.168.1.126,255.255.255.0,192.168.1.1,255.255.255.255,3000,2000,2001*71").append("\r\n"); - buffer.append("$GLF,0,0,0,0,00000,0,0,0,0,0,0,0,0,0,0,0,1900,2355,0,0,0,0,0,1,1,1*4C").append("\r\n"); - buffer.append("$GLP,0,1000,0,100,20000*45").append("\r\n"); - } else if (portName == "TimeSwitcher") - { - // time switcher - buffer.append("$GPZDA,211219.00,01,01,2000,0,01*5D").append("\r\n"); - buffer.append("$GLC,0*54").append("\r\n"); - buffer.append("$GLF,1,1,1,010,100,100,000,000,0,-235136964.75,-235136959.89,0,0,0,5,50,-16390,-16710,-15660,-16700,-17300,11111,11111,11111*51").append("\r\n"); - buffer.append("$GLN,0,192.168.000.126,255.255.255.000,192.168.000.001,192.168.001.126,255.255.255.000,192.168.001.001,255.255.255.255,3000,2000,2001*75").append("\r\n"); - } else if (portName == "FreqSwitcher") - { - // freq switcher - buffer.append("$GLF,0,4,0,0,22000,-745,-776,0,0,0,0,0,100,100,0,0,0,1,00000,111111,111111*54").append("\r\n"); - buffer.append("$GLC,0*54").append("\r\n"); - buffer.append("$GLN,0,192.168.000.123,255.255.255.000,192.168.000.001,192.168.001.123,255.255.255.000,192.168.001.001,255.255.255.255,3000,2000,2001*75").append("\r\n"); - } else if (portName == "BCodeTerminal") - { - // b-code terminal - buffer.append("$2621304-20 210100112100f90210.035422*"); - buffer.append("$3e21304-20 2101001111304-20 2101001210801H.1.00S.1.030008432*"); - } else if (portName == "TimeReplicator") - { - // time replicator - buffer.append("$2B21308-13 21010012200000000000000000faf0*"); - buffer.append("$3C21308-13 2101008220322222222111111110000000000000000ca24*"); - buffer.append("$3C21308-13 21010052207111111111111111111111111000000005984*"); - buffer.append("$3C21308-13 2101005121511111111111111111111111111111111bcfe*"); - buffer.append("$3E21308-13 2101001211308-13 2101001210929H.1.00S.1.00000292b*"); - } else if (portName == "FreqReplicator") - { - // freq replicator - buffer = QByteUtil::hexStringToBytes("AA550015010001000101010101010101010101010101010101EB"); - buffer.append(QByteUtil::hexStringToBytes("AA550015020001000101010101010101010101010101010101E8")); - } - - emit dataRecieved(buffer); -} -*/ diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index dc8d791..9c7a9bd 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -83,9 +83,3 @@ this->serialUtilB.openSerialPort(comNameList.at(1), this->baudRate); } } - -void DeviceBase::sendDataToSerial(QByteArray data) -{ - data.append(FRAME_TAIL); - this->serialUtil.sendData(data); -} diff --git a/DeviceHub/TimeSwitcherForm.cpp b/DeviceHub/TimeSwitcherForm.cpp index 83278db..4d18f0c 100644 --- a/DeviceHub/TimeSwitcherForm.cpp +++ b/DeviceHub/TimeSwitcherForm.cpp @@ -19,7 +19,12 @@ // 获取设备对象 int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; TimeSwitcher * device = (TimeSwitcher *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("05").at(devIndex); - device->mockReceivData(); + + // mock测试工作模式则产生一条mock数据 + if (SettingConfig::getInstance().WORK_MODE == "mock") + { + device->mockReceivData(); + } } void TimeSwitcherForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) diff --git a/DeviceHub/common/utils/QSerialPortUtil.cpp b/DeviceHub/common/utils/QSerialPortUtil.cpp index a5e07ac..2331e2a 100644 --- a/DeviceHub/common/utils/QSerialPortUtil.cpp +++ b/DeviceHub/common/utils/QSerialPortUtil.cpp @@ -25,22 +25,16 @@ open = serial.open(QIODevice::ReadWrite); - std::cout << portName.toStdString() << " " << QString::number(baudRate).toStdString() << " " << open << std::endl; - if (open == true) { - // mock data received per second -// QTimer * timer = new QTimer(this); -// connect(timer, &QTimer::timeout, -// this, &QSerialPortUtil::mockReceivData); -// timer->start(1000 * 10); + std::cout << portName.toStdString() << " " << QString::number(baudRate).toStdString() << " " << open << std::endl; } } void QSerialPortUtil::sendData(QByteArray data) { - std::cout << data.toStdString() << std::endl; + std::cout << "[send]" << data.toStdString() << std::endl; if (this->open == true) { serial.write(data); @@ -50,7 +44,7 @@ void QSerialPortUtil::readData() { QByteArray buffer = serial.readAll(); - + std::cout << "[recv]" << buffer.toStdString() << std::endl; emit dataRecieved(buffer); } @@ -58,61 +52,3 @@ { return this->open; } -/* -void QSerialPortUtil::mockReceivData(QString portName) -{ - QByteArray buffer; - buffer.clear(); - - if (portName == "SignalGenerator") - { - - // signal generator - buffer.append("$GLN,0,192.168.000.126,255.255.255.000,192.168.000.001,192.168.001.126,255.255.255.000,192.168.001.001,255.255.255.255,3000,2000,2001*75").append("\r\n"); - buffer.append("$GLF,1,0,20210929,1,1,1,-0.01,20000,0,2,50,1*48").append("\r\n"); - buffer.append("$GPZDA,192157.00,01,01,2000,0,01*5C").append("\r\n"); - buffer.append("$GPMJD,192157.00,51544,0,01*73").append("\r\n"); - buffer.append("$GLC,0,0*48").append("\r\n"); - - } else if (portName == "FrequencyTuning") - { - // frequency tuning - buffer.append("$GLN,0,192.168.1.126,255.255.255.0,192.168.1.1,255.255.255.255,3000,2000,2001*71").append("\r\n"); - buffer.append("$GLF,0,0,0,0,00000,0,0,0,0,0,0,0,0,0,0,0,1900,2355,0,0,0,0,0,1,1,1*4C").append("\r\n"); - buffer.append("$GLP,0,1000,0,100,20000*45").append("\r\n"); - } else if (portName == "TimeSwitcher") - { - // time switcher - buffer.append("$GPZDA,211219.00,01,01,2000,0,01*5D").append("\r\n"); - buffer.append("$GLC,0*54").append("\r\n"); - buffer.append("$GLF,1,1,1,010,100,100,000,000,0,-235136964.75,-235136959.89,0,0,0,5,50,-16390,-16710,-15660,-16700,-17300,11111,11111,11111*51").append("\r\n"); - buffer.append("$GLN,0,192.168.000.126,255.255.255.000,192.168.000.001,192.168.001.126,255.255.255.000,192.168.001.001,255.255.255.255,3000,2000,2001*75").append("\r\n"); - } else if (portName == "FreqSwitcher") - { - // freq switcher - buffer.append("$GLF,0,4,0,0,22000,-745,-776,0,0,0,0,0,100,100,0,0,0,1,00000,111111,111111*54").append("\r\n"); - buffer.append("$GLC,0*54").append("\r\n"); - buffer.append("$GLN,0,192.168.000.123,255.255.255.000,192.168.000.001,192.168.001.123,255.255.255.000,192.168.001.001,255.255.255.255,3000,2000,2001*75").append("\r\n"); - } else if (portName == "BCodeTerminal") - { - // b-code terminal - buffer.append("$2621304-20 210100112100f90210.035422*"); - buffer.append("$3e21304-20 2101001111304-20 2101001210801H.1.00S.1.030008432*"); - } else if (portName == "TimeReplicator") - { - // time replicator - buffer.append("$2B21308-13 21010012200000000000000000faf0*"); - buffer.append("$3C21308-13 2101008220322222222111111110000000000000000ca24*"); - buffer.append("$3C21308-13 21010052207111111111111111111111111000000005984*"); - buffer.append("$3C21308-13 2101005121511111111111111111111111111111111bcfe*"); - buffer.append("$3E21308-13 2101001211308-13 2101001210929H.1.00S.1.00000292b*"); - } else if (portName == "FreqReplicator") - { - // freq replicator - buffer = QByteUtil::hexStringToBytes("AA550015010001000101010101010101010101010101010101EB"); - buffer.append(QByteUtil::hexStringToBytes("AA550015020001000101010101010101010101010101010101E8")); - } - - emit dataRecieved(buffer); -} -*/ diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index dc8d791..9c7a9bd 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -83,9 +83,3 @@ this->serialUtilB.openSerialPort(comNameList.at(1), this->baudRate); } } - -void DeviceBase::sendDataToSerial(QByteArray data) -{ - data.append(FRAME_TAIL); - this->serialUtil.sendData(data); -} diff --git a/DeviceHub/device/DeviceBase.h b/DeviceHub/device/DeviceBase.h index 666615a..0a6ef5d 100644 --- a/DeviceHub/device/DeviceBase.h +++ b/DeviceHub/device/DeviceBase.h @@ -28,7 +28,7 @@ void initSerialPort(); bool isSerialOpen(); - virtual void sendDataToSerial(QByteArray data); + virtual void sendDataToSerial(QByteArray data) = 0; virtual void afterFrameParse(DeviceFrameBaseDto * frameDto) = 0; virtual void afterCommandReply(DeviceFrameBaseDto * frameDto) = 0; @@ -45,6 +45,7 @@ QString devType; QString commandId; + QByteArray rawCommandBytes; QSerialPortUtil serialUtil; QSerialPortUtil serialUtilB; // serial port 2 diff --git a/DeviceHub/TimeSwitcherForm.cpp b/DeviceHub/TimeSwitcherForm.cpp index 83278db..4d18f0c 100644 --- a/DeviceHub/TimeSwitcherForm.cpp +++ b/DeviceHub/TimeSwitcherForm.cpp @@ -19,7 +19,12 @@ // 获取设备对象 int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; TimeSwitcher * device = (TimeSwitcher *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("05").at(devIndex); - device->mockReceivData(); + + // mock测试工作模式则产生一条mock数据 + if (SettingConfig::getInstance().WORK_MODE == "mock") + { + device->mockReceivData(); + } } void TimeSwitcherForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) diff --git a/DeviceHub/common/utils/QSerialPortUtil.cpp b/DeviceHub/common/utils/QSerialPortUtil.cpp index a5e07ac..2331e2a 100644 --- a/DeviceHub/common/utils/QSerialPortUtil.cpp +++ b/DeviceHub/common/utils/QSerialPortUtil.cpp @@ -25,22 +25,16 @@ open = serial.open(QIODevice::ReadWrite); - std::cout << portName.toStdString() << " " << QString::number(baudRate).toStdString() << " " << open << std::endl; - if (open == true) { - // mock data received per second -// QTimer * timer = new QTimer(this); -// connect(timer, &QTimer::timeout, -// this, &QSerialPortUtil::mockReceivData); -// timer->start(1000 * 10); + std::cout << portName.toStdString() << " " << QString::number(baudRate).toStdString() << " " << open << std::endl; } } void QSerialPortUtil::sendData(QByteArray data) { - std::cout << data.toStdString() << std::endl; + std::cout << "[send]" << data.toStdString() << std::endl; if (this->open == true) { serial.write(data); @@ -50,7 +44,7 @@ void QSerialPortUtil::readData() { QByteArray buffer = serial.readAll(); - + std::cout << "[recv]" << buffer.toStdString() << std::endl; emit dataRecieved(buffer); } @@ -58,61 +52,3 @@ { return this->open; } -/* -void QSerialPortUtil::mockReceivData(QString portName) -{ - QByteArray buffer; - buffer.clear(); - - if (portName == "SignalGenerator") - { - - // signal generator - buffer.append("$GLN,0,192.168.000.126,255.255.255.000,192.168.000.001,192.168.001.126,255.255.255.000,192.168.001.001,255.255.255.255,3000,2000,2001*75").append("\r\n"); - buffer.append("$GLF,1,0,20210929,1,1,1,-0.01,20000,0,2,50,1*48").append("\r\n"); - buffer.append("$GPZDA,192157.00,01,01,2000,0,01*5C").append("\r\n"); - buffer.append("$GPMJD,192157.00,51544,0,01*73").append("\r\n"); - buffer.append("$GLC,0,0*48").append("\r\n"); - - } else if (portName == "FrequencyTuning") - { - // frequency tuning - buffer.append("$GLN,0,192.168.1.126,255.255.255.0,192.168.1.1,255.255.255.255,3000,2000,2001*71").append("\r\n"); - buffer.append("$GLF,0,0,0,0,00000,0,0,0,0,0,0,0,0,0,0,0,1900,2355,0,0,0,0,0,1,1,1*4C").append("\r\n"); - buffer.append("$GLP,0,1000,0,100,20000*45").append("\r\n"); - } else if (portName == "TimeSwitcher") - { - // time switcher - buffer.append("$GPZDA,211219.00,01,01,2000,0,01*5D").append("\r\n"); - buffer.append("$GLC,0*54").append("\r\n"); - buffer.append("$GLF,1,1,1,010,100,100,000,000,0,-235136964.75,-235136959.89,0,0,0,5,50,-16390,-16710,-15660,-16700,-17300,11111,11111,11111*51").append("\r\n"); - buffer.append("$GLN,0,192.168.000.126,255.255.255.000,192.168.000.001,192.168.001.126,255.255.255.000,192.168.001.001,255.255.255.255,3000,2000,2001*75").append("\r\n"); - } else if (portName == "FreqSwitcher") - { - // freq switcher - buffer.append("$GLF,0,4,0,0,22000,-745,-776,0,0,0,0,0,100,100,0,0,0,1,00000,111111,111111*54").append("\r\n"); - buffer.append("$GLC,0*54").append("\r\n"); - buffer.append("$GLN,0,192.168.000.123,255.255.255.000,192.168.000.001,192.168.001.123,255.255.255.000,192.168.001.001,255.255.255.255,3000,2000,2001*75").append("\r\n"); - } else if (portName == "BCodeTerminal") - { - // b-code terminal - buffer.append("$2621304-20 210100112100f90210.035422*"); - buffer.append("$3e21304-20 2101001111304-20 2101001210801H.1.00S.1.030008432*"); - } else if (portName == "TimeReplicator") - { - // time replicator - buffer.append("$2B21308-13 21010012200000000000000000faf0*"); - buffer.append("$3C21308-13 2101008220322222222111111110000000000000000ca24*"); - buffer.append("$3C21308-13 21010052207111111111111111111111111000000005984*"); - buffer.append("$3C21308-13 2101005121511111111111111111111111111111111bcfe*"); - buffer.append("$3E21308-13 2101001211308-13 2101001210929H.1.00S.1.00000292b*"); - } else if (portName == "FreqReplicator") - { - // freq replicator - buffer = QByteUtil::hexStringToBytes("AA550015010001000101010101010101010101010101010101EB"); - buffer.append(QByteUtil::hexStringToBytes("AA550015020001000101010101010101010101010101010101E8")); - } - - emit dataRecieved(buffer); -} -*/ diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index dc8d791..9c7a9bd 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -83,9 +83,3 @@ this->serialUtilB.openSerialPort(comNameList.at(1), this->baudRate); } } - -void DeviceBase::sendDataToSerial(QByteArray data) -{ - data.append(FRAME_TAIL); - this->serialUtil.sendData(data); -} diff --git a/DeviceHub/device/DeviceBase.h b/DeviceHub/device/DeviceBase.h index 666615a..0a6ef5d 100644 --- a/DeviceHub/device/DeviceBase.h +++ b/DeviceHub/device/DeviceBase.h @@ -28,7 +28,7 @@ void initSerialPort(); bool isSerialOpen(); - virtual void sendDataToSerial(QByteArray data); + virtual void sendDataToSerial(QByteArray data) = 0; virtual void afterFrameParse(DeviceFrameBaseDto * frameDto) = 0; virtual void afterCommandReply(DeviceFrameBaseDto * frameDto) = 0; @@ -45,6 +45,7 @@ QString devType; QString commandId; + QByteArray rawCommandBytes; QSerialPortUtil serialUtil; QSerialPortUtil serialUtilB; // serial port 2 diff --git a/DeviceHub/device/TimeReplicator.cpp b/DeviceHub/device/TimeReplicator.cpp index 2e22c67..8212c87 100644 --- a/DeviceHub/device/TimeReplicator.cpp +++ b/DeviceHub/device/TimeReplicator.cpp @@ -42,7 +42,7 @@ QTimer::singleShot(1000, this, [=](){ QByteArray buffer; buffer.clear(); - buffer.append("$3E21308-13 2101001211308-13 2101001210929H.1.00S.1.00000292b*").append("\r\n"); + buffer.append("$3C21308-13 2101005121511111111111111111111111111111111bcfe*").append("\r\n"); this->dataReceivedHandler(buffer); }); @@ -90,7 +90,6 @@ frameDto->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); frameDto->milisecond = now.toMSecsSinceEpoch(); frameDto->rawFrame = frameByte; - frameDto->devCode = devCode; if (frameType == TimeReplicatorProtocolBM::TIME_REPLICATOR_FRAME_TYPE::STATUS_FRAME) @@ -100,6 +99,7 @@ if (frameType == TimeReplicatorProtocolBM::TIME_REPLICATOR_FRAME_TYPE::CMDREP_FRAME) { + frameDto->rawCommand = rawCommandBytes; frameDto->commandId = commandId; this->afterCommandReply(frameDto); } @@ -116,9 +116,6 @@ void TimeReplicator::afterFrameParse(DeviceFrameBaseDto * frameDto) { - std::cout << "frame type: " << typeid(* frameDto).name() << std::endl; - std::cout << frameDto->rawFrame.toStdString() << std::endl; - // 0. 输出到日志文件中 QString date = frameDto->timestamp.mid(0, 10); @@ -157,7 +154,7 @@ // 2. 解析后的json数据 QString frameFilename = "frame_" + devCode + ".log"; - QString frameContent = frameDto->timestamp + " [recv] " + QJsonDocument(frameDto->toJSON()).toJson(QJsonDocument::Compact); + QString frameContent = frameDto->timestamp + " [reply] " + QJsonDocument(frameDto->toJSON()).toJson(QJsonDocument::Compact); QLogUtil::writeChannelDataLogByDate(date, frameFilename, frameContent); // 3. 输出到中间件,执行后续处理过程 @@ -171,7 +168,32 @@ void TimeReplicator::sendDataToSerial(QByteArray data) { data.append(FRAME_TAIL); - this->serialUtil.sendData(data); + if (SettingConfig::getInstance().WORK_MODE == "real") + { + if (serialUtil.isOpen() == true) + { + this->serialUtil.sendData(data); + } else if (serialUtilB.isOpen() == true) + { + this->serialUtilB.sendData(data); + } else + { + TimeReplicatorCmdRepDto replyDto; + replyDto.devCode = devCode; + replyDto.commandId = commandId; + replyDto.rawCommand = rawCommandBytes; + replyDto.cmdStatus = 0; + replyDto.remarks = "serialport not avilable"; + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + kafkaProducer.produceMessage(SettingConfig::getInstance().KAFKA_CMDCB_TOPIC, + QString(QJsonDocument(replyDto.toJSON()).toJson(QJsonDocument::Compact))); + } + return; + } + } // 记录日志 // 0. 输出到日志文件中 @@ -182,22 +204,24 @@ 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); + + std::cout << content.toStdString() << std::endl; } void TimeReplicator::commandReceivedHandler(QJsonObject command) { if (command.contains("deviceType") == false || command.value("deviceType").toString() != "09") { - std::cout << "device type [" << command.value("deviceType").toString().toStdString() << "] not matched. return" << std::endl; return; } if (command.contains("deviceId") == false || command.value("deviceId").toString() != deviceId) { - std::cout << "deviceId [" << command.value("deviceId").toString().toStdString() << "] not matched. return" << std::endl; return; } + std::cout << QString(QJsonDocument(command).toJson(QJsonDocument::Compact)).toStdString() << std::endl; + // 记录日志 // 0. 输出到日志文件中 QDateTime now = QDateTime::currentDateTime(); @@ -205,20 +229,15 @@ // 1. 原始字节数组数据 QString filename = "frame_" + devCode + ".log"; - QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + command.find("cmdStr")->toString(); + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [recv] " + command.find("cmdStr")->toString(); QLogUtil::writeChannelDataLogByDate(date, filename, content); QByteArray commandBytes = protocol->generateSettingCommand(devCode, command.value("command").toString(), command.value("params").toString()); this->sendDataToSerial(commandBytes); command.insert("rawCommand", QString::fromUtf8(commandBytes)); this->commandId = command.value("commandId").toString(); -/* - 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))); -*/ + this->rawCommandBytes = commandBytes; + // display on page emit sendCommandToDisplay(command); } diff --git a/DeviceHub/TimeSwitcherForm.cpp b/DeviceHub/TimeSwitcherForm.cpp index 83278db..4d18f0c 100644 --- a/DeviceHub/TimeSwitcherForm.cpp +++ b/DeviceHub/TimeSwitcherForm.cpp @@ -19,7 +19,12 @@ // 获取设备对象 int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; TimeSwitcher * device = (TimeSwitcher *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("05").at(devIndex); - device->mockReceivData(); + + // mock测试工作模式则产生一条mock数据 + if (SettingConfig::getInstance().WORK_MODE == "mock") + { + device->mockReceivData(); + } } void TimeSwitcherForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) diff --git a/DeviceHub/common/utils/QSerialPortUtil.cpp b/DeviceHub/common/utils/QSerialPortUtil.cpp index a5e07ac..2331e2a 100644 --- a/DeviceHub/common/utils/QSerialPortUtil.cpp +++ b/DeviceHub/common/utils/QSerialPortUtil.cpp @@ -25,22 +25,16 @@ open = serial.open(QIODevice::ReadWrite); - std::cout << portName.toStdString() << " " << QString::number(baudRate).toStdString() << " " << open << std::endl; - if (open == true) { - // mock data received per second -// QTimer * timer = new QTimer(this); -// connect(timer, &QTimer::timeout, -// this, &QSerialPortUtil::mockReceivData); -// timer->start(1000 * 10); + std::cout << portName.toStdString() << " " << QString::number(baudRate).toStdString() << " " << open << std::endl; } } void QSerialPortUtil::sendData(QByteArray data) { - std::cout << data.toStdString() << std::endl; + std::cout << "[send]" << data.toStdString() << std::endl; if (this->open == true) { serial.write(data); @@ -50,7 +44,7 @@ void QSerialPortUtil::readData() { QByteArray buffer = serial.readAll(); - + std::cout << "[recv]" << buffer.toStdString() << std::endl; emit dataRecieved(buffer); } @@ -58,61 +52,3 @@ { return this->open; } -/* -void QSerialPortUtil::mockReceivData(QString portName) -{ - QByteArray buffer; - buffer.clear(); - - if (portName == "SignalGenerator") - { - - // signal generator - buffer.append("$GLN,0,192.168.000.126,255.255.255.000,192.168.000.001,192.168.001.126,255.255.255.000,192.168.001.001,255.255.255.255,3000,2000,2001*75").append("\r\n"); - buffer.append("$GLF,1,0,20210929,1,1,1,-0.01,20000,0,2,50,1*48").append("\r\n"); - buffer.append("$GPZDA,192157.00,01,01,2000,0,01*5C").append("\r\n"); - buffer.append("$GPMJD,192157.00,51544,0,01*73").append("\r\n"); - buffer.append("$GLC,0,0*48").append("\r\n"); - - } else if (portName == "FrequencyTuning") - { - // frequency tuning - buffer.append("$GLN,0,192.168.1.126,255.255.255.0,192.168.1.1,255.255.255.255,3000,2000,2001*71").append("\r\n"); - buffer.append("$GLF,0,0,0,0,00000,0,0,0,0,0,0,0,0,0,0,0,1900,2355,0,0,0,0,0,1,1,1*4C").append("\r\n"); - buffer.append("$GLP,0,1000,0,100,20000*45").append("\r\n"); - } else if (portName == "TimeSwitcher") - { - // time switcher - buffer.append("$GPZDA,211219.00,01,01,2000,0,01*5D").append("\r\n"); - buffer.append("$GLC,0*54").append("\r\n"); - buffer.append("$GLF,1,1,1,010,100,100,000,000,0,-235136964.75,-235136959.89,0,0,0,5,50,-16390,-16710,-15660,-16700,-17300,11111,11111,11111*51").append("\r\n"); - buffer.append("$GLN,0,192.168.000.126,255.255.255.000,192.168.000.001,192.168.001.126,255.255.255.000,192.168.001.001,255.255.255.255,3000,2000,2001*75").append("\r\n"); - } else if (portName == "FreqSwitcher") - { - // freq switcher - buffer.append("$GLF,0,4,0,0,22000,-745,-776,0,0,0,0,0,100,100,0,0,0,1,00000,111111,111111*54").append("\r\n"); - buffer.append("$GLC,0*54").append("\r\n"); - buffer.append("$GLN,0,192.168.000.123,255.255.255.000,192.168.000.001,192.168.001.123,255.255.255.000,192.168.001.001,255.255.255.255,3000,2000,2001*75").append("\r\n"); - } else if (portName == "BCodeTerminal") - { - // b-code terminal - buffer.append("$2621304-20 210100112100f90210.035422*"); - buffer.append("$3e21304-20 2101001111304-20 2101001210801H.1.00S.1.030008432*"); - } else if (portName == "TimeReplicator") - { - // time replicator - buffer.append("$2B21308-13 21010012200000000000000000faf0*"); - buffer.append("$3C21308-13 2101008220322222222111111110000000000000000ca24*"); - buffer.append("$3C21308-13 21010052207111111111111111111111111000000005984*"); - buffer.append("$3C21308-13 2101005121511111111111111111111111111111111bcfe*"); - buffer.append("$3E21308-13 2101001211308-13 2101001210929H.1.00S.1.00000292b*"); - } else if (portName == "FreqReplicator") - { - // freq replicator - buffer = QByteUtil::hexStringToBytes("AA550015010001000101010101010101010101010101010101EB"); - buffer.append(QByteUtil::hexStringToBytes("AA550015020001000101010101010101010101010101010101E8")); - } - - emit dataRecieved(buffer); -} -*/ diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index dc8d791..9c7a9bd 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -83,9 +83,3 @@ this->serialUtilB.openSerialPort(comNameList.at(1), this->baudRate); } } - -void DeviceBase::sendDataToSerial(QByteArray data) -{ - data.append(FRAME_TAIL); - this->serialUtil.sendData(data); -} diff --git a/DeviceHub/device/DeviceBase.h b/DeviceHub/device/DeviceBase.h index 666615a..0a6ef5d 100644 --- a/DeviceHub/device/DeviceBase.h +++ b/DeviceHub/device/DeviceBase.h @@ -28,7 +28,7 @@ void initSerialPort(); bool isSerialOpen(); - virtual void sendDataToSerial(QByteArray data); + virtual void sendDataToSerial(QByteArray data) = 0; virtual void afterFrameParse(DeviceFrameBaseDto * frameDto) = 0; virtual void afterCommandReply(DeviceFrameBaseDto * frameDto) = 0; @@ -45,6 +45,7 @@ QString devType; QString commandId; + QByteArray rawCommandBytes; QSerialPortUtil serialUtil; QSerialPortUtil serialUtilB; // serial port 2 diff --git a/DeviceHub/device/TimeReplicator.cpp b/DeviceHub/device/TimeReplicator.cpp index 2e22c67..8212c87 100644 --- a/DeviceHub/device/TimeReplicator.cpp +++ b/DeviceHub/device/TimeReplicator.cpp @@ -42,7 +42,7 @@ QTimer::singleShot(1000, this, [=](){ QByteArray buffer; buffer.clear(); - buffer.append("$3E21308-13 2101001211308-13 2101001210929H.1.00S.1.00000292b*").append("\r\n"); + buffer.append("$3C21308-13 2101005121511111111111111111111111111111111bcfe*").append("\r\n"); this->dataReceivedHandler(buffer); }); @@ -90,7 +90,6 @@ frameDto->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); frameDto->milisecond = now.toMSecsSinceEpoch(); frameDto->rawFrame = frameByte; - frameDto->devCode = devCode; if (frameType == TimeReplicatorProtocolBM::TIME_REPLICATOR_FRAME_TYPE::STATUS_FRAME) @@ -100,6 +99,7 @@ if (frameType == TimeReplicatorProtocolBM::TIME_REPLICATOR_FRAME_TYPE::CMDREP_FRAME) { + frameDto->rawCommand = rawCommandBytes; frameDto->commandId = commandId; this->afterCommandReply(frameDto); } @@ -116,9 +116,6 @@ void TimeReplicator::afterFrameParse(DeviceFrameBaseDto * frameDto) { - std::cout << "frame type: " << typeid(* frameDto).name() << std::endl; - std::cout << frameDto->rawFrame.toStdString() << std::endl; - // 0. 输出到日志文件中 QString date = frameDto->timestamp.mid(0, 10); @@ -157,7 +154,7 @@ // 2. 解析后的json数据 QString frameFilename = "frame_" + devCode + ".log"; - QString frameContent = frameDto->timestamp + " [recv] " + QJsonDocument(frameDto->toJSON()).toJson(QJsonDocument::Compact); + QString frameContent = frameDto->timestamp + " [reply] " + QJsonDocument(frameDto->toJSON()).toJson(QJsonDocument::Compact); QLogUtil::writeChannelDataLogByDate(date, frameFilename, frameContent); // 3. 输出到中间件,执行后续处理过程 @@ -171,7 +168,32 @@ void TimeReplicator::sendDataToSerial(QByteArray data) { data.append(FRAME_TAIL); - this->serialUtil.sendData(data); + if (SettingConfig::getInstance().WORK_MODE == "real") + { + if (serialUtil.isOpen() == true) + { + this->serialUtil.sendData(data); + } else if (serialUtilB.isOpen() == true) + { + this->serialUtilB.sendData(data); + } else + { + TimeReplicatorCmdRepDto replyDto; + replyDto.devCode = devCode; + replyDto.commandId = commandId; + replyDto.rawCommand = rawCommandBytes; + replyDto.cmdStatus = 0; + replyDto.remarks = "serialport not avilable"; + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + kafkaProducer.produceMessage(SettingConfig::getInstance().KAFKA_CMDCB_TOPIC, + QString(QJsonDocument(replyDto.toJSON()).toJson(QJsonDocument::Compact))); + } + return; + } + } // 记录日志 // 0. 输出到日志文件中 @@ -182,22 +204,24 @@ 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); + + std::cout << content.toStdString() << std::endl; } void TimeReplicator::commandReceivedHandler(QJsonObject command) { if (command.contains("deviceType") == false || command.value("deviceType").toString() != "09") { - std::cout << "device type [" << command.value("deviceType").toString().toStdString() << "] not matched. return" << std::endl; return; } if (command.contains("deviceId") == false || command.value("deviceId").toString() != deviceId) { - std::cout << "deviceId [" << command.value("deviceId").toString().toStdString() << "] not matched. return" << std::endl; return; } + std::cout << QString(QJsonDocument(command).toJson(QJsonDocument::Compact)).toStdString() << std::endl; + // 记录日志 // 0. 输出到日志文件中 QDateTime now = QDateTime::currentDateTime(); @@ -205,20 +229,15 @@ // 1. 原始字节数组数据 QString filename = "frame_" + devCode + ".log"; - QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + command.find("cmdStr")->toString(); + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [recv] " + command.find("cmdStr")->toString(); QLogUtil::writeChannelDataLogByDate(date, filename, content); QByteArray commandBytes = protocol->generateSettingCommand(devCode, command.value("command").toString(), command.value("params").toString()); this->sendDataToSerial(commandBytes); command.insert("rawCommand", QString::fromUtf8(commandBytes)); this->commandId = command.value("commandId").toString(); -/* - 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))); -*/ + this->rawCommandBytes = commandBytes; + // display on page emit sendCommandToDisplay(command); } diff --git a/DeviceHub/device/TimeSwitcher.cpp b/DeviceHub/device/TimeSwitcher.cpp index fcaeb61..40e7b35 100644 --- a/DeviceHub/device/TimeSwitcher.cpp +++ b/DeviceHub/device/TimeSwitcher.cpp @@ -121,8 +121,34 @@ void TimeSwitcher::sendDataToSerial(QByteArray data) { + TimeReplicatorCmdRepDto replyDto; + replyDto.devCode = devCode; + replyDto.commandId = commandId; + replyDto.rawCommand = rawCommandBytes; + data.append(FRAME_TAIL); - this->serialUtil.sendData(data); + if (SettingConfig::getInstance().WORK_MODE == "real") + { + if (serialUtil.isOpen() == true) + { + this->serialUtil.sendData(data); + } else if (serialUtilB.isOpen() == true) + { + this->serialUtilB.sendData(data); + } else + { + replyDto.cmdStatus = 0; + replyDto.remarks = "serialport not avilable"; + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + kafkaProducer.produceMessage(SettingConfig::getInstance().KAFKA_CMDCB_TOPIC, + QString(QJsonDocument(replyDto.toJSON()).toJson(QJsonDocument::Compact))); + } + return; + } + } // 记录日志 // 0. 输出到日志文件中 @@ -133,22 +159,39 @@ 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); + + // reply success + replyDto.cmdStatus = 1; + + // 2. 解析后的json数据 + QString frameFilename = "frame_" + devCode + ".log"; + QString frameContent = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [reply] " + QJsonDocument(replyDto.toJSON()).toJson(QJsonDocument::Compact); + QLogUtil::writeChannelDataLogByDate(date, frameFilename, frameContent); + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + kafkaProducer.produceMessage(SettingConfig::getInstance().KAFKA_CMDCB_TOPIC, + QString(QJsonDocument(replyDto.toJSON()).toJson(QJsonDocument::Compact))); + } + + std::cout << content.toStdString() << std::endl; } void TimeSwitcher::commandReceivedHandler(QJsonObject command) { if (command.contains("deviceType") == false || command.value("deviceType").toString() != "05") { - 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; } + std::cout << QString(QJsonDocument(command).toJson(QJsonDocument::Compact)).toStdString() << std::endl; + // 记录日志 // 0. 输出到日志文件中 QDateTime now = QDateTime::currentDateTime(); @@ -156,18 +199,14 @@ // 1. 原始字节数组数据 QString filename = "frame_" + devCode + ".log"; - QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + command.find("cmdStr")->toString(); + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [recv] " + command.find("cmdStr")->toString(); QLogUtil::writeChannelDataLogByDate(date, filename, content); QByteArray commandBytes = protocol->generateSettingCommand(devCode, 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))); + this->commandId = command.value("commandId").toString(); + this->rawCommandBytes = commandBytes; + this->sendDataToSerial(commandBytes); // display on page emit sendCommandToDisplay(command); diff --git a/DeviceHub/TimeSwitcherForm.cpp b/DeviceHub/TimeSwitcherForm.cpp index 83278db..4d18f0c 100644 --- a/DeviceHub/TimeSwitcherForm.cpp +++ b/DeviceHub/TimeSwitcherForm.cpp @@ -19,7 +19,12 @@ // 获取设备对象 int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; TimeSwitcher * device = (TimeSwitcher *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("05").at(devIndex); - device->mockReceivData(); + + // mock测试工作模式则产生一条mock数据 + if (SettingConfig::getInstance().WORK_MODE == "mock") + { + device->mockReceivData(); + } } void TimeSwitcherForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) diff --git a/DeviceHub/common/utils/QSerialPortUtil.cpp b/DeviceHub/common/utils/QSerialPortUtil.cpp index a5e07ac..2331e2a 100644 --- a/DeviceHub/common/utils/QSerialPortUtil.cpp +++ b/DeviceHub/common/utils/QSerialPortUtil.cpp @@ -25,22 +25,16 @@ open = serial.open(QIODevice::ReadWrite); - std::cout << portName.toStdString() << " " << QString::number(baudRate).toStdString() << " " << open << std::endl; - if (open == true) { - // mock data received per second -// QTimer * timer = new QTimer(this); -// connect(timer, &QTimer::timeout, -// this, &QSerialPortUtil::mockReceivData); -// timer->start(1000 * 10); + std::cout << portName.toStdString() << " " << QString::number(baudRate).toStdString() << " " << open << std::endl; } } void QSerialPortUtil::sendData(QByteArray data) { - std::cout << data.toStdString() << std::endl; + std::cout << "[send]" << data.toStdString() << std::endl; if (this->open == true) { serial.write(data); @@ -50,7 +44,7 @@ void QSerialPortUtil::readData() { QByteArray buffer = serial.readAll(); - + std::cout << "[recv]" << buffer.toStdString() << std::endl; emit dataRecieved(buffer); } @@ -58,61 +52,3 @@ { return this->open; } -/* -void QSerialPortUtil::mockReceivData(QString portName) -{ - QByteArray buffer; - buffer.clear(); - - if (portName == "SignalGenerator") - { - - // signal generator - buffer.append("$GLN,0,192.168.000.126,255.255.255.000,192.168.000.001,192.168.001.126,255.255.255.000,192.168.001.001,255.255.255.255,3000,2000,2001*75").append("\r\n"); - buffer.append("$GLF,1,0,20210929,1,1,1,-0.01,20000,0,2,50,1*48").append("\r\n"); - buffer.append("$GPZDA,192157.00,01,01,2000,0,01*5C").append("\r\n"); - buffer.append("$GPMJD,192157.00,51544,0,01*73").append("\r\n"); - buffer.append("$GLC,0,0*48").append("\r\n"); - - } else if (portName == "FrequencyTuning") - { - // frequency tuning - buffer.append("$GLN,0,192.168.1.126,255.255.255.0,192.168.1.1,255.255.255.255,3000,2000,2001*71").append("\r\n"); - buffer.append("$GLF,0,0,0,0,00000,0,0,0,0,0,0,0,0,0,0,0,1900,2355,0,0,0,0,0,1,1,1*4C").append("\r\n"); - buffer.append("$GLP,0,1000,0,100,20000*45").append("\r\n"); - } else if (portName == "TimeSwitcher") - { - // time switcher - buffer.append("$GPZDA,211219.00,01,01,2000,0,01*5D").append("\r\n"); - buffer.append("$GLC,0*54").append("\r\n"); - buffer.append("$GLF,1,1,1,010,100,100,000,000,0,-235136964.75,-235136959.89,0,0,0,5,50,-16390,-16710,-15660,-16700,-17300,11111,11111,11111*51").append("\r\n"); - buffer.append("$GLN,0,192.168.000.126,255.255.255.000,192.168.000.001,192.168.001.126,255.255.255.000,192.168.001.001,255.255.255.255,3000,2000,2001*75").append("\r\n"); - } else if (portName == "FreqSwitcher") - { - // freq switcher - buffer.append("$GLF,0,4,0,0,22000,-745,-776,0,0,0,0,0,100,100,0,0,0,1,00000,111111,111111*54").append("\r\n"); - buffer.append("$GLC,0*54").append("\r\n"); - buffer.append("$GLN,0,192.168.000.123,255.255.255.000,192.168.000.001,192.168.001.123,255.255.255.000,192.168.001.001,255.255.255.255,3000,2000,2001*75").append("\r\n"); - } else if (portName == "BCodeTerminal") - { - // b-code terminal - buffer.append("$2621304-20 210100112100f90210.035422*"); - buffer.append("$3e21304-20 2101001111304-20 2101001210801H.1.00S.1.030008432*"); - } else if (portName == "TimeReplicator") - { - // time replicator - buffer.append("$2B21308-13 21010012200000000000000000faf0*"); - buffer.append("$3C21308-13 2101008220322222222111111110000000000000000ca24*"); - buffer.append("$3C21308-13 21010052207111111111111111111111111000000005984*"); - buffer.append("$3C21308-13 2101005121511111111111111111111111111111111bcfe*"); - buffer.append("$3E21308-13 2101001211308-13 2101001210929H.1.00S.1.00000292b*"); - } else if (portName == "FreqReplicator") - { - // freq replicator - buffer = QByteUtil::hexStringToBytes("AA550015010001000101010101010101010101010101010101EB"); - buffer.append(QByteUtil::hexStringToBytes("AA550015020001000101010101010101010101010101010101E8")); - } - - emit dataRecieved(buffer); -} -*/ diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index dc8d791..9c7a9bd 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -83,9 +83,3 @@ this->serialUtilB.openSerialPort(comNameList.at(1), this->baudRate); } } - -void DeviceBase::sendDataToSerial(QByteArray data) -{ - data.append(FRAME_TAIL); - this->serialUtil.sendData(data); -} diff --git a/DeviceHub/device/DeviceBase.h b/DeviceHub/device/DeviceBase.h index 666615a..0a6ef5d 100644 --- a/DeviceHub/device/DeviceBase.h +++ b/DeviceHub/device/DeviceBase.h @@ -28,7 +28,7 @@ void initSerialPort(); bool isSerialOpen(); - virtual void sendDataToSerial(QByteArray data); + virtual void sendDataToSerial(QByteArray data) = 0; virtual void afterFrameParse(DeviceFrameBaseDto * frameDto) = 0; virtual void afterCommandReply(DeviceFrameBaseDto * frameDto) = 0; @@ -45,6 +45,7 @@ QString devType; QString commandId; + QByteArray rawCommandBytes; QSerialPortUtil serialUtil; QSerialPortUtil serialUtilB; // serial port 2 diff --git a/DeviceHub/device/TimeReplicator.cpp b/DeviceHub/device/TimeReplicator.cpp index 2e22c67..8212c87 100644 --- a/DeviceHub/device/TimeReplicator.cpp +++ b/DeviceHub/device/TimeReplicator.cpp @@ -42,7 +42,7 @@ QTimer::singleShot(1000, this, [=](){ QByteArray buffer; buffer.clear(); - buffer.append("$3E21308-13 2101001211308-13 2101001210929H.1.00S.1.00000292b*").append("\r\n"); + buffer.append("$3C21308-13 2101005121511111111111111111111111111111111bcfe*").append("\r\n"); this->dataReceivedHandler(buffer); }); @@ -90,7 +90,6 @@ frameDto->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); frameDto->milisecond = now.toMSecsSinceEpoch(); frameDto->rawFrame = frameByte; - frameDto->devCode = devCode; if (frameType == TimeReplicatorProtocolBM::TIME_REPLICATOR_FRAME_TYPE::STATUS_FRAME) @@ -100,6 +99,7 @@ if (frameType == TimeReplicatorProtocolBM::TIME_REPLICATOR_FRAME_TYPE::CMDREP_FRAME) { + frameDto->rawCommand = rawCommandBytes; frameDto->commandId = commandId; this->afterCommandReply(frameDto); } @@ -116,9 +116,6 @@ void TimeReplicator::afterFrameParse(DeviceFrameBaseDto * frameDto) { - std::cout << "frame type: " << typeid(* frameDto).name() << std::endl; - std::cout << frameDto->rawFrame.toStdString() << std::endl; - // 0. 输出到日志文件中 QString date = frameDto->timestamp.mid(0, 10); @@ -157,7 +154,7 @@ // 2. 解析后的json数据 QString frameFilename = "frame_" + devCode + ".log"; - QString frameContent = frameDto->timestamp + " [recv] " + QJsonDocument(frameDto->toJSON()).toJson(QJsonDocument::Compact); + QString frameContent = frameDto->timestamp + " [reply] " + QJsonDocument(frameDto->toJSON()).toJson(QJsonDocument::Compact); QLogUtil::writeChannelDataLogByDate(date, frameFilename, frameContent); // 3. 输出到中间件,执行后续处理过程 @@ -171,7 +168,32 @@ void TimeReplicator::sendDataToSerial(QByteArray data) { data.append(FRAME_TAIL); - this->serialUtil.sendData(data); + if (SettingConfig::getInstance().WORK_MODE == "real") + { + if (serialUtil.isOpen() == true) + { + this->serialUtil.sendData(data); + } else if (serialUtilB.isOpen() == true) + { + this->serialUtilB.sendData(data); + } else + { + TimeReplicatorCmdRepDto replyDto; + replyDto.devCode = devCode; + replyDto.commandId = commandId; + replyDto.rawCommand = rawCommandBytes; + replyDto.cmdStatus = 0; + replyDto.remarks = "serialport not avilable"; + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + kafkaProducer.produceMessage(SettingConfig::getInstance().KAFKA_CMDCB_TOPIC, + QString(QJsonDocument(replyDto.toJSON()).toJson(QJsonDocument::Compact))); + } + return; + } + } // 记录日志 // 0. 输出到日志文件中 @@ -182,22 +204,24 @@ 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); + + std::cout << content.toStdString() << std::endl; } void TimeReplicator::commandReceivedHandler(QJsonObject command) { if (command.contains("deviceType") == false || command.value("deviceType").toString() != "09") { - std::cout << "device type [" << command.value("deviceType").toString().toStdString() << "] not matched. return" << std::endl; return; } if (command.contains("deviceId") == false || command.value("deviceId").toString() != deviceId) { - std::cout << "deviceId [" << command.value("deviceId").toString().toStdString() << "] not matched. return" << std::endl; return; } + std::cout << QString(QJsonDocument(command).toJson(QJsonDocument::Compact)).toStdString() << std::endl; + // 记录日志 // 0. 输出到日志文件中 QDateTime now = QDateTime::currentDateTime(); @@ -205,20 +229,15 @@ // 1. 原始字节数组数据 QString filename = "frame_" + devCode + ".log"; - QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + command.find("cmdStr")->toString(); + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [recv] " + command.find("cmdStr")->toString(); QLogUtil::writeChannelDataLogByDate(date, filename, content); QByteArray commandBytes = protocol->generateSettingCommand(devCode, command.value("command").toString(), command.value("params").toString()); this->sendDataToSerial(commandBytes); command.insert("rawCommand", QString::fromUtf8(commandBytes)); this->commandId = command.value("commandId").toString(); -/* - 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))); -*/ + this->rawCommandBytes = commandBytes; + // display on page emit sendCommandToDisplay(command); } diff --git a/DeviceHub/device/TimeSwitcher.cpp b/DeviceHub/device/TimeSwitcher.cpp index fcaeb61..40e7b35 100644 --- a/DeviceHub/device/TimeSwitcher.cpp +++ b/DeviceHub/device/TimeSwitcher.cpp @@ -121,8 +121,34 @@ void TimeSwitcher::sendDataToSerial(QByteArray data) { + TimeReplicatorCmdRepDto replyDto; + replyDto.devCode = devCode; + replyDto.commandId = commandId; + replyDto.rawCommand = rawCommandBytes; + data.append(FRAME_TAIL); - this->serialUtil.sendData(data); + if (SettingConfig::getInstance().WORK_MODE == "real") + { + if (serialUtil.isOpen() == true) + { + this->serialUtil.sendData(data); + } else if (serialUtilB.isOpen() == true) + { + this->serialUtilB.sendData(data); + } else + { + replyDto.cmdStatus = 0; + replyDto.remarks = "serialport not avilable"; + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + kafkaProducer.produceMessage(SettingConfig::getInstance().KAFKA_CMDCB_TOPIC, + QString(QJsonDocument(replyDto.toJSON()).toJson(QJsonDocument::Compact))); + } + return; + } + } // 记录日志 // 0. 输出到日志文件中 @@ -133,22 +159,39 @@ 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); + + // reply success + replyDto.cmdStatus = 1; + + // 2. 解析后的json数据 + QString frameFilename = "frame_" + devCode + ".log"; + QString frameContent = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [reply] " + QJsonDocument(replyDto.toJSON()).toJson(QJsonDocument::Compact); + QLogUtil::writeChannelDataLogByDate(date, frameFilename, frameContent); + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + kafkaProducer.produceMessage(SettingConfig::getInstance().KAFKA_CMDCB_TOPIC, + QString(QJsonDocument(replyDto.toJSON()).toJson(QJsonDocument::Compact))); + } + + std::cout << content.toStdString() << std::endl; } void TimeSwitcher::commandReceivedHandler(QJsonObject command) { if (command.contains("deviceType") == false || command.value("deviceType").toString() != "05") { - 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; } + std::cout << QString(QJsonDocument(command).toJson(QJsonDocument::Compact)).toStdString() << std::endl; + // 记录日志 // 0. 输出到日志文件中 QDateTime now = QDateTime::currentDateTime(); @@ -156,18 +199,14 @@ // 1. 原始字节数组数据 QString filename = "frame_" + devCode + ".log"; - QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + command.find("cmdStr")->toString(); + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [recv] " + command.find("cmdStr")->toString(); QLogUtil::writeChannelDataLogByDate(date, filename, content); QByteArray commandBytes = protocol->generateSettingCommand(devCode, 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))); + this->commandId = command.value("commandId").toString(); + this->rawCommandBytes = commandBytes; + this->sendDataToSerial(commandBytes); // display on page emit sendCommandToDisplay(command); diff --git a/DeviceHub/protocol/dto/DeviceFrameBaseDto.h b/DeviceHub/protocol/dto/DeviceFrameBaseDto.h index 02457ce..d222cde 100644 --- a/DeviceHub/protocol/dto/DeviceFrameBaseDto.h +++ b/DeviceHub/protocol/dto/DeviceFrameBaseDto.h @@ -13,6 +13,7 @@ explicit DeviceFrameBaseDto(QObject *parent = nullptr) {} QByteArray rawFrame; // 原始帧字节数组 + QByteArray rawCommand; QString timestamp; // 时间戳字符串 qlonglong milisecond; // 毫秒计数 diff --git a/DeviceHub/TimeSwitcherForm.cpp b/DeviceHub/TimeSwitcherForm.cpp index 83278db..4d18f0c 100644 --- a/DeviceHub/TimeSwitcherForm.cpp +++ b/DeviceHub/TimeSwitcherForm.cpp @@ -19,7 +19,12 @@ // 获取设备对象 int devIndex = ((DeviceHubWindow *) this->parent()->parent())->currentDevIndex; TimeSwitcher * device = (TimeSwitcher *) ((DeviceHubWindow *) this->parent()->parent())->allTypeDevList.value("05").at(devIndex); - device->mockReceivData(); + + // mock测试工作模式则产生一条mock数据 + if (SettingConfig::getInstance().WORK_MODE == "mock") + { + device->mockReceivData(); + } } void TimeSwitcherForm::drawDeviceFrameOnForm(DeviceFrameBaseDto * frameData) diff --git a/DeviceHub/common/utils/QSerialPortUtil.cpp b/DeviceHub/common/utils/QSerialPortUtil.cpp index a5e07ac..2331e2a 100644 --- a/DeviceHub/common/utils/QSerialPortUtil.cpp +++ b/DeviceHub/common/utils/QSerialPortUtil.cpp @@ -25,22 +25,16 @@ open = serial.open(QIODevice::ReadWrite); - std::cout << portName.toStdString() << " " << QString::number(baudRate).toStdString() << " " << open << std::endl; - if (open == true) { - // mock data received per second -// QTimer * timer = new QTimer(this); -// connect(timer, &QTimer::timeout, -// this, &QSerialPortUtil::mockReceivData); -// timer->start(1000 * 10); + std::cout << portName.toStdString() << " " << QString::number(baudRate).toStdString() << " " << open << std::endl; } } void QSerialPortUtil::sendData(QByteArray data) { - std::cout << data.toStdString() << std::endl; + std::cout << "[send]" << data.toStdString() << std::endl; if (this->open == true) { serial.write(data); @@ -50,7 +44,7 @@ void QSerialPortUtil::readData() { QByteArray buffer = serial.readAll(); - + std::cout << "[recv]" << buffer.toStdString() << std::endl; emit dataRecieved(buffer); } @@ -58,61 +52,3 @@ { return this->open; } -/* -void QSerialPortUtil::mockReceivData(QString portName) -{ - QByteArray buffer; - buffer.clear(); - - if (portName == "SignalGenerator") - { - - // signal generator - buffer.append("$GLN,0,192.168.000.126,255.255.255.000,192.168.000.001,192.168.001.126,255.255.255.000,192.168.001.001,255.255.255.255,3000,2000,2001*75").append("\r\n"); - buffer.append("$GLF,1,0,20210929,1,1,1,-0.01,20000,0,2,50,1*48").append("\r\n"); - buffer.append("$GPZDA,192157.00,01,01,2000,0,01*5C").append("\r\n"); - buffer.append("$GPMJD,192157.00,51544,0,01*73").append("\r\n"); - buffer.append("$GLC,0,0*48").append("\r\n"); - - } else if (portName == "FrequencyTuning") - { - // frequency tuning - buffer.append("$GLN,0,192.168.1.126,255.255.255.0,192.168.1.1,255.255.255.255,3000,2000,2001*71").append("\r\n"); - buffer.append("$GLF,0,0,0,0,00000,0,0,0,0,0,0,0,0,0,0,0,1900,2355,0,0,0,0,0,1,1,1*4C").append("\r\n"); - buffer.append("$GLP,0,1000,0,100,20000*45").append("\r\n"); - } else if (portName == "TimeSwitcher") - { - // time switcher - buffer.append("$GPZDA,211219.00,01,01,2000,0,01*5D").append("\r\n"); - buffer.append("$GLC,0*54").append("\r\n"); - buffer.append("$GLF,1,1,1,010,100,100,000,000,0,-235136964.75,-235136959.89,0,0,0,5,50,-16390,-16710,-15660,-16700,-17300,11111,11111,11111*51").append("\r\n"); - buffer.append("$GLN,0,192.168.000.126,255.255.255.000,192.168.000.001,192.168.001.126,255.255.255.000,192.168.001.001,255.255.255.255,3000,2000,2001*75").append("\r\n"); - } else if (portName == "FreqSwitcher") - { - // freq switcher - buffer.append("$GLF,0,4,0,0,22000,-745,-776,0,0,0,0,0,100,100,0,0,0,1,00000,111111,111111*54").append("\r\n"); - buffer.append("$GLC,0*54").append("\r\n"); - buffer.append("$GLN,0,192.168.000.123,255.255.255.000,192.168.000.001,192.168.001.123,255.255.255.000,192.168.001.001,255.255.255.255,3000,2000,2001*75").append("\r\n"); - } else if (portName == "BCodeTerminal") - { - // b-code terminal - buffer.append("$2621304-20 210100112100f90210.035422*"); - buffer.append("$3e21304-20 2101001111304-20 2101001210801H.1.00S.1.030008432*"); - } else if (portName == "TimeReplicator") - { - // time replicator - buffer.append("$2B21308-13 21010012200000000000000000faf0*"); - buffer.append("$3C21308-13 2101008220322222222111111110000000000000000ca24*"); - buffer.append("$3C21308-13 21010052207111111111111111111111111000000005984*"); - buffer.append("$3C21308-13 2101005121511111111111111111111111111111111bcfe*"); - buffer.append("$3E21308-13 2101001211308-13 2101001210929H.1.00S.1.00000292b*"); - } else if (portName == "FreqReplicator") - { - // freq replicator - buffer = QByteUtil::hexStringToBytes("AA550015010001000101010101010101010101010101010101EB"); - buffer.append(QByteUtil::hexStringToBytes("AA550015020001000101010101010101010101010101010101E8")); - } - - emit dataRecieved(buffer); -} -*/ diff --git a/DeviceHub/device/DeviceBase.cpp b/DeviceHub/device/DeviceBase.cpp index dc8d791..9c7a9bd 100644 --- a/DeviceHub/device/DeviceBase.cpp +++ b/DeviceHub/device/DeviceBase.cpp @@ -83,9 +83,3 @@ this->serialUtilB.openSerialPort(comNameList.at(1), this->baudRate); } } - -void DeviceBase::sendDataToSerial(QByteArray data) -{ - data.append(FRAME_TAIL); - this->serialUtil.sendData(data); -} diff --git a/DeviceHub/device/DeviceBase.h b/DeviceHub/device/DeviceBase.h index 666615a..0a6ef5d 100644 --- a/DeviceHub/device/DeviceBase.h +++ b/DeviceHub/device/DeviceBase.h @@ -28,7 +28,7 @@ void initSerialPort(); bool isSerialOpen(); - virtual void sendDataToSerial(QByteArray data); + virtual void sendDataToSerial(QByteArray data) = 0; virtual void afterFrameParse(DeviceFrameBaseDto * frameDto) = 0; virtual void afterCommandReply(DeviceFrameBaseDto * frameDto) = 0; @@ -45,6 +45,7 @@ QString devType; QString commandId; + QByteArray rawCommandBytes; QSerialPortUtil serialUtil; QSerialPortUtil serialUtilB; // serial port 2 diff --git a/DeviceHub/device/TimeReplicator.cpp b/DeviceHub/device/TimeReplicator.cpp index 2e22c67..8212c87 100644 --- a/DeviceHub/device/TimeReplicator.cpp +++ b/DeviceHub/device/TimeReplicator.cpp @@ -42,7 +42,7 @@ QTimer::singleShot(1000, this, [=](){ QByteArray buffer; buffer.clear(); - buffer.append("$3E21308-13 2101001211308-13 2101001210929H.1.00S.1.00000292b*").append("\r\n"); + buffer.append("$3C21308-13 2101005121511111111111111111111111111111111bcfe*").append("\r\n"); this->dataReceivedHandler(buffer); }); @@ -90,7 +90,6 @@ frameDto->timestamp = now.toString("yyyy-MM-dd HH:mm:ss.zzz"); frameDto->milisecond = now.toMSecsSinceEpoch(); frameDto->rawFrame = frameByte; - frameDto->devCode = devCode; if (frameType == TimeReplicatorProtocolBM::TIME_REPLICATOR_FRAME_TYPE::STATUS_FRAME) @@ -100,6 +99,7 @@ if (frameType == TimeReplicatorProtocolBM::TIME_REPLICATOR_FRAME_TYPE::CMDREP_FRAME) { + frameDto->rawCommand = rawCommandBytes; frameDto->commandId = commandId; this->afterCommandReply(frameDto); } @@ -116,9 +116,6 @@ void TimeReplicator::afterFrameParse(DeviceFrameBaseDto * frameDto) { - std::cout << "frame type: " << typeid(* frameDto).name() << std::endl; - std::cout << frameDto->rawFrame.toStdString() << std::endl; - // 0. 输出到日志文件中 QString date = frameDto->timestamp.mid(0, 10); @@ -157,7 +154,7 @@ // 2. 解析后的json数据 QString frameFilename = "frame_" + devCode + ".log"; - QString frameContent = frameDto->timestamp + " [recv] " + QJsonDocument(frameDto->toJSON()).toJson(QJsonDocument::Compact); + QString frameContent = frameDto->timestamp + " [reply] " + QJsonDocument(frameDto->toJSON()).toJson(QJsonDocument::Compact); QLogUtil::writeChannelDataLogByDate(date, frameFilename, frameContent); // 3. 输出到中间件,执行后续处理过程 @@ -171,7 +168,32 @@ void TimeReplicator::sendDataToSerial(QByteArray data) { data.append(FRAME_TAIL); - this->serialUtil.sendData(data); + if (SettingConfig::getInstance().WORK_MODE == "real") + { + if (serialUtil.isOpen() == true) + { + this->serialUtil.sendData(data); + } else if (serialUtilB.isOpen() == true) + { + this->serialUtilB.sendData(data); + } else + { + TimeReplicatorCmdRepDto replyDto; + replyDto.devCode = devCode; + replyDto.commandId = commandId; + replyDto.rawCommand = rawCommandBytes; + replyDto.cmdStatus = 0; + replyDto.remarks = "serialport not avilable"; + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + kafkaProducer.produceMessage(SettingConfig::getInstance().KAFKA_CMDCB_TOPIC, + QString(QJsonDocument(replyDto.toJSON()).toJson(QJsonDocument::Compact))); + } + return; + } + } // 记录日志 // 0. 输出到日志文件中 @@ -182,22 +204,24 @@ 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); + + std::cout << content.toStdString() << std::endl; } void TimeReplicator::commandReceivedHandler(QJsonObject command) { if (command.contains("deviceType") == false || command.value("deviceType").toString() != "09") { - std::cout << "device type [" << command.value("deviceType").toString().toStdString() << "] not matched. return" << std::endl; return; } if (command.contains("deviceId") == false || command.value("deviceId").toString() != deviceId) { - std::cout << "deviceId [" << command.value("deviceId").toString().toStdString() << "] not matched. return" << std::endl; return; } + std::cout << QString(QJsonDocument(command).toJson(QJsonDocument::Compact)).toStdString() << std::endl; + // 记录日志 // 0. 输出到日志文件中 QDateTime now = QDateTime::currentDateTime(); @@ -205,20 +229,15 @@ // 1. 原始字节数组数据 QString filename = "frame_" + devCode + ".log"; - QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + command.find("cmdStr")->toString(); + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [recv] " + command.find("cmdStr")->toString(); QLogUtil::writeChannelDataLogByDate(date, filename, content); QByteArray commandBytes = protocol->generateSettingCommand(devCode, command.value("command").toString(), command.value("params").toString()); this->sendDataToSerial(commandBytes); command.insert("rawCommand", QString::fromUtf8(commandBytes)); this->commandId = command.value("commandId").toString(); -/* - 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))); -*/ + this->rawCommandBytes = commandBytes; + // display on page emit sendCommandToDisplay(command); } diff --git a/DeviceHub/device/TimeSwitcher.cpp b/DeviceHub/device/TimeSwitcher.cpp index fcaeb61..40e7b35 100644 --- a/DeviceHub/device/TimeSwitcher.cpp +++ b/DeviceHub/device/TimeSwitcher.cpp @@ -121,8 +121,34 @@ void TimeSwitcher::sendDataToSerial(QByteArray data) { + TimeReplicatorCmdRepDto replyDto; + replyDto.devCode = devCode; + replyDto.commandId = commandId; + replyDto.rawCommand = rawCommandBytes; + data.append(FRAME_TAIL); - this->serialUtil.sendData(data); + if (SettingConfig::getInstance().WORK_MODE == "real") + { + if (serialUtil.isOpen() == true) + { + this->serialUtil.sendData(data); + } else if (serialUtilB.isOpen() == true) + { + this->serialUtilB.sendData(data); + } else + { + replyDto.cmdStatus = 0; + replyDto.remarks = "serialport not avilable"; + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + kafkaProducer.produceMessage(SettingConfig::getInstance().KAFKA_CMDCB_TOPIC, + QString(QJsonDocument(replyDto.toJSON()).toJson(QJsonDocument::Compact))); + } + return; + } + } // 记录日志 // 0. 输出到日志文件中 @@ -133,22 +159,39 @@ 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); + + // reply success + replyDto.cmdStatus = 1; + + // 2. 解析后的json数据 + QString frameFilename = "frame_" + devCode + ".log"; + QString frameContent = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [reply] " + QJsonDocument(replyDto.toJSON()).toJson(QJsonDocument::Compact); + QLogUtil::writeChannelDataLogByDate(date, frameFilename, frameContent); + + // 3. 输出到中间件,执行后续处理过程 + if (SettingConfig::getInstance().NEED_KAFKA == 1) + { + kafkaProducer.produceMessage(SettingConfig::getInstance().KAFKA_CMDCB_TOPIC, + QString(QJsonDocument(replyDto.toJSON()).toJson(QJsonDocument::Compact))); + } + + std::cout << content.toStdString() << std::endl; } void TimeSwitcher::commandReceivedHandler(QJsonObject command) { if (command.contains("deviceType") == false || command.value("deviceType").toString() != "05") { - 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; } + std::cout << QString(QJsonDocument(command).toJson(QJsonDocument::Compact)).toStdString() << std::endl; + // 记录日志 // 0. 输出到日志文件中 QDateTime now = QDateTime::currentDateTime(); @@ -156,18 +199,14 @@ // 1. 原始字节数组数据 QString filename = "frame_" + devCode + ".log"; - QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [send] " + command.find("cmdStr")->toString(); + QString content = now.toString("yyyy-MM-dd HH:mm:ss.zzz") + " [recv] " + command.find("cmdStr")->toString(); QLogUtil::writeChannelDataLogByDate(date, filename, content); QByteArray commandBytes = protocol->generateSettingCommand(devCode, 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))); + this->commandId = command.value("commandId").toString(); + this->rawCommandBytes = commandBytes; + this->sendDataToSerial(commandBytes); // display on page emit sendCommandToDisplay(command); diff --git a/DeviceHub/protocol/dto/DeviceFrameBaseDto.h b/DeviceHub/protocol/dto/DeviceFrameBaseDto.h index 02457ce..d222cde 100644 --- a/DeviceHub/protocol/dto/DeviceFrameBaseDto.h +++ b/DeviceHub/protocol/dto/DeviceFrameBaseDto.h @@ -13,6 +13,7 @@ explicit DeviceFrameBaseDto(QObject *parent = nullptr) {} QByteArray rawFrame; // 原始帧字节数组 + QByteArray rawCommand; QString timestamp; // 时间戳字符串 qlonglong milisecond; // 毫秒计数 diff --git a/DeviceHub/protocol/dto/TimeReplicatorCmdRepDto.cpp b/DeviceHub/protocol/dto/TimeReplicatorCmdRepDto.cpp index cb6a8ed..038f004 100644 --- a/DeviceHub/protocol/dto/TimeReplicatorCmdRepDto.cpp +++ b/DeviceHub/protocol/dto/TimeReplicatorCmdRepDto.cpp @@ -13,8 +13,9 @@ jsonObj.insert("commandId", this->commandId); if (this->cmdStatus != 1) { - jsonObj.insert("remarks", "failed"); + jsonObj.insert("remarks", remarks); } + jsonObj.insert("rawCommand", QString::fromUtf8(rawCommand)); return jsonObj; }