LCOV - code coverage report
Current view: top level - src/cli - flashledcommand.cpp (source / functions) Coverage Total Hit
Project: Dokit Lines: 61.1 % 54 33
Version: Functions: 71.4 % 7 5

            Line data    Source code
       1              : // SPDX-FileCopyrightText: 2022-2026 Paul Colby <git@colby.id.au>
       2              : // SPDX-License-Identifier: LGPL-3.0-or-later
       3              : 
       4              : #include "flashledcommand.h"
       5              : #include "../stringliterals_p.h"
       6              : 
       7              : #include <qtpokit/pokitdevice.h>
       8              : #include <qtpokit/statusservice.h>
       9              : 
      10              : #include <iostream>
      11              : 
      12              : DOKIT_USE_STRINGLITERALS
      13              : 
      14              : /*!
      15              :  * \class FlashLedCommand
      16              :  *
      17              :  * The FlashLedCommand class implements the `flash-led` CLI command.
      18              :  */
      19              : 
      20              : /*!
      21              :  * Construct a new FlashLedCommand object with \a parent.
      22              :  */
      23         1148 : FlashLedCommand::FlashLedCommand(QObject * const parent) : DeviceCommand(parent)
      24          777 : {
      25              : 
      26         1267 : }
      27              : 
      28          350 : QStringList FlashLedCommand::requiredOptions(const QCommandLineParser &parser) const
      29          555 : {
      30         1235 :     return DeviceCommand::requiredOptions(parser) + QStringList{
      31         1085 :     };
      32          555 : }
      33              : 
      34          140 : QStringList FlashLedCommand::supportedOptions(const QCommandLineParser &parser) const
      35          222 : {
      36          362 :     return DeviceCommand::supportedOptions(parser);
      37          222 : }
      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           70 : QStringList FlashLedCommand::processOptions(const QCommandLineParser &parser)
      46          111 : {
      47          181 :     QStringList errors = DeviceCommand::processOptions(parser);
      48          111 :     if (!errors.isEmpty()) {
      49            0 :         return errors;
      50            0 :     }
      51              : 
      52          111 :     return errors;
      53          111 : }
      54              : 
      55              : /*!
      56              :  * \copybrief DeviceCommand::getService
      57              :  *
      58              :  * This override returns a pointer to a StatusService object.
      59              :  */
      60            0 : AbstractPokitService * FlashLedCommand::getService()
      61            0 : {
      62            0 :     Q_ASSERT(device);
      63            0 :     if (!service) {
      64            0 :         service = device->status();
      65            0 :         Q_ASSERT(service);
      66            0 :         connect(service, &StatusService::deviceLedFlashed,
      67            0 :                 this, &FlashLedCommand::deviceLedFlashed);
      68            0 :     }
      69            0 :     return service;
      70            0 : }
      71              : 
      72              : /*!
      73              :  * \copybrief DeviceCommand::serviceDetailsDiscovered
      74              :  *
      75              :  * This override flashes the device's LED, via the Pokit Status service.
      76              :  */
      77            0 : void FlashLedCommand::serviceDetailsDiscovered()
      78            0 : {
      79            0 :     DeviceCommand::serviceDetailsDiscovered(); // Just logs consistently.
      80            0 :     qCInfo(lc).noquote() << tr("Flashing Pokit device LED...");
      81            0 :     if (!service->flashLed()) {
      82            0 :         QCoreApplication::exit(EXIT_FAILURE);
      83            0 :     }
      84            0 : }
      85              : 
      86              : /*!
      87              :  * Handles StatusService::deviceLedFlashed events, by outputting the result and exiting.
      88              :  */
      89          210 : void FlashLedCommand::deviceLedFlashed()
      90          333 : {
      91          543 :     switch (format) {
      92          181 :     case OutputFormat::Csv:
      93          194 :         std::cout << qUtf8Printable(tr("flash_led_result\nsuccess\n"));
      94          181 :         break;
      95          181 :     case OutputFormat::Json:
      96          194 :         std::cout << qUtf8Printable(u"true\n"_s);
      97          181 :         break;
      98          181 :     case OutputFormat::Text:
      99          194 :         std::cout << qUtf8Printable(tr("Done.\n"));
     100          181 :         break;
     101          333 :     }
     102          543 :     if (device) disconnect(); // Will exit the application once disconnected.
     103          543 : }
        

Generated by: LCOV version 2.4-0