Skip to content

Commit 3ce392e

Browse files
committed
BAD COMMIT
This breaks everything ASCOM attempting to upgrade to ASCOM7 protocol
1 parent fad547a commit 3ce392e

File tree

4 files changed

+150
-35
lines changed

4 files changed

+150
-35
lines changed

plugins/TelescopeControl/src/ASCOM/ASCOMDevice.cpp

Lines changed: 131 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,57 +18,135 @@
1818

1919
#include "ASCOMDevice.hpp"
2020
#include <comdef.h>
21+
#include <QTimer>
22+
//#include <qwaitcondition.h>
23+
#include <QMessageBox>
24+
#include "StelTranslator.hpp"
2125

2226
ASCOMDevice::ASCOMDevice(QObject* parent, QString ascomDeviceId) : QObject(parent),
23-
mAscomDeviceId(ascomDeviceId)
24-
{}
27+
mAscomDeviceId(ascomDeviceId),
28+
connectionRetries(0)
29+
{
30+
}
2531

26-
bool ASCOMDevice::connect()
32+
// FIXME! ASCOM7 deprecates writing to the LConnected property. One shall use methods Connect()/Disconnect() instead,
33+
// and detect the state per "Connecting" property.
34+
void ASCOMDevice::connect()
2735
{
28-
if (mConnected) return true;
36+
if (mConnected) return; // true;
2937

3038
BOOL initResult = OleInit(COINIT_APARTMENTTHREADED);
3139
HRESULT hResult = OleCreateInstance(reinterpret_cast<const wchar_t*>(mAscomDeviceId.toStdWString().c_str()), &pTelescopeDispatch);
3240

3341
if (!initResult || FAILED(hResult))
3442
{
35-
qDebug() << "Initialization failed for device: " << mAscomDeviceId;
36-
return false;
43+
qCritical() << "Initialization failed for device: " << mAscomDeviceId;
44+
QMessageBox::critical(nullptr, q_("ERROR!"), q_("Initialization failed for device: ")+mAscomDeviceId);
45+
46+
return;
3747
}
3848

39-
VARIANT v1 = OleBoolToVariant(TRUE);
49+
// Initiate asynchronous Connect() according to ASCOM7
50+
hResult = OleMethodCall(pTelescopeDispatch, nullptr, const_cast<wchar_t*>(LConnect), 0);
51+
if (FAILED(hResult))
52+
{
53+
qCritical() << "Could not send Connect signal to device: " << mAscomDeviceId;
54+
QMessageBox::critical(nullptr, q_("ERROR!"), q_("Could not send Connect signal to device: ")+mAscomDeviceId);
55+
return;
56+
}
57+
connectionRetries=10;
58+
QObject::connect(&connectionTimer, &QTimer::timeout, this, &ASCOMDevice::tryFinishConnect);
59+
connectionTimer.start(250); // 1/4 s interval
60+
}
4061

41-
hResult = OlePropertyPut(pTelescopeDispatch, nullptr, const_cast<wchar_t*>(LConnected), 1, v1);
62+
void ASCOMDevice::tryFinishConnect()
63+
{
64+
qDebug() << "ASCOMDevice::tryFinishConnect()";
4265

43-
if (FAILED(hResult))
66+
if (isConnecting())
4467
{
45-
qDebug() << "Could not connect to device: " << mAscomDeviceId;
46-
return false;
68+
if (--connectionRetries <= 0)
69+
{
70+
// Give up connecting!
71+
connectionTimer.stop();
72+
QObject::disconnect(&connectionTimer, &QTimer::timeout, this, &ASCOMDevice::tryFinishConnect);
73+
qCritical() << "Could not establish connection to device: " << mAscomDeviceId;
74+
QMessageBox::critical(nullptr, q_("ERROR!"), q_("Could not establish connection to device: ")+mAscomDeviceId);
75+
}
76+
}
77+
else // connection has probably been established. Read value for certainty.
78+
{
79+
connectionTimer.stop();
80+
QObject::disconnect(&connectionTimer, &QTimer::timeout, this, &ASCOMDevice::tryFinishConnect);
81+
mConnected=isDeviceConnected();
4782
}
48-
49-
mConnected = true;
50-
return true;
5183
}
5284

53-
bool ASCOMDevice::disconnect()
85+
void ASCOMDevice::tryFinishDisconnect()
5486
{
55-
if (!mConnected) return true;
87+
qDebug() << "ASCOMDevice::tryFinishDisconnect()";
88+
if (isConnecting())
89+
{
90+
if (--connectionRetries <= 0)
91+
{
92+
// Give up disconnecting! Is that at all useful?
93+
connectionTimer.stop();
94+
QObject::disconnect(&connectionTimer, &QTimer::timeout, this, &ASCOMDevice::tryFinishDisconnect);
95+
qCritical() << "Could not send Disconnect signal to device: " << mAscomDeviceId;
96+
// TODO: Show a screen panel?
97+
QMessageBox::critical(nullptr, q_("ERROR!"), q_("Could not cleanly disconnect from device: ")+mAscomDeviceId);
98+
}
99+
}
100+
else // disconnection has been successful. Read value for certainty.
101+
{
102+
connectionTimer.stop();
103+
QObject::disconnect(&connectionTimer, &QTimer::timeout, this, &ASCOMDevice::tryFinishDisconnect);
104+
mConnected=false;
105+
pTelescopeDispatch->Release();
106+
}
107+
}
108+
56109

57-
VARIANT v1 = OleBoolToVariant(FALSE);
58-
HRESULT hResult = OlePropertyPut(pTelescopeDispatch, nullptr, const_cast<wchar_t*>(LConnected), 1, v1);
59110

111+
//void ASCOMDevice::disconnect()
112+
//{
113+
// if (!mConnected) return; // true;
114+
//
115+
// VARIANT v1 = OleBoolToVariant(FALSE);
116+
// HRESULT hResult = OlePropertyPut(pTelescopeDispatch, nullptr, const_cast<wchar_t*>(LConnected), 1, v1);
117+
//
118+
// if (FAILED(hResult))
119+
// {
120+
// qDebug() << "Could not disconnect device: " << mAscomDeviceId;
121+
// return; // false;
122+
// }
123+
//
124+
// pTelescopeDispatch->Release(); // TODO: Move that to tryFinishDisconnect
125+
//
126+
// mConnected = false;
127+
// return; // true;
128+
//}
129+
130+
131+
void ASCOMDevice::disconnect()
132+
{
133+
if (mConnected) return; // true;
134+
135+
136+
// Initiate asynchronous Disconnect() according to ASCOM7
137+
HRESULT hResult = OleMethodCall(pTelescopeDispatch, nullptr, const_cast<wchar_t*>(LDisconnect), 0);
60138
if (FAILED(hResult))
61139
{
62-
qDebug() << "Could not disconnect device: " << mAscomDeviceId;
63-
return false;
140+
qCritical() << "Could not send Disconnect signal to device: " << mAscomDeviceId;
141+
// TODO: Show a screen panel?
142+
return;
64143
}
65-
66-
pTelescopeDispatch->Release();
67-
68-
mConnected = false;
69-
return true;
144+
connectionRetries=10;
145+
QObject::connect(&connectionTimer, &QTimer::timeout, this, &ASCOMDevice::tryFinishDisconnect);
146+
connectionTimer.start(250); // 1/4 s interval
70147
}
71148

149+
72150
ASCOMDevice::ASCOMCoordinates ASCOMDevice::position() const
73151
{
74152
return mCoordinates;
@@ -114,6 +192,11 @@ void ASCOMDevice::abortSlew()
114192
}
115193
}
116194

195+
bool ASCOMDevice::isConnected() const
196+
{
197+
return mConnected;
198+
}
199+
117200
bool ASCOMDevice::isDeviceConnected() const
118201
{
119202
if (!mConnected) return false;
@@ -127,7 +210,21 @@ bool ASCOMDevice::isDeviceConnected() const
127210
return false;
128211
}
129212

130-
return v1.boolVal == -1;
213+
return v1.boolVal == VARIANT_TRUE;
214+
}
215+
216+
bool ASCOMDevice::isConnecting() const
217+
{
218+
VARIANT v1;
219+
HRESULT hResult = OlePropertyGet(pTelescopeDispatch, &v1, const_cast<wchar_t*>(LConnecting));
220+
221+
if (FAILED(hResult))
222+
{
223+
qDebug() << "Could not get connecting state for device: " << mAscomDeviceId;
224+
return false;
225+
}
226+
227+
return v1.boolVal == VARIANT_TRUE;
131228
}
132229

133230
bool ASCOMDevice::isParked() const
@@ -143,7 +240,7 @@ bool ASCOMDevice::isParked() const
143240
return false;
144241
}
145242

146-
return v1.boolVal == -1;
243+
return v1.boolVal == VARIANT_TRUE;
147244
}
148245

149246

@@ -176,7 +273,7 @@ bool ASCOMDevice::doesRefraction()
176273
return false;
177274
}
178275

179-
return v1.boolVal == -1;
276+
return v1.boolVal == VARIANT_TRUE;
180277
}
181278

182279

@@ -228,9 +325,12 @@ QString ASCOMDevice::showDeviceChooser(QString previousDeviceId)
228325

229326
const wchar_t* ASCOMDevice::LSlewToCoordinatesAsync = L"SlewToCoordinatesAsync";
230327
const wchar_t* ASCOMDevice::LSyncToCoordinates = L"SyncToCoordinates";
231-
const wchar_t* ASCOMDevice::LAbortSlew = L"AbortSlew";
232-
const wchar_t* ASCOMDevice::LConnected = L"Connected";
233-
const wchar_t* ASCOMDevice::LAtPark = L"AtPark";
328+
const wchar_t* ASCOMDevice::LAbortSlew = L"AbortSlew";
329+
const wchar_t* ASCOMDevice::LConnected = L"Connected";
330+
const wchar_t* ASCOMDevice::LConnecting = L"Connecting";
331+
const wchar_t* ASCOMDevice::LConnect = L"Connect";
332+
const wchar_t* ASCOMDevice::LDisconnect = L"Disconnect";
333+
const wchar_t* ASCOMDevice::LAtPark = L"AtPark";
234334
const wchar_t* ASCOMDevice::LEquatorialSystem = L"EquatorialSystem";
235335
const wchar_t* ASCOMDevice::LDoesRefraction = L"DoesRefraction";
236336
const wchar_t* ASCOMDevice::LRightAscension = L"RightAscension";

plugins/TelescopeControl/src/ASCOM/ASCOMDevice.hpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <QObject>
2323
#include <QStringList>
24+
#include <QTimer>
2425
#include "../common/OLE.hpp"
2526

2627
class ASCOMDevice final : public QObject
@@ -43,10 +44,14 @@ class ASCOMDevice final : public QObject
4344

4445
ASCOMDevice(QObject* parent = nullptr, QString ascomDeviceId = nullptr);
4546

46-
bool isDeviceConnected() const;
47+
bool isConnected() const; // just return mConnected, i.e., connection has been etablished previously so that this device is said "active".
48+
bool isDeviceConnected() const; // Thorough test if connection to actual telescope is still present. Slow!
4749
bool isParked() const;
48-
bool connect();
49-
bool disconnect();
50+
void connect(); // initiate connection.
51+
void disconnect(); // initiate disconnection.
52+
bool isConnecting() const; // According to ASCOM7 protocol standards.
53+
void tryFinishConnect(); // finalize connection or give up after some time.
54+
void tryFinishDisconnect();
5055
ASCOMCoordinates position() const;
5156
void slewToCoordinates(ASCOMCoordinates coords);
5257
void syncToCoordinates(ASCOMCoordinates coords);
@@ -56,6 +61,7 @@ class ASCOMDevice final : public QObject
5661
ASCOMCoordinates getCoordinates();
5762

5863
signals:
64+
// TODO: These signals are empty definitions and are not connected and never emitted!
5965
void deviceConnected();
6066
void deviceDisconnected();
6167

@@ -64,10 +70,15 @@ class ASCOMDevice final : public QObject
6470
bool mConnected = false;
6571
ASCOMCoordinates mCoordinates;
6672
QString mAscomDeviceId;
73+
QTimer connectionTimer;
74+
int connectionRetries;
6775
static const wchar_t* LSlewToCoordinatesAsync;
6876
static const wchar_t* LSyncToCoordinates;
6977
static const wchar_t* LAbortSlew;
7078
static const wchar_t* LConnected;
79+
static const wchar_t* LConnecting;
80+
static const wchar_t* LConnect;
81+
static const wchar_t* LDisconnect;
7182
static const wchar_t* LAtPark;
7283
static const wchar_t* LEquatorialSystem;
7384
static const wchar_t* LDoesRefraction;

plugins/TelescopeControl/src/ASCOM/TelescopeClientASCOM.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ TelescopeClientASCOM::TelescopeClientASCOM(const QString& name, const QString& p
4747

4848
mAscomDevice = new ASCOMDevice(this, mAscomDeviceId);
4949
mAscomDevice->connect();
50+
// TODO: Wait for actual confirmation of connection...
5051
mDoesRefraction = mAscomDevice->doesRefraction();
5152
mCoordinateType = mAscomDevice->getEquatorialCoordinateType();
5253
}
@@ -167,7 +168,9 @@ void TelescopeClientASCOM::telescopeAbortSlew()
167168

168169
bool TelescopeClientASCOM::isConnected() const
169170
{
170-
return mAscomDevice->isDeviceConnected();
171+
//return mAscomDevice->isDeviceConnected();
172+
// Called every frame, we should only see if we have been connected before...
173+
return mAscomDevice->isConnected();
171174
}
172175

173176
bool TelescopeClientASCOM::hasKnownPosition() const

plugins/TelescopeControl/src/TelescopeControl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ void TelescopeControl::draw(StelCore* core)
302302
reticleTexture->bind();
303303
for (const auto& telescope : std::as_const(telescopeClients))
304304
{
305+
// FIXME: This seems to flood IP telescopes with excessive network traffic.
305306
if (telescope->isConnected() && telescope->hasKnownPosition())
306307
{
307308
Vec3f XY;

0 commit comments

Comments
 (0)