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 2160 : CalibrateCommand::CalibrateCommand(QObject * const parent) : DeviceCommand(parent)
27 1008 : {
28 :
29 2448 : }
30 :
31 2160 : QStringList CalibrateCommand::requiredOptions(const QCommandLineParser &parser) const
32 1512 : {
33 9234 : return DeviceCommand::requiredOptions(parser) + QStringList{
34 1512 : u"temperature"_s,
35 7209 : };
36 1512 : }
37 :
38 1040 : QStringList CalibrateCommand::supportedOptions(const QCommandLineParser &parser) const
39 728 : {
40 1768 : return DeviceCommand::supportedOptions(parser);
41 728 : }
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 960 : QStringList CalibrateCommand::processOptions(const QCommandLineParser &parser)
50 672 : {
51 1632 : QStringList errors = DeviceCommand::processOptions(parser);
52 1632 : if (!errors.isEmpty()) {
53 56 : return errors;
54 56 : }
55 :
56 1496 : const QString temperatureString = parser.value(u"temperature"_s);
57 616 : bool ok;
58 1496 : const float temperatureFloat = temperatureString.toFloat(&ok);
59 1496 : if (ok) {
60 1088 : temperature = temperatureFloat;
61 448 : } else {
62 537 : errors.append(tr("Unrecognised temperature format: %1").arg(temperatureString));
63 168 : }
64 616 : return errors;
65 1079 : }
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 240 : void CalibrateCommand::temperatureCalibrated()
103 168 : {
104 408 : switch (format) {
105 136 : case OutputFormat::Csv:
106 148 : std::cout << qUtf8Printable(tr("calibration_result\nsuccess\n"));
107 136 : break;
108 136 : case OutputFormat::Json:
109 148 : std::cout << qUtf8Printable(u"true\n"_s);
110 136 : break;
111 136 : case OutputFormat::Text:
112 148 : std::cout << qUtf8Printable(tr("Done.\n"));
113 136 : break;
114 168 : }
115 408 : if (device) disconnect(); // Will exit the application once disconnected.
116 408 : }
|