#include "MainWindow.h" #include "ui_mainwindow.h" #include "MonitorCommon.h" #include "StartUpForm.h" #include "AppMngForm.h" #include "ProcMngForm.h" #include "DataMng.h" #include "ButtonForm.h" #include "BrushDataThread.h" #include "StatusForm.h" #include "SystemSetForm.h" #include "SetupFunc.h" #include "localstateform.h" #include "CheckBoxDialog.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "pub_utility_api/FileUtil.h" #include "pub_utility_api/I18N.h" #include "pub_utility_api/FileStyle.h" #include "pub_widget/MessageBox.h" #include "pub_widget/PubWidgetInit.h" using namespace workBench; using namespace iot_public; using namespace iot_idl; using namespace iot_sys; static QString restartAppName = QString(); MainWindow::MainWindow(QString language, QWidget *parent) : CustomUiMainWindow(parent), ui(new Ui::MainWindow), m_bRunning(false), m_pLocalServer(Q_NULLPTR), m_language(language), m_pStartUp(Q_NULLPTR), m_pAppMng(Q_NULLPTR), m_pProcMng(Q_NULLPTR), m_pStatusMng(Q_NULLPTR), m_pSysSetMng(Q_NULLPTR), m_pSysTrayIcon(Q_NULLPTR), m_updateTreeTimer(Q_NULLPTR), m_updateSysTimer(Q_NULLPTR), m_startSysTimer(Q_NULLPTR), m_stopSysTimer(Q_NULLPTR), m_appRestartTimer(Q_NULLPTR), m_dogAndLocalNetTimer(Q_NULLPTR), m_hmiAutoStartTimer(Q_NULLPTR), m_brushDataThread(Q_NULLPTR), m_checkObj(Q_NULLPTR), m_checkThread(Q_NULLPTR), m_pProcess(Q_NULLPTR), m_ptrDogAuthApi(Q_NULLPTR), m_bLastDogStatus(false), m_pShowAction(nullptr), m_pExitAction(nullptr), m_pLocalState(Q_NULLPTR) { ui->setupUi(this); DataMng::instance(); initLog(); initMsgBus(); // 初始化变量 initVariables(); // 初始化界面 initUi(); //初始化信号槽 initConn(); // 加载qss样式 initSheet(); //校验ip verifyIp(); //启动线程和定时器 startThread(); initLocalConnection(); } MainWindow::~MainWindow() { if(m_pProcess) { delete m_pProcess; } m_pProcess = Q_NULLPTR; if(m_startSysTimer) { m_startSysTimer->stop(); delete m_startSysTimer; } m_startSysTimer = Q_NULLPTR; if(m_stopSysTimer) { m_stopSysTimer->stop(); delete m_stopSysTimer; } m_stopSysTimer = Q_NULLPTR; if(m_dogAndLocalNetTimer) { m_dogAndLocalNetTimer->stop(); delete m_dogAndLocalNetTimer; } m_dogAndLocalNetTimer = Q_NULLPTR; if(m_hmiAutoStartTimer) { m_hmiAutoStartTimer->stop(); delete m_hmiAutoStartTimer; } m_hmiAutoStartTimer = Q_NULLPTR; if(m_appRestartTimer) { m_appRestartTimer->stop(); delete m_appRestartTimer; } m_appRestartTimer = Q_NULLPTR; if(m_updateTreeTimer) { m_updateTreeTimer->stop(); delete m_updateTreeTimer; } m_updateTreeTimer = Q_NULLPTR; if(m_updateSysTimer) { m_updateSysTimer->stop(); delete m_updateSysTimer; } m_updateSysTimer = Q_NULLPTR; if(m_checkThread) { m_checkThread->exit(); m_checkThread->wait(); delete m_checkThread; } m_checkThread = Q_NULLPTR; if(m_brushDataThread) { m_brushDataThread->suspend(); m_brushDataThread->quit(); m_brushDataThread.reset(); } if(m_pSysTrayIcon) { delete m_pSysTrayIcon; } m_pSysTrayIcon = Q_NULLPTR; DataMng::instance()->destory(); if(! iot_net::releaseMsgBus()) { LOGERROR("释放消息总线失败"); } iot_public::StopLogSystem(); if (m_pLocalServer) { delete m_pLocalServer; m_pLocalServer = NULL; } if(m_pStopProcess) { delete m_pStopProcess; } delete ui; } bool MainWindow::isRunning() { return m_bRunning; } void MainWindow::initLocalConnection() { // 已经用了SingleApplication,为什么还要增加这种方式: // SingleApplication只能解决同一个文件执行多次的情况 // 如果是不同的文件,比如安装了两份软件在不同目录,要靠本函数的机制防止 m_bRunning = false; QLocalSocket socket; socket.connectToServer(STARTUP_SERVER_NAME); if(socket.waitForConnected(500)) { m_bRunning = true; return; } newLocalServer(); } void MainWindow::newLocalServer() { m_pLocalServer = new QLocalServer(this); if(!m_pLocalServer->listen(STARTUP_SERVER_NAME)) { if(m_pLocalServer->serverError() == QAbstractSocket::AddressInUseError) { QLocalServer::removeServer(STARTUP_SERVER_NAME); m_pLocalServer->listen(STARTUP_SERVER_NAME); } } } void MainWindow::startWork() { m_brushDataThread->resume(); m_updateTreeTimer->start(); m_updateSysTimer->start(); m_dogAndLocalNetTimer->start(); } void MainWindow::stopWork() { m_dogAndLocalNetTimer->stop(); m_updateSysTimer->stop(); m_updateTreeTimer->stop(); m_brushDataThread->suspend(); } void MainWindow::startAutoStartHmiTimer() { m_hmiAutoStartTimer->start(); } void MainWindow::initSheet() { QString qss = QString(); std::string strFullPath = iot_public::CFileStyle::getPathOfStyleFile("public.qss","zh","light"); QFile qssfile1(QString::fromStdString(strFullPath)); qssfile1.open(QFile::ReadOnly); if (qssfile1.isOpen()) { qss += QLatin1String(qssfile1.readAll()); qssfile1.close(); } strFullPath = iot_public::CFileStyle::getPathOfStyleFile("workBench.qss","","light") ; QFile qssfile(QString::fromStdString(strFullPath)) ; qssfile.open(QFile::ReadOnly); if (qssfile.isOpen()) { qss += QLatin1String(qssfile.readAll()); qssfile.close(); } if (!qss.isEmpty()) { qApp->setStyleSheet(qss); } iot_public::installTranslator(iot_public::getCurLanguage()); } void MainWindow::initVariables() { if(m_pStartUp == Q_NULLPTR) { m_pStartUp = new StartUpForm; } if(m_pAppMng == Q_NULLPTR) { m_pAppMng = new AppMngForm; } if(m_pProcMng == Q_NULLPTR) { m_pProcMng = new ProcMngForm; } if(m_pStatusMng == Q_NULLPTR) { m_pStatusMng = new StatusForm; } if(m_pSysSetMng == Q_NULLPTR) { m_pSysSetMng = new SystemSetForm; } if( m_pLocalState == Q_NULLPTR) { m_pLocalState = new LocalStateForm; } if(m_pSysTrayIcon == Q_NULLPTR) { m_pSysTrayIcon = new QSystemTrayIcon; QString sysIcon = DataMng::instance()->getSysIcon(); if(sysIcon.isEmpty()) m_pSysTrayIcon->setIcon(QIcon(":/sys_startup.ico")); else m_pSysTrayIcon->setIcon(QIcon(sysIcon)); QMenu* pMenu = new QMenu(this); m_pShowAction = pMenu->addAction(tr("显示")); m_pExitAction = pMenu->addAction(tr("退出")); connect(m_pShowAction, &QAction::triggered, [=](){ startWork(); this->show(); this->showNormal(); this->activateWindow(); }); connect(m_pExitAction, &QAction::triggered, [=](){ qApp->quit(); }); m_pSysTrayIcon->setContextMenu(pMenu); m_pSysTrayIcon->show(); } m_brushDataThread = boost::make_shared(); m_brushDataThread->initialize(DataMng::instance()->getDomainId(),1); m_updateTreeTimer = new QTimer; m_updateTreeTimer->setInterval(5000); m_checkObj = new OnTimeCheckThread(nullptr); m_checkThread = new QThread(); m_checkObj->moveToThread(m_checkThread); m_updateSysTimer = new QTimer; m_updateSysTimer->setInterval(1000); m_pProcess=new QProcess(this); { const std::string strProc = std::move( iot_public::CFileUtil::getPathOfBinFile( std::string("sys_ctrl") + iot_public::CFileUtil::getProcSuffix())); if(strProc.empty()) LOGERROR("未找到可执行文件sys_ctrl"); else m_strSysCtrlPath = QString::fromStdString(strProc); } m_startSysTimer = new QTimer; m_startSysTimer->setInterval(1000); m_stopSysTimer = new QTimer; m_stopSysTimer->setInterval(1000); m_appRestartTimer = new QTimer; m_appRestartTimer->setInterval(1000); m_dogAndLocalNetTimer = new QTimer; m_dogAndLocalNetTimer->setInterval(60000); m_hmiAutoStartTimer = new QTimer; m_hmiAutoStartTimer->setInterval(2000); m_nStartStopResult = E_PROCESS_DEFAULT; m_ptrDogAuthApi = getDogAuthInstance(); if (!m_ptrDogAuthApi) { LOGERROR("创建加密狗实例失败"); } } void MainWindow::initUi() { //标题栏 #ifdef OS_LINUX setWindowIcon( QIcon( ":/sys_startup.ico" ) ); #endif setWindowTitle(DataMng::instance()->getTitle()); m_pSysTrayIcon->setToolTip(DataMng::instance()->getTitle()); //菜单栏 QPushButton* btn; btn = new QPushButton(); btn->setObjectName("btn_sysState"); btn->setText(tr("系统状态")); ui->widget_menu->addToolBtn(btn); btn = new QPushButton(); btn->setObjectName("btn_sysProcess"); btn->setText(tr("系统进程")); ui->widget_menu->addToolBtn(btn); btn = new QPushButton(); btn->setObjectName("btn_sysManage"); btn->setText(tr("系统管理")); ui->widget_menu->addToolBtn(btn); btn = new QPushButton(); btn->setObjectName("btn_sysTool"); btn->setText(tr("系统工具")); ui->widget_menu->addToolBtn(btn); btn = new QPushButton(); btn->setObjectName("btn_sysSetting"); btn->setText(tr("系统设置")); ui->widget_menu->addToolBtn(btn); //进度条 QGridLayout *startUpLayout = new QGridLayout(ui->startUpFrame); startUpLayout->setSpacing(6); startUpLayout->setContentsMargins(0, 0, 0, 0); startUpLayout->addWidget(m_pStartUp); //应用树 QGridLayout * appLayout = new QGridLayout(ui->appFrame); appLayout->setSpacing(6); appLayout->setContentsMargins(0, 0, 0, 0); appLayout->addWidget(m_pAppMng); //进程表格 QGridLayout *procLayout = new QGridLayout(ui->procWidget); procLayout->setSpacing(6); procLayout->setContentsMargins(0, 0, 0, 0); procLayout->addWidget(m_pProcMng); //本机应用状态 QGridLayout *localStaLayout = new QGridLayout(ui->localstate); localStaLayout->setSpacing(6); localStaLayout->setContentsMargins(0, 0, 0, 0); localStaLayout->addWidget(m_pLocalState); //首页快捷按钮 ButtonForm *page1form = new ButtonForm(EN_SYS_STARTUP_PAGE1,m_language,this); page1form->setTitleHide(true); QVector buttons = DataMng::instance()->getPage1ButtonInfo(); page1form->addToolButtons(buttons); connect(page1form,&ButtonForm::sigLogText,m_pStatusMng,&StatusForm::slotLogText); QGridLayout *page1layout = new QGridLayout(ui->customWidget); page1layout->setSpacing(0); page1layout->setContentsMargins(0,0,0,0); page1layout->addWidget(page1form); //工具箱按钮 QMap buttonGroupMap = DataMng::instance()->getPage3ButtonInfo(); QMap::iterator it = buttonGroupMap.begin(); QVBoxLayout *page3layout = new QVBoxLayout(ui->boxWidget); page3layout->setSpacing(20); while (it != buttonGroupMap.end()) { ButtonForm *page3form = new ButtonForm(EN_SYS_STARTUP_PAGE3,m_language,this); page3form->setTitleText(it.value().m_desc); page3form->addToolButtons(it.value().m_buttonVec , it.key()); connect(page3form,&ButtonForm::sigLogText,m_pStatusMng,&StatusForm::slotLogText); connect(page3form , &ButtonForm::sigUpdataButton , this , &MainWindow::updataButtonForm); page3layout->addWidget(page3form); it++; } page3layout->addStretch(); //状态栏视图 QGridLayout *statusLayout = new QGridLayout(ui->status_mng); statusLayout->setSpacing(6); statusLayout->setContentsMargins(0, 0, 0, 0); statusLayout->addWidget(m_pStatusMng); //版本 QGridLayout *versionLayout = new QGridLayout(ui->verWidget); versionLayout->setSpacing(6); versionLayout->setContentsMargins(0, 0, 0, 0); versionLayout->addWidget(m_pSysSetMng); connect(m_pSysSetMng,&SystemSetForm::sigLogText,m_pStatusMng,&StatusForm::slotLogText); setTitleWidget(ui->titleMenu); setSendCloseSignal(true); ui->widget_menu->setButtonClicked(0); } void MainWindow::initConn() { connect(m_updateTreeTimer,&QTimer::timeout,m_pAppMng,&AppMngForm::updateAppTree); connect(m_updateTreeTimer,&QTimer::timeout,m_pLocalState,&LocalStateForm::updataLocalState); // connect(m_updateTreeTimer,&QTimer::timeout,m_pProcMng,&ProcMngForm::updateProcTree); connect(m_updateSysTimer,&QTimer::timeout,m_checkObj,&OnTimeCheckThread::brush,Qt::QueuedConnection); connect(m_checkObj,&OnTimeCheckThread::changeTab,m_pStartUp,&StartUpForm::changeIndex,Qt::QueuedConnection); connect(m_checkThread, &QThread::finished, m_checkObj, &QObject::deleteLater); connect(m_checkThread, &QThread::finished, m_updateSysTimer, &QTimer::deleteLater); connect(m_pStartUp,&StartUpForm::sigStartSystem,this,&MainWindow::slotStartSystem); connect(m_pStartUp,&StartUpForm::sigStopSystem,this,&MainWindow::slotStopSystem); connect(m_pProcess,SIGNAL(readyReadStandardOutput()),this,SLOT(outPut())); connect(m_pProcess,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(finish(int,QProcess::ExitStatus))); connect(m_startSysTimer,&QTimer::timeout,this,&MainWindow::startPercent); connect(m_stopSysTimer,&QTimer::timeout,this,&MainWindow::stopPercent); connect(this,&MainWindow::changeTab,m_pStartUp,&StartUpForm::changeIndex); connect(m_pAppMng,&AppMngForm::sigReloadAppAndProc,this,&MainWindow::reloadData); connect(m_pAppMng,&AppMngForm::sigStartApp,this,&MainWindow::startApp); connect(m_pAppMng,&AppMngForm::sigStopApp,this,&MainWindow::stopApp); connect(m_pAppMng,&AppMngForm::sigRestartApp,this,&MainWindow::restartApp); connect(m_pLocalState , &LocalStateForm::sigStartServer , this , &MainWindow::startApp); connect(m_pLocalState , &LocalStateForm::sigStopServer , this , &MainWindow::stopApp); connect(m_pLocalState , &LocalStateForm::sigRestartServer , this , &MainWindow::restartApp); connect(m_appRestartTimer,&QTimer::timeout,this,&MainWindow::ifStartApp); connect(m_pProcMng,&ProcMngForm::sigReloadAppAndProc,this,&MainWindow::reloadData); connect(m_pProcMng,&ProcMngForm::sigStartProc,this,&MainWindow::startProc); connect(m_pProcMng,&ProcMngForm::sigStopProc,this,&MainWindow::stopProc); connect(m_pProcMng,&ProcMngForm::sigRestartProc,this,&MainWindow::reStartProc); connect(m_pProcMng,&ProcMngForm::sigForceStopProc,this,&MainWindow::forceStopProc); connect(m_dogAndLocalNetTimer,&QTimer::timeout,this,&MainWindow::dogAndNetCheck); connect(m_hmiAutoStartTimer,&QTimer::timeout,this,&MainWindow::autoStartHmi); connect(m_pSysTrayIcon,&QSystemTrayIcon::activated,this,&MainWindow::iconActived); connect(ui->widget_menu, SIGNAL(buttonClicked(int)),ui->stackedWidget, SLOT(setCurrentIndex(int))); connect(ui->stackedWidget, &QStackedWidget::currentChanged,this,&MainWindow::tabWidget_currentIdxChanged); connect(m_pSysSetMng , &SystemSetForm::signalUpdataPage1 , this , &MainWindow::updataButtonForm); connect(this, &MainWindow::closeClicked , this , &MainWindow::slot_closeWindow ); } void MainWindow::initLog() { //注册日志 iot_public::StartLogSystem(CN_AppName_BASE.c_str(),STARTUP_SERVER_NAME); } void MainWindow::initMsgBus() { //初始化消息总线 if(! iot_net::initMsgBus(STARTUP_SERVER_NAME,STARTUP_SERVER_NAME)) { LOGERROR("初始化消息总线失败"); } } void MainWindow::startThread() { dogAndNetCheck(); m_checkThread->start(); startWork(); } void MainWindow::showMessDialog(const QString &mess) { N_MessageBox::warning(this,tr("提示"),mess); } void MainWindow::reloadAppTree() { m_pAppMng->initAppTree(); } void MainWindow::reloadProcTree() { m_pProcMng->initProcTree(); } void MainWindow::reloadLocalState() { m_pLocalState->initlocalState(); } void MainWindow::slotStartSystem() { //1.检查系统是否全部退出 if(OnTimeCheckThread::isSystemRunning()) { LOGDEBUG("内存库可能被占用,现在先停止系统,清除共享内存!"); if(m_pStopProcess == nullptr) { m_pStopProcess = new QProcess; } else { m_pStopProcess->close(); } disconnect(m_pStopProcess,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(startSystem(int,QProcess::ExitStatus))); connect(m_pStopProcess,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(startSystem(int,QProcess::ExitStatus))); m_pStopProcess->start(m_strSysCtrlPath + " -s"); return ; }else { startSystem(); } } void MainWindow::slotStopSystem() { //1.屏蔽信号 blockSignal(true); //2.切换到正在停止界面 emit changeTab(EN_SYSTEM_STOPPING); m_nStartStopResult = E_PROCESS_DEFAULT; DataMng::instance()->setAllowedStatus(false); m_pProcess->start(m_strSysCtrlPath + " -s"); //更新状态 QString text = tr("开始停止系统"); m_pStatusMng->slotLogText(text); m_stopSysTimer->start(); //启动停止定时器 m_pProcess->waitForStarted(); } void MainWindow::outPut() { QString ret = QString::fromLocal8Bit(m_pProcess->readAllStandardOutput()); ret=ret.trimmed(); QStringList list=ret.split("\n"); for(int i=0;islotLogText(list.at(i)); } } } void MainWindow::finish(int res, QProcess::ExitStatus exit) { Q_UNUSED(exit); m_nStartStopResult = (E_PROCESS_RESULT)res; DataMng::instance()->setAllowedStatus(true); } void MainWindow::startPercent() { int num = 0; emit changeTab(EN_SYSTEM_STARTING); if(m_nStartStopResult == E_PROCESS_FAILD || m_nStartStopResult == E_PROCESS_SUCCEED) { m_startSysTimer->stop(); DataMng::instance()->setAllowedStatus(true); blockSignal(false); m_nStartStopResult = E_PROCESS_DEFAULT; m_pStartUp->initStartValue(); return ; }else { float disk = 0.0; vector procVec ; DataMng::instance()->getRunProcInfo(procVec,disk); vector::iterator it = procVec.begin(); for(;it!=procVec.end();it++) { if(it->m_isStartUp == 1) { num++; } } int localProcNum = DataMng::instance()->getLocalProcNum(); m_pStartUp->changeStartValue(num*100/localProcNum); } } void MainWindow::stopPercent() { int num = 0; emit changeTab(EN_SYSTEM_STOPPING); if(m_nStartStopResult == E_PROCESS_FAILD || m_nStartStopResult == E_PROCESS_SUCCEED) { m_stopSysTimer->stop(); DataMng::instance()->setAllowedStatus(true); blockSignal(false); m_nStartStopResult = E_PROCESS_DEFAULT; m_pStartUp->initStopValue(); return ; }else { float disk = 0.0; vector procVec ; DataMng::instance()->getRunProcInfo(procVec,disk); vector::iterator it = procVec.begin(); for(;it!=procVec.end();it++) { if(it->m_isStartUp == 0) { num++; } } int localProcNum = DataMng::instance()->getLocalProcNum(); m_pStartUp->changeStopValue(num*100/localProcNum); } } void MainWindow::reloadData() { DataMng::instance()->loadXml(); reloadAppTree(); reloadProcTree(); reloadLocalState(); } void MainWindow::startApp(const QString appName) { DataMng::instance()->setAllowedStatus(false); m_nStartStopResult = E_PROCESS_DEFAULT; m_pProcess->start(QString("%1 -a %2").arg(m_strSysCtrlPath).arg(appName)); } void MainWindow::stopApp(const QString appName) { DataMng::instance()->setAllowedStatus(false); m_nStartStopResult = E_PROCESS_DEFAULT; m_pProcess->start(QString("%1 -s -a %2").arg(m_strSysCtrlPath).arg(appName)); } void MainWindow::restartApp(const QString appName) { restartAppName = appName; stopApp(appName); m_appRestartTimer->start(); } void MainWindow::ifStartApp() { if(m_nStartStopResult == E_PROCESS_FAILD) { m_appRestartTimer->stop(); m_nStartStopResult = E_PROCESS_DEFAULT; }else if(m_nStartStopResult == E_PROCESS_SUCCEED) { m_appRestartTimer->stop(); m_nStartStopResult = E_PROCESS_DEFAULT; QString appName = restartAppName; restartAppName = QString(); startApp(appName); } } void MainWindow::startProc(int appId, const QString procName) { vector procVec; float disk = 0.0; DataMng::instance()->getRunProcInfo(procVec,disk); for(size_t index(0);indexsetAllowedStatus(false); QString logText = QString(tr("开始启动进程:%1")).arg(QString::fromStdString(procVec[index].m_procName)); LOGDEBUG("startProc()-%s",logText.toStdString().c_str()); m_pStatusMng->slotLogText(logText); string strArgs; if (appId != CN_AppId_BASE) { strArgs += "-a " + DataMng::instance()->getAppNameByAppId(appId); } if (!procVec[index].m_procParam.empty()) { strArgs += " " + procVec[index].m_procParam; } string strModulePath; CFileUtil::getPathOfBinFile(procName.toStdString() + CFileUtil::getProcSuffix(), &strModulePath); if(!CProcUtil::startProcess(strModulePath, procName.toStdString(), procVec[index].m_procParam, false,false)) { logText = QString(tr("启动进程:%1失败")).arg(QString::fromStdString(procVec[index].m_procName)); LOGERROR("startProc()-%s",logText.toStdString().c_str()); m_pStatusMng->slotLogText(logText); }else { if(isProcStartSuccess(appId,procName)) { logText = QString(tr("启动进程:%1成功")).arg(QString::fromStdString(procVec[index].m_procName)); LOGDEBUG("startProc()-%s",logText.toStdString().c_str()); m_pStatusMng->slotLogText(logText); } else { logText = QString(tr("启动进程:%1失败")).arg(QString::fromStdString(procVec[index].m_procName)); LOGDEBUG("startProc()-%s",logText.toStdString().c_str()); m_pStatusMng->slotLogText(logText); } } DataMng::instance()->setAllowedStatus(true); break; } } } bool MainWindow::stopProc(int appId, const QString procName) { CRdbNetApi netApi; netApi.connect(DataMng::instance()->getDomainId(),1); vector procVec; float disk = 0.0; DataMng::instance()->getRunProcInfo(procVec,disk); for(size_t index(0);indexsetAllowedStatus(false); QString logText = QString(tr("开始停止进程:%1")).arg(QString::fromStdString(procVec[index].m_procName)); LOGDEBUG("stopProc()-%s",logText.toStdString().c_str()); m_pStatusMng->slotLogText(logText); iot_idl::RdbUpdate rdbupdate; rdbupdate.set_strtablename(CN_TN_SysRunProcInfo); rdbupdate.add_msgcondition(); rdbupdate.mutable_msgcondition(0)->set_enlogic(enumCondAnd); rdbupdate.mutable_msgcondition(0)->set_enrelation(enumCondEqual); rdbupdate.mutable_msgcondition(0)->set_strcolumnname("node_name"); rdbupdate.mutable_msgcondition(0)->mutable_msgvalue()->set_edatatype(CN_DATATYPE_STRING); rdbupdate.mutable_msgcondition(0)->mutable_msgvalue()->set_strvalue(procVec[index].m_nodeName); rdbupdate.add_msgcondition(); rdbupdate.mutable_msgcondition(1)->set_enlogic(enumCondAnd); rdbupdate.mutable_msgcondition(1)->set_enrelation(enumCondEqual); rdbupdate.mutable_msgcondition(1)->set_strcolumnname("proc_name"); rdbupdate.mutable_msgcondition(1)->mutable_msgvalue()->set_edatatype(CN_DATATYPE_STRING); rdbupdate.mutable_msgcondition(1)->mutable_msgvalue()->set_strvalue(procName.toStdString()); rdbupdate.add_msgcondition(); rdbupdate.mutable_msgcondition(2)->set_enlogic(enumCondAnd); rdbupdate.mutable_msgcondition(2)->set_enrelation(enumCondEqual); rdbupdate.mutable_msgcondition(2)->set_strcolumnname("app_id"); rdbupdate.mutable_msgcondition(2)->mutable_msgvalue()->set_edatatype(CN_DATATYPE_INT32); rdbupdate.mutable_msgcondition(2)->mutable_msgvalue()->set_nvalue(appId); rdbupdate.add_msgupdatevalue(); rdbupdate.mutable_msgupdatevalue(0)->set_strcolumnname("to_quit"); rdbupdate.mutable_msgupdatevalue(0)->mutable_msgvalue()->set_edatatype(CN_DATATYPE_INT32); rdbupdate.mutable_msgupdatevalue(0)->mutable_msgvalue()->set_nvalue(1); bool bRet = false; bRet=netApi.update(rdbupdate); if(bRet==false) { LOGERROR("stopProc()-写表失败:%s",netApi.getErr().c_str()); logText = QString(tr("停止进程:%1失败")).arg(QString::fromStdString(procVec[index].m_procName)); m_pStatusMng->slotLogText(logText); DataMng::instance()->setAllowedStatus(true); return false; } if(isProcStopSuccess(appId,procName)) { logText = QString(tr("停止进程:%1成功")).arg(QString::fromStdString(procVec[index].m_procName)); LOGDEBUG("stopProc()-%s",logText.toStdString().c_str()); m_pStatusMng->slotLogText(logText); DataMng::instance()->setAllowedStatus(true); return true; } else { logText = QString(tr("停止进程:%1失败")).arg(QString::fromStdString(procVec[index].m_procName)); LOGERROR("stopProc()-%s",logText.toStdString().c_str()); m_pStatusMng->slotLogText(logText); DataMng::instance()->setAllowedStatus(true); return false; } } } return false; } void MainWindow::reStartProc(int appId, const QString procName) { if(stopProc(appId,procName)) { startProc(appId,procName); } } void MainWindow::forceStopProc(int appId, const QString procName) { CRdbNetApi netApi; netApi.connect(DataMng::instance()->getDomainId(),1); vector procVec; float disk = 0.0; DataMng::instance()->getRunProcInfo(procVec,disk); for(size_t index(0);indexsetAllowedStatus(false); QString logText = QString(tr("强制停止进程:%1")).arg(QString::fromStdString(procVec[index].m_procName)); LOGDEBUG("forceStopProc()-%s",logText.toStdString().c_str()); m_pStatusMng->slotLogText(logText); iot_idl::RdbUpdate rdbupdate; rdbupdate.set_strtablename(CN_TN_SysRunProcInfo); rdbupdate.add_msgcondition(); rdbupdate.mutable_msgcondition(0)->set_enlogic(enumCondAnd); rdbupdate.mutable_msgcondition(0)->set_enrelation(enumCondEqual); rdbupdate.mutable_msgcondition(0)->set_strcolumnname("node_name"); rdbupdate.mutable_msgcondition(0)->mutable_msgvalue()->set_edatatype(CN_DATATYPE_STRING); rdbupdate.mutable_msgcondition(0)->mutable_msgvalue()->set_strvalue(procVec[index].m_nodeName); rdbupdate.add_msgcondition(); rdbupdate.mutable_msgcondition(1)->set_enlogic(enumCondAnd); rdbupdate.mutable_msgcondition(1)->set_enrelation(enumCondEqual); rdbupdate.mutable_msgcondition(1)->set_strcolumnname("proc_name"); rdbupdate.mutable_msgcondition(1)->mutable_msgvalue()->set_edatatype(CN_DATATYPE_STRING); rdbupdate.mutable_msgcondition(1)->mutable_msgvalue()->set_strvalue(procName.toStdString()); rdbupdate.add_msgcondition(); rdbupdate.mutable_msgcondition(2)->set_enlogic(enumCondAnd); rdbupdate.mutable_msgcondition(2)->set_enrelation(enumCondEqual); rdbupdate.mutable_msgcondition(2)->set_strcolumnname("app_id"); rdbupdate.mutable_msgcondition(2)->mutable_msgvalue()->set_edatatype(CN_DATATYPE_INT32); rdbupdate.mutable_msgcondition(2)->mutable_msgvalue()->set_nvalue(appId); rdbupdate.add_msgupdatevalue(); rdbupdate.mutable_msgupdatevalue(0)->set_strcolumnname("is_fault"); rdbupdate.mutable_msgupdatevalue(0)->mutable_msgvalue()->set_edatatype(CN_DATATYPE_INT32); rdbupdate.mutable_msgupdatevalue(0)->mutable_msgvalue()->set_nvalue(0); rdbupdate.add_msgupdatevalue(); rdbupdate.mutable_msgupdatevalue(1)->set_strcolumnname("is_startup"); rdbupdate.mutable_msgupdatevalue(1)->mutable_msgvalue()->set_edatatype(CN_DATATYPE_INT32); rdbupdate.mutable_msgupdatevalue(1)->mutable_msgvalue()->set_nvalue(0); rdbupdate.add_msgupdatevalue(); rdbupdate.mutable_msgupdatevalue(2)->set_strcolumnname("to_quit"); rdbupdate.mutable_msgupdatevalue(2)->mutable_msgvalue()->set_edatatype(CN_DATATYPE_INT32); rdbupdate.mutable_msgupdatevalue(2)->mutable_msgvalue()->set_nvalue(1); rdbupdate.add_msgupdatevalue(); rdbupdate.mutable_msgupdatevalue(3)->set_strcolumnname("is_quit"); rdbupdate.mutable_msgupdatevalue(3)->mutable_msgvalue()->set_edatatype(CN_DATATYPE_INT32); rdbupdate.mutable_msgupdatevalue(3)->mutable_msgvalue()->set_nvalue(1); rdbupdate.add_msgupdatevalue(); rdbupdate.mutable_msgupdatevalue(4)->set_strcolumnname("is_regist"); rdbupdate.mutable_msgupdatevalue(4)->mutable_msgvalue()->set_edatatype(CN_DATATYPE_INT32); rdbupdate.mutable_msgupdatevalue(4)->mutable_msgvalue()->set_nvalue(0); rdbupdate.add_msgupdatevalue(); rdbupdate.mutable_msgupdatevalue(5)->set_strcolumnname("is_active"); rdbupdate.mutable_msgupdatevalue(5)->mutable_msgvalue()->set_edatatype(CN_DATATYPE_INT32); rdbupdate.mutable_msgupdatevalue(5)->mutable_msgvalue()->set_nvalue(0); rdbupdate.add_msgupdatevalue(); rdbupdate.mutable_msgupdatevalue(6)->set_strcolumnname("is_master"); rdbupdate.mutable_msgupdatevalue(6)->mutable_msgvalue()->set_edatatype(CN_DATATYPE_INT32); rdbupdate.mutable_msgupdatevalue(6)->mutable_msgvalue()->set_nvalue(0); rdbupdate.add_msgupdatevalue(); rdbupdate.mutable_msgupdatevalue(7)->set_strcolumnname("is_slave"); rdbupdate.mutable_msgupdatevalue(7)->mutable_msgvalue()->set_edatatype(CN_DATATYPE_INT32); rdbupdate.mutable_msgupdatevalue(7)->mutable_msgvalue()->set_nvalue(0); bool bRet = false; bRet=netApi.update(rdbupdate); if(bRet==false) { LOGERROR("forceStopProc()写表失败:%s",netApi.getErr().c_str()); logText = QString(tr("停止进程:%1失败")).arg(QString::fromStdString(procVec[index].m_procName)); m_pStatusMng->slotLogText(logText); DataMng::instance()->setAllowedStatus(true); return ; } if(procVec[index].m_procId <= 0) { logText = QString(tr("停止进程:%1成功")).arg(QString::fromStdString(procVec[index].m_procName)); LOGDEBUG("forceStopProc()-%s",logText.toStdString().c_str()); m_pStatusMng->slotLogText(logText); DataMng::instance()->setAllowedStatus(true); return ; } QProcess process; QStringList stList; #ifdef Q_OS_WIN stList<<"TASKKILL"<<"/PID"<slotLogText(logText); DataMng::instance()->setAllowedStatus(true); break; } } } void MainWindow::dogAndNetCheck() { checkNet(); checkDog(); checkSysIsRun(); } void MainWindow::autoStartHmi() { QString strHmiErr; bool isHmi = false; #ifdef OS_WINDOWS isHmi = iot_sys::isSysStartupVbsExist(); #endif #ifdef OS_LINUX isHmi = iot_sys::isHmiAutoStart(strHmiErr); #endif if(!isHmi) { m_hmiAutoStartTimer->stop(); }else { if(DataMng::instance()->getBaseStatus()) { m_hmiAutoStartTimer->stop(); openHmi(); } } } void MainWindow::iconActived(QSystemTrayIcon::ActivationReason reason) { switch (reason) { case QSystemTrayIcon::DoubleClick: LOGINFO("右下角双击应用图标,开始工作!"); startWork(); this->show(); this->showNormal(); this->activateWindow(); break; case QSystemTrayIcon::Context: { if(!DataMng::instance()->getAllowedStatus()) m_pExitAction->setEnabled(false); else m_pExitAction->setEnabled(true); break; } default: break; } } void MainWindow::startSystem(int res,QProcess::ExitStatus exit) { Q_UNUSED(exit); Q_UNUSED(res); //1.检查系统是否全部退出 if(OnTimeCheckThread::isSystemRunning()) { QString mess = tr("系统启动失败,请检查所有配置工具和人机界面是否已经全部退出!"); m_pStatusMng->slotLogText(mess); showMessDialog(mess); return ; } //2.重新加载数据、重新初始化应用树和进程树 reloadData(); //3.检查模型数据是否导出 if(DataMng::instance()->getLocalProcNum() <= 0) { int result = N_MessageBox::question(this,tr("错误"),tr("系统模型配置信息异常,是否打开系统建模工具检查配置并导出?") , N_MessageBox::Yes | N_MessageBox::No, N_MessageBox::Yes); if(result == N_MessageBox::Yes) { const std::string strProc = std::move( iot_public::CFileUtil::getPathOfBinFile( std::string("model_system_config") + iot_public::CFileUtil::getProcSuffix())); if(strProc.empty()) { LOGERROR("未找到可执行文件model_system_config"); return; } QProcess::startDetached(strProc.c_str()); } return ; } //4.校验ip verifyIp(); //5.屏蔽其他信号 blockSignal(true); //6.切换到正在启动界面 emit changeTab(EN_SYSTEM_STARTING); //7.启动系统 DataMng::instance()->setAllowedStatus(false); m_pProcess->start(m_strSysCtrlPath); m_startSysTimer->start(); m_pProcess->waitForStarted(); } void MainWindow::tabWidget_currentIdxChanged(int /*index*/) { if( ui->stackedWidget->currentWidget() == ui->stackedWidgetPage1) { disconnect(m_updateTreeTimer,&QTimer::timeout,m_pProcMng,&ProcMngForm::updateProcTree); disconnect(m_updateTreeTimer,&QTimer::timeout,m_pAppMng,&AppMngForm::updateAppTree); connect(m_updateTreeTimer,&QTimer::timeout , m_pLocalState , &LocalStateForm::updataLocalState); } else if( ui->stackedWidget->currentWidget() == ui->stackedWidgetPage2 ) { connect(m_updateTreeTimer,&QTimer::timeout,m_pProcMng,&ProcMngForm::updateProcTree); disconnect(m_updateTreeTimer,&QTimer::timeout,m_pAppMng,&AppMngForm::updateAppTree); disconnect(m_updateTreeTimer,&QTimer::timeout , m_pLocalState , &LocalStateForm::updataLocalState); }else if( ui->stackedWidget->currentWidget() == ui->stackedWidgetPage3 ) { disconnect(m_updateTreeTimer,&QTimer::timeout,m_pProcMng,&ProcMngForm::updateProcTree); disconnect(m_updateTreeTimer,&QTimer::timeout , m_pLocalState , &LocalStateForm::updataLocalState); connect(m_updateTreeTimer,&QTimer::timeout,m_pAppMng,&AppMngForm::updateAppTree); } else { disconnect(m_updateTreeTimer,&QTimer::timeout,m_pProcMng,&ProcMngForm::updateProcTree); disconnect(m_updateTreeTimer,&QTimer::timeout,m_pAppMng,&AppMngForm::updateAppTree); disconnect(m_updateTreeTimer,&QTimer::timeout , m_pLocalState , &LocalStateForm::updataLocalState); } } void MainWindow::changeTabWidgetIndex(int index) { ui->stackedWidget->setCurrentIndex(index); } void MainWindow::updataButtonForm( int model ) { // 获取新的按钮信息集合 if( EN_SYS_STARTUP_PAGE3 == model) { QMap buttonGroupMap = DataMng::instance()->getPage3ButtonInfo(); if(buttonGroupMap.isEmpty()) return; QLayout *oldLayout = ui->boxWidget->layout(); if (oldLayout) { QLayoutItem *item; while ((item = oldLayout->takeAt(0)) != nullptr) { if (item->widget()) { delete item->widget(); } delete item; } delete oldLayout; } // 重新生成并添加新的按钮控件 QVBoxLayout *page3layout = new QVBoxLayout(ui->boxWidget); //page3layout->setSpacing(20); QMap::iterator it = buttonGroupMap.begin(); while (it != buttonGroupMap.end()) { ButtonForm *page3form = new ButtonForm(EN_SYS_STARTUP_PAGE3,m_language,this); page3form->setTitleText(it.value().m_desc); page3form->addToolButtons(it.value().m_buttonVec , it.key()); connect(page3form,&ButtonForm::sigLogText,m_pStatusMng,&StatusForm::slotLogText); connect(page3form , &ButtonForm::sigUpdataButton , this , &MainWindow::updataButtonForm); page3layout->addWidget(page3form); it++; } //page3layout->addStretch(); ui->boxWidget->update(); }else { QVector buttons = DataMng::instance()->getPage1ButtonInfo(); if(buttons.isEmpty()) return; QLayout *oldLayout = ui->customWidget->layout(); if (oldLayout) { QLayoutItem *item; while ((item = oldLayout->takeAt(0)) != nullptr) { if (item->widget()) { delete item->widget(); } delete item; } delete oldLayout; } ButtonForm *page1form = new ButtonForm(EN_SYS_STARTUP_PAGE1,m_language,this); page1form->setTitleHide(true); page1form->addToolButtons(buttons); connect(page1form,&ButtonForm::sigLogText,m_pStatusMng,&StatusForm::slotLogText); QGridLayout *page1layout = new QGridLayout(ui->customWidget); page1layout->setSpacing(0); page1layout->setContentsMargins(0,0,0,0); page1layout->addWidget(page1form); ui->customWidget->update(); } } void MainWindow::slot_closeWindow() { #ifdef KBD_ARM QApplication::exit(0); #endif QMap qmap; qmap.clear(); qmap.insert(0 , QString(tr("缩小至托盘"))); qmap.insert(1 , QString(tr("退出程序"))); CheckBoxDialog dia(true , QString(tr("提示")) ,QString(tr("确认退出?")) , qmap , 0, this); if( QDialog::Accepted == dia.exec()) { QList number = dia.getCheckedNumbers(); if( number.indexOf(1) != -1 ) { qApp->quit(); }else { this->hide(); LOGINFO("点击应用关闭按钮,停止工作!"); stopWork(); } } } void MainWindow::closeEvent(QCloseEvent *event) { #ifdef KBD_ARM QApplication::exit(0); #endif event->ignore(); this->hide(); LOGINFO("点击应用关闭按钮,停止工作!"); stopWork(); } void MainWindow::changeEvent(QEvent *e) { QWidget::changeEvent(e); } void MainWindow::keyPressEvent(QKeyEvent *event) { if(event->modifiers().testFlag(Qt::ControlModifier) && event->key() == Qt::Key_B) { DataMng::instance()->setAllowedStatus(true); } } bool MainWindow::isProcStartSuccess(int appId, const QString &strProcName) { int checkCount =0; while(true) { if(checkCount > processOptOuttime) { return false; } QEventLoop eventloop; QTimer::singleShot(1000,&eventloop,SLOT(quit())); eventloop.exec(); vector procVec; float disk = 0.0; DataMng::instance()->getRunProcInfo(procVec,disk); for(size_t index(0);index processOptOuttime) { return false; } QEventLoop eventloop; QTimer::singleShot(1000,&eventloop,SLOT(quit())); eventloop.exec(); vector procVec; float disk = 0.0; DataMng::instance()->getRunProcInfo(procVec,disk); for(size_t index(0);indexblockSignals(block); m_checkObj->blockSignals(block); m_pAppMng->blockAppSignals(block); m_pProcMng->blockAppSignals(block); m_pLocalState->blockSignals(block); } bool MainWindow::checkNet() { QNetworkConfigurationManager manager; #ifdef OS_LINUX QList activeConfigs = manager.allConfigurations(QNetworkConfiguration::Active); bool hasNonLocal = false; for (int i = 0; i < activeConfigs.size(); i++) { if (!activeConfigs[i].identifier().contains("/org/freedesktop/NetworkManager/Settings/")) { hasNonLocal = true; } } #endif if(manager.isOnline() #ifdef OS_LINUX && hasNonLocal #endif ) { m_pStatusMng->netIsOnline(); return true; } else { m_pStatusMng->netIsNotOnline(); return false; } } void MainWindow::checkDog() { if(!m_ptrDogAuthApi) { LOGERROR("创建加密狗实例失败"); m_pStatusMng->dogUncheck(); m_pSysSetMng->setAuthorityLabelText(0); return ; } if(iotSuccess != m_ptrDogAuthApi->checkAuthStatus()) { m_pStatusMng->dogIsNotHave(); m_pSysSetMng->setAuthorityLabelText(1); if(m_bLastDogStatus == true) { m_bLastDogStatus = false; N_MessageBox::information(this , tr("提示") , tr("加密狗退出") , N_MessageBox::Yes); } } else { m_bLastDogStatus = true; m_pStatusMng->dogIsHave(); m_pSysSetMng->setAuthorityLabelText(2); } } void MainWindow::checkSysIsRun() { if( DataMng::instance()->getSysStatus() ) m_pStatusMng->sysIsRun(); else m_pStatusMng->sysIsNotRun(); } void MainWindow::verifyIp() { updateVerifyIp(checkNet() == true ? IP_VERIFY:IP_NOT_VERIFY); } void MainWindow::updateVerifyIp(int verifyValue) { return;//暂时不写sys_ctrl文件,一直写这个文件会存在问题, 在linux系统下,突然断电会经常导致此文件损坏 QString path = iot_public::CFileUtil::getPathOfCfgFile("sys_ctrl.xml",CN_DIR_PLATFORM).c_str(); QFile file(path); if(!file.open(QFile::ReadOnly)) { file.close(); LOGERROR("updateVerifyIp(): 文件打开失败,路径:[%s]",path.toStdString().c_str()); return ; } QDomDocument doc; if(!doc.setContent(&file)) { file.close(); LOGERROR("updateVerifyIp(): 文件读取失败,路径:[%s]",path.toStdString().c_str()); return ; } file.close(); QDomElement root=doc.documentElement(); if(root.tagName()!="root") { LOGERROR("updateVerifyIp(): 文件内容错误,不包含[root],路径:[%s]",path.toStdString().c_str()); return ; } QDomNode module=root.firstChild(); QDomElement element=module.toElement(); if(!element.isNull()) { if(element.nodeName()=="module") { QDomNodeList list=element.childNodes(); for(int i=0;i