-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #790 from otahirs/oresutlsService
feat: OResults service
- Loading branch information
Showing
10 changed files
with
493 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
quickevent/app/quickevent/plugins/Event/src/services/oresultsclient.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
#include "oresultsclient.h" | ||
#include "oresultsclientwidget.h" | ||
|
||
#include "../eventplugin.h" | ||
|
||
#include <qf/qmlwidgets/framework/mainwindow.h> | ||
#include <qf/qmlwidgets/dialogs/dialog.h> | ||
|
||
#include <qf/core/log.h> | ||
#include <plugins/Runs/src/runsplugin.h> | ||
#include <plugins/Relays/src/relaysplugin.h> | ||
|
||
#include <QDir> | ||
#include <QFile> | ||
#include <QHttpPart> | ||
#include <QNetworkAccessManager> | ||
#include <QNetworkReply> | ||
#include <QSettings> | ||
#include <QStandardPaths> | ||
#include <QTextStream> | ||
#include <QTimer> | ||
|
||
namespace qfc = qf::core; | ||
namespace qfw = qf::qmlwidgets; | ||
namespace qfd = qf::qmlwidgets::dialogs; | ||
namespace qfs = qf::core::sql; | ||
using qf::qmlwidgets::framework::getPlugin; | ||
using Event::EventPlugin; | ||
using Relays::RelaysPlugin; | ||
using Runs::RunsPlugin; | ||
|
||
namespace Event { | ||
namespace services { | ||
|
||
OResultsClient::OResultsClient(QObject *parent) | ||
: Super(OResultsClient::serviceName(), parent) | ||
{ | ||
m_networkManager = new QNetworkAccessManager(this); | ||
m_exportTimer = new QTimer(this); | ||
connect(m_exportTimer, &QTimer::timeout, this, &OResultsClient::onExportTimerTimeOut); | ||
connect(this, &OResultsClient::statusChanged, [this](Status status) { | ||
if(status == Status::Running) { | ||
onExportTimerTimeOut(); | ||
m_exportTimer->start(); | ||
} | ||
else { | ||
m_exportTimer->stop(); | ||
} | ||
}); | ||
connect(this, &OResultsClient::settingsChanged, this, &OResultsClient::init, Qt::QueuedConnection); | ||
|
||
} | ||
|
||
QString OResultsClient::serviceName() | ||
{ | ||
return QStringLiteral("OResults"); | ||
} | ||
|
||
void OResultsClient::exportResultsIofXml3() | ||
{ | ||
int current_stage = getPlugin<EventPlugin>()->currentStageId(); | ||
bool is_relays = getPlugin<EventPlugin>()->eventConfig()->isRelays(); | ||
|
||
QString str = is_relays | ||
? getPlugin<RelaysPlugin>()->resultsIofXml30() | ||
: getPlugin<RunsPlugin>()->resultsIofXml30Stage(current_stage); | ||
|
||
sendFile("results upload", "/results", str); | ||
} | ||
|
||
void OResultsClient::exportStartListIofXml3() | ||
{ | ||
|
||
int current_stage = getPlugin<EventPlugin>()->currentStageId(); | ||
bool is_relays = getPlugin<EventPlugin>()->eventConfig()->isRelays(); | ||
|
||
QString str = is_relays | ||
? getPlugin<RelaysPlugin>()->startListIofXml30() | ||
: getPlugin<RunsPlugin>()->startListStageIofXml30(current_stage); | ||
|
||
sendFile("start list upload", "/start-lists?format=xml", str); | ||
} | ||
|
||
qf::qmlwidgets::framework::DialogWidget *OResultsClient::createDetailWidget() | ||
{ | ||
auto *w = new OResultsClientWidget(); | ||
return w; | ||
} | ||
|
||
void OResultsClient::init() | ||
{ | ||
OResultsClientSettings ss = settings(); | ||
m_exportTimer->setInterval(ss.exportIntervalSec() * 1000); | ||
} | ||
|
||
void OResultsClient::onExportTimerTimeOut() | ||
{ | ||
if(status() != Status::Running) | ||
return; | ||
|
||
exportResultsIofXml3(); | ||
} | ||
|
||
void OResultsClient::loadSettings() | ||
{ | ||
Super::loadSettings(); | ||
init(); | ||
} | ||
|
||
void OResultsClient::sendFile(QString name, QString request_path, QString file) { | ||
|
||
QHttpMultiPart *multi_part = new QHttpMultiPart(QHttpMultiPart::FormDataType); | ||
|
||
QHttpPart api_key_part; | ||
auto api_key = settings().apiKey(); | ||
api_key_part.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"apiKey\"")); | ||
api_key_part.setBody(api_key.toUtf8()); | ||
|
||
QHttpPart file_part; | ||
file_part.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/xml")); | ||
file_part.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"")); | ||
file_part.setBody(file.toUtf8()); | ||
|
||
multi_part->append(api_key_part); | ||
multi_part->append(file_part); | ||
|
||
QUrl url(API_URL + request_path); | ||
QNetworkRequest request(url); | ||
QNetworkReply *reply = m_networkManager->post(request, multi_part); | ||
|
||
connect(reply, &QNetworkReply::finished, [reply, name]() | ||
{ | ||
if(reply->error()) | ||
{ | ||
qfError() << "OReuslts.eu [" + name + "]: " + QString(reply->readAll()); | ||
} | ||
else | ||
{ | ||
qfInfo() << "OReuslts.eu [" + name + "]: success"; | ||
} | ||
reply->deleteLater(); | ||
}); | ||
} | ||
|
||
}} |
53 changes: 53 additions & 0 deletions
53
quickevent/app/quickevent/plugins/Event/src/services/oresultsclient.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#ifndef ORESULTSCLIENT_H | ||
#define ORESULTSCLIENT_H | ||
|
||
#pragma once | ||
|
||
#include "service.h" | ||
|
||
class QTimer; | ||
class QNetworkAccessManager; | ||
|
||
namespace Event { | ||
namespace services { | ||
|
||
class OResultsClientSettings : public ServiceSettings | ||
{ | ||
using Super = ServiceSettings; | ||
|
||
QF_VARIANTMAP_FIELD(QString, a, setA, piKey) | ||
QF_VARIANTMAP_FIELD2(int, e, setE, xportIntervalSec, 15) | ||
public: | ||
OResultsClientSettings(const QVariantMap &o = QVariantMap()) : Super(o) {} | ||
}; | ||
|
||
class OResultsClient : public Service | ||
{ | ||
Q_OBJECT | ||
|
||
using Super = Service; | ||
public: | ||
OResultsClient(QObject *parent); | ||
|
||
//void run() override; | ||
//void stop() override; | ||
OResultsClientSettings settings() const {return OResultsClientSettings(m_settings);} | ||
|
||
static QString serviceName(); | ||
|
||
void exportResultsIofXml3(); | ||
void exportStartListIofXml3(); | ||
void loadSettings() override; | ||
private: | ||
qf::qmlwidgets::framework::DialogWidget *createDetailWidget() override; | ||
void onExportTimerTimeOut(); | ||
void init(); | ||
QTimer *m_exportTimer = nullptr; | ||
QNetworkAccessManager *m_networkManager = nullptr; | ||
void sendFile(QString name, QString request_path, QString file); | ||
const QString API_URL = "https://api.oresults.eu"; | ||
}; | ||
|
||
}} | ||
|
||
#endif // ORESULTSCLIENT_H |
88 changes: 88 additions & 0 deletions
88
quickevent/app/quickevent/plugins/Event/src/services/oresultsclientwidget.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#include "oresultsclientwidget.h" | ||
#include "ui_oresultsclientwidget.h" | ||
#include "oresultsclient.h" | ||
#include "../eventplugin.h" | ||
#include <qf/qmlwidgets/framework/mainwindow.h> | ||
|
||
#include <qf/qmlwidgets/dialogs/messagebox.h> | ||
|
||
#include <qf/core/assert.h> | ||
|
||
#include <QFileDialog> | ||
using qf::qmlwidgets::framework::getPlugin; | ||
|
||
namespace Event { | ||
namespace services { | ||
|
||
OResultsClientWidget::OResultsClientWidget(QWidget *parent) | ||
: Super(parent) | ||
, ui(new Ui::OResultsClientWidget) | ||
{ | ||
setPersistentSettingsId("OResultsClientWidget"); | ||
ui->setupUi(this); | ||
|
||
OResultsClient *svc = service(); | ||
if(svc) { | ||
OResultsClientSettings ss = svc->settings(); | ||
ui->edExportInterval->setValue(ss.exportIntervalSec()); | ||
ui->edApiKey->setText(ss.apiKey()); | ||
} | ||
|
||
connect(ui->btExportResultsXml30, &QPushButton::clicked, this, &OResultsClientWidget::onBtExportResultsXml30Clicked); | ||
connect(ui->btExportStartListXml30, &QPushButton::clicked, this, &OResultsClientWidget::onBtExportStartListXml30Clicked); | ||
} | ||
|
||
OResultsClientWidget::~OResultsClientWidget() | ||
{ | ||
delete ui; | ||
} | ||
|
||
bool OResultsClientWidget::acceptDialogDone(int result) | ||
{ | ||
if(result == QDialog::Accepted) { | ||
if(!saveSettings()) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
OResultsClient *OResultsClientWidget::service() | ||
{ | ||
OResultsClient *svc = qobject_cast<OResultsClient*>(Service::serviceByName(OResultsClient::serviceName())); | ||
QF_ASSERT(svc, OResultsClient::serviceName() + " doesn't exist", return nullptr); | ||
return svc; | ||
} | ||
|
||
bool OResultsClientWidget::saveSettings() | ||
{ | ||
OResultsClient *svc = service(); | ||
if(svc) { | ||
OResultsClientSettings ss = svc->settings(); | ||
ss.setExportIntervalSec(ui->edExportInterval->value()); | ||
ss.setApiKey(ui->edApiKey->text().trimmed()); | ||
|
||
svc->setSettings(ss); | ||
} | ||
return true; | ||
} | ||
|
||
void OResultsClientWidget::onBtExportResultsXml30Clicked() | ||
{ | ||
OResultsClient *svc = service(); | ||
if(svc) { | ||
saveSettings(); | ||
svc->exportResultsIofXml3(); | ||
} | ||
} | ||
|
||
void OResultsClientWidget::onBtExportStartListXml30Clicked() | ||
{ | ||
OResultsClient *svc = service(); | ||
if(svc) { | ||
saveSettings(); | ||
svc->exportStartListIofXml3(); | ||
} | ||
} | ||
}} | ||
|
33 changes: 33 additions & 0 deletions
33
quickevent/app/quickevent/plugins/Event/src/services/oresultsclientwidget.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include <qf/qmlwidgets/framework/dialogwidget.h> | ||
|
||
namespace Event { | ||
namespace services { | ||
|
||
namespace Ui { | ||
class OResultsClientWidget; | ||
} | ||
|
||
class OResultsClient; | ||
|
||
class OResultsClientWidget : public qf::qmlwidgets::framework::DialogWidget | ||
{ | ||
Q_OBJECT | ||
|
||
using Super = qf::qmlwidgets::framework::DialogWidget; | ||
public: | ||
explicit OResultsClientWidget(QWidget *parent = nullptr); | ||
~OResultsClientWidget(); | ||
private: | ||
void onBtExportResultsXml30Clicked(); | ||
void onBtExportStartListXml30Clicked(); | ||
OResultsClient* service(); | ||
bool saveSettings(); | ||
private: | ||
Ui::OResultsClientWidget *ui; | ||
bool acceptDialogDone(int result); | ||
}; | ||
|
||
}} | ||
|
Oops, something went wrong.