Line data Source code
1 : /*! 2 : * @file SystemManager.cpp 3 : * @brief Implementation of the SystemManager class. 4 : * @version 0.1 5 : * @date 2025-01-31 6 : * @details This file contains the implementation of the SystemManager class, 7 : * which is used to manage the system status. 8 : * @note This class is used to manage the system status, including the time, 9 : * WiFi, temperature, battery, and IP address. 10 : * @author Félix LE BIHAN (@Fle-bihh) 11 : * @author Tiago Pereira (@t-pereira06) 12 : * @author Ricardo Melo (@reomelo) 13 : * @author Michel Batista (@MicchelFAB) 14 : * @warning Ensure that the WiFi interface is properly configured and the 15 : * temperature sensor is connected. 16 : * @see SystemManager.hpp for the class definition. 17 : * @copyright Copyright (c) 2025 18 : */ 19 : 20 : #include "SystemManager.hpp" 21 : #include <QDateTime> 22 : #include <QDebug> 23 : #include <QLocale> 24 : #include "BatteryController.hpp" 25 : #include "SystemCommandExecutor.hpp" 26 : #include "SystemInfoProvider.hpp" 27 : 28 : /*! 29 : * @brief Constructor for the SystemManager class. 30 : * @details Allocates a BatteryController, SystemInfoProvider, and 31 : * SystemCommandExecutor if the ones provided are nullptr. 32 : * @param batteryController The battery controller to use. If nullptr, a new 33 : * BatteryController is allocated. 34 : * @param systemInfoProvider The system info provider to use. If nullptr, a new 35 : * SystemInfoProvider is allocated. 36 : * @param systemCommandExecutor The system command executor to use. If nullptr, 37 : * a new SystemCommandExecutor is allocated. 38 : * @param parent The parent object of this SystemManager. 39 : */ 40 4 : SystemManager::SystemManager(IBatteryController *batteryController, 41 : ISystemInfoProvider *systemInfoProvider, 42 : ISystemCommandExecutor *systemCommandExecutor, 43 4 : QObject *parent) 44 : : QObject(parent) 45 4 : , m_batteryController(batteryController ? batteryController : new BatteryController()) 46 4 : , m_systemInfoProvider(systemInfoProvider ? systemInfoProvider : new SystemInfoProvider()) 47 4 : , m_systemCommandExecutor(systemCommandExecutor ? systemCommandExecutor 48 4 : : new SystemCommandExecutor()) 49 4 : , m_ownBatteryController(batteryController == nullptr) 50 4 : , m_ownSystemInfoProvider(systemInfoProvider == nullptr) 51 12 : , m_ownSystemCommandExecutor(systemCommandExecutor == nullptr) 52 4 : {} 53 : 54 : /*! 55 : * @brief Destructor for the SystemManager class. 56 : * @details Calls shutdown() to stop all threads and then deletes the 57 : * BatteryController, SystemInfoProvider, and SystemCommandExecutor objects if 58 : * they were allocated by the SystemManager. 59 : */ 60 8 : SystemManager::~SystemManager() 61 : { 62 4 : shutdown(); 63 4 : if (m_ownBatteryController) 64 0 : delete m_batteryController; 65 4 : if (m_ownSystemInfoProvider) 66 0 : delete m_systemInfoProvider; 67 4 : if (m_ownSystemCommandExecutor) 68 4 : delete m_systemCommandExecutor; 69 8 : } 70 : 71 : /*! 72 : * @brief Initializes the SystemManager object. 73 : * @details This method initializes the SystemManager object by starting two 74 : * timers: one to update the time every second and another to update the system 75 : * status every 5 seconds. It also calls updateSystemStatus() to update the 76 : * system status immediately. 77 : */ 78 0 : void SystemManager::initialize() 79 : { 80 0 : connect(&m_timeTimer, &QTimer::timeout, this, &SystemManager::updateTime); 81 0 : connect(&m_statusTimer, &QTimer::timeout, this, &SystemManager::updateSystemStatus); 82 0 : m_timeTimer.start(1000); 83 0 : m_statusTimer.start(5000); 84 0 : updateSystemStatus(); 85 0 : } 86 : 87 : 88 : /*! 89 : * @brief Shuts down the SystemManager object. 90 : * @details This method stops the time and status timers to halt periodic updates. 91 : */ 92 4 : void SystemManager::shutdown() 93 : { 94 4 : m_timeTimer.stop(); 95 4 : m_statusTimer.stop(); 96 4 : } 97 : 98 : /*! 99 : * @brief Updates the current time. 100 : * @details This function retrieves the current date and time and emits the 101 : * timeUpdated signal with the formatted date, time, and weekday. 102 : */ 103 1 : void SystemManager::updateTime() 104 : { 105 2 : QDateTime currentDateTime = QDateTime::currentDateTime(); 106 2 : QLocale english(QLocale::English); 107 2 : QString currentMonth = english.toString(currentDateTime, "MMMM").left(3); 108 : 109 1 : emit timeUpdated(currentMonth, 110 2 : currentDateTime.toString("HH:mm"), 111 2 : currentDateTime.toString("d")); 112 1 : } 113 : 114 : /*! 115 : * @brief Updates the system status. 116 : * @details This function updates the system status by calling the getters on the 117 : 118 : * SystemInfoProvider and BatteryController objects and emitting the corresponding 119 : * signals. It does not block and is intended to be called regularly. 120 : */ 121 3 : void SystemManager::updateSystemStatus() 122 : { 123 6 : QString wifiName; 124 3 : emit wifiStatusUpdated(m_systemInfoProvider->getWifiStatus(wifiName), wifiName); 125 3 : emit temperatureUpdated(m_systemInfoProvider->getTemperature()); 126 3 : emit batteryPercentageUpdated(m_batteryController->getBatteryPercentage()); 127 3 : }