#include "backend.hpp"
#include "QDebug"
#include "QQuickView"
#include <cvb/data_type.hpp>
#include <cvb/device.hpp>
#include <cvb/device_factory.hpp>
#include <cvb/ui/ui.hpp>
#include <sstream>
BackEnd::BackEnd(QQuickView *view, QObject *parent)
: QObject(parent), view_{view} {
qRegisterMetaType<QWindow::Visibility>();
}
BackEnd::~BackEnd() {}
void BackEnd::ShowStatus(const std::string &statusMessage) {
auto statusText = view_->findChild<QObject *>("status");
if (statusText)
statusText->setProperty("text", statusMessage.c_str());
}
void BackEnd::ShowGufContent(const std::string &statusMessage) {
auto statusText = view_->findChild<QObject *>("gufContent");
if (statusText)
statusText->setProperty("text", statusMessage.c_str());
}
const QVariantList BackEnd::CameraCombo() { return cameraCombo_; }
const QVariantList BackEnd::GufEntriesCombo() { return gufEntriesCombo_; }
void BackEnd::SetCameraCombo(const QVariantList &cameraCombo) {
if (cameraCombo_ != cameraCombo) {
cameraCombo_ = cameraCombo;
emit CameraComboChanged();
}
}
void BackEnd::SetGufEntriesCombo(const QVariantList &gufEntriesCombo) {
if (gufEntriesCombo_ != gufEntriesCombo) {
gufEntriesCombo_ = gufEntriesCombo;
emit GufEntriesComboChanged();
}
}
void BackEnd::UpdateGufEntryInfo(const int &gufEntryIdx) {
if (fwupdater_) {
try {
ShowStatus("Loading guf file.");
auto entries = fwupdater_->UpdateFileInfos(gufEntryIdx);
QString text;
for (const auto &entry : entries) {
text += "\n";
}
ShowGufContent(text.toStdString());
ShowStatus("Done.");
} catch (...) {
ShowStatus("Failed to retrieve available update file infos from guf.");
}
}
}
void BackEnd::BtnOpenGuf(const QString &path) {
qDebug() << "Using guf file " << path;
try {
ShowStatus("Reading guf content.");
ShowStatus("Done");
} catch (const std::exception &e) {
std::stringstream error;
error << "Error creating firmware updater using guf file.\n"
<< path.toUtf8().constData() << "\n"
<< e.what() << "\nMaybe check the Control.xml in the guf file.";
ShowStatus(error.str());
} catch (...) {
ShowStatus("Failed to create firmware updater.");
}
QVariantList list;
list.clear();
if (fwupdater_) {
try {
gufFileEntries_ = fwupdater_->AvailableUpdateFiles();
for (const auto &entry : gufFileEntries_) {
}
SetGufEntriesCombo(list);
} catch (...) {
ShowStatus("Failed to retrieve available update files from guf.");
}
}
}
void BackEnd::ListAvailableDevices() {
deviceMap_.clear();
discover_ =
for (const auto &device : discover_) {
if (device.TryGetProperty(Cvb::Driver::DiscoveryProperties::DeviceModel,
devModel))
deviceMap_[devModel] = static_cast<int32_t>(device.Index());
}
}
void BackEnd::BtnScan() {
qDebug() << "Starting scan of available devices for guf file.";
QVariantList list;
list.clear();
ListAvailableDevices();
for (const auto &device : deviceMap_) {
}
SetCameraCombo(list);
}
void BackEnd::BtnStartUpdate(const int gufEntryIdx, const QString &selCamera) {
if (gufEntryIdx < 0) {
ShowStatus("No guf entry set.");
return;
}
if (selCamera.isEmpty()) {
ShowStatus("No camera selected.");
return;
}
qDebug() << "Starting update for device " << selCamera << " with update file "
<< QString::number(gufEntryIdx) << "\""
ShowStatus("Updating camera.");
try {
auto tmpDevPtr{
Cvb::AcquisitionStack::PreferGenTL)};
fwupdater_->Update(std::move(tmpDevPtr), gufEntryIdx);
ShowStatus("Update successful.");
} catch (const std::exception &e) {
std::stringstream error;
error << "Update failed: " << e.what();
ShowStatus(error.str());
} catch (...) {
ShowStatus(
"Sorry, something unknown and unexpected has happened. Following the "
"mainstream we could have ignored this leaving you even more puzzled. "
"However we must admit that we have no clue either.");
}
}
static std::vector< DiscoveryInformation > Discover()
static std::shared_ptr< T > Open(const String &provider, AcquisitionStack acquisitionStack=AcquisitionStack::PreferVin)
static std::unique_ptr< FWUpdater > Create(const String &filename, bool verify=true)
Cvb::String QtToCvb(const QString text) noexcept
QString CvbToQt(const Cvb::String &text) noexcept