Adds signal handling for graceful shutdown via SIGINT and SIGTERM. Updates the control loop to be interruptible, allowing for a clean exit when a shutdown signal is received. Changes the node state management to use an enum for clarity and better control the run states (starting, running, stopping, stopped). The heartbeat is stopped when the node is stopping or stopped. Also updates the line request to force an inactive state when claiming the pin. Also updates the version number.
51 lines
No EOL
1.2 KiB
C++
51 lines
No EOL
1.2 KiB
C++
#include <csignal>
|
|
#include <iostream>
|
|
#include "DixonBrain.h"
|
|
#include "Version.h" // The generated header
|
|
|
|
|
|
|
|
void signal_handler(int signal) {
|
|
if (signal == SIGINT || signal == SIGTERM) {
|
|
std::cout << "\nShutdown signal received (" << signal << ")." << std::endl;
|
|
|
|
// Use your atomic flag to tell the brain to stop looping
|
|
DixonNodeState::instance().setNodeStatus(NodeStatus::Stopping);
|
|
}
|
|
}
|
|
|
|
|
|
int main()
|
|
{
|
|
// The "Splash Screen"
|
|
std::cout << "Starting Dixon v" << DIXON_VERSION << "...\n";
|
|
|
|
std::signal(SIGINT, signal_handler);
|
|
std::signal(SIGTERM, signal_handler);
|
|
|
|
// Create the brain controller
|
|
DixonBrain brain;
|
|
|
|
// Start the brain's control loop
|
|
brain.start();
|
|
|
|
std::cout << "Dixon is alive...\n";
|
|
|
|
auto count = 1;
|
|
while (DixonNodeState::instance().getNodeStatus() == NodeStatus::Running)
|
|
{
|
|
|
|
count++;
|
|
if (count>10)
|
|
{
|
|
count = 0;
|
|
std::cout << "Dixon state is " << static_cast<int>( DixonNodeState::instance().getNodeStatus()) << "\n";
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
}
|
|
// Stop the brain cleanly
|
|
brain.stop();
|
|
std::cout << "Dixon has left the building...\n";
|
|
return 0;
|
|
} |