Line data Source code
1 : // SPDX-FileCopyrightText: 2022-2025 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 737 : SetTorchCommand::SetTorchCommand(QObject * const parent) : DeviceCommand(parent)
20 572 : {
21 :
22 1067 : }
23 :
24 585 : QStringList SetTorchCommand::requiredOptions(const QCommandLineParser &parser) const
25 676 : {
26 2873 : return DeviceCommand::requiredOptions(parser) + QStringList{
27 676 : QLatin1String("mode"),
28 2119 : };
29 988 : }
30 :
31 270 : QStringList SetTorchCommand::supportedOptions(const QCommandLineParser &parser) const
32 312 : {
33 582 : return DeviceCommand::supportedOptions(parser);
34 312 : }
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 225 : QStringList SetTorchCommand::processOptions(const QCommandLineParser &parser)
43 260 : {
44 485 : QStringList errors = DeviceCommand::processOptions(parser);
45 485 : if (!errors.isEmpty()) {
46 52 : return errors;
47 52 : }
48 :
49 472 : if (const QString value = parser.value(QLatin1String("mode"));
50 388 : value.trimmed().compare(QLatin1String("on"), Qt::CaseInsensitive) == 0) {
51 97 : newStatus = StatusService::TorchStatus::On;
52 291 : } else if (value.trimmed().compare(QLatin1String("off"), Qt::CaseInsensitive) == 0) {
53 97 : newStatus = StatusService::TorchStatus::Off;
54 104 : } else {
55 236 : errors.append(tr("Invalid status value: %1").arg(value));
56 200 : }
57 388 : return errors;
58 260 : }
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 135 : void SetTorchCommand::torchStatusWritten()
94 156 : {
95 291 : switch (format) {
96 97 : case OutputFormat::Csv:
97 97 : std::cout << qUtf8Printable(tr("set_torch_result\nsuccess\n"));
98 97 : break;
99 52 : case OutputFormat::Json:
100 109 : std::cout << qUtf8Printable(QLatin1String("true\n"));
101 97 : break;
102 97 : case OutputFormat::Text:
103 97 : std::cout << qUtf8Printable(tr("Done.\n"));
104 97 : break;
105 156 : }
106 291 : if (device) disconnect(); // Will exit the application once disconnected.
107 291 : }
|