fixes issues with new settings approach

This commit is contained in:
Dorian Zedler 2020-10-03 17:01:05 +02:00
parent a56eae71a7
commit 4dd4bc21f9
Signed by: dorian
GPG key ID: D3B255CB8BC7CD37
2 changed files with 78 additions and 57 deletions

View file

@ -128,7 +128,7 @@ public slots:
virtual ScStw::StatusCode cancel(); virtual ScStw::StatusCode cancel();
// setters // setters
bool writeStartSoundSetting(ScStwSoundPlayer::StartSound sound, bool enabled, int delay); bool setStartSoundSetting(ScStwSoundPlayer::StartSound sound, bool enabled, int delay);
bool setSoundVolume(double volume); bool setSoundVolume(double volume);
virtual bool addTimer(ScStwTimer *timer); virtual bool addTimer(ScStwTimer *timer);
bool setCompetitionMode(bool competitionMode); bool setCompetitionMode(bool competitionMode);
@ -163,7 +163,10 @@ private slots:
void handleTimerReadyStateChange(ScStwTimer::ReadyState readyState); void handleTimerReadyStateChange(ScStwTimer::ReadyState readyState);
// settings // settings
bool writeStartSoundSettingKey(ScStwSoundPlayer::StartSound sound, QString key, QVariant value); bool setStartSoundSetting(ScStwSoundPlayer::StartSound sound, bool enabled, int delay, bool force);
bool setSoundVolume(double volume, bool force);
bool setCompetitionMode(bool competitionMode, bool force);
bool writeStartSoundSetting(ScStwSoundPlayer::StartSound sound, QString key, QVariant value);
virtual void handleSettingChange(int keyInt, int keyLevel, QVariant value); virtual void handleSettingChange(int keyInt, int keyLevel, QVariant value);
void reloadAllSettings(); void reloadAllSettings();

View file

@ -42,15 +42,17 @@ ScStwRace::ScStwRace(ScStwSettings *settings, QObject *parent) : QObject(parent)
// init settings // init settings
this->competitionMode = false; this->competitionMode = false;
this->competitionModeChangedRecently = false;
this->soundVolume = 1; this->soundVolume = 1;
this->startSoundSettings.insert(ScStwSoundPlayer::Start, {{"Enabled", true}, {"Delay", 1}}); this->startSoundSettings.insert(ScStwSoundPlayer::Start, {{"Enabled", true}, {"Delay", 1}});
this->writeStartSoundSetting(ScStwSoundPlayer::AtYourMarks, false, 0); this->setStartSoundSetting(ScStwSoundPlayer::AtYourMarks, false, 0);
this->writeStartSoundSetting(ScStwSoundPlayer::Ready, false, 0); this->setStartSoundSetting(ScStwSoundPlayer::Ready, false, 0);
this->setSoundVolume(1.0); this->setSoundVolume(1.0);
this->settings = settings; this->settings = settings;
if(this->settings != nullptr) { if(this->settings != nullptr) {
connect(this->settings, &ScStwSettings::settingChanged, this, &ScStwRace::handleSettingChange); connect(this->settings, &ScStwSettings::settingChanged, this, &ScStwRace::handleSettingChange);
this->reloadAllSettings();
} }
} }
@ -94,7 +96,7 @@ ScStw::StatusCode ScStwRace::stop() {
if(this->competitionMode) if(this->competitionMode)
return this->cancel(); return this->cancel();
qDebug() << "+ [INFO] stopping race"; qDebug() << "[INFO][RACE] stopping race";
double timeOfStop = QDateTime::currentMSecsSinceEpoch(); double timeOfStop = QDateTime::currentMSecsSinceEpoch();
@ -158,7 +160,7 @@ ScStw::StatusCode ScStwRace::reset() {
return ScStw::CurrentStateNotVaildForOperationError; return ScStw::CurrentStateNotVaildForOperationError;
} }
qDebug() << "+ [INFO] resetting race"; qDebug() << "[INFO][RACE] resetting race";
ScStw::StatusCode returnCode = ScStw::Success; ScStw::StatusCode returnCode = ScStw::Success;
@ -233,7 +235,6 @@ bool ScStwRace::playSoundsAndStartTimers() {
return true; return true;
// The check if all timers are ready has already happened at this point // The check if all timers are ready has already happened at this point
qDebug() << "now playing at marks sound";
if(!this->doDelayAndSoundOfCurrentStartState()) if(!this->doDelayAndSoundOfCurrentStartState())
return false; return false;
@ -241,7 +242,6 @@ bool ScStwRace::playSoundsAndStartTimers() {
if(!this->isStarting()) if(!this->isStarting())
return false; return false;
qDebug() << "Now in waiting state";
this->setState(WAITING); this->setState(WAITING);
// do climber readiness tests // do climber readiness tests
@ -299,11 +299,11 @@ bool ScStwRace::playSoundsAndStartTimers() {
} while(this->startDelayTimer->remainingTime() > 0 || !timerTriggered || !this->getIsReadyForNextState()); } while(this->startDelayTimer->remainingTime() > 0 || !timerTriggered || !this->getIsReadyForNextState());
qDebug() << "[RACE][DEBUG] Wait finished, starting now!"; qDebug() << "[DEBUG][RACE] Wait finished, starting now!";
// play ready tone // play ready tone
if(!this->soundPlayer->play(ScStwSoundPlayer::Ready, this->soundVolume)) { if(!this->soundPlayer->play(ScStwSoundPlayer::Ready, this->soundVolume)) {
qDebug() << "Ready sound redturned false!"; qDebug() << "[ERROR][RACE] Ready sound returned false!";
this->technicalIncident(); this->technicalIncident();
return false; return false;
} }
@ -320,20 +320,16 @@ bool ScStwRace::playSoundsAndStartTimers() {
} while(loopExitCode != LoopManualExit || !this->getIsReadyForNextState()); } while(loopExitCode != LoopManualExit || !this->getIsReadyForNextState());
} }
else { else {
qDebug() << "now playing ready sound";
if(!this->doDelayAndSoundOfCurrentStartState()) { if(!this->doDelayAndSoundOfCurrentStartState()) {
this->cancel(); this->cancel();
return false; return false;
} }
} }
qDebug() << "now in starting state";
// enter starting state // enter starting state
this->setState(STARTING); this->setState(STARTING);
// play start tone // play start tone
qDebug() << "now playing start sound";
double timeOfSoundPlaybackStart; double timeOfSoundPlaybackStart;
if(!this->doDelayAndSoundOfCurrentStartState(&timeOfSoundPlaybackStart)) { if(!this->doDelayAndSoundOfCurrentStartState(&timeOfSoundPlaybackStart)) {
this->technicalIncident(); this->technicalIncident();
@ -350,13 +346,13 @@ bool ScStwRace::playSoundsAndStartTimers() {
} }
} }
if(!startOk) { if(!startOk) {
qDebug() << "[ERROR][START] error staring all timers"; qDebug() << "[ERROR][RACE] error staring all timers";
this->technicalIncident(); this->technicalIncident();
return false; return false;
} }
if(!this->soundPlayer->waitForSoundFinish()) { if(!this->soundPlayer->waitForSoundFinish()) {
qDebug() << "[ERROR][START] start sound wait error"; qDebug() << "[ERROR][RACE] start sound wait error";
this->technicalIncident(); this->technicalIncident();
return false; return false;
} }
@ -442,7 +438,6 @@ void ScStwRace::setState(RaceState newState) {
} }
void ScStwRace::refreshTimerStates() { void ScStwRace::refreshTimerStates() {
qDebug() << "[INFO][MAIN] refreshing timer states"; qDebug() << "[INFO][MAIN] refreshing timer states";
// check if the race is over // check if the race is over
@ -483,8 +478,6 @@ bool ScStwRace::getIsReadyForNextState() {
if(timer->getReadyState() == ScStwTimer::ExtensionIsNotConnected || timer->getReadyState() == ScStwTimer::ExtensionBatteryNotFine) { if(timer->getReadyState() == ScStwTimer::ExtensionIsNotConnected || timer->getReadyState() == ScStwTimer::ExtensionBatteryNotFine) {
qDebug() << "Timer ready state is: " << timer->getReadyState();
timer->technicalIncident(); timer->technicalIncident();
foreach (ScStwTimer *subTimer, this->timers) { foreach (ScStwTimer *subTimer, this->timers) {
@ -534,31 +527,10 @@ void ScStwRace::handleTimerWantsToBeDisabledChange(ScStwTimer* timer, bool wants
} }
} }
bool ScStwRace::setCompetitionMode(bool competitionMode) {
if(this->settings != nullptr)
return false;
if(this->competitionMode == competitionMode)
return true;
qDebug() << "Setting competition mode to " << competitionMode;
this->competitionMode = competitionMode;
if(this->state != IDLE)
this->competitionModeChangedRecently = true;
else if(this->competitionMode)
this->enableAllTimers();
return true;
}
void ScStwRace::enableAllTimers() { void ScStwRace::enableAllTimers() {
if(this->state != IDLE) if(this->state != IDLE)
return; return;
qDebug() << "ENABLING ALL TIMERS";
foreach (ScStwTimer *timer, this->timers) { foreach (ScStwTimer *timer, this->timers) {
timer->setDisabled(false); timer->setDisabled(false);
} }
@ -611,20 +583,53 @@ QVariantMap ScStwRace::getCurrentStartDelay() {
return currentStartDelay; return currentStartDelay;
} }
bool ScStwRace::writeStartSoundSetting(ScStwSoundPlayer::StartSound sound, bool enabled, int delay) { bool ScStwRace::setCompetitionMode(bool competitionMode) {
if(this->settings != nullptr) if(this->settings != nullptr)
return false; return false;
return this->setCompetitionMode(competitionMode, true);
}
bool ScStwRace::setCompetitionMode(bool competitionMode, bool force) {
if(!force)
return false;
if(this->competitionMode == competitionMode)
return true;
qDebug() << "[INFO][RACE] Setting competition mode to " << competitionMode;
this->competitionMode = competitionMode;
if(this->state != IDLE)
this->competitionModeChangedRecently = true;
else if(this->competitionMode)
this->enableAllTimers();
return true;
}
bool ScStwRace::setStartSoundSetting(ScStwSoundPlayer::StartSound sound, bool enabled, int delay) {
if(this->settings != nullptr)
return false;
return this->setStartSoundSetting(sound, enabled, delay, true);
}
bool ScStwRace::setStartSoundSetting(ScStwSoundPlayer::StartSound sound, bool enabled, int delay, bool force) {
if(!force)
return false;
if(sound != ScStwSoundPlayer::AtYourMarks && sound != ScStwSoundPlayer::Ready) if(sound != ScStwSoundPlayer::AtYourMarks && sound != ScStwSoundPlayer::Ready)
return false; return false;
this->writeStartSoundSettingKey(sound, "Enabled", enabled); this->writeStartSoundSetting(sound, "Enabled", enabled);
this->writeStartSoundSettingKey(sound, "Delay", delay); this->writeStartSoundSetting(sound, "Delay", delay);
return true; return true;
} }
bool ScStwRace::writeStartSoundSettingKey(ScStwSoundPlayer::StartSound sound, QString key, QVariant value) { bool ScStwRace::writeStartSoundSetting(ScStwSoundPlayer::StartSound sound, QString key, QVariant value) {
if(key != "Delay" && key != "Enabled") if(key != "Delay" && key != "Enabled")
return false; return false;
@ -644,6 +649,13 @@ bool ScStwRace::setSoundVolume(double volume) {
if(this->settings != nullptr) if(this->settings != nullptr)
return false; return false;
return this->setSoundVolume(volume, true);
}
bool ScStwRace::setSoundVolume(double volume, bool force) {
if(!force)
return false;
if(volume >= 0 && volume <= 1) { if(volume >= 0 && volume <= 1) {
this->soundVolume = volume; this->soundVolume = volume;
return true; return true;
@ -681,21 +693,25 @@ bool ScStwRace::addTimer(ScStwTimer *timer) {
} }
void ScStwRace::reloadAllSettings() { void ScStwRace::reloadAllSettings() {
this->writeStartSoundSetting( qDebug() << "reloading all settings";
this->setStartSoundSetting(
ScStwSoundPlayer::AtYourMarks, ScStwSoundPlayer::AtYourMarks,
this->settings->readSetting(ScStwSettings::BaseStationSetting::AtYourMarksSoundEnableSetting).toBool(), this->settings->readSetting(ScStwSettings::BaseStationSetting::AtYourMarksSoundEnableSetting).toBool(),
this->settings->readSetting(ScStwSettings::BaseStationSetting::AtYourMarksSoundDelaySetting).toDouble() this->settings->readSetting(ScStwSettings::BaseStationSetting::AtYourMarksSoundDelaySetting).toDouble(),
true
); );
this->writeStartSoundSetting( this->setStartSoundSetting(
ScStwSoundPlayer::Ready, ScStwSoundPlayer::Ready,
this->settings->readSetting(ScStwSettings::BaseStationSetting::ReadySoundEnableSetting).toBool(), this->settings->readSetting(ScStwSettings::BaseStationSetting::ReadySoundEnableSetting).toBool(),
this->settings->readSetting(ScStwSettings::BaseStationSetting::ReadySoundDelaySetting).toDouble() this->settings->readSetting(ScStwSettings::BaseStationSetting::ReadySoundDelaySetting).toDouble(),
true
); );
this->setSoundVolume(this->settings->readSetting(ScStwSettings::SoundVolumeSetting).toDouble()); this->setSoundVolume(this->settings->readSetting(ScStwSettings::SoundVolumeSetting).toDouble(), true);
this->setCompetitionMode(this->settings->readSetting(ScStwSettings::CompetitionModeSetting).toBool()); this->setCompetitionMode(this->settings->readSetting(ScStwSettings::CompetitionModeSetting).toBool(), true);
} }
void ScStwRace::handleSettingChange(int keyInt, int keyLevel, QVariant value) { void ScStwRace::handleSettingChange(int keyInt, int keyLevel, QVariant value) {
@ -704,7 +720,7 @@ void ScStwRace::handleSettingChange(int keyInt, int keyLevel, QVariant value) {
return; return;
} }
qDebug() << "Setting changed"; qDebug() << "Setting chagned: key: " << keyInt << " level: " << keyLevel << " value: " << value;
switch (keyLevel) { switch (keyLevel) {
case 0: { case 0: {
@ -712,22 +728,22 @@ void ScStwRace::handleSettingChange(int keyInt, int keyLevel, QVariant value) {
ScStwSettings::BaseStationSetting key = static_cast<ScStwSettings::BaseStationSetting>(keyInt); ScStwSettings::BaseStationSetting key = static_cast<ScStwSettings::BaseStationSetting>(keyInt);
switch (key) { switch (key) {
case ScStwSettings::AtYourMarksSoundEnableSetting: case ScStwSettings::AtYourMarksSoundEnableSetting:
this->writeStartSoundSettingKey(ScStwSoundPlayer::AtYourMarks, "Enabled", value); this->writeStartSoundSetting(ScStwSoundPlayer::AtYourMarks, "Enabled", value);
break; break;
case ScStwSettings::AtYourMarksSoundDelaySetting: case ScStwSettings::AtYourMarksSoundDelaySetting:
this->writeStartSoundSettingKey(ScStwSoundPlayer::AtYourMarks, "Delay", value); this->writeStartSoundSetting(ScStwSoundPlayer::AtYourMarks, "Delay", value);
break; break;
case ScStwSettings::ReadySoundEnableSetting: case ScStwSettings::ReadySoundEnableSetting:
this->writeStartSoundSettingKey(ScStwSoundPlayer::Ready, "Enabled", value); this->writeStartSoundSetting(ScStwSoundPlayer::Ready, "Enabled", value);
break; break;
case ScStwSettings::ReadySoundDelaySetting: case ScStwSettings::ReadySoundDelaySetting:
this->writeStartSoundSettingKey(ScStwSoundPlayer::Ready, "Delay", value); this->writeStartSoundSetting(ScStwSoundPlayer::Ready, "Delay", value);
break; break;
case ScStwSettings::SoundVolumeSetting: case ScStwSettings::SoundVolumeSetting:
this->setSoundVolume(value.toDouble()); this->setSoundVolume(value.toDouble(), true);
break; break;
case ScStwSettings::CompetitionModeSetting: case ScStwSettings::CompetitionModeSetting:
this->setCompetitionMode(value.toBool()); this->setCompetitionMode(value.toBool(), true);
default: default:
return; return;
@ -774,6 +790,8 @@ QVariantMap ScStwRace::getDetails() {
if(this->state == WAITING) if(this->state == WAITING)
tmpDetails.insert("isReadyForNextState", this->getIsReadyForNextState()); tmpDetails.insert("isReadyForNextState", this->getIsReadyForNextState());
else
tmpDetails.insert("isReadyForNextState", true);
return tmpDetails; return tmpDetails;
} }