LCOV - code coverage report
Current view: top level - mileage - MileageManager.cpp (source / functions) Hit Total Coverage
Test: filtered.info Lines: 60 62 96.8 %
Date: 2025-07-25 11:48:17 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /*!
       2             :  * @file MileageManager.cpp
       3             :  * @brief Implementation of the MileageManager class.
       4             :  * @version 0.1
       5             :  * @date 2025-01-31
       6             :  * @details This file contains the implementation of the MileageManager class,
       7             :  * which is used to manage the mileage of the vehicle.
       8             :  * @note This class is used to manage the mileage of the vehicle.
       9             :  * @author Félix LE BIHAN (@Fle-bihh)
      10             :  * @author Tiago Pereira (@t-pereira06)
      11             :  * @author Ricardo Melo (@reomelo)
      12             :  * @author Michel Batista (@MicchelFAB)
      13             :  * @warning Ensure that the file path is valid and accessible.
      14             :  * @see MileageManager.hpp
      15             :  * @copyright Copyright (c) 2025
      16             :  */
      17             : 
      18             : #include "MileageManager.hpp"
      19             : #include <QDebug>
      20             : #include "MileageCalculator.hpp"
      21             : #include "MileageFileHandler.hpp"
      22             : 
      23             : /*!
      24             :  * @brief Constructs a MileageManager object with the specified file path, calculator, and file handler.
      25             :  * @param filePath The path to the mileage file.
      26             :  * @param calculator The mileage calculator to use.
      27             :  * @param fileHandler The file handler to use.
      28             :  * @param parent The parent object.
      29             :  *
      30             :  * @details This constructor initializes the MileageManager object with the specified
      31             :  * file path, calculator, and file handler. It also sets the total mileage to 0.0.
      32             :  */
      33           4 : MileageManager::MileageManager(const QString &filePath,
      34             :                                                            IMileageCalculator *calculator,
      35             :                                                            IMileageFileHandler *fileHandler,
      36           4 :                                                            QObject *parent)
      37             :         : QObject(parent)
      38           4 :     , m_manager(new QNetworkAccessManager(this))
      39           4 :     , m_calculator(calculator ? calculator : new MileageCalculator())
      40           8 :     , m_fileHandler(fileHandler ? fileHandler : new MileageFileHandler(filePath))
      41           4 :     , m_ownCalculator(calculator == nullptr)
      42           4 :     , m_ownFileHandler(fileHandler == nullptr)
      43          20 :     , m_totalMileage(0.0)
      44           4 : {}
      45             : 
      46             : /*!
      47             :  * @brief Destructs the MileageManager object.
      48             :  *
      49             :  * @details This destructor calls the shutdown method to stop the timers and
      50             :  * saves the mileage to the file. It also deletes the calculator and file handler
      51             :  * if they were created internally.
      52             :  */
      53           8 : MileageManager::~MileageManager()
      54             : {
      55           4 :         shutdown();
      56             : 
      57             :         // Only delete instances if they were created internally
      58           4 :         if (m_ownCalculator) {
      59           0 :                 delete m_calculator;
      60             :         }
      61           4 :         if (m_ownFileHandler) {
      62           0 :                 delete m_fileHandler;
      63             :         }
      64           8 : }
      65             : 
      66             : /*!
      67             :  * @brief Initializes the MileageManager object.
      68             :  *
      69             :  * @details This method initializes the MileageManager object by reading the
      70             :  * mileage from the file and starting the update and persistence timers.
      71             :  */
      72           1 : void MileageManager::initialize()
      73             : {
      74           1 :         m_totalMileage = m_fileHandler->readMileage();
      75             : 
      76           2 :         QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
      77           3 :         QString apiBaseUrl = env.value("API_KEY");
      78             : 
      79           2 :         QUrl baseUrl(apiBaseUrl);
      80           3 :         QUrl fullUrl = baseUrl.resolved(QUrl("/mileage"));
      81             : 
      82           2 :         QJsonObject json;
      83           1 :         json["mileage"] = static_cast<int>(m_totalMileage);
      84             : 
      85           2 :         QJsonDocument doc(json);
      86           2 :         QByteArray jsonData = doc.toJson();
      87             : 
      88           2 :         QNetworkRequest request(fullUrl);
      89           1 :         request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
      90             : 
      91           1 :         m_manager->post(request,jsonData);
      92             : 
      93           1 :         connect(&m_updateTimer, &QTimer::timeout, this, &MileageManager::updateMileage);
      94           1 :         m_updateTimer.start(1000);
      95             : 
      96           1 :         connect(&m_persistenceTimer, &QTimer::timeout, this, &MileageManager::saveMileage);
      97           1 :         m_persistenceTimer.start(10000);
      98           1 : }
      99             : 
     100             : /*!
     101             :  * @brief Shuts down the MileageManager object.
     102             :  *
     103             :  * @details This method stops the update and persistence timers and saves the
     104             :  * mileage to the file.
     105             :  */
     106           4 : void MileageManager::shutdown()
     107             : {
     108           4 :         saveMileage();
     109           4 :         m_updateTimer.stop();
     110           4 :         m_persistenceTimer.stop();
     111           4 : }
     112             : 
     113             : /*!
     114             :  * @brief Updates the mileage.
     115             :  *
     116             :  * @details This method updates the mileage by calculating the distance traveled
     117             :  * since the last update and adding it to the total mileage.
     118             :  */
     119           3 : void MileageManager::updateMileage()
     120             : {
     121           3 :         double distance = m_calculator->calculateDistance();
     122           3 :         m_totalMileage += distance;
     123           3 :         emit mileageUpdated(m_totalMileage);
     124             : 
     125           3 :         if (distance > 1.0) {
     126           6 :                 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
     127           9 :                 QString apiBaseUrl = env.value("API_KEY");
     128             : 
     129           6 :                 QUrl baseUrl(apiBaseUrl);
     130           9 :                 QUrl fullUrl = baseUrl.resolved(QUrl("/mileage"));
     131             : 
     132           6 :                 QJsonObject json;
     133           3 :                 json["mileage"] = static_cast<int>(m_totalMileage);
     134             : 
     135           6 :                 QJsonDocument doc(json);
     136           6 :                 QByteArray jsonData = doc.toJson();
     137             : 
     138           6 :                 QNetworkRequest request(fullUrl);
     139           3 :                 request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
     140             : 
     141           3 :                 m_manager->post(request,jsonData);
     142             :         }
     143           3 : }
     144             : 
     145             : /*!
     146             :  * @brief Saves the mileage to the file.
     147             :  *
     148             :  * @details This method saves the total mileage to the file using the file handler.
     149             :  */
     150           6 : void MileageManager::saveMileage()
     151             : {
     152           6 :         m_fileHandler->writeMileage(m_totalMileage);
     153           6 : }
     154             : 
     155             : /*!
     156             :  * @brief Handles the speed updated signal.
     157             :  * @param speed The new speed value.
     158             :  *
     159             :  * @details This method handles the speed updated signal by adding the speed value
     160             :  * to the calculator.
     161             :  */
     162           1 : void MileageManager::onSpeedUpdated(float speed)
     163             : {
     164           1 :         m_calculator->addSpeed(speed);
     165           1 : }

Generated by: LCOV version 1.14