diff --git a/debian/changelog b/debian/changelog index a4c4cea2..b5079fdf 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,8 @@ +nymea (0.13.0) UNRELEASED; urgency=medium + + + -- Michael Zanetti Fri, 10 May 2019 12:18:54 +0200 + nymea (0.12.1) xenial; urgency=medium [ Michael Zanetti ] diff --git a/debian/control b/debian/control index b9ff6750..617d1184 100644 --- a/debian/control +++ b/debian/control @@ -68,7 +68,9 @@ Depends: libqt5network5, ${shlibs:Depends}, ${misc:Depends} Recommends: nymea-cli, - network-manager + network-manager, + nymea-update-plugin-impl, + nymea-system-plugin-impl, Replaces: guhd Description: An open source IoT server - daemon The nymea daemon is a plugin based IoT (Internet of Things) server. The @@ -131,7 +133,7 @@ Description: Translation files for nymead and plugins - translations Package: nymea-tests -Section: misc +Section: devel Architecture: any Multi-Arch: same Depends: nymea (= ${binary:Version}), diff --git a/libnymea-core/hardware/network/avahi/qtavahiservicebrowserimplementation_p.cpp b/libnymea-core/hardware/network/avahi/qtavahiservicebrowserimplementation_p.cpp index 26614345..d35a6a6c 100644 --- a/libnymea-core/hardware/network/avahi/qtavahiservicebrowserimplementation_p.cpp +++ b/libnymea-core/hardware/network/avahi/qtavahiservicebrowserimplementation_p.cpp @@ -28,6 +28,8 @@ #include #include +#include + namespace nymeaserver { QtAvahiServiceBrowserImplementationPrivate::QtAvahiServiceBrowserImplementationPrivate(QtAvahiClient *client) : diff --git a/libnymea-core/jsonrpc/jsonrpcserver.cpp b/libnymea-core/jsonrpc/jsonrpcserver.cpp index a4e1d37e..1bea5280 100644 --- a/libnymea-core/jsonrpc/jsonrpcserver.cpp +++ b/libnymea-core/jsonrpc/jsonrpcserver.cpp @@ -47,6 +47,7 @@ #include "rule.h" #include "ruleengine.h" #include "loggingcategories.h" +#include "platform/platform.h" #include "devicehandler.h" #include "actionhandler.h" @@ -57,6 +58,7 @@ #include "configurationhandler.h" #include "networkmanagerhandler.h" #include "tagshandler.h" +#include "systemhandler.h" #include #include @@ -518,6 +520,7 @@ void JsonRPCServer::setup() registerHandler(new ConfigurationHandler(this)); registerHandler(new NetworkManagerHandler(this)); registerHandler(new TagsHandler(this)); + registerHandler(new SystemHandler(NymeaCore::instance()->platform(), this)); connect(NymeaCore::instance()->cloudManager(), &CloudManager::pairingReply, this, &JsonRPCServer::pairingFinished); connect(NymeaCore::instance()->cloudManager(), &CloudManager::connectionStateChanged, this, &JsonRPCServer::onCloudConnectionStateChanged); @@ -684,7 +687,8 @@ void JsonRPCServer::sendNotification(const QVariantMap ¶ms) Q_ASSERT_X(handler->validateParams(method.name(), params).first, "validating return value", formatAssertion(handler->name(), method.name(), QMetaMethod::Signal, handler, notification).toLatin1().data()); QByteArray data = QJsonDocument::fromVariant(notification).toJson(QJsonDocument::Compact); - qCDebug(dcJsonRpcTraffic()) << "Sending notification:" << data; + qCDebug(dcJsonRpc()) << "Sending notification:" << handler->name() + "." + method.name(); + qCDebug(dcJsonRpcTraffic()) << "Notification content:" << data; foreach (const QUuid &clientId, m_clientNotifications.keys(true)) { m_clientTransports.value(clientId)->sendData(clientId, data); diff --git a/libnymea-core/jsonrpc/jsontypes.cpp b/libnymea-core/jsonrpc/jsontypes.cpp index ddf3e851..e7de6f8b 100644 --- a/libnymea-core/jsonrpc/jsontypes.cpp +++ b/libnymea-core/jsonrpc/jsontypes.cpp @@ -126,6 +126,8 @@ QVariantMap JsonTypes::s_serverConfiguration; QVariantMap JsonTypes::s_webServerConfiguration; QVariantMap JsonTypes::s_tag; QVariantMap JsonTypes::s_mqttPolicy; +QVariantMap JsonTypes::s_package; +QVariantMap JsonTypes::s_repository; void JsonTypes::init() { @@ -400,6 +402,20 @@ void JsonTypes::init() s_tag.insert("tagId", basicTypeToString(QVariant::String)); s_tag.insert("o:value", basicTypeToString(QVariant::String)); + s_package.insert("id", basicTypeToString(QVariant::String)); + s_package.insert("displayName", basicTypeToString(QVariant::String)); + s_package.insert("summary", basicTypeToString(QVariant::String)); + s_package.insert("installedVersion", basicTypeToString(QVariant::String)); + s_package.insert("candidateVersion", basicTypeToString(QVariant::String)); + s_package.insert("changelog", basicTypeToString(QVariant::String)); + s_package.insert("updateAvailable", basicTypeToString(QVariant::Bool)); + s_package.insert("rollbackAvailable", basicTypeToString(QVariant::Bool)); + s_package.insert("canRemove", basicTypeToString(QVariant::Bool)); + + s_repository.insert("id", basicTypeToString(QVariant::String)); + s_repository.insert("displayName", basicTypeToString(QVariant::String)); + s_repository.insert("enabled", basicTypeToString(QVariant::Bool)); + s_initialized = true; } @@ -482,6 +498,8 @@ QVariantMap JsonTypes::allTypes() allTypes.insert("WebServerConfiguration", serverConfigurationDescription()); allTypes.insert("Tag", tagDescription()); allTypes.insert("MqttPolicy", mqttPolicyDescription()); + allTypes.insert("Package", packageDescription()); + allTypes.insert("Repository", repositoryDescription()); return allTypes; } @@ -1268,6 +1286,30 @@ QVariantMap JsonTypes::packTokenInfo(const TokenInfo &tokenInfo) return ret; } +QVariantMap JsonTypes::packPackage(const Package &package) +{ + QVariantMap ret; + ret.insert("id", package.packageId()); + ret.insert("displayName", package.displayName()); + ret.insert("summary", package.summary()); + ret.insert("installedVersion", package.installedVersion()); + ret.insert("candidateVersion", package.candidateVersion()); + ret.insert("changelog", package.changelog()); + ret.insert("updateAvailable", package.updateAvailable()); + ret.insert("rollbackAvailable", package.rollbackAvailable()); + ret.insert("canRemove", package.canRemove()); + return ret; +} + +QVariantMap JsonTypes::packRepository(const Repository &repository) +{ + QVariantMap ret; + ret.insert("id", repository.id()); + ret.insert("displayName", repository.displayName()); + ret.insert("enabled", repository.enabled()); + return ret; +} + /*! Returns the type string for the given \a type. */ QString JsonTypes::basicTypeToString(const QVariant::Type &type) { @@ -2013,6 +2055,18 @@ QPair JsonTypes::validateVariant(const QVariant &templateVariant, qCWarning(dcJsonRpc) << "Tag not matching"; return result; } + } else if (refName == packageRef()) { + QPair result = validateMap(packageDescription(), variant.toMap()); + if (!result.first) { + qCWarning(dcJsonRpc) << "Package not matching"; + return result; + } + } else if (refName == repositoryRef()) { + QPair result = validateMap(repositoryDescription(), variant.toMap()); + if (!result.first) { + qCWarning(dcJsonRpc) << "Repository not matching"; + return result; + } } else if (refName == basicTypeRef()) { QPair result = validateBasicType(variant); if (!result.first) { diff --git a/libnymea-core/jsonrpc/jsontypes.h b/libnymea-core/jsonrpc/jsontypes.h index 6fc417eb..5be24709 100644 --- a/libnymea-core/jsonrpc/jsontypes.h +++ b/libnymea-core/jsonrpc/jsontypes.h @@ -57,6 +57,8 @@ #include "networkmanager/wirelessaccesspoint.h" #include "cloud/cloudmanager.h" +#include "platform/package.h" +#include "platform/repository.h" #include @@ -176,6 +178,8 @@ public: DECLARE_OBJECT(webServerConfiguration, "WebServerConfiguration") DECLARE_OBJECT(tag, "Tag") DECLARE_OBJECT(mqttPolicy, "MqttPolicy") + DECLARE_OBJECT(package, "Package") + DECLARE_OBJECT(repository, "Repository") // pack types static QVariantMap packEventType(const EventType &eventType, const PluginId &pluginId, const QLocale &locale); @@ -232,6 +236,9 @@ public: static QVariantMap packTokenInfo(const TokenInfo &tokenInfo); + static QVariantMap packPackage(const Package &package); + static QVariantMap packRepository(const Repository &repository); + static QString basicTypeToString(const QVariant::Type &type); // unpack Types diff --git a/libnymea-core/jsonrpc/logginghandler.cpp b/libnymea-core/jsonrpc/logginghandler.cpp index c50c6e61..abbeaadf 100644 --- a/libnymea-core/jsonrpc/logginghandler.cpp +++ b/libnymea-core/jsonrpc/logginghandler.cpp @@ -118,7 +118,6 @@ QString LoggingHandler::name() const void LoggingHandler::logEntryAdded(const LogEntry &logEntry) { - qCDebug(dcJsonRpc) << "Notify \"Logging.LogEntryAdded\""; QVariantMap params; params.insert("logEntry", JsonTypes::packLogEntry(logEntry)); emit LogEntryAdded(params); @@ -126,14 +125,11 @@ void LoggingHandler::logEntryAdded(const LogEntry &logEntry) void LoggingHandler::logDatabaseUpdated() { - qCDebug(dcJsonRpc) << "Notify \"Logging.LogDatabaseUpdated\""; emit LogDatabaseUpdated(QVariantMap()); } JsonReply* LoggingHandler::GetLogEntries(const QVariantMap ¶ms) const { - qCDebug(dcJsonRpc) << "Asked for log entries" << params; - LogFilter filter = JsonTypes::unpackLogFilter(params); QVariantList entries; diff --git a/libnymea-core/jsonrpc/systemhandler.cpp b/libnymea-core/jsonrpc/systemhandler.cpp new file mode 100644 index 00000000..3b16c84a --- /dev/null +++ b/libnymea-core/jsonrpc/systemhandler.cpp @@ -0,0 +1,338 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2019 Michael Zanetti * + * * + * This file is part of nymea. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library 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 * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; If not, see * + * . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "systemhandler.h" + +#include "platform/platform.h" +#include "platform/platformupdatecontroller.h" +#include "platform/platformsystemcontroller.h" + +namespace nymeaserver { + +SystemHandler::SystemHandler(Platform *platform, QObject *parent): + JsonHandler(parent), + m_platform(platform) +{ + // Methods + QVariantMap params; QVariantMap returns; + setDescription("GetCapabilities", "Get the list of capabilites on this system. This allows reading whether things like rebooting or shutting down the system running nymea:core is supported on this host."); + setParams("GetCapabilities", params); + returns.insert("powerManagement", JsonTypes::basicTypeToString(JsonTypes::Bool)); + returns.insert("updateManagement", JsonTypes::basicTypeToString(JsonTypes::Bool)); + setReturns("GetCapabilities", returns); + + params.clear(); returns.clear(); + setDescription("Reboot", "Initiate a reboot of the system. The return value will indicate whether the procedure has been initiated successfully."); + setParams("Reboot", params); + returns.insert("success", JsonTypes::basicTypeToString(JsonTypes::Bool)); + setReturns("Reboot", returns); + + params.clear(); returns.clear(); + setDescription("Shutdown", "Initiate a shutdown of the system. The return value will indicate whether the procedure has been initiated successfully."); + setParams("Shutdown", params); + returns.insert("success", JsonTypes::basicTypeToString(JsonTypes::Bool)); + setReturns("Shutdown", returns); + + params.clear(); returns.clear(); + setDescription("GetUpdateStatus", + "Get the current status of the update system. \"busy\" indicates that the system is current busy with " + "an operation regarding updates. This does not necessarily mean an actual update is running. When this " + "is true, update related functions on the client should be marked as busy and no interaction with update " + "components shall be allowed. An example for such a state is when the system queries the server if there " + "are updates available, typically after a call to CheckForUpdates. \"updateRunning\" on the other hand " + "indicates an actual update process is ongoing. The user should be informed about it, the system also " + "might restart at any point while an update is running."); + setParams("GetUpdateStatus", params); + returns.insert("busy", JsonTypes::basicTypeToString(JsonTypes::Bool)); + returns.insert("updateRunning", JsonTypes::basicTypeToString(JsonTypes::Bool)); + setReturns("GetUpdateStatus", returns); + + params.clear(); returns.clear(); + setDescription("CheckForUpdates", + "Instruct the system to poll the server for updates. Normally the system should automatically do this " + "in regular intervals, however, if the client wants to allow the user to manually check for new updates " + "now, this can be called. Returns true if the operation has been started successfully and the update " + "manager will become busy. In order to know whether there are updates available, clients should walk through " + "the list of packages retrieved from GetPackages and check whether there are packages with the updateAvailable " + "flag set to true."); + setParams("CheckForUpdates", params); + returns.insert("success", JsonTypes::basicTypeToString(JsonTypes::Bool)); + setReturns("CheckForUpdates", returns); + + params.clear(); returns.clear(); + setDescription("GetPackages", + "Get the list of packages currently available to the system. This might include installed available but " + "not installed packages. Installed packages will have the installedVersion set to a non-empty value."); + setParams("GetPackages", params); + returns.insert("packages", QVariantList() << JsonTypes::packageRef()); + setReturns("GetPackages", returns); + + params.clear(); returns.clear(); + setDescription("UpdatePackages", + "Starts updating/installing packages with the given ids. Returns true if the upgrade has been started " + "successfully. Note that it might still fail later. Before calling this method, clients should " + "check the packages whether they are in a state where they can either be installed (no installedVersion " + "set) or upgraded (updateAvailable set to true)."); + params.insert("o:packageIds", QVariantList() << JsonTypes::basicTypeToString(JsonTypes::String)); + setParams("UpdatePackages", params); + returns.insert("success", JsonTypes::basicTypeToString(JsonTypes::Bool)); + setReturns("UpdatePackages", returns); + + params.clear(); returns.clear(); + setDescription("RollbackPackages", + "Starts a rollback. Returns true if the rollback has been started successfully. Before calling this " + "method, clients should check whether the package can be rolled back (canRollback set to true)."); + params.insert("packageIds", QVariantList() << JsonTypes::basicTypeToString(JsonTypes::String)); + setParams("RollbackPackages", params); + returns.insert("success", JsonTypes::basicTypeToString(JsonTypes::Bool)); + setReturns("RollbackPackages", returns); + + params.clear(); returns.clear(); + setDescription("RemovePackages", + "Starts removing a package. Returns true if the removal has been started successfully. Before calling " + "this method, clients should check whether the package can be removed (canRemove set to true)."); + params.insert("packageIds", QVariantList() << JsonTypes::basicTypeToString(JsonTypes::String)); + setParams("RemovePackages", params); + returns.insert("success", JsonTypes::basicTypeToString(JsonTypes::Bool)); + setReturns("RemovePackages", returns); + + params.clear(); returns.clear(); + setDescription("GetRepositories", "Get the list of repositories currently available to the system."); + setParams("GetRepositories", params); + returns.insert("repositories", QVariantList() << JsonTypes::repositoryRef()); + setReturns("GetRepositories", returns); + + params.clear(); returns.clear(); + setDescription("EnableRepository", "Enable or disable a repository."); + params.insert("repositoryId", JsonTypes::basicTypeToString(JsonTypes::String)); + params.insert("enabled", JsonTypes::basicTypeToString(JsonTypes::Bool)); + setParams("EnableRepository", params); + returns.insert("success", JsonTypes::basicTypeToString(JsonTypes::Bool)); + setReturns("EnableRepository", returns); + + + // Notifications + params.clear(); + setDescription("CapabilitiesChanged", "Emitted whenever the system capabilities change."); + params.insert("powerManagement", JsonTypes::basicTypeToString(JsonTypes::Bool)); + params.insert("updateManagement", JsonTypes::basicTypeToString(JsonTypes::Bool)); + setParams("CapabilitiesChanged", params); + + params.clear(); + setDescription("UpdateStatusChanged", "Emitted whenever the update status changes."); + params.insert("busy", JsonTypes::basicTypeToString(JsonTypes::Bool)); + params.insert("updateRunning", JsonTypes::basicTypeToString(JsonTypes::Bool)); + setParams("UpdateStatusChanged", params); + + params.clear(); + setDescription("PackageAdded", "Emitted whenever a package is added to the list of packages."); + params.insert("package", JsonTypes::packageRef()); + setParams("PackageAdded", params); + + params.clear(); + setDescription("PackageChanged", "Emitted whenever a package in the list of packages changes."); + params.insert("package", JsonTypes::packageRef()); + setParams("PackageChanged", params); + + params.clear(); + setDescription("PackageRemoved", "Emitted whenever a package is removed from the list of packages."); + params.insert("packageId", JsonTypes::basicTypeToString(JsonTypes::String)); + setParams("PackageRemoved", params); + + params.clear(); + setDescription("RepositoryAdded", "Emitted whenever a repository is added to the list of repositories."); + params.insert("repository", JsonTypes::repositoryRef()); + setParams("RepositoryAdded", params); + + params.clear(); + setDescription("RepositoryChanged", "Emitted whenever a repository in the list of repositories changes."); + params.insert("repository", JsonTypes::repositoryRef()); + setParams("RepositoryChanged", params); + + params.clear(); + setDescription("RepositoryRemoved", "Emitted whenever a repository is removed from the list of repositories."); + params.insert("repositoryId", JsonTypes::basicTypeToString(JsonTypes::String)); + setParams("RepositoryRemoved", params); + + + connect(m_platform->systemController(), &PlatformSystemController::availableChanged, this, &SystemHandler::onCapabilitiesChanged); + connect(m_platform->updateController(), &PlatformUpdateController::availableChanged, this, &SystemHandler::onCapabilitiesChanged); + connect(m_platform->updateController(), &PlatformUpdateController::busyChanged, this, [this](){ + QVariantMap params; + params.insert("busy", m_platform->updateController()->busy()); + params.insert("updateRunning", m_platform->updateController()->updateRunning()); + emit UpdateStatusChanged(params); + }); + connect(m_platform->updateController(), &PlatformUpdateController::updateRunningChanged, this, [this](){ + QVariantMap params; + params.insert("busy", m_platform->updateController()->busy()); + params.insert("updateRunning", m_platform->updateController()->updateRunning()); + emit UpdateStatusChanged(params); + }); + connect(m_platform->updateController(), &PlatformUpdateController::packageAdded, this, [this](const Package &package){ + QVariantMap params; + params.insert("package", JsonTypes::packPackage(package)); + emit PackageAdded(params); + }); + connect(m_platform->updateController(), &PlatformUpdateController::packageChanged, this, [this](const Package &package){ + QVariantMap params; + params.insert("package", JsonTypes::packPackage(package)); + emit PackageChanged(params); + }); + connect(m_platform->updateController(), &PlatformUpdateController::packageRemoved, this, [this](const QString &packageId){ + QVariantMap params; + params.insert("packageId", packageId); + emit PackageRemoved(params); + }); + connect(m_platform->updateController(), &PlatformUpdateController::repositoryAdded, this, [this](const Repository &repository){ + QVariantMap params; + params.insert("repository", JsonTypes::packRepository(repository)); + emit RepositoryAdded(params); + }); + connect(m_platform->updateController(), &PlatformUpdateController::repositoryChanged, this, [this](const Repository &repository){ + QVariantMap params; + params.insert("repository", JsonTypes::packRepository(repository)); + emit RepositoryChanged(params); + }); + connect(m_platform->updateController(), &PlatformUpdateController::repositoryRemoved, this, [this](const QString &repositoryId){ + QVariantMap params; + params.insert("repositoryId", repositoryId); + emit RepositoryRemoved(params); + }); +} + +QString SystemHandler::name() const +{ + return "System"; +} + +JsonReply *SystemHandler::GetCapabilities(const QVariantMap ¶ms) +{ + Q_UNUSED(params) + QVariantMap data; + data.insert("powerManagement", m_platform->systemController()->powerManagementAvailable()); + data.insert("updateManagement", m_platform->updateController()->updateManagementAvailable()); + return createReply(data); +} + +JsonReply *SystemHandler::Reboot(const QVariantMap ¶ms) const +{ + Q_UNUSED(params); + bool status = m_platform->systemController()->reboot(); + QVariantMap returns; + returns.insert("success", status); + return createReply(returns); +} + +JsonReply *SystemHandler::Shutdown(const QVariantMap ¶ms) const +{ + Q_UNUSED(params); + bool status = m_platform->systemController()->shutdown(); + QVariantMap returns; + returns.insert("success", status); + return createReply(returns); +} + +JsonReply *SystemHandler::GetUpdateStatus(const QVariantMap ¶ms) const +{ + Q_UNUSED(params) + QVariantMap ret; + ret.insert("busy", m_platform->updateController()->updateRunning()); + ret.insert("updateRunning", m_platform->updateController()->updateRunning()); + return createReply(ret); +} + +JsonReply *SystemHandler::CheckForUpdates(const QVariantMap ¶ms) const +{ + Q_UNUSED(params) + QVariantMap ret; + bool success = m_platform->updateController()->checkForUpdates(); + ret.insert("success", success); + return createReply(ret); +} + +JsonReply *SystemHandler::GetPackages(const QVariantMap ¶ms) const +{ + Q_UNUSED(params) + QVariantList packagelist; + foreach (const Package &package, m_platform->updateController()->packages()) { + packagelist.append(JsonTypes::packPackage(package)); + } + QVariantMap returns; + returns.insert("packages", packagelist); + return createReply(returns); +} + +JsonReply *SystemHandler::UpdatePackages(const QVariantMap ¶ms) const +{ + bool success = m_platform->updateController()->startUpdate(params.value("packageIds").toStringList()); + QVariantMap returns; + returns.insert("success", success); + return createReply(returns); +} + +JsonReply *SystemHandler::RollbackPackages(const QVariantMap ¶ms) const +{ + bool success = m_platform->updateController()->rollback(params.value("packageIds").toStringList()); + QVariantMap returns; + returns.insert("success", success); + return createReply(returns); +} + +JsonReply *SystemHandler::RemovePackages(const QVariantMap ¶ms) const +{ + bool success = m_platform->updateController()->removePackages(params.value("packageIds").toStringList()); + QVariantMap returns; + returns.insert("success", success); + return createReply(returns); +} + +JsonReply *SystemHandler::GetRepositories(const QVariantMap ¶ms) const +{ + Q_UNUSED(params); + QVariantList repos; + foreach (const Repository &repository, m_platform->updateController()->repositories()) { + repos.append(JsonTypes::packRepository(repository)); + } + QVariantMap returns; + returns.insert("repositories", repos); + return createReply(returns); + +} + +JsonReply *SystemHandler::EnableRepository(const QVariantMap ¶ms) const +{ + bool success = m_platform->updateController()->enableRepository(params.value("repositoryId").toString(), params.value("enabled").toBool()); + QVariantMap returns; + returns.insert("success", success); + return createReply(returns); +} + +void SystemHandler::onCapabilitiesChanged() +{ + QVariantMap caps; + caps.insert("powerManagement", m_platform->systemController()->powerManagementAvailable()); + caps.insert("updateManagement", m_platform->updateController()->updateManagementAvailable()); + emit CapabilitiesChanged(caps); +} + +} diff --git a/libnymea-core/jsonrpc/systemhandler.h b/libnymea-core/jsonrpc/systemhandler.h new file mode 100644 index 00000000..36dc09df --- /dev/null +++ b/libnymea-core/jsonrpc/systemhandler.h @@ -0,0 +1,73 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2019 Michael Zanetti * + * * + * This file is part of nymea. * + * * + * nymea 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, version 2 of the License. * + * * + * nymea 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 for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with nymea. If not, see . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef SYSTEMHANDLER_H +#define SYSTEMHANDLER_H + +#include + +#include "jsonhandler.h" + +#include "platform/platform.h" + +namespace nymeaserver { + +class SystemHandler : public JsonHandler +{ + Q_OBJECT +public: + explicit SystemHandler(Platform *platform, QObject *parent = nullptr); + + QString name() const override; + + Q_INVOKABLE JsonReply *GetCapabilities(const QVariantMap ¶ms); + + Q_INVOKABLE JsonReply *Reboot(const QVariantMap ¶ms) const; + Q_INVOKABLE JsonReply *Shutdown(const QVariantMap ¶ms) const; + + Q_INVOKABLE JsonReply *GetUpdateStatus(const QVariantMap ¶ms) const; + Q_INVOKABLE JsonReply *CheckForUpdates(const QVariantMap ¶ms) const; + Q_INVOKABLE JsonReply *GetPackages(const QVariantMap ¶ms) const; + Q_INVOKABLE JsonReply *UpdatePackages(const QVariantMap ¶ms) const; + Q_INVOKABLE JsonReply *RollbackPackages(const QVariantMap ¶ms) const; + Q_INVOKABLE JsonReply *RemovePackages(const QVariantMap ¶ms) const; + Q_INVOKABLE JsonReply *GetRepositories(const QVariantMap ¶ms) const; + Q_INVOKABLE JsonReply *EnableRepository(const QVariantMap ¶ms) const; + +signals: + void CapabilitiesChanged(const QVariantMap ¶ms); + void UpdateStatusChanged(const QVariantMap ¶ms); + void PackageAdded(const QVariantMap ¶ms); + void PackageChanged(const QVariantMap ¶ms); + void PackageRemoved(const QVariantMap ¶ms); + void RepositoryAdded(const QVariantMap ¶ms); + void RepositoryChanged(const QVariantMap ¶ms); + void RepositoryRemoved(const QVariantMap ¶ms); + +private slots: + void onCapabilitiesChanged(); + +private: + Platform *m_platform = nullptr; +}; + +} + +#endif // SYSTEMHANDLER_H diff --git a/libnymea-core/libnymea-core.pro b/libnymea-core/libnymea-core.pro index b7a4adf1..b66bc2c6 100644 --- a/libnymea-core/libnymea-core.pro +++ b/libnymea-core/libnymea-core.pro @@ -99,7 +99,9 @@ HEADERS += nymeacore.h \ tagging/tag.h \ jsonrpc/tagshandler.h \ cloud/cloudtransport.h \ - debugreportgenerator.h + debugreportgenerator.h \ + platform/platform.h \ + jsonrpc/systemhandler.h SOURCES += nymeacore.cpp \ ruleengine.cpp \ @@ -184,3 +186,5 @@ SOURCES += nymeacore.cpp \ jsonrpc/tagshandler.cpp \ cloud/cloudtransport.cpp \ debugreportgenerator.cpp \ + platform/platform.cpp \ + jsonrpc/systemhandler.cpp diff --git a/libnymea-core/nymeacore.cpp b/libnymea-core/nymeacore.cpp index 149fd3c1..e7d8ee2f 100644 --- a/libnymea-core/nymeacore.cpp +++ b/libnymea-core/nymeacore.cpp @@ -106,11 +106,13 @@ #include "nymeacore.h" #include "loggingcategories.h" +#include "platform/platform.h" #include "jsonrpc/jsonrpcserver.h" #include "ruleengine.h" #include "networkmanager/networkmanager.h" #include "nymeasettings.h" #include "tagging/tagsstorage.h" +#include "platform/platform.h" #include "devicemanager.h" #include "plugin/device.h" @@ -143,6 +145,9 @@ NymeaCore::NymeaCore(QObject *parent) : void NymeaCore::init() { qCDebug(dcApplication()) << "Initializing NymeaCore"; + qCDebug(dcPlatform()) << "Loading platform abstraction"; + m_platform = new Platform(this); + qCDebug(dcApplication()) << "Loading nymea configurations" << NymeaSettings(NymeaSettings::SettingsRoleGlobal).fileName(); m_configuration = new NymeaConfiguration(this); @@ -660,6 +665,13 @@ TagsStorage *NymeaCore::tagsStorage() const return m_tagsStorage; } +/*! Returns a pointer to the \l{Platform} instance owned by NymeaCore. + The Platform represents the host system this nymea instance is running on. +*/ +Platform *NymeaCore::platform() const +{ + return m_platform; +} /*! Connected to the DeviceManager's emitEvent signal. Events received in diff --git a/libnymea-core/nymeacore.h b/libnymea-core/nymeacore.h index 7911e5d9..4c1be9ec 100644 --- a/libnymea-core/nymeacore.h +++ b/libnymea-core/nymeacore.h @@ -51,6 +51,8 @@ class NetworkManager; class NymeaConfiguration; class TagsStorage; class UserManager; +class Platform; +class System; class NymeaCore : public QObject { @@ -88,6 +90,7 @@ public: CloudManager *cloudManager() const; DebugServerHandler *debugServerHandler() const; TagsStorage *tagsStorage() const; + Platform *platform() const; static QStringList getAvailableLanguages(); @@ -116,6 +119,8 @@ private: explicit NymeaCore(QObject *parent = nullptr); static NymeaCore *s_instance; + Platform *m_platform = nullptr; + NymeaConfiguration *m_configuration; ServerManager *m_serverManager; DeviceManager *m_deviceManager; @@ -129,6 +134,7 @@ private: NetworkManager *m_networkManager; UserManager *m_userManager; + System *m_system; QHash m_pendingActions; QList m_executingRules; diff --git a/libnymea-core/platform/platform.cpp b/libnymea-core/platform/platform.cpp new file mode 100644 index 00000000..4a5c8b05 --- /dev/null +++ b/libnymea-core/platform/platform.cpp @@ -0,0 +1,141 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2019 Michael Zanetti * + * * + * This file is part of nymea. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library 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 * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; If not, see * + * . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "platform.h" +#include "platform/platformsystemcontroller.h" +#include "platform/platformupdatecontroller.h" + +#include "loggingcategories.h" + +#include +#include +#include + +namespace nymeaserver { + +Platform::Platform(QObject *parent) : QObject(parent) +{ + foreach (const QString &path, pluginSearchDirs()) { + QDir dir(path); + qCDebug(dcPlatform) << "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_systemplugin") && entry.endsWith(".so")) { + loadSystemPlugin(path + "/" + entry); + } else if (entry.startsWith("libnymea_updateplugin") && entry.endsWith(".so")) { + loadUpdatePlugin(path + "/" + entry); + } + } else if (fi.isDir()) { + if (QFileInfo::exists(path + "/" + entry + "/libnymea_systemplugin" + entry + ".so")) { + loadSystemPlugin(path + "/" + entry + "/libnymea_systemplugin" + entry + ".so"); + } else if (QFileInfo::exists(path + "/" + entry + "/libnymea_updateplugin" + entry + ".so")) { + loadUpdatePlugin(path + "/" + entry + "/libnymea_updateplugin" + entry + ".so"); + } + } + } + if (m_platformSystemController && m_platformUpdateController) { + break; + } + } + if (!m_platformSystemController) { + qCWarning(dcPlatform()) << "Could not load a system plugin. System control features won't be available."; + m_platformSystemController = new PlatformSystemController(this); + } + if (!m_platformUpdateController) { + qCWarning(dcPlatform()) << "Could not load an update plugin. System update features won't be available."; + m_platformUpdateController = new PlatformUpdateController(this); + } +} + +PlatformSystemController *Platform::systemController() const +{ + return m_platformSystemController; +} + +PlatformUpdateController *Platform::updateController() const +{ + return m_platformUpdateController; +} + +QStringList Platform::pluginSearchDirs() const +{ + QStringList searchDirs; + QByteArray envPath = qgetenv("NYMEA_PLATFORM_PLUGINS_PATH"); + if (!envPath.isEmpty()) { + searchDirs << QString(envPath).split(':'); + } + + foreach (QString libraryPath, QCoreApplication::libraryPaths()) { + searchDirs << libraryPath.replace("qt5", "nymea").replace("plugins", "platform"); + } + searchDirs << QCoreApplication::applicationDirPath() + "/../lib/nymea/platform"; + searchDirs << QCoreApplication::applicationDirPath() + "/../platform/"; + searchDirs << QCoreApplication::applicationDirPath() + "/../../../platform/"; + return searchDirs; +} + +void Platform::loadSystemPlugin(const QString &file) +{ + if (m_platformSystemController) { + return; // Not loading another... + } + QPluginLoader loader; + loader.setFileName(file); + loader.setLoadHints(QLibrary::ResolveAllSymbolsHint); + if (!loader.load()) { + qCWarning(dcPlatform) << "Could not load plugin data of" << file << "\n" << loader.errorString(); + return; + } + m_platformSystemController = qobject_cast(loader.instance()); + if (!m_platformSystemController) { + qCWarning(dcPlatform) << "Could not get plugin instance of" << file; + loader.unload(); + return; + } + qCDebug(dcPlatform()) << "Loaded system plugin:" << file; + m_platformSystemController->setParent(this); +} + +void Platform::loadUpdatePlugin(const QString &file) +{ + if (m_platformUpdateController) { + return; // Not loading another... + } + QPluginLoader loader; + loader.setFileName(file); + loader.setLoadHints(QLibrary::ResolveAllSymbolsHint); + if (!loader.load()) { + qCWarning(dcPlatform) << "Could not load plugin data of" << file << "\n" << loader.errorString(); + return; + } + m_platformUpdateController = qobject_cast(loader.instance()); + if (!m_platformUpdateController) { + qCWarning(dcPlatform) << "Could not get plugin instance of" << file; + loader.unload(); + return; + } + qCDebug(dcPlatform()) << "Loaded update plugin:" << file; + m_platformUpdateController->setParent(this); +} + +} diff --git a/libnymea-core/platform/platform.h b/libnymea-core/platform/platform.h new file mode 100644 index 00000000..2b8657b7 --- /dev/null +++ b/libnymea-core/platform/platform.h @@ -0,0 +1,55 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2019 Michael Zanetti * + * * + * This file is part of nymea. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library 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 * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; If not, see * + * . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef PLATFORM_H +#define PLATFORM_H + +#include + +class PlatformSystemController; +class PlatformUpdateController; + +namespace nymeaserver { + +class Platform : public QObject +{ + Q_OBJECT +public: + explicit Platform(QObject *parent = nullptr); + + PlatformSystemController *systemController() const; + PlatformUpdateController *updateController() const; + +private: + QStringList pluginSearchDirs() const; + + void loadSystemPlugin(const QString &file); + void loadUpdatePlugin(const QString &file); + +private: + PlatformSystemController *m_platformSystemController = nullptr; + PlatformUpdateController *m_platformUpdateController = nullptr; +}; + +} + +#endif // PLATFORM_H diff --git a/libnymea/coap/coap.h b/libnymea/coap/coap.h index 568b1030..cea66042 100644 --- a/libnymea/coap/coap.h +++ b/libnymea/coap/coap.h @@ -43,8 +43,6 @@ * */ -Q_DECLARE_LOGGING_CATEGORY(dcCoap) - class LIBNYMEA_EXPORT Coap : public QObject { Q_OBJECT diff --git a/libnymea/libnymea.pro b/libnymea/libnymea.pro index 0324f856..9c3f4bdd 100644 --- a/libnymea/libnymea.pro +++ b/libnymea/libnymea.pro @@ -12,6 +12,8 @@ LIBS += -lavahi-common -lavahi-client HEADERS += devicemanager.h \ libnymea.h \ + platform/package.h \ + platform/repository.h \ typeutils.h \ loggingcategories.h \ nymeasettings.h \ @@ -65,11 +67,15 @@ HEADERS += devicemanager.h \ nymeadbusservice.h \ network/mqtt/mqttprovider.h \ network/mqtt/mqttchannel.h \ - translator.h + translator.h \ + platform/platformsystemcontroller.h \ + platform/platformupdatecontroller.h SOURCES += devicemanager.cpp \ loggingcategories.cpp \ nymeasettings.cpp \ + platform/package.cpp \ + platform/repository.cpp \ plugin/device.cpp \ plugin/deviceplugin.cpp \ plugin/devicedescriptor.cpp \ @@ -120,7 +126,9 @@ SOURCES += devicemanager.cpp \ nymeadbusservice.cpp \ network/mqtt/mqttprovider.cpp \ network/mqtt/mqttchannel.cpp \ - translator.cpp + translator.cpp \ + platform/platformsystemcontroller.cpp \ + platform/platformupdatecontroller.cpp RESOURCES += \ diff --git a/libnymea/loggingcategories.cpp b/libnymea/loggingcategories.cpp index 80208bec..657174a2 100644 --- a/libnymea/loggingcategories.cpp +++ b/libnymea/loggingcategories.cpp @@ -24,6 +24,9 @@ Q_LOGGING_CATEGORY(dcApplication, "Application") Q_LOGGING_CATEGORY(dcDeviceManager, "DeviceManager") +Q_LOGGING_CATEGORY(dcSystem, "System") +Q_LOGGING_CATEGORY(dcPlatform, "Platform") +Q_LOGGING_CATEGORY(dcPlatformUpdate, "PlatformUpdate") Q_LOGGING_CATEGORY(dcTimeManager, "TimeManager") Q_LOGGING_CATEGORY(dcRuleEngine, "RuleEngine") Q_LOGGING_CATEGORY(dcRuleEngineDebug, "RuleEngineDebug") diff --git a/libnymea/loggingcategories.h b/libnymea/loggingcategories.h index 6c5f4c43..d9c8ef42 100644 --- a/libnymea/loggingcategories.h +++ b/libnymea/loggingcategories.h @@ -26,12 +26,12 @@ #include #include -// Include dcCoap -#include "coap/coap.h" - // Core / libnymea Q_DECLARE_LOGGING_CATEGORY(dcApplication) Q_DECLARE_LOGGING_CATEGORY(dcDeviceManager) +Q_DECLARE_LOGGING_CATEGORY(dcSystem) +Q_DECLARE_LOGGING_CATEGORY(dcPlatform) +Q_DECLARE_LOGGING_CATEGORY(dcPlatformUpdate) Q_DECLARE_LOGGING_CATEGORY(dcTimeManager) Q_DECLARE_LOGGING_CATEGORY(dcRuleEngine) Q_DECLARE_LOGGING_CATEGORY(dcRuleEngineDebug) @@ -63,5 +63,6 @@ Q_DECLARE_LOGGING_CATEGORY(dcBluetoothServer) Q_DECLARE_LOGGING_CATEGORY(dcBluetoothServerTraffic) Q_DECLARE_LOGGING_CATEGORY(dcMqtt) Q_DECLARE_LOGGING_CATEGORY(dcTranslations) +Q_DECLARE_LOGGING_CATEGORY(dcCoap) #endif // LOGGINGCATEGORYS_H diff --git a/libnymea/platform/package.cpp b/libnymea/platform/package.cpp new file mode 100644 index 00000000..195205ec --- /dev/null +++ b/libnymea/platform/package.cpp @@ -0,0 +1,131 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2019 Michael Zanetti * + * * + * This file is part of nymea. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library 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 * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; If not, see * + * . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "package.h" + +Package::Package(const QString &packageId, const QString &displayName, const QString &installedVersion, const QString &candidateVersion, const QString &changelog): + m_packageId(packageId), + m_displayName(displayName), + m_installedVersion(installedVersion), + m_candidateVersion(candidateVersion), + m_changeLog(changelog) +{ + +} + +QString Package::packageId() const +{ + return m_packageId; +} + +QString Package::displayName() const +{ + return m_displayName; +} + +QString Package::summary() const +{ + return m_summary; +} + +void Package::setSummary(const QString &summary) +{ + m_summary = summary; +} + +QString Package::installedVersion() const +{ + return m_installedVersion; +} + +void Package::setInstalledVersion(const QString &installedVersion) +{ + m_installedVersion = installedVersion; +} + +QString Package::candidateVersion() const +{ + return m_candidateVersion; +} + +void Package::setCandidateVersion(const QString &candidateVersion) +{ + m_candidateVersion = candidateVersion; +} + +QString Package::changelog() const +{ + return m_changeLog; +} + +void Package::setChangelog(const QString &changelog) +{ + m_changeLog = changelog; +} + +bool Package::updateAvailable() const +{ + return m_updateAvailable; +} + +void Package::setUpdateAvailable(bool updateAvailable) +{ + m_updateAvailable = updateAvailable; +} + +bool Package::rollbackAvailable() const +{ + return m_rollbackAvailable; +} + +void Package::setRollbackAvailable(bool rollbackAvailable) +{ + m_rollbackAvailable = rollbackAvailable; +} + +bool Package::canRemove() const +{ + return m_canRemove; +} + +void Package::setCanRemove(bool canRemove) +{ + m_canRemove = canRemove; +} + +bool Package::operator==(const Package &other) const +{ + return m_packageId == other.packageId() && + m_displayName == other.displayName() && + m_summary == other.summary() && + m_installedVersion == other.installedVersion() && + m_candidateVersion == other.candidateVersion() && + m_changeLog == other.changelog() && + m_updateAvailable == other.updateAvailable() && + m_rollbackAvailable == other.rollbackAvailable() && + m_canRemove == other.canRemove(); +} + +bool Package::operator!=(const Package &other) const +{ + return !operator==(other); +} diff --git a/libnymea/platform/package.h b/libnymea/platform/package.h new file mode 100644 index 00000000..a0c90ab1 --- /dev/null +++ b/libnymea/platform/package.h @@ -0,0 +1,73 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2019 Michael Zanetti * + * * + * This file is part of nymea. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library 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 * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; If not, see * + * . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef PACKAGE_H +#define PACKAGE_H + +#include + +class Package +{ +public: + explicit Package(const QString &packageId = QString(), const QString &displayName = QString(), const QString &installedVersion = QString(), const QString &candidateVersion = QString(), const QString &changelog = QString()); + + QString packageId() const; + QString displayName() const; + + QString summary() const; + void setSummary(const QString &summary); + + QString installedVersion() const; + void setInstalledVersion(const QString &installedVersion); + + QString candidateVersion() const; + void setCandidateVersion(const QString &candidateVersion); + + QString changelog() const; + void setChangelog(const QString &changelog); + + bool updateAvailable() const; + void setUpdateAvailable(bool updateAvailable); + + bool rollbackAvailable() const; + void setRollbackAvailable(bool rollbackAvailable); + + bool canRemove() const; + void setCanRemove(bool canRemove); + + bool operator==(const Package &other) const; + bool operator!=(const Package &other) const; + +private: + QString m_packageId; + QString m_displayName; + QString m_summary; + QString m_installedVersion; + QString m_candidateVersion; + QString m_changeLog; + + bool m_updateAvailable = false; + bool m_rollbackAvailable = false; + bool m_canRemove = false; +}; + +#endif // PACKAGE_H diff --git a/libnymea/platform/platformsystemcontroller.cpp b/libnymea/platform/platformsystemcontroller.cpp new file mode 100644 index 00000000..dda77859 --- /dev/null +++ b/libnymea/platform/platformsystemcontroller.cpp @@ -0,0 +1,43 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2019 Michael Zanetti * + * * + * This file is part of nymea. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library 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 * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; If not, see * + * . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "platformsystemcontroller.h" + +PlatformSystemController::PlatformSystemController(QObject *parent) : QObject(parent) +{ + +} + +bool PlatformSystemController::powerManagementAvailable() const +{ + return false; +} + +bool PlatformSystemController::reboot() +{ + return false; +} + +bool PlatformSystemController::shutdown() +{ + return false; +} diff --git a/libnymea/platform/platformsystemcontroller.h b/libnymea/platform/platformsystemcontroller.h new file mode 100644 index 00000000..a1de5fa9 --- /dev/null +++ b/libnymea/platform/platformsystemcontroller.h @@ -0,0 +1,45 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2019 Michael Zanetti * + * * + * This file is part of nymea. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library 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 * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; If not, see * + * . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef PLATFORMSYSTEMCONTROLLER_H +#define PLATFORMSYSTEMCONTROLLER_H + +#include + +class PlatformSystemController : public QObject +{ + Q_OBJECT +public: + explicit PlatformSystemController(QObject *parent = nullptr); + virtual ~PlatformSystemController() = default; + + virtual bool powerManagementAvailable() const; + virtual bool reboot(); + virtual bool shutdown(); + +signals: + void availableChanged(); +}; + +Q_DECLARE_INTERFACE(PlatformSystemController, "io.nymea.PlatformSystemController") + +#endif // PLATFORMSYSTEMCONTROLLER_H diff --git a/libnymea/platform/platformupdatecontroller.cpp b/libnymea/platform/platformupdatecontroller.cpp new file mode 100644 index 00000000..00c997cc --- /dev/null +++ b/libnymea/platform/platformupdatecontroller.cpp @@ -0,0 +1,164 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2019 Michael Zanetti * + * * + * This file is part of nymea. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library 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 * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; If not, see * + * . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "platformupdatecontroller.h" + +PlatformUpdateController::PlatformUpdateController(QObject *parent) : QObject(parent) +{ + +} + +/*! Whether or not the update management is available. Returns true if the system is ready + to perform any upgrade tasks. When the return value is true, it is assumed that the + system is capable of updating and nymea has permissions to do so. + + A backend plugin should override this to indicate whether update management is available. Defaults to false. + If the update mechanism becomes available during runtime, \l{availableChanged()} should be emitted + to indicate the change. + */ +bool PlatformUpdateController::updateManagementAvailable() const +{ + return false; +} + +/*! Triggers a cache update on the update system, e.g. call "apt-get update" on the system. + + A backend plugin may override this to to allow the system triggering package manager cache updates. + */ +bool PlatformUpdateController::checkForUpdates() +{ + return false; +} + +/*! Indicates whether the update system is busy. This might be the case when \l{checkForUpdates()} or + \l{enableRepository()} are called. In general, this indicates a state where the update system + is working and might not be ready to take additional requests. Clients should not perform any operations + on the upgrade system while this is true. + + A backend plugin may override this and return true when appropriate. For instance if \l{checkForUpdates()} + is a long-running task and the client should be informed about it being worked on. Additionally, + whenever the value changes \l{busyChanged()} should be emitted. + */ +bool PlatformUpdateController::busy() const +{ + return false; +} + +/*! Indicates whether an update is running. While this is true, clients should not perform + any operations on the upgrade system and in general expect the system to go down for + restart any moment. This flag can be used by clients to indicate the running update + and may restrict interaction with the system. + + A backend plugin should override this and return true when an update is running. Additionally. + whenever the value changes, \l{updateRunningChanged()} should be emitted. + */ +bool PlatformUpdateController::updateRunning() const +{ + return false; +} + +/*! Returns a list of packages availabe in the system. If a backend supports installation of new packages, + the list of packages may contain not installed packages. Such packages are marked by + returning an empty \l{Package::installedVersion()}. If the backend supports removal + of installed packages, uninstallable packages are marked with \l{Package::canRemove()}. + Packages that can be updated or rolled back will be marked with \l{Package::updateAvailable()} + and \l{Package::rollbackAvailable()}. + + A backend plugin should override this and return the list of all packages available in the system + filling in \l{Package} details as described above. + + */ +QList PlatformUpdateController::packages() const +{ + return {}; +} + +/*! Returns a list of all optional repositories available in the system. Such repositories + can be enabled or disabled in order to follow prereleases. + + A backend plugin may override this if enabling different repositories is supported. + */ +QList PlatformUpdateController::repositories() const +{ + return {}; +} + +/*! Starts upgrading the packages with the given \a packageIds. If \a packageIds is an empty + list, a full system upgrade will be performed. Use \l {Package::updateAvailable()} + to find packages with available upgrades. Passing packageIds for packages which are not + installed at this time will install the packages, provided the backend supports package + installation. If \l{canInstallPackages()} is false, only installed packages should + be provided for upgrades. The return value indicates whether the update has been started or not. + + A backend plugin should override this and start the system upgrades indicating success in + the return value. Additionally, once the update procedure has started, it should emit + \l {updateRunningChanged()} and \l {updateRunning()} should return true. + */ +bool PlatformUpdateController::startUpdate(const QStringList &packageIds) +{ + Q_UNUSED(packageIds) + return false; +} + +/*! Starts a rollback of the packages with the given \a packageIds. Use \l {Package::rollbackAvailable()} + to find packages with available rollbacks. + + A backend plugin should override this and start the rollback process indicating success in + the return value. Once the rollback procedure has started, it should emit + \l {updateRunningChanged()} and \l {updateRunning()} should return true. + */ +bool PlatformUpdateController::rollback(const QStringList &packageIds) +{ + Q_UNUSED(packageIds) + return false; +} + +/*! Starts the removal of the packages with the given \a packageIds. Use \l {Package::installedVersion()} + to find packages which are currently installed. Check \l{Package::canRemove()} to see whether a package + can be removed or not. + + A backend plugin should override this and start the removal process indicating success in + the return value. Once the removal procedure has started, it should emit + \l {updateRunningChanged()} and \l {updateRunning()} should return true. + */ +bool PlatformUpdateController::removePackages(const QStringList &packageIds) +{ + Q_UNUSED(packageIds) + return false; +} + +/*! Enables or disables the given \l{Repository} in the system. The return value indicates whether + the request has been started successfully or not. Upon successful completion of the request, + \l{repositoryChanged()} signals should be emitted. + + A backend plugin should override this if multiple install repositories are supported (e.g. if + \l{repositories()} returns a non-empty list) and perform the requested operation. If enabling/disabling + a repository implies the immediate upgrade/downgrade of packages, \l{updateRunning()} should be + marked accordingly. + */ +bool PlatformUpdateController::enableRepository(const QString &repositoryId, bool enabled) +{ + Q_UNUSED(repositoryId) + Q_UNUSED(enabled) + return false; +} + diff --git a/libnymea/platform/platformupdatecontroller.h b/libnymea/platform/platformupdatecontroller.h new file mode 100644 index 00000000..d63a1622 --- /dev/null +++ b/libnymea/platform/platformupdatecontroller.h @@ -0,0 +1,67 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2019 Michael Zanetti * + * * + * This file is part of nymea. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library 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 * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; If not, see * + * . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef PLATFORMUPDATECONTROLLER_H +#define PLATFORMUPDATECONTROLLER_H + +#include "package.h" +#include "repository.h" + +#include + +class PlatformUpdateController : public QObject +{ + Q_OBJECT +public: + explicit PlatformUpdateController(QObject *parent = nullptr); + virtual ~PlatformUpdateController() = default; + + virtual bool updateManagementAvailable() const; + + virtual bool checkForUpdates(); + virtual bool busy() const; + virtual bool updateRunning() const; + + virtual QList packages() const; + virtual QList repositories() const; + + virtual bool startUpdate(const QStringList &packageIds = QStringList()); + virtual bool rollback(const QStringList &packageIds); + virtual bool removePackages(const QStringList &packageIds); + + virtual bool enableRepository(const QString &repositoryId, bool enabled); + +signals: + void availableChanged(); + void busyChanged(); + void updateRunningChanged(); + void packageAdded(const Package &pacakge); + void packageChanged(const Package &package); + void packageRemoved(const QString &packageId); + void repositoryAdded(const Repository &repository); + void repositoryChanged(const Repository &repository); + void repositoryRemoved(const QString &repositoryId); +}; + +Q_DECLARE_INTERFACE(PlatformUpdateController, "io.nymea.PlatformUpdateController") + +#endif // PLATFORMUPDATECONTROLLER_H diff --git a/libnymea/platform/repository.cpp b/libnymea/platform/repository.cpp new file mode 100644 index 00000000..c3eeac53 --- /dev/null +++ b/libnymea/platform/repository.cpp @@ -0,0 +1,56 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2019 Michael Zanetti * + * * + * This file is part of nymea. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library 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 * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; If not, see * + * . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "repository.h" + +Repository::Repository(const QString &id, const QString &displayName, bool enabled): + m_id(id), + m_displayName(displayName), + m_enabled(enabled) +{ + +} + +Repository::Repository() +{ + +} + +QString Repository::id() const +{ + return m_id; +} + +QString Repository::displayName() const +{ + return m_displayName; +} + +bool Repository::enabled() const +{ + return m_enabled; +} + +void Repository::setEnabled(bool enabled) +{ + m_enabled = enabled; +} diff --git a/libnymea/platform/repository.h b/libnymea/platform/repository.h new file mode 100644 index 00000000..5ec2b86b --- /dev/null +++ b/libnymea/platform/repository.h @@ -0,0 +1,46 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2019 Michael Zanetti * + * * + * This file is part of nymea. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library 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 * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; If not, see * + * . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef REPOSITORY_H +#define REPOSITORY_H + +#include + +class Repository +{ +public: + Repository(); + Repository(const QString &id, const QString &displayName, bool enabled); + + QString id() const; + QString displayName() const; + + bool enabled() const; + void setEnabled(bool enabled); + +private: + QString m_id; + QString m_displayName; + bool m_enabled = false; +}; + +#endif // REPOSITORY_H diff --git a/nymea.pri b/nymea.pri index 3a1a78eb..914914a5 100644 --- a/nymea.pri +++ b/nymea.pri @@ -3,7 +3,7 @@ NYMEA_VERSION_STRING=$$system('dpkg-parsechangelog | sed -n -e "s/^Version: //p" # define protocol versions JSON_PROTOCOL_VERSION_MAJOR=2 -JSON_PROTOCOL_VERSION_MINOR=0 +JSON_PROTOCOL_VERSION_MINOR=1 REST_API_VERSION=1 COPYRIGHT_YEAR_FROM=2013 diff --git a/server/main.cpp b/server/main.cpp index f333cd63..1633fc9a 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -99,8 +99,11 @@ int main(int argc, char *argv[]) // logging filers for core and libnymea QStringList loggingFilters = { - "Application", "Warnings", + "Application", + "System", + "Platform", + "PlatformUpdate", "DeviceManager", "RuleEngine", "RuleEngineDebug", diff --git a/tests/auto/api.json b/tests/auto/api.json index 70bb436e..1a8639c6 100644 --- a/tests/auto/api.json +++ b/tests/auto/api.json @@ -1,4 +1,4 @@ -2.0 +2.1 { "methods": { "Actions.ExecuteAction": { @@ -867,6 +867,111 @@ "o:stateType": "$ref:StateType" } }, + "System.CheckForUpdates": { + "description": "Instruct the system to poll the server for updates. Normally the system should automatically do this in regular intervals, however, if the client wants to allow the user to manually check for new updates now, this can be called. Returns true if the operation has been started successfully and the update manager will become busy. In order to know whether there are updates available, clients should walk through the list of packages retrieved from GetPackages and check whether there are packages with the updateAvailable flag set to true.", + "params": { + }, + "returns": { + "success": "Bool" + } + }, + "System.EnableRepository": { + "description": "Enable or disable a repository.", + "params": { + "enabled": "Bool", + "repositoryId": "String" + }, + "returns": { + "success": "Bool" + } + }, + "System.GetCapabilities": { + "description": "Get the list of capabilites on this system. This allows reading whether things like rebooting or shutting down the system running nymea:core is supported on this host.", + "params": { + }, + "returns": { + "powerManagement": "Bool", + "updateManagement": "Bool" + } + }, + "System.GetPackages": { + "description": "Get the list of packages currently available to the system. This might include installed available but not installed packages. Installed packages will have the installedVersion set to a non-empty value.", + "params": { + }, + "returns": { + "packages": [ + "$ref:Package" + ] + } + }, + "System.GetRepositories": { + "description": "Get the list of repositories currently available to the system.", + "params": { + }, + "returns": { + "repositories": [ + "$ref:Repository" + ] + } + }, + "System.GetUpdateStatus": { + "description": "Get the current status of the update system. \"busy\" indicates that the system is current busy with an operation regarding updates. This does not necessarily mean an actual update is running. When this is true, update related functions on the client should be marked as busy and no interaction with update components shall be allowed. An example for such a state is when the system queries the server if there are updates available, typically after a call to CheckForUpdates. \"updateRunning\" on the other hand indicates an actual update process is ongoing. The user should be informed about it, the system also might restart at any point while an update is running.", + "params": { + }, + "returns": { + "busy": "Bool", + "updateRunning": "Bool" + } + }, + "System.Reboot": { + "description": "Initiate a reboot of the system. The return value will indicate whether the procedure has been initiated successfully.", + "params": { + }, + "returns": { + "success": "Bool" + } + }, + "System.RemovePackages": { + "description": "Starts removing a package. Returns true if the removal has been started successfully. Before calling this method, clients should check whether the package can be removed (canRemove set to true).", + "params": { + "packageIds": [ + "String" + ] + }, + "returns": { + "success": "Bool" + } + }, + "System.RollbackPackages": { + "description": "Starts a rollback. Returns true if the rollback has been started successfully. Before calling this method, clients should check whether the package can be rolled back (canRollback set to true).", + "params": { + "packageIds": [ + "String" + ] + }, + "returns": { + "success": "Bool" + } + }, + "System.Shutdown": { + "description": "Initiate a shutdown of the system. The return value will indicate whether the procedure has been initiated successfully.", + "params": { + }, + "returns": { + "success": "Bool" + } + }, + "System.UpdatePackages": { + "description": "Starts updating/installing packages with the given ids. Returns true if the upgrade has been started successfully. Note that it might still fail later. Before calling this method, clients should check the packages whether they are in a state where they can either be installed (no installedVersion set) or upgraded (updateAvailable set to true).", + "params": { + "o:packageIds": [ + "String" + ] + }, + "returns": { + "success": "Bool" + } + }, "Tags.AddTag": { "description": "Add a Tag. A Tag must have a deviceId OR a ruleId (call this method twice if you want to attach the same tag to a device and a rule), an appId (Use the appId of your app), a tagId (e.g. \"favorites\") and a value. Upon success, a TagAdded notification will be emitted. Calling this method twice for the same ids (device/rule, appId and tagId) but with a different value will update the tag's value and the TagValueChanged notification will be emitted.", "params": { @@ -1127,6 +1232,56 @@ "ruleId": "Uuid" } }, + "System.CapabilitiesChanged": { + "description": "Emitted whenever the system capabilities change.", + "params": { + "powerManagement": "Bool", + "updateManagement": "Bool" + } + }, + "System.PackageAdded": { + "description": "Emitted whenever a package is added to the list of packages.", + "params": { + "package": "$ref:Package" + } + }, + "System.PackageChanged": { + "description": "Emitted whenever a package in the list of packages changes.", + "params": { + "package": "$ref:Package" + } + }, + "System.PackageRemoved": { + "description": "Emitted whenever a package is removed from the list of packages.", + "params": { + "packageId": "String" + } + }, + "System.RepositoryAdded": { + "description": "Emitted whenever a repository is added to the list of repositories.", + "params": { + "repository": "$ref:Repository" + } + }, + "System.RepositoryChanged": { + "description": "Emitted whenever a repository in the list of repositories changes.", + "params": { + "repository": "$ref:Repository" + } + }, + "System.RepositoryRemoved": { + "description": "Emitted whenever a repository is removed from the list of repositories.", + "params": { + "repositoryId": "String" + } + }, + "System.UpdateStatusChanged": { + "description": "Emitted whenever the update status changes.", + "params": { + "busy": "Bool", + "updateRunning": "Bool" + } + }, "Tags.TagAdded": { "description": "Emitted whenever a tag is added to the system. ", "params": { @@ -1398,6 +1553,17 @@ "NetworkManagerStateConnectedSite", "NetworkManagerStateConnectedGlobal" ], + "Package": { + "canRemove": "Bool", + "candidateVersion": "String", + "changelog": "String", + "displayName": "String", + "id": "String", + "installedVersion": "String", + "rollbackAvailable": "Bool", + "summary": "String", + "updateAvailable": "Bool" + }, "Param": { "paramTypeId": "Uuid", "value": "$ref:BasicType" @@ -1453,6 +1619,11 @@ "Int" ] }, + "Repository": { + "displayName": "String", + "enabled": "Bool", + "id": "String" + }, "Rule": { "actions": [ "$ref:RuleAction"