| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "IEventBus.h" | ||
| 4 | #include "IGate.h" | ||
| 5 | #include "ITicketService.h" | ||
| 6 | #include "freertos/FreeRTOS.h" | ||
| 7 | #include "freertos/timers.h" | ||
| 8 | #include <memory> | ||
| 9 | |||
| 10 | /** | ||
| 11 | * @brief Exit gate state machine states | ||
| 12 | */ | ||
| 13 | enum class ExitGateState { | ||
| 14 | Idle, | ||
| 15 | ValidatingTicket, | ||
| 16 | OpeningBarrier, | ||
| 17 | WaitingForCarToPass, | ||
| 18 | CarPassing, | ||
| 19 | WaitingBeforeClose, | ||
| 20 | ClosingBarrier | ||
| 21 | }; | ||
| 22 | |||
| 23 | /** | ||
| 24 | * @brief Exit gate controller with state machine | ||
| 25 | * | ||
| 26 | * Handles exit sequence: | ||
| 27 | * 1. Car arrives (detected via IGate) | ||
| 28 | * 2. Validate ticket (simulated - assumes paid) | ||
| 29 | * 3. Open barrier via IGate interface | ||
| 30 | * 4. Wait for car to pass through | ||
| 31 | * 5. Close barrier via IGate interface | ||
| 32 | * | ||
| 33 | * Uses pure Dependency Injection - all dependencies are injected via constructor. | ||
| 34 | */ | ||
| 35 | class ExitGateController { | ||
| 36 | public: | ||
| 37 | /** | ||
| 38 | * @brief Construct exit gate controller with injected dependencies | ||
| 39 | * @param eventBus Event bus for publishing/subscribing | ||
| 40 | * @param gate Gate abstraction (barrier + light barrier) | ||
| 41 | * @param ticketService Ticket service | ||
| 42 | * @param barrierTimeoutMs Barrier open/close timeout in ms | ||
| 43 | * @param validationTimeMs Ticket validation simulation time in ms | ||
| 44 | */ | ||
| 45 | ExitGateController( | ||
| 46 | IEventBus& eventBus, | ||
| 47 | IGate& gate, | ||
| 48 | ITicketService& ticketService, | ||
| 49 | uint32_t barrierTimeoutMs = 2000, | ||
| 50 | uint32_t validationTimeMs = 500); | ||
| 51 | |||
| 52 | ~ExitGateController(); | ||
| 53 | |||
| 54 | // Prevent copying | ||
| 55 | ExitGateController(const ExitGateController&) = delete; | ||
| 56 | ExitGateController& operator=(const ExitGateController&) = delete; | ||
| 57 | |||
| 58 | /** | ||
| 59 | * @brief Get current state | ||
| 60 | */ | ||
| 61 | 14 | [[nodiscard]] ExitGateState getState() const { return m_state; } | |
| 62 | |||
| 63 | /** | ||
| 64 | * @brief Get state as string | ||
| 65 | */ | ||
| 66 | [[nodiscard]] const char* getStateString() const; | ||
| 67 | |||
| 68 | /** | ||
| 69 | * @brief Get gate reference (for debugging/console commands) | ||
| 70 | */ | ||
| 71 | [[nodiscard]] IGate& getGate() { return *m_gate; } | ||
| 72 | |||
| 73 | /** | ||
| 74 | * @brief Manually validate ticket (for console commands) | ||
| 75 | * @param ticketId Ticket ID to validate | ||
| 76 | * @return true if validation successful | ||
| 77 | */ | ||
| 78 | bool validateTicketManually(uint32_t ticketId); | ||
| 79 | |||
| 80 | /** | ||
| 81 | * @brief Setup GPIO interrupts | ||
| 82 | * Call this after construction to enable hardware interrupts | ||
| 83 | */ | ||
| 84 | void setupGpioInterrupts(); | ||
| 85 | |||
| 86 | /** | ||
| 87 | * @brief Reset controller to initial state | ||
| 88 | * Stops timers, resets state to Idle, clears current ticket, closes barrier | ||
| 89 | */ | ||
| 90 | void reset(); | ||
| 91 | |||
| 92 | #ifdef UNIT_TEST | ||
| 93 | // Test helpers to simulate timer expirations without FreeRTOS timers | ||
| 94 | 3 | void TEST_forceBarrierTimeout() { | |
| 95 | 3 | onBarrierTimeout(); | |
| 96 | 3 | } | |
| 97 | void TEST_forceValidationTimeout() { | ||
| 98 | onValidationTimeout(); | ||
| 99 | } | ||
| 100 | #endif | ||
| 101 | |||
| 102 | private: | ||
| 103 | void onLightBarrierBlocked(const Event& event); | ||
| 104 | void onLightBarrierCleared(const Event& event); | ||
| 105 | void onBarrierTimeout(); | ||
| 106 | void onValidationTimeout(); | ||
| 107 | |||
| 108 | void setState(ExitGateState newState); | ||
| 109 | void startBarrierTimer(); | ||
| 110 | void stopBarrierTimer(); | ||
| 111 | void startValidationTimer(); | ||
| 112 | void stopValidationTimer(); | ||
| 113 | |||
| 114 | static void barrierTimerCallback(TimerHandle_t xTimer); | ||
| 115 | static void validationTimerCallback(TimerHandle_t xTimer); | ||
| 116 | |||
| 117 | IEventBus& m_eventBus; | ||
| 118 | IGate* m_gate; | ||
| 119 | ITicketService& m_ticketService; | ||
| 120 | |||
| 121 | ExitGateState m_state; | ||
| 122 | uint32_t m_barrierTimeoutMs; | ||
| 123 | uint32_t m_validationTimeMs; | ||
| 124 | uint32_t m_currentTicketId; | ||
| 125 | TimerHandle_t m_barrierTimer; | ||
| 126 | TimerHandle_t m_validationTimer; | ||
| 127 | }; | ||
| 128 |