#include "backend.hpp"
#include "QDebug"
#include <cvb/data_type.hpp>
#include <cvb/device.hpp>
#include <cvb/device_factory.hpp>
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(CVB_LIT("Loading guf file."));
auto entries = fwupdater_->UpdateFileInfos(gufEntryIdx);
std::stringstream ss;
for (const auto &entry : entries) {
ss << entry << "\n";
}
ShowGufContent(ss.str());
ShowStatus(CVB_LIT("Done."));
} catch (...) {
ShowStatus(
CVB_LIT("Failed to retrieve available update file infos from guf."));
}
}
}
void BackEnd::BtnOpenGuf(const QString &path) {
qDebug() << "Using guf file " << path;
gufFilePath_ = path.toLocal8Bit().constData();
try {
ShowStatus("Reading guf content.");
ShowStatus("Done");
} catch (const std::exception &e) {
std::stringstream error;
error << CVB_LIT("Error creating firmware updater using guf file.") +
path.toStdString() + CVB_LIT("\n")
<< e.what() << "\n Maybe 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_) {
std::stringstream ss;
ss << entry;
list.append(std::string(ss.str()).c_str());
}
SetGufEntriesCombo(list);
} catch (...) {
ShowStatus(CVB_LIT("Failed to retieve 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 (auto const &device : deviceMap_) {
std::stringstream ss;
ss << device.first;
list.append(std::string(ss.str()).c_str());
}
SetCameraCombo(list);
}
void BackEnd::BtnStartUpdate(const int gufEntryIdx, const QString &selCamera) {
if (gufEntryIdx < 0) {
ShowStatus(CVB_LIT("No guf entry set."));
return;
}
if (selCamera.isEmpty()) {
ShowStatus(CVB_LIT("No camera selected."));
return;
}
auto selDeviceIdx = deviceMap_[selCamera.toStdString()];
qDebug() << "Starting update for device " << selCamera << " with update file "
<< QString::number(gufEntryIdx) << " \""
<< gufFileEntries_[gufEntryIdx].c_str() << "\".";
ShowStatus(CVB_LIT("Updating camera."));
try {
auto tmpDevPtr{
Cvb::AcquisitionStack::PreferGenTL)};
fwupdater_->Update(std::move(tmpDevPtr), gufEntryIdx);
ShowStatus(CVB_LIT("Update successful."));
} catch (const std::exception &e) {
std::stringstream error;
error << CVB_LIT("Update failed: ") << e.what();
ShowStatus(error.str());
} catch (...) {
ShowStatus(CVB_LIT(
"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)