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 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         344 : InfoCommand::InfoCommand(QObject * const parent) : DeviceCommand(parent), service(nullptr)
      24             : {
      25             : 
      26         344 : }
      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         276 : void InfoCommand::serviceDetailsDiscovered()
      76             : {
      77         276 :     DeviceCommand::serviceDetailsDiscovered(); // Just logs consistently.
      78         396 :     const QString deviceName = device->controller()->remoteName();
      79         468 :     const QBluetoothAddress deviceAddress = device->controller()->remoteAddress();
      80         396 :     const QBluetoothUuid deviceUuid = device->controller()->remoteDeviceUuid();
      81         276 :     switch (format) {
      82          28 :     case OutputFormat::Csv:
      83         116 :         std::cout << qUtf8Printable(tr("device_name,device_address,device_uuid,manufacturer_name,model_number,"
      84             :                             "hardware_revision,firmware_revision,software_revision\n"));
      85         351 :         std::cout << qUtf8Printable(QString::fromLatin1("%1,%2,%3,%4,%5,%6,%7,%8\n").arg(
      86             :             escapeCsvField(deviceName),
      87             :             (deviceAddress.isNull()) ? QString() : deviceAddress.toString(),
      88             :             (deviceUuid.isNull()) ? QString() : deviceUuid.toString(),
      89             :             escapeCsvField(service->manufacturer()), escapeCsvField(service->modelNumber()),
      90             :             escapeCsvField(service->hardwareRevision()), escapeCsvField(service->firmwareRevision()),
      91             :             escapeCsvField(service->softwareRevision())));
      92          92 :         break;
      93             :     case OutputFormat::Json: {
      94             :         QJsonObject jsonObject{
      95         160 :             { QLatin1String("manufacturerName"), service->manufacturer() },
      96         184 :             { QLatin1String("modelNumber"),      service->modelNumber() },
      97         184 :             { QLatin1String("hardwareRevision"), service->hardwareRevision() },
      98         184 :             { QLatin1String("firmwareRevision"), service->firmwareRevision() },
      99         184 :             { QLatin1String("softwareRevision"), service->softwareRevision() },
     100         992 :         };
     101          92 :         if (!deviceName.isEmpty()) {
     102         101 :             jsonObject.insert(QLatin1String("deviceName"), deviceName);
     103             :         }
     104          92 :         if (!deviceAddress.isNull()) {
     105          19 :             jsonObject.insert(QLatin1String("deviceAddress"), deviceAddress.toString());
     106             :         }
     107          92 :         if (!deviceUuid.isNull()) {
     108          28 :             jsonObject.insert(QLatin1String("deviceUuid"), deviceUuid.toString());
     109             :         }
     110         184 :         std::cout << QJsonDocument(jsonObject).toJson().toStdString();
     111          92 :     }   break;
     112          64 :     case OutputFormat::Text:
     113          92 :         if (!deviceName.isEmpty()) {
     114         171 :             std::cout << qUtf8Printable(tr("Device name:       %1\n").arg(deviceName));
     115             :         }
     116          92 :         if (!deviceAddress.isNull()) {
     117          29 :             std::cout << qUtf8Printable(tr("Device addres:     %1\n").arg(deviceAddress.toString()));
     118             :         }
     119          92 :         if (!deviceUuid.isNull()) {
     120          68 :             std::cout << qUtf8Printable(tr("Device UUID:       %1\n").arg(deviceUuid.toString()));
     121             :         }
     122         208 :         std::cout << qUtf8Printable(tr("Manufacturer name: %1\n").arg(service->manufacturer()));
     123         208 :         std::cout << qUtf8Printable(tr("Model number:      %1\n").arg(service->modelNumber()));
     124         208 :         std::cout << qUtf8Printable(tr("Hardware revision: %1\n").arg(service->hardwareRevision()));
     125         208 :         std::cout << qUtf8Printable(tr("Firmware revision: %1\n").arg(service->firmwareRevision()));
     126         208 :         std::cout << qUtf8Printable(tr("Software revision: %1\n").arg(service->softwareRevision()));
     127          92 :         break;
     128             :     }
     129         276 :     if (device) disconnect(); // Will exit the application once disconnected.
     130         276 : }

Generated by: LCOV version 1.14