File indexing completed on 2024-04-21 03:44:08

0001 /*
0002     SPDX-FileCopyrightText: 2012 Jasem Mutlaq <mutlaqja@ikarustech.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 
0006     Handle INDI Standard properties.
0007 */
0008 
0009 #pragma once
0010 
0011 #include "indi/indistd.h"
0012 
0013 #include <indiproperty.h>
0014 
0015 #include <QObject>
0016 
0017 class ClientManager;
0018 class FITSViewer;
0019 class DeviceInfo;
0020 
0021 /**
0022  * @class INDIListener
0023  * INDIListener is responsible for creating ISD::GDInterface generic devices as new devices arrive from
0024  * ClientManager. It can support multiple ClientManagers and will first create a generic INDI device.
0025  * Upon arrival of INDI properties, INDIListener can create specialized devices (e.g. Telescope) if it
0026  * detects key Standard INDI property that signifies a particular device family. The generic device
0027  * functionality is extended via the Decorator design pattern.
0028  *
0029  * INDIListener also delegates INDI properties as they are received from ClientManager to the appropriate
0030  * device to be processed.
0031  *
0032  * @author Jasem Mutlaq
0033  */
0034 class INDIListener : public QObject
0035 {
0036         Q_OBJECT
0037 
0038     public:
0039         static INDIListener *Instance();
0040         // Convenience function
0041         static const QList<QSharedPointer<ISD::GenericDevice>> &devices()
0042         {
0043             return INDIListener::Instance()->getDevices();
0044         }
0045         static const QList<QSharedPointer<ISD::GenericDevice>> devicesByInterface(uint32_t interface)
0046         {
0047             QList<QSharedPointer<ISD::GenericDevice>> filteredDevices;
0048             auto all = devices();
0049             std::copy_if(all.begin(), all.end(), std::back_inserter(filteredDevices), [interface](auto & oneDevice)
0050             {
0051                 return oneDevice->getDriverInterface() & interface;
0052             });
0053             return filteredDevices;
0054         }
0055         static bool findDevice(const QString &name, QSharedPointer<ISD::GenericDevice> &device);
0056 
0057         void addClient(ClientManager *cm);
0058         void removeClient(ClientManager *cm);
0059 
0060         bool getDevice(const QString &name, QSharedPointer<ISD::GenericDevice> &device) const;
0061         const QList<QSharedPointer<ISD::GenericDevice>> &getDevices() const
0062         {
0063             return m_Devices;
0064         }
0065 
0066         int size() const
0067         {
0068             return m_Devices.size();
0069         }
0070 
0071     public slots:
0072         void processDevice(DeviceInfo *dv);
0073         void removeDevice(const QString &deviceName);
0074 
0075         void registerProperty(INDI::Property prop);
0076         void updateProperty(INDI::Property prop);
0077         void removeProperty(INDI::Property prop);
0078 
0079         void processMessage(INDI::BaseDevice dp, int messageID);
0080         void processUniversalMessage(const QString &message);
0081 
0082     private:
0083         explicit INDIListener(QObject *parent);
0084 
0085         static INDIListener *_INDIListener;
0086 
0087         QList<ClientManager *> clients;
0088         QList<QSharedPointer<ISD::GenericDevice>> m_Devices;
0089 
0090 
0091     signals:
0092         void newDevice(const QSharedPointer<ISD::GenericDevice> &device);
0093         void deviceRemoved(const QSharedPointer<ISD::GenericDevice> &device);
0094 };