Skip to content

Commit

Permalink
Improve check for empty configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Chi-EEE committed Mar 14, 2024
1 parent c0067b6 commit 6cbba41
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions app/rpi/daemon/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class rpi_daemon : public daemon
host,
car_name,
});
this->any_configuration_empty = host.empty() || car_name.empty();
if (this->any_configuration_empty) {
spdlog::warn("A property in the configuration is empty, this daemon will not run with an empty property.");
}

spdlog::info("Created the Configuration");

spdlog::info("Starting to create the Sub Systems");
Expand All @@ -67,7 +72,7 @@ class rpi_daemon : public daemon
std::unique_ptr<MovementSystem> movement_system = std::make_unique<MovementSystem>(std::make_unique<DummyMovementController>());
// std::unique_ptr<MovementSystem> movement_system = std::make_unique<MovementSystem>(std::make_unique<DeviceMovementController>());
spdlog::info("Created the MovementSystem");

std::unique_ptr<PluginManager> plugin_manager = std::make_unique<PluginManager>(PluginManager());
spdlog::info("Created the PluginManager");

Expand All @@ -92,23 +97,18 @@ class rpi_daemon : public daemon

inline void update()
{
if (this->any_configuration_empty)
{
return;
}

std::chrono::time_point<std::chrono::steady_clock> now = std::chrono::steady_clock::now();
if (!this->car_system->isConnected() && now - this->last_connected >= this->connection_interval)
{
if (!this->attempted_to_reconnect)
{
this->attempted_to_reconnect = true;
auto configuration = this->car_system->getConfiguration();
if (configuration->car_name.empty() || configuration->host.empty())
{
dlog::error("Car name or host is empty, cannot connect to the WS Server.");
return;
}
else
{
dlog::info(fmt::format(R"(Attempting to connect to the WS Server at "{}")", configuration->host));
return;
}
dlog::info(fmt::format(R"(Going to repeatedly attempt to connect to the WS Server "{}" at {} second intervals.)", this->car_system->getConfiguration()->host, this->connection_interval.count()));
}
auto connection_result = this->car_system->tryConnect();
if (!connection_result.has_value())
Expand All @@ -121,10 +121,6 @@ class rpi_daemon : public daemon
}
this->last_connected = now;
}
else
{
this->attempted_to_reconnect = false;
}
this->car_system->update();
}

Expand Down Expand Up @@ -169,8 +165,13 @@ class rpi_daemon : public daemon
host,
car_name,
});
this->any_configuration_empty = host.empty() || car_name.empty();
if (this->any_configuration_empty) {
spdlog::warn("A property in the configuration is empty, this daemon will not run with an empty property.");
}

this->car_system->setConfiguration(std::move(configuration));

this->attempted_to_reconnect = false;

this->car_system->reload();
Expand All @@ -179,7 +180,12 @@ class rpi_daemon : public daemon
private:
std::shared_ptr<CarSystem> car_system;

// To check if any configuration is empty
bool any_configuration_empty = false;

// To print out reconnect message once
bool attempted_to_reconnect = false;

std::chrono::seconds connection_interval = std::chrono::seconds(1);

// This is initialized as 0
Expand Down

0 comments on commit 6cbba41

Please sign in to comment.