This repository has been archived on 2026-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
Michael Zanetti 8ebd1ddc4e Rework translation mechanism
This branch moves the translation logic out of the DevicePlugin
into the JsonRpc layers (JsonRpcHandlers and Rest handlers) and
deprecates the Configuration.SetLanguage method call. Instead
it adds a "locale" property in the JSONRPC.Hello message. This
will allow multiple clients to connect to a single core using
different languages.
2019-04-08 16:49:24 +02:00

122 lines
5.2 KiB
C++

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2015 Simon Stürz <simon.stuerz@guh.io> *
* Copyright (C) 2014-2017 Michael Zanetti <michael.zanetti@guh.io> *
* *
* 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 <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef JSONRPCSERVER_H
#define JSONRPCSERVER_H
#include "jsonhandler.h"
#include "transportinterface.h"
#include "usermanager.h"
#include "types/deviceclass.h"
#include "types/action.h"
#include "types/event.h"
#include <QObject>
#include <QVariantMap>
#include <QString>
#include <QSslConfiguration>
class Device;
namespace nymeaserver {
class JsonRPCServer: public JsonHandler
{
Q_OBJECT
public:
JsonRPCServer(const QSslConfiguration &sslConfiguration = QSslConfiguration(), QObject *parent = nullptr);
// JsonHandler API implementation
QString name() const;
Q_INVOKABLE JsonReply *Hello(const QVariantMap &params);
Q_INVOKABLE JsonReply *Introspect(const QVariantMap &params) const;
Q_INVOKABLE JsonReply *Version(const QVariantMap &params) const;
Q_INVOKABLE JsonReply *SetNotificationStatus(const QVariantMap &params);
Q_INVOKABLE JsonReply *CreateUser(const QVariantMap &params);
Q_INVOKABLE JsonReply *Authenticate(const QVariantMap &params);
Q_INVOKABLE JsonReply *RequestPushButtonAuth(const QVariantMap &params);
Q_INVOKABLE JsonReply *Tokens(const QVariantMap &params) const;
Q_INVOKABLE JsonReply *RemoveToken(const QVariantMap &params);
Q_INVOKABLE JsonReply *SetupCloudConnection(const QVariantMap &params);
Q_INVOKABLE JsonReply *SetupRemoteAccess(const QVariantMap &params);
Q_INVOKABLE JsonReply *IsCloudConnected(const QVariantMap &params);
Q_INVOKABLE JsonReply *KeepAlive(const QVariantMap &params);
signals:
void CloudConnectedChanged(const QVariantMap &map);
void PushButtonAuthFinished(const QVariantMap &params);
// Server API
public:
void registerTransportInterface(TransportInterface *interface, bool authenticationRequired);
void unregisterTransportInterface(TransportInterface *interface);
private:
QHash<QString, JsonHandler *> handlers() const;
void sendResponse(TransportInterface *interface, const QUuid &clientId, int commandId, const QVariantMap &params = QVariantMap());
void sendErrorResponse(TransportInterface *interface, const QUuid &clientId, int commandId, const QString &error);
void sendUnauthorizedResponse(TransportInterface *interface, const QUuid &clientId, int commandId, const QString &error);
QVariantMap createWelcomeMessage(TransportInterface *interface) const;
void processJsonPacket(TransportInterface *interface, const QUuid &clientId, const QByteArray &data);
private slots:
void setup();
void clientConnected(const QUuid &clientId);
void clientDisconnected(const QUuid &clientId);
void processData(const QUuid &clientId, const QByteArray &data);
void sendNotification(const QVariantMap &params);
void asyncReplyFinished();
void pairingFinished(QString cognitoUserId, int status, const QString &message);
void onCloudConnectionStateChanged();
void onPushButtonAuthFinished(int transactionId, bool success, const QByteArray &token);
private:
QMap<TransportInterface*, bool> m_interfaces; // Interface, authenticationRequired
QHash<QString, JsonHandler *> m_handlers;
QHash<JsonReply *, TransportInterface *> m_asyncReplies;
QHash<QUuid, TransportInterface*> m_clientTransports;
QHash<QUuid, QByteArray> m_clientBuffers;
QHash<QUuid, bool> m_clientNotifications;
QHash<QUuid, QLocale> m_clientLocales;
QHash<int, QUuid> m_pushButtonTransactions;
QHash<QString, JsonReply*> m_pairingRequests;
int m_notificationId;
void registerHandler(JsonHandler *handler);
QString formatAssertion(const QString &targetNamespace, const QString &method, QMetaMethod::MethodType methodType, JsonHandler *handler, const QVariantMap &data) const;
};
}
#endif // JSONRPCSERVER_H