/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright (C) 2015 Simon Stürz * * Copyright (C) 2014-2017 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 JSONRPCSERVER_H #define JSONRPCSERVER_H #include "jsonhandler.h" #include "transportinterface.h" #include "usermanager/usermanager.h" #include "types/deviceclass.h" #include "types/action.h" #include "types/event.h" #include #include #include #include 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 ¶ms); Q_INVOKABLE JsonReply *Introspect(const QVariantMap ¶ms) const; Q_INVOKABLE JsonReply *Version(const QVariantMap ¶ms) const; Q_INVOKABLE JsonReply *SetNotificationStatus(const QVariantMap ¶ms); Q_INVOKABLE JsonReply *CreateUser(const QVariantMap ¶ms); Q_INVOKABLE JsonReply *Authenticate(const QVariantMap ¶ms); Q_INVOKABLE JsonReply *RequestPushButtonAuth(const QVariantMap ¶ms); Q_INVOKABLE JsonReply *Tokens(const QVariantMap ¶ms) const; Q_INVOKABLE JsonReply *RemoveToken(const QVariantMap ¶ms); Q_INVOKABLE JsonReply *SetupCloudConnection(const QVariantMap ¶ms); Q_INVOKABLE JsonReply *SetupRemoteAccess(const QVariantMap ¶ms); Q_INVOKABLE JsonReply *IsCloudConnected(const QVariantMap ¶ms); Q_INVOKABLE JsonReply *KeepAlive(const QVariantMap ¶ms); signals: void CloudConnectedChanged(const QVariantMap &map); void PushButtonAuthFinished(const QVariantMap ¶ms); // Server API public: void registerTransportInterface(TransportInterface *interface, bool authenticationRequired); void unregisterTransportInterface(TransportInterface *interface); private: QHash handlers() const; void sendResponse(TransportInterface *interface, const QUuid &clientId, int commandId, const QVariantMap ¶ms = 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 QUuid &clientId) 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 ¶ms); void asyncReplyFinished(); void pairingFinished(QString cognitoUserId, int status, const QString &message); void onCloudConnectionStateChanged(); void onPushButtonAuthFinished(int transactionId, bool success, const QByteArray &token); private: QMap m_interfaces; // Interface, authenticationRequired QHash m_handlers; QHash m_asyncReplies; QHash m_clientTransports; QHash m_clientBuffers; QHash m_clientNotifications; QHash m_clientLocales; QHash m_pushButtonTransactions; QHash m_newConnectionWaitTimers; QHash 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