dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
flightlogdownload.cpp
Go to the documentation of this file.
1 
15 /*
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful, but
22  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24  * for more details.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with this program; if not, see <http://www.gnu.org/licenses/>
28  */
29 #include "flightlogdownload.h"
30 #include "ui_flightlogdownload.h"
31 
35 
36 #include "loggingstats.h"
37 
38 #include <QDateTime>
39 #include <QFile>
40 #include <QFileDialog>
41 #include <QDebug>
42 
44  : QDialog(parent)
45  , ui(new Ui::FlightLogDownload)
46 {
47  ui->setupUi(this);
48 
49  dl_state = DL_IDLE;
50 
51  ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
52  UAVObjectManager *uavoManager = pm->getObject<UAVObjectManager>();
53  loggingStats = LoggingStats::GetInstance(uavoManager);
54  Q_ASSERT(loggingStats);
55 
56  connect(ui->fileNameButton, SIGNAL(clicked()), this, SLOT(getFilename()));
57  connect(ui->saveButton, SIGNAL(clicked()), this, SLOT(startDownload()));
58 
59  // Create default file name
60  QString fileName =
61  tr("dRonin-%0.drlog").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd_hh-mm-ss"));
62  ui->fileName->setText(QDir::current().relativeFilePath(fileName));
63 
64  // Get the current status
65  connect(loggingStats, SIGNAL(objectUnpacked(UAVObject *)), this, SLOT(updateReceived()));
66  loggingStats->requestUpdate();
67 }
68 
70 {
71  delete ui;
72 }
73 
75 void FlightLogDownload::getFilename()
76 {
77  QString fileName = QFileDialog::getSaveFileName(
78  this, tr("Save log as..."),
79  tr("dRonin-%0.drlog").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd_hh-mm-ss")),
80  tr("Log (*.drlog)"));
81  if (!fileName.isEmpty())
82  ui->fileName->setText(fileName);
83 }
84 
90 void FlightLogDownload::updateReceived()
91 {
92  LoggingStats::DataFields logging = loggingStats->getData();
93 
94  switch (dl_state) {
95  case DL_IDLE:
96  // Update the file selector
97  ui->cbFileId->clear();
98  for (int i = logging.MinFileId; i <= logging.MaxFileId; i++)
99  ui->cbFileId->addItem(QString::number(i), QVariant(i));
100  return;
101  case DL_COMPLETE:
102  return;
103  case DL_DOWNLOADING:
104  break;
105  }
106 
107  UAVObject::Metadata mdata;
108 
109  switch (logging.Operation) {
110  case LoggingStats::OPERATION_IDLE:
111  log.append(reinterpret_cast<char *>(logging.FileSector), LoggingStats::FILESECTOR_NUMELEM);
112  logging.Operation = LoggingStats::OPERATION_DOWNLOAD;
113  logging.FileSectorNum++;
114  loggingStats->setData(logging);
115  loggingStats->updated();
116  ui->sectorLabel->setText(QString::number(logging.FileSectorNum));
117  qDebug() << "Requesting sector num: " << logging.FileSectorNum;
118 
119  ui->lb_operationStatus->setText("Downloading...");
120  break;
121  case LoggingStats::OPERATION_COMPLETE: {
122  log.append(reinterpret_cast<char *>(logging.FileSector), LoggingStats::FILESECTOR_NUMELEM);
123 
124  dl_state = DL_IDLE;
125 
126  mdata = loggingStats->getMetadata();
128  loggingStats->setMetadata(mdata);
129 
130  logFile->write(log);
131  logFile->close();
132 
133  ui->lb_operationStatus->setText("Download complete.");
134  break;
135  }
136  case LoggingStats::OPERATION_ERROR:
137  dl_state = DL_IDLE;
138 
139  mdata = loggingStats->getMetadata();
141  loggingStats->setMetadata(mdata);
142 
143  ui->lb_operationStatus->setText("Download error.");
144  break;
145  default:
146  qDebug() << "Unhandled";
147  }
148 }
149 
155 void FlightLogDownload::startDownload()
156 {
157  bool ok;
158  qint32 file_id = ui->cbFileId->currentData().toInt(&ok);
159  if (!ok)
160  return;
161 
162  logFile = new QFile(ui->fileName->text(), this);
163  if (!logFile->open(QIODevice::WriteOnly))
164  return;
165 
166  log.clear();
167 
168  LoggingStats::DataFields logging = loggingStats->getData();
169 
170  // Stop any existing log file
171  dl_state = DL_IDLE;
172  logging.Operation = LoggingStats::OPERATION_IDLE;
173  loggingStats->setData(logging);
174  loggingStats->updated();
175 
176  UAVObject::Metadata mdata = loggingStats->getMetadata();
178  loggingStats->setMetadata(mdata);
179 
180  qDebug() << "Download file id: " << file_id;
181  dl_state = DL_DOWNLOADING;
182  logging.Operation = LoggingStats::OPERATION_DOWNLOAD;
183  logging.FileRequest = file_id;
184  logging.FileSectorNum = 0;
185  loggingStats->setData(logging);
186  loggingStats->updated();
187 }
188 
FlightLogDownload(QWidget *parent=nullptr)
Core plugin system that manages the plugins, their life cycle and their registered objects...
Definition: pluginmanager.h:53
for i
Definition: OPPlots.m:140
static void SetFlightTelemetryUpdateMode(Metadata &meta, UpdateMode val)
Definition: uavobject.cpp:468
Import/Export Plugin.