/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright (C) 2017-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 AWSCONNECTOR_H #define AWSCONNECTOR_H #include #include #include #include #include class AWSConnector : public QObject { Q_OBJECT public: explicit AWSConnector(QObject *parent = nullptr); ~AWSConnector(); class PushNotificationsEndpoint { public: QString userId; QString endpointId; QString displayName; }; void connect2AWS(const QString &endpoint, const QString &clientId, const QString &clientName, const QString &caFile, const QString &clientCertFile, const QString &clientPrivKeyFile); void disconnectAWS(); bool isConnected() const; void setDeviceName(const QString &deviceName); void pairDevice(const QString &idToken, const QString &userId); public slots: int sendPushNotification(const QString &userId, const QString &endpointId, const QString &title, const QString &text); void requestTURNCredentials(); signals: void connected(); void disconnected(); void devicePaired(const QString &cognritoUserId, int errorCode, const QString &message); void pushNotificationEndpointsUpdated(const QList pushNotificationEndpoints); void pushNotificationEndpointAdded(const AWSConnector::PushNotificationsEndpoint &pushNotificationEndpoint); void pushNotificationSent(int id, int status); void turnCredentialsReceived(const QVariantMap &turnCredentials); void proxyConnectionRequestReceived(const QString &token, const QString &nonce, const QString &serverUrl); private slots: void doConnect(); void onConnected(); void onDisconnected(); void onPublished(quint16 msgid, const QString &topic); void onSubscribed(const QString &topic, Mqtt::SubscribeReturnCode returnCode); void onPublishReceived(const QString &topic, const QByteArray &payload); void registerDevice(); void onDeviceRegistered(bool needsReconnect); void setupSubscriptions(); void fetchPairings(); void onPairingsRetrieved(const QVariantMap &pairings); void setName(); void onTurnCredentialsReceived(const QVariantMap &turnCredentials); private: quint16 publish(const QString &topic, const QVariantMap &message); void subscribe(const QStringList &topics); void storeRegisteredFlag(bool registered); bool readRegisteredFlag() const; void storeSyncedNameCache(const QString &syncedName); QString readSyncedNameCache(); QString getCertificateFingerprint(const QSslCertificate &certificate) const; private: MqttClient *m_client = nullptr; QString m_currentEndpoint; QString m_caFile; QString m_clientCertFile; QString m_clientPrivKeyFile; QString m_clientId; QString m_clientName; QFuture m_connectingFuture; bool m_isCleanSession = true; bool m_shouldReconnect = false; QTimer m_reconnectTimer; quint8 m_transactionId = 0; QString m_createDeviceId; int m_createDeviceSubscriptionId = 0; QHash m_pairingRequests; bool m_setupInProgress = false; int m_reconnectCounter = 0; QDateTime m_lastConnectionDrop; QStringList m_subscriptionCache; QPair m_cachedTURNCredentials; }; Q_DECLARE_METATYPE(AWSConnector::PushNotificationsEndpoint) #endif // AWSCONNECTOR_H