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

Zwift Coordinates from API #1978

Open
wants to merge 3 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
2 changes: 2 additions & 0 deletions src/qdomyos-zwift.pri
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ HEADERS += \
$$PWD/proformtelnetbike.h \
$$PWD/windows_zwift_workout_paddleocr_thread.h \
$$PWD/fakerower.h \
$$PWD/zwift-api/MapCoordinate.h \
$$PWD/zwift-api/PlayerStateWrapper.h \
$$PWD/zwift-api/ZwiftWorldConstants.h \
$$PWD/zwift-api/zwift_client_auth.h \
virtualdevice.h \
$$PWD/androidactivityresultreceiver.h \
Expand Down
27 changes: 26 additions & 1 deletion src/trainprogram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,15 +621,27 @@ void trainprogram::scheduler() {
static int zwift_counter = 5;
if(zwift_counter++ >= 4) {
zwift_counter = 0;

QString id = zwift_world->player_id();
QJsonParseError parseError;
QJsonDocument document = QJsonDocument::fromJson(id.toLocal8Bit(), &parseError);
QJsonObject ride = document.object();
qDebug() << "zwift api player" << ride;
int worldId = ride[QStringLiteral("worldId")].toInt();

QByteArray bb = zwift_world->playerStatus(zwift_player_id);
#ifdef Q_OS_IOS
#ifndef IO_UNDER_QT
h->zwift_api_decodemessage_player(bb.data(), bb.length());
float alt = h->zwift_api_getaltitude();
float distance = h->zwift_api_getdistance();
float lat = h->zwift_api_getlatitude();
float lon = h->zwift_api_getlongitude();
#else
float alt = 0;
float distance = 0;
float lat = 0;
float lon = 0;
#endif
#elif defined(Q_OS_ANDROID)
QAndroidJniEnvironment env;
Expand All @@ -648,11 +660,24 @@ void trainprogram::scheduler() {
#else
float alt = 0;
float distance = 0;
float lat = 0;
float lon = 0;
#endif
static float old_distance = 0;
static float old_alt = 0;

qDebug() << "zwift api incline1" << old_distance << old_alt << distance << alt;
qDebug() << "zwift api incline1" << old_distance << old_alt << distance << alt << lat << lon;

MapCoordinate* coord = zwift_world->ToMapCoordinate(worldId, lon, lat, alt);
if(coord) {
qDebug() << "zwift coordinate" << coord->X << coord->Y;
QGeoCoordinate p;
p.setLatitude(coord->X);
p.setLongitude(coord->Y);
p.setAltitude(coord->Altitude);
emit changeGeoPosition(p, 0, 0);
delete coord;
}

if(old_distance > 0) {
float delta = distance - old_distance;
Expand Down
21 changes: 21 additions & 0 deletions src/zwift-api/MapCoordinate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef MAPCOORDINATE_H
#define MAPCOORDINATE_H

#include "ZwiftWorldConstants.h"

class MapCoordinate {
public:
double X;
double Y;
double Altitude;
double WorldId;

MapCoordinate(double X, double Y, double Altitude, double WorldID) {
this->X = X;
this->Y = Y;
this->Altitude = Altitude;
this->WorldId = WorldID;
};
};

#endif // MAPCOORDINATE_H
87 changes: 87 additions & 0 deletions src/zwift-api/PlayerStateWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include <QEventLoop>
#include <QDebug>

#include "MapCoordinate.h"
#include "ZwiftWorldConstants.h"

class ZwiftRequest: public QObject {
Q_OBJECT

Expand Down Expand Up @@ -62,6 +65,61 @@ class World {
public:
World(int worldId, const QString& getAccessToken) : worldId(worldId), request(getAccessToken) {}

MapCoordinate* ToMapCoordinate(int worldId, double Longitude, double Latitude, double Altitude)
{
ZwiftWorldConstants* worldConstants;

switch (worldId)
{
case _Watopia:
worldConstants = Watopia;
break;
case _MakuriIslands:
worldConstants = MakuriIslands;
break;
case _Richmond:
worldConstants = Richmond;
break;
case _London:
worldConstants = London;
break;
case _NewYork:
worldConstants = NewYork;
break;
case _Innsbruck:
worldConstants = Innsbruck;
break;
case _Bologna:
worldConstants = Bologna;
break;
case _Yorkshire:
worldConstants = Yorkshire;
break;
case _CritCity:
worldConstants = CritCity;
break;
case _France:
worldConstants = France;
break;
case _Paris:
worldConstants = Paris;
break;
default:
return new MapCoordinate(0,0,0,0);
}

// NOTE: The coordinates in Zwift itself are flipped which
// is why you see longitude used to calculate latitude
// and negative latitude to calculate longitude.
double latitudeAsCentimetersFromOrigin = (Longitude * worldConstants->MetersBetweenLatitudeDegree * 100);
double latitudeOffsetCentimeters = latitudeAsCentimetersFromOrigin - worldConstants->CenterLatitudeFromOrigin;

double longitudeAsCentimetersFromOrigin = -Latitude * worldConstants->MetersBetweenLongitudeDegree * 100;
double longitudeOffsetCentimeters = longitudeAsCentimetersFromOrigin - worldConstants->CenterLongitudeFromOrigin;

return new MapCoordinate(latitudeOffsetCentimeters, longitudeOffsetCentimeters, Altitude, worldId);
}

QString getPlayers() {
return request.json("/relay/worlds/" + QString::number(worldId));
}
Expand All @@ -78,6 +136,35 @@ class World {
private:
int worldId;
ZwiftRequest request;

enum ZwiftWorldId
{
_Unknown = -1,
_Watopia = 1,
_Richmond = 2,
_London = 3,
_NewYork = 4,
_Innsbruck = 5,
_Bologna = 6,
_Yorkshire = 7,
_CritCity = 8,
_MakuriIslands = 9,
_France = 10,
_Paris = 11
};

// https://github.com/sandermvanvliet/RoadCaptain/blob/d8ec891349212d2a8ef2691925376712680e0bc4/src/RoadCaptain/TrackPoint.cs#L256
ZwiftWorldConstants* Watopia = new ZwiftWorldConstants(110614.71, 109287.52, -11.644904f, 166.95293);
ZwiftWorldConstants* Richmond = new ZwiftWorldConstants(110987.82, 88374.68, 37.543f, -77.4374f);
ZwiftWorldConstants* London = new ZwiftWorldConstants(111258.3, 69400.28, 51.501705f, -0.16794094f);
ZwiftWorldConstants* NewYork = new ZwiftWorldConstants(110850.0, 84471.0, 40.76723f, -73.97667f);
ZwiftWorldConstants* Innsbruck = new ZwiftWorldConstants(111230.0, 75027.0, 47.2728f, 11.39574f);
ZwiftWorldConstants* Bologna = new ZwiftWorldConstants(111230.0, 79341.0, 44.49477f, 11.34324f);
ZwiftWorldConstants* Yorkshire = new ZwiftWorldConstants(111230.0, 65393.0, 53.991127f, -1.541751f);
ZwiftWorldConstants* CritCity = new ZwiftWorldConstants(110614.71, 109287.52, -10.3844f, 165.8011f);
ZwiftWorldConstants* MakuriIslands = new ZwiftWorldConstants(110614.71, 109287.52, -10.749806f, 165.83644f);
ZwiftWorldConstants* France = new ZwiftWorldConstants(110726.0, 103481.0, -21.695074f, 166.19745f);
ZwiftWorldConstants* Paris = new ZwiftWorldConstants(111230.0, 73167.0, 48.86763f, 2.31413f);
};

class PlayerStateWrapper {
Expand Down
25 changes: 25 additions & 0 deletions src/zwift-api/ZwiftWorldConstants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef ZWIFTWORLDCONSTANTS_H
#define ZWIFTWORLDCONSTANTS_H

class ZwiftWorldConstants
{
public:
double MetersBetweenLatitudeDegree;
double MetersBetweenLongitudeDegree;
double MetersBetweenLatitudeDegreeMul;
double MetersBetweenLongitudeDegreeMul;
double CenterLatitudeFromOrigin;
double CenterLongitudeFromOrigin;

ZwiftWorldConstants(double metersBetweenLatitudeDegree, double metersBetweenLongitudeDegree, double centerLatitudeFromOrigin, double centerLongitudeFromOrigin)
{
MetersBetweenLatitudeDegree = metersBetweenLatitudeDegree;
MetersBetweenLongitudeDegree = metersBetweenLongitudeDegree;
MetersBetweenLatitudeDegreeMul = 1 / metersBetweenLatitudeDegree;
MetersBetweenLongitudeDegreeMul = 1 / metersBetweenLongitudeDegree;
CenterLatitudeFromOrigin = centerLatitudeFromOrigin * metersBetweenLatitudeDegree * 100;
CenterLongitudeFromOrigin = centerLongitudeFromOrigin * metersBetweenLongitudeDegree * 100;
}
};

#endif // ZWIFTWORLDCONSTANTS_H
Loading