136 lines
3.7 KiB
C++
136 lines
3.7 KiB
C++
#include "../headers/scstwappbackend.h"
|
|
|
|
ScStwAppBackend::ScStwAppBackend(QObject *parent) : QObject(parent)
|
|
{
|
|
this->scStwClient = new ScStwClient(this);
|
|
|
|
this->appSettings = new ScStwAppSettings(this->scStwClient, this);
|
|
|
|
this->race = new ScStwRemoteRace(this->scStwClient, this->appSettings, this);
|
|
this->race->addTimer(new ScStwTimer(this));
|
|
|
|
this->scStwClient->setIP(this->appSettings->readSetting(ScStwAppSettings::BaseStationIpSetting).toString());
|
|
|
|
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::refreshTimerText() {
|
|
|
|
// --- refresh timer text ---
|
|
if(this->getRace()->getState() == ScStwRace::RUNNING) {
|
|
emit this->getRace()->timersChanged();
|
|
}
|
|
|
|
// --- refresh next start action delay progress ---
|
|
if(this->getRace()->getState() == ScStwRace::WAITING || this->getRace()->getState() == ScStwRace::PREPAIRING) {
|
|
emit this->getRace()->currentStartDelayChanged();
|
|
}
|
|
|
|
this->timerTextRefreshTimer->start();
|
|
}
|
|
|
|
// - athlete management -
|
|
|
|
// TODO: move to client
|
|
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() {
|
|
return this->race;
|
|
}
|
|
|
|
ScStwAppSettings * ScStwAppBackend::getSettings() {
|
|
return this->appSettings;
|
|
}
|
|
|
|
ScStwClient* ScStwAppBackend::getScStwClient() {
|
|
return this->scStwClient;
|
|
}
|