diff --git a/src/main.cpp b/src/main.cpp index 738c5f5..b20e3a2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -256,16 +256,7 @@ void DaemonCoreApplication::moduleGotInfo(uint8_t addr, Mtb::ModuleInfo info) { if ((modules[addr] != nullptr) && (static_cast(modules[addr]->moduleType()) != info.type)) { log("Detected module "+QString::number(addr)+" type & stored module type mismatch! Forgetting config...", Mtb::LogLevel::Warning); - - if ((info.type&0xF0) == (static_cast(MtbModuleType::Univ2ir)&0xF0)) { - modules[addr] = std::make_unique(addr); - } else if (info.type == static_cast(MtbModuleType::Unis10)) { - modules[addr] = std::make_unique(addr); - } else { - log("Unknown module type: "+QString::number(addr)+": 0x"+ - QString::number(info.type, 16)+"!", Mtb::LogLevel::Warning); - modules[addr] = std::make_unique(addr); - } + modules[addr] = this->newModule(info.type, addr); } modules[addr]->mtbBusActivate(info); @@ -512,12 +503,7 @@ void DaemonCoreApplication::serverReceived(QTcpSocket *socket, const QJsonObject return sendError(socket, request, MTB_MODULE_INVALID_ADDR, "Invalid module address"); if (modules[addr] == nullptr) { - if ((type&0xF0) == (static_cast(MtbModuleType::Univ2ir)&0xF0)) - modules[addr] = std::make_unique(addr); - else if (type == static_cast(MtbModuleType::Unis10)) - modules[addr] = std::make_unique(addr); - else - modules[addr] = std::make_unique(addr); + modules[addr] = this->newModule(type, addr); } if ((modules[addr]->isActive()) && (type != static_cast(modules[addr]->moduleType()))) @@ -638,12 +624,7 @@ void DaemonCoreApplication::loadConfig(const QString& filename) { size_t type = module["type"].toInt(); if (modules[addr] == nullptr) { - if ((type&0xF0) == (static_cast(MtbModuleType::Univ2ir)&0xF0)) - modules[addr] = std::make_unique(addr); - else if (type == static_cast(MtbModuleType::Unis10)) - modules[addr] = std::make_unique(addr); - else - modules[addr] = std::make_unique(addr); + modules[addr] = this->newModule(type, addr); modules[addr]->loadConfig(module); } else { if (static_cast(modules[addr]->moduleType()) == type) { @@ -752,6 +733,18 @@ bool DaemonCoreApplication::hasWriteAccess(const QTcpSocket *socket) { } +std::unique_ptr DaemonCoreApplication::newModule(size_t type, uint8_t addr) { + if ((type&0xF0) == (static_cast(MtbModuleType::Univ2ir)&0xF0)) { + return std::make_unique(addr); + } else if (type == static_cast(MtbModuleType::Unis10)) { + return std::make_unique(addr); + } + + log("Unknown module type: "+QString::number(addr)+": 0x"+ + QString::number(type, 16)+"!", Mtb::LogLevel::Warning); + return std::make_unique(addr); +} + #ifdef Q_OS_WIN // Handler function will be called on separate thread! static BOOL WINAPI console_ctrl_handler(DWORD dwCtrlType) { diff --git a/src/main.h b/src/main.h index 106deff..c2c94f1 100644 --- a/src/main.h +++ b/src/main.h @@ -58,6 +58,7 @@ class DaemonCoreApplication : public QCoreApplication { void activateModule(uint8_t addr, size_t attemptsRemaining = 5); void moduleGotInfo(uint8_t addr, Mtb::ModuleInfo); void moduleDidNotGetInfo(); + static std::unique_ptr newModule(size_t type, uint8_t addr); void loadConfig(const QString &filename); void saveConfig(const QString &filename);