Line data Source code
1 : // SPDX-FileCopyrightText: 2022-2026 Paul Colby <git@colby.id.au>
2 : // SPDX-License-Identifier: LGPL-3.0-or-later
3 :
4 : #include "calibratecommand.h"
5 : #include "../stringliterals_p.h"
6 :
7 : #include <qtpokit/calibrationservice.h>
8 : #include <qtpokit/pokitdevice.h>
9 :
10 : #include <QJsonDocument>
11 : #include <QJsonObject>
12 :
13 : #include <iostream>
14 :
15 : DOKIT_USE_STRINGLITERALS
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 : */
26 2952 : CalibrateCommand::CalibrateCommand(QObject * const parent) : DeviceCommand(parent)
27 1998 : {
28 :
29 3258 : }
30 :
31 1890 : QStringList CalibrateCommand::requiredOptions(const QCommandLineParser &parser) const
32 2997 : {
33 9882 : return DeviceCommand::requiredOptions(parser) + QStringList{
34 2997 : u"temperature"_s,
35 7749 : };
36 2997 : }
37 :
38 910 : QStringList CalibrateCommand::supportedOptions(const QCommandLineParser &parser) const
39 1443 : {
40 2353 : return DeviceCommand::supportedOptions(parser);
41 1443 : }
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 : */
49 840 : QStringList CalibrateCommand::processOptions(const QCommandLineParser &parser)
50 1332 : {
51 2172 : QStringList errors = DeviceCommand::processOptions(parser);
52 2172 : if (!errors.isEmpty()) {
53 111 : return errors;
54 111 : }
55 :
56 1991 : const QString temperatureString = parser.value(u"temperature"_s);
57 1221 : bool ok;
58 1991 : const float temperatureFloat = temperatureString.toFloat(&ok);
59 1991 : if (ok) {
60 1448 : temperature = temperatureFloat;
61 888 : } else {
62 681 : errors.append(tr("Unrecognised temperature format: %1").arg(temperatureString));
63 333 : }
64 1221 : return errors;
65 1596 : }
66 :
67 : /*!
68 : * \copybrief DeviceCommand::getService
69 : *
70 : * This override returns a pointer to a CalibrationService object.
71 : */
72 0 : AbstractPokitService * CalibrateCommand::getService()
73 0 : {
74 0 : Q_ASSERT(device);
75 0 : if (!service) {
76 0 : service = device->calibration();
77 0 : Q_ASSERT(service);
78 0 : connect(service, &CalibrationService::temperatureCalibrated,
79 0 : this, &CalibrateCommand::temperatureCalibrated);
80 0 : }
81 0 : return service;
82 0 : }
83 :
84 : /*!
85 : * \copybrief DeviceCommand::serviceDetailsDiscovered
86 : *
87 : * This override sets the ambient temperature, via the Calibration service.
88 : */
89 0 : void CalibrateCommand::serviceDetailsDiscovered()
90 0 : {
91 0 : Q_ASSERT(service);
92 0 : DeviceCommand::serviceDetailsDiscovered(); // Just logs consistently.
93 0 : qCInfo(lc).noquote() << tr("Calibrating temperature at %1 degrees celsius...").arg(temperature);
94 0 : if (!service->calibrateTemperature(0)) {
95 0 : QCoreApplication::exit(EXIT_FAILURE);
96 0 : }
97 0 : }
98 :
99 : /*!
100 : * Handles CalibrationService::temperatureCalibrated events, by outputting the result and exiting.
101 : */
102 210 : void CalibrateCommand::temperatureCalibrated()
103 333 : {
104 543 : switch (format) {
105 181 : case OutputFormat::Csv:
106 194 : std::cout << qUtf8Printable(tr("calibration_result\nsuccess\n"));
107 181 : break;
108 181 : case OutputFormat::Json:
109 194 : std::cout << qUtf8Printable(u"true\n"_s);
110 181 : break;
111 181 : case OutputFormat::Text:
112 194 : std::cout << qUtf8Printable(tr("Done.\n"));
113 181 : break;
114 333 : }
115 543 : if (device) disconnect(); // Will exit the application once disconnected.
116 543 : }
|