Skip to content

Commit

Permalink
refactor: đź’ˇ Removed the demo code from main and into its own
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeSixth committed Oct 28, 2024
1 parent 330ba4a commit b297503
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 85 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ add_library(railway_signaling
src/RailwaySignal.cpp
src/SignalState.cpp
src/SignalCommand.cpp
src/SignalDemo.cpp
)

target_include_directories(railway_signaling PUBLIC
Expand Down
39 changes: 39 additions & 0 deletions include/SignalDemo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
#include <memory>
#include <thread>
#include <chrono>

#include "RailwaySignal.hpp"
#include "RailwayTypes.hpp"
#include "SignalCommand.hpp"

/**
* @brief Programme principal de démonstration
* @safety_level SIL4
* @requirement REQ_DEMO_001
*/
class SignalDemo {
public:
SignalDemo();
~SignalDemo() = default;

void run();

private:
static std::string aspectToString(Railway::Aspect aspect) {
switch (aspect) {
using enum Railway::Aspect;

case RED:
return "RED";
case YELLOW:
return "YELLOW";
case GREEN:
return "GREEN";
default:
return "UNKNOWN";
}
}

Railway::RailwaySignal signal_{"SIGNAL_DEMO_001"};
};
51 changes: 51 additions & 0 deletions src/SignalDemo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "SignalDemo.hpp"

using namespace std::chrono_literals;

SignalDemo::SignalDemo() {
// Configuration du handler pour les changements d'aspect
signal_.aspectChanged.connect([](Railway::Aspect aspect) {
std::cout << "Signal aspect changed to: " << aspectToString(aspect) << std::endl;
});
}

void SignalDemo::run() {
std::cout << "Starting Railway Signal Demo\nInitial aspect: " << aspectToString(signal_.getAspect()) << "\n";

// Démonstration des commandes et de leur réversibilité
try {
std::cout << "\nDémonstration des commandes et de leur réversibilité:\n";

// Transition vers jaune
std::cout << "\nChanging to YELLOW...\n";
auto yellowCmd = std::make_unique<Railway::ChangeAspectCommand>(signal_, Railway::Aspect::YELLOW);
signal_.executeCommand(std::move(yellowCmd));
std::this_thread::sleep_for(2s);

// Transition vers vert
std::cout << "\nChanging to GREEN...\n";
auto greenCmd = std::make_unique<Railway::ChangeAspectCommand>(signal_, Railway::Aspect::GREEN);
signal_.executeCommand(std::move(greenCmd));
std::this_thread::sleep_for(2s);

// DĂ©monstration de l'annulation
std::cout << "\nUndoing last command (should return to YELLOW)...\n";
signal_.undoLastCommand();
std::this_thread::sleep_for(2s);

// Démonstration de la réexécution
std::cout << "\nRedoing command (should return to GREEN)...\n";
signal_.redoCommand();
std::this_thread::sleep_for(2s);

// Retour à rouge pour la sécurité
std::cout << "\nReturning to RED (safety state)...\n";
auto redCmd = std::make_unique<Railway::ChangeAspectCommand>(signal_, Railway::Aspect::RED);
signal_.executeCommand(std::move(redCmd));

} catch (const std::exception& e) {
std::cerr << "Error during demo: " << e.what() << std::endl;
// En cas d'erreur, on s'assure de revenir à l'état sûr (rouge)
signal_.setAspect(Railway::Aspect::RED);
}
}
86 changes: 1 addition & 85 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,93 +1,9 @@
#include <boost/asio.hpp>
#include <chrono>
#include <iostream>
#include <thread>

#include "RailwaySignal.hpp"
#include "RailwayTypes.hpp"
#include "SignalCommand.hpp"

using namespace Railway;
using namespace std::chrono_literals;

/**
* @brief Programme principal de démonstration
* @safety_level SIL4
* @requirement REQ_DEMO_001
*/
class SignalDemo {
public:
SignalDemo() : signal_("SIGNAL_DEMO_001") {
// Configuration du handler pour les changements d'aspect
signal_.aspectChanged.connect([](Aspect aspect) {
std::cout << "Signal aspect changed to: "
<< aspectToString(aspect) << std::endl;
});
}

void run() {
std::cout << "Starting Railway Signal Demo\n";
std::cout << "Initial aspect: " << aspectToString(signal_.getAspect()) << "\n";

// Démonstration des commandes et de leur réversibilité
try {
std::cout << "\nDémonstration des commandes et de leur réversibilité:\n";

// Transition vers jaune
std::cout << "\nChanging to YELLOW...\n";
auto yellowCmd = std::make_unique<ChangeAspectCommand>(
signal_, Aspect::YELLOW);
signal_.executeCommand(std::move(yellowCmd));
std::this_thread::sleep_for(2s);

// Transition vers vert
std::cout << "\nChanging to GREEN...\n";
auto greenCmd = std::make_unique<ChangeAspectCommand>(
signal_, Aspect::GREEN);
signal_.executeCommand(std::move(greenCmd));
std::this_thread::sleep_for(2s);

// DĂ©monstration de l'annulation
std::cout << "\nUndoing last command (should return to YELLOW)...\n";
signal_.undoLastCommand();
std::this_thread::sleep_for(2s);

// Démonstration de la réexécution
std::cout << "\nRedoing command (should return to GREEN)...\n";
signal_.redoCommand();
std::this_thread::sleep_for(2s);

// Retour à rouge pour la sécurité
std::cout << "\nReturning to RED (safety state)...\n";
auto redCmd = std::make_unique<ChangeAspectCommand>(
signal_, Aspect::RED);
signal_.executeCommand(std::move(redCmd));

} catch (const std::exception& e) {
std::cerr << "Error during demo: " << e.what() << std::endl;
// En cas d'erreur, on s'assure de revenir à l'état sûr (rouge)
signal_.setAspect(Aspect::RED);
}
}

private:
static std::string aspectToString(Aspect aspect) {
switch (aspect) {
using enum Railway::Aspect;

case RED:
return "RED";
case YELLOW:
return "YELLOW";
case Aspect::GREEN:
return "GREEN";
default:
return "UNKNOWN";
}
}

RailwaySignal signal_;
};
#include "SignalDemo.hpp"

/**
* @brief Point d'entrée du programme
Expand Down

0 comments on commit b297503

Please sign in to comment.