added program lock, only one instance

This commit is contained in:
carina@hades 2023-08-17 12:50:45 -04:00
parent 58dcd0d717
commit 31eefc2d92
4 changed files with 59 additions and 2 deletions

2
.gitignore vendored
View File

@ -8,6 +8,8 @@ programSettings.txt
EventKenshikushi
DataGenerator
DataReaderScript
pid.dat
DAQLock.dat
data

View File

@ -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("<font style=\"color: green;\"><b>ACQ On</b></font>");
influx->AddDataPoint("SavingData,ExpName=" + elogName.toStdString() + " value=1");
}else{
lbScalarACQStatus->setText("<font style=\"color: red;\"><b>ACQ Off</b></font>");
influx->AddDataPoint("SavingData,ExpName=" + elogName.toStdString() + " value=0");
}
influx->WriteData(dataBaseName.toStdString());
influx->ClearDataPointsBuffer();
}
if( canvas ){

View File

@ -15,6 +15,9 @@
#define SETTINGSIZE 2048
#define DAQLockFile "DAQLock.dat"
#define PIDFile "pid.dat"
#include <sys/time.h> /** struct timeval, select() */
inline unsigned int get_time(){

View File

@ -1,10 +1,55 @@
#include "FSUDAQ.h"
#include <QApplication>
#include <QMessageBox>
#include <QProcess>
#include <QPushButton>
#include <QFile>
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 <pid>\" 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();