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 "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 1876 : InfoCommand::InfoCommand(QObject * const parent) : DeviceCommand(parent)
24 736 : {
25 :
26 1996 : }
27 :
28 225 : QStringList InfoCommand::requiredOptions(const QCommandLineParser &parser) const
29 260 : {
30 710 : return DeviceCommand::requiredOptions(parser) + QStringList{
31 645 : };
32 260 : }
33 :
34 90 : QStringList InfoCommand::supportedOptions(const QCommandLineParser &parser) const
35 104 : {
36 194 : return DeviceCommand::supportedOptions(parser);
37 104 : }
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 45 : QStringList InfoCommand::processOptions(const QCommandLineParser &parser)
46 52 : {
47 97 : QStringList errors = DeviceCommand::processOptions(parser);
48 52 : if (!errors.isEmpty()) {
49 0 : return errors;
50 0 : }
51 :
52 52 : return errors;
53 52 : }
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 1080 : void InfoCommand::serviceDetailsDiscovered()
76 528 : {
77 1608 : DeviceCommand::serviceDetailsDiscovered(); // Just logs consistently.
78 1608 : const QLowEnergyController * const controller = (device) ? device->controller() : nullptr;
79 1608 : const QString deviceName = (controller) ? controller->remoteName() : QString();
80 1608 : const QBluetoothAddress deviceAddress = (controller) ? controller->remoteAddress() : QBluetoothAddress();
81 1608 : const QBluetoothUuid deviceUuid = (controller) ? controller->remoteDeviceUuid() : QBluetoothUuid();
82 1608 : const QString serialNumber = service->serialNumber();
83 1608 : switch (format) {
84 536 : case OutputFormat::Csv:
85 704 : std::cout << qUtf8Printable(tr("device_name,device_address,device_uuid,manufacturer_name,model_number,"
86 176 : "hardware_revision,firmware_revision,software_revision,serial_number\n"));
87 2454 : std::cout << qUtf8Printable(QString::fromLatin1("%1,%2,%3,%4,%5,%6,%7,%8,%9\n").arg(
88 176 : escapeCsvField(deviceName),
89 176 : (deviceAddress.isNull()) ? QString() : deviceAddress.toString(),
90 176 : (deviceUuid.isNull()) ? QString() : deviceUuid.toString(),
91 176 : escapeCsvField(service->manufacturer()), escapeCsvField(service->modelNumber()),
92 176 : escapeCsvField(service->hardwareRevision()), escapeCsvField(service->firmwareRevision()),
93 176 : escapeCsvField(service->softwareRevision()), escapeCsvField(serialNumber)));
94 536 : break;
95 536 : case OutputFormat::Json: {
96 176 : QJsonObject jsonObject{
97 728 : { QLatin1String("manufacturerName"), service->manufacturer() },
98 896 : { QLatin1String("modelNumber"), service->modelNumber() },
99 896 : { QLatin1String("hardwareRevision"), service->hardwareRevision() },
100 896 : { QLatin1String("firmwareRevision"), service->firmwareRevision() },
101 896 : { QLatin1String("softwareRevision"), service->softwareRevision() },
102 3184 : };
103 536 : if (!deviceName.isEmpty()) {
104 609 : jsonObject.insert(QLatin1String("deviceName"), deviceName);
105 154 : }
106 536 : if (!deviceAddress.isNull()) {
107 104 : jsonObject.insert(QLatin1String("deviceAddress"), deviceAddress.toString());
108 22 : }
109 512 : if (!deviceUuid.isNull()) {
110 316 : jsonObject.insert(QLatin1String("deviceUuid"), deviceUuid.toString());
111 68 : }
112 536 : if (!serialNumber.isNull()) {
113 0 : jsonObject.insert(QLatin1String("serialNumber"), serialNumber);
114 0 : }
115 896 : std::cout << QJsonDocument(jsonObject).toJson().toStdString();
116 536 : } break;
117 176 : case OutputFormat::Text:
118 536 : if (!deviceName.isEmpty()) {
119 931 : std::cout << qUtf8Printable(tr("Device name: %1\n").arg(deviceName));
120 154 : }
121 536 : if (!deviceAddress.isNull()) {
122 132 : std::cout << qUtf8Printable(tr("Device addres: %1\n").arg(deviceAddress.toString()));
123 22 : }
124 512 : if (!deviceUuid.isNull()) {
125 428 : std::cout << qUtf8Printable(tr("Device UUID: %1\n").arg(deviceUuid.toString()));
126 68 : }
127 1088 : std::cout << qUtf8Printable(tr("Manufacturer name: %1\n").arg(service->manufacturer()));
128 1088 : std::cout << qUtf8Printable(tr("Model number: %1\n").arg(service->modelNumber()));
129 1088 : std::cout << qUtf8Printable(tr("Hardware revision: %1\n").arg(service->hardwareRevision()));
130 1088 : std::cout << qUtf8Printable(tr("Firmware revision: %1\n").arg(service->firmwareRevision()));
131 1088 : std::cout << qUtf8Printable(tr("Software revision: %1\n").arg(service->softwareRevision()));
132 536 : if (!serialNumber.isNull()) {
133 0 : std::cout << qUtf8Printable(tr("Serial number: %1\n").arg(serialNumber));
134 0 : }
135 176 : break;
136 528 : }
137 1608 : if (device) disconnect(); // Will exit the application once disconnected.
138 4608 : }
|