LCOV - code coverage report
Current view: top level - src/cli - settorchcommand.cpp (source / functions) Coverage Total Hit
Project: Dokit Lines: 71.0 % 62 44
Version: Functions: 75.0 % 8 6

            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          594 : SetTorchCommand::SetTorchCommand(QObject * const parent) : DeviceCommand(parent)
      20          484 : {
      21              : 
      22          902 : }
      23              : 
      24          494 : QStringList SetTorchCommand::requiredOptions(const QCommandLineParser &parser) const
      25          572 : {
      26         2418 :     return DeviceCommand::requiredOptions(parser) + QStringList{
      27          572 :         QLatin1String("mode"),
      28         1807 :     };
      29          858 : }
      30              : 
      31          228 : QStringList SetTorchCommand::supportedOptions(const QCommandLineParser &parser) const
      32          264 : {
      33          492 :     return DeviceCommand::supportedOptions(parser);
      34          264 : }
      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          190 : QStringList SetTorchCommand::processOptions(const QCommandLineParser &parser)
      43          220 : {
      44          410 :     QStringList errors = DeviceCommand::processOptions(parser);
      45          410 :     if (!errors.isEmpty()) {
      46           44 :         return errors;
      47           44 :     }
      48              : 
      49          388 :     if (const QString value = parser.value(QLatin1String("mode"));
      50          328 :         value.trimmed().compare(QLatin1String("on"), Qt::CaseInsensitive) == 0) {
      51           82 :         newStatus = StatusService::TorchStatus::On;
      52          246 :     } else if (value.trimmed().compare(QLatin1String("off"), Qt::CaseInsensitive) == 0) {
      53           82 :         newStatus = StatusService::TorchStatus::Off;
      54           88 :     } else {
      55          194 :         errors.append(tr("Invalid status value: %1").arg(value));
      56          180 :     }
      57          328 :     return errors;
      58          220 : }
      59              : 
      60              : /*!
      61              :  * \copybrief DeviceCommand::getService
      62              :  *
      63              :  * This override returns a pointer to a StatusService object.
      64              :  */
      65            0 : AbstractPokitService * SetTorchCommand::getService()
      66            0 : {
      67            0 :     Q_ASSERT(device);
      68            0 :     if (!service) {
      69            0 :         service = device->status();
      70            0 :         Q_ASSERT(service);
      71            0 :         connect(service, &StatusService::torchStatusWritten,
      72            0 :                 this, &SetTorchCommand::torchStatusWritten);
      73            0 :     }
      74            0 :     return service;
      75            0 : }
      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            0 : {
      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            0 :     }
      88            0 : }
      89              : 
      90              : /*!
      91              :  * Handles StatusService::torchStatusWritten events, by outputting the result and exiting.
      92              :  */
      93          114 : void SetTorchCommand::torchStatusWritten()
      94          132 : {
      95          246 :     switch (format) {
      96           82 :     case OutputFormat::Csv:
      97           82 :         std::cout << qUtf8Printable(tr("set_torch_result\nsuccess\n"));
      98           82 :         break;
      99           44 :     case OutputFormat::Json:
     100           93 :         std::cout << qUtf8Printable(QLatin1String("true\n"));
     101           82 :         break;
     102           82 :     case OutputFormat::Text:
     103           82 :         std::cout << qUtf8Printable(tr("Done.\n"));
     104           82 :         break;
     105          132 :     }
     106          246 :     if (device) disconnect(); // Will exit the application once disconnected.
     107          246 : }
        

Generated by: LCOV version 2.0-1