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 "settorchcommand.h"
5 : #include "../stringliterals_p.h"
6 :
7 : #include <qtpokit/pokitdevice.h>
8 :
9 : #include <iostream>
10 :
11 : DOKIT_USE_STRINGLITERALS
12 :
13 : /*!
14 : * \class SetTorchCommand
15 : *
16 : * The SetTorchCommand class implements the `set-torch` CLI command.
17 : */
18 :
19 : /*!
20 : * Construct a new SetTorchCommand object with \a parent.
21 : */
22 1804 : SetTorchCommand::SetTorchCommand(QObject * const parent) : DeviceCommand(parent)
23 1221 : {
24 :
25 1991 : }
26 :
27 910 : QStringList SetTorchCommand::requiredOptions(const QCommandLineParser &parser) const
28 1443 : {
29 4758 : return DeviceCommand::requiredOptions(parser) + QStringList{
30 1443 : u"mode"_s,
31 3731 : };
32 1443 : }
33 :
34 420 : QStringList SetTorchCommand::supportedOptions(const QCommandLineParser &parser) const
35 666 : {
36 1086 : return DeviceCommand::supportedOptions(parser);
37 666 : }
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 350 : QStringList SetTorchCommand::processOptions(const QCommandLineParser &parser)
46 555 : {
47 905 : QStringList errors = DeviceCommand::processOptions(parser);
48 905 : if (!errors.isEmpty()) {
49 111 : return errors;
50 111 : }
51 :
52 944 : if (const QString value = parser.value(u"mode"_s);
53 908 : value.trimmed().compare(u"on"_s, Qt::CaseInsensitive) == 0) {
54 181 : newStatus = StatusService::TorchStatus::On;
55 681 : } else if (value.trimmed().compare(u"off"_s, Qt::CaseInsensitive) == 0) {
56 181 : newStatus = StatusService::TorchStatus::Off;
57 222 : } else {
58 454 : errors.append(tr("Invalid status value: %1").arg(value));
59 318 : }
60 724 : return errors;
61 555 : }
62 :
63 : /*!
64 : * \copybrief DeviceCommand::getService
65 : *
66 : * This override returns a pointer to a StatusService object.
67 : */
68 0 : AbstractPokitService * SetTorchCommand::getService()
69 0 : {
70 0 : Q_ASSERT(device);
71 0 : if (!service) {
72 0 : service = device->status();
73 0 : Q_ASSERT(service);
74 0 : connect(service, &StatusService::torchStatusWritten,
75 0 : this, &SetTorchCommand::torchStatusWritten);
76 0 : }
77 0 : return service;
78 0 : }
79 :
80 : /*!
81 : * \copybrief DeviceCommand::serviceDetailsDiscovered
82 : *
83 : * This override sets the device's name, via the Pokit Status service.
84 : */
85 0 : void SetTorchCommand::serviceDetailsDiscovered()
86 0 : {
87 0 : qCInfo(lc).noquote() << tr("Setting torch %1").arg(StatusService::toString(newStatus).toLower());
88 0 : if (!service->setTorchStatus(newStatus)) {
89 0 : QCoreApplication::exit(EXIT_FAILURE);
90 0 : }
91 0 : }
92 :
93 : /*!
94 : * Handles StatusService::torchStatusWritten events, by outputting the result and exiting.
95 : */
96 210 : void SetTorchCommand::torchStatusWritten()
97 333 : {
98 543 : switch (format) {
99 181 : case OutputFormat::Csv:
100 194 : std::cout << qUtf8Printable(tr("set_torch_result\nsuccess\n"));
101 181 : break;
102 181 : case OutputFormat::Json:
103 194 : std::cout << qUtf8Printable(u"true\n"_s);
104 181 : break;
105 181 : case OutputFormat::Text:
106 194 : std::cout << qUtf8Printable(tr("Done.\n"));
107 181 : break;
108 333 : }
109 543 : if (device) disconnect(); // Will exit the application once disconnected.
110 543 : }
|