LCOV - code coverage report
Current view: top level - src/app - statuscommand.cpp (source / functions) Hit Total Coverage
Project: QtPokit Lines: 65 70 92.9 %
Version: Functions: 7 8 87.5 %

          Line data    Source code
       1             : // SPDX-FileCopyrightText: 2022-2023 Paul Colby <git@colby.id.au>
       2             : // SPDX-License-Identifier: LGPL-3.0-or-later
       3             : 
       4             : #include "statuscommand.h"
       5             : 
       6             : #include <qtpokit/pokitdevice.h>
       7             : 
       8             : #include <QJsonDocument>
       9             : #include <QJsonObject>
      10             : 
      11             : #include <iostream>
      12             : 
      13             : /*!
      14             :  * \class StatusCommand
      15             :  *
      16             :  * The StatusCommand class implements the `status` CLI command.
      17             :  */
      18             : 
      19             : /*!
      20             :  * Construct a new StatusCommand object with \a parent.
      21             :  */
      22         238 : StatusCommand::StatusCommand(QObject * const parent) : DeviceCommand(parent), service(nullptr)
      23             : {
      24             : 
      25         238 : }
      26             : 
      27          85 : QStringList StatusCommand::requiredOptions(const QCommandLineParser &parser) const
      28             : {
      29         170 :     return DeviceCommand::requiredOptions(parser) + QStringList{
      30         170 :     };
      31             : }
      32             : 
      33          34 : QStringList StatusCommand::supportedOptions(const QCommandLineParser &parser) const
      34             : {
      35          34 :     return DeviceCommand::supportedOptions(parser);
      36             : }
      37             : 
      38             : /*!
      39             :  * \copybrief DeviceCommand::processOptions
      40             :  *
      41             :  * This implementation extends DeviceCommand::processOptions to process additional CLI options
      42             :  * supported (or required) by this command.
      43             :  */
      44          17 : QStringList StatusCommand::processOptions(const QCommandLineParser &parser)
      45             : {
      46          17 :     QStringList errors = DeviceCommand::processOptions(parser);
      47             :     if (!errors.isEmpty()) {
      48             :         return errors;
      49             :     }
      50             : 
      51             :     return errors;
      52             : }
      53             : 
      54             : /*!
      55             :  * \copybrief DeviceCommand::getService
      56             :  *
      57             :  * This override returns a pointer to a StatusService object.
      58             :  */
      59           0 : AbstractPokitService * StatusCommand::getService()
      60             : {
      61             :     Q_ASSERT(device);
      62           0 :     if (!service) {
      63           0 :         service = device->status();
      64             :         Q_ASSERT(service);
      65             :     }
      66           0 :     return service;
      67             : }
      68             : 
      69             : /*!
      70             :  * \copybrief DeviceCommand::serviceDetailsDiscovered
      71             :  *
      72             :  * This override fetches the current device's status, and outputs it in the selected format.
      73             :  */
      74          17 : void StatusCommand::serviceDetailsDiscovered()
      75             : {
      76          17 :     DeviceCommand::serviceDetailsDiscovered(); // Just logs consistently.
      77          17 :     const StatusService::DeviceCharacteristics chrs = service->deviceCharacteristics();
      78          17 :     if (chrs.firmwareVersion.isNull()) {
      79          37 :         qCWarning(lc).noquote() << tr("Failed to parse device information");
      80          17 :         QCoreApplication::exit(EXIT_FAILURE);
      81             :         return;
      82             :     }
      83           0 :     outputDeviceStatus(chrs);
      84             : }
      85             : 
      86             : /*!
      87             :  * Outputs the Pokit device's details, including \a chrs, in the selected format.
      88             :  */
      89         153 : void StatusCommand::outputDeviceStatus(const StatusService::DeviceCharacteristics chrs)
      90             : {
      91         153 :     const QString deviceName = service->deviceName();
      92         153 :     const StatusService::Status status = service->status();
      93         153 :     const QString statusLabel = StatusService::toString(status.deviceStatus);
      94         153 :     const QString batteryLabel = StatusService::toString(status.batteryStatus);
      95             : 
      96         153 :     switch (format) {
      97          51 :     case OutputFormat::Csv:
      98          60 :         std::cout << qUtf8Printable(tr("device_name,device_status,firmware_version,maximum_voltage,"
      99             :                             "maximum_current,maximum_resistance,maximum_sampling_rate,"
     100             :                             "sampling_buffer_size,capability_mask,mac_address,battery_voltage,"
     101             :                             "battery_status\n"));
     102         240 :         std::cout << qUtf8Printable(QString::fromLatin1("%1,%2,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12\n")
     103             :             .arg(escapeCsvField(deviceName),statusLabel.toLower(),chrs.firmwareVersion.toString())
     104             :             .arg(chrs.maximumVoltage).arg(chrs.maximumCurrent).arg(chrs.maximumResistance)
     105             :             .arg(chrs.maximumSamplingRate).arg(chrs.samplingBufferSize).arg(chrs.capabilityMask)
     106             :             .arg(chrs.macAddress.toString()).arg(status.batteryVoltage)
     107             :             .arg(batteryLabel.toLower()));
     108          51 :         break;
     109             :     case OutputFormat::Json: {
     110             :         QJsonObject battery{
     111          51 :             { QLatin1String("level"),  status.batteryVoltage },
     112         153 :         };
     113          51 :         if (!batteryLabel.isNull()) {
     114          78 :             battery.insert(QLatin1String("status"), batteryLabel);
     115             :         }
     116        1230 :         std::cout << QJsonDocument(QJsonObject{
     117           9 :                 { QLatin1String("deviceName"),   deviceName },
     118         246 :                 { QLatin1String("firmwareVersion"), QJsonObject{
     119           9 :                       { QLatin1String("major"), chrs.firmwareVersion.majorVersion() },
     120           9 :                       { QLatin1String("minor"), chrs.firmwareVersion.minorVersion() },
     121         102 :                 }},
     122          51 :                 { QLatin1String("maximumVoltage"),      chrs.maximumVoltage },
     123          51 :                 { QLatin1String("maximumCurrent"),      chrs.maximumCurrent },
     124          51 :                 { QLatin1String("maximumResistance"),   chrs.maximumResistance },
     125          51 :                 { QLatin1String("maximumSamplingRate"), chrs.maximumSamplingRate },
     126          51 :                 { QLatin1String("samplingBufferSize"),  chrs.samplingBufferSize },
     127          51 :                 { QLatin1String("capabilityMask"),      chrs.capabilityMask },
     128         102 :                 { QLatin1String("macAddress"),          chrs.macAddress.toString() },
     129         246 :                 { QLatin1String("deviceStatus"), QJsonObject{
     130          51 :                       { QLatin1String("code"), (quint8)status.deviceStatus },
     131           9 :                       { QLatin1String("label"), statusLabel },
     132         102 :                 }},
     133           9 :                 { QLatin1String("battery"), battery },
     134         663 :             }).toJson().toStdString();
     135          51 :     }   break;
     136          51 :     case OutputFormat::Text:
     137         111 :         std::cout << qUtf8Printable(tr("Device name:           %1\n").arg(deviceName));
     138         111 :         std::cout << qUtf8Printable(tr("Firmware version:      %1\n").arg(chrs.firmwareVersion.toString()));
     139          69 :         std::cout << qUtf8Printable(tr("Maximum voltage:       %1\n").arg(chrs.maximumVoltage));
     140          69 :         std::cout << qUtf8Printable(tr("Maximum current:       %1\n").arg(chrs.maximumCurrent));
     141          69 :         std::cout << qUtf8Printable(tr("Maximum resistance:    %1\n").arg(chrs.maximumResistance));
     142          69 :         std::cout << qUtf8Printable(tr("Maximum sampling rate: %1\n").arg(chrs.maximumSamplingRate));
     143          69 :         std::cout << qUtf8Printable(tr("Sampling buffer size:  %1\n").arg(chrs.samplingBufferSize));
     144          69 :         std::cout << qUtf8Printable(tr("Capability mask:       %1\n").arg(chrs.capabilityMask));
     145         111 :         std::cout << qUtf8Printable(tr("MAC address:           %1\n").arg(chrs.macAddress.toString()));
     146          69 :         std::cout << qUtf8Printable(tr("Device status:         %1 (%2)\n").arg(statusLabel)
     147             :             .arg((quint8)status.deviceStatus));
     148         111 :         std::cout << qUtf8Printable(tr("Battery voltage:       %1\n").arg(status.batteryVoltage));
     149         120 :         std::cout << qUtf8Printable(tr("Battery status:        %1 (%2)\n")
     150             :             .arg(batteryLabel.isNull() ? QString::fromLatin1("N/A") : batteryLabel)
     151             :             .arg((quint8)status.batteryStatus));
     152          51 :         break;
     153             :     }
     154         153 :     if (device) disconnect(); // Will exit the application once disconnected.
     155         153 : }

Generated by: LCOV version 1.14