Skip to content

Commit

Permalink
Feat/RT/Weather: Config 3-way, with Auto=When XP is set to real weather
Browse files Browse the repository at this point in the history
  • Loading branch information
TwinFan committed Jul 16, 2024
1 parent e3ed0c6 commit 942d2c5
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 47 deletions.
4 changes: 2 additions & 2 deletions Include/DataRefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ class DataRefs
SimTimeCtrlTy rtSTC = STC_SIM_TIME_PLUS_BUFFER; ///< Which sim time to send to RealTraffic?
int rtManTOfs = 0; ///< manually configure time offset for requesting historic data
RTConnTypeTy rtConnType = RT_CONN_REQU_REPL; ///< Which type of connection to use for RealTraffic data
int rtSetWeather = 0; ///< Set X-Plane's weather based on RealTraffic weather data
int rtSetWeather = 1; ///< Set X-Plane's weather based on RealTraffic weather data? (0-Off, 1-Auto, 2-On)
int ffListenPort = 63093; ///< UDP Port to listen to ForeFlight announcing itself, https://www.foreflight.com/connect/spec/
int ffSendPort = 49002; ///< UDP Port to send simulator data to ForeFlight, https://www.foreflight.com/support/network-gps/
int bffUserPlane = 1; // bool Send User plane data?
Expand Down Expand Up @@ -993,7 +993,7 @@ class DataRefs
RTConnTypeTy GetRTConnType () const { return rtConnType; }
const std::string& GetRTLicense () const { return sRTLicense; }
void SetRTLicense (const std::string& license) { sRTLicense = license; }
bool ShallSetRTWeather() const { return rtSetWeather; }
int GetRTSetWeather() const { return rtSetWeather; }

size_t GetFSCEnv() const { return (size_t)fscEnv; }
void GetFSCharterCredentials (std::string& user, std::string& pwd)
Expand Down
18 changes: 11 additions & 7 deletions Include/LTWeather.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@

/// Initialize Weather module, dataRefs
bool WeatherInit ();
/// Shutdown Weather module
void WeatherStop ();

/// Can we, technically, set weather? (X-Plane 12 forward only)
bool WeatherCanSet ();
/// Shall we actuall set the weather as per ability and user's configuration?
bool WeatherShallSet ();

/// Indicate that we are taking control now
void WeatherTakeControl ();
/// Are we controlling weather?
bool WeatherInControl ();
/// Return a human readable string on the weather source, is "LiveTraffic" if WeatherInControl()
std::string WeatherGetSource ();
/// Reset weather settings to what they were before X-Plane took over
void WeatherReset ();
/// Shutdown Weather module
void WeatherStop ();

/// Return a human readable string on the weather source, is "LiveTraffic" if WeatherInControl()
std::string WeatherGetSource ();

//
// MARK: Set X-Plane Weather
Expand Down Expand Up @@ -105,9 +112,6 @@ struct LTWeather

};

/// Can we set weather? (X-Plane 12 forward only)
bool WeatherCanSet ();

//
// MARK: Fetch METAR
//
Expand Down
2 changes: 1 addition & 1 deletion Src/DataRefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ DataRefs::dataRefDefinitionT DATA_REFS_LT[CNT_DATAREFS_LT] = {
{"livetraffic/channel/real_traffic/sim_time_ctrl",DataRefs::LTGetInt,DataRefs::LTSetCfgValue, GET_VAR, true },
{"livetraffic/channel/real_traffic/man_toffset",DataRefs::LTGetInt,DataRefs::LTSetCfgValue, GET_VAR, true },
{"livetraffic/channel/real_traffic/connect_type",DataRefs::LTGetInt,DataRefs::LTSetCfgValue, GET_VAR, true },
{"livetraffic/channel/real_traffic/set_weather",DataRefs::LTGetInt, DataRefs::LTSetBool, GET_VAR, true, true },
{"livetraffic/channel/real_traffic/set_weather",DataRefs::LTGetInt, DataRefs::LTSetCfgValue, GET_VAR, true, true },
{"livetraffic/channel/fore_flight/listen_port", DataRefs::LTGetInt, DataRefs::LTSetCfgValue, GET_VAR, true },
{"livetraffic/channel/fore_flight/send_port", DataRefs::LTGetInt, DataRefs::LTSetCfgValue, GET_VAR, true },
{"livetraffic/channel/fore_flight/user_plane", DataRefs::LTGetInt, DataRefs::LTSetBool, GET_VAR, true },
Expand Down
2 changes: 1 addition & 1 deletion Src/LTRealTraffic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ bool RealTrafficConnection::ProcessFetchedData ()
}

// If requested to set X-Plane's weather process the detailed weather data
if (WeatherCanSet() && dataRefs.ShallSetRTWeather())
if (WeatherShallSet())
ProcessWeather (json_object_get_object(pObj, "data"));

// Successfully received local pressure information
Expand Down
86 changes: 53 additions & 33 deletions Src/LTWeather.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,39 @@ bool WeatherInit ()
return bWeatherCanSet;
}

// Shutdown Weather module
void WeatherStop ()
{
WeatherReset();
}

// Can we set weather? (X-Plane 12 forward only)
bool WeatherCanSet ()
{
return bWeatherCanSet;
}

// Shall we actuall set the weather as per ability and user's configuration?
bool WeatherShallSet ()
{
bool bRet = false;
if (WeatherCanSet() && dataRefs.GetRTSetWeather() > 0) // can and want?
bRet = dataRefs.GetRTSetWeather() == 2 || // user always wants
wdr_weather_source.get() == 1; // or real weather is on

// So should we be off but aren't?
if (!bRet && WeatherInControl())
{
if (dataRefs.GetRTSetWeather() > 0) { // if user still wants per LiveTraffic's config
weatherOrigSource = -1; // then don't actually touch XP's settings any longer, because the user must have done so already
weatherOrigChangeMode = -1;
}
WeatherReset();
}

return bRet;
}

// Indicate that we are taking control now
void WeatherTakeControl ()
{
Expand All @@ -679,30 +712,6 @@ bool WeatherInControl ()
return bWeatherControlling;
}

/// Return a human readable string on the weather source, is "LiveTraffic" if WeatherInControl()
std::string WeatherGetSource ()
{
// Preset closest to current conditions
static std::array<const char*,10> WEATHER_PRESETS = {
"Clear", "VFR Few", "VFR Scattered", "VFR Broken", "VFR Marginal", "IFR Non-precision", "IFR Precision", "Convective", "Large-cell Storms", "Unknown"
};
int preset = wdr_weather_preset.get();
if (preset < 0 || preset > 8) preset = 9; // 'Unknown'

// Weather Source
static std::array<const char*,5> WEATHER_SOURCES = {
"X-Plane Preset", "X-Plane Real Weather", "Controlpad", "Plugin", "Unknown"
};
int source = wdr_weather_source.get();
if (source < 0 || source > 3) source = 4;

// Are we in control? Say so!
if (WeatherInControl())
return std::string("LiveTraffic using RealTraffic data, ") + WEATHER_PRESETS[size_t(preset)];
else
return std::string(WEATHER_SOURCES[size_t(source)]) + ", " + WEATHER_PRESETS[size_t(preset)];
}

// Reset weather settings to what they were before X-Plane took over
void WeatherReset ()
{
Expand All @@ -723,18 +732,29 @@ void WeatherReset ()
weatherOrigChangeMode = -1;
}

// Shutdown Weather module
void WeatherStop ()
/// Return a human readable string on the weather source, is "LiveTraffic" if WeatherInControl()
std::string WeatherGetSource ()
{
WeatherReset();
}
// Preset closest to current conditions
static std::array<const char*,10> WEATHER_PRESETS = {
"Clear", "VFR Few", "VFR Scattered", "VFR Broken", "VFR Marginal", "IFR Non-precision", "IFR Precision", "Convective", "Large-cell Storms", "Unknown"
};
int preset = wdr_weather_preset.get();
if (preset < 0 || preset > 8) preset = 9; // 'Unknown'

// Can we set weather? (X-Plane 12 forward only)
bool WeatherCanSet ()
{
return bWeatherCanSet;
}
// Weather Source
static std::array<const char*,5> WEATHER_SOURCES = {
"X-Plane Preset", "X-Plane Real Weather", "Controlpad", "Plugin", "Unknown"
};
int source = wdr_weather_source.get();
if (source < 0 || source > 3) source = 4;

// Are we in control? Say so!
if (WeatherInControl())
return std::string("LiveTraffic using RealTraffic data, ") + WEATHER_PRESETS[size_t(preset)];
else
return std::string(WEATHER_SOURCES[size_t(source)]) + ", " + WEATHER_PRESETS[size_t(preset)];
}

/// Is currently an async operation running to fetch METAR?
static std::future<bool> futWeather;
Expand Down
16 changes: 13 additions & 3 deletions Src/SettingsUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,9 +586,19 @@ void LTSettingsUI::buildInterface()
}

// Weather
ImGui::FilteredCfgCheckbox("Set Weather from RealTraffic", sFilter, DR_CFG_RT_SET_WEATHER,
"Set X-Plane's weather based on RealTraffic's weather information (also historically)");

if (ImGui::FilteredLabel("Set Weather from RealTraffic", sFilter, WeatherCanSet())) {
if (WeatherCanSet()) {
const float cbWidth = ImGui::CalcTextSize("Auto (if XP set to real weather)____").x;
ImGui::SetNextItemWidth(cbWidth);
int n = dataRefs.GetRTSetWeather();
if (ImGui::Combo("##RTSetWeather", &n, "Off\0Auto (if XP set to real weather)\0On\0", 3))
DATA_REFS_LT[DR_CFG_RT_SET_WEATHER].setData(n);
} else {
ImGui::TextUnformatted("Setting weather is supported in X-Plane 12 and later");
}
ImGui::TableNextCell();
}

// Historic Data
if (ImGui::FilteredLabel("Request Historic Data", sFilter)) {
float cbWidth = ImGui::CalcTextSize("Use live data, not historic_____").x;
Expand Down

0 comments on commit 942d2c5

Please sign in to comment.