///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2017 Edouard Griffiths, F4EXB. //
// //
// Swagger server adapter interface //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation as version 3 of the License, or //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License V3 for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see . //
///////////////////////////////////////////////////////////////////////////////////
#include
#include
#include "dsp/dspengine.h"
#include "dsp/dspdevicesourceengine.h"
#include "plugin/pluginmanager.h"
#include "loggerwithfile.h"
#include "webapi/webapirequestmapper.h"
#include "webapi/webapiserver.h"
#include "webapi/webapiadaptersrv.h"
#include "maincore.h"
MESSAGE_CLASS_DEFINITION(MainCore::MsgDeleteInstance, Message)
MainCore *MainCore::m_instance = 0;
MainCore::MainCore(qtwebapp::LoggerWithFile *logger, const MainParser& parser, QObject *parent) :
QObject(parent),
m_settings(),
m_masterTabIndex(-1),
m_dspEngine(DSPEngine::instance()),
m_lastEngineState((DSPDeviceSourceEngine::State)-1),
m_logger(logger)
{
qDebug() << "MainCore::MainCore: start";
m_instance = this;
m_settings.setAudioDeviceInfo(&m_audioDeviceInfo);
m_pluginManager = new PluginManager(this);
m_pluginManager->loadPlugins(QString("pluginssrv"));
connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleMessages()), Qt::QueuedConnection);
m_masterTimer.start(50);
loadSettings();
m_apiAdapter = new WebAPIAdapterSrv(*this);
m_requestMapper = new WebAPIRequestMapper(this);
m_requestMapper->setAdapter(m_apiAdapter);
m_apiServer = new WebAPIServer(parser.getServerAddress(), parser.getServerPort(), m_requestMapper);
m_apiServer->start();
qDebug() << "MainCore::MainCore: end";
}
MainCore::~MainCore()
{
m_apiServer->stop();
m_settings.save();
delete m_apiServer;
delete m_requestMapper;
delete m_apiAdapter;
delete m_pluginManager;
qDebug() << "MainCore::~MainCore: end";
delete m_logger;
}
bool MainCore::handleMessage(const Message& cmd)
{
if (MsgDeleteInstance::match(cmd))
{
emit finished();
return true;
}
else
{
return false;
}
}
void MainCore::handleMessages()
{
Message* message;
while ((message = m_inputMessageQueue.pop()) != 0)
{
qDebug("MainCore::handleMessages: message: %s", message->getIdentifier());
handleMessage(*message);
delete message;
}
}
void MainCore::loadSettings()
{
qDebug() << "MainCore::loadSettings";
m_settings.load();
m_settings.sortPresets();
setLoggingOptions();
}
void MainCore::setLoggingOptions()
{
m_logger->setConsoleMinMessageLevel(m_settings.getConsoleMinLogLevel());
if (m_settings.getUseLogFile())
{
qtwebapp::FileLoggerSettings fileLoggerSettings; // default values
if (m_logger->hasFileLogger()) {
fileLoggerSettings = m_logger->getFileLoggerSettings(); // values from file logger if it exists
}
fileLoggerSettings.fileName = m_settings.getLogFileName(); // put new values
m_logger->createOrSetFileLogger(fileLoggerSettings, 2000); // create file logger if it does not exist and apply settings in any case
}
if (m_logger->hasFileLogger()) {
m_logger->setFileMinMessageLevel(m_settings.getFileMinLogLevel());
}
m_logger->setUseFileLogger(m_settings.getUseLogFile());
}