some cleanup
This commit is contained in:
parent
f985197ffa
commit
130213d09d
5 changed files with 78 additions and 80 deletions
|
@ -87,7 +87,10 @@ public:
|
||||||
Error = 900,
|
Error = 900,
|
||||||
NotConnectedError = 910,
|
NotConnectedError = 910,
|
||||||
TimeoutError = 911,
|
TimeoutError = 911,
|
||||||
SettingNotAccessibleError = 901
|
SettingNotAccessibleError = 901,
|
||||||
|
CurrentStateNotVaildForOperation = 904,
|
||||||
|
InternalError = 950,
|
||||||
|
InternalErrorTimerOperationFailed = 951
|
||||||
};
|
};
|
||||||
Q_ENUM(ScStw::StatusCode)
|
Q_ENUM(ScStw::StatusCode)
|
||||||
|
|
||||||
|
|
|
@ -80,10 +80,28 @@ private:
|
||||||
|
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
/*!
|
||||||
|
* \brief Function to start the race
|
||||||
|
*
|
||||||
|
* \param asyncronous if the function should just start the start sequence and then quit (true)
|
||||||
|
* or if if should wait until the start sequence is over and quit after that (false)
|
||||||
|
* \return 200: OK; 904: state not matching
|
||||||
|
*/
|
||||||
int start(bool asyncronous = true);
|
int start(bool asyncronous = true);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Function to stop the currently running race
|
||||||
|
*
|
||||||
|
* \return 200: OK; 904: state not matching
|
||||||
|
*/
|
||||||
int stop();
|
int stop();
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Function to reset a stopped race
|
||||||
|
* \return
|
||||||
|
*/
|
||||||
int reset();
|
int reset();
|
||||||
void cancelStart(bool falseStart);
|
int cancel();
|
||||||
|
|
||||||
// setters
|
// setters
|
||||||
bool writeStartActionSetting(StartAction action, bool enabled, int delay);
|
bool writeStartActionSetting(StartAction action, bool enabled, int delay);
|
||||||
|
@ -102,7 +120,13 @@ protected slots:
|
||||||
private slots:
|
private slots:
|
||||||
void refreshTimerStates();
|
void refreshTimerStates();
|
||||||
void handleTimerEnable(ScStwTimer* timer);
|
void handleTimerEnable(ScStwTimer* timer);
|
||||||
|
int handleFalseStart();
|
||||||
bool playSoundsAndStartTimers(StartAction thisAction);
|
bool playSoundsAndStartTimers(StartAction thisAction);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Function to declare the winner and looser timers after a timer has been stopped
|
||||||
|
*/
|
||||||
void handleTimerStop();
|
void handleTimerStop();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
|
|
|
@ -305,12 +305,6 @@ signals:
|
||||||
*/
|
*/
|
||||||
void stateChanged();
|
void stateChanged();
|
||||||
|
|
||||||
/*!
|
|
||||||
* \brief Emitted when the start was canceled
|
|
||||||
* \param falseStart Wheter a false start occured
|
|
||||||
*/
|
|
||||||
void startCanceled(bool falseStart);
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Emitted when the reaction time changed
|
* \brief Emitted when the reaction time changed
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -27,15 +27,9 @@ ScStwRace::ScStwRace(QObject *parent) : QObject(parent)
|
||||||
// --- Main Functionality ---
|
// --- Main Functionality ---
|
||||||
// --------------------------
|
// --------------------------
|
||||||
|
|
||||||
/**
|
|
||||||
* Function to start the race
|
|
||||||
*
|
|
||||||
* @brief ScStwRace::startRace
|
|
||||||
* @return {int} 200: OK; 904: MAIN state not matching
|
|
||||||
*/
|
|
||||||
int ScStwRace::start(bool asyncronous) {
|
int ScStwRace::start(bool asyncronous) {
|
||||||
if(this->state != IDLE) {
|
if(this->state != IDLE) {
|
||||||
return 904;
|
return ScStw::CurrentStateNotVaildForOperation;
|
||||||
}
|
}
|
||||||
|
|
||||||
qDebug() << "+ [INFO] starting race";
|
qDebug() << "+ [INFO] starting race";
|
||||||
|
@ -48,47 +42,33 @@ int ScStwRace::start(bool asyncronous) {
|
||||||
else
|
else
|
||||||
this->playSoundsAndStartTimers(None);
|
this->playSoundsAndStartTimers(None);
|
||||||
|
|
||||||
return 200;
|
return ScStw::Success;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Function to stop the currently running race
|
|
||||||
*
|
|
||||||
* @brief ScStwRace::stopRace
|
|
||||||
* @param {int} type 0: stop; 1: cancel; 2: false start
|
|
||||||
* @return {int} 200: OK; 904: MAIN state not matching
|
|
||||||
*/
|
|
||||||
int ScStwRace::stop() {
|
int ScStwRace::stop() {
|
||||||
if(this->state != RUNNING && this->state != STARTING) {
|
if(this->state != RUNNING && this->state != STARTING) {
|
||||||
return 904;
|
return ScStw::CurrentStateNotVaildForOperation;
|
||||||
}
|
}
|
||||||
|
|
||||||
qDebug() << "+ [INFO] stopping race";
|
qDebug() << "+ [INFO] stopping race";
|
||||||
|
|
||||||
double timeOfStop = QDateTime::currentMSecsSinceEpoch();
|
double timeOfStop = QDateTime::currentMSecsSinceEpoch();
|
||||||
|
|
||||||
int returnCode = 900;
|
int returnCode = ScStw::Success;
|
||||||
|
|
||||||
bool stopOk = true;
|
|
||||||
|
|
||||||
foreach(ScStwTimer *speedTimer, this->timers){
|
foreach(ScStwTimer *speedTimer, this->timers){
|
||||||
if(!speedTimer->stop(timeOfStop) && speedTimer->getState() != ScStwTimer::DISABLED){
|
if(!speedTimer->stop(timeOfStop) && speedTimer->getState() != ScStwTimer::DISABLED){
|
||||||
stopOk = false;
|
returnCode = ScStw::InternalErrorTimerOperationFailed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
returnCode = stopOk ? 200:904;
|
if(returnCode == ScStw::Success) {
|
||||||
|
|
||||||
if(returnCode == 200) {
|
|
||||||
this->setState(STOPPED);
|
this->setState(STOPPED);
|
||||||
}
|
}
|
||||||
|
|
||||||
return returnCode;
|
return returnCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief ScStwRace::handleTimerStop Function to declare the winner and looser timers after a timer has been stopped
|
|
||||||
*/
|
|
||||||
void ScStwRace::handleTimerStop() {
|
void ScStwRace::handleTimerStop() {
|
||||||
if(this->state == RUNNING) {
|
if(this->state == RUNNING) {
|
||||||
// find out which timer has won
|
// find out which timer has won
|
||||||
|
@ -129,56 +109,64 @@ void ScStwRace::handleTimerStop() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief ScStwRace::resetRace
|
|
||||||
* @return {int} 200: OK; 904: MAIN state not matching
|
|
||||||
*/
|
|
||||||
int ScStwRace::reset() {
|
int ScStwRace::reset() {
|
||||||
if(this->state != STOPPED) {
|
if(this->state != STOPPED) {
|
||||||
return 904;
|
return ScStw::CurrentStateNotVaildForOperation;
|
||||||
}
|
}
|
||||||
|
|
||||||
qDebug() << "+ [INFO] resetting race";
|
qDebug() << "+ [INFO] resetting race";
|
||||||
|
|
||||||
int returnCode = 200;
|
int returnCode = ScStw::Success;
|
||||||
|
|
||||||
foreach(ScStwTimer *speedTimer, this->timers){
|
foreach(ScStwTimer *speedTimer, this->timers){
|
||||||
if(!speedTimer->reset() && speedTimer->getState() != ScStwTimer::DISABLED) {
|
if(!speedTimer->reset() && speedTimer->getState() != ScStwTimer::DISABLED) {
|
||||||
returnCode = 904;
|
returnCode = ScStw::InternalErrorTimerOperationFailed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(returnCode == 200){
|
if(returnCode == ScStw::Success){
|
||||||
this->setState(IDLE);
|
this->setState(IDLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
return returnCode;
|
return returnCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScStwRace::cancelStart(bool falseStart){
|
int ScStwRace::cancel() {
|
||||||
// TODO: FIXME
|
if(this->state != STARTING && this->state != RUNNING)
|
||||||
qDebug() << "+ [INFO] cancelling race: " << falseStart;
|
return ScStw::CurrentStateNotVaildForOperation;
|
||||||
|
|
||||||
if(falseStart){
|
qDebug() << "[INFO][RACE] cancelling race";
|
||||||
// cancel all running timers
|
|
||||||
foreach(ScStwTimer *timer, this->timers) {
|
|
||||||
timer->cancel();
|
|
||||||
}
|
|
||||||
|
|
||||||
this->setState(STOPPED);
|
this->soundPlayer->cancel(this->soundVolume);
|
||||||
|
this->nextActionTimer->stop();
|
||||||
|
this->nextActionLoop->quit();
|
||||||
|
this->nextStartAction = None;
|
||||||
|
|
||||||
this->soundPlayer->cancel(this->soundVolume);
|
int returnCode = ScStw::Success;
|
||||||
|
|
||||||
|
foreach(ScStwTimer *timer, this->timers){
|
||||||
|
if(!timer->cancel() && timer->getState() != ScStwTimer::DISABLED)
|
||||||
|
returnCode = ScStw::InternalErrorTimerOperationFailed;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
this->soundPlayer->cancel(this->soundVolume);
|
|
||||||
this->nextActionTimer->stop();
|
|
||||||
this->nextActionLoop->quit();
|
|
||||||
this->nextStartAction = None;
|
|
||||||
|
|
||||||
foreach(ScStwTimer *timer, this->timers){
|
return returnCode;
|
||||||
timer->cancel();
|
}
|
||||||
}
|
|
||||||
|
int ScStwRace::handleFalseStart() {
|
||||||
|
if(this->getState() != STARTING && this->getState() != RUNNING)
|
||||||
|
return ScStw::CurrentStateNotVaildForOperation;
|
||||||
|
|
||||||
|
int returnCode = ScStw::Success;
|
||||||
|
// cancel all running timers
|
||||||
|
foreach(ScStwTimer *timer, this->timers) {
|
||||||
|
if(!timer->cancel() && timer->getState() != ScStwTimer::DISABLED && timer->getState() != ScStwTimer::FAILED)
|
||||||
|
returnCode = ScStw::InternalErrorTimerOperationFailed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this->setState(STOPPED);
|
||||||
|
this->soundPlayer->cancel(this->soundVolume);
|
||||||
|
|
||||||
|
return returnCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ScStwRace::playSoundsAndStartTimers(StartAction thisAction) {
|
bool ScStwRace::playSoundsAndStartTimers(StartAction thisAction) {
|
||||||
|
@ -243,10 +231,6 @@ bool ScStwRace::playSoundsAndStartTimers(StartAction thisAction) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief ScStwRace::setState
|
|
||||||
* @param {QString} newState
|
|
||||||
*/
|
|
||||||
void ScStwRace::setState(RaceState newState) {
|
void ScStwRace::setState(RaceState newState) {
|
||||||
if(newState != this->state) {
|
if(newState != this->state) {
|
||||||
qDebug() << "[INFO][RACE] state changed: " << newState;
|
qDebug() << "[INFO][RACE] state changed: " << newState;
|
||||||
|
@ -264,9 +248,6 @@ void ScStwRace::setState(RaceState newState) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief ScStwRace::refreshTimerStates
|
|
||||||
*/
|
|
||||||
void ScStwRace::refreshTimerStates() {
|
void ScStwRace::refreshTimerStates() {
|
||||||
|
|
||||||
qDebug() << "[INFO][MAIN] refreshing timer states";
|
qDebug() << "[INFO][MAIN] refreshing timer states";
|
||||||
|
@ -283,12 +264,13 @@ void ScStwRace::refreshTimerStates() {
|
||||||
else if(timer->getState() == ScStwTimer::WAITING) {
|
else if(timer->getState() == ScStwTimer::WAITING) {
|
||||||
this->handleTimerStop();
|
this->handleTimerStop();
|
||||||
}
|
}
|
||||||
|
else if (timer->getState() == ScStwTimer::FAILED) {
|
||||||
|
this->handleFalseStart();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(raceIsOver){
|
if(raceIsOver)
|
||||||
this->setState(STOPPED);
|
this->setState(STOPPED);
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------
|
// ------------------------
|
||||||
|
@ -329,8 +311,8 @@ QVariantList ScStwRace::getNextStartActionDetails() {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
this->nextStartAction,
|
this->nextStartAction,
|
||||||
nextActionDelay,
|
nextActionDelay,
|
||||||
nextActionDelayProg
|
nextActionDelayProg
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -369,7 +351,6 @@ bool ScStwRace::addTimer(ScStwTimer *timer) {
|
||||||
|
|
||||||
this->timers.append(timer);
|
this->timers.append(timer);
|
||||||
|
|
||||||
connect(timer, &ScStwTimer::startCanceled, this, &ScStwRace::cancelStart);
|
|
||||||
connect(timer, &ScStwTimer::stateChanged, this, &ScStwRace::refreshTimerStates);
|
connect(timer, &ScStwTimer::stateChanged, this, &ScStwRace::refreshTimerStates);
|
||||||
connect(timer, &ScStwTimer::requestEnableChange, this, &ScStwRace::handleTimerEnable);
|
connect(timer, &ScStwTimer::requestEnableChange, this, &ScStwRace::handleTimerEnable);
|
||||||
connect(timer, &ScStwTimer::stateChanged, this, &ScStwRace::timersChanged);
|
connect(timer, &ScStwTimer::stateChanged, this, &ScStwRace::timersChanged);
|
||||||
|
|
|
@ -84,7 +84,6 @@ bool ScStwTimer::stop(StopReason reason, double timeOfStop) {
|
||||||
switch (reason) {
|
switch (reason) {
|
||||||
case ManualStop: {
|
case ManualStop: {
|
||||||
if(this->state == STARTING){
|
if(this->state == STARTING){
|
||||||
emit startCanceled(false);
|
|
||||||
this->setState(CANCELLED);
|
this->setState(CANCELLED);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -96,19 +95,16 @@ bool ScStwTimer::stop(StopReason reason, double timeOfStop) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case FailStop: {
|
case FailStop: {
|
||||||
qDebug() << "+ [INFO][TIMER] False Start detected: " << "start Time: " << startTime << " reactionTime: " << reactionTime;
|
qDebug() << "[INFO][TIMER] False Start detected: " << "start Time: " << startTime << " reactionTime: " << reactionTime;
|
||||||
this->setState(FAILED);
|
this->setState(FAILED);
|
||||||
|
break;
|
||||||
emit startCanceled(true);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
qDebug() << "+ [INFO][TIMER] Stopped: " << "start Time: " << startTime << " stopTime: " << stopTime << " stoppedTime: " << this->getCurrentTime() << " reactionTime: " << reactionTime;
|
qDebug() << "[INFO][TIMER] Stopped: " << "start Time: " << startTime << " stopTime: " << stopTime << " stoppedTime: " << this->getCurrentTime() << " reactionTime: " << reactionTime;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue