LCOV - code coverage report
Current view: top level - src/cli - settorchcommand.cpp (source / functions) Hit Total Coverage
Project: Dokit Lines: 29 40 72.5 %
Version: Functions: 6 15 40.0 %

          Line data    Source code
       1             : // SPDX-FileCopyrightText: 2022-2024 Paul Colby <git@colby.id.au>
       2             : // SPDX-License-Identifier: LGPL-3.0-or-later
       3             : 
       4             : #include "settorchcommand.h"
       5             : 
       6             : #include <qtpokit/pokitdevice.h>
       7             : 
       8             : #include <iostream>
       9             : 
      10             : /*!
      11             :  * \class SetTorchCommand
      12             :  *
      13             :  * The SetTorchCommand class implements the `set-torch` CLI command.
      14             :  */
      15             : 
      16             : /*!
      17             :  * Construct a new SetTorchCommand object with \a parent.
      18             :  */
      19         220 : SetTorchCommand::SetTorchCommand(QObject * const parent) : DeviceCommand(parent)
      20             : {
      21             : 
      22         220 : }
      23             : 
      24         260 : QStringList SetTorchCommand::requiredOptions(const QCommandLineParser &parser) const
      25             : {
      26         858 :     return DeviceCommand::requiredOptions(parser) + QStringList{
      27             :         QLatin1String("mode"),
      28         754 :     };
      29             : }
      30             : 
      31         120 : QStringList SetTorchCommand::supportedOptions(const QCommandLineParser &parser) const
      32             : {
      33         120 :     return DeviceCommand::supportedOptions(parser);
      34             : }
      35             : 
      36             : /*!
      37             :  * \copybrief DeviceCommand::processOptions
      38             :  *
      39             :  * This implementation extends DeviceCommand::processOptions to process additional CLI options
      40             :  * supported (or required) by this command.
      41             :  */
      42         100 : QStringList SetTorchCommand::processOptions(const QCommandLineParser &parser)
      43             : {
      44         100 :     QStringList errors = DeviceCommand::processOptions(parser);
      45         100 :     if (!errors.isEmpty()) {
      46             :         return errors;
      47             :     }
      48             : 
      49         136 :     const QString value = parser.value(QLatin1String("mode"));
      50          80 :     if (value.trimmed().compare(QLatin1String("on"), Qt::CaseInsensitive) == 0) {
      51          20 :         newStatus = StatusService::TorchStatus::On;
      52          60 :     } else if (value.trimmed().compare(QLatin1String("off"), Qt::CaseInsensitive) == 0) {
      53          20 :         newStatus = StatusService::TorchStatus::Off;
      54             :     } else {
      55          52 :         errors.append(tr("Invalid status value: %1").arg(value));
      56             :     }
      57             :     return errors;
      58          56 : }
      59             : 
      60             : /*!
      61             :  * \copybrief DeviceCommand::getService
      62             :  *
      63             :  * This override returns a pointer to a StatusService object.
      64             :  */
      65           0 : AbstractPokitService * SetTorchCommand::getService()
      66             : {
      67             :     Q_ASSERT(device);
      68           0 :     if (!service) {
      69           0 :         service = device->status();
      70             :         Q_ASSERT(service);
      71           0 :         connect(service, &StatusService::torchStatusWritten,
      72           0 :                 this, &SetTorchCommand::torchStatusWritten);
      73             :     }
      74           0 :     return service;
      75             : }
      76             : 
      77             : /*!
      78             :  * \copybrief DeviceCommand::serviceDetailsDiscovered
      79             :  *
      80             :  * This override sets the device's name, via the Pokit Status service.
      81             :  */
      82           0 : void SetTorchCommand::serviceDetailsDiscovered()
      83             : {
      84           0 :     qCInfo(lc).noquote() << tr("Setting torch %1").arg(StatusService::toString(newStatus).toLower());
      85           0 :     if (!service->setTorchStatus(newStatus)) {
      86           0 :         QCoreApplication::exit(EXIT_FAILURE);
      87             :     }
      88           0 : }
      89             : 
      90             : /*!
      91             :  * Handles StatusService::torchStatusWritten events, by outputting the result and exiting.
      92             :  */
      93          60 : void SetTorchCommand::torchStatusWritten()
      94             : {
      95          60 :     switch (format) {
      96          20 :     case OutputFormat::Csv:
      97          26 :         std::cout << qUtf8Printable(tr("set_torch_result\nsuccess\n"));
      98          20 :         break;
      99             :     case OutputFormat::Json:
     100          32 :         std::cout << qUtf8Printable(QLatin1String("true\n"));
     101          20 :         break;
     102          20 :     case OutputFormat::Text:
     103          26 :         std::cout << qUtf8Printable(tr("Done.\n"));
     104          20 :         break;
     105             :     }
     106          60 :     if (device) disconnect(); // Will exit the application once disconnected.
     107          60 : }

Generated by: LCOV version 1.14