HM-SPMS/product/src/sys/sys_startup/MainWindow.cpp
2025-08-30 18:00:55 +08:00

1346 lines
42 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "MainWindow.h"
#include "ui_mainwindow.h"
#include "MonitorCommon.h"
#include "StartUpForm.h"
#include "SheetStyle.h"
#include "AppMngForm.h"
#include "ProcMngForm.h"
#include "DataMng.h"
#include "ButtonForm.h"
#include "BrushDataThread.h"
#include "StatusForm.h"
#include "VersionForm.h"
#include "SetupFunc.h"
#include <QPicture>
#include <QPixmap>
#include <QTabWidget>
#include <QLocalSocket>
#include <QLocalServer>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QTimer>
#include <QThread>
#include <QMessageBox>
#include <QTextCodec>
#include <QNetworkConfigurationManager>
#include <QCloseEvent>
#include <QMenu>
#include <QIcon>
#include <QFile>
#include <QDomComment>
#include <QString>
#include "pub_utility_api/FileUtil.h"
#include <QTabBar>
using namespace sys_startup;
using namespace iot_public;
using namespace iot_idl;
using namespace iot_sys;
static QString restartAppName = QString();
MainWindow::MainWindow(bool isZh, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
m_bRunning(false),
m_pLocalServer(Q_NULLPTR),
m_bIsZh(isZh),
m_pStartUp(Q_NULLPTR),
m_pAppMng(Q_NULLPTR),
m_pProcMng(Q_NULLPTR),
m_pStatusMng(Q_NULLPTR),
m_pVersionMng(Q_NULLPTR),
m_pSysTrayIcon(Q_NULLPTR),
m_updateTreeTimer(Q_NULLPTR),
m_startOrStopButtonTimer(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_pDogMessBox(Q_NULLPTR),
m_pShowAction(nullptr),
m_pExitAction(nullptr)
{
ui->setupUi(this);
DataMng::instance();
initLog();
initMsgBus();
// 初始化变量
initVariables();
// 初始化界面
initUi();
//初始化信号槽
initConn();
// 加载qss样式
initSheet();
//校验ip
verifyIp();
//启动线程和定时器
startThread();
initLocalConnection();
}
MainWindow::~MainWindow()
{
if(m_pDogMessBox)
{
delete m_pDogMessBox;
}
m_pDogMessBox = Q_NULLPTR;
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_startOrStopButtonTimer)
{
m_startOrStopButtonTimer->stop();
delete m_startOrStopButtonTimer;
}
m_startOrStopButtonTimer = 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_startOrStopButtonTimer->start();
m_updateSysTimer->start();
m_dogAndLocalNetTimer->start();
}
void MainWindow::stopWork()
{
m_dogAndLocalNetTimer->stop();
m_updateSysTimer->stop();
m_updateTreeTimer->stop();
m_startOrStopButtonTimer->stop();
m_brushDataThread->suspend();
}
void MainWindow::startAutoStartHmiTimer()
{
m_hmiAutoStartTimer->start();
}
void MainWindow::initSheet()
{
SheetStyle sheetStyle;
QString sheet;
if(sheetStyle.getSheet(sheet))
{
setStyleSheet(sheet);
}else
{
LOGERROR("MainWindow::initSheet():获取样式文件失败!");
}
}
void MainWindow::initVariables()
{
if(m_pStartUp == Q_NULLPTR)
{
m_pStartUp = new StartUpForm;
//ui->startUpFrame->setHidden(true);
}
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_pVersionMng == Q_NULLPTR)
{
m_pVersionMng = new VersionForm;
}
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();
}
QString sysIcon = DataMng::instance()->getSysIcon();
if(sysIcon.isEmpty())
{
setWindowIcon(QIcon(":/sys_startup.ico"));
}else
{
setWindowIcon(QIcon(sysIcon));
}
m_brushDataThread = boost::make_shared<BrushDataThread>();
m_brushDataThread->initialize(DataMng::instance()->getDomainId(),1);
m_updateTreeTimer = new QTimer;
m_updateTreeTimer->setInterval(1000);
m_startOrStopButtonTimer = new QTimer;
m_startOrStopButtonTimer->setInterval(1000);
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()
{
QString title = DataMng::instance()->getTitle();
QString titleSize = DataMng::instance()->getTitleSize();
QString titleFont = DataMng::instance()->getTitleFont();
QString titleColor = DataMng::instance()->getTitleColor();
int height = DataMng::instance()->getHeight();
int titleWidth = DataMng::instance()->getTitleWidth();
int pngWidth = DataMng::instance()->getPngWidth();
int right = DataMng::instance()->getRight();
int left = DataMng::instance()->getLeft();
int buttom = DataMng::instance()->getButtom();
int top = DataMng::instance()->getTop();
QString png = DataMng::instance()->getPng();
setWindowFlags( Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint| Qt::WindowCloseButtonHint );
ui->label->setStyleSheet("");
ui->label->setSizeIncrement(QSize(pngWidth,height));
//标题栏
if(m_bIsZh)
{
//setWindowTitle(DataMng::instance()->getZhTitle());
setWindowTitle("运行管理器");
m_pSysTrayIcon->setToolTip("运行管理器");
}else
{
//setWindowTitle(DataMng::instance()->getEnTitle());
setWindowTitle("Run Manager");
m_pSysTrayIcon->setToolTip("Run Manager");
}
QString sheet=QString("font-size:%1px;color:%2;font:%3;min-width: %4px; max-width: %5px;min-height:%6px;max-height:%7px;").arg(titleSize).arg(titleColor)
.arg(titleFont).arg(titleWidth).arg(titleWidth).arg(height).arg(height);
//ui->label_2->setStyleSheet("font-size:20pt; color:#52a6ff");
ui->label_2->setStyleSheet(sheet);
ui->label_2->setText(title);
//QString sheet3=QString("min-width: %1px; max-width: %2px;min-height:%3px;max-height:%4px;").arg(left).arg(left).arg(height).arg(height);
//QString sheet4=QString("min-width: %1px; max-width: %2px;min-height:%3px;max-height:%4px;").arg(right).arg(right).arg(height).arg(height);
//ui->label_3->setStyleSheet(sheet3);
//ui->label_4->setStyleSheet(sheet4);
ui->horizontalLayout->setContentsMargins(left,top,right,buttom);
//进度条
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);
//首页快捷按钮
ButtonForm *page1form = new ButtonForm(EN_SYS_STARTUP_PAGE1,m_bIsZh,this);
//page1form->setTitleHide(true);
QVector<QString> buttons = DataMng::instance()->getPage1ButtonInfo();
page1form->addToolButtons(buttons);
connect(page1form,&ButtonForm::sigLogText,m_pStatusMng,&StatusForm::slotLogText);
connect(page1form,&ButtonForm::sigButtonClick,this,&MainWindow::slotStartOrStop);
connect(this,&MainWindow::sigButtonStatus,page1form,&ButtonForm::sigEnableStatus);
QGridLayout *page1layout = new QGridLayout(ui->customWidget);
page1layout->setSpacing(0);
page1layout->setContentsMargins(0,0,0,0);
page1layout->addWidget(page1form);
//状态栏视图
QGridLayout *statusLayout = new QGridLayout(ui->status_mng);
statusLayout->setSpacing(6);
statusLayout->setContentsMargins(0, 0, 0, 0);
statusLayout->addWidget(m_pStatusMng);
m_pStatusMng->setStyleSheet("background-color: #eaf9ea;");
ui->label->setPixmap(QPixmap(png));
}
void MainWindow::initConn()
{
connect(m_updateTreeTimer,&QTimer::timeout,m_pAppMng,&AppMngForm::updateAppTree);
connect(m_startOrStopButtonTimer,&QTimer::timeout,this,&MainWindow::slotUpdataStartOrStopStatus);
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(this,&MainWindow::sigStartFormCanClickButton,m_pStartUp,&StartUpForm::slotStartFormCanClickButton);
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_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->tabWidget,&QTabWidget::currentChanged,this,&MainWindow::tabWidget_currentIdxChanged);
}
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)
{
QMessageBox::warning(0,tr("提示"),mess);
}
void MainWindow::reloadAppTree()
{
m_pAppMng->initAppTree();
}
void MainWindow::reloadProcTree()
{
m_pProcMng->initProcTree();
}
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;i<list.size();++i)
{
if(list.at(i)!="")
{
m_pStatusMng->slotLogText(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<SProcess> procVec ;
DataMng::instance()->getRunProcInfo(procVec,disk);
vector<SProcess>::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<SProcess> procVec ;
DataMng::instance()->getRunProcInfo(procVec,disk);
vector<SProcess>::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();
}
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<SProcess> procVec;
float disk = 0.0;
DataMng::instance()->getRunProcInfo(procVec,disk);
for(size_t index(0);index<procVec.size();index++)
{
if(procVec[index].m_appId == appId && procVec[index].m_procName == procName.toStdString())
{
DataMng::instance()->setAllowedStatus(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<SProcess> procVec;
float disk = 0.0;
DataMng::instance()->getRunProcInfo(procVec,disk);
for(size_t index(0);index<procVec.size();index++)
{
if(procVec[index].m_appId == appId && procVec[index].m_procName == procName.toStdString())
{
DataMng::instance()->setAllowedStatus(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<SProcess> procVec;
float disk = 0.0;
DataMng::instance()->getRunProcInfo(procVec,disk);
for(size_t index(0);index<procVec.size();index++)
{
if(procVec[index].m_appId == appId && procVec[index].m_procName == procName.toStdString())
{
DataMng::instance()->setAllowedStatus(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"<<QString::number(procVec[index].m_procId)<<"/T"<<"/F";
#else
stList<<"kill"<<"-9"<<QString::number(procVec[index].m_procId);
#endif
process.start( "cmd", stList);
process.waitForStarted();
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);
break;
}
}
}
void MainWindow::dogAndNetCheck()
{
checkNet();
checkDog();
}
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)
{
QMessageBox::StandardButton result = QMessageBox::question(this,tr("错误"),tr("系统模型配置信息异常,是否打开系统拓扑工具检查配置并导出?"));
if(result == QMessageBox::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);
//更新状态
QString text = tr("系统正在启动...");
m_pStatusMng->slotLogText(text);
m_pProcess->start(m_strSysCtrlPath);
m_startSysTimer->start();
m_pProcess->waitForStarted();
}
void MainWindow::tabWidget_currentIdxChanged(int /*index*/)
{
if( ui->tabWidget->currentWidget() == ui->app_mng)
{
disconnect(m_updateTreeTimer,&QTimer::timeout,m_pProcMng,&ProcMngForm::updateProcTree);
connect(m_updateTreeTimer,&QTimer::timeout,m_pAppMng,&AppMngForm::updateAppTree);
}
else if( ui->tabWidget->currentWidget() == ui->proc_mng )
{
connect(m_updateTreeTimer,&QTimer::timeout,m_pProcMng,&ProcMngForm::updateProcTree);
disconnect(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);
}
}
void MainWindow::slotStartOrStop()
{
emit sigStartFormCanClickButton();
}
void MainWindow::slotUpdataStartOrStopStatus()
{
bool enable =DataMng::instance()->getAllowedStatus();
emit sigButtonStatus(enable);
}
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<SProcess> procVec;
float disk = 0.0;
DataMng::instance()->getRunProcInfo(procVec,disk);
for(size_t index(0);index<procVec.size();index++)
{
if(procVec[index].m_appId == appId && procVec[index].m_procName == strProcName.toStdString() && procVec[index].m_isStartUp == EN_PROC_RUNNING)
{
return true;
}
}
checkCount++;
}
return false;
}
bool MainWindow::isProcStopSuccess(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<SProcess> procVec;
float disk = 0.0;
DataMng::instance()->getRunProcInfo(procVec,disk);
for(size_t index(0);index<procVec.size();index++)
{
if(procVec[index].m_appId == appId && procVec[index].m_procName == strProcName.toStdString() && procVec[index].m_isStartUp == EN_PROC_STOP)
{
return true;
}
}
checkCount++;
}
return false;
}
void MainWindow::blockSignal(bool block)
{
m_updateSysTimer->blockSignals(block);
m_checkObj->blockSignals(block);
m_pAppMng->blockAppSignals(block);
m_pProcMng->blockAppSignals(block);
}
bool MainWindow::checkNet()
{
QNetworkConfigurationManager manager;
#ifdef OS_LINUX
QList<QNetworkConfiguration> 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
)
{
return true;
}
else
{
return false;
}
}
void MainWindow::checkDog()
{
if(!m_ptrDogAuthApi)
{
LOGERROR("创建加密狗实例失败");
return ;
}
if(iotSuccess != m_ptrDogAuthApi->checkAuthStatus())
{
if(m_bLastDogStatus == true)
{
m_bLastDogStatus = false;
if(m_pDogMessBox == Q_NULLPTR)
{
m_pDogMessBox = new QMessageBox(this);
m_pDogMessBox->setWindowTitle(tr("提示"));
m_pDogMessBox->setText(tr("加密狗退出"));
m_pDogMessBox->show();
}
}
}
else
{
m_bLastDogStatus = true;
if(m_pDogMessBox != Q_NULLPTR)
{
delete m_pDogMessBox;
}
m_pDogMessBox = Q_NULLPTR;
}
}
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<list.count();++i)
{
QDomNode node=list.at(i);
if(node.toElement().attribute("key")=="verify_ip")
{
node.toElement().setAttribute("value",verifyValue);
if(!file.open(QFile::WriteOnly|QFile::Truncate))
{
return ;
}
QTextStream ts(&file);
doc.save(ts,4);
file.close();
}
}
}
}
}
void MainWindow::openHmi()
{
#ifdef Q_OS_WIN
const std::string strFileName = "hmi_explorer.bat";
const std::string strPath = std::move( iot_public::CFileUtil::getPathOfBinFile(strFileName));
if(strPath.empty())
{
LOGERROR("未找到脚本文件[%s]",strFileName.c_str());
return;
}
system(strPath.c_str());
#else
const std::string strFileName = "hmi_explorer.sh";
const std::string strPath = std::move( iot_public::CFileUtil::getPathOfBinFile(strFileName));
if(strPath.empty())
{
LOGERROR("未找到脚本文件[%s]",strFileName.c_str());
return;
}
QProcess::startDetached(QString("bash -c '%1'").arg(strPath.c_str()));
#endif
}