335 lines
12 KiB
C++
335 lines
12 KiB
C++
#include "ImportConfig.h"
|
|
#include "ui_ImportConfig.h"
|
|
#include <QFileDialog>
|
|
#include <QProcess>
|
|
#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;
|
|
}
|
|
|
|
#ifdef Q_OS_WIN32
|
|
QString copy_path = QCoreApplication::applicationDirPath() + "\\config\\copy\\";
|
|
#endif
|
|
#ifdef Q_OS_LINUX
|
|
QString copy_path = QCoreApplication::applicationDirPath() + "/config/copy/";
|
|
#endif
|
|
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',IP = '%2' ;").arg(MAC).arg(IP);
|
|
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);
|
|
value.replace(oldMAC,MAC);
|
|
file.write(value.toUtf8());
|
|
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");
|
|
|
|
g_SqliteDB->CloseDataBase();
|
|
|
|
#ifdef Q_OS_WIN32
|
|
copyDirectory(copy_path, QCoreApplication::applicationDirPath() + "\\config\\");
|
|
#endif
|
|
#ifdef Q_OS_LINUX
|
|
copyDirectory(copy_path, QCoreApplication::applicationDirPath() + "/config/");
|
|
#endif
|
|
QMessageBox::information(this,tr("导入"),tr("导入成功"));
|
|
// 如果目标文件夹已存在,则删除它
|
|
QDir copyDir2(copy_path);
|
|
if (copyDir2.exists()) {
|
|
copyDir2.removeRecursively();
|
|
}
|
|
|
|
|
|
#ifdef Q_OS_WIN32
|
|
QString fileNameInfo = QCoreApplication::applicationDirPath() + "\\config\\UnitWorkConditionsInfo.json";
|
|
#endif
|
|
#ifdef Q_OS_LINUX
|
|
QString fileNameInfo = QCoreApplication::applicationDirPath() + "/config/UnitWorkConditionsInfo.json";
|
|
#endif
|
|
|
|
QString strURL = QString("ftp://%1/CIDW/qtconfig/%2").arg(IP).arg("UnitWorkConditionsInfo.json");
|
|
|
|
g_FtpClient->uploadFile(strURL,fileNameInfo,"UnitWorkConditionsInfo.json");
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN32
|
|
QString fileNameUnit = QCoreApplication::applicationDirPath() + "\\config\\UnitConfigurations.json";
|
|
#endif
|
|
#ifdef Q_OS_LINUX
|
|
QString fileNameUnit = QCoreApplication::applicationDirPath() + "/config/UnitConfigurations.json";
|
|
#endif
|
|
|
|
QString str2 = QString("ftp://%1/CIDW/qtconfig/%2").arg(IP).arg("UnitConfigurations.json");
|
|
|
|
g_FtpClient->uploadFile(str2,fileNameUnit,"UnitConfigurations.json");
|
|
#ifdef Q_OS_WIN32
|
|
QString fileNameUnit2 = QCoreApplication::applicationDirPath() + "\\config\\UnitConfigurations2.json";
|
|
#endif
|
|
#ifdef Q_OS_LINUX
|
|
QString fileNameUnit2 = QCoreApplication::applicationDirPath() + "/config/UnitConfigurations2.json";
|
|
#endif
|
|
|
|
QString str22 = QString("ftp://%1/CIDW/qtconfig/%2").arg(IP).arg("UnitConfigurations2.json");
|
|
|
|
g_FtpClient->uploadFile(str22,fileNameUnit2,"UnitConfigurations2.json");
|
|
|
|
#ifdef Q_OS_WIN32
|
|
QString fileNameTri = QCoreApplication::applicationDirPath() + "\\config\\TriggerSettings.json";
|
|
#endif
|
|
#ifdef Q_OS_LINUX
|
|
QString fileNameTri = QCoreApplication::applicationDirPath() + "/config/TriggerSettings.json";
|
|
#endif
|
|
|
|
QString str1 = QString("ftp://%1/CIDW/qtconfig/%2").arg(IP).arg("TriggerSettings.json");
|
|
|
|
g_FtpClient->uploadFile(str1,fileNameTri,"TriggerSettings.json");
|
|
|
|
#ifdef Q_OS_WIN32
|
|
QString fileNameTri2 = QCoreApplication::applicationDirPath() + "\\config\\TriggerSettings2.json";
|
|
#endif
|
|
#ifdef Q_OS_LINUX
|
|
QString fileNameTri2 = QCoreApplication::applicationDirPath() + "/config/TriggerSettings2.json";
|
|
#endif
|
|
|
|
QString strTri2 = QString("ftp://%1/CIDW/qtconfig/%2").arg(IP).arg("TriggerSettings2.json");
|
|
|
|
g_FtpClient->uploadFile(strTri2,fileNameTri2,"TriggerSettings2.json");
|
|
|
|
#ifdef Q_OS_WIN32
|
|
QString name = QCoreApplication::applicationDirPath() + "\\config\\UnitBoardsInfo.json";
|
|
#endif
|
|
#ifdef Q_OS_LINUX
|
|
QString name = QCoreApplication::applicationDirPath() + "/config/UnitBoardsInfo.json";
|
|
#endif
|
|
QString str = QString("ftp://%1/CIDW/qtconfig/%2").arg(IP).arg("UnitBoardsInfo.json");
|
|
|
|
g_FtpClient->uploadFile(str,name,"UnitBoardsInfo.json");
|
|
|
|
#ifdef Q_OS_WIN32
|
|
QString fileNameChannel = QCoreApplication::applicationDirPath() + "\\config\\ChannelSettings.json";
|
|
#endif
|
|
#ifdef Q_OS_LINUX
|
|
QString fileNameChannel = QCoreApplication::applicationDirPath() + "/config/ChannelSettings.json";
|
|
#endif
|
|
QString strChannelSettings = QString("ftp://%1/CIDW/qtconfig/%2").arg(IP).arg("ChannelSettings.json");
|
|
|
|
g_FtpClient->uploadFile(strChannelSettings,fileNameChannel,"ChannelSettings.json");
|
|
|
|
qApp->exit(0);
|
|
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);
|
|
//qDebug() << "srcFilePath" << srcFilePath << destFilePath <<endl;
|
|
if (fileInfo.isDir()) {
|
|
// 递归复制子文件夹
|
|
if (!copyDirectory(srcFilePath, destFilePath)) {
|
|
return false;
|
|
}
|
|
} else {
|
|
// 复制文件
|
|
QFile dest(destFilePath);
|
|
if (dest.exists()) {
|
|
bool re = dest.remove(); // 如果目标文件存在,先删除它
|
|
if(!re){
|
|
qDebug() << "删除失败 "<<dest;
|
|
}
|
|
}
|
|
if (!QFile::copy(srcFilePath, destFilePath)) {
|
|
qDebug() << "无法复制文件" << srcFilePath << "到" << destFilePath;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|