add loading of plugins

pull/228/head
Michael Zanetti 2019-10-18 15:28:42 +02:00
parent f1ca2a5b86
commit a47bded031
5 changed files with 77 additions and 0 deletions

View File

@ -1,11 +1,74 @@
#include "experiencemanager.h"
#include "experiences/experienceplugin.h"
#include "jsonrpc/jsonrpcserver.h"
#include "loggingcategories.h"
#include <QCoreApplication>
#include <QDir>
#include <QDebug>
#include <QPluginLoader>
namespace nymeaserver {
ExperienceManager::ExperienceManager(JsonRPCServer *jsonRpcServer, QObject *parent) : QObject(parent)
{
// jsonRpcServer->registerHandler();
foreach (const QString &path, pluginSearchDirs()) {
QDir dir(path);
qCDebug(dcExperiences) << "Loading platform plugins from:" << dir.absolutePath();
foreach (const QString &entry, dir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot)) {
QFileInfo fi(path + "/" + entry);
if (fi.isFile()) {
if (entry.startsWith("libnymea_experienceplugin") && entry.endsWith(".so")) {
loadExperiencePlugin(path + "/" + entry);
}
} else if (fi.isDir()) {
if (QFileInfo::exists(path + "/" + entry + "/libnymea_experienceplugin" + entry + ".so")) {
loadExperiencePlugin(path + "/" + entry + "/libnymea_experienceplugin" + entry + ".so");
}
}
}
}
}
QStringList ExperienceManager::pluginSearchDirs() const
{
QStringList searchDirs;
QByteArray envPath = qgetenv("NYMEA_EXPERIENCE_PLUGINS_PATH");
if (!envPath.isEmpty()) {
searchDirs << QString(envPath).split(':');
}
foreach (QString libraryPath, QCoreApplication::libraryPaths()) {
searchDirs << libraryPath.replace("qt5", "nymea").replace("plugins", "experiences");
}
searchDirs << QCoreApplication::applicationDirPath() + "/../lib/nymea/experiences";
searchDirs << QCoreApplication::applicationDirPath() + "/../experiences/";
searchDirs << QCoreApplication::applicationDirPath() + "/../../../experiences/";
return searchDirs;
}
void ExperienceManager::loadExperiencePlugin(const QString &file)
{
QPluginLoader loader;
loader.setFileName(file);
loader.setLoadHints(QLibrary::ResolveAllSymbolsHint);
if (!loader.load()) {
qCWarning(dcExperiences()) << loader.errorString();
return;
}
ExperiencePlugin *plugin = qobject_cast<ExperiencePlugin*>(loader.instance());
if (!plugin) {
qCWarning(dcExperiences()) << "Could not get plugin instance of" << loader.fileName();
loader.unload();
return;
}
qCDebug(dcExperiences()) << "Loaded experience plugin:" << loader.fileName();
plugin->setParent(this);
m_plugins.append(plugin);
}
}

View File

@ -3,6 +3,8 @@
#include <QObject>
class ExperiencePlugin;
namespace nymeaserver {
class JsonRPCServer;
@ -16,6 +18,14 @@ public:
signals:
public slots:
private:
QStringList pluginSearchDirs() const;
void loadExperiencePlugin(const QString &file);
private:
QList<ExperiencePlugin*> m_plugins;
};
}

View File

@ -97,6 +97,7 @@
#include "nymeasettings.h"
#include "tagging/tagsstorage.h"
#include "platform/platform.h"
#include "experiences/experiencemanager.h"
#include "devices/devicemanagerimplementation.h"
#include "devices/device.h"
@ -172,6 +173,7 @@ void NymeaCore::init() {
m_cloudManager = new CloudManager(m_configuration, m_networkManager, this);
qCDebug(dcApplication()) << "Loading experiences";
new ExperienceManager(m_serverManager->jsonServer(), this);
CloudNotifications *cloudNotifications = m_cloudManager->createNotificationsPlugin();

View File

@ -30,6 +30,7 @@ Q_LOGGING_CATEGORY(dcSystem, "System")
Q_LOGGING_CATEGORY(dcPlatform, "Platform")
Q_LOGGING_CATEGORY(dcPlatformUpdate, "PlatformUpdate")
Q_LOGGING_CATEGORY(dcPlatformZeroConf, "PlatformZeroConf")
Q_LOGGING_CATEGORY(dcExperiences, "Experiences")
Q_LOGGING_CATEGORY(dcTimeManager, "TimeManager")
Q_LOGGING_CATEGORY(dcRuleEngine, "RuleEngine")
Q_LOGGING_CATEGORY(dcRuleEngineDebug, "RuleEngineDebug")

View File

@ -35,6 +35,7 @@ Q_DECLARE_LOGGING_CATEGORY(dcSystem)
Q_DECLARE_LOGGING_CATEGORY(dcPlatform)
Q_DECLARE_LOGGING_CATEGORY(dcPlatformUpdate)
Q_DECLARE_LOGGING_CATEGORY(dcPlatformZeroConf)
Q_DECLARE_LOGGING_CATEGORY(dcExperiences)
Q_DECLARE_LOGGING_CATEGORY(dcTimeManager)
Q_DECLARE_LOGGING_CATEGORY(dcRuleEngine)
Q_DECLARE_LOGGING_CATEGORY(dcRuleEngineDebug)