diff --git a/Cigarette/common.h b/Cigarette/common.h index c784730..0b85862 100644 --- a/Cigarette/common.h +++ b/Cigarette/common.h @@ -8,6 +8,7 @@ //#define __DEBUG //debug信息输出功能 //#define __UDPSend //网络发送功能 //#define __TCPSend // TCP发送 +#define __ExportData // FTP发送 #define USB_BASLER_NEW_FW //使用basler定制固件 //#define IMM_PROCESS //拍照后立马处理,不等校验信号 //#define IMM_FEED_BACK //处理完后立马反馈,不等校验信号 diff --git a/Cigarette/exportData.cpp b/Cigarette/exportData.cpp index 2a3644a..40565d9 100644 --- a/Cigarette/exportData.cpp +++ b/Cigarette/exportData.cpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #include //#include @@ -48,6 +50,8 @@ ExportDataThread::ExportDataThread(QObject* parent) : QThread(parent) qDebug() << "First connect FTP failed because " << GetLastError(); */ readyToSendZip = false; + listToZip.clear(); + tmpListToZip.clear(); } @@ -103,8 +107,8 @@ void ExportDataThread::stop() b_quit = true; _XMLExportDataInfo data; export_XMLData_Info_queue->put(data); - InternetCloseHandle(hftp); - InternetCloseHandle(hint); + //InternetCloseHandle(hftp); + //InternetCloseHandle(hint); } bool ExportDataThread::ConnectFtp(string *ip_, int *port_, string *username_, string *userpwd_) { @@ -117,12 +121,20 @@ bool ExportDataThread::ConnectFtp(string *ip_, int *port_, string *username_, st //string userName = "FTP2"; ///* 密码 */ //string pwd = "123"; - cout << "222ftp ip =" << ip_ << "| prot =" << *port_ << endl; - cout << "222username =" << username_ << "| pwd =" << userpwd_ << endl; + cout << "222ftp ip =" << *ip_ << "| prot =" << *port_ << endl; + cout << "222username =" << *username_ << "| pwd =" << *userpwd_ << endl; if (hftp != NULL) { InternetCloseHandle(hftp); hftp = NULL; } + + if (hint == NULL) { + hint = InternetOpen(0, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0); + if (hint == NULL) { + qDebug() << "Faile to open internet"; + return false; + } + } // 创建ftp连接 // hftp = InternetConnectA(hint, ftpServer.c_str(), port, userName.c_str(), pwd.c_str(), INTERNET_SERVICE_FTP, 0, 0); hftp = InternetConnectA(hint, ip_->c_str(), *port_, username_->c_str(), userpwd_->c_str(), INTERNET_SERVICE_FTP, 0, 0); @@ -285,36 +297,53 @@ int ExportDataThread::insertXMLNode(const char* xmlPath, _XMLExportDataInfo& dat void ExportDataThread::run() { - while (!b_quit) { - + while (!b_quit || (b_quit && (!export_XMLData_Info_queue->isEmpty() || readyToSendZip || !tmpListToZip.isEmpty()))) { _XMLExportDataInfo element; - export_XMLData_Info_queue->take(element); - - if (element.cameraId != -1 && flag) { - char buf[256]; - memset(buf, 0, 256); - QString xmlPath = QString(EXPORTDATA_FILE).arg(element.cameraId); - QString filePath = g_conf_path.config_path + "/" + xmlPath; - sprintf(buf, filePath.toLocal8Bit().constData(), element.cameraId); - insertXMLNode(buf, element); - QString remotePath = "/tmp/" + xmlPath; - ConnectServer(filePath, remotePath); + if (!export_XMLData_Info_queue->isEmpty()) { + export_XMLData_Info_queue->take(element); + + if (element.cameraId != -1 && flag) { + char buf[256]; + memset(buf, 0, 256); + QString xmlPath = QString(EXPORTDATA_FILE).arg(element.cameraId); + QString filePath = g_conf_path.config_path + "/" + xmlPath; + sprintf(buf, filePath.toLocal8Bit().constData(), element.cameraId); + insertXMLNode(buf, element); + QString remotePath = "/tmp/" + xmlPath; + ConnectServer(filePath, remotePath); + } + } + + if (b_quit && (!tmpListToZip.isEmpty())) { + listToZip += tmpListToZip; + tmpListToZip.clear(); + readyToSendZip = true; } if (readyToSendZip) { QString remotePath; QStringList zipFilePaths; - QString zipFilePath; - QString okStr = "ok"; + QStringList fileList; + QString okStr = "OK"; QProcess process(0); QDateTime now_ts = QDateTime::currentDateTime(); - - zipFilePaths.append(g_conf_path.save_pics_path + now_ts.toString("yyyy-MM-dd_HH-mm-ss_zzz_") + "ng.zip"); - zipFilePaths.append(g_conf_path.save_pics_path + now_ts.toString("yyyy-MM-dd_HH-mm-ss_zzz_") + "ok.zip"); - remotePath = listToZip[0].mid(listToZip[0].indexOf("image") - 1); QStringList argsNg; QStringList argsOk; + zipFilePaths.append(g_conf_path.save_pics_path + "/" + now_ts.toString("yyyy-MM-dd_HH-mm-ss_") + "ng.zip"); + zipFilePaths.append(g_conf_path.save_pics_path + "/" + now_ts.toString("yyyy-MM-dd_HH-mm-ss_") + "ok.zip"); + fileList.append(g_conf_path.save_pics_path + "/" + now_ts.toString("yyyy-MM-dd_HH-mm-ss_") + "ng.txt"); + fileList.append(g_conf_path.save_pics_path + "/" + now_ts.toString("yyyy-MM-dd_HH-mm-ss_") + "ok.txt"); + + QFile fileNg(fileList[0]); + QFile fileOk(fileList[1]); + + fileNg.open(QIODevice::WriteOnly); + fileOk.open(QIODevice::WriteOnly); + + QTextStream ngStream(&fileNg); + QTextStream okStream(&fileOk); + argsNg.append("a"); argsNg.append(zipFilePaths[0]); @@ -326,11 +355,11 @@ void ExportDataThread::run() strList = strList[strList.size() - 1].split("."); int index = listToZip[i].lastIndexOf("/\\", 0); if (strList[0] == okStr) { - argsOk.append(listToZip[i]); + okStream << listToZip[i] << endl; } else { - argsNg.append(listToZip[i]); + ngStream << listToZip[i] << endl; } /* QDir dir; @@ -340,16 +369,29 @@ void ExportDataThread::run() } */ } + argsNg.append("-i@" + fileList[0]); argsNg.append("-mx=3"); + argsOk.append("-i@" + fileList[1]); argsOk.append("-mx=3"); + fileNg.close(); + fileOk.close(); + for (int i = 0; i < zipFilePaths.size(); i++) { process.start(QApplication::applicationDirPath() + "/7z.exe", i == 0 ? argsNg : argsOk); process.waitForStarted(); process.waitForFinished(); + //QString out = QString::fromLocal8Bit(process.readAllStandardOutput()); + //qDebug() << out; + remotePath = zipFilePaths[i].mid(zipFilePaths[i].indexOf("image") - 1); + qDebug() << remotePath; ConnectServer(zipFilePaths[i], remotePath); + QFile file(zipFilePaths[i]); + file.remove(); } + fileNg.remove(); + fileOk.remove(); readyToSendZip = false; } } @@ -358,6 +400,11 @@ void ExportDataThread::run() for (int index = 0; index < NumberOfSupportedCameras; index++) { pDocument[index]->~XMLDocument(); } + + InternetCloseHandle(hftp); + InternetCloseHandle(hint); + hftp = NULL; + hint = NULL; } //*****************************************已经调试好,没有理清逻辑前不要动********************** @@ -434,18 +481,72 @@ void ExportDataThread::ConnectServer(QString srcPath, QString destPath) { } } +/* +bool compression_zip_file(const QString& selectFile2DirPath, const QString& savePath) +{ + if (selectFile2DirPath.isEmpty() || savePath.isEmpty()) + { + return false; + } + if (!QFile::exists(selectFile2DirPath) || !QFileInfo(savePath).isDir()) + { + return false; + } + + if (QFileInfo(selectFile2DirPath).isFile())///压缩的是一个文件 + { + QString fileName = QFileInfo(selectFile2DirPath).baseName(); + QString writerFilePath = savePath + "/" + fileName + ".zip"; + + QFile selectFile(selectFile2DirPath); + qint64 size = selectFile.size() / 1024 / 1024; + if (!selectFile.open(QIODevice::ReadOnly) || size > FILE_MAX_SIZE) + { + ///打开文件失败,或者大于1GB导致无法压缩的文件 + return false; + } + QString addFileName = QFileInfo(selectFile2DirPath).fileName(); + QZipWriter writer(writerFilePath); + writer.addFile(addFileName, selectFile.readAll()); + selectFile.close(); + return true; + } + else///压缩的是一个文件夹 + { + QString zipRootFolder = selectFile2DirPath.mid(selectFile2DirPath.lastIndexOf("/") + 1); + QString selectDirUpDir = selectFile2DirPath.left(selectFile2DirPath.lastIndexOf("/")); + QString saveFilePath = savePath + "/" + zipRootFolder + ".zip"; + + QZipWriter writer(saveFilePath); + writer.addDirectory(zipRootFolder); + QFileInfoList fileList = ergodic_compression_file(&writer, selectDirUpDir, selectFile2DirPath); + writer.close(); + if (0 == fileList.size()) + return true; + return false; + } +} +*/ + + void ExportDataThread::GetDataFromSaveThread(QString filePath) { QString remotePath; if (timer->isActive()) { - tmpListToZip.append(filePath); + if ((filePath.indexOf("ng_result") != -1) || (filePath.indexOf("ng") == -1)) { + tmpListToZip.append(filePath); + } } else { listToZip = tmpListToZip; tmpListToZip.clear(); - tmpListToZip.append(filePath); + if ((filePath.indexOf("ng_result") != -1) || (filePath.indexOf("ng") == -1)) { + tmpListToZip.append(filePath); + } - readyToSendZip = true; + if (!listToZip.isEmpty()) { + readyToSendZip = true; + } //remotePath = filePath.mid(filePath.indexOf("image") - 1); //ConnectServer(filePath, remotePath); timer->start(1000 * 60 * 5); // 5min