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-04-18 14:25:48 +02:00
|
|
|
|
2020-10-03 15:15:49 +02:00
|
|
|
ScStwRemoteRace::ScStwRemoteRace(ScStwClient *scStwClient, ScStwSettings *settings, QObject *parent) : ScStwRace(settings, parent)
|
2020-04-18 14:25:48 +02:00
|
|
|
{
|
2020-10-03 11:10:15 +02:00
|
|
|
this->isReadyForNextState = true;
|
2020-10-02 16:49:24 +02:00
|
|
|
|
2020-10-03 13:06:14 +02:00
|
|
|
this->scStwClient = scStwClient;
|
2020-04-18 14:25:48 +02:00
|
|
|
|
2020-10-03 11:10:15 +02:00
|
|
|
this->scStwClient->addSignalSubscription(ScStw::RaceDetailsChanged);
|
2020-07-11 11:17:20 +02:00
|
|
|
|
2020-10-03 13:06:14 +02:00
|
|
|
connect(this->scStwClient, &ScStwClient::stateChanged, this, &ScStwRemoteRace::handleClientStateChange);
|
2020-10-02 19:56:08 +02:00
|
|
|
connect(this->scStwClient, &ScStwClient::gotSignal, this, &ScStwRemoteRace::handleBaseStationSignal);
|
2020-04-18 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------
|
|
|
|
// --- Main Functionality ---
|
|
|
|
// --------------------------
|
|
|
|
|
2020-10-03 13:06:14 +02:00
|
|
|
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;
|
2020-04-18 14:25:48 +02:00
|
|
|
|
|
|
|
qDebug() << "+ --- starting race";
|
|
|
|
|
2020-10-02 16:49:24 +02:00
|
|
|
QVariantMap reply = this->scStwClient->sendCommand(ScStw::StartRaceCommand);
|
2020-04-18 14:25:48 +02:00
|
|
|
|
2020-10-02 16:49:24 +02:00
|
|
|
return ScStw::StatusCode(reply["status"].toInt());
|
|
|
|
}
|
2020-04-18 14:25:48 +02:00
|
|
|
|
2020-10-02 19:56:08 +02:00
|
|
|
ScStw::StatusCode ScStwRemoteRace::cancel() {
|
2020-10-03 13:06:14 +02:00
|
|
|
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-04-18 14:25:48 +02:00
|
|
|
|
2020-10-02 16:49:24 +02:00
|
|
|
qDebug() << "+ --- stopping race";
|
2020-04-18 14:25:48 +02:00
|
|
|
|
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-04-18 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
2020-10-02 19:56:08 +02:00
|
|
|
ScStw::StatusCode ScStwRemoteRace::stop() {
|
2020-10-03 13:06:14 +02:00
|
|
|
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;
|
2020-04-18 14:25:48 +02:00
|
|
|
|
|
|
|
qDebug() << "+ --- stopping race";
|
|
|
|
|
2020-10-02 16:49:24 +02:00
|
|
|
QVariantMap reply = this->scStwClient->sendCommand(ScStw::StopRaceCommand);
|
2020-04-18 14:25:48 +02:00
|
|
|
|
2020-10-02 16:49:24 +02:00
|
|
|
return ScStw::StatusCode(reply["status"].toInt());
|
2020-04-18 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
2020-10-02 19:56:08 +02:00
|
|
|
ScStw::StatusCode ScStwRemoteRace::reset() {
|
2020-10-03 13:06:14 +02:00
|
|
|
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;
|
2020-04-18 14:25:48 +02:00
|
|
|
|
|
|
|
qDebug() << "+ --- resetting race";
|
|
|
|
|
2020-10-02 16:49:24 +02:00
|
|
|
QVariantMap reply = this->scStwClient->sendCommand(ScStw::ResetRaceCommand);
|
2020-04-18 14:25:48 +02:00
|
|
|
|
2020-10-02 16:49:24 +02:00
|
|
|
return ScStw::StatusCode(reply["status"].toInt());
|
2020-04-18 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------
|
|
|
|
// --- Base Station sync ---
|
|
|
|
// -------------------------
|
|
|
|
|
2020-10-03 13:06:14 +02:00
|
|
|
void ScStwRemoteRace::handleClientStateChange() {
|
2020-04-18 14:25:48 +02:00
|
|
|
switch (this->scStwClient->getState()) {
|
|
|
|
case ScStwClient::CONNECTED:
|
2020-10-03 13:06:14 +02:00
|
|
|
this->localTimers.clear();
|
|
|
|
this->localTimers = this->timers;
|
|
|
|
this->timers.clear();
|
2020-04-18 14:25:48 +02:00
|
|
|
break;
|
2020-10-03 13:06:14 +02:00
|
|
|
case ScStwClient::DISCONNECTED:
|
|
|
|
foreach(ScStwRemoteTimer *remoteTimer, this->remoteTimers)
|
|
|
|
remoteTimer->deleteLater();
|
|
|
|
this->remoteTimers.clear();
|
|
|
|
|
2020-04-19 13:09:24 +02:00
|
|
|
this->timers.clear();
|
2020-10-03 13:06:14 +02:00
|
|
|
this->timers = this->localTimers;
|
|
|
|
this->localTimers.clear();
|
|
|
|
emit this->timersChanged();
|
|
|
|
this->competitionMode = false;
|
2020-04-19 13:09:24 +02:00
|
|
|
this->setState(IDLE);
|
2020-04-18 14:25:48 +02:00
|
|
|
break;
|
2020-10-03 13:06:14 +02:00
|
|
|
default:
|
|
|
|
break;
|
2020-04-18 14:25:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-03 13:06:14 +02:00
|
|
|
ScStwRemoteRace::RaceMode ScStwRemoteRace::getMode() {
|
|
|
|
if(this->scStwClient->getState() == ScStwClient::CONNECTED)
|
|
|
|
return ScStwRemoteRace::REMOTE;
|
|
|
|
else
|
|
|
|
return ScStwRemoteRace::LOCAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScStwRemoteRace::local() {
|
|
|
|
return this->getMode() == LOCAL;
|
|
|
|
}
|
|
|
|
|
2020-10-03 15:15:49 +02:00
|
|
|
void ScStwRemoteRace::handleSettingChange(int keyInt, int keyLevel, QVariant value) {
|
|
|
|
if(this->local())
|
|
|
|
return ScStwRace::handleSettingChange(keyInt, keyLevel, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-18 14:25:48 +02:00
|
|
|
/**
|
|
|
|
* @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) {
|
2020-04-19 13:09:24 +02:00
|
|
|
//qDebug() << "got signal: " << data;
|
2020-04-18 14:25:48 +02:00
|
|
|
switch (key) {
|
2020-10-03 11:10:15 +02:00
|
|
|
case ScStw::RaceDetailsChanged:
|
2020-04-18 14:25:48 +02:00
|
|
|
{
|
2020-10-03 11:10:15 +02:00
|
|
|
this->refreshDetails(data.toMap());
|
2020-04-18 14:25:48 +02:00
|
|
|
break;
|
|
|
|
}
|
2020-10-03 11:10:15 +02:00
|
|
|
default:
|
|
|
|
return;
|
2020-04-18 14:25:48 +02:00
|
|
|
}
|
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
|
|
|
|
2020-10-03 11:10:15 +02:00
|
|
|
// state
|
|
|
|
this->setState(ScStwRace::RaceState(details["state"].toInt()));
|
2020-04-18 14:25:48 +02:00
|
|
|
|
2020-10-03 11:10:15 +02:00
|
|
|
// competition mode
|
2020-10-03 11:44:56 +02:00
|
|
|
if(this->competitionMode != details["competitionMode"].toBool()) {
|
|
|
|
this->competitionMode = details["competitionMode"].toBool();
|
|
|
|
emit this->competitionModeChanged();
|
|
|
|
}
|
2020-04-18 14:25:48 +02:00
|
|
|
|
2020-10-03 11:10:15 +02:00
|
|
|
// ready sound enabled
|
2020-10-03 13:06:14 +02:00
|
|
|
if(this->readySoundEnabled != details["readySoundEnabled"].toBool()) {
|
|
|
|
this->readySoundEnabled = details["readySoundEnabled"].toBool();
|
2020-10-03 11:44:56 +02:00
|
|
|
emit this->readySoundEnabledChanged();
|
|
|
|
}
|
2020-10-03 11:10:15 +02:00
|
|
|
|
|
|
|
// current start delay
|
2020-10-03 11:44:56 +02:00
|
|
|
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());
|
|
|
|
|
|
|
|
// isReady
|
|
|
|
if(this->state == WAITING) {
|
|
|
|
this->isReadyForNextState = details["isReadyForNextState"].toBool();
|
|
|
|
emit this->isReadyForNextStateChanged();
|
2020-04-18 14:25:48 +02:00
|
|
|
}
|
2020-10-03 11:10:15 +02:00
|
|
|
|
|
|
|
emit this->detailsChanged();
|
|
|
|
|
2020-04-18 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
2020-10-03 13:06:14 +02:00
|
|
|
void ScStwRemoteRace::rebuildRemoteTimers(QVariantList remoteTimers) {
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 19:56:08 +02:00
|
|
|
bool ScStwRemoteRace::refreshRemoteTimers(QVariantList remoteTimers) {
|
2020-04-18 14:25:48 +02:00
|
|
|
|
2020-10-03 13:06:14 +02:00
|
|
|
if(remoteTimers.length() != this->remoteTimers.length()){
|
2020-04-18 14:25:48 +02:00
|
|
|
// local timers are out of sync
|
2020-10-03 13:06:14 +02:00
|
|
|
this->rebuildRemoteTimers(remoteTimers);
|
2020-04-18 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach(QVariant remoteTimer, remoteTimers){
|
|
|
|
int currId = remoteTimer.toMap()["id"].toInt();
|
2020-10-02 19:56:08 +02:00
|
|
|
|
2020-10-03 13:06:14 +02:00
|
|
|
if(this->remoteTimers.length() <= currId)
|
|
|
|
this->rebuildRemoteTimers(remoteTimers);
|
|
|
|
|
2020-04-18 14:25:48 +02:00
|
|
|
ScStwTimer::TimerState newState = ScStwTimer::TimerState(remoteTimer.toMap()["state"].toInt());
|
2020-10-02 19:56:08 +02:00
|
|
|
|
2020-10-03 13:06:14 +02: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
|
|
|
|
2020-04-18 14:25:48 +02:00
|
|
|
double currentMSecsSinceEpoch = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
|
2020-10-02 19:56:08 +02:00
|
|
|
this->remoteTimers[currId]->setStartTime(currentMSecsSinceEpoch - remoteTimer.toMap()["currentTime"].toDouble());
|
2020-04-18 14:25:48 +02:00
|
|
|
|
|
|
|
if(newState >= ScStwTimer::WAITING)
|
2020-10-02 19:56:08 +02:00
|
|
|
this->remoteTimers[currId]->setStopTime(currentMSecsSinceEpoch);
|
2020-04-18 14:25:48 +02:00
|
|
|
|
2020-10-02 19:56:08 +02: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
|
|
|
|
2020-10-02 19:56:08 +02:00
|
|
|
this->remoteTimers[currId]->setState(newState);
|
2020-04-18 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-02 19:56:08 +02:00
|
|
|
bool ScStwRemoteRace::addTimer(ScStwTimer* timer) {
|
2020-10-03 13:06:14 +02:00
|
|
|
if(this->local())
|
|
|
|
return ScStwRace::addTimer(timer);
|
|
|
|
|
2020-04-18 14:25:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-19 13:09:24 +02:00
|
|
|
|
2020-10-03 11:44:56 +02:00
|
|
|
QVariantMap ScStwRemoteRace::getCurrentStartDelay() {
|
2020-10-03 13:06:14 +02:00
|
|
|
if(this->local())
|
|
|
|
return ScStwRace::getCurrentStartDelay();
|
2020-04-19 13:09:24 +02:00
|
|
|
|
2020-10-03 11:44:56 +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;
|
2020-10-03 11:44:56 +02:00
|
|
|
currentStartDelay["total"] = this->currentStartTotalDelay;
|
|
|
|
if(elapsed < 0 || elapsed > currentStartDelay["total"].toDouble()) {
|
|
|
|
elapsed = currentStartDelay["total"].toDouble();
|
2020-04-19 13:09:24 +02:00
|
|
|
}
|
2020-10-03 11:44:56 +02:00
|
|
|
currentStartDelay["progress"] = elapsed / currentStartDelay["total"].toDouble();
|
2020-04-19 13:09:24 +02:00
|
|
|
}
|
|
|
|
|
2020-10-03 11:44:56 +02:00
|
|
|
return currentStartDelay;
|
2020-04-19 13:09:24 +02:00
|
|
|
}
|
2020-10-03 11:10:15 +02:00
|
|
|
|
|
|
|
bool ScStwRemoteRace::getIsReadyForNextState() {
|
2020-10-03 13:06:14 +02:00
|
|
|
if(this->local())
|
|
|
|
return ScStwRace::getIsReadyForNextState();
|
|
|
|
|
2020-10-03 11:10:15 +02:00
|
|
|
return this->isReadyForNextState;
|
|
|
|
}
|
2020-10-03 13:06:14 +02:00
|
|
|
|
|
|
|
bool ScStwRemoteRace::getReadySoundEnabled() {
|
|
|
|
if(this->local())
|
|
|
|
return ScStwRace::getReadySoundEnabled();
|
|
|
|
|
|
|
|
return this->readySoundEnabled;
|
|
|
|
}
|