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-02 19:56:08 +02:00
|
|
|
ScStwRemoteRace::ScStwRemoteRace(ScStwClient *monitorClient, QObject *parent) : ScStwRace(parent)
|
2020-04-18 14:25:48 +02:00
|
|
|
{
|
2020-10-02 16:49:24 +02:00
|
|
|
this->currentlyWaitingForClimbers = false;
|
2020-10-03 11:10:15 +02:00
|
|
|
this->isReadyForNextState = true;
|
2020-10-02 16:49:24 +02:00
|
|
|
|
2020-04-18 14:25:48 +02:00
|
|
|
this->scStwClient = monitorClient;
|
|
|
|
|
2020-10-03 11:10:15 +02:00
|
|
|
this->scStwClient->addSignalSubscription(ScStw::RaceDetailsChanged);
|
2020-07-11 11:17:20 +02:00
|
|
|
|
2020-10-02 19:56:08 +02:00
|
|
|
connect(this->scStwClient, &ScStwClient::stateChanged, this, &ScStwRemoteRace::handleClientStateChanged);
|
|
|
|
connect(this->scStwClient, &ScStwClient::gotSignal, this, &ScStwRemoteRace::handleBaseStationSignal);
|
2020-04-18 14:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------
|
|
|
|
// --- Main Functionality ---
|
|
|
|
// --------------------------
|
|
|
|
|
2020-10-02 19:56:08 +02:00
|
|
|
ScStw::StatusCode ScStwRemoteRace::start(bool) {
|
|
|
|
return this->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
ScStw::StatusCode ScStwRemoteRace::start() {
|
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-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-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-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-02 19:56:08 +02:00
|
|
|
void ScStwRemoteRace::handleClientStateChanged() {
|
2020-04-18 14:25:48 +02:00
|
|
|
// TODO
|
|
|
|
switch (this->scStwClient->getState()) {
|
|
|
|
case ScStwClient::CONNECTED:
|
|
|
|
break;
|
|
|
|
default:
|
2020-04-19 13:09:24 +02:00
|
|
|
this->timers.clear();
|
|
|
|
this->setState(IDLE);
|
2020-04-18 14:25:48 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
this->competitionMode = details["competitionMode"].toBool();
|
2020-04-18 14:25:48 +02:00
|
|
|
|
2020-10-03 11:10:15 +02:00
|
|
|
// ready sound enabled
|
|
|
|
this->writeStartSoundSetting(ScStwSoundPlayer::Ready, details["readySoundEnabled"].toBool(), 0);
|
|
|
|
|
|
|
|
// current start delay
|
|
|
|
this->currentStartTotalDelay = details["currentStartDelay"].toList()[ScStwRace::CurrentStartStateTotalDelay].toInt();
|
|
|
|
this->currentStartDelayStartedAt = QDateTime::currentMSecsSinceEpoch() - (this->currentStartTotalDelay * details["currentStartDelay"].toList()[ScStwRace::CurrentStartStateDelayProgress].toDouble());
|
|
|
|
this->currentlyWaitingForClimbers = details["currentStartDelay"].toList()[ScStwRace::CurrentStartStateDelayProgress].toDouble() == 0 && this->currentStartTotalDelay == -1;
|
|
|
|
|
|
|
|
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-02 19:56:08 +02:00
|
|
|
bool ScStwRemoteRace::refreshRemoteTimers(QVariantList remoteTimers) {
|
2020-04-18 14:25:48 +02:00
|
|
|
|
|
|
|
if(remoteTimers.length() != this->timers.length()){
|
|
|
|
// local timers are out of sync
|
|
|
|
|
|
|
|
// delete all current timers
|
|
|
|
foreach(ScStwTimer * locTimer, this->timers){
|
|
|
|
delete locTimer;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->timers.clear();
|
|
|
|
|
|
|
|
foreach(QVariant remoteTimer, remoteTimers){
|
|
|
|
// create a local timer for each remote timer
|
2020-10-02 19:56:08 +02:00
|
|
|
ScStwRemoteTimer * timer = new ScStwRemoteTimer(this);
|
2020-04-19 13:09:24 +02:00
|
|
|
this->timers.append(timer);
|
2020-10-02 19:56:08 +02:00
|
|
|
this->remoteTimers.append(timer);
|
2020-04-19 13:09:24 +02:00
|
|
|
connect(timer, &ScStwTimer::stateChanged, this, &ScStwRace::timersChanged);
|
|
|
|
connect(timer, &ScStwTimer::reactionTimeChanged, this, &ScStwRace::timersChanged);
|
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-04-18 14:25:48 +02:00
|
|
|
ScStwTimer::TimerState newState = ScStwTimer::TimerState(remoteTimer.toMap()["state"].toInt());
|
2020-10-02 19:56:08 +02:00
|
|
|
|
|
|
|
qDebug() << "refreshing timers: id: " << currId << " state: " << newState << " readyState: " << remoteTimer.toMap()["readyState"].toInt();
|
|
|
|
|
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-04-18 14:25:48 +02:00
|
|
|
Q_UNUSED(timer)
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-19 13:09:24 +02:00
|
|
|
|
2020-10-02 19:56:08 +02:00
|
|
|
QVariantList ScStwRemoteRace::getCurrentStartDelay() {
|
2020-10-02 16:49:24 +02:00
|
|
|
int nextActionDelay = -1;
|
2020-04-19 13:09:24 +02:00
|
|
|
double nextActionDelayProg = -1;
|
|
|
|
|
2020-10-02 16:49:24 +02:00
|
|
|
if(this->getState() == WAITING && this->currentlyWaitingForClimbers) {
|
|
|
|
nextActionDelayProg = 0;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
nextActionDelay = this->currentStartTotalDelay;
|
2020-04-19 13:09:24 +02:00
|
|
|
if(elapsed < 0 || elapsed > nextActionDelay) {
|
|
|
|
elapsed = nextActionDelay;
|
|
|
|
}
|
|
|
|
nextActionDelayProg = elapsed / nextActionDelay;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
nextActionDelay,
|
|
|
|
nextActionDelayProg
|
|
|
|
};
|
|
|
|
}
|
2020-10-03 11:10:15 +02:00
|
|
|
|
|
|
|
bool ScStwRemoteRace::getIsReadyForNextState() {
|
|
|
|
return this->isReadyForNextState;
|
|
|
|
}
|