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

0001 /*
0002     SPDX-FileCopyrightText: 2012 Jasem Mutlaq <mutlaqja@ikarustech.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QPointer>
0010 
0011 #ifdef USE_QT5_INDI
0012 #include <baseclientqt.h>
0013 #else
0014 #include <baseclient.h>
0015 #include <QObject>
0016 #endif
0017 
0018 #include "blobmanager.h"
0019 
0020 class DeviceInfo;
0021 class DriverInfo;
0022 class ServerManager;
0023 
0024 /**
0025  * @class ClientManager
0026  * ClientManager manages connection to INDI server, creation of devices, and receiving/sending properties.
0027  *
0028  * ClientManager is a subclass of INDI::BaseClient class part of the INDI Library.
0029  * This enables the class to communicate with INDI server and to receive notification of devices, properties, and messages.
0030  *
0031  * @author Jasem Mutlaq
0032  * @version 1.3
0033  */
0034 #ifdef USE_QT5_INDI
0035 class ClientManager : public INDI::BaseClientQt
0036 #else
0037 class ClientManager : public QObject, public INDI::BaseClient
0038 #endif
0039 {
0040         Q_OBJECT
0041 
0042     public:
0043         ClientManager();
0044         virtual ~ClientManager() = default;
0045 
0046         /**
0047          * @brief appendManagedDriver Add driver to pool of managed drivers by this client manager.
0048          * @param dv pointer to driver info instance.
0049          * @note This function is ALWAYS called from the main KStars thread.
0050          */
0051         void appendManagedDriver(const QSharedPointer<DriverInfo> &driver);
0052 
0053         /**
0054          * @brief removeManagedDriver Remove managed driver from pool of drivers managed by this client manager.
0055          * @param dv pointer to driver info instance.
0056          * @note This function is ALWAYS called from the main KStars thread.
0057          */
0058         void removeManagedDriver(const QSharedPointer<DriverInfo> &driver);
0059 
0060         /**
0061          * @brief disconnectAll Disconnect from server and disconnect all BLOB Managers.
0062          */
0063         void disconnectAll();
0064 
0065         int count()
0066         {
0067             return m_ManagedDrivers.count();
0068         }
0069 
0070         bool isBLOBEnabled(const QString &device, const QString &property);
0071         void setBLOBEnabled(bool enabled, const QString &device, const QString &property);
0072 
0073         ServerManager *getServerManager()
0074         {
0075             return sManager;
0076         }
0077 
0078         const QSharedPointer<DriverInfo> &findDriverInfoByName(const QString &name);
0079         const QSharedPointer<DriverInfo> &findDriverInfoByLabel(const QString &label);
0080 
0081         bool isDriverManaged(const QSharedPointer<DriverInfo> &driver);
0082 
0083         const QList<QSharedPointer<DriverInfo>> &getManagedDrivers() const;
0084 
0085         void establishConnection();
0086 
0087     protected:
0088         virtual void newDevice(INDI::BaseDevice dp) override;
0089         virtual void removeDevice(INDI::BaseDevice dp) override;
0090 
0091         virtual void newProperty(INDI::Property prop) override;
0092         virtual void updateProperty(INDI::Property prop) override;
0093         virtual void removeProperty(INDI::Property prop) override;
0094 
0095         virtual void newMessage(INDI::BaseDevice dp, int messageID) override;
0096         virtual void newUniversalMessage(std::string message) override;
0097 
0098         virtual void serverConnected() override;
0099         virtual void serverDisconnected(int exitCode) override;
0100 
0101     private:
0102         void processNewProperty(INDI::Property prop);
0103         void processRemoveBLOBManager(const QString &device, const QString &property);
0104         QList<QSharedPointer<DriverInfo>> m_ManagedDrivers;
0105         QList<BlobManager *> blobManagers;
0106         ServerManager *sManager { nullptr };
0107 
0108     signals:
0109         // Client successfully connected to the server.
0110         void started();
0111         // Client failed to connect to the server.
0112         void failed(const QString &message);
0113         // Running client was abnormally disconnected from server.
0114         void terminated(const QString &message);
0115 
0116         // @note If using INDI Posix client, the following newINDIDevice/Property and removeINDIDevice/Property signals
0117         // must be connected to slots using Qt::BlockingQueuedConnection to ensure operation is fully completed before
0118         // resuming the INDI client thread. For Qt Based INDI client, Qt::DirectConnection should be used.
0119 
0120         void newINDIDevice(DeviceInfo *dv);
0121         void removeINDIDevice(const QString &name);
0122 
0123         void newINDIProperty(INDI::Property prop);
0124         void updateINDIProperty(INDI::Property prop);
0125         void removeINDIProperty(INDI::Property prop);
0126 
0127         void newBLOBManager(const char *device, INDI::Property prop);
0128         void removeBLOBManager(const QString &device, const QString &property);
0129 
0130         void newINDIMessage(INDI::BaseDevice dp, int messageID);
0131         void newINDIUniversalMessage(const QString &message);
0132 
0133     private:
0134         static constexpr uint8_t MAX_RETRIES {2};
0135         uint8_t m_ConnectionRetries {MAX_RETRIES};
0136         bool m_PendingConnection {false};
0137 };