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

          Line data    Source code
       1             : /*!
       2             :  * @file SystemDataManager.cpp
       3             :  * @brief
       4             :  * @version 0.1
       5             :  * @date 2025-02-12
       6             :  * @details
       7             :  * @author Félix LE BIHAN (@Fle-bihh)
       8             :  * @author Tiago Pereira (@t-pereira06)
       9             :  * @author Ricardo Melo (@reomelo)
      10             :  * @author Michel Batista (@MicchelFAB)
      11             :  *
      12             :  * @copyright Copyright (c) 2025
      13             :  */
      14             : 
      15             : #include "SystemDataManager.hpp"
      16             : #include <QtMath>
      17             : 
      18           5 : SystemDataManager::SystemDataManager(QObject *parent)
      19             :         : QObject(parent)
      20           5 :     , m_manager(new QNetworkAccessManager(this))
      21           5 : {}
      22             : 
      23          10 : SystemDataManager::~SystemDataManager() {}
      24             : 
      25             : /*!
      26             :  * @brief Handle Time data.
      27             :  * @param currentDate The current date.
      28             :  * @param currentTime The current time.
      29             :  * @param currentDay The current day.
      30             :  * @details This function processes the time data.
      31             :  */
      32           1 : void SystemDataManager::handleTimeData(const QString &currentMonth,
      33             :                                                                            const QString &currentTime,
      34             :                                                                            const QString &currentDay)
      35             : {
      36           1 :         m_time = currentTime;
      37           1 :         emit systemTimeUpdated(currentMonth, currentTime, currentDay);
      38           1 : }
      39             : 
      40             : /*!
      41             :  * @brief Handle WiFi data.
      42             :  * @param status The WiFi status.
      43             :  * @param wifiName The WiFi name.
      44             :  * @details This function processes the WiFi data.
      45             :  */
      46           3 : void SystemDataManager::handleWifiData(const QString &status, const QString &wifiName)
      47             : {
      48           3 :         if (m_wifiStatus != status || m_wifiName != wifiName) {
      49           2 :                 m_wifiStatus = status;
      50           2 :                 m_wifiName = wifiName;
      51             : 
      52           4 :                 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
      53           6 :                 QString apiBaseUrl = env.value("API_KEY");
      54             : 
      55           4 :                 QUrl baseUrl(apiBaseUrl);
      56           6 :                 QUrl fullUrl = baseUrl.resolved(QUrl("/wifi"));
      57             : 
      58           4 :                 QJsonObject json;
      59           2 :                 json["wifi"] = wifiName;
      60             : 
      61           4 :                 QJsonDocument doc(json);
      62           4 :                 QByteArray jsonData = doc.toJson();
      63             : 
      64           4 :                 QNetworkRequest request(fullUrl);
      65           2 :                 request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
      66             : 
      67           2 :                 m_manager->post(request,jsonData);
      68             :         }
      69           3 : }
      70             : 
      71             : /*!
      72             :  * @brief Handle Temperature data.
      73             :  * @param temperature The temperature data.
      74             :  * @details This function processes the temperature data.
      75             :  */
      76           2 : void SystemDataManager::handleTemperatureData(const QString &temperature)
      77             : {
      78           2 :         if (m_temperature != temperature) {
      79           1 :                 m_temperature = temperature;
      80           1 :                 emit systemTemperatureUpdated(temperature);
      81             : 
      82           2 :                 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
      83           3 :                 QString apiBaseUrl = env.value("API_KEY");
      84             : 
      85           2 :                 QUrl baseUrl(apiBaseUrl);
      86           3 :                 QUrl fullUrl = baseUrl.resolved(QUrl("/temperature"));
      87             : 
      88           2 :                 QString temp = temperature;
      89           1 :                 temp.remove("°C");
      90             : 
      91           2 :                 QJsonObject json;
      92           1 :                 json["temperature"] = temp;
      93             : 
      94             :                 // Convert the JSON object to a QJsonDocument
      95           2 :                 QJsonDocument doc(json);
      96           2 :                 QByteArray jsonData = doc.toJson();
      97             : 
      98           2 :                 QNetworkRequest request(fullUrl);
      99           1 :                 request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
     100             : 
     101           1 :                 m_manager->post(request,jsonData);
     102             :         }
     103           2 : }
     104             : 
     105             : /*!
     106             :  * @brief Handle IP Address data.
     107             :  * @param ipAddress The IP address.
     108             :  * @details This function processes the IP address data.
     109             :  */
     110           3 : void SystemDataManager::handleIpAddressData(const QString &ipAddress)
     111             : {
     112           3 :         if (m_ipAddress != ipAddress) {
     113           2 :                 m_ipAddress = ipAddress;
     114             : 
     115           4 :                 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
     116           6 :                 QString apiBaseUrl = env.value("API_KEY");
     117             : 
     118           4 :                 QUrl baseUrl(apiBaseUrl);
     119           6 :                 QUrl fullUrl = baseUrl.resolved(QUrl("/ip"));
     120             : 
     121           4 :                 QJsonObject json;
     122           2 :                 json["ip"] = ipAddress;
     123             : 
     124           4 :                 QJsonDocument doc(json);
     125           4 :                 QByteArray jsonData = doc.toJson();
     126             : 
     127           4 :                 QNetworkRequest request(fullUrl);
     128           2 :                 request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
     129             : 
     130           2 :                 m_manager->post(request,jsonData);
     131             :         }
     132           3 : }
     133             : 
     134             : /*!
     135             :  * @brief Handle Battery Percentage data.
     136             :  * @param batteryPercentage The battery percentage.
     137             :  * @details This function processes the battery percentage data.
     138             :  */
     139           2 : void SystemDataManager::handleBatteryPercentage(float batteryPercentage)
     140             : {
     141           2 :         if (!qFuzzyCompare(batteryPercentage, m_batteryPercentage)) {
     142           1 :                 m_batteryPercentage = batteryPercentage;
     143           1 :                 emit batteryPercentageUpdated(batteryPercentage);
     144             : 
     145           2 :                 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
     146           3 :                 QString apiBaseUrl = env.value("API_KEY");
     147             : 
     148           2 :                 QUrl baseUrl(apiBaseUrl);
     149           3 :                 QUrl fullUrl = baseUrl.resolved(QUrl("/battery"));
     150             : 
     151           2 :                 QString temp = QString::number(batteryPercentage);
     152           1 :                 temp.remove("%");
     153             : 
     154           2 :                 QJsonObject json;
     155           1 :                 json["battery"] = temp;
     156             : 
     157           2 :                 QJsonDocument doc(json);
     158           2 :                 QByteArray jsonData = doc.toJson();
     159             : 
     160           2 :                 QNetworkRequest request(fullUrl);
     161           1 :                 request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
     162             : 
     163           1 :         m_manager->post(request,jsonData);
     164             :         }
     165           2 : }

Generated by: LCOV version 1.14