From 31eefc2d92e536c3367aee5c93208a0f7036e2eb Mon Sep 17 00:00:00 2001 From: "carina@hades" Date: Thu, 17 Aug 2023 12:50:45 -0400 Subject: [PATCH] added program lock, only one instance --- .gitignore | 2 ++ FSUDAQ.cpp | 11 +++++++++-- macro.h | 3 +++ main.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 2eef895..9aee1f1 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,8 @@ programSettings.txt EventKenshikushi DataGenerator DataReaderScript +pid.dat +DAQLock.dat data diff --git a/FSUDAQ.cpp b/FSUDAQ.cpp index 93ec271..6a0b0cf 100644 --- a/FSUDAQ.cpp +++ b/FSUDAQ.cpp @@ -337,6 +337,9 @@ MainWindow::~MainWindow(){ delete influx; + printf("-------- remove %s\n", DAQLockFile); + remove(DAQLockFile); + } //*************************************************************** @@ -984,7 +987,7 @@ void MainWindow::StartACQ(){ if( onlineAnalyzer ) onlineAnalyzer->StartThread(); {//^=== elog and database - if( influx && chkSaveData->isChecked() ){ + if( influx ){ influx->AddDataPoint("RunID value=" + std::to_string(runID)); influx->AddDataPoint("SavingData,ExpName=" + elogName.toStdString() + " value=1"); influx->WriteData(dataBaseName.toStdString()); @@ -1055,7 +1058,7 @@ void MainWindow::StopACQ(){ cbAutoRun->setEnabled(true); {//^=== elog and database - if( influx && chkSaveData->isChecked() ){ + if( influx ){ influx->AddDataPoint("SavingData,ExpName=" + elogName.toStdString() + " value=0"); influx->WriteData(dataBaseName.toStdString()); influx->ClearDataPointsBuffer(); @@ -1349,9 +1352,13 @@ void MainWindow::OpenScope(){ if( scope ) { if( onOff ) { lbScalarACQStatus->setText("ACQ On"); + influx->AddDataPoint("SavingData,ExpName=" + elogName.toStdString() + " value=1"); }else{ lbScalarACQStatus->setText("ACQ Off"); + influx->AddDataPoint("SavingData,ExpName=" + elogName.toStdString() + " value=0"); } + influx->WriteData(dataBaseName.toStdString()); + influx->ClearDataPointsBuffer(); } if( canvas ){ diff --git a/macro.h b/macro.h index 3d18413..4ee75ac 100644 --- a/macro.h +++ b/macro.h @@ -15,6 +15,9 @@ #define SETTINGSIZE 2048 +#define DAQLockFile "DAQLock.dat" +#define PIDFile "pid.dat" + #include /** struct timeval, select() */ inline unsigned int get_time(){ diff --git a/main.cpp b/main.cpp index f57ccdc..2643467 100644 --- a/main.cpp +++ b/main.cpp @@ -1,10 +1,55 @@ #include "FSUDAQ.h" #include +#include +#include +#include +#include int main(int argc, char *argv[]){ QApplication a(argc, argv); + bool isLock = false; + int pid = 0; + QFile lockFile(DAQLockFile); + if( lockFile.open(QIODevice::Text | QIODevice::ReadOnly) ){ + QTextStream in(&lockFile); + QString line = in.readLine(); + isLock = line.toInt(); + lockFile.close(); + } + QFile pidFile(PIDFile); + if( pidFile.open(QIODevice::Text | QIODevice::ReadOnly)){ + QTextStream in(&pidFile); + QString line = in.readLine(); + pid = line.toInt(); + pidFile.close(); + } + if( isLock ) { + qDebug() << "The DAQ program is already opened. PID is " + QString::number(pid) + ", and delete the " + DAQLockFile ; + QMessageBox msgBox; + msgBox.setWindowTitle("Oopss...."); + msgBox.setText("The DAQ program is already opened, or crashed perviously. \nPID is " + QString::number(pid) + "\n You can kill the procee by \"kill -9 \" and delete the " + DAQLockFile + "\n or click the \"Kill\" button"); + msgBox.setIcon(QMessageBox::Information); + + QPushButton * kill = msgBox.addButton("Kill and Open New", QMessageBox::AcceptRole); + msgBox.setStandardButtons(QMessageBox::Ok); + msgBox.setDefaultButton(QMessageBox::Ok); + msgBox.exec(); + if(msgBox.clickedButton() == kill){ + remove(DAQLockFile); + QProcess::execute("kill", QStringList() << "-9" << QString::number(pid)); + }else{ + return 0; + } + } + lockFile.open(QIODevice::Text | QIODevice::WriteOnly); + lockFile.write( "1" ); + lockFile.close(); + pidFile.open(QIODevice::Text | QIODevice::WriteOnly); + pidFile.write( QString::number(QCoreApplication::applicationPid() ).toStdString().c_str() ); + pidFile.close(); + MainWindow w; w.show(); return a.exec();