Dokit
Internal development documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
calibratecommand.cpp
1// SPDX-FileCopyrightText: 2022-2025 Paul Colby <git@colby.id.au>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4#include "calibratecommand.h"
6
9
10#include <QJsonDocument>
11#include <QJsonObject>
12
13#include <iostream>
14
16
17/*!
18 * \class CalibrateCommand
19 *
20 * The CalibrateCommand class implements the `calibrate` CLI command.
21 */
22
23/*!
24 * Construct a new CalibrateCommand object with \a parent.
25 */
30
32{
34 u"temperature"_s,
35 };
36}
37
42
43/*!
44 * \copybrief DeviceCommand::processOptions
45 *
46 * This implementation extends DeviceCommand::processOptions to process additional CLI options
47 * supported (or required) by this command.
48 */
50{
52 if (!errors.isEmpty()) {
53 return errors;
54 }
55
56 const QString temperatureString = parser.value(u"temperature"_s);
57 bool ok;
58 const float temperatureFloat = temperatureString.toFloat(&ok);
59 if (ok) {
60 temperature = temperatureFloat;
61 } else {
62 errors.append(tr("Unrecognised temperature format: %1").arg(temperatureString));
63 }
64 return errors;
65}
66
67/*!
68 * \copybrief DeviceCommand::getService
69 *
70 * This override returns a pointer to a CalibrationService object.
71 */
83
84/*!
85 * \copybrief DeviceCommand::serviceDetailsDiscovered
86 *
87 * This override sets the ambient temperature, via the Calibration service.
88 */
90{
91 Q_ASSERT(service);
92 DeviceCommand::serviceDetailsDiscovered(); // Just logs consistently.
93 qCInfo(lc).noquote() << tr("Calibrating temperature at %1 degrees celsius...").arg(temperature);
94 if (!service->calibrateTemperature(0)) {
95 QCoreApplication::exit(EXIT_FAILURE);
96 }
97}
98
99/*!
100 * Handles CalibrationService::temperatureCalibrated events, by outputting the result and exiting.
101 */
103{
104 switch (format) {
106 std::cout << qUtf8Printable(tr("calibration_result\nsuccess\n"));
107 break;
109 std::cout << qUtf8Printable(u"true\n"_s);
110 break;
112 std::cout << qUtf8Printable(tr("Done.\n"));
113 break;
114 }
115 if (device) disconnect(); // Will exit the application once disconnected.
116}
Declares the CalibrationService class.
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.
QStringList processOptions(const QCommandLineParser &parser) override
Processes the relevant options from the command line parser.
CalibrateCommand(QObject *const parent=nullptr)
Construct a new CalibrateCommand object with parent.
CalibrationService * service
Bluetooth service this command interacts with.
float temperature
Ambient temperature from the CLI options.
AbstractPokitService * getService() override
Returns a Pokit service object for the derived command class.
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.
QStringList requiredOptions(const QCommandLineParser &parser) const override
Returns a list of CLI option names required by this command.
void temperatureCalibrated()
Handles CalibrationService::temperatureCalibrated events, by outputting the result and exiting.
void temperatureCalibrated()
This signal is emitted when the Temperature characteristic has been written successfully.
PokitDevice * device
Pokit Bluetooth device (if any) this command interacts with.
DeviceCommand(QObject *const parent=nullptr)
Construct a new DeviceCommand object with parent.
virtual void serviceDetailsDiscovered()
Handles service detail discovery events.
void disconnect(int exitCode=EXIT_SUCCESS)
Disconnects the underlying Pokit device, and sets exitCode to be return to the OS once the disconnect...
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
float toFloat(bool *ok) const const
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 ...