Skip to content

Commit 9f765f5

Browse files
committed
Fixes, especially for Qt client. New EXTPLANE-WARNING message.
1 parent 717c244 commit 9f765f5

File tree

17 files changed

+174
-82
lines changed

17 files changed

+174
-82
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
A plugin for X-Plane and other simulators that allows commanding the simulation from
44
external programs through an easy-to-use TCP protocol.
55

6-
Current version 1001
6+
Current version 1002
77

88
## Supported simulators ##
99

@@ -288,6 +288,7 @@ Supported settings are:
288288

289289
* **EXTPLANE {protocol}** Sent when connected. Protocol is currently 1.
290290
* **EXTPLANE-VERSION {version}** Sent when connected. Feature version integer, which is incremented with each new bug fix or feature.
291+
* **EXTPLANE-WARNING {message}** Show warning message for developer and/or user
291292
* **u{type} {dataref} {value}** Dataref has changed in value based on accuracy.
292293
* Types may be `i` (int), `f` (float), `d` (double), `ia` (int array), `fa` (float array), or `b` (data).
293294

clients/extplane-client-qt/clientdataref.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ ClientDataRef::ClientDataRef(QObject *parent, QString newName, double accuracy)
77
, m_subscribers(0)
88
, m_client(nullptr)
99
, m_changedOnce(false)
10-
{ }
10+
{
11+
qDebug() << Q_FUNC_INFO << m_name;
12+
}
1113

1214
ClientDataRef::~ClientDataRef() {
13-
unsubscribe();
15+
if(m_subscribers > 0) {
16+
qDebug() << Q_FUNC_INFO << "Warning: ref " << m_name << " destroyed but still subscribed by " << m_subscribers;
17+
}
1418
}
1519

1620
QString& ClientDataRef::name() {
@@ -36,7 +40,7 @@ ExtPlaneClient *ClientDataRef::client() const {
3640
void ClientDataRef::setClient(ExtPlaneClient *client) {
3741
if (m_client == client)
3842
return;
39-
43+
qDebug() << Q_FUNC_INFO << name() << client;
4044
m_client = client;
4145
if(m_client) {
4246
connect(m_client, &ExtPlaneClient::destroyed, this, &ClientDataRef::clientDestroyed);
@@ -45,6 +49,7 @@ void ClientDataRef::setClient(ExtPlaneClient *client) {
4549
}
4650

4751
void ClientDataRef::clientDestroyed() {
52+
qDebug() << Q_FUNC_INFO << name();
4853
setClient(nullptr);
4954
}
5055

@@ -116,11 +121,14 @@ int ClientDataRef::subscribers() {
116121
}
117122

118123
void ClientDataRef::setSubscribers(int sub) {
124+
qDebug() << Q_FUNC_INFO << name() << sub;
125+
Q_ASSERT(sub >= 0);
119126
m_subscribers = sub;
120127
}
121128

122129
void ClientDataRef::unsubscribe() {
123-
if(m_client) emit unsubscribed(this);
130+
qDebug() << Q_FUNC_INFO << m_client << m_subscribers;
131+
emit unsubscribed(this);
124132
}
125133

126134
QString ClientDataRef::dataFormat() const {

clients/extplane-client-qt/clientdataref.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class ClientDataRef : public QObject {
4646
int subscribers();
4747
void setSubscribers(int sub);
4848
bool isArray();
49-
void unsubscribe(); // Call to unsubscribe ref.
49+
void unsubscribe(); // Call to unsubscribe ref. Subscriber count will be reduced by one.
5050
QString dataFormat() const;
5151

5252
public slots:
@@ -60,7 +60,7 @@ public slots:
6060
signals:
6161
void changed(ClientDataRef *ref); // Emitted when simulator updates value
6262
void valueSet(ClientDataRef *ref); // Emitted when client sets value
63-
void unsubscribed(ClientDataRef *ref);
63+
void unsubscribed(ClientDataRef *ref); // Emitted when one subscriber unsubscribes (not necessarily all)
6464
void nameChanged(QString name);
6565
void accuracyChanged(double accuracy);
6666
void clientChanged(ExtPlaneClient* client);

clients/extplane-client-qt/dataref.cpp

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ DataRef::DataRef(QObject *parent) : QObject(parent)
66
, m_clientDataRef(nullptr)
77
, m_client(nullptr)
88
, m_accuracy(0)
9+
, m_scaleFactor(1)
910
{
1011
// Connect both valueset and changed to valuechanged
1112
connect(this, &DataRef::valueSet, this, &DataRef::valueChanged);
@@ -15,7 +16,11 @@ DataRef::DataRef(QObject *parent) : QObject(parent)
1516
}
1617

1718
DataRef::~DataRef() {
18-
if(m_clientDataRef) m_clientDataRef->unsubscribe();
19+
qDebug() << Q_FUNC_INFO << m_name << m_clientDataRef;
20+
if(m_clientDataRef) {
21+
m_clientDataRef->unsubscribe();
22+
m_clientDataRef = nullptr;
23+
}
1924
}
2025

2126
void DataRef::subscribeIfPossible() {
@@ -50,18 +55,40 @@ void DataRef::setName(QString &name) {
5055
if (m_name == name)
5156
return;
5257

58+
if(!m_name.isEmpty()) {
59+
if(m_clientDataRef) { // Unsub old dataref..
60+
qDebug() << Q_FUNC_INFO << "Unsubbing" << m_clientDataRef->name();
61+
m_clientDataRef->unsubscribe();
62+
m_clientDataRef = nullptr;
63+
} else {
64+
qDebug() << Q_FUNC_INFO << "No cdr - can't unsub " << m_name << " after subscribing" << name;
65+
}
66+
}
5367
m_name = name;
5468
emit nameChanged(m_name);
5569

56-
if(m_clientDataRef) { // Unsub old dataref..
57-
m_clientDataRef->unsubscribe();
58-
m_clientDataRef = nullptr;
59-
}
6070
subscribeIfPossible();
6171
}
6272

6373
QString DataRef::value() {
64-
return m_clientDataRef ? m_clientDataRef->value() : "";
74+
if(m_clientDataRef) {
75+
if(qFuzzyCompare(m_scaleFactor, 1)) {
76+
return m_clientDataRef->value();
77+
} else {
78+
bool ok;
79+
double refValue = m_clientDataRef->value().toDouble(&ok);
80+
refValue = refValue * m_scaleFactor;
81+
if(!ok) {
82+
if(m_clientDataRef->value().isEmpty()) return "";
83+
84+
qDebug() << Q_FUNC_INFO << "Warning: Ref " << name() << "scale factor is set, but can't convert value to double: " << m_clientDataRef->value();
85+
return m_clientDataRef->value();
86+
}
87+
return QString::number(refValue);
88+
}
89+
} else {
90+
return "";
91+
}
6592
}
6693

6794
ExtPlaneClient *DataRef::client() const {
@@ -72,6 +99,10 @@ QString DataRef::dataFormat() const {
7299
return m_clientDataRef ? m_clientDataRef->dataFormat() : "";
73100
}
74101

102+
double DataRef::scaleFactor() const {
103+
return m_scaleFactor;
104+
}
105+
75106
void DataRef::setClient(ExtPlaneClient *client) {
76107
if (m_client == client)
77108
return;
@@ -93,6 +124,15 @@ void DataRef::setDataFormat(QString dataFormat) {
93124
if(m_clientDataRef) m_clientDataRef->setDataFormat(m_dataFormat);
94125
}
95126

127+
void DataRef::scaleFactor(double scaleFactor) {
128+
if (qFuzzyCompare(m_scaleFactor, scaleFactor))
129+
return;
130+
131+
m_scaleFactor = scaleFactor;
132+
emit scaleFactorChanged(m_scaleFactor);
133+
emit valueChanged(m_clientDataRef);
134+
}
135+
96136
void DataRef::clientDatarefDestroyed() {
97137
m_clientDataRef = nullptr;
98138
}

clients/extplane-client-qt/dataref.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ class DataRefProvicer;
1212
* want. About the same API as with ClientDataRef.
1313
*
1414
* Will automatically connect when name is set.
15+
*
16+
* Scalefactor can be set if a numeric dataref needs to be scaled with
17+
* a number (for unit conversion etc). If scaleFactor is 2, value()
18+
* returns 2*(real value).
1519
*/
1620
class DataRef : public QObject
1721
{
@@ -22,6 +26,7 @@ class DataRef : public QObject
2226
Q_PROPERTY(double accuracy READ accuracy WRITE setAccuracy NOTIFY accuracyChanged)
2327
Q_PROPERTY(ExtPlaneClient* client READ client WRITE setClient NOTIFY clientChanged)
2428
Q_PROPERTY(QString dataFormat READ dataFormat WRITE setDataFormat NOTIFY dataFormatChanged)
29+
Q_PROPERTY(double scaleFactor READ scaleFactor WRITE scaleFactor NOTIFY scaleFactorChanged)
2530

2631
public:
2732
explicit DataRef(QObject *parent = nullptr);
@@ -32,6 +37,7 @@ class DataRef : public QObject
3237
QString value(); // Returns first value
3338
ExtPlaneClient* client() const;
3439
QString dataFormat() const;
40+
double scaleFactor() const;
3541

3642
signals:
3743
void changed(ClientDataRef *ref); // Emitted when simulator updates value
@@ -42,6 +48,7 @@ class DataRef : public QObject
4248
void accuracyChanged(double accuracy);
4349
void clientChanged(ExtPlaneClient* client);
4450
void dataFormatChanged(QString dataFormat);
51+
void scaleFactorChanged(double scaleFactor);
4552

4653
public slots:
4754
void setName(QString &name);
@@ -51,6 +58,7 @@ public slots:
5158
void setClient(ExtPlaneClient* client);
5259
void setDataRefProvider();
5360
void setDataFormat(QString dataFormat);
61+
void scaleFactor(double scaleFactor);
5462

5563
private slots:
5664
void clientDatarefDestroyed();
@@ -62,6 +70,7 @@ private slots:
6270
QString m_name, m_dataFormat;
6371
QStringList m_emptyStringList; // Return if no client dataref available yet
6472
double m_accuracy;
73+
double m_scaleFactor;
6574
};
6675

6776
#endif // DATAREF_H

clients/extplane-client-qt/extplaneclient.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ ExtPlaneClient::ExtPlaneClient(QObject *parent, QString name, bool simulated) :
1919
_instance = this;
2020
}
2121

22-
void ExtPlaneClient::createClient()
23-
{
22+
void ExtPlaneClient::createClient() {
2423
if(m_connection) return; // Already created
2524
connect(&m_simulatedExtplaneConnection, &ExtPlaneConnection::connectionMessage, this, &ExtPlaneClient::setConnectionMessage);
2625
connect(&m_extplaneConnection, &ExtPlaneConnection::connectionMessage, this, &ExtPlaneClient::setConnectionMessage);
26+
connect(&m_extplaneConnection, &ExtPlaneConnection::extplaneWarning, this, &ExtPlaneClient::extplaneWarning);
2727
qDebug() << Q_FUNC_INFO << "simulated:" << m_simulated;
2828
if(m_simulated) {
2929
m_simulatedExtplaneConnection.registerClient(this);
@@ -55,16 +55,28 @@ ClientDataRef* ExtPlaneClient::subscribeDataRef(QString name, double accuracy) {
5555
ClientDataRef *ref = m_connection->subscribeDataRef(name, accuracy);
5656
connect(ref, &ClientDataRef::changed, this, &ExtPlaneClient::cdrChanged);
5757
connect(ref, &ClientDataRef::destroyed, this, &ExtPlaneClient::refDestroyed);
58+
ref->setClient(this);
5859
m_dataRefs.append(ref);
5960
return ref;
6061
}
6162

63+
64+
void ExtPlaneClient::unsubscribeDataRefByName(QString name) {
65+
qDebug() << Q_FUNC_INFO << "Warning: this functions is deprecated and will be removed. Used with ref" << name;
66+
for(ClientDataRef *ref : m_dataRefs) {
67+
if(ref->name() == name) {
68+
m_dataRefs.removeOne(ref);
69+
m_connection->unsubscribeDataRef(ref);
70+
return;
71+
}
72+
}
73+
}
74+
6275
void ExtPlaneClient::refDestroyed(QObject* refqo) {
6376
m_dataRefs.removeOne(static_cast<ClientDataRef*>(refqo));
6477
}
6578

66-
void ExtPlaneClient::setConnectionMessage(QString msg)
67-
{
79+
void ExtPlaneClient::setConnectionMessage(QString msg) {
6880
m_connectionMessage = msg;
6981
emit connectionMessageChanged(m_connectionMessage);
7082
}
@@ -86,9 +98,9 @@ void ExtPlaneClient::cdrChanged(ClientDataRef *ref) {
8698
}
8799
}
88100

89-
void ExtPlaneClient::unsubscribeDataRef(QString name) {
101+
void ExtPlaneClient::unsubscribeDataRef(ClientDataRef *refToUnsubscribe) {
90102
for(ClientDataRef *ref : m_dataRefs) {
91-
if(ref->name() == name) {
103+
if(ref->name() == refToUnsubscribe->name()) {
92104
m_dataRefs.removeOne(ref);
93105
m_connection->unsubscribeDataRef(ref);
94106
return;
@@ -161,7 +173,7 @@ void ExtPlaneClient::setSimulated(bool simulated) {
161173
return;
162174

163175
while(!m_dataRefs.isEmpty())
164-
unsubscribeDataRef(m_dataRefs.first()->name());
176+
unsubscribeDataRef(m_dataRefs.first());
165177

166178
qDebug() << Q_FUNC_INFO << simulated;
167179

@@ -186,7 +198,3 @@ void ExtPlaneClient::setSimulated(bool simulated) {
186198
void ExtPlaneClient::valueSet(ClientDataRef *ref) {
187199
m_connection->setValue(ref->name(), ref->value());
188200
}
189-
190-
void ExtPlaneClient::unsubscribed(ClientDataRef *ref) {
191-
unsubscribeDataRef(ref->name());
192-
}

clients/extplane-client-qt/extplaneclient.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ExtPlaneClient : public QObject {
3232
static ExtPlaneClient &instance();
3333
Q_INVOKABLE void createClient(); // MUST be called before use
3434
ClientDataRef* subscribeDataRef(QString name, double accuracy=0);
35-
void unsubscribeDataRef(QString name);
35+
void unsubscribeDataRefByName(QString name); // TODO: Shouldn't be used - REMOVE
3636
bool isDataRefSubscribed(QString name);
3737
void keyPress(int id);
3838
void buttonPress(int id);
@@ -46,6 +46,7 @@ class ExtPlaneClient : public QObject {
4646
QString connectionMessage();
4747

4848
public slots:
49+
void unsubscribeDataRef(ClientDataRef *refToUnsubscribe);
4950
void setUpdateInterval(double newInterval);
5051
void setSimulated(bool simulated);
5152

@@ -56,11 +57,11 @@ public slots:
5657
void datarefProviderChanged(ClientDataRefProvider* datarefProvider);
5758
void simulatedChanged(bool simulated);
5859
void connectionMessageChanged(QString connectionMessage);
60+
void extplaneWarning(QString message);
5961

6062
private slots:
6163
void cdrChanged(ClientDataRef *ref);
6264
void valueSet(ClientDataRef *ref);
63-
void unsubscribed(ClientDataRef *ref);
6465
void refDestroyed(QObject* refqo);
6566
void setConnectionMessage(QString msg);
6667

0 commit comments

Comments
 (0)