Skip to content

Commit

Permalink
Feat/RealTraffic/Direct: Fetch weather to correct baro altitude
Browse files Browse the repository at this point in the history
  • Loading branch information
TwinFan committed Jan 1, 2024
1 parent c472da4 commit a6960aa
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 118 deletions.
17 changes: 9 additions & 8 deletions Data/RealTraffic/RTAPI_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,18 @@ def UDPbcast(ip, bcast, port, data):
# Example area: Heidiland
traffic_payload = { "GUID": "%s" % GUID,
"querytype": "locationtraffic",
"top": 47.8,
"bottom": 45.6,
"left": 5.7,
"right": 10.7,
"top": 52.4, # Around EDLE
"bottom": 50.4,
"left": 5.9,
"right": 7.9,
"toffset": 0 }

weather_payload = { "GUID": "%s" % GUID,
"querytype": "locwx",
"lat": 47.8,
"lon": 7.7,
"alt": 36000,
"airports": "LSZB|LSZH|LSGG|LFSB",
"lat": 51.4069, # EDLE
"lon": 6.9391,
"alt": 0,
"airports": "UNKN", # Don't need airport METAR
"toffset": 0 }

###############################################################
Expand All @@ -110,6 +110,7 @@ def UDPbcast(ip, bcast, port, data):
# fetch weather

response = requests.post(weather_url, weather_payload, headers=header)
print(response.text)
try:
json_data = response.json()
except Exception as e:
Expand Down
Binary file added Data/RealTraffic/RT_API.sjson/725717638.083122
Binary file not shown.
Binary file modified Data/RealTraffic/RT_API.sjson/data
Binary file not shown.
Binary file modified Data/RealTraffic/RT_API.sjson/metaData
Binary file not shown.
1 change: 1 addition & 0 deletions Include/CoordCalc.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ struct positionTy {
// short-cuts to coord functions
inline double angle (const positionTy& pos2 ) const { return CoordAngle ( *this, pos2); }
inline double dist (const positionTy& pos2 ) const { return CoordDistance ( *this, pos2); }
double distRoughSqr (const positionTy& pos2) const { return DistLatLonSqr(lat(), lon(), pos2.lat(), pos2.lon()); }
inline vectorTy between (const positionTy& pos2 ) const { return CoordVectorBetween ( *this, pos2); }
inline positionTy destPos (const vectorTy& vec ) const { return CoordPlusVector ( *this, vec); }
inline positionTy operator+ (const vectorTy& vec ) const { return destPos (vec); }
Expand Down
10 changes: 5 additions & 5 deletions Include/LTChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,12 @@ inline long jog_sl (const JSON_Object *object, const char *name)
// access to JSON number field (just a shorter name, returns 0 if not a number)
inline double jog_n (const JSON_Object *object, const char *name)
{
return json_object_get_number (object, name);
return json_object_dotget_number (object, name);
}

inline long jog_l (const JSON_Object *object, const char *name)
{
return std::lround(json_object_get_number (object, name));
return std::lround(json_object_dotget_number (object, name));
}

// access to JSON number with 'null' returned as 'NAN'
Expand All @@ -415,9 +415,9 @@ double jog_sn_nan (const JSON_Object *object, const char *name);
// access to JSON boolean field (replaces -1 with false)
inline bool jog_b (const JSON_Object *object, const char *name)
{
// json_object_get_boolean returns -1 if field doesn't exit, so we
// json_object_dotget_boolean returns -1 if field doesn't exit, so we
// 'convert' -1 and 0 both to false with the following comparison:
return json_object_get_boolean (object, name) > 0;
return json_object_dotget_boolean (object, name) > 0;
}

// interprets a string-encapsulated number "0" as false, all else as true
Expand All @@ -444,7 +444,7 @@ double jag_n_nan (const JSON_Array *array, size_t idx);
// access to JSON array boolean field (replaces -1 with false)
inline bool jag_b (const JSON_Array *array, size_t idx)
{
// json_object_get_boolean returns -1 if field doesn't exit, so we
// json_array_get_boolean returns -1 if field doesn't exit, so we
// 'convert' -1 and 0 both to false with the following comparison:
return json_array_get_boolean (array, idx) > 0;
}
Expand Down
37 changes: 35 additions & 2 deletions Include/LTRealTraffic.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@

#define RT_AUTH_URL "https://rtw.flyrealtraffic.com/v3/auth"
#define RT_AUTH_POST "license=%s&software=%s"
#define RT_WEATHER_URL "https://rtw.flyrealtraffic.com/v3/weather"
#define RT_WEATHER_POST "GUID=%s&lat=%.2f&lon=%.2f&alt=%ld&airports=UNKN&querytype=locwx&toffset=%ld"
#define RT_TRAFFIC_URL "https://rtw.flyrealtraffic.com/v3/traffic"
#define RT_TRAFFIC_POST "GUID=%s&top=%.2f&bottom=%.2f&left=%.2f&right=%.2f&querytype=locationtraffic&toffset=%ld"


#define RT_LOCALHOST "0.0.0.0"
constexpr size_t RT_NET_BUF_SIZE = 8192;

Expand All @@ -69,6 +72,13 @@ constexpr double RT_VSI_AIRBORNE = 80.0; ///< if VSI is more than this then w
#define RT_TRAFFIC_XATTPSX "XATTPSX"
#define RT_TRAFFIC_XGPSPSX "XGPSPSX"

// Constant for direct connection
constexpr long RT_DRCT_DEFAULT_WAIT = 8000L; ///< [ms] Default wait time between traffic requests
constexpr std::chrono::seconds RT_DRCT_ERR_WAIT = std::chrono::seconds(5); ///< standard wait between errors
constexpr std::chrono::minutes RT_DRCT_WX_WAIT = std::chrono::minutes(10); ///< How often to update weather?
constexpr long RT_DRCT_WX_DIST = 10L * M_per_NM; ///< Distance for which weather is considered valid, greater than that and we re-request
constexpr int RT_DRCT_MAX_WX_ERR = 10; ///< Max number of consecutive errors during weather requests we wait for...before not asking for weather any longer

/// Fields in a response of a direct connection's request
enum RT_DIRECT_FIELDS_TY {
RT_DRCT_HexId = 0, ///< hexid (7c68a1)
Expand Down Expand Up @@ -228,17 +238,39 @@ class RealTrafficConnection : public LTFlightDataChannel
// RealTraffic connection status
volatile rtStatusTy status = RT_STATUS_NONE;

/// UID returned by RealTraffic upon authentication, valid for 10s only
std::string sGUID;
/// RealTraffic License type
enum RTLicTypeTy : int {
RT_LIC_UNKNOWN = 0,
RT_LIC_STANDARD = 1, ///< Standard RealTraffic license
RT_LIC_PROFESSIONAL = 2, ///< Professional RT license, allowing for historical data
} eLicType = RT_LIC_UNKNOWN;
/// QNH to use for altitude correction (will be historic QNH in case of historic data)
/// Data for the current request
struct CurrTy {
/// Which kind of call do we need next?
enum RTRequestTypeTy : int {
RT_REQU_AUTH = 1, ///< Perform Authentication request
RT_REQU_WEATHER, ///< Perform Weather request
RT_REQU_TRAFFIC, ///< Perform Traffic request
} eRequType = RT_REQU_AUTH; ///< Which type of request is being performed now?
std::string sGUID; ///< UID returned by RealTraffic upon authentication, valid for 10s only
positionTy pos; ///< viewer position for which we receive Realtraffic data
long tOff = 0; ///< time offset for which we request data
} curr; ///< Data for the current request
/// How long to wait before making the next request?
std::chrono::milliseconds rrlWait = std::chrono::milliseconds(0);

/// Weather data
struct WxTy {
double QNH = NAN; ///< baro pressure
std::chrono::steady_clock::time_point time; ///< time when RealTraffic weather was received
positionTy pos; ///< viewer position for which we received Realtraffic weather
long tOff = 0; ///< time offset for which we requested weather
int nErr = 0; ///< How many errors did we have during weather requests?

WxTy& operator = (const CurrTy& o); ///< fill from `current` data
} rtWx; ///< Data with which latest weather was requested

// TCP connection to send current position
std::thread thrTcpServer; ///< thread of the TCP listening thread (short-lived)
TCPConnection tcpPosSender; ///< TCP connection to communicate with RealTraffic
Expand Down Expand Up @@ -282,6 +314,7 @@ class RealTrafficConnection : public LTFlightDataChannel
// MARK: Direct Connection by Request/Reply
protected:
void MainDirect (); ///< thread main function for the direct connection
void SetRequType (const positionTy& pos); ///< Which request do we need now?
public:
std::string GetURL (const positionTy&) override; ///< in direct mode return URL and set
void ComputeBody (const positionTy& pos) override; ///< in direct mode puts together the POST request with the position data etc.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,33 @@
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "27C46535-2F86-4DF5-8711-ADCE3ADB80C5"
uuid = "6CB165B5-E8A3-44BE-81E2-AEA1B5DE4454"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Src/LTRealTraffic.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "357"
endingLineNumber = "357"
startingLineNumber = "424"
endingLineNumber = "424"
landmarkName = "RealTrafficConnection::ProcessFetchedData()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "1DF7D9C3-05FF-4208-9FB2-8AF1D2CD573F"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Src/LTRealTraffic.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "339"
endingLineNumber = "339"
landmarkName = "RealTrafficConnection::ProcessFetchedData()"
landmarkType = "7">
<Locations>
<Location
uuid = "27C46535-2F86-4DF5-8711-ADCE3ADB80C5 - e27fe6228f154efc"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "RealTrafficConnection::ProcessFetchedData()"
moduleName = "LiveTraffic.xpl"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/birger/Programming/XPlane/LiveTraffic/Src/LTRealTraffic.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "358"
endingLineNumber = "358"
offsetFromSymbolStart = "2866">
</Location>
<Location
uuid = "27C46535-2F86-4DF5-8711-ADCE3ADB80C5 - e27fe6228f154e9d"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "RealTrafficConnection::ProcessFetchedData()"
moduleName = "LiveTraffic.xpl"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/birger/Programming/XPlane/LiveTraffic/Src/LTRealTraffic.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "357"
endingLineNumber = "357"
offsetFromSymbolStart = "2887">
</Location>
</Locations>
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
Expand Down
14 changes: 10 additions & 4 deletions Src/LTChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ listPtrLTChannelTy listFDC;
// tests for 'null', return ptr to value if wanted
bool jog_is_null (const JSON_Object *object, const char *name, JSON_Value** ppValue)
{
JSON_Value* pJSONVal = json_object_get_value(object, name);
JSON_Value* pJSONVal = json_object_dotget_value(object, name);
if (ppValue)
*ppValue = pJSONVal;
return !pJSONVal || json_type(pJSONVal) == JSONNull;
Expand All @@ -71,7 +71,7 @@ bool jag_is_null (const JSON_Array *array,
// access to JSON string fields, with NULL or text "null" replaced by ""
const char* jog_s (const JSON_Object *object, const char *name)
{
const char* s = json_object_get_string ( object, name );
const char* s = json_object_dotget_string ( object, name );
return
!s ? "" : // found nothing
std::strcmp(s, "null") ? s : ""; // test if text is "null"
Expand All @@ -80,7 +80,7 @@ const char* jog_s (const JSON_Object *object, const char *name)
// access to JSON number fields, encapsulated as string, with NULL replaced by 0
double jog_sn (const JSON_Object *object, const char *name)
{
const char* s = json_object_get_string ( object, name );
const char* s = json_object_dotget_string ( object, name );
return s ? strtod(s,NULL) : 0.0;
}

Expand All @@ -96,7 +96,7 @@ double jog_n_nan (const JSON_Object *object, const char *name)

double jog_sn_nan (const JSON_Object *object, const char *name)
{
const char* s = json_object_get_string ( object, name );
const char* s = json_object_dotget_string ( object, name );
return s ? strtod(s,NULL) : NAN;
}

Expand Down Expand Up @@ -669,6 +669,12 @@ void LTOnlineChannel::DebugLogRaw(const char *data, long httpCode, bool bHeader)
const double now = GetSysTime();
outRaw.precision(2);
if (bHeader) {

// Empty line before a (new) SENDING request
if (httpCode == HTTP_FLAG_SENDING)
outRaw << "\n";

// Actual header
outRaw
<< std::fixed << now << ' ' << ts2string(now,2)
<< " - SimTime "
Expand Down
Loading

0 comments on commit a6960aa

Please sign in to comment.