diff --git a/ScStwLibraries/headers/ScStw.hpp b/ScStwLibraries/headers/ScStw.hpp index d7666b1..705b12d 100644 --- a/ScStwLibraries/headers/ScStw.hpp +++ b/ScStwLibraries/headers/ScStw.hpp @@ -147,10 +147,10 @@ public: * \brief The ExtensionState enum contains all possible states of an extension */ enum ExtensionState { - Disconnected = 0, - Connecting = 1, - Initialising = 2, - Connected = 3 + ExtensionDisconnected = 0, + ExtensionConnecting = 1, + ExtensionInitialising = 2, + ExtensionConnected = 3 }; Q_ENUM(ExtensionState); diff --git a/ScStwLibraries/headers/scstwrace.h b/ScStwLibraries/headers/scstwrace.h index 2feff1b..bf0e28d 100644 --- a/ScStwLibraries/headers/scstwrace.h +++ b/ScStwLibraries/headers/scstwrace.h @@ -56,7 +56,7 @@ class ScStwRace : public QObject public: explicit ScStwRace(QObject *parent = nullptr); - enum RaceState { IDLE, PREPAIRING, WAITING, STARTING, RUNNING, STOPPED }; + enum RaceState { IDLE, PREPAIRING, WAITING, STARTING, RUNNING, STOPPED, INCIDENT }; Q_ENUM(RaceState) enum StartAction { None = -1, AtYourMarks = 0, Ready = 1, Start = 2 }; @@ -79,7 +79,6 @@ private: QTimer *nextActionTimer; QEventLoop *nextActionLoop; - QTimer *climberReadyWaitTimer; QEventLoop *climberReadyWaitLoop; // sounds diff --git a/ScStwLibraries/sources/scstwrace.cpp b/ScStwLibraries/sources/scstwrace.cpp index 74a545a..e3af55c 100644 --- a/ScStwLibraries/sources/scstwrace.cpp +++ b/ScStwLibraries/sources/scstwrace.cpp @@ -58,7 +58,7 @@ int ScStwRace::start(bool asyncronous) { if(timer->getState() == ScStwTimer::DISABLED) continue; - if(timer->getReadyState() != ScStwTimer::ClimberIsNotReady) { + if(timer->getReadyState() == ScStwTimer::ExtensionIsNotConnected || timer->getReadyState() == ScStwTimer::ExtensionBatteryNotFine) { if(this->allowAutomaticTimerDisable) { timer->setDisabled(true); @@ -70,13 +70,13 @@ int ScStwRace::start(bool asyncronous) { timer->technicalIncident(); foreach (ScStwTimer *subTimer, this->timers) { - if(timer != subTimer && subTimer->getReadyState() != ScStwTimer::ClimberIsNotReady) + if(timer != subTimer && (timer->getReadyState() == ScStwTimer::ExtensionIsNotConnected || timer->getReadyState() == ScStwTimer::ExtensionBatteryNotFine)) subTimer->technicalIncident(); else if(timer != subTimer) subTimer->setState(ScStwTimer::CANCELLED); } - this->setState(STOPPED); + this->setState(INCIDENT); qDebug() << "[ERROR][RACE] Could not start due to not-ready timers"; @@ -161,7 +161,7 @@ void ScStwRace::handleTimerStop() { } int ScStwRace::reset() { - if(this->state != STOPPED) { + if(this->state != STOPPED && this->state != INCIDENT) { return ScStw::CurrentStateNotVaildForOperationError; } @@ -183,7 +183,7 @@ int ScStwRace::reset() { } int ScStwRace::cancel() { - if(this->state != STARTING && this->state != RUNNING) + if(this->state != PREPAIRING && this->state != WAITING && this->state != STARTING && this->state != RUNNING) return ScStw::CurrentStateNotVaildForOperationError; qDebug() << "[INFO][RACE] cancelling race"; @@ -191,6 +191,7 @@ int ScStwRace::cancel() { this->soundPlayer->cancel(this->soundVolume); this->nextActionTimer->stop(); this->nextActionLoop->quit(); + this->climberReadyWaitLoop->quit(); this->nextStartAction = None; int returnCode = ScStw::Success; @@ -230,6 +231,10 @@ bool ScStwRace::playSoundsAndStartTimers() { return false; } + // check if the start was cancelled + if(this->state != PREPAIRING) + return false; + qDebug() << "NOW IN WAITING"; this->setState(WAITING); @@ -240,42 +245,51 @@ bool ScStwRace::playSoundsAndStartTimers() { // if the automatic ready tone is enabled, wait for the climbers to become ready if(this->startActionSettings.contains(Ready) && this->startActionSettings[Ready]["Enabled"].toBool()) { - qDebug() << "[RACE][INFO] Now waiting for climbers"; + qDebug() << "[RACE][INFO] Now waiting for climbers"; - // get delay - int minimumReadyDelay = 1000; - if(this->startActionSettings[Ready]["Delay"].toInt() > 1000 || !this->allowAutomaticTimerDisable) - minimumReadyDelay = this->startActionSettings[Ready]["Delay"].toInt(); + // get delay + int minimumReadyDelay = 1000; + if(this->startActionSettings[Ready]["Delay"].toInt() > 1000 || !this->allowAutomaticTimerDisable) + minimumReadyDelay = this->startActionSettings[Ready]["Delay"].toInt(); - // wait for climbers to become ready initially - bool allClimbersReady = false; - while (!allClimbersReady) { - allClimbersReady = true; - foreach (ScStwTimer *timer, this->timers) { - if(timer->getReadyState() != ScStwTimer::IsReady) - allClimbersReady = false; - } - - if(!allClimbersReady) - this->climberReadyWaitLoop->exec(); + // wait for climbers to become ready initially + bool allClimbersReady = false; + while (!allClimbersReady) { + allClimbersReady = true; + foreach (ScStwTimer *timer, this->timers) { + if(timer->getReadyState() != ScStwTimer::IsReady) + allClimbersReady = false; } - qDebug() << "[RACE][DEBUG] Initial wait finished"; - - // wait for all climbers to be ready for the ReadyActionDelay, but at least one second continuosly - // the climber ready wait loop will also quit, if the climber steps of the pad - // -> wait for both climbers to stand on the pad for at least one second - do { - this->nextActionTimer->stop(); - this->nextActionTimer->start(minimumReadyDelay); + if(!allClimbersReady) this->climberReadyWaitLoop->exec(); - } while(this->nextActionTimer->remainingTime() > 0); - qDebug() << "[RACE][DEBUG] Wait finished, starting now!"; - - // play ready tone - if(!this->soundPlayer->play(Ready, this->soundVolume)) + // check if the start was cancelled + if(this->state != WAITING) return false; + } + + qDebug() << "[RACE][DEBUG] Initial wait finished"; + + // wait for all climbers to be ready for the ReadyActionDelay, but at least one second continuosly + // the climber ready wait loop will also quit, if the climber steps of the pad + // -> wait for both climbers to stand on the pad for at least one second + do { + this->nextActionTimer->stop(); + this->nextActionTimer->start(minimumReadyDelay); + this->climberReadyWaitLoop->exec(); + + // check if the start was cancelled + if(this->state != WAITING) + return false; + + } while(this->nextActionTimer->remainingTime() > 0); + + qDebug() << "[RACE][DEBUG] Wait finished, starting now!"; + + // play ready tone + if(!this->soundPlayer->play(Ready, this->soundVolume)) + return false; } // enter starting state