Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

local zwift server #2328

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/qdomyos-zwift.pri
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ DEFINES += QT_DEPRECATED_WARNINGS IO_UNDER_QT SMTP_BUILD NOMINMAX
SOURCES += \
$$PWD/devices/focustreadmill/focustreadmill.cpp \
$$PWD/devices/trxappgateusbelliptical/trxappgateusbelliptical.cpp \
$$PWD/zwift-api/PlayerStateWrapper.cpp \
QTelnet.cpp \
devices/bkoolbike/bkoolbike.cpp \
devices/csaferower/csafe.cpp \
Expand Down
8 changes: 5 additions & 3 deletions src/trainprogram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ trainprogram::trainprogram(const QList<trainrow> &rows, bluetooth *b, QString *d
this->description = *description;
if (tags)
this->tags = *tags;

if(settings.value(QZSettings::zwift_username, QZSettings::default_zwift_username).toString().length() > 0) {
zwift_auth_token = new AuthToken(settings.value(QZSettings::zwift_username, QZSettings::default_zwift_username).toString(), settings.value(QZSettings::zwift_password, QZSettings::default_zwift_password).toString());
zwift_auth_token->getAccessToken();
Expand Down Expand Up @@ -602,8 +602,8 @@ void trainprogram::scheduler() {
bluetoothManager->device()->isPaused()) {

if(bluetoothManager->device() && (bluetoothManager->device()->deviceType() == bluetoothdevice::TREADMILL || bluetoothManager->device()->deviceType() == bluetoothdevice::ELLIPTICAL) &&
settings.value(QZSettings::zwift_username, QZSettings::default_zwift_username).toString().length() > 0 && zwift_auth_token &&
zwift_auth_token->access_token.length() > 0) {
settings.value(QZSettings::zwift_username, QZSettings::default_zwift_username).toString().length() > 0 && zwift_auth_token /*&&
zwift_auth_token->access_token.length() > 0*/) {
if(!zwift_world) {
zwift_world = new World(1, zwift_auth_token->getAccessToken());
qDebug() << "creating zwift api world";
Expand All @@ -615,6 +615,8 @@ void trainprogram::scheduler() {
h = new lockscreen();
#endif
#endif
zwift_player_id = 2; // on zoffline is always 2

if(zwift_player_id == -1) {
QString id = zwift_world->player_id();
QJsonParseError parseError;
Expand Down
59 changes: 59 additions & 0 deletions src/zwift-api/PlayerStateWrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "PlayerStateWrapper.h"
#include "../homeform.h"

ZwiftRequest::ZwiftRequest(const QString& getAccessToken) : getAccessToken(getAccessToken) {
// Load the custom CA certificate
QFile caFile(homeform::getWritableAppDir() + "ca.pem");
if(caFile.open(QIODevice::ReadOnly))
qDebug() << "cert file OK";
else {
qDebug() << "cert file ERR";
return;
}

QSslCertificate caCert(&caFile, QSsl::Pem);
caFile.close();

// Set up the SSL configuration
QSslConfiguration sslConfig = QSslConfiguration::defaultConfiguration();
QList<QSslCertificate> caCerts = sslConfig.caCertificates();
caCerts.append(caCert);
sslConfig.setCaCertificates(caCerts);
QSslConfiguration::setDefaultConfiguration(sslConfig);
}

QString ZwiftRequest::json(const QString& url) {
QNetworkRequest request(QUrl(BASE_URL + url));
request.setRawHeader("Accept", "application/json");
request.setRawHeader("Authorization", "Bearer " + getAccessToken.toUtf8());

QNetworkReply* reply = manager.get(request);
QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();

if (reply->error() != QNetworkReply::NoError) {
qDebug() << "Error: " << reply->errorString();
return "";
}

return reply->readAll();
}

QByteArray ZwiftRequest::protobuf(const QString& url) {
QNetworkRequest request(QUrl(BASE_URL + url));
request.setRawHeader("Accept", "application/x-protobuf-lite");
request.setRawHeader("Authorization", "Bearer " + getAccessToken.toUtf8());

QNetworkReply* reply = manager.get(request);
QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();

if (reply->error() != QNetworkReply::NoError) {
qDebug() << "Error: " << reply->errorString();
return QByteArray();
}

return reply->readAll();
}
43 changes: 5 additions & 38 deletions src/zwift-api/PlayerStateWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,19 @@
#include <QNetworkReply>
#include <QEventLoop>
#include <QDebug>
#include <QFile>

class ZwiftRequest: public QObject {
Q_OBJECT

public:
ZwiftRequest(const QString& getAccessToken) : getAccessToken(getAccessToken) {}

QString json(const QString& url) {
QNetworkRequest request(QUrl(BASE_URL + url));
request.setRawHeader("Accept", "application/json");
request.setRawHeader("Authorization", "Bearer " + getAccessToken.toUtf8());

QNetworkReply* reply = manager.get(request);
QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();

if (reply->error() != QNetworkReply::NoError) {
qDebug() << "Error: " << reply->errorString();
return "";
}

return reply->readAll();
}

QByteArray protobuf(const QString& url) {
QNetworkRequest request(QUrl(BASE_URL + url));
request.setRawHeader("Accept", "application/x-protobuf-lite");
request.setRawHeader("Authorization", "Bearer " + getAccessToken.toUtf8());

QNetworkReply* reply = manager.get(request);
QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();

if (reply->error() != QNetworkReply::NoError) {
qDebug() << "Error: " << reply->errorString();
return QByteArray();
}

return reply->readAll();
}
ZwiftRequest(const QString& getAccessToken);
QString json(const QString& url);
QByteArray protobuf(const QString& url);

private:
QNetworkAccessManager manager;
const QString BASE_URL = "https://us-or-rly101.zwift.com";
const QString BASE_URL = "https://192.168.1.3";
const QString getAccessToken;
};

Expand Down
4 changes: 2 additions & 2 deletions src/zwift-api/zwift_client_auth.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class AuthToken : public QObject {
public slots:
void updateTokenData() {
if (haveValidRefreshToken()) {
QUrl url("https://secure.zwift.com/auth/realms/zwift/tokens/access/codes");
QUrl url("https://192.168.1.3/auth/realms/zwift/tokens/access/codes");
QUrlQuery query;
query.addQueryItem("refresh_token", refresh_token);
query.addQueryItem("grant_type", "refresh_token");
Expand All @@ -64,7 +64,7 @@ public slots:
handleTokenResponse(reply);
});
} else {
QUrl url("https://secure.zwift.com/auth/realms/zwift/tokens/access/codes");
QUrl url("https://192.168.1.3/auth/realms/zwift/tokens/access/codes");
QUrlQuery postData;
postData.addQueryItem("username", QUrl::toPercentEncoding(username));
postData.addQueryItem("password", password);
Expand Down
Loading