Skip to content

Commit

Permalink
Changing directory and file checks for USB update
Browse files Browse the repository at this point in the history
  • Loading branch information
erinharrington-12 committed Nov 8, 2024
1 parent bd2b23a commit 64fc3c7
Showing 1 changed file with 68 additions and 69 deletions.
137 changes: 68 additions & 69 deletions src/WombatUpdateWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,70 @@
#include <QMessageBox>

WombatUpdateWidget::WombatUpdateWidget(Device *device, QWidget *parent)
: StandardWidget(device, parent),
ui(new Ui::WombatUpdateWidget)
: StandardWidget(device, parent),
ui(new Ui::WombatUpdateWidget)
{
ui->setupUi(this);
performStandardSetup("Update");

ui->updateConsole->setVisible(false);

connect(ui->update, SIGNAL(clicked()), SLOT(update()));
connect(ui->refresh, SIGNAL(clicked()), SLOT(refresh()));
connect(ui->ethernet, SIGNAL(clicked()), SLOT(ethernet()));
connect(ui->ethernet, SIGNAL(clicked()), SLOT(ethernet()));
}

WombatUpdateWidget::~WombatUpdateWidget()
{
delete ui;
}

void WombatUpdateWidget::update()
{
{
// Get single selected item
const QList<QListWidgetItem *> selected = ui->updateList->selectedItems();
if(selected.size() != 1)
if (selected.size() != 1)
return;
const QString selectedName = selected.at(0)->text();

// Verify with user that they want to do the update
if(QMessageBox::question(this, "Update?",
QString("Are you sure you want to update using %1?").arg(selectedName),
QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
return;
if (QMessageBox::question(this, "Update?",
QString("Are you sure you want to update using %1?").arg(selectedName),
QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
return;

// Disable buttons
ui->update->setEnabled(false);
ui->refresh->setEnabled(false);
ui->ethernet->setEnabled(false);

// Mount USB drive
if(!this->mountUsb("/dev/sda1", WombatUpdateWidget::mountDir) && !this->mountUsb("/dev/sdb1", WombatUpdateWidget::mountDir))
if (!this->mountUsb("/dev/sda1", WombatUpdateWidget::mountDir) && !this->mountUsb("/dev/sdb1", WombatUpdateWidget::mountDir))
QMessageBox::warning(this, "USB not found", "Failed to mount USB device");
else {
// Check if update file exists
QDir subDir = WombatUpdateWidget::mountDir;
subDir.cd(selectedName);
QDir updateDir = subDir;
updateDir.cd("updateFiles");

if(!updateDir.exists("wombat_update.sh"))
QMessageBox::warning(this, "File not found", "The update file wombat_update.sh does not exist");
else {
else
{

// Check if selectedName contains ".zip"
if (!selectedName.contains(".zip", Qt::CaseInsensitive))
{
QMessageBox::warning(this, "File Not Found", "The selected folder is not a .zip folder");
}
else
{
// Change UI to show output
ui->updateConsole->setVisible(true);
ui->selectionWidget->setVisible(false);
ui->statusLabel->setText("Update progress:");

// Run update process
m_updateProc = new QProcess();
m_updateProc->setProcessChannelMode(QProcess::MergedChannels);
m_updateProc->setWorkingDirectory(subDir.absolutePath());
m_updateProc->setWorkingDirectory("/home/kipr");
ui->updateConsole->setProcess(m_updateProc);
connect(m_updateProc, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(updateFinished(int, QProcess::ExitStatus)));
m_updateProc->start("sh", QStringList() << WombatUpdateWidget::updateFileName);

QString selectedPath = WombatUpdateWidget::mountDir.absoluteFilePath(selectedName);
m_updateProc->start("sh", QStringList() << "updateMe.sh" << selectedPath);

// Update script will reboot the controller
}
}
Expand All @@ -78,60 +79,60 @@ void WombatUpdateWidget::update()
void WombatUpdateWidget::refresh()
{
ui->refresh->setEnabled(false);

// Clear items
ui->updateList->clear();

// Mount USB drive
if(!this->mountUsb("/dev/sda1", WombatUpdateWidget::mountDir) && !this->mountUsb("/dev/sdb1", WombatUpdateWidget::mountDir))
if (!this->mountUsb("/dev/sda1", WombatUpdateWidget::mountDir) && !this->mountUsb("/dev/sdb1", WombatUpdateWidget::mountDir))
QMessageBox::warning(this, "USB not found", "Failed to mount USB device");
else {
else
{
// Look at each directory
foreach(const QString &name, WombatUpdateWidget::mountDir.entryList(QDir::NoDotAndDotDot | QDir::Dirs))
foreach (const QString &name, WombatUpdateWidget::mountDir.entryList(QDir::NoDotAndDotDot | QDir::Dirs))
{
// Filter out directories without updates
QDir subDir = WombatUpdateWidget::mountDir;
subDir.cd(name);
if(!subDir.exists(WombatUpdateWidget::updateFileName))

if (!subDir.exists(WombatUpdateWidget::updateFileName))
continue;

// Add directory to the list
ui->updateList->addItem(subDir.dirName());
}

// Unmount USB drive
if(!this->unmountUsb(WombatUpdateWidget::mountDir.absolutePath()))
if (!this->unmountUsb(WombatUpdateWidget::mountDir.absolutePath()))
qDebug() << "Failed to unmount USB drive";
}

ui->refresh->setEnabled(true);
}

void WombatUpdateWidget::updateFinished(int, QProcess::ExitStatus exitStatus)
{

StandardWidget::enableMenuBar();

//Check to see if the update failed
if(m_updateProc->exitStatus() != QProcess::NormalExit){
ui->updateConsole->insertPlainText("\n Update Failed (Crashed): \n The update script has crashed with an error. \n Contact KIPR tech support for assistance if the problem persists \n");
ui->updateConsole->moveCursor(QTextCursor::End, QTextCursor::KeepAnchor);
}

// Check to see if the update failed
if (m_updateProc->exitStatus() != QProcess::NormalExit)
{
ui->updateConsole->insertPlainText("\n Update Failed (Crashed): \n The update script has crashed with an error. \n Contact KIPR tech support for assistance if the problem persists \n");
ui->updateConsole->moveCursor(QTextCursor::End, QTextCursor::KeepAnchor);
}

// Cleanup process
ui->updateConsole->setProcess(0);
delete m_updateProc;

// Unmount USB drive
if(!this->unmountUsb(WombatUpdateWidget::mountDir.absolutePath()))
if (!this->unmountUsb(WombatUpdateWidget::mountDir.absolutePath()))
qDebug() << "Failed to unmount USB drive";

// Re-enable buttons
ui->refresh->setEnabled(true);
ui->update->setEnabled(true);

}

bool WombatUpdateWidget::mountUsb(const QString device, const QDir dir)
Expand All @@ -151,27 +152,25 @@ bool WombatUpdateWidget::unmountUsb(const QString device)
const QString WombatUpdateWidget::updateFileName = "wombat_update.sh";
const QDir WombatUpdateWidget::mountDir = QDir("/mnt");

void WombatUpdateWidget::ethernet(){
if(QMessageBox::question(this, "Update?", QString("Is your controller connected to the internet over ethernet or Wifi?"), QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes) {
void WombatUpdateWidget::ethernet()
{
if (QMessageBox::question(this, "Update?", QString("Is your controller connected to the internet over ethernet or Wifi?"), QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
{
return;
}

// Change UI to show output
ui->updateConsole->setVisible(true);
ui->selectionWidget->setVisible(false);
ui->statusLabel->setText("Update progress:");


//Disables menubar (Back and Home buttons) in StandardWidget
StandardWidget::disableMenuBar();


// Run update process
m_updateProc = new QProcess();
m_updateProc->setProcessChannelMode(QProcess::MergedChannels);
ui->updateConsole->setProcess(m_updateProc);
connect(m_updateProc, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(updateFinished(int, QProcess::ExitStatus)));
m_updateProc->startCommand("sudo sh /home/kipr/updateMe.sh");
}
// Change UI to show output
ui->updateConsole->setVisible(true);
ui->selectionWidget->setVisible(false);
ui->statusLabel->setText("Update progress:");

// Disables menubar (Back and Home buttons) in StandardWidget
StandardWidget::disableMenuBar();

// Run update process
m_updateProc = new QProcess();
m_updateProc->setProcessChannelMode(QProcess::MergedChannels);
ui->updateConsole->setProcess(m_updateProc);
connect(m_updateProc, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(updateFinished(int, QProcess::ExitStatus)));
m_updateProc->startCommand("sudo sh /home/kipr/updateMe.sh");
}

0 comments on commit 64fc3c7

Please sign in to comment.