Dokit
Internal development documentation
Loading...
Searching...
No Matches
calibratecommand.cpp
1// SPDX-FileCopyrightText: 2022-2023 Paul Colby <git@colby.id.au>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4#include "calibratecommand.h"
5
8
9#include <QJsonDocument>
10#include <QJsonObject>
11
12#include <iostream>
13
14/*!
15 * \class CalibrateCommand
16 *
17 * The CalibrateCommand class implements the `calibrate` CLI command.
18 */
19
20/*!
21 * Construct a new CalibrateCommand object with \a parent.
22 */
24{
25
26}
27
29{
31 QLatin1String("temperature"),
32 };
33}
34
39
40/*!
41 * \copybrief DeviceCommand::processOptions
42 *
43 * This implementation extends DeviceCommand::processOptions to process additional CLI options
44 * supported (or required) by this command.
45 */
47{
49 if (!errors.isEmpty()) {
50 return errors;
51 }
52
53 const QString temperatureString = parser.value(QLatin1String("temperature"));
54 bool ok;
55 const float temperatureFloat = temperatureString.toFloat(&ok);
56 if (ok) {
57 temperature = temperatureFloat;
58 } else {
59 errors.append(tr("Unrecognised temperature format: %1").arg(temperatureString));
60 }
61 return errors;
62}
63
64/*!
65 * \copybrief DeviceCommand::getService
66 *
67 * This override returns a pointer to a CalibrationService object.
68 */
80
81/*!
82 * \copybrief DeviceCommand::serviceDetailsDiscovered
83 *
84 * This override sets the ambient temperature, via the Calibration service.
85 */
87{
88 Q_ASSERT(service);
89 DeviceCommand::serviceDetailsDiscovered(); // Just logs consistently.
90 qCInfo(lc).noquote() << tr("Calibrating temperature at %1 degrees celcius...").arg(temperature);
92 QCoreApplication::exit(EXIT_FAILURE);
93 }
94}
95
96/*!
97 * Handles CalibrationService::temperatureCalibrated events, by outputting the result and exiting.
98 */
100{
101 switch (format) {
103 std::cout << qUtf8Printable(tr("calibration_result\nsuccess\n"));
104 break;
106 std::cout << qUtf8Printable(QLatin1String("true\n"));
107 break;
109 std::cout << qUtf8Printable(tr("Done.\n"));
110 break;
111 }
112 if (device) disconnect(); // Will exit the application once disconnected.
113}
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 interracts 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.
bool calibrateTemperature(const float ambientTemperature)
Set's the Pokit device's name to name.
void temperatureCalibrated()
This signal is emitted when the Temperature characteristic has been written succesfully.
The AbstractCommand class extends AbstractCommand to add a PokitDevice instance.
PokitDevice * device
Pokit Bluetooth device (if any) this command inerracts with.
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...
CalibrationService * calibration()
Returns a pointer to a CalibrationService instance that uses this device's controller for access.
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
float toFloat(bool *ok) const const