2020-04-15 21:47:55 +02:00
|
|
|
#include "../headers/scstwappbackend.h"
|
|
|
|
|
|
|
|
ScStwAppBackend::ScStwAppBackend(QObject *parent) : QObject(parent)
|
|
|
|
{
|
|
|
|
this->appSettings = new AppSettings(this);
|
|
|
|
this->scStwClient = new ScStwClient();
|
|
|
|
this->localRace = new ScStwRace(this);
|
2020-04-19 13:09:46 +02:00
|
|
|
this->remoteRace = new ScStwRemoteMonitorRace(this->scStwClient, this);
|
2020-05-19 08:45:45 +02:00
|
|
|
this->mode = LOCAL;
|
2020-04-15 21:47:55 +02:00
|
|
|
|
|
|
|
this->scStwClient->setIP(pGlobalAppSettings->loadSetting("baseStationIpAdress"));
|
|
|
|
connect(this->scStwClient, &ScStwClient::stateChanged, this, &ScStwAppBackend::baseStationStateChanged);
|
|
|
|
connect(this->scStwClient, &ScStwClient::stateChanged, this, &ScStwAppBackend::refreshMode);
|
|
|
|
connect(this, &ScStwAppBackend::baseStationStateChanged, this, &ScStwAppBackend::baseStationPropertiesChanged);
|
|
|
|
|
|
|
|
this->localRace->addTimer(new ScStwTimer(this));
|
2020-04-17 19:57:33 +02:00
|
|
|
this->reloadRaceSettings();
|
2020-04-15 21:47:55 +02:00
|
|
|
|
|
|
|
this->timerTextRefreshTimer = new QTimer(this);
|
|
|
|
this->timerTextRefreshTimer->setInterval(1);
|
|
|
|
this->timerTextRefreshTimer->setSingleShot(true);
|
|
|
|
this->timerTextRefreshTimer->connect(this->timerTextRefreshTimer, &QTimer::timeout, this, &ScStwAppBackend::refreshTimerText);
|
|
|
|
this->refreshTimerText();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------
|
|
|
|
// --- helper functions ---
|
|
|
|
// ------------------------
|
|
|
|
|
|
|
|
void ScStwAppBackend::refreshMode() {
|
|
|
|
RaceMode newMode;
|
|
|
|
if(this->scStwClient->getState() == ScStwClient::CONNECTED){
|
|
|
|
newMode = REMOTE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
newMode = LOCAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this->mode != newMode){
|
|
|
|
|
|
|
|
if(newMode == LOCAL){
|
|
|
|
// if the new mode is local -> connection to base station has been lost
|
|
|
|
|
2020-04-17 19:57:33 +02:00
|
|
|
// reset local race
|
2020-04-15 21:47:55 +02:00
|
|
|
this->getRace()->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
this->mode = newMode;
|
|
|
|
emit this->modeChanged();
|
2020-04-19 13:09:46 +02:00
|
|
|
emit this->raceChanged();
|
|
|
|
emit this->getRace()->stateChanged(this->getRace()->getState());
|
2020-04-15 21:47:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScStwAppBackend::refreshTimerText() {
|
|
|
|
|
|
|
|
// --- refresh timer text ---
|
2020-04-17 19:57:33 +02:00
|
|
|
if(this->getRace()->getState() == ScStwRace::RUNNING) {
|
|
|
|
emit this->getRace()->timersChanged();
|
|
|
|
}
|
2020-04-15 21:47:55 +02:00
|
|
|
|
|
|
|
// --- refresh next start action delay progress ---
|
2020-04-17 19:57:33 +02:00
|
|
|
if(this->getRace()->getState() == ScStwRace::STARTING) {
|
|
|
|
emit this->getRace()->nextStartActionDetailsChanged();
|
|
|
|
}
|
2020-04-15 21:47:55 +02:00
|
|
|
|
|
|
|
this->timerTextRefreshTimer->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
// - athlete management -
|
|
|
|
|
2020-04-19 13:09:46 +02:00
|
|
|
// TODO: move to client
|
2020-04-15 21:47:55 +02:00
|
|
|
QVariant ScStwAppBackend::getAthletes() {
|
|
|
|
QVariantMap reply = this->scStwClient->sendCommand(4003);
|
|
|
|
|
|
|
|
if(reply["status"] != 200){
|
|
|
|
//handle Error!!
|
|
|
|
qDebug() << "+ --- error getting athletes: " << reply["status"];
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariantMap tmpAthletes = reply["data"].toMap();
|
|
|
|
|
|
|
|
//qDebug() << tmpAthletes;
|
|
|
|
|
|
|
|
return tmpAthletes;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScStwAppBackend::createAthlete(QString userName, QString fullName) {
|
|
|
|
|
|
|
|
QVariant requestData = QVariantMap({{"fullName", fullName}, {"userName", userName}});
|
|
|
|
|
|
|
|
QVariantMap reply = this->scStwClient->sendCommand(4001, requestData.toJsonValue());
|
|
|
|
|
|
|
|
if(reply["status"] != 200){
|
|
|
|
//handle Error!!
|
|
|
|
qDebug() << "+ --- error creating athlete: " << reply["status"];
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScStwAppBackend::deleteAthlete( QString userName ){
|
|
|
|
|
|
|
|
QVariant requestData = QVariantMap({{"userName", userName}});
|
|
|
|
|
|
|
|
QVariantMap reply = this->scStwClient->sendCommand(4002, requestData.toJsonValue());
|
|
|
|
|
|
|
|
if(reply["status"] != 200){
|
|
|
|
//handle Error!!
|
|
|
|
qDebug() << "+ --- error deleting athlete: " << reply["status"];
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScStwAppBackend::selectAthlete( QString userName, int timerId ){
|
|
|
|
|
|
|
|
QVariant requestData = QVariantMap({{"userName", userName}, {"timerId", timerId}});
|
|
|
|
|
|
|
|
QVariantMap reply = this->scStwClient->sendCommand(4000, requestData.toJsonValue());
|
|
|
|
|
|
|
|
if(reply["status"] != 200){
|
|
|
|
//handle Error!!
|
|
|
|
qDebug() << "+ --- error selecting athlete: " << reply["status"];
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant ScStwAppBackend::getResults( QString userName ){
|
|
|
|
QVariantMap reply = this->scStwClient->sendCommand(4004, userName);
|
|
|
|
|
|
|
|
if(reply["status"] != 200){
|
|
|
|
//handle Error!!
|
|
|
|
qDebug() << "+ --- error getting results: " << reply["status"];
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariantList tmpAthletes = reply["data"].toList();
|
|
|
|
|
|
|
|
//qDebug() << tmpAthletes;
|
|
|
|
|
|
|
|
return tmpAthletes;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------
|
|
|
|
// --- functions for qml ---
|
|
|
|
// -------------------------
|
|
|
|
|
|
|
|
ScStwRace* ScStwAppBackend::getRace() {
|
|
|
|
switch (this->mode) {
|
|
|
|
case LOCAL:
|
|
|
|
return this->localRace;
|
2020-04-19 13:09:46 +02:00
|
|
|
case REMOTE:
|
|
|
|
return this->remoteRace;
|
2020-04-15 21:47:55 +02:00
|
|
|
}
|
2020-04-19 13:09:46 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScStwClient* ScStwAppBackend::getScStwClient() {
|
|
|
|
return this->scStwClient;
|
2020-04-15 21:47:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int ScStwAppBackend::getMode() {
|
|
|
|
return this->mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScStwAppBackend::writeSetting(QString key, QVariant value) {
|
|
|
|
if(this->mode == REMOTE && ScStw::baseStationSettingFromString(key) != ScStw::InvalidSetting ){
|
|
|
|
this->scStwClient->writeRemoteSetting(ScStw::baseStationSettingFromString(key), value.toString());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this->appSettings->writeSetting(key, value);
|
|
|
|
}
|
2020-04-17 19:57:33 +02:00
|
|
|
|
|
|
|
this->reloadRaceSettings();
|
2020-04-19 13:09:46 +02:00
|
|
|
this->reloadBaseStationIpAdress();
|
2020-04-15 21:47:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScStwAppBackend::writeSetting(ScStw::BaseStationSetting key, QVariant value) {
|
|
|
|
if(ScStw::baseStationSettingToString(key) != "Invalid" ){
|
|
|
|
this->writeSetting(ScStw::baseStationSettingToString(key), value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ScStwAppBackend::readSetting(QString key) {
|
|
|
|
if(this->mode == REMOTE && ScStw::baseStationSettingFromString(key) != ScStw::InvalidSetting){
|
|
|
|
return this->scStwClient->readRemoteSetting(ScStw::baseStationSettingFromString(key));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return this->appSettings->loadSetting(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ScStwAppBackend::readSetting(ScStw::BaseStationSetting key) {
|
|
|
|
if(ScStw::baseStationSettingToString(key) != "Invalid") {
|
|
|
|
return this->readSetting(ScStw::baseStationSettingToString(key));
|
|
|
|
}
|
|
|
|
return "false";
|
|
|
|
}
|
|
|
|
|
2020-04-17 19:57:33 +02:00
|
|
|
void ScStwAppBackend::reloadRaceSettings() {
|
|
|
|
this->getRace()->writeStartActionSetting(
|
|
|
|
ScStwRace::AtYourMarks,
|
|
|
|
this->appSettings->loadSetting(ScStw::baseStationSettingToString(ScStw::AtYourMarksSoundEnableSetting)) == "true",
|
|
|
|
this->appSettings->loadSetting(ScStw::baseStationSettingToString(ScStw::AtYourMarksSoundDelaySetting)).toDouble()
|
|
|
|
);
|
|
|
|
|
|
|
|
this->getRace()->writeStartActionSetting(
|
|
|
|
ScStwRace::Ready,
|
|
|
|
this->appSettings->loadSetting(ScStw::baseStationSettingToString(ScStw::ReadySoundEnableSetting)) == "true",
|
|
|
|
this->appSettings->loadSetting(ScStw::baseStationSettingToString(ScStw::ReadySoundDelaySetting)).toDouble()
|
|
|
|
);
|
|
|
|
|
|
|
|
this->getRace()->setSoundVolume(1);
|
|
|
|
|
|
|
|
}
|
2020-04-15 21:47:55 +02:00
|
|
|
|
2020-04-19 13:09:46 +02:00
|
|
|
void ScStwAppBackend::reloadBaseStationIpAdress() {
|
|
|
|
this->scStwClient->setIP(pGlobalAppSettings->loadSetting("baseStationIpAdress"));
|
2020-04-15 21:47:55 +02:00
|
|
|
}
|