2020-04-11 23:41:34 +02:00
# include "../headers/scstwtimer.h"
ScStwTimer : : ScStwTimer ( QObject * parent ) : QObject ( parent )
{
this - > startTime = 0 ;
this - > stopTime = 0 ;
this - > stoppedTime = 0 ;
this - > reactionTime = 0 ;
2020-04-14 00:08:58 +02:00
this - > state = IDLE ;
2020-04-11 23:41:34 +02:00
}
bool ScStwTimer : : start ( ) {
2020-04-14 00:08:58 +02:00
return this - > start ( QDateTime : : currentMSecsSinceEpoch ( ) ) ;
}
bool ScStwTimer : : start ( double timeOfStart ) {
2020-04-11 23:41:34 +02:00
switch ( this - > state ) {
2020-04-14 00:08:58 +02:00
case IDLE : {
// in case of IDLE, start the race!
2020-04-11 23:41:34 +02:00
2020-04-14 00:08:58 +02:00
this - > startTime = timeOfStart ;
2020-04-11 23:41:34 +02:00
this - > stopTime = 0 ;
this - > stoppedTime = 0 ;
2020-04-14 00:08:58 +02:00
if ( timeOfStart - QDateTime : : currentMSecsSinceEpoch ( ) > 0 ) {
this - > setState ( STARTING ) ;
QTimer : : singleShot ( timeOfStart - QDateTime : : currentMSecsSinceEpoch ( ) , [ = ] ( ) {
if ( this - > state = = STARTING )
this - > setState ( RUNNING ) ;
} ) ;
QTimer : : singleShot ( timeOfStart - QDateTime : : currentMSecsSinceEpoch ( ) - 1000 , [ = ] ( ) {
//this->handleClimberStart(QDateTime::currentMSecsSinceEpoch());
} ) ;
}
else
this - > setState ( RUNNING ) ;
2020-04-11 23:41:34 +02:00
return true ;
}
default : {
// otherwise the timer is not supposed to be started!
return false ;
}
}
}
2020-04-14 00:08:58 +02:00
void ScStwTimer : : handleClimberStart ( double timeOfStart ) {
this - > reactionTime = timeOfStart - this - > startTime ;
qDebug ( ) < < " + [INFO][TIMER] reaction time: " < < this - > reactionTime ;
if ( this - > reactionTime < = 0 ) {
this - > stop ( FailStop ) ;
return ;
}
// TODO: check if necessary: emit this->reactionTimeChanged();
}
2020-04-11 23:41:34 +02:00
bool ScStwTimer : : cancel ( ) {
2020-04-14 00:08:58 +02:00
if ( ! ( this - > state = = IDLE | | this - > state = = STARTING | | this - > state = = RUNNING ) )
2020-04-11 23:41:34 +02:00
return false ;
this - > setState ( CANCELLED ) ;
return true ;
}
bool ScStwTimer : : stop ( ) {
2020-04-14 00:08:58 +02:00
return this - > stop ( ManualStop ) ;
2020-04-11 23:41:34 +02:00
}
bool ScStwTimer : : stop ( TimerState result ) {
switch ( result ) {
case WON :
this - > setState ( WON ) ;
return true ;
case LOST :
this - > setState ( LOST ) ;
return false ;
default :
return false ;
}
}
bool ScStwTimer : : stop ( StopReason reason ) {
if ( this - > state ! = STARTING & & this - > state ! = RUNNING & & this - > state ! = WAITING ) {
return false ;
}
switch ( reason ) {
case ManualStop : {
if ( this - > state = = STARTING ) {
emit startCanceled ( false ) ;
this - > setState ( CANCELLED ) ;
}
else {
this - > stopTime = this - > date - > currentMSecsSinceEpoch ( ) ;
this - > stoppedTime = this - > stopTime - this - > startTime ;
// trigger an external state refresh to set the state to either WON or LOST depending on the other timers values (see MainActivity::refreshTimerStates())
emit this - > stopRequested ( ) ;
}
break ;
}
case FailStop : {
this - > stoppedTime = 0 ;
2020-04-14 00:08:58 +02:00
qDebug ( ) < < " + [INFO][TIMER] False Start detected: " < < " start Time: " < < startTime < < " reactionTime: " < < reactionTime ;
this - > setState ( FAILED ) ;
2020-04-11 23:41:34 +02:00
emit startCanceled ( true ) ;
return true ;
}
default : {
return false ;
}
}
qDebug ( ) < < " + [INFO][TIMER] Stopped: " < < " start Time: " < < startTime < < " stopTime: " < < stopTime < < " stoppedTime: " < < stoppedTime < < " reactionTime: " < < reactionTime ;
return true ;
}
bool ScStwTimer : : reset ( ) {
if ( this - > state < WON | | this - > state = = DISABLED ) {
return false ;
}
this - > startTime = 0 ;
this - > stopTime = 0 ;
this - > stoppedTime = 0 ;
this - > reactionTime = 0 ;
this - > startPadTriggerTime = 0 ;
this - > setState ( IDLE ) ;
return true ;
}
// ------------------------
// --- helper functions ---
// ------------------------
void ScStwTimer : : setState ( TimerState newState ) {
if ( this - > state = = DISABLED & & newState ! = IDLE )
return ;
if ( this - > state ! = newState ) {
this - > state = newState ;
qDebug ( ) < < " + [INFO][TIMER] timer state changed: " < < newState ;
emit this - > stateChanged ( ) ;
}
}
ScStwTimer : : TimerState ScStwTimer : : getState ( ) {
return this - > state ;
}
double ScStwTimer : : getCurrentTime ( ) {
if ( this - > state = = RUNNING ) {
return this - > date - > currentMSecsSinceEpoch ( ) - this - > startTime ;
}
else {
return this - > stoppedTime ;
}
}
double ScStwTimer : : getReactionTime ( ) {
return this - > reactionTime ;
}
void ScStwTimer : : setDisabled ( bool disabled ) {
if ( disabled )
this - > setState ( DISABLED ) ;
else
this - > setState ( IDLE ) ;
}