Kstars

drivermanager.h
1 /*
2  SPDX-FileCopyrightText: 2012 Jasem Mutlaq <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #pragma once
8 
9 #include "indicommon.h"
10 #include "customdrivers.h"
11 #include "ui_drivermanager.h"
12 
13 #include <QDialog>
14 #include <QFrame>
15 #include <QIcon>
16 #include <QString>
17 #include <QPointer>
18 #include <QJsonArray>
19 
20 #include <lilxml.h>
21 
22 class QStringList;
23 class QTreeWidgetItem;
24 
25 class DriverManager;
26 class ServerManager;
27 class ClientManager;
28 class DriverInfo;
29 
30 class DriverManagerUI : public QFrame, public Ui::DriverManager
31 {
32  Q_OBJECT
33 
34  public:
35  explicit DriverManagerUI(QWidget *parent = nullptr);
36 
37  public slots:
38  void makePortEditable(QTreeWidgetItem *selectedItem, int column);
39 
40  public:
41  QIcon runningPix;
42  QIcon stopPix;
43  QIcon connected;
44  QIcon disconnected;
45  QIcon localMode;
46  QIcon serverMode;
47 };
48 
49 /**
50  * @brief DriverManager is the primary class to handle all operations related to starting and stopping INDI drivers.
51  *
52  * INDI drivers can be local or remote drivers. For remote hosts, driver information is not known and devices are built
53  * as they arrive dynamically. The class parses INDI primary devices XML file (drivers.xml) and any 3rd party INDI Driver
54  * XML file to build a tree of devices grouped by driver family type.
55  *
56  * When starting local drivers, DriverManager also establishes an INDI server with the requested drivers and then connect to
57  * the local server to receive the devices dynamically.
58  *
59  * The class also handles INDI hosts which can be added in order to connect to a local or remote INDI server.
60  *
61  * @author Jasem Mutlaq
62  */
63 class DriverManager : public QDialog
64 {
65  Q_OBJECT
66 
67  public:
68  static DriverManager *Instance();
69 
70  enum
71  {
72  LOCAL_NAME_COLUMN = 0,
73  LOCAL_STATUS_COLUMN,
74  LOCAL_MODE_COLUMN,
75  LOCAL_VERSION_COLUMN,
76  LOCAL_PORT_COLUMN
77  };
78  enum
79  {
80  HOST_STATUS_COLUMN = 0,
81  HOST_NAME_COLUMN,
82  HOST_PORT_COLUMN
83  };
84 
85  bool readXMLDrivers();
86  bool readINDIHosts();
87  void processXMLDriver(const QString &driverName);
88  bool buildDeviceGroup(XMLEle *root, char errmsg[]);
89  bool buildDriverElement(XMLEle *root, QTreeWidgetItem *DGroup, DeviceFamily groupType, char errmsg[]);
90 
91  int getINDIPort(int customPort);
92  bool isDeviceRunning(const QString &deviceLabel);
93 
94  void saveHosts();
95 
96  void processLocalTree(bool dState);
97  void processRemoteTree(bool dState);
98 
99  DriverInfo *findDriverByName(const QString &name);
100  DriverInfo *findDriverByLabel(const QString &label);
101  DriverInfo *findDriverByExec(const QString &exec);
102 
103  ClientManager *getClientManager(DriverInfo *dv);
104 
105  const QList<DriverInfo *> &getDrivers() const
106  {
107  return driversList;
108  }
109  const QList<QVariantMap> &getCustomDrivers() const
110  {
111  return m_CustomDrivers->customDrivers();
112  }
113  QJsonArray getDriverList() const;
114 
115  const QStringList &getDriversStringList()
116  {
117  return driversStringList;
118  }
119 
120  /**
121  * @brief getUniqueHosts Given a list of DriverInfos, extract all the host:port information from all the drivers.
122  * and then consolidate each groups of drivers that belong to the same server & port to a specific list
123  * e.g. If we have driver1 (localhost:7624), driver2(192.168.1.90:7624), driver3(localhost:7624) then this would create
124  * two lists. First list contains [driver1,driver3] and second list contains [driver2] making each list _unique_ in terms of host params.
125  * @param dList list of driver to examine
126  * @param uHosts List of unique hosts, each with a group of drivers that belong to it.
127  */
128  void getUniqueHosts(const QList<DriverInfo *> &dList, QList<QList<DriverInfo *>> &uHosts);
129 
130  void addDriver(DriverInfo *di)
131  {
132  driversList.append(di);
133  }
134  void removeDriver(DriverInfo *di)
135  {
136  driversList.removeOne(di);
137  }
138 
139  void startDevices(QList<DriverInfo *> &dList);
140  void stopDevices(const QList<DriverInfo *> &dList);
141  void stopAllDevices()
142  {
143  stopDevices(driversList);
144  }
145  bool restartDriver(DriverInfo *dv);
146 
147  void connectRemoteHost(DriverInfo *dv);
148  bool disconnectRemoteHost(DriverInfo *dv);
149 
150  QString getUniqueDeviceLabel(const QString &label);
151 
152  void startClientManager(const QList<DriverInfo *> &qdv, const QString &host, int port);
153  void startLocalDrivers(ServerManager *serverManager);
154  void processDriverStartup(DriverInfo *dv);
155  void processDriverFailure(DriverInfo *dv, const QString &message);
156 
157  void disconnectClients();
158  void clearServers();
159 
160  private:
162  ~DriverManager() override;
163 
164  bool checkDriverAvailability(const QString &driver);
165 
166  static DriverManager *_DriverManager;
167 
168  ServerMode connectionMode { SERVER_CLIENT };
169  QTreeWidgetItem *lastGroup { nullptr };
170  int currentPort;
171  //DriverInfo::XMLSource xmlSource;
172  DriverSource driverSource;
173  DriverManagerUI *ui { nullptr };
174  QList<DriverInfo *> driversList;
175  QList<ServerManager *> servers;
176  QList<ClientManager *> clients;
177  QStringList driversStringList;
178  QPointer<CustomDrivers> m_CustomDrivers;
179 
180  public slots:
181  //void enableDevice(INDI_D *device);
182  //void disableDevice(INDI_D *device);
183 
184  void resizeDeviceColumn();
185  void updateLocalTab();
186  void updateClientTab();
187 
188  void updateMenuActions();
189 
190  void addINDIHost();
191  void modifyINDIHost();
192  void removeINDIHost();
193  void activateRunService();
194  void activateStopService();
195  void activateHostConnection();
196  void activateHostDisconnection();
197 
198  void updateCustomDrivers();
199 
200  void setClientStarted();
201  void setClientFailed(const QString &message);
202  void setClientTerminated(const QString &message);
203 
204  void setServerStarted();
205  void setServerFailed(const QString &message);
206  void setServerTerminated(const QString &message);
207 
208  void processDeviceStatus(DriverInfo *dv);
209 
210  void showCustomDrivers()
211  {
212  m_CustomDrivers->show();
213  }
214 
215  signals:
216 
217  // Server Signals
218 
219  // Server started successfully
220  void serverStarted(const QString &host, int port);
221  // Server failed to start.
222  void serverFailed(const QString &host, int port, const QString &message);
223  // Running server was abruptly terminated.
224  void serverTerminated(const QString &host, int port, const QString &message);
225 
226  // Client Signals
227  // Client connected to server successfully.
228  void clientStarted(const QString &host, int port);
229  // Client failed to connect to server.
230  void clientFailed(const QString &host, int port, const QString &message);
231  // Running server lost connection to server.
232  void clientTerminated(const QString &host, int port, const QString &message);
233 
234  // Driver Signals
235  void driverStarted(DriverInfo *driver);
236  void driverFailed(DriverInfo *driver, const QString &message);
237  void driverStopped(DriverInfo *driver);
238  void driverRestarted(DriverInfo *driver);
239 };
Q_OBJECTQ_OBJECT
void append(const T &value)
void getUniqueHosts(const QList< DriverInfo * > &dList, QList< QList< DriverInfo * >> &uHosts)
getUniqueHosts Given a list of DriverInfos, extract all the host:port information from all the driver...
DriverManager is the primary class to handle all operations related to starting and stopping INDI dri...
Definition: drivermanager.h:63
bool removeOne(const T &value)
virtual int exec()
ScriptableExtension * host() const
QObject * parent() const const
QString message
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 19 2022 03:57:50 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.