Dokit
Internal development documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
settorchcommand.cpp
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"
6
8
9#include <iostream>
10
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 */
26
28{
30 u"mode"_s,
31 };
32}
33
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 */
46{
48 if (!errors.isEmpty()) {
49 return errors;
50 }
51
52 if (const QString value = parser.value(u"mode"_s);
53 value.trimmed().compare(u"on"_s, Qt::CaseInsensitive) == 0) {
55 } else if (value.trimmed().compare(u"off"_s, Qt::CaseInsensitive) == 0) {
57 } else {
58 errors.append(tr("Invalid status value: %1").arg(value));
59 }
60 return errors;
61}
62
63/*!
64 * \copybrief DeviceCommand::getService
65 *
66 * This override returns a pointer to a StatusService object.
67 */
79
80/*!
81 * \copybrief DeviceCommand::serviceDetailsDiscovered
82 *
83 * This override sets the device's name, via the Pokit Status service.
84 */
86{
87 qCInfo(lc).noquote() << tr("Setting torch %1").arg(StatusService::toString(newStatus).toLower());
88 if (!service->setTorchStatus(newStatus)) {
89 QCoreApplication::exit(EXIT_FAILURE);
90 }
91}
92
93/*!
94 * Handles StatusService::torchStatusWritten events, by outputting the result and exiting.
95 */
97{
98 switch (format) {
100 std::cout << qUtf8Printable(tr("set_torch_result\nsuccess\n"));
101 break;
103 std::cout << qUtf8Printable(u"true\n"_s);
104 break;
106 std::cout << qUtf8Printable(tr("Done.\n"));
107 break;
108 }
109 if (device) disconnect(); // Will exit the application once disconnected.
110}
virtual QStringList supportedOptions(const QCommandLineParser &parser) const
Returns a list of CLI option names supported by this command.
OutputFormat format
Selected output format.
@ Text
Plain unstructured text.
@ Csv
RFC 4180 compliant CSV text.
@ Json
RFC 8259 compliant JSON text.
virtual QStringList processOptions(const QCommandLineParser &parser)
Processes the relevant options from the command line parser.
virtual QStringList requiredOptions(const QCommandLineParser &parser) const
Returns a list of CLI option names required by this command.
The AbstractPokitService class provides a common base for Pokit services classes.
PokitDevice * device
Pokit Bluetooth device (if any) this command interacts with.
DeviceCommand(QObject *const parent=nullptr)
Construct a new DeviceCommand object with parent.
void disconnect(int exitCode=EXIT_SUCCESS)
Disconnects the underlying Pokit device, and sets exitCode to be return to the OS once the disconnect...
StatusService::TorchStatus newStatus
New status to set on the Pokit device's torch to.
SetTorchCommand(QObject *const parent=nullptr)
Construct a new SetTorchCommand object with parent.
QStringList processOptions(const QCommandLineParser &parser) override
Processes the relevant options from the command line parser.
QStringList requiredOptions(const QCommandLineParser &parser) const override
Returns a list of CLI option names required by this command.
StatusService * service
Bluetooth service this command interacts with.
QStringList supportedOptions(const QCommandLineParser &parser) const override
Returns a list of CLI option names supported by this command.
void serviceDetailsDiscovered() override
Handles service detail discovery events.
void torchStatusWritten()
Handles StatusService::torchStatusWritten events, by outputting the result and exiting.
AbstractPokitService * getService() override
Returns a Pokit service object for the derived command class.
static QString toString(const StatusService::DeviceStatus &status)
Returns a string version of the status enum label.
void torchStatusWritten()
This signal is emitted when the Torch characteristic has been written successfully.
Declares the PokitDevice class.
QString value(const QString &optionName) const const
void exit(int returnCode)
void append(const T &value)
bool isEmpty() const const
QObject(QObject *parent)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
int compare(const QString &other, Qt::CaseSensitivity cs) const const
QString trimmed() const const
CaseInsensitive
Declares the DOKIT_USE_STRINGLITERALS macro, and related functions.
#define DOKIT_USE_STRINGLITERALS
Internal macro for using either official Qt string literals (added in Qt 6.4), or our own equivalent ...