This repository has been archived on 2024-06-03. You can view files and clone it, but cannot push or open issues or pull requests.
shared-libraries/ScStwLibraries/sources/client/scstwremoterace.cpp

371 lines
12 KiB
C++
Raw Permalink Normal View History

2020-06-07 14:43:47 +02:00
/****************************************************************************
** ScStw Libraries
** Copyright (C) 2020 Itsblue development
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
2020-10-02 19:56:08 +02:00
#include "scstwremoterace.h"
2020-10-09 15:18:56 +02:00
ScStwRemoteRace::ScStwRemoteRace(QObject* parent) : ScStwRemoteRace(nullptr, nullptr, parent)
{
}
ScStwRemoteRace::ScStwRemoteRace(ScStwClient *scStwClient, ScStwSettings *settings, QObject *parent) : ScStwRace(settings, parent)
{
2020-10-03 11:10:15 +02:00
this->isReadyForNextState = true;
2020-10-02 16:49:24 +02:00
this->remoteTimers = {};
2020-10-07 11:57:50 +02:00
this->localTimers = {};
2020-10-09 15:18:56 +02:00
this->setScStwClient(scStwClient);
}
// --------------------------
// --- Main Functionality ---
// --------------------------
ScStw::StatusCode ScStwRemoteRace::start(bool asyncronous) {
if(this->local())
return ScStwRace::start(asyncronous);
2020-10-02 19:56:08 +02:00
2020-10-02 16:49:24 +02:00
if(this->getState() != ScStwRace::IDLE && this->getState() != ScStwRace::WAITING)
return ScStw::CurrentStateNotVaildForOperationError;
qDebug() << "+ --- starting race";
2020-10-02 16:49:24 +02:00
QVariantMap reply = this->scStwClient->sendCommand(ScStw::StartRaceCommand);
2020-10-02 16:49:24 +02:00
return ScStw::StatusCode(reply["status"].toInt());
}
2020-10-02 19:56:08 +02:00
ScStw::StatusCode ScStwRemoteRace::cancel() {
if(this->local())
return ScStwRace::cancel();
2020-10-02 16:49:24 +02:00
if(this->getState() != PREPAIRING && this->getState() != WAITING && this->getState() != STARTING && this->getState() != RUNNING)
return ScStw::CurrentStateNotVaildForOperationError;
2020-10-02 16:49:24 +02:00
qDebug() << "+ --- stopping race";
2020-10-02 16:49:24 +02:00
QVariantMap reply = this->scStwClient->sendCommand(ScStw::CancelRaceCommand);
2020-05-26 17:57:32 +02:00
2020-10-02 16:49:24 +02:00
return ScStw::StatusCode(reply["status"].toInt());
}
2020-10-02 19:56:08 +02:00
ScStw::StatusCode ScStwRemoteRace::stop() {
if(this->local())
return ScStwRace::stop();
2020-10-02 16:49:24 +02:00
if(this->getState() != ScStwRace::RUNNING && this->getState() != ScStwRace::STARTING)
return ScStw::CurrentStateNotVaildForOperationError;
qDebug() << "+ --- stopping race";
2020-10-02 16:49:24 +02:00
QVariantMap reply = this->scStwClient->sendCommand(ScStw::StopRaceCommand);
2020-10-02 16:49:24 +02:00
return ScStw::StatusCode(reply["status"].toInt());
}
2020-10-02 19:56:08 +02:00
ScStw::StatusCode ScStwRemoteRace::reset() {
if(this->local())
return ScStwRace::reset();
2020-10-02 16:49:24 +02:00
if(this->getState() != ScStwRace::STOPPED && this->getState() != ScStwRace::INCIDENT)
return ScStw::CurrentStateNotVaildForOperationError;
qDebug() << "+ --- resetting race";
2020-10-02 16:49:24 +02:00
QVariantMap reply = this->scStwClient->sendCommand(ScStw::ResetRaceCommand);
2020-10-02 16:49:24 +02:00
return ScStw::StatusCode(reply["status"].toInt());
}
ScStw::StatusCode ScStwRemoteRace::setTimerDisabled(int timerId, bool disabled) {
if(this->state != IDLE && this->state != WAITING)
return ScStw::CurrentStateNotVaildForOperationError;
if(timerId < 0 || timerId - 1 > this->timers.length())
return ScStw::ItemNotFoundError;
qDebug() << "+ --- setting timer " << timerId << " to disabled: " << disabled;
QVariantMap requestData = {
2021-01-09 23:12:28 +01:00
{"timerId", timerId},
{"disabled", disabled}
};
QVariantMap reply = this->scStwClient->sendCommand(ScStw::SetTimerDisabledCommand, QJsonValue::fromVariant(requestData));
qDebug() << "+ --- timer disabled command returned: " << reply;
return ScStw::StatusCode(reply["status"].toInt());
}
// -------------------------
// --- Base Station sync ---
// -------------------------
void ScStwRemoteRace::handleClientStateChange() {
switch (this->scStwClient->getState()) {
case ScStwClient::INITIALISING:
// disconnect all local timers from race
this->setTimers({}, false);
// delete all obsolete remote timers
for(ScStwRemoteTimer* oldRemoteTimer : this->remoteTimers)
oldRemoteTimer->deleteLater();
2021-01-09 23:12:28 +01:00
2020-10-03 21:47:53 +02:00
this->remoteTimers.clear();
break;
case ScStwClient::DISCONNECTED:
this->remoteTimers.clear();
this->setTimers(this->localTimers, false);
emit this->timersChanged();
2020-10-04 16:05:16 +02:00
emit this->detailsChanged();
emit this->currentStartDelayChanged();
this->competitionMode = false;
2020-04-19 13:09:24 +02:00
this->setState(IDLE);
break;
default:
break;
}
}
2021-01-09 23:12:28 +01:00
void ScStwRemoteRace::setTimers(QList<ScStwTimer*> timers, bool deleteOldTimers) {
// disconnect all signals of all current timers
//qDebug() << "SETTING TIMERS";
foreach(ScStwTimer *existingTimer, this->timers) {
disconnect(existingTimer, &ScStwTimer::stateChanged, this, &ScStwRace::handleTimerStateChange);
disconnect(existingTimer, &ScStwTimer::stateChanged, this, &ScStwRace::timersChanged);
disconnect(existingTimer, &ScStwTimer::wantsToBeDisabledChanged, this, &ScStwRace::handleTimerWantsToBeDisabledChange);
disconnect(existingTimer, &ScStwTimer::reactionTimeChanged, this, &ScStwRace::timersChanged);
disconnect(existingTimer, &ScStwTimer::readyStateChanged, this, &ScStwRace::handleTimerReadyStateChange);
disconnect(existingTimer, &ScStwTimer::readyStateChanged, this, &ScStwRace::isReadyForNextStateChanged);
if(deleteOldTimers)
existingTimer->deleteLater();
}
this->timers.clear();
for(ScStwTimer* timer : timers) {
this->addTimer(timer);
}
}
ScStwRemoteRace::RaceMode ScStwRemoteRace::getMode() {
2020-10-09 15:18:56 +02:00
if(this->scStwClient == nullptr || this->scStwClient->getState() != ScStwClient::CONNECTED)
return LOCAL;
else
2020-10-09 15:18:56 +02:00
return REMOTE;
}
bool ScStwRemoteRace::local() {
return this->getMode() == LOCAL;
}
/**
* @brief ScStwAppBackend::handleBaseStationUpdate
*
* Function to handle an update, sent by the base station, which indicates
* that some remote value (like a state) has changed
*
* @param data
*/
2020-10-02 19:56:08 +02:00
void ScStwRemoteRace::handleBaseStationSignal(ScStw::SignalKey key, QVariant data) {
switch (key) {
2020-10-03 11:10:15 +02:00
case ScStw::RaceDetailsChanged:
{
2020-10-03 11:10:15 +02:00
this->refreshDetails(data.toMap());
break;
}
2020-10-03 11:10:15 +02:00
default:
return;
}
2020-10-03 11:10:15 +02:00
}
2020-10-02 16:49:24 +02:00
2020-10-03 11:10:15 +02:00
void ScStwRemoteRace::refreshDetails(QVariantMap details) {
// the details of the race have changed:
2020-10-02 16:49:24 +02:00
qDebug() << "RACE DETAILS: " << details;
2020-10-03 11:10:15 +02:00
// competition mode
if(this->competitionMode != details["competitionMode"].toBool()) {
this->competitionMode = details["competitionMode"].toBool();
emit this->competitionModeChanged();
}
2020-10-03 11:10:15 +02:00
// ready sound enabled
if(this->readySoundEnabled != details["readySoundEnabled"].toBool()) {
this->readySoundEnabled = details["readySoundEnabled"].toBool();
emit this->readySoundEnabledChanged();
}
2020-10-03 11:10:15 +02:00
// current start delay
this->currentStartTotalDelay = details["currentStartDelay"].toMap()["total"].toInt();
this->latestStartDelayProgress = details["currentStartDelay"].toMap()["progress"].toDouble();
this->currentStartDelayStartedAt = QDateTime::currentMSecsSinceEpoch() - (this->currentStartTotalDelay * this->latestStartDelayProgress);
2020-10-03 11:10:15 +02:00
emit this->currentStartDelayChanged();
// timers
this->refreshRemoteTimers(details["timers"].toList());
// state
this->setState(ScStwRace::RaceState(details["state"].toInt()));
2020-10-03 11:10:15 +02:00
// isReady
if(this->state == WAITING) {
this->isReadyForNextState = details["isReadyForNextState"].toBool();
emit this->isReadyForNextStateChanged();
}
2020-10-03 11:10:15 +02:00
emit this->detailsChanged();
}
void ScStwRemoteRace::rebuildRemoteTimers(QVariantList remoteTimers) {
qDebug() << "REBUILDING TIMERS";
// delete all current timers
foreach(ScStwTimer * oldTimer, this->timers){
oldTimer->deleteLater();
}
this->remoteTimers.clear();
this->timers.clear();
foreach(QVariant remoteTimer, remoteTimers){
// create a local timer for each remote timer
ScStwRemoteTimer * timer = new ScStwRemoteTimer(this);
this->timers.append(timer);
this->remoteTimers.append(timer);
connect(timer, &ScStwTimer::stateChanged, this, &ScStwRace::timersChanged);
connect(timer, &ScStwTimer::reactionTimeChanged, this, &ScStwRace::timersChanged);
}
}
2021-01-09 23:12:28 +01:00
bool ScStwRemoteRace::refreshRemoteTimers(QVariantList newRemoteTimers) {
qDebug() << "REFRESHING TIMERS";
2021-01-09 23:12:28 +01:00
if(newRemoteTimers.length() != this->remoteTimers.length()){
// local timers are out of sync
this->rebuildRemoteTimers(newRemoteTimers);
qDebug() << "rebuilding remote timers";
}
2021-01-09 23:12:28 +01:00
for(QVariant remoteTimer : newRemoteTimers){
int currId = remoteTimer.toMap()["id"].toInt();
2020-10-02 19:56:08 +02:00
2021-01-09 23:12:28 +01:00
if(this->remoteTimers.length() <= currId)
this->rebuildRemoteTimers(newRemoteTimers);
2021-01-09 23:12:28 +01:00
ScStwTimer::TimerState newState = ScStwTimer::TimerState(remoteTimer.toMap()["state"].toInt());
2020-10-02 19:56:08 +02:00
2021-01-09 23:12:28 +01:00
qDebug() << "refreshing timers: id: " << currId << " state: " << newState << " readyState: " << remoteTimer.toMap()["readyState"].toInt() << " currentTime: " << remoteTimer.toMap()["currentTime"].toDouble();
2020-10-02 19:56:08 +02:00
2021-01-09 23:12:28 +01:00
double currentMSecsSinceEpoch = QDateTime::currentMSecsSinceEpoch();
2021-01-09 23:12:28 +01:00
this->remoteTimers[currId]->setStartTime(currentMSecsSinceEpoch - remoteTimer.toMap()["currentTime"].toDouble());
2021-01-09 23:12:28 +01:00
if(newState >= ScStwTimer::WAITING)
this->remoteTimers[currId]->setStopTime(currentMSecsSinceEpoch);
2021-01-09 23:12:28 +01:00
this->remoteTimers[currId]->setReactionTime(remoteTimer.toMap()["reactionTime"].toDouble());
this->remoteTimers[currId]->setLetter(remoteTimer.toMap()["letter"].toString());
this->remoteTimers[currId]->setReadyState(ScStwTimer::ReadyState(remoteTimer.toMap()["readyState"].toInt()));
2020-06-13 15:57:27 +02:00
2021-01-09 23:12:28 +01:00
this->remoteTimers[currId]->setState(newState);
}
2021-01-09 23:12:28 +01:00
emit this->timersChanged();
2021-01-09 23:12:28 +01:00
return true;
}
2020-10-02 19:56:08 +02:00
bool ScStwRemoteRace::addTimer(ScStwTimer* timer) {
if(this->local()) {
this->localTimers.append(timer);
return ScStwRace::addTimer(timer);
}
return false;
}
2020-04-19 13:09:24 +02:00
QVariantMap ScStwRemoteRace::getCurrentStartDelay() {
if(this->local())
return ScStwRace::getCurrentStartDelay();
2020-04-19 13:09:24 +02:00
QVariantMap currentStartDelay = {
{"total", -1.0},
{"progress", -1.0}
};
if(this->currentStartTotalDelay == -1) {
currentStartDelay["progress"] = this->latestStartDelayProgress;
2020-10-02 16:49:24 +02:00
}
else if(this->getState() == PREPAIRING || this->getState() == WAITING) {
2020-04-19 13:09:24 +02:00
// get the total delay and the delay progress of the next action timer
2020-10-02 16:49:24 +02:00
double elapsed = QDateTime::currentMSecsSinceEpoch() - this->currentStartDelayStartedAt;
currentStartDelay["total"] = this->currentStartTotalDelay;
if(elapsed < 0 || elapsed > currentStartDelay["total"].toDouble()) {
elapsed = currentStartDelay["total"].toDouble();
2020-04-19 13:09:24 +02:00
}
currentStartDelay["progress"] = elapsed / currentStartDelay["total"].toDouble();
2020-04-19 13:09:24 +02:00
}
return currentStartDelay;
2020-04-19 13:09:24 +02:00
}
2020-10-03 11:10:15 +02:00
bool ScStwRemoteRace::getIsReadyForNextState() {
if(this->local())
return ScStwRace::getIsReadyForNextState();
2020-10-03 11:10:15 +02:00
return this->isReadyForNextState;
}
bool ScStwRemoteRace::getReadySoundEnabled() {
if(this->local())
return ScStwRace::getReadySoundEnabled();
return this->readySoundEnabled;
}
2020-10-03 18:32:39 +02:00
void ScStwRemoteRace::refreshCompetitionMode() {
if(this->local())
return ScStwRace::refreshCompetitionMode();
}
2020-10-09 15:18:56 +02:00
ScStwClient* ScStwRemoteRace::getScStwClient() {
return this->scStwClient;
}
void ScStwRemoteRace::setScStwClient(ScStwClient* client) {
if(client == this->scStwClient)
return;
this->scStwClient = client;
if(this->scStwClient != nullptr) {
this->scStwClient->addSignalSubscription(ScStw::RaceDetailsChanged);
connect(this->scStwClient, &ScStwClient::stateChanged, this, &ScStwRemoteRace::handleClientStateChange);
connect(this->scStwClient, &ScStwClient::gotSignal, this, &ScStwRemoteRace::handleBaseStationSignal);
}
}