2929
3030// MARK: OpenSky Constants
3131#define OPSKY_CHECK_NAME " OpenSky Explorer"
32- #define OPSKY_CHECK_URL " https://opensky-network.org/network/explorer "
32+ #define OPSKY_CHECK_URL " https://map. opensky-network.org/?lat=%.3f&lon=%.3f "
3333#define OPSKY_CHECK_POPUP " Check OpenSky's coverage"
3434
35+ #define OPSKY_URL_GETTOKEN " https://auth.opensky-network.org/auth/realms/opensky-network/protocol/openid-connect/token"
36+ #define OPSKY_BODY_GETTOKEN " grant_type=client_credentials&client_id=%s&client_secret=%s"
37+ #define OPSKY_ACCESS_TOKEN " access_token"
38+ #define OPSKY_AUTH_BEARER " Authorization: Bearer "
39+ #define OPSKY_AUTH_EXPIRES " expires_in"
40+ constexpr long OPSKY_AUTH_EXP_DEFAULT = 1800 ; // /< default expiration in case we don't find expiration field
41+
3542#define OPSKY_NAME " OpenSky Network"
3643#define OPSKY_URL_ALL " https://opensky-network.org/api/states/all?lamin=%.3f&lomin=%.3f&lamax=%.3f&lomax=%.3f"
37- #define OPSKY_SLUG_FMT " https://opensky-network.org/network/explorer?icao24 =%06lx"
44+ #define OPSKY_SLUG_FMT " https://map. opensky-network.org/?icao =%06lx"
3845#define OPSKY_TIME " time"
3946#define OPSKY_AIRCRAFT_ARR " states"
40- #define OPSKY_RREMAIN " X-Rate-Limit-Remaining :"
41- #define OPSKY_RETRY " X-Rate-Limit-Retry-After-Seconds :"
47+ #define OPSKY_RREMAIN " x-rate-limit-remaining :"
48+ #define OPSKY_RETRY " x-rate-limit-retry-after-seconds :"
4249constexpr int OPSKY_TRANSP_ICAO = 0 ; // icao24
4350constexpr int OPSKY_CALL = 1 ; // callsign
4451constexpr int OPSKY_COUNTRY = 2 ; // origin_county
@@ -60,14 +67,31 @@ constexpr double OPSKY_SMOOTH_GROUND = 35.0; // smooth 35s of ground data
6067//
6168class OpenSkyConnection : public LTFlightDataChannel
6269{
70+ protected:
71+ enum State {
72+ OPSKY_STATE_NONE = 0 , // /< no/initial/unknown status
73+ OPSKY_STATE_GETTING_TOKEN, // /< have credentials, but no access token yet
74+ OPSKY_STATE_GET_PLANES, // /< normal operations: fetch planes
75+ } eState = OPSKY_STATE_NONE;
76+ struct curl_slist * pHdrForm = nullptr ; // /< HTTP Header (needed during fetching a token)
77+ struct curl_slist * pHdrToken = nullptr ; // /< HTTP Header containing the bearer token
78+ float tTokenExpiration = NAN; // /< when will the token expire?
6379public:
6480 OpenSkyConnection ();
81+ void ResetStatus (); // /< used to force fetching a new token, e.g. after change of credentials
6582 std::string GetURL (const positionTy& pos) override ;
83+ void ComputeBody (const positionTy& pos) override ; // /< only needed for token request, will then form token request body
6684 bool ProcessFetchedData () override ;
6785 std::string GetStatusText () const override ; // /< return a human-readable staus
6886// // shall data of this channel be subject to LTFlightData::DataSmoothing?
6987// bool DoDataSmoothing (double& gndRange, double& airbRange) const override
7088// { gndRange = OPSKY_SMOOTH_GROUND; airbRange = OPSKY_SMOOTH_AIRBORNE; return true; }
89+
90+ // / @brief Process OpenSKy's 'crendetials.json' file to fetch User ID/Secret from it
91+ static bool ProcessCredentialsJson (const std::string& sFileName ,
92+ std::string& sClientId ,
93+ std::string& sSecret );
94+
7195protected:
7296 void Main () override ; // /< virtual thread main function
7397
@@ -155,6 +179,7 @@ constexpr unsigned long OPSKY_NUM_LN_PER_POS = 250;
155179class OpenSkyAcMasterFile : public LTACMasterdataChannel
156180{
157181protected:
182+ char sAcDbfileName [50 ] = {0 }; // /< Aircraft Database file name
158183 std::ifstream fAcDb ; // /< Aircraft Database file
159184 std::string ln; // /< a line in the database file
160185
@@ -167,7 +192,8 @@ class OpenSkyAcMasterFile : public LTACMasterdataChannel
167192public:
168193 std::string GetURL (const positionTy&) override { return " " ; } // /< No URL for the standard request processing
169194 bool ProcessFetchedData () override ; // /< Process looked up master data
170- protected:
195+ std::string GetStatusText () const override ; // /< adds the database date to the status text
196+ protected:
171197 bool AcceptRequest (const acStatUpdateTy& requ) override ; // /< accept only master data requests
172198 void Main () override ; // /< virtual thread main function
173199 bool LookupData (); // /< perform the file lookup
0 commit comments