File indexing completed on 2024-04-21 14:46:02

0001 /*
0002     SPDX-FileCopyrightText: 2003 Jasem Mutlaq <mutlaqja@ikarustech.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QTcpSocket>
0010 
0011 class INDIMenu;
0012 class INDI_P;
0013 class INDI_D;
0014 
0015 class KProcess;
0016 class QTcpSocket;
0017 
0018 // INDI device manager
0019 class DeviceManager : public QObject
0020 {
0021     Q_OBJECT
0022   public:
0023     enum ManagerMode
0024     {
0025         M_LOCAL,
0026         M_SERVER,
0027         M_CLIENT
0028     };
0029     enum
0030     {
0031         INDI_DEVICE_NOT_FOUND    = -1,
0032         INDI_PROPERTY_INVALID    = -2,
0033         INDI_PROPERTY_DUPLICATED = -3,
0034         INDI_DISPATCH_ERROR      = -4
0035     };
0036 
0037     DeviceManager(INDIMenu *INDIparent, QString inHost, uint inPort, ManagerMode inMode);
0038     ~DeviceManager();
0039 
0040     INDIMenu *parent;
0041     QList<INDI_D *> indi_dev;
0042     QList<IDevice *> managed_devices;
0043 
0044     QTcpSocket serverSocket;
0045     LilXML *XMLParser;
0046     QString host;
0047     uint port;
0048     QString serverBuffer;
0049     ManagerMode mode;
0050     KProcess *serverProcess;
0051 
0052     int dispatchCommand(XMLEle *root, QString &errmsg);
0053 
0054     INDI_D *addDevice(XMLEle *dep, QString &errmsg);
0055     INDI_D *findDev(XMLEle *root, int create, QString &errmsg);
0056 
0057     /*****************************************************************
0058         * Send to server
0059         ******************************************************************/
0060     void sendNewText(INDI_P *pp);
0061     void sendNewNumber(INDI_P *pp);
0062     void sendNewSwitch(INDI_P *pp, INDI_E *lp);
0063     void startBlob(const QString &devName, const QString &propName, const QString &timestamp);
0064     void sendOneBlob(const QString &blobName, unsigned int blobSize, const QString &blobFormat,
0065                      unsigned char *blobBuffer);
0066     void finishBlob();
0067 
0068     /*****************************************************************
0069         * Misc.
0070         ******************************************************************/
0071     int delPropertyCmd(XMLEle *root, QString &errmsg);
0072     int removeDevice(const QString &devName, QString &errmsg);
0073     INDI_D *findDev(const QString &devName, QString &errmsg);
0074 
0075     int messageCmd(XMLEle *root, QString &errmsg);
0076     void checkMsg(XMLEle *root, INDI_D *dp);
0077     void doMsg(XMLEle *msg, INDI_D *dp);
0078 
0079     void appendManagedDevices(QList<IDevice *> &processed_devices);
0080     void startServer();
0081     void stopServer();
0082     void connectToServer();
0083     void enableBLOB(bool enable, QString device = QString(), QString property = QString());
0084 
0085     QString getServerBuffer() { return serverBuffer; }
0086 
0087   public slots:
0088     void dataReceived();
0089     void connectionSuccess();
0090     void connectionError();
0091     void processStandardError();
0092 
0093   signals:
0094     void newDevice(INDI_D *);
0095     void deviceManagerError(DeviceManager *);
0096     void newServerInput();
0097 };