更新数据上传

This commit is contained in:
Mr.V 2024-04-04 01:51:29 +08:00
parent 556c025d88
commit 0be507724f
4 changed files with 104 additions and 51 deletions

View File

@ -89,8 +89,10 @@ void TcpClient::connectToServer(QString ip, int port) {
{ {
// 来车 // 来车
emit this->comeTrainSignals(true); emit this->comeTrainSignals(true);
if (jsonObject["direction"].toInt(0) != 0) emit this->getDirectionSignals(jsonObject["direction"].toInt(0));
come_time = jsonObject["cometime"].toString(); come_time = jsonObject["cometime"].toString();
carType = jsonObject["type"].toString(); carType = jsonObject["type"].toString();
// direction = jsonObject["direction"].toBool(false);
} }
else if (jsonObject["type"].toString() != carType && carType == "1") else if (jsonObject["type"].toString() != carType && carType == "1")
{ {
@ -99,6 +101,12 @@ void TcpClient::connectToServer(QString ip, int port) {
come_time = jsonObject["cometime"].toString(); come_time = jsonObject["cometime"].toString();
carType = jsonObject["type"].toString(); carType = jsonObject["type"].toString();
} }
if (jsonObject["type"].toString() == carType && jsonObject["direction"].toInt(0) != 0)
{
// 判断出火车方向
emit this->getDirectionSignals(jsonObject["direction"].toInt(0));
}
} }
} }
}); });

View File

@ -23,7 +23,7 @@ private:
int port_; int port_;
QString carType = "0"; QString carType = "0";
QString direction = "0"; bool direction = false;
QString come_time = ""; QString come_time = "";
bool exit_flag = false; // 退出标记 bool exit_flag = false; // 退出标记
@ -33,6 +33,7 @@ public slots:
signals: signals:
void comeTrainSignals(bool type); void comeTrainSignals(bool type);
void getDirectionSignals(int direction);
void sendTcpInfoSignals(QString info); void sendTcpInfoSignals(QString info);
void socketComplete(QTcpSocket* tcp, QString ip, quint16 port); // tcp句柄发送给主线程监管 void socketComplete(QTcpSocket* tcp, QString ip, quint16 port); // tcp句柄发送给主线程监管
void restartSocket(QString ip, int port); //重启Socket连接 void restartSocket(QString ip, int port); //重启Socket连接

View File

@ -126,7 +126,7 @@ MainWindow::MainWindow(QWidget *parent)
emit connect_socket(this->socketServerConfig_.server_ip, this->socketServerConfig_.server_port); emit connect_socket(this->socketServerConfig_.server_ip, this->socketServerConfig_.server_port);
connect(this->tcpClient, &TcpClient::comeTrainSignals, this, [=](bool type){ connect(this->tcpClient, &TcpClient::comeTrainSignals, this, [=](bool type) {
if (type) if (type)
{ {
this->logInfo("视频车号识别-来车了"); this->logInfo("视频车号识别-来车了");
@ -141,9 +141,53 @@ MainWindow::MainWindow(QWidget *parent)
emit IdentifyType(); emit IdentifyType();
} }
}); });
connect(this->tcpClient, &TcpClient::getDirectionSignals, this, [=](int direction) {
if (this->iDirection != direction)
{
this->logInfo("获得方向判定:" + QString(direction > -1 ? (direction > 0 ? "需识别" : "待定") : "无需上传"));
this->iDirection = direction;
}
});
} }
ui->openComButton->click(); ui->openComButton->click();
// QThread *thread = new QThread;
// // 创建并移至线程的lambda表达式
// moveToThread(thread);
// connect(thread, &QThread::started, [=]() {
//// // 配置文件读取
//// QString errorMessage = "";
//// ai_matrix::BaseConfig baseConfig;
//// ai_matrix::InterfaceConfig interfaceConfig;
//// if (!ConfigUtil::readBaseConfig(this->configPath_, errorMessage, baseConfig))
//// {
//// this->logError(errorMessage);
//// }
//// if (!ConfigUtil::readInterfaceConfig(this->configPath_, errorMessage, interfaceConfig))
//// {
//// this->logError(errorMessage);
//// }
//
// while (true)
// {
//// if ((this->iDirection == 0 && this->queueTrainInfo_.size() <= 4) || this->iDirection < 0) continue;
// while (!this->queueTrainInfo_.isEmpty())
// {
// TrainInfo trainInfo_ = this->queueTrainInfo_.pop();
// if (!this->upWeb(trainInfo_))
// {
// //this->logError("第" + QString::fromStdString(trainInfo.strOrder) + "节,识别结果上传失败!");
// }
//
//// QThread::msleep(1000);
// }
// QThread::msleep(2000);
// }
// });
//
// connect(thread, &QThread::finished, thread, &QObject::deleteLater);
// thread->start();
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
@ -178,6 +222,7 @@ void MainWindow::initParam() {
this->needIdentify = false; this->needIdentify = false;
this->rnameRfidLog(); this->rnameRfidLog();
this->queue_.clear(); this->queue_.clear();
this->iDirection = 0;
} }
/** /**
@ -357,21 +402,17 @@ QStandardItem* MainWindow::getStandardItem(const QString &value)
* @return: :true * @return: :true
* :false * :false
*/ */
bool MainWindow::upWeb(std::string &carriageType, bool MainWindow::upWeb(TrainInfo &trainInfo)
std::string &carriageNumber,
std::string &carriageOrder,
std::string &time,
std::string &strRfidInfo)
{ {
try { try {
Json::Value arrayObj; //构建对象 Json::Value arrayObj; //构建对象
arrayObj["comeTime"] = time; arrayObj["comeTime"] = trainInfo.trainTime;
arrayObj["collectTime"] = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz").toStdString(); arrayObj["collectTime"] = trainInfo.collectTime;
arrayObj["carriageNumber"] = carriageNumber; arrayObj["carriageNumber"] = trainInfo.carriageNum;
arrayObj["carriageType"] = carriageType; arrayObj["carriageType"] = trainInfo.carriageType;
arrayObj["carriageOrder"] = carriageOrder; arrayObj["carriageOrder"] = trainInfo.strOrder;
arrayObj["steel"] = strRfidInfo; // arrayObj["steel"] = trainInfo.strRfidInfo;
Json::Value trainParams; Json::Value trainParams;
trainParams["poundNo"] = std::to_string(this->baseConfig_.trackName); trainParams["poundNo"] = std::to_string(this->baseConfig_.trackName);
@ -379,22 +420,23 @@ bool MainWindow::upWeb(std::string &carriageType,
Json::StreamWriterBuilder writer; Json::StreamWriterBuilder writer;
std::string str = Json::writeString(writer, arrayObj); std::string str = Json::writeString(writer, arrayObj);
this->logInfo("" + QString::fromStdString(carriageOrder) + "发送web: " + QString::fromStdString(str)); this->logInfo("" + QString::fromStdString(trainInfo.strOrder) + "发送web: " + QString::fromStdString(str));
httplib::Client cli(this->interfaceConfig_.httpIp.toStdString(), this->interfaceConfig_.httpPort); httplib::Client cli(this->interfaceConfig_.httpIp.toStdString(), this->interfaceConfig_.httpPort);
cli.set_connection_timeout(0, 300 * 1000);
cli.set_read_timeout(1,0); cli.set_connection_timeout(3, 0);
cli.set_read_timeout(0, 700 * 1000);
httplib::Headers header; httplib::Headers header;
httplib::Params params; httplib::Params params;
header.emplace("blade-auth", this->webToken); header.emplace("blade-auth", this->webToken);
//header.emplace("Content-Type", "application/json"); //header.emplace("Content-Type", "application/json");
auto res = cli.Post(this->interfaceConfig_.upResultPath.toStdString(), header, str, "application/json"); auto res = cli.Post(this->interfaceConfig_.upResultPath.toStdString(), header, str, "application/json");
if (res) if (res)
{ {
if (res->status == 200) if (res->status == 200)
{ {
this->logInfo("" + QString::fromStdString(carriageOrder) + "web返回: " + QString::fromStdString(res->body)); this->logInfo("" + QString::fromStdString(trainInfo.strOrder) + "web返回: " + QString::fromStdString(res->body));
Json::CharReaderBuilder readerBuilder; Json::CharReaderBuilder readerBuilder;
std::istringstream iss(res->body); std::istringstream iss(res->body);
Json::Value root; Json::Value root;
@ -410,37 +452,36 @@ bool MainWindow::upWeb(std::string &carriageType,
{ {
if (root["msg"].asString() == "请求未授权") { if (root["msg"].asString() == "请求未授权") {
this->logWarn("" + QString::fromStdString(carriageOrder) + "因请求未授权而上传识别结果失败重新请求token。"); this->logWarn("" + QString::fromStdString(trainInfo.strOrder) + "因请求未授权而上传识别结果失败重新请求token。");
if (!this->getToken()) return false; if (!this->getToken()) return false;
return this->upWeb(carriageType, carriageNumber, carriageOrder, time, strRfidInfo); return this->upWeb(trainInfo);
} }
this->logError("---"); this->logError("" + QString::fromStdString(trainInfo.strOrder) + "节,识别结果上传失败,原因:" + QString::fromStdString(root["msg"].asString()));
this->logError("" + QString::fromStdString(carriageOrder) + "节,识别结果上传失败,原因:" + QString::fromStdString(root["msg"].asString()));
} }
} }
else else
{ {
this->logError("" + QString::fromStdString(carriageOrder) + "识别结果上传失败返回数据解析异常返回数据非json" + QString::fromStdString(res->body)); this->logError("" + QString::fromStdString(trainInfo.strOrder) + "识别结果上传失败返回数据解析异常返回数据非json" + QString::fromStdString(res->body));
} }
} }
else else
{ {
this->logError("" + QString::fromStdString(carriageOrder) + "节,识别结果上传失败,原因:" + QString::number(res->status) + " - " + QString::fromStdString(res->body)); this->logError("" + QString::fromStdString(trainInfo.strOrder) + "节,识别结果上传失败,原因:" + QString::number(res->status) + " - " + QString::fromStdString(res->body));
if (res->status == 401) { if (res->status == 401) {
this->logWarn("因请求未授权而上传识别结果失败重新请求token。"); this->logWarn("因请求未授权而上传识别结果失败重新请求token。");
this->getToken(); this->getToken();
return this->upWeb(carriageType, carriageNumber, carriageOrder, time, strRfidInfo); return this->upWeb(trainInfo);
} }
} }
} }
else else
{ {
this->logError("" + QString::fromStdString(carriageOrder) + "节,上传数据失败,请检查网络,或者上传地址!"); this->logError("" + QString::fromStdString(trainInfo.strOrder) + "节,上传数据失败,请检查网络,或者上传地址!");
} }
} }
catch (std::exception &e) catch (std::exception &e)
{ {
this->logError("" + QString::fromStdString(carriageOrder) + "节,上传识别结果失败,原因:"); this->logError("" + QString::fromStdString(trainInfo.strOrder) + "节,上传识别结果失败,原因:");
this->logError(e.what()); this->logError(e.what());
} }
return false; return false;
@ -642,6 +683,7 @@ void MainWindow::openComClicked()
logs.append("串口打开成功\n"); logs.append("串口打开成功\n");
logs.append("串口号:" + ui->comBox->currentText() + "\n"); logs.append("串口号:" + ui->comBox->currentText() + "\n");
logs.append("波特率:" + ui->BTBox->currentText() + "\n"); logs.append("波特率:" + ui->BTBox->currentText() + "\n");
if (this->baseConfig_.havaMagnetSteel)
logs.append("磁钢顺序:" + this->baseConfig_.magnetSteelOrder); logs.append("磁钢顺序:" + this->baseConfig_.magnetSteelOrder);
this->isOpenCom = true; this->isOpenCom = true;
this->logInfo(logs); this->logInfo(logs);
@ -649,7 +691,7 @@ void MainWindow::openComClicked()
this->ui->LEDlabel->setStyleSheet("border-radius: 16px;\ " this->ui->LEDlabel->setStyleSheet("border-radius: 16px;\ "
"background-color: rgba(74, 221, 108, 225);\ " "background-color: rgba(74, 221, 108, 225);\ "
"border:1px solid rgba(168, 168, 168, 105);"); "border:1px solid rgba(168, 168, 168, 105);");
connect(this->serial_,&QSerialPort::readyRead,this,&MainWindow::readCom); connect(this->serial_,&QSerialPort::readyRead,this,&MainWindow::readCom); //
connect(this->serial_, &QSerialPort::errorOccurred, this, &MainWindow::serialPort_error); connect(this->serial_, &QSerialPort::errorOccurred, this, &MainWindow::serialPort_error);
@ -712,9 +754,13 @@ void MainWindow::readCom()
this->queue_.push(QString(data)); this->queue_.push(QString(data));
emit getRfid_signals(); emit getRfid_signals();
} }
this->serial_->clear(); // this->serial_->clear();
data.clear(); data.clear();
} }
else
{
this->logError("等待读取超时");
}
} catch(...) { } catch(...) {
this->logError("读取串口数据异常!"); this->logError("读取串口数据异常!");
} }
@ -844,25 +890,22 @@ void MainWindow::upRfid(QString rfidInfo)
trainInfo.carriageType = info.left(6).simplified().toStdString(); trainInfo.carriageType = info.left(6).simplified().toStdString();
trainInfo.carriageNum = info.mid(6, info.size() - 6).simplified().toStdString(); trainInfo.carriageNum = info.mid(6, info.size() - 6).simplified().toStdString();
trainInfo.strOrder = QString::number(order).toStdString(); trainInfo.strOrder = QString::number(order).toStdString();
trainInfo.nowTime = this->trainTime.toStdString(); trainInfo.trainTime = this->trainTime.toStdString();
trainInfo.collectTime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz").toStdString();
trainInfo.strRfidInfo = rfidInfo.toStdString(); trainInfo.strRfidInfo = rfidInfo.toStdString();
this->queueTrainInfo_.push(trainInfo); this->queueTrainInfo_.push(trainInfo);
auto up = QtConcurrent::run([=](){ // QtConcurrent::run([=](){
if ((this->iDirection == 0 && this->queueTrainInfo_.size() <= 4) || this->iDirection < 0) return;
while (!this->queueTrainInfo_.isEmpty()) while (!this->queueTrainInfo_.isEmpty())
{ {
TrainInfo trainInfo = this->queueTrainInfo_.pop(); TrainInfo trainInfo_ = this->queueTrainInfo_.pop();
if (!this->upWeb(trainInfo.carriageType, if (!this->upWeb(trainInfo_))
trainInfo.carriageNum,
trainInfo.strOrder,
trainInfo.nowTime,
trainInfo.strRfidInfo))
{ {
this->logError("" + QString::fromStdString(trainInfo.strOrder) + "节,识别结果上传失败!"); //this->logError("第" + QString::fromStdString(trainInfo.strOrder) + "节,识别结果上传失败!");
} }
} }
return true; // });
});
} }
void MainWindow::IdentifyTypeUpdate() { void MainWindow::IdentifyTypeUpdate() {

View File

@ -70,7 +70,8 @@ public:
void logInfo(const QString& message); void logInfo(const QString& message);
void logWarn(const QString& message); void logWarn(const QString& message);
void logError(const QString& message); void logError(const QString& message);
// 创建日志目录
static bool mkLogDir();
bool mkRfidLog(); bool mkRfidLog();
void deleteOldFiles(const QString &path, int days); void deleteOldFiles(const QString &path, int days);
bool rnameRfidLog(); bool rnameRfidLog();
@ -78,12 +79,7 @@ public:
void initParam(); void initParam();
bool upWeb(std::string &carriageType,
std::string &carriageNumber,
std::string &carriageOrder,
std::string &time,
std::string &rfidSourceInfo);
bool getToken();
QString getSystemTime(); QString getSystemTime();
@ -91,8 +87,7 @@ public:
QList<QString> getComList(); QList<QString> getComList();
// 将所有串口const 展示在下&拉列表 // 将所有串口const 展示在下&拉列表
void initComboBox(); void initComboBox();
// 创建日志目录
static bool mkLogDir();
// 获取QStandardItem // 获取QStandardItem
QStandardItemModel* getStandardItemModel(); QStandardItemModel* getStandardItemModel();
QStandardItem* getStandardItem(const QString &value); QStandardItem* getStandardItem(const QString &value);
@ -102,7 +97,8 @@ private:
std::string carriageType; std::string carriageType;
std::string carriageNum; std::string carriageNum;
std::string strOrder; std::string strOrder;
std::string nowTime; std::string trainTime;
std::string collectTime;
std::string strRfidInfo; std::string strRfidInfo;
}; };
QString trainTime; QString trainTime;
@ -139,11 +135,16 @@ private:
bool videoHasTrain = false; bool videoHasTrain = false;
// RFID识别有火车标志 // RFID识别有火车标志
bool rfidHasTrain = false; bool rfidHasTrain = false;
// 是否是需要识别的方向 -1:不识别 0:未确定 1:识别
int iDirection = 0;
// 获取当前时间 // 获取当前时间
QDateTime currentTime_ = QDateTime::currentDateTime(); QDateTime currentTime_ = QDateTime::currentDateTime();
TcpClient* tcpClient; TcpClient* tcpClient;
bool upWeb(TrainInfo &trainInfo);
bool getToken();
private: private:
QTcpSocket* tcp_ = nullptr; QTcpSocket* tcp_ = nullptr;
QStatusBar* statusBar; QStatusBar* statusBar;