#include "QueueManager.h" QM::QueueManager::QueueManager(wxEvtHandler *eventHandler, std::string jobsdir) { // need to send events into the mainwindow by the threads... this->eventHandler = eventHandler; this->eventHandler->Bind(wxEVT_THREAD, &QueueManager::OnThreadMessage, this); this->jobsDir = jobsdir; this->QueueList = std::map(); } int QM::QueueManager::AddItem(QM::QueueItem item) { if (!item.id) { item.id = this->GetCurrentUnixTimestamp(); } this->QueueList[item.id] = item; this->SendEventToMainWindow(QM::QueueEvents::ITEM_ADDED, item); if (this->isRunning == false) { this->SendEventToMainWindow(QM::QueueEvents::ITEM_START, item); this->isRunning = true; } return item.id; } int QM::QueueManager::AddItem(sd_gui_utils::SDParams *params) { QM::QueueItem item; item.params = *params; item.created_at = item.id = this->GetCurrentUnixTimestamp(); return this->AddItem(item); } int QM::QueueManager::AddItem(sd_gui_utils::SDParams params) { QM::QueueItem item; item.params = params; item.created_at = item.id = this->GetCurrentUnixTimestamp(); return this->AddItem(item); } QM::QueueItem QM::QueueManager::GetItem(int id) { if (this->QueueList.find(id) == this->QueueList.end()) { return QM::QueueItem(); } else { return this->QueueList[id]; } } QM::QueueItem QM::QueueManager::GetItem(QM::QueueItem item) { return this->GetItem(item.id); } const std::map QM::QueueManager::getList() { return this->QueueList; } int QM::QueueManager::Duplicate(QM::QueueItem item) { if (this->QueueList.find(item.id) == this->QueueList.end()) { return -1; } else { return this->AddItem(this->QueueList.at(item.id).params); } } int QM::QueueManager::Duplicate(int id) { return this->AddItem(this->GetItem(id).params); } void QM::QueueManager::SetStatus(QM::QueueStatus status, int id) { if (this->QueueList.find(id) != this->QueueList.end()) { this->QueueList[id].status = status; this->SendEventToMainWindow(QM::QueueEvents::ITEM_STATUS_CHANGED, this->QueueList[id]); } } void QM::QueueManager::PauseAll() { for (auto [key, value] : this->QueueList) { if (value.status == QM::QueueStatus::PENDING) { this->SetStatus(QM::PAUSED, key); this->SendEventToMainWindow(QM::QueueEvents::ITEM_STATUS_CHANGED); } } } void QM::QueueManager::SendEventToMainWindow(QM::QueueEvents eventType, QM::QueueItem item) { wxThreadEvent *e = new wxThreadEvent(); e->SetString(wxString::Format("QUEUE:%d", (int)eventType)); e->SetPayload(item); wxQueueEvent(this->eventHandler, e); } void QM::QueueManager::OnThreadMessage(wxThreadEvent &e) { e.Skip(); auto msg = e.GetString().ToStdString(); std::string token = msg.substr(0, msg.find(":")); std::string content = msg.substr(msg.find(":") + 1); // only numbers here... // only handle the QUEUE messages, what this class generate if (token == "QUEUE") { QM::QueueEvents event = (QM::QueueEvents)std::stoi(content); auto payload = e.GetPayload(); if (event == QM::QueueEvents::ITEM_START) { this->SetStatus(QM::QueueStatus::RUNNING, payload.id); this->isRunning = true; } if (event == QM::QueueEvents::ITEM_FINISHED) { this->SetStatus(QM::QueueStatus::DONE, payload.id); this->isRunning = false; // jump to the next item in queue // find waiting jobs for (auto job : this->QueueList) { if (job.second.status == QM::QueueStatus::PENDING) { if (this->isRunning == false) { this->SendEventToMainWindow(QM::QueueEvents::ITEM_START, job.second); this->isRunning = true; } break; } } // move finished job into another list - ?? } } if (token == "MODEL_LOAD_START") { auto payload = e.GetPayload(); this->SetStatus(QM::QueueStatus::MODEL_LOADING, payload.id); } // this state can not usable at here, because the payload is the sd_ctx* pointer here.. // we can't identify the current running job here... (we can maybe guess it, but not needed) // see GENERATION_START if (token == "MODEL_LOAD_DONE") { // auto payload = e.GetPayload(); // this->SetStatus(QM::QueueStatus::RUNNING, payload.id); } if (token == "MODEL_LOAD_ERROR" || token == "GENERATION_ERROR") { auto payload = e.GetPayload(); this->SetStatus(QM::QueueStatus::FAILED, payload.id); this->isRunning = false; } if (token == "GENERATION_START") { auto payload = e.GetPayload(); this->SetStatus(QM::QueueStatus::RUNNING, payload.id); this->isRunning = true; } // nothing to todo here, the payload is the generated image list if (token == "GENERATION_DONE") { } } int QM::QueueManager::GetCurrentUnixTimestamp() { const auto p1 = std::chrono::system_clock::now(); return static_cast(std::chrono::duration_cast(p1.time_since_epoch()).count()); } void QM::QueueManager::onItemAdded(QM::QueueItem item) { // this->parent->m_joblist // auto dataTable = this->parent->m_joblist; }