LCOV - code coverage report
Current view: top level - src/app - infocommand.cpp (source / functions) Hit Total Coverage
Project: QtPokit Lines: 48 52 92.3 %
Version: Functions: 6 7 85.7 %

          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 "infocommand.h"
       5             : 
       6             : #include <qtpokit/deviceinfoservice.h>
       7             : #include <qtpokit/pokitdevice.h>
       8             : 
       9             : #include <QJsonDocument>
      10             : #include <QJsonObject>
      11             : 
      12             : #include <iostream>
      13             : 
      14             : /*!
      15             :  * \class InfoCommand
      16             :  *
      17             :  * The InfoCommand class implements the `info` CLI command.
      18             :  */
      19             : 
      20             : /*!
      21             :  * Construct a new InfoCommand object with \a parent.
      22             :  */
      23         356 : InfoCommand::InfoCommand(QObject * const parent) : DeviceCommand(parent), service(nullptr)
      24             : {
      25             : 
      26         356 : }
      27             : 
      28          85 : QStringList InfoCommand::requiredOptions(const QCommandLineParser &parser) const
      29             : {
      30         170 :     return DeviceCommand::requiredOptions(parser) + QStringList{
      31         170 :     };
      32             : }
      33             : 
      34          34 : QStringList InfoCommand::supportedOptions(const QCommandLineParser &parser) const
      35             : {
      36          34 :     return DeviceCommand::supportedOptions(parser);
      37             : }
      38             : 
      39             : /*!
      40             :  * \copybrief DeviceCommand::processOptions
      41             :  *
      42             :  * This implementation extends DeviceCommand::processOptions to process additional CLI options
      43             :  * supported (or required) by this command.
      44             :  */
      45          17 : QStringList InfoCommand::processOptions(const QCommandLineParser &parser)
      46             : {
      47          17 :     QStringList errors = DeviceCommand::processOptions(parser);
      48             :     if (!errors.isEmpty()) {
      49             :         return errors;
      50             :     }
      51             : 
      52             :     return errors;
      53             : }
      54             : 
      55             : /*!
      56             :  * \copybrief DeviceCommand::getService
      57             :  *
      58             :  * This override returns a pointer to a DeviceInfoService object.
      59             :  */
      60           0 : AbstractPokitService * InfoCommand::getService()
      61             : {
      62             :     Q_ASSERT(device);
      63           0 :     if (!service) {
      64           0 :         service = device->deviceInformation();
      65             :         Q_ASSERT(service);
      66             :     }
      67           0 :     return service;
      68             : }
      69             : 
      70             : /*!
      71             :  * \copybrief DeviceCommand::serviceDetailsDiscovered
      72             :  *
      73             :  * This override fetches the current device's information, and outputs it in the selected format.
      74             :  */
      75         288 : void InfoCommand::serviceDetailsDiscovered()
      76             : {
      77         288 :     DeviceCommand::serviceDetailsDiscovered(); // Just logs consistently.
      78         288 :     const QLowEnergyController * const controller = (device) ? device->controller() : nullptr;
      79         288 :     const QString deviceName = (controller) ? controller->remoteName() : QString();
      80         288 :     const QBluetoothAddress deviceAddress = (controller) ? controller->remoteAddress() : QBluetoothAddress();
      81         288 :     const QBluetoothUuid deviceUuid = (controller) ? controller->remoteDeviceUuid() : QBluetoothUuid();
      82         288 :     switch (format) {
      83          96 :     case OutputFormat::Csv:
      84         120 :         std::cout << qUtf8Printable(tr("device_name,device_address,device_uuid,manufacturer_name,model_number,"
      85             :                             "hardware_revision,firmware_revision,software_revision\n"));
      86         363 :         std::cout << qUtf8Printable(QString::fromLatin1("%1,%2,%3,%4,%5,%6,%7,%8\n").arg(
      87             :             escapeCsvField(deviceName),
      88             :             (deviceAddress.isNull()) ? QString() : deviceAddress.toString(),
      89             :             (deviceUuid.isNull()) ? QString() : deviceUuid.toString(),
      90             :             escapeCsvField(service->manufacturer()), escapeCsvField(service->modelNumber()),
      91             :             escapeCsvField(service->hardwareRevision()), escapeCsvField(service->firmwareRevision()),
      92             :             escapeCsvField(service->softwareRevision())));
      93          96 :         break;
      94             :     case OutputFormat::Json: {
      95             :         QJsonObject jsonObject{
      96         192 :             { QLatin1String("manufacturerName"), service->manufacturer() },
      97         192 :             { QLatin1String("modelNumber"),      service->modelNumber() },
      98         192 :             { QLatin1String("hardwareRevision"), service->hardwareRevision() },
      99         192 :             { QLatin1String("firmwareRevision"), service->firmwareRevision() },
     100         192 :             { QLatin1String("softwareRevision"), service->softwareRevision() },
     101        1416 :         };
     102          96 :         if (!deviceName.isEmpty()) {
     103         102 :             jsonObject.insert(QLatin1String("deviceName"), deviceName);
     104             :         }
     105          96 :         if (!deviceAddress.isNull()) {
     106          18 :             jsonObject.insert(QLatin1String("deviceAddress"), deviceAddress.toString());
     107             :         }
     108          96 :         if (!deviceUuid.isNull()) {
     109          32 :             jsonObject.insert(QLatin1String("deviceUuid"), deviceUuid.toString());
     110             :         }
     111         192 :         std::cout << QJsonDocument(jsonObject).toJson().toStdString();
     112          96 :     }   break;
     113             :     case OutputFormat::Text:
     114          96 :         if (!deviceName.isEmpty()) {
     115         179 :             std::cout << qUtf8Printable(tr("Device name:       %1\n").arg(deviceName));
     116             :         }
     117          96 :         if (!deviceAddress.isNull()) {
     118          29 :             std::cout << qUtf8Printable(tr("Device addres:     %1\n").arg(deviceAddress.toString()));
     119             :         }
     120          96 :         if (!deviceUuid.isNull()) {
     121          76 :             std::cout << qUtf8Printable(tr("Device UUID:       %1\n").arg(deviceUuid.toString()));
     122             :         }
     123         216 :         std::cout << qUtf8Printable(tr("Manufacturer name: %1\n").arg(service->manufacturer()));
     124         216 :         std::cout << qUtf8Printable(tr("Model number:      %1\n").arg(service->modelNumber()));
     125         216 :         std::cout << qUtf8Printable(tr("Hardware revision: %1\n").arg(service->hardwareRevision()));
     126         216 :         std::cout << qUtf8Printable(tr("Firmware revision: %1\n").arg(service->firmwareRevision()));
     127         216 :         std::cout << qUtf8Printable(tr("Software revision: %1\n").arg(service->softwareRevision()));
     128          96 :         break;
     129             :     }
     130         288 :     if (device) disconnect(); // Will exit the application once disconnected.
     131         360 : }

Generated by: LCOV version 1.14