Skip to content

Commit

Permalink
Avoid code duplication.
Browse files Browse the repository at this point in the history
  • Loading branch information
horacekj committed Nov 9, 2023
1 parent ce9c9cc commit 1794ca8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
37 changes: 15 additions & 22 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,7 @@ void DaemonCoreApplication::moduleGotInfo(uint8_t addr, Mtb::ModuleInfo info) {
if ((modules[addr] != nullptr) && (static_cast<size_t>(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<size_t>(MtbModuleType::Univ2ir)&0xF0)) {
modules[addr] = std::make_unique<MtbUni>(addr);
} else if (info.type == static_cast<size_t>(MtbModuleType::Unis10)) {
modules[addr] = std::make_unique<MtbUnis>(addr);
} else {
log("Unknown module type: "+QString::number(addr)+": 0x"+
QString::number(info.type, 16)+"!", Mtb::LogLevel::Warning);
modules[addr] = std::make_unique<MtbModule>(addr);
}
modules[addr] = this->newModule(info.type, addr);
}

modules[addr]->mtbBusActivate(info);
Expand Down Expand Up @@ -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<size_t>(MtbModuleType::Univ2ir)&0xF0))
modules[addr] = std::make_unique<MtbUni>(addr);
else if (type == static_cast<size_t>(MtbModuleType::Unis10))
modules[addr] = std::make_unique<MtbUnis>(addr);
else
modules[addr] = std::make_unique<MtbModule>(addr);
modules[addr] = this->newModule(type, addr);
}

if ((modules[addr]->isActive()) && (type != static_cast<size_t>(modules[addr]->moduleType())))
Expand Down Expand Up @@ -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<size_t>(MtbModuleType::Univ2ir)&0xF0))
modules[addr] = std::make_unique<MtbUni>(addr);
else if (type == static_cast<size_t>(MtbModuleType::Unis10))
modules[addr] = std::make_unique<MtbUnis>(addr);
else
modules[addr] = std::make_unique<MtbModule>(addr);
modules[addr] = this->newModule(type, addr);
modules[addr]->loadConfig(module);
} else {
if (static_cast<size_t>(modules[addr]->moduleType()) == type) {
Expand Down Expand Up @@ -752,6 +733,18 @@ bool DaemonCoreApplication::hasWriteAccess(const QTcpSocket *socket) {

}

std::unique_ptr<MtbModule> DaemonCoreApplication::newModule(size_t type, uint8_t addr) {
if ((type&0xF0) == (static_cast<size_t>(MtbModuleType::Univ2ir)&0xF0)) {
return std::make_unique<MtbUni>(addr);
} else if (type == static_cast<size_t>(MtbModuleType::Unis10)) {
return std::make_unique<MtbUnis>(addr);
}

log("Unknown module type: "+QString::number(addr)+": 0x"+
QString::number(type, 16)+"!", Mtb::LogLevel::Warning);
return std::make_unique<MtbModule>(addr);
}

#ifdef Q_OS_WIN
// Handler function will be called on separate thread!
static BOOL WINAPI console_ctrl_handler(DWORD dwCtrlType) {
Expand Down
1 change: 1 addition & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<MtbModule> newModule(size_t type, uint8_t addr);

void loadConfig(const QString &filename);
void saveConfig(const QString &filename);
Expand Down

0 comments on commit 1794ca8

Please sign in to comment.