Skip to content

Commit

Permalink
Added toggle between reported and ES calculated ground speed
Browse files Browse the repository at this point in the history
Removed default weather update URL and added warning if no URL was specified
  • Loading branch information
MorpheusXAUT committed Nov 21, 2022
1 parent c3b9dbb commit 54cfcac
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 31 deletions.
79 changes: 50 additions & 29 deletions IASsure/IASsure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ IASsure::IASsure::IASsure() :
weatherUpdateInterval(5),
loginState(0),
weatherUpdater(nullptr),
weatherUpdateURL(DEFAULT_WEATHER_UPDATE_URL)
useReportedGS(true)
{
std::ostringstream msg;
msg << "Version " << PLUGIN_VERSION << " loaded.";
Expand All @@ -21,17 +21,13 @@ IASsure::IASsure::IASsure() :

this->RegisterTagItems();

this->LoadSettings();
this->TryLoadConfigFile();
this->LoadSettings();
}

IASsure::IASsure::~IASsure()
{
if (this->weatherUpdater != nullptr) {
this->weatherUpdater->stop();
delete this->weatherUpdater;
this->weatherUpdater = nullptr;
}
this->StopWeatherUpdater();
}

bool IASsure::IASsure::OnCompileCommand(const char* sCommandLine)
Expand All @@ -41,7 +37,7 @@ bool IASsure::IASsure::OnCompileCommand(const char* sCommandLine)
if (args[0] == ".ias") {
if (args.size() == 1) {
std::ostringstream msg;
msg << "Version " << PLUGIN_VERSION << " loaded. Available commands: debug, reset, weather";
msg << "Version " << PLUGIN_VERSION << " loaded. Available commands: debug, reset, weather, gs";

this->LogMessage(msg.str());
return true;
Expand Down Expand Up @@ -140,6 +136,19 @@ bool IASsure::IASsure::OnCompileCommand(const char* sCommandLine)
return true;
}
}
else if (args[1] == "gs") {
if (this->useReportedGS) {
this->LogMessage("Switched to using ground speed estimated by EuroScope for CAS/Mach calculations", "Config");
}
else {
this->LogMessage("Switched to using ground speed reported by pilot client for CAS/Mach calculations", "Config");
}

this->useReportedGS = !this->useReportedGS;

this->SaveSettings();
return true;
}
}

return false;
Expand Down Expand Up @@ -193,7 +202,7 @@ void IASsure::IASsure::OnFunctionCall(int FunctionId, const char* sItemString, P
return;
}

int ias = rt.GetPosition().GetReportedGS();
int ias = this->useReportedGS ? rt.GetPosition().GetReportedGS() : rt.GetGS();
double calculatedIAS = this->CalculateIAS(rt);
if (calculatedIAS >= 0) {
ias = ::IASsure::roundToNearest(calculatedIAS, INTERVAL_REPORTED_IAS);
Expand Down Expand Up @@ -328,7 +337,7 @@ double IASsure::IASsure::CalculateIAS(const EuroScopePlugIn::CRadarTarget& rt)
}

int hdg = rt.GetPosition().GetReportedHeading(); // heading in degrees
int gs = rt.GetPosition().GetReportedGS(); // ground speed in knots
int gs = this->useReportedGS ? rt.GetPosition().GetReportedGS() : rt.GetGS(); // ground speed in knots
int alt = rt.GetPosition().GetPressureAltitude(); // altitude in feet

WeatherReferenceLevel level = this->weather.findClosest(rt.GetPosition().GetPosition().m_Latitude, rt.GetPosition().GetPosition().m_Longitude, alt);
Expand Down Expand Up @@ -429,7 +438,7 @@ double IASsure::IASsure::CalculateMach(const EuroScopePlugIn::CRadarTarget& rt)
}

int hdg = rt.GetPosition().GetReportedHeading(); // heading in degrees
int gs = rt.GetPosition().GetReportedGS(); // ground speed in knots
int gs = this->useReportedGS ? rt.GetPosition().GetReportedGS() : rt.GetGS(); // ground speed in knots
int alt = rt.GetPosition().GetPressureAltitude(); // altitude in feet

WeatherReferenceLevel level = this->weather.findClosest(rt.GetPosition().GetPosition().m_Latitude, rt.GetPosition().GetPosition().m_Longitude, alt);
Expand Down Expand Up @@ -490,17 +499,11 @@ void IASsure::IASsure::CheckLoginState()
case EuroScopePlugIn::CONNECTION_TYPE_NO:
case EuroScopePlugIn::CONNECTION_TYPE_PLAYBACK:
// user just disconnected, stop weather updater if it's running
if (this->weatherUpdater != nullptr) {
this->weatherUpdater->stop();
delete this->weatherUpdater;
this->weatherUpdater = nullptr;
}
this->StopWeatherUpdater();
break;
default:
// user just connected, start weather update if it's not running yet
if (this->weatherUpdater == nullptr && this->weatherUpdateInterval.count() > 0) {
this->weatherUpdater = new ::IASsure::thread::PeriodicAction(std::chrono::milliseconds(0), std::chrono::milliseconds(this->weatherUpdateInterval), std::bind(&IASsure::UpdateWeather, this));
}
this->StartWeatherUpdater();
}
}

Expand Down Expand Up @@ -529,17 +532,31 @@ void IASsure::IASsure::UpdateWeather()
this->LogDebugMessage("Successfully updated weather data", "Weather");
}

void IASsure::IASsure::ResetWeatherUpdater()
void IASsure::IASsure::StartWeatherUpdater()
{
if (this->weatherUpdateURL.empty() && this->weatherUpdateInterval.count() > 0) {
this->LogMessage("Weather update URL is empty, cannot fetch weather data for calculations. Configure via config file (config.json in same directory as IASsure.dll) or .ias weather url <URL>.", "Config");
return;
}

if (this->weatherUpdater == nullptr && this->weatherUpdateInterval.count() > 0) {
this->weatherUpdater = new ::IASsure::thread::PeriodicAction(std::chrono::milliseconds(0), std::chrono::milliseconds(this->weatherUpdateInterval), std::bind(&IASsure::UpdateWeather, this));
}
}

void IASsure::IASsure::StopWeatherUpdater()
{
if (this->weatherUpdater != nullptr) {
this->weatherUpdater->stop();
delete this->weatherUpdater;
this->weatherUpdater = nullptr;
}
}

if (this->weatherUpdateInterval.count() > 0) {
this->weatherUpdater = new ::IASsure::thread::PeriodicAction(std::chrono::milliseconds(0), std::chrono::milliseconds(this->weatherUpdateInterval), std::bind(&IASsure::UpdateWeather, this));
}
void IASsure::IASsure::ResetWeatherUpdater()
{
this->StopWeatherUpdater();
this->StartWeatherUpdater();
}

void IASsure::IASsure::LoadSettings()
Expand All @@ -548,7 +565,7 @@ void IASsure::IASsure::LoadSettings()
if (settings) {
std::vector<std::string> splitSettings = ::IASsure::split(settings, SETTINGS_DELIMITER);

if (splitSettings.size() < 3) {
if (splitSettings.size() < 4) {
this->LogMessage("Invalid saved settings found, reverting to default.");

this->SaveSettings();
Expand All @@ -559,9 +576,12 @@ void IASsure::IASsure::LoadSettings()
int weatherUpdateMin;
std::istringstream(splitSettings[1]) >> weatherUpdateMin;
this->weatherUpdateInterval = std::chrono::minutes(weatherUpdateMin);
this->weatherUpdateURL = splitSettings[2];
if (!splitSettings[2].empty()) {
this->weatherUpdateURL = splitSettings[2];
}
std::istringstream(splitSettings[3]) >> this->useReportedGS;

this->LogDebugMessage("Successfully loaded settings.");
this->LogDebugMessage("Successfully loaded settings.", "Config");
}
else {
this->LogMessage("No saved settings found, using defaults.");
Expand All @@ -573,14 +593,15 @@ void IASsure::IASsure::SaveSettings()
std::ostringstream ss;
ss << this->debug << SETTINGS_DELIMITER
<< this->weatherUpdateInterval.count() << SETTINGS_DELIMITER
<< this->weatherUpdateURL;
<< this->weatherUpdateURL << SETTINGS_DELIMITER
<< this->useReportedGS;

this->SaveDataToSettings(PLUGIN_NAME, "Settings", ss.str().c_str());
}

void IASsure::IASsure::TryLoadConfigFile()
{
this->LogDebugMessage("Attempting to load config file");
this->LogDebugMessage("Attempting to load config file", "Config");

nlohmann::json cfg;
try {
Expand Down Expand Up @@ -610,7 +631,7 @@ void IASsure::IASsure::TryLoadConfigFile()
this->LogDebugMessage("Failed to parse weather section of config file, might not exist. Ignoring", "Config");
}

this->LogDebugMessage("Successfully loaded config file");
this->LogDebugMessage("Successfully loaded config file", "Config");
}

void IASsure::IASsure::LogMessage(std::string message)
Expand Down
3 changes: 3 additions & 0 deletions IASsure/IASsure.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace IASsure {
bool debug;
std::chrono::minutes weatherUpdateInterval;
std::string weatherUpdateURL;
bool useReportedGS;

std::unordered_map<std::string, int> reportedIAS;
std::unordered_set<std::string> calculatedIASToggled;
Expand Down Expand Up @@ -63,6 +64,8 @@ namespace IASsure {

void CheckLoginState();
void UpdateWeather();
void StartWeatherUpdater();
void StopWeatherUpdater();
void ResetWeatherUpdater();

void LoadSettings();
Expand Down
2 changes: 0 additions & 2 deletions IASsure/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ constexpr auto PLUGIN_LICENSE = "(c) 2022, MIT License";

constexpr auto SETTINGS_DELIMITER = '|';

constexpr auto DEFAULT_WEATHER_UPDATE_URL = "https://weather.morpheusxaut.net/weather.json";

const int TAG_ITEM_CALCULATED_IAS = 1;
const int TAG_ITEM_CALCULATED_IAS_TOGGLABLE = 2;
const int TAG_ITEM_CALCULATED_IAS_ABBREVIATED = 3;
Expand Down

0 comments on commit 54cfcac

Please sign in to comment.