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 :
6 : #include <qtpokit/calibrationservice.h>
7 : #include <qtpokit/pokitdevice.h>
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 : */
23 1206 : CalibrateCommand::CalibrateCommand(QObject * const parent) : DeviceCommand(parent)
24 936 : {
25 :
26 1746 : }
27 :
28 1215 : QStringList CalibrateCommand::requiredOptions(const QCommandLineParser &parser) const
29 1404 : {
30 5967 : return DeviceCommand::requiredOptions(parser) + QStringList{
31 1404 : QLatin1String("temperature"),
32 4401 : };
33 2052 : }
34 :
35 585 : QStringList CalibrateCommand::supportedOptions(const QCommandLineParser &parser) const
36 676 : {
37 1261 : return DeviceCommand::supportedOptions(parser);
38 676 : }
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 : */
46 540 : QStringList CalibrateCommand::processOptions(const QCommandLineParser &parser)
47 624 : {
48 1164 : QStringList errors = DeviceCommand::processOptions(parser);
49 1164 : if (!errors.isEmpty()) {
50 52 : return errors;
51 52 : }
52 :
53 1067 : const QString temperatureString = parser.value(QLatin1String("temperature"));
54 572 : bool ok;
55 1067 : const float temperatureFloat = temperatureString.toFloat(&ok);
56 1067 : if (ok) {
57 776 : temperature = temperatureFloat;
58 416 : } else {
59 354 : errors.append(tr("Unrecognised temperature format: %1").arg(temperatureString));
60 156 : }
61 572 : return errors;
62 888 : }
63 :
64 : /*!
65 : * \copybrief DeviceCommand::getService
66 : *
67 : * This override returns a pointer to a CalibrationService object.
68 : */
69 0 : AbstractPokitService * CalibrateCommand::getService()
70 0 : {
71 0 : Q_ASSERT(device);
72 0 : if (!service) {
73 0 : service = device->calibration();
74 0 : Q_ASSERT(service);
75 0 : connect(service, &CalibrationService::temperatureCalibrated,
76 0 : this, &CalibrateCommand::temperatureCalibrated);
77 0 : }
78 0 : return service;
79 0 : }
80 :
81 : /*!
82 : * \copybrief DeviceCommand::serviceDetailsDiscovered
83 : *
84 : * This override sets the ambient temperature, via the Calibration service.
85 : */
86 0 : void CalibrateCommand::serviceDetailsDiscovered()
87 0 : {
88 0 : Q_ASSERT(service);
89 0 : DeviceCommand::serviceDetailsDiscovered(); // Just logs consistently.
90 0 : qCInfo(lc).noquote() << tr("Calibrating temperature at %1 degrees celcius...").arg(temperature);
91 0 : if (!service->calibrateTemperature(0)) {
92 0 : QCoreApplication::exit(EXIT_FAILURE);
93 0 : }
94 0 : }
95 :
96 : /*!
97 : * Handles CalibrationService::temperatureCalibrated events, by outputting the result and exiting.
98 : */
99 135 : void CalibrateCommand::temperatureCalibrated()
100 156 : {
101 291 : switch (format) {
102 97 : case OutputFormat::Csv:
103 97 : std::cout << qUtf8Printable(tr("calibration_result\nsuccess\n"));
104 97 : break;
105 52 : case OutputFormat::Json:
106 109 : std::cout << qUtf8Printable(QLatin1String("true\n"));
107 97 : break;
108 97 : case OutputFormat::Text:
109 97 : std::cout << qUtf8Printable(tr("Done.\n"));
110 97 : break;
111 156 : }
112 291 : if (device) disconnect(); // Will exit the application once disconnected.
113 291 : }
|