Dokit
Internal development documentation
Loading...
Searching...
No Matches
settorchcommand.cpp
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
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 */
20{
21
22}
23
30
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 */
43{
45 if (!errors.isEmpty()) {
46 return errors;
47 }
48
49 const QString value = parser.value(QLatin1String("mode"));
50 if (value.trimmed().compare(QLatin1String("on"), Qt::CaseInsensitive) == 0) {
52 } else if (value.trimmed().compare(QLatin1String("off"), Qt::CaseInsensitive) == 0) {
54 } else {
55 errors.append(tr("Invalid status value: %1").arg(value));
56 }
57 return errors;
58}
59
60/*!
61 * \copybrief DeviceCommand::getService
62 *
63 * This override returns a pointer to a StatusService object.
64 */
76
77/*!
78 * \copybrief DeviceCommand::serviceDetailsDiscovered
79 *
80 * This override sets the device's name, via the Pokit Status service.
81 */
83{
84 qCInfo(lc).noquote() << tr("Setting torch %1").arg(StatusService::toString(newStatus).toLower());
86 QCoreApplication::exit(EXIT_FAILURE);
87 }
88}
89
90/*!
91 * Handles StatusService::torchStatusWritten events, by outputting the result and exiting.
92 */
94{
95 switch (format) {
97 std::cout << qUtf8Printable(tr("set_torch_result\nsuccess\n"));
98 break;
100 std::cout << qUtf8Printable(QLatin1String("true\n"));
101 break;
103 std::cout << qUtf8Printable(tr("Done.\n"));
104 break;
105 }
106 if (device) disconnect(); // Will exit the application once disconnected.
107}
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.
The AbstractCommand class extends AbstractCommand to add a PokitDevice instance.
PokitDevice * device
Pokit Bluetooth device (if any) this command interracts with.
void disconnect(int exitCode=EXIT_SUCCESS)
Disconnects the underlying Pokit device, and sets exitCode to be return to the OS once the disconnect...
StatusService * status()
Returns a pointer to StatusService instance that uses this device's controller for access.
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 interracts 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.
void torchStatusWritten()
This signal is emitted when the Torch characteristic has been written successfully.
bool setTorchStatus(const TorchStatus status)
Set the Pokit device's torch to status.
static QString toString(const StatusService::DeviceStatus &status)
Returns a string version of the status enum label.
Declares the PokitDevice class.
QString value(const QString &optionName) const const
void exit(int returnCode)
void append(const T &value)
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
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