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-2023 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         209 : SetTorchCommand::SetTorchCommand(QObject * const parent) : DeviceCommand(parent)
      20             : {
      21             : 
      22         209 : }
      23             : 
      24         247 : QStringList SetTorchCommand::requiredOptions(const QCommandLineParser &parser) const
      25             : {
      26         819 :     return DeviceCommand::requiredOptions(parser) + QStringList{
      27             :         QLatin1String("mode"),
      28         715 :     };
      29             : }
      30             : 
      31         114 : QStringList SetTorchCommand::supportedOptions(const QCommandLineParser &parser) const
      32             : {
      33         114 :     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          95 : QStringList SetTorchCommand::processOptions(const QCommandLineParser &parser)
      43             : {
      44          95 :     QStringList errors = DeviceCommand::processOptions(parser);
      45          95 :     if (!errors.isEmpty()) {
      46             :         return errors;
      47             :     }
      48             : 
      49         128 :     const QString value = parser.value(QLatin1String("mode"));
      50          76 :     if (value.trimmed().compare(QLatin1String("on"), Qt::CaseInsensitive) == 0) {
      51          19 :         newStatus = StatusService::TorchStatus::On;
      52          57 :     } else if (value.trimmed().compare(QLatin1String("off"), Qt::CaseInsensitive) == 0) {
      53          19 :         newStatus = StatusService::TorchStatus::Off;
      54             :     } else {
      55          48 :         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          57 : void SetTorchCommand::torchStatusWritten()
      94             : {
      95          57 :     switch (format) {
      96          19 :     case OutputFormat::Csv:
      97          24 :         std::cout << qUtf8Printable(tr("set_torch_result\nsuccess\n"));
      98          19 :         break;
      99             :     case OutputFormat::Json:
     100          29 :         std::cout << qUtf8Printable(QLatin1String("true\n"));
     101          19 :         break;
     102          19 :     case OutputFormat::Text:
     103          24 :         std::cout << qUtf8Printable(tr("Done.\n"));
     104          19 :         break;
     105             :     }
     106          57 :     if (device) disconnect(); // Will exit the application once disconnected.
     107          57 : }

Generated by: LCOV version 1.14