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