- added parents
- waiting request are deleted after completed successfully
This commit is contained in:
parent
c571cb8c54
commit
5ad720ecf3
4 changed files with 39 additions and 27 deletions
|
@ -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);
|
||||
|
|
|
@ -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){
|
||||
bool replyFound = false;
|
||||
|
||||
// find reply and delete the request from waiting list
|
||||
for(int i = 0; i<this->waitingRequests.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);
|
||||
}
|
||||
}
|
||||
|
||||
if(!replyFound) {
|
||||
// some internal error occured
|
||||
return {{"status", 900}, {"data", ""}};
|
||||
}
|
||||
|
||||
if(timer->remainingTime() == -1){
|
||||
//the time has been triggered -> timeout
|
||||
|
||||
return {{"status", 911}, {"data", ""}};
|
||||
}
|
||||
|
||||
for(int i = 0; i<this->waitingRequests.length(); i++){
|
||||
if(this->waitingRequests[i].id == thisId){
|
||||
reply = this->waitingRequests[i].reply;
|
||||
}
|
||||
}
|
||||
|
||||
// stop the timer as the connection has been established
|
||||
timer.stop();
|
||||
delete timer;
|
||||
|
||||
return {{"status", reply.value("header").toInt()}, {"data", reply.value("data").toVariant()}};
|
||||
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -110,6 +110,7 @@ QString SpeedTimer::getState(){
|
|||
case CANCELLED:
|
||||
return("CANCELLED");
|
||||
}
|
||||
return "ERROR";
|
||||
}
|
||||
|
||||
double SpeedTimer::getCurrTime() {
|
||||
|
|
Reference in a new issue