Line data Source code
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"
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 2178 : CalibrateCommand::CalibrateCommand(QObject * const parent) : DeviceCommand(parent)
27 1296 : {
28 :
29 2466 : }
30 :
31 1755 : QStringList CalibrateCommand::requiredOptions(const QCommandLineParser &parser) const
32 1944 : {
33 8316 : return DeviceCommand::requiredOptions(parser) + QStringList{
34 1944 : u"temperature"_s,
35 6426 : };
36 1944 : }
37 :
38 845 : QStringList CalibrateCommand::supportedOptions(const QCommandLineParser &parser) const
39 936 : {
40 1781 : return DeviceCommand::supportedOptions(parser);
41 936 : }
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 780 : QStringList CalibrateCommand::processOptions(const QCommandLineParser &parser)
50 864 : {
51 1644 : QStringList errors = DeviceCommand::processOptions(parser);
52 1644 : if (!errors.isEmpty()) {
53 72 : return errors;
54 72 : }
55 :
56 1507 : const QString temperatureString = parser.value(u"temperature"_s);
57 792 : bool ok;
58 1507 : const float temperatureFloat = temperatureString.toFloat(&ok);
59 1507 : if (ok) {
60 1096 : temperature = temperatureFloat;
61 576 : } else {
62 534 : errors.append(tr("Unrecognised temperature format: %1").arg(temperatureString));
63 216 : }
64 792 : return errors;
65 1128 : }
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 195 : void CalibrateCommand::temperatureCalibrated()
103 216 : {
104 411 : switch (format) {
105 137 : case OutputFormat::Csv:
106 149 : std::cout << qUtf8Printable(tr("calibration_result\nsuccess\n"));
107 137 : break;
108 137 : case OutputFormat::Json:
109 149 : std::cout << qUtf8Printable(u"true\n"_s);
110 137 : break;
111 137 : case OutputFormat::Text:
112 149 : std::cout << qUtf8Printable(tr("Done.\n"));
113 137 : break;
114 216 : }
115 411 : if (device) disconnect(); // Will exit the application once disconnected.
116 411 : }
|