File indexing completed on 2024-07-21 06:28:26

0001 /*
0002     SPDX-FileCopyrightText: 2019 Jasem Mutlaq <mutlaqja@ikarustech.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QtWebSockets/QWebSocket>
0010 #include <memory>
0011 
0012 namespace ISD
0013 {
0014 class Camera;
0015 class WSMedia : public QObject
0016 {
0017         Q_OBJECT
0018 
0019     public:
0020         WSMedia(Camera *manager);
0021         virtual ~WSMedia() = default;
0022 
0023         void setURL(const QUrl &url)
0024         {
0025             m_URL = url;
0026         }
0027 
0028     signals:
0029         void connected();
0030         void disconnected();
0031         void newFile(const QByteArray &message, const QString &extension);
0032 
0033     public slots:
0034         void connectServer();
0035         void disconnectServer();
0036 
0037     private slots:
0038 
0039         // Connection
0040         void onConnected();
0041         void onDisconnected();
0042         void onError(QAbstractSocket::SocketError error);
0043 
0044         // Communication
0045         void onTextReceived(const QString &message);
0046         void onBinaryReceived(const QByteArray &message);
0047 
0048     private:
0049         QWebSocket m_WebSocket;
0050         uint16_t m_ReconnectTries {0};
0051         Camera *m_Manager { nullptr };
0052         QUrl m_URL;
0053 
0054         bool m_isConnected { false };
0055         bool m_sendBlobs { true};
0056         QString extension;
0057 
0058         // Retry every 5 seconds in case remote server is down
0059         static const uint16_t RECONNECT_INTERVAL = 5000;
0060         // Retry for 1 hour before giving up
0061         static const uint16_t RECONNECT_MAX_TRIES = 720;
0062 };
0063 }