Line data Source code
1 : // SPDX-FileCopyrightText: 2022-2024 Paul Colby <git@colby.id.au>
2 : // SPDX-License-Identifier: LGPL-3.0-or-later
3 :
4 : #include "infocommand.h"
5 :
6 : #include <qtpokit/deviceinfoservice.h>
7 : #include <qtpokit/pokitdevice.h>
8 :
9 : #include <QJsonDocument>
10 : #include <QJsonObject>
11 :
12 : #include <iostream>
13 :
14 : /*!
15 : * \class InfoCommand
16 : *
17 : * The InfoCommand class implements the `info` CLI command.
18 : */
19 :
20 : /*!
21 : * Construct a new InfoCommand object with \a parent.
22 : */
23 1278 : InfoCommand::InfoCommand(QObject * const parent) : DeviceCommand(parent)
24 500 : {
25 :
26 1390 : }
27 :
28 190 : QStringList InfoCommand::requiredOptions(const QCommandLineParser &parser) const
29 220 : {
30 600 : return DeviceCommand::requiredOptions(parser) + QStringList{
31 560 : };
32 220 : }
33 :
34 76 : QStringList InfoCommand::supportedOptions(const QCommandLineParser &parser) const
35 88 : {
36 164 : return DeviceCommand::supportedOptions(parser);
37 88 : }
38 :
39 : /*!
40 : * \copybrief DeviceCommand::processOptions
41 : *
42 : * This implementation extends DeviceCommand::processOptions to process additional CLI options
43 : * supported (or required) by this command.
44 : */
45 38 : QStringList InfoCommand::processOptions(const QCommandLineParser &parser)
46 44 : {
47 82 : QStringList errors = DeviceCommand::processOptions(parser);
48 44 : if (!errors.isEmpty()) {
49 0 : return errors;
50 0 : }
51 :
52 44 : return errors;
53 44 : }
54 :
55 : /*!
56 : * \copybrief DeviceCommand::getService
57 : *
58 : * This override returns a pointer to a DeviceInfoService object.
59 : */
60 0 : AbstractPokitService * InfoCommand::getService()
61 0 : {
62 0 : Q_ASSERT(device);
63 0 : if (!service) {
64 0 : service = device->deviceInformation();
65 0 : Q_ASSERT(service);
66 0 : }
67 0 : return service;
68 0 : }
69 :
70 : /*!
71 : * \copybrief DeviceCommand::serviceDetailsDiscovered
72 : *
73 : * This override fetches the current device's information, and outputs it in the selected format.
74 : */
75 738 : void InfoCommand::serviceDetailsDiscovered()
76 324 : {
77 1062 : DeviceCommand::serviceDetailsDiscovered(); // Just logs consistently.
78 1062 : const QLowEnergyController * const controller = (device) ? device->controller() : nullptr;
79 1062 : const QString deviceName = (controller) ? controller->remoteName() : QString();
80 1062 : const QBluetoothAddress deviceAddress = (controller) ? controller->remoteAddress() : QBluetoothAddress();
81 1062 : const QBluetoothUuid deviceUuid = (controller) ? controller->remoteDeviceUuid() : QBluetoothUuid();
82 1062 : const QString serialNumber = service->serialNumber();
83 1062 : switch (format) {
84 354 : case OutputFormat::Csv:
85 474 : std::cout << qUtf8Printable(tr("device_name,device_address,device_uuid,manufacturer_name,model_number,"
86 108 : "hardware_revision,firmware_revision,software_revision,serial_number\n"));
87 1680 : std::cout << qUtf8Printable(QString::fromLatin1("%1,%2,%3,%4,%5,%6,%7,%8,%9\n").arg(
88 108 : escapeCsvField(deviceName),
89 108 : (deviceAddress.isNull()) ? QString() : deviceAddress.toString(),
90 108 : (deviceUuid.isNull()) ? QString() : deviceUuid.toString(),
91 108 : escapeCsvField(service->manufacturer()), escapeCsvField(service->modelNumber()),
92 108 : escapeCsvField(service->hardwareRevision()), escapeCsvField(service->firmwareRevision()),
93 108 : escapeCsvField(service->softwareRevision()), escapeCsvField(serialNumber)));
94 354 : break;
95 354 : case OutputFormat::Json: {
96 108 : QJsonObject jsonObject{
97 480 : { QLatin1String("manufacturerName"), service->manufacturer() },
98 600 : { QLatin1String("modelNumber"), service->modelNumber() },
99 600 : { QLatin1String("hardwareRevision"), service->hardwareRevision() },
100 600 : { QLatin1String("firmwareRevision"), service->firmwareRevision() },
101 600 : { QLatin1String("softwareRevision"), service->softwareRevision() },
102 2170 : };
103 354 : if (!deviceName.isEmpty()) {
104 368 : jsonObject.insert(QLatin1String("deviceName"), deviceName);
105 92 : }
106 354 : if (!deviceAddress.isNull()) {
107 83 : jsonObject.insert(QLatin1String("deviceAddress"), deviceAddress.toString());
108 16 : }
109 354 : if (!deviceUuid.isNull()) {
110 216 : jsonObject.insert(QLatin1String("deviceUuid"), deviceUuid.toString());
111 44 : }
112 354 : if (!serialNumber.isNull()) {
113 0 : jsonObject.insert(QLatin1String("serialNumber"), serialNumber);
114 0 : }
115 600 : std::cout << QJsonDocument(jsonObject).toJson().toStdString();
116 354 : } break;
117 108 : case OutputFormat::Text:
118 354 : if (!deviceName.isEmpty()) {
119 613 : std::cout << qUtf8Printable(tr("Device name: %1\n").arg(deviceName));
120 92 : }
121 354 : if (!deviceAddress.isNull()) {
122 103 : std::cout << qUtf8Printable(tr("Device addres: %1\n").arg(deviceAddress.toString()));
123 16 : }
124 354 : if (!deviceUuid.isNull()) {
125 296 : std::cout << qUtf8Printable(tr("Device UUID: %1\n").arg(deviceUuid.toString()));
126 44 : }
127 720 : std::cout << qUtf8Printable(tr("Manufacturer name: %1\n").arg(service->manufacturer()));
128 720 : std::cout << qUtf8Printable(tr("Model number: %1\n").arg(service->modelNumber()));
129 720 : std::cout << qUtf8Printable(tr("Hardware revision: %1\n").arg(service->hardwareRevision()));
130 720 : std::cout << qUtf8Printable(tr("Firmware revision: %1\n").arg(service->firmwareRevision()));
131 720 : std::cout << qUtf8Printable(tr("Software revision: %1\n").arg(service->softwareRevision()));
132 354 : if (!serialNumber.isNull()) {
133 0 : std::cout << qUtf8Printable(tr("Serial number: %1\n").arg(serialNumber));
134 0 : }
135 108 : break;
136 324 : }
137 1062 : if (device) disconnect(); // Will exit the application once disconnected.
138 3132 : }
|