#include "ImportConfig.h" #include "ui_ImportConfig.h" #include #include #include "sqlitedb.h" CImportConfig::CImportConfig(QWidget *parent) : QWidget(parent), ui(new Ui::CImportConfig) { ui->setupUi(this); } CImportConfig::~CImportConfig() { delete ui; } void CImportConfig::on_pushButton_importpath_clicked() { QString dirpath = QFileDialog::getExistingDirectory(this, QStringLiteral("选择目录"), "./", QFileDialog::ShowDirsOnly); if(dirpath.isEmpty()) dirpath = QDir::currentPath(); ui->lineEdit_filepath->setText(dirpath + "/"); import_path = dirpath; } void CImportConfig::on_pushButton_confirm_clicked() { if(ui->lineEdit_filepath->text() == ""){ QMessageBox::warning(this,tr("提示"),tr("请选择导入文件路径!")); return; } QString copy_path = QCoreApplication::applicationDirPath() + "\\config\\copy\\"; QDir copyDir(copy_path); // 创建目标文件夹 if (!copyDir.mkpath(".")) { qDebug() << "无法创建目标文件夹"; return; } copyDirectory(import_path, copy_path); QFile fileMAC(copy_path+"macbackup"); if (!fileMAC.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "无法打开文件"; return; } QTextStream in(&fileMAC); QString oldMAC= ""; while (!in.atEnd()) { oldMAC = in.readLine(); qDebug() << oldMAC; } fileMAC.close(); QFile sourceFile(copy_path + "config.db"); // if (!sourceFile.copy(copy_path + "\\config.db")) { // QMessageBox::question(NULL, "提示", "导入数据库失败!"); // return; // } qDebug() << QSqlDatabase::drivers() << "\r\n"; QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(copy_path + "config.db"); if (!db.open()) { qDebug() << "Error: Failed to connect database." << database.lastError(); return ; } QString strSql = QString("update t_ChannelSetting SET channelId = '%1' || SUBSTR(channelId, 13),dataWatchNo='%2';").arg(MAC).arg(MAC); ExeSqlData(strSql); strSql = QString("update t_ChannelSetting SET pairChannelId = '%1' || SUBSTR(channelId, 13) where pairChannelId <> '' AND pairChannelId <> 'NONE' ;").arg(MAC); ExeSqlData(strSql); strSql = QString("update t_ChannelSetting SET speedRefChannelId = '%1' || SUBSTR(channelId, 13) where speedRefChannelId <> '' AND pairChannelId <> 'NONE';").arg(MAC); ExeSqlData(strSql); strSql = QString("update t_DeviceInfo SET MAC = '%1' ;").arg(MAC); ExeSqlData(strSql); strSql = QString("update t_TriggerConfig SET ChannelID = '%1' || SUBSTR(ChannelID, 13) ;").arg(MAC); ExeSqlData(strSql); strSql = QString("update t_WorkConditionChannels SET ChannelId = '%1' || SUBSTR(ChannelId, 13) where ChannelId <> '';").arg(MAC); ExeSqlData(strSql); strSql = QString("update t_WorkConditionRules SET ChannelId = '%1' || SUBSTR(ChannelId, 13) ;").arg(MAC); ExeSqlData(strSql); strSql = QString("update t_UnitConfiguration SET ChannelId = '%1' || SUBSTR(ChannelId, 13) ;").arg(MAC); ExeSqlData(strSql); db.close(); QString fileName = copy_path + "Graph1.json"; QString value; QFile file(fileName); file.open(QIODevice::ReadWrite | QIODevice::Text); value = file.readAll(); file.resize(0); file.write(value.toUtf8()); value.replace(oldMAC,MAC); value = file.readAll(); file.close(); QString fileName2 = copy_path + "Graph2.json"; QFile file2(fileName2); file2.open(QIODevice::ReadWrite | QIODevice::Text); value = file2.readAll(); file2.resize(0); value.replace(oldMAC,MAC); file2.write(value.toUtf8()); file2.close(); QString ChannelSettings = copy_path + "ChannelSettings.json"; QFile fileChannelSettings(ChannelSettings); fileChannelSettings.open(QIODevice::ReadWrite | QIODevice::Text); value = fileChannelSettings.readAll(); fileChannelSettings.resize(0); value.replace(oldMAC,MAC); fileChannelSettings.write(value.toUtf8()); fileChannelSettings.close(); QString UnitConfigurations = copy_path + "UnitConfigurations.json"; QFile fileUnitConfigurations(UnitConfigurations); fileUnitConfigurations.open(QIODevice::ReadWrite | QIODevice::Text); value = fileUnitConfigurations.readAll(); fileUnitConfigurations.resize(0); value.replace(oldMAC,MAC); fileUnitConfigurations.write(value.toUtf8()); fileUnitConfigurations.close(); QString TriggerSettings = copy_path + "TriggerSettings.json"; QFile fileTriggerSettings(TriggerSettings); fileTriggerSettings.open(QIODevice::ReadWrite | QIODevice::Text); value = fileTriggerSettings.readAll(); fileTriggerSettings.resize(0); value.replace(oldMAC,MAC); fileTriggerSettings.write(value.toUtf8()); fileTriggerSettings.close(); copyFile(QCoreApplication::applicationDirPath() + "\\config\\" + "UnitWorkConditionsInfo.json", copy_path + "UnitWorkConditionsInfo.json"); copyFile(QCoreApplication::applicationDirPath() + "\\config\\" + "UnitParameters.json", copy_path + "UnitParameters.json"); copyFile(QCoreApplication::applicationDirPath() + "\\config\\" + "ServerConfig.json", copy_path + "ServerConfig.json"); copyFile(QCoreApplication::applicationDirPath() + "\\config\\" + "NetWorkConfig.json", copy_path + "NetWorkConfig.json"); copyFile(QCoreApplication::applicationDirPath() + "\\config\\" + "ModbusConfig.json", copy_path + "ModbusConfig.json"); copyFile(QCoreApplication::applicationDirPath() + "\\config\\" + "TcpConfig.json", copy_path + "TcpConfig.json"); copyFile(QCoreApplication::applicationDirPath() + "\\config\\" + "UnitBoardsInfo.json", copy_path + "UnitBoardsInfo.json"); copyFile(QCoreApplication::applicationDirPath() + "\\config\\" + "ZeroDrift.json", copy_path + "ZeroDrift.json"); copyFile(QCoreApplication::applicationDirPath() + "\\config\\" + "UnitBoardsInfo.json", copy_path + "UnitBoardsInfo.json"); copyFile(QCoreApplication::applicationDirPath() + "\\config\\" + "ConfidenceDegree.json", copy_path + "ConfidenceDegree.json"); copyFile(QCoreApplication::applicationDirPath() + "\\config\\" + "TriggerConfig.json", copy_path + "TriggerConfig.json"); copyFile(QCoreApplication::applicationDirPath() + "\\config\\" + "config.ini", copy_path + "config.ini"); qApp->exit(0); g_SqliteDB->CloseDataBase(); copyDirectory(copy_path, QCoreApplication::applicationDirPath() + "\\config\\"); QMessageBox::information(this,tr("导入"),tr("导入成功")); // 如果目标文件夹已存在,则删除它 QDir copyDir2(copy_path); if (copyDir2.exists()) { copyDir2.removeRecursively(); } QProcess::startDetached(qApp->applicationFilePath(), QStringList()); } int CImportConfig::ExeSqlData(QString& strSql) { QSqlQuery sql_query; int iRet = -1; qDebug() << "strSql" << strSql << endl; if(!sql_query.exec(strSql)) { qDebug() << sql_query.lastError(); } else { while(sql_query.next()) { iRet = sql_query.value(0).toInt(); } } return iRet; } bool CImportConfig::copyFile(const QString &sourceFile, const QString &destinationFile) { QFile srcFile(sourceFile); QFile dstFile(destinationFile); if (!srcFile.exists()) { qDebug() << "Source file does not exist:" << sourceFile; return false; } if (!srcFile.open(QIODevice::ReadOnly)) { qDebug() << "Unable to open source file for reading:" << sourceFile; return false; } if (!dstFile.open(QIODevice::WriteOnly)) { srcFile.close(); qDebug() << "Unable to open destination file for writing:" << destinationFile; return false; } dstFile.write(srcFile.readAll()); srcFile.close(); dstFile.close(); return true; } bool CImportConfig::copyDirectory(const QString &sourceDir, const QString &destinationDir) { QDir srcDir(sourceDir); QDir destDir(destinationDir); // 复制文件 foreach (const QFileInfo &fileInfo, srcDir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot)) { QString fileName = fileInfo.fileName(); QString srcFilePath = srcDir.absoluteFilePath(fileName); QString destFilePath = destDir.absoluteFilePath(fileName); if (fileInfo.isDir()) { // 递归复制子文件夹 if (!copyDirectory(srcFilePath, destFilePath)) { return false; } } else { // 复制文件 QFile dest(destFilePath); if (dest.exists()) { bool re = dest.remove(); // 如果目标文件存在,先删除它 if(!re){ qDebug() << "删除失败 "<