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 "flashledcommand.h"
5 :
6 : #include <qtpokit/pokitdevice.h>
7 : #include <qtpokit/statusservice.h>
8 :
9 : #include <iostream>
10 :
11 : /*!
12 : * \class FlashLedCommand
13 : *
14 : * The FlashLedCommand class implements the `flash-led` CLI command.
15 : */
16 :
17 : /*!
18 : * Construct a new FlashLedCommand object with \a parent.
19 : */
20 469 : FlashLedCommand::FlashLedCommand(QObject * const parent) : DeviceCommand(parent)
21 364 : {
22 :
23 679 : }
24 :
25 225 : QStringList FlashLedCommand::requiredOptions(const QCommandLineParser &parser) const
26 260 : {
27 710 : return DeviceCommand::requiredOptions(parser) + QStringList{
28 640 : };
29 260 : }
30 :
31 90 : QStringList FlashLedCommand::supportedOptions(const QCommandLineParser &parser) const
32 104 : {
33 194 : return DeviceCommand::supportedOptions(parser);
34 104 : }
35 :
36 : /*!
37 : * \copybrief DeviceCommand::processOptions
38 : *
39 : * This implementation extends DeviceCommand::processOptions to process additional CLI options
40 : * supported (or required) by this command.
41 : */
42 45 : QStringList FlashLedCommand::processOptions(const QCommandLineParser &parser)
43 52 : {
44 97 : QStringList errors = DeviceCommand::processOptions(parser);
45 52 : if (!errors.isEmpty()) {
46 0 : return errors;
47 0 : }
48 :
49 52 : return errors;
50 52 : }
51 :
52 : /*!
53 : * \copybrief DeviceCommand::getService
54 : *
55 : * This override returns a pointer to a StatusService object.
56 : */
57 0 : AbstractPokitService * FlashLedCommand::getService()
58 0 : {
59 0 : Q_ASSERT(device);
60 0 : if (!service) {
61 0 : service = device->status();
62 0 : Q_ASSERT(service);
63 0 : connect(service, &StatusService::deviceLedFlashed,
64 0 : this, &FlashLedCommand::deviceLedFlashed);
65 0 : }
66 0 : return service;
67 0 : }
68 :
69 : /*!
70 : * \copybrief DeviceCommand::serviceDetailsDiscovered
71 : *
72 : * This override flashes the device's LED, via the Pokit Status service.
73 : */
74 0 : void FlashLedCommand::serviceDetailsDiscovered()
75 0 : {
76 0 : DeviceCommand::serviceDetailsDiscovered(); // Just logs consistently.
77 0 : qCInfo(lc).noquote() << tr("Flashing Pokit device LED...");
78 0 : if (!service->flashLed()) {
79 0 : QCoreApplication::exit(EXIT_FAILURE);
80 0 : }
81 0 : }
82 :
83 : /*!
84 : * Handles StatusService::deviceLedFlashed events, by outputting the result and exiting.
85 : */
86 135 : void FlashLedCommand::deviceLedFlashed()
87 156 : {
88 291 : switch (format) {
89 97 : case OutputFormat::Csv:
90 97 : std::cout << qUtf8Printable(tr("flash_led_result\nsuccess\n"));
91 97 : break;
92 52 : case OutputFormat::Json:
93 109 : std::cout << qUtf8Printable(QLatin1String("true\n"));
94 97 : break;
95 97 : case OutputFormat::Text:
96 97 : std::cout << qUtf8Printable(tr("Done.\n"));
97 97 : break;
98 156 : }
99 291 : if (device) disconnect(); // Will exit the application once disconnected.
100 291 : }
|