From 5ad720ecf3967691211183c4e31f29125775624b Mon Sep 17 00:00:00 2001 From: dorian Date: Tue, 20 Aug 2019 10:19:35 +0200 Subject: [PATCH] - added parents - waiting request are deleted after completed successfully --- sources/appsettings.cpp | 2 +- sources/baseconn.cpp | 51 ++++++++++++++++++++++++---------------- sources/climbingrace.cpp | 12 +++++----- sources/speedtimer.cpp | 1 + 4 files changed, 39 insertions(+), 27 deletions(-) diff --git a/sources/appsettings.cpp b/sources/appsettings.cpp index ec0c601..9bff130 100644 --- a/sources/appsettings.cpp +++ b/sources/appsettings.cpp @@ -24,7 +24,7 @@ AppSettings::AppSettings(QObject* parent) { QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); - this->settingsManager = new QSettings(path+"/settings.ini", QSettings::IniFormat); + this->settingsManager = new QSettings(path+"/settings.ini", QSettings::IniFormat, this); this->setDefaultSetting("ready_en", "false"); this->setDefaultSetting("ready_delay", 0); diff --git a/sources/baseconn.cpp b/sources/baseconn.cpp index 94605c4..a1c2129 100644 --- a/sources/baseconn.cpp +++ b/sources/baseconn.cpp @@ -5,7 +5,7 @@ BaseConn * pGlobalBaseConn = nullptr; BaseConn::BaseConn(QObject *parent) : QObject(parent) { pGlobalBaseConn = this; - socket = new QTcpSocket(); + socket = new QTcpSocket(this); this->setState("disconnected"); connect(this->socket, SIGNAL(error(QAbstractSocket::SocketError)), @@ -143,54 +143,65 @@ QVariantMap BaseConn::sendCommand(int header, QJsonValue data){ // generate id and witing requests entry int thisId = nextConnectionId; - //qDebug() << "sending command: " << header << " with data: " << data << " and id: " << thisId; + qDebug() << "sending command: " << header << " with data: " << data << " and id: " << thisId; nextConnectionId ++; - QEventLoop loop; + QEventLoop *loop = new QEventLoop(this); + QTimer *timer = new QTimer(this); QJsonObject reply; - this->waitingRequests.append({thisId, &loop, reply}); + this->waitingRequests.append({thisId, loop, reply}); QJsonObject requestObj; requestObj.insert("id", thisId); requestObj.insert("header", header); requestObj.insert("data", data); - QString jsonRequest = QJsonDocument(requestObj).toJson(); - QTimer timer; - - timer.setSingleShot(true); + timer->setSingleShot(true); // quit the loop when the timer times out - loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); + loop->connect(timer, SIGNAL(timeout()), loop, SLOT(quit())); // quit the loop when the connection was established // loop.connect(this, &BaseConn::gotReply, &loop, &QEventLoop::quit); // start the timer before starting to connect - timer.start(3000); + timer->start(3000); //write data - socket->write(jsonRequest.toLatin1()); //wait for an answer to finish (programm gets stuck in here) - loop.exec(); + loop->exec(); - //loop finished - if(timer.remainingTime() == -1){ - //the time has been triggered -> timeout - - return {{"status", 911}, {"data", ""}}; - } + bool replyFound = false; + // find reply and delete the request from waiting list for(int i = 0; iwaitingRequests.length(); i++){ if(this->waitingRequests[i].id == thisId){ + // request was found + replyFound = true; + // delete event loop + if(this->waitingRequests[i].loop != nullptr) { + delete this->waitingRequests[i].loop; + } + // store reply reply = this->waitingRequests[i].reply; + // remove reply from waiting list + this->waitingRequests.removeAt(i); } } - // stop the timer as the connection has been established - timer.stop(); + if(!replyFound) { + // some internal error occured + return {{"status", 900}, {"data", ""}}; + } + + if(timer->remainingTime() == -1){ + //the time has been triggered -> timeout + return {{"status", 911}, {"data", ""}}; + } + + delete timer; return {{"status", reply.value("header").toInt()}, {"data", reply.value("data").toVariant()}}; diff --git a/sources/climbingrace.cpp b/sources/climbingrace.cpp index d4f54bc..4613f3e 100644 --- a/sources/climbingrace.cpp +++ b/sources/climbingrace.cpp @@ -15,15 +15,15 @@ ClimbingRace::ClimbingRace(QObject *parent) : QObject(parent) this->state = IDLE; this->mode = LOCAL; - this->appSettings = new AppSettings; - this->baseConn = new BaseConn; + this->appSettings = new AppSettings(this); + this->baseConn = new BaseConn(this); this->baseConn->setIP(pGlobalAppSettings->loadSetting("baseStationIpAdress")); connect(this->baseConn, &BaseConn::stateChanged, this, &ClimbingRace::baseStationStateChanged); connect(this->baseConn, &BaseConn::stateChanged, this, &ClimbingRace::refreshMode); connect(this->baseConn, &BaseConn::connectionsChanged, this, &ClimbingRace::baseStationConnectionsChanged); - this->speedTimers.append( new SpeedTimer ); + this->speedTimers.append( new SpeedTimer(this) ); this->player = new QMediaPlayer; this->date = new QDateTime; @@ -31,13 +31,13 @@ ClimbingRace::ClimbingRace(QObject *parent) : QObject(parent) this->nextStartActionTimer = new QTimer(this); nextStartActionTimer->setSingleShot(true); - this->baseStationSyncTimer = new QTimer(); + this->baseStationSyncTimer = new QTimer(this); this->baseStationSyncTimer->setInterval(100); this->baseStationSyncTimer->setSingleShot(true); this->baseStationSyncTimer->connect(this->baseStationSyncTimer, &QTimer::timeout, this, &ClimbingRace::syncWithBaseStation); this->baseStationSyncTimer->start(); - this->timerTextRefreshTimer = new QTimer(); + this->timerTextRefreshTimer = new QTimer(this); this->timerTextRefreshTimer->setInterval(1); this->timerTextRefreshTimer->setSingleShot(true); this->timerTextRefreshTimer->connect(this->timerTextRefreshTimer, &QTimer::timeout, this, &ClimbingRace::refreshTimerText); @@ -460,7 +460,7 @@ bool ClimbingRace::refreshRemoteTimers() { foreach(QVariant remTimer, timers){ // create a local timer for each remote timer - this->speedTimers.append(new SpeedTimer); + this->speedTimers.append(new SpeedTimer(this)); } } diff --git a/sources/speedtimer.cpp b/sources/speedtimer.cpp index 549d647..ead2087 100644 --- a/sources/speedtimer.cpp +++ b/sources/speedtimer.cpp @@ -110,6 +110,7 @@ QString SpeedTimer::getState(){ case CANCELLED: return("CANCELLED"); } + return "ERROR"; } double SpeedTimer::getCurrTime() {