7#include "clientmanager.h"
10#include "drivermanager.h"
11#include "guimanager.h"
12#include "indilistener.h"
14#include "servermanager.h"
16#include <indi_debug.h>
19ClientManager::ClientManager()
27 return std::any_of(m_ManagedDrivers.
begin(), m_ManagedDrivers.
end(), [driver](
const auto & oneDriver)
29 return driver == oneDriver;
33void ClientManager::newDevice(INDI::BaseDevice dp)
38 setBLOBMode(B_NEVER, dp.getDeviceName());
42 qCWarning(KSTARS_INDI) <<
"Received invalid device with empty name! Ignoring the device...";
46 qCDebug(KSTARS_INDI) <<
"Received new device" << dp.getDeviceName();
49 for (
auto &oneDriverInfo : m_ManagedDrivers)
51 if (oneDriverInfo->getUniqueLabel() ==
QString(dp.getDeviceName()))
53 oneDriverInfo->setUniqueLabel(dp.getDeviceName());
55 oneDriverInfo->addDevice(devInfo);
56 qCDebug(KSTARS_INDI) <<
"Driver" << oneDriverInfo->getName() <<
"is adding device" << dp.getDeviceName() <<
57 "(exact match by label)";
58 emit newINDIDevice(devInfo);
64 for (
auto &oneDriverInfo : m_ManagedDrivers)
66 if (oneDriverInfo->getName() ==
QString(dp.getDeviceName()))
68 oneDriverInfo->setUniqueLabel(dp.getDeviceName());
70 oneDriverInfo->addDevice(devInfo);
71 qCDebug(KSTARS_INDI) <<
"Driver" << oneDriverInfo->getName() <<
"is adding device" << dp.getDeviceName() <<
72 "(exact match by name)";
73 emit newINDIDevice(devInfo);
79 for (
auto &oneDriverInfo : m_ManagedDrivers)
83 oneDriverInfo->setUniqueLabel(dp.getDeviceName());
85 oneDriverInfo->addDevice(devInfo);
86 qCDebug(KSTARS_INDI) <<
"Driver" << oneDriverInfo->getName() <<
"is adding device" << dp.getDeviceName() <<
88 emit newINDIDevice(devInfo);
94 for (
auto &oneDriverInfo : m_ManagedDrivers)
96 auto dvName = oneDriverInfo->getName().split(
' ').first();
98 dvName = oneDriverInfo->getName();
101 ((oneDriverInfo->getDriverSource() == HOST_SOURCE || oneDriverInfo->getDriverSource() == GENERATED_SOURCE)))
103 oneDriverInfo->setUniqueLabel(dp.getDeviceName());
105 qCDebug(KSTARS_INDI) <<
"Driver" << oneDriverInfo->getName() <<
"is adding device" << dp.getDeviceName() <<
107 oneDriverInfo->addDevice(devInfo);
108 emit newINDIDevice(devInfo);
114void ClientManager::newProperty(INDI::Property property)
117 if (!isServerConnected())
119 IDLog(
"Received new property %s for disconnected device %s, discarding\n",
property.getName(),
property.getDeviceName());
127void ClientManager::updateProperty(INDI::Property property)
132void ClientManager::removeProperty(INDI::Property prop)
135 const QString device = prop.getDeviceName();
136 emit removeINDIProperty(prop);
139 if (blobManagers.
empty() ==
false && prop.getType() == INDI_BLOB && prop.getPermission() != IP_WO)
140 emit removeBLOBManager(device, name);
143void ClientManager::processRemoveBLOBManager(
const QString &device,
const QString &property)
145 auto manager = std::find_if(blobManagers.
begin(), blobManagers.
end(), [device,
property](
auto & oneManager)
147 const auto bProperty = oneManager->property(
"property").toString();
148 const auto bDevice = oneManager->property(
"device").toString();
149 return (device == bDevice && property == bProperty);
152 if (manager != blobManagers.
end())
154 (*manager)->disconnectServer();
155 (*manager)->deleteLater();
160void ClientManager::processNewProperty(INDI::Property prop)
163 if (prop.getType() == INDI_BLOB && prop.getPermission() != IP_WO)
166 connect(bm, &BlobManager::propertyUpdated,
this, &ClientManager::updateINDIProperty);
167 connect(bm, &BlobManager::connected,
this, [prop,
this]()
169 if (prop && prop.getRegistered())
170 emit newBLOBManager(prop.getDeviceName(), prop);
179 for (
auto &oneManager : blobManagers)
180 oneManager->disconnectServer();
183void ClientManager::removeDevice(INDI::BaseDevice dp)
185 QString deviceName = dp.getDeviceName();
190 auto &oneManager = it.next();
191 if (oneManager->property(
"device").toString() == deviceName)
193 oneManager->disconnect();
198 for (
auto &driverInfo : m_ManagedDrivers)
200 for (
auto &deviceInfo : driverInfo->getDevices())
202 if (deviceInfo->getDeviceName() == deviceName)
204 qCDebug(KSTARS_INDI) <<
"Removing device" << deviceName;
206 emit removeINDIDevice(deviceName);
208 driverInfo->removeDevice(deviceInfo);
210 if (driverInfo->isEmpty())
212 driverInfo->setClientState(
false);
213 m_ManagedDrivers.removeOne(driverInfo);
222void ClientManager::newMessage(INDI::BaseDevice dp,
int messageID)
224 emit newINDIMessage(dp, messageID);
227void ClientManager::newUniversalMessage(std::string message)
235 qCDebug(KSTARS_INDI) <<
"Adding managed driver" << driver->getName();
237 m_ManagedDrivers.append(driver);
239 driver->setClientManager(
this);
241 sManager = driver->getServerManager();
246 if (m_ManagedDrivers.empty())
248 qCDebug(KSTARS_INDI) <<
"removeManagedDriver: no managed drivers!";
252 qCDebug(KSTARS_INDI) <<
"Removing managed driver" << driver->getName();
254 driver->setClientState(
false);
256 for (
auto &di : driver->getDevices())
258 qCDebug(KSTARS_INDI) <<
"Managed driver" << driver->getName() <<
"has device" << di->getDeviceName() <<
259 "that will be removed";
262 GUIManager::Instance()->removeDevice(di->getDeviceName());
265 INDIListener::Instance()->removeDevice(di->getDeviceName());
268 driver->removeDevice(di);
270 m_ManagedDrivers.removeOne(driver);
273void ClientManager::serverConnected()
275 qCDebug(KSTARS_INDI) <<
"INDI server connected.";
277 for (
auto &oneDriverInfo : m_ManagedDrivers)
279 oneDriverInfo->setClientState(
true);
281 oneDriverInfo->setHostParameters(sManager->getHost(), sManager->getPort());
284 m_PendingConnection =
false;
285 m_ConnectionRetries = MAX_RETRIES;
290void ClientManager::serverDisconnected(
int exitCode)
292 if (m_PendingConnection)
293 qCDebug(KSTARS_INDI) <<
"INDI server connection refused.";
295 qCDebug(KSTARS_INDI) <<
"INDI server disconnected. Exit code:" << exitCode;
297 for (
auto &oneDriverInfo : m_ManagedDrivers)
299 oneDriverInfo->setClientState(
false);
300 oneDriverInfo->reset();
303 if (m_PendingConnection)
306 if (m_ConnectionRetries-- > 0)
311 qCDebug(KSTARS_INDI) <<
"Retrying connection again...";
312 if (connectServer() ==
false)
313 serverDisconnected(0);
315 m_PendingConnection =
false;
321 m_PendingConnection =
false;
322 m_ConnectionRetries = MAX_RETRIES;
323 emit failed(
i18n(
"Failed to connect to INDI server %1:%2", getHost(), getPort()));
327 else if (exitCode < 0)
328 emit terminated(
i18n(
"Connection to INDI host at %1 on port %2 lost. Server disconnected: %3", getHost(), getPort(),
334 return m_ManagedDrivers;
337void ClientManager::establishConnection()
340 <<
"INDI: Connecting to local INDI server on port " << getPort() <<
" ...";
342 m_PendingConnection =
true;
343 m_ConnectionRetries = 2;
345 if (connectServer() ==
false)
346 serverDisconnected(0);
348 m_PendingConnection =
false;
355 return oneDriverInfo->getName() == name;
365 return oneDriverInfo->getLabel() == label;
371void ClientManager::setBLOBEnabled(
bool enabled,
const QString &device,
const QString &property)
373 for(
auto &bm : blobManagers)
375 if (bm->property(
"device") == device && (
property.isEmpty() || bm->property(
"property") ==
property))
377 bm->setEnabled(enabled);
383bool ClientManager::isBLOBEnabled(
const QString &device,
const QString &property)
385 for(
auto &bm : blobManagers)
387 if (bm->property(
"device") == device && bm->property(
"property") ==
property)
388 return bm->property(
"enabled").toBool();
BlobManager manages connection to INDI server to handle a specific BLOB.
void disconnectAll()
disconnectAll Disconnect from server and disconnect all BLOB Managers.
void removeManagedDriver(const QSharedPointer< DriverInfo > &driver)
removeManagedDriver Remove managed driver from pool of drivers managed by this client manager.
void appendManagedDriver(const QSharedPointer< DriverInfo > &driver)
appendManagedDriver Add driver to pool of managed drivers by this client manager.
DeviceInfo is simple class to hold DriverInfo and INDI::BaseDevice associated with a particular devic...
QString i18n(const char *text, const TYPE &arg...)
QString name(StandardAction id)
void append(QList< T > &&value)
bool removeOne(const AT &t)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QVariant property(const char *name) const const
QString fromStdString(const std::string &str)
bool isEmpty() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const