Skip to content

Commit

Permalink
Fixes, especially for Qt client. New EXTPLANE-WARNING message.
Browse files Browse the repository at this point in the history
  • Loading branch information
vranki committed Sep 12, 2019
1 parent 717c244 commit 9f765f5
Show file tree
Hide file tree
Showing 17 changed files with 174 additions and 82 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
A plugin for X-Plane and other simulators that allows commanding the simulation from
external programs through an easy-to-use TCP protocol.

Current version 1001
Current version 1002

## Supported simulators ##

Expand Down Expand Up @@ -288,6 +288,7 @@ Supported settings are:

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

Expand Down
16 changes: 12 additions & 4 deletions clients/extplane-client-qt/clientdataref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ ClientDataRef::ClientDataRef(QObject *parent, QString newName, double accuracy)
, m_subscribers(0)
, m_client(nullptr)
, m_changedOnce(false)
{ }
{
qDebug() << Q_FUNC_INFO << m_name;
}

ClientDataRef::~ClientDataRef() {
unsubscribe();
if(m_subscribers > 0) {
qDebug() << Q_FUNC_INFO << "Warning: ref " << m_name << " destroyed but still subscribed by " << m_subscribers;
}
}

QString& ClientDataRef::name() {
Expand All @@ -36,7 +40,7 @@ ExtPlaneClient *ClientDataRef::client() const {
void ClientDataRef::setClient(ExtPlaneClient *client) {
if (m_client == client)
return;

qDebug() << Q_FUNC_INFO << name() << client;
m_client = client;
if(m_client) {
connect(m_client, &ExtPlaneClient::destroyed, this, &ClientDataRef::clientDestroyed);
Expand All @@ -45,6 +49,7 @@ void ClientDataRef::setClient(ExtPlaneClient *client) {
}

void ClientDataRef::clientDestroyed() {
qDebug() << Q_FUNC_INFO << name();
setClient(nullptr);
}

Expand Down Expand Up @@ -116,11 +121,14 @@ int ClientDataRef::subscribers() {
}

void ClientDataRef::setSubscribers(int sub) {
qDebug() << Q_FUNC_INFO << name() << sub;
Q_ASSERT(sub >= 0);
m_subscribers = sub;
}

void ClientDataRef::unsubscribe() {
if(m_client) emit unsubscribed(this);
qDebug() << Q_FUNC_INFO << m_client << m_subscribers;
emit unsubscribed(this);
}

QString ClientDataRef::dataFormat() const {
Expand Down
4 changes: 2 additions & 2 deletions clients/extplane-client-qt/clientdataref.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ClientDataRef : public QObject {
int subscribers();
void setSubscribers(int sub);
bool isArray();
void unsubscribe(); // Call to unsubscribe ref.
void unsubscribe(); // Call to unsubscribe ref. Subscriber count will be reduced by one.
QString dataFormat() const;

public slots:
Expand All @@ -60,7 +60,7 @@ public slots:
signals:
void changed(ClientDataRef *ref); // Emitted when simulator updates value
void valueSet(ClientDataRef *ref); // Emitted when client sets value
void unsubscribed(ClientDataRef *ref);
void unsubscribed(ClientDataRef *ref); // Emitted when one subscriber unsubscribes (not necessarily all)
void nameChanged(QString name);
void accuracyChanged(double accuracy);
void clientChanged(ExtPlaneClient* client);
Expand Down
52 changes: 46 additions & 6 deletions clients/extplane-client-qt/dataref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ DataRef::DataRef(QObject *parent) : QObject(parent)
, m_clientDataRef(nullptr)
, m_client(nullptr)
, m_accuracy(0)
, m_scaleFactor(1)
{
// Connect both valueset and changed to valuechanged
connect(this, &DataRef::valueSet, this, &DataRef::valueChanged);
Expand All @@ -15,7 +16,11 @@ DataRef::DataRef(QObject *parent) : QObject(parent)
}

DataRef::~DataRef() {
if(m_clientDataRef) m_clientDataRef->unsubscribe();
qDebug() << Q_FUNC_INFO << m_name << m_clientDataRef;
if(m_clientDataRef) {
m_clientDataRef->unsubscribe();
m_clientDataRef = nullptr;
}
}

void DataRef::subscribeIfPossible() {
Expand Down Expand Up @@ -50,18 +55,40 @@ void DataRef::setName(QString &name) {
if (m_name == name)
return;

if(!m_name.isEmpty()) {
if(m_clientDataRef) { // Unsub old dataref..
qDebug() << Q_FUNC_INFO << "Unsubbing" << m_clientDataRef->name();
m_clientDataRef->unsubscribe();
m_clientDataRef = nullptr;
} else {
qDebug() << Q_FUNC_INFO << "No cdr - can't unsub " << m_name << " after subscribing" << name;
}
}
m_name = name;
emit nameChanged(m_name);

if(m_clientDataRef) { // Unsub old dataref..
m_clientDataRef->unsubscribe();
m_clientDataRef = nullptr;
}
subscribeIfPossible();
}

QString DataRef::value() {
return m_clientDataRef ? m_clientDataRef->value() : "";
if(m_clientDataRef) {
if(qFuzzyCompare(m_scaleFactor, 1)) {
return m_clientDataRef->value();
} else {
bool ok;
double refValue = m_clientDataRef->value().toDouble(&ok);
refValue = refValue * m_scaleFactor;
if(!ok) {
if(m_clientDataRef->value().isEmpty()) return "";

qDebug() << Q_FUNC_INFO << "Warning: Ref " << name() << "scale factor is set, but can't convert value to double: " << m_clientDataRef->value();
return m_clientDataRef->value();
}
return QString::number(refValue);
}
} else {
return "";
}
}

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

double DataRef::scaleFactor() const {
return m_scaleFactor;
}

void DataRef::setClient(ExtPlaneClient *client) {
if (m_client == client)
return;
Expand All @@ -93,6 +124,15 @@ void DataRef::setDataFormat(QString dataFormat) {
if(m_clientDataRef) m_clientDataRef->setDataFormat(m_dataFormat);
}

void DataRef::scaleFactor(double scaleFactor) {
if (qFuzzyCompare(m_scaleFactor, scaleFactor))
return;

m_scaleFactor = scaleFactor;
emit scaleFactorChanged(m_scaleFactor);
emit valueChanged(m_clientDataRef);
}

void DataRef::clientDatarefDestroyed() {
m_clientDataRef = nullptr;
}
Expand Down
9 changes: 9 additions & 0 deletions clients/extplane-client-qt/dataref.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class DataRefProvicer;
* want. About the same API as with ClientDataRef.
*
* Will automatically connect when name is set.
*
* Scalefactor can be set if a numeric dataref needs to be scaled with
* a number (for unit conversion etc). If scaleFactor is 2, value()
* returns 2*(real value).
*/
class DataRef : public QObject
{
Expand All @@ -22,6 +26,7 @@ class DataRef : public QObject
Q_PROPERTY(double accuracy READ accuracy WRITE setAccuracy NOTIFY accuracyChanged)
Q_PROPERTY(ExtPlaneClient* client READ client WRITE setClient NOTIFY clientChanged)
Q_PROPERTY(QString dataFormat READ dataFormat WRITE setDataFormat NOTIFY dataFormatChanged)
Q_PROPERTY(double scaleFactor READ scaleFactor WRITE scaleFactor NOTIFY scaleFactorChanged)

public:
explicit DataRef(QObject *parent = nullptr);
Expand All @@ -32,6 +37,7 @@ class DataRef : public QObject
QString value(); // Returns first value
ExtPlaneClient* client() const;
QString dataFormat() const;
double scaleFactor() const;

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

public slots:
void setName(QString &name);
Expand All @@ -51,6 +58,7 @@ public slots:
void setClient(ExtPlaneClient* client);
void setDataRefProvider();
void setDataFormat(QString dataFormat);
void scaleFactor(double scaleFactor);

private slots:
void clientDatarefDestroyed();
Expand All @@ -62,6 +70,7 @@ private slots:
QString m_name, m_dataFormat;
QStringList m_emptyStringList; // Return if no client dataref available yet
double m_accuracy;
double m_scaleFactor;
};

#endif // DATAREF_H
30 changes: 19 additions & 11 deletions clients/extplane-client-qt/extplaneclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ ExtPlaneClient::ExtPlaneClient(QObject *parent, QString name, bool simulated) :
_instance = this;
}

void ExtPlaneClient::createClient()
{
void ExtPlaneClient::createClient() {
if(m_connection) return; // Already created
connect(&m_simulatedExtplaneConnection, &ExtPlaneConnection::connectionMessage, this, &ExtPlaneClient::setConnectionMessage);
connect(&m_extplaneConnection, &ExtPlaneConnection::connectionMessage, this, &ExtPlaneClient::setConnectionMessage);
connect(&m_extplaneConnection, &ExtPlaneConnection::extplaneWarning, this, &ExtPlaneClient::extplaneWarning);
qDebug() << Q_FUNC_INFO << "simulated:" << m_simulated;
if(m_simulated) {
m_simulatedExtplaneConnection.registerClient(this);
Expand Down Expand Up @@ -55,16 +55,28 @@ ClientDataRef* ExtPlaneClient::subscribeDataRef(QString name, double accuracy) {
ClientDataRef *ref = m_connection->subscribeDataRef(name, accuracy);
connect(ref, &ClientDataRef::changed, this, &ExtPlaneClient::cdrChanged);
connect(ref, &ClientDataRef::destroyed, this, &ExtPlaneClient::refDestroyed);
ref->setClient(this);
m_dataRefs.append(ref);
return ref;
}


void ExtPlaneClient::unsubscribeDataRefByName(QString name) {
qDebug() << Q_FUNC_INFO << "Warning: this functions is deprecated and will be removed. Used with ref" << name;
for(ClientDataRef *ref : m_dataRefs) {
if(ref->name() == name) {
m_dataRefs.removeOne(ref);
m_connection->unsubscribeDataRef(ref);
return;
}
}
}

void ExtPlaneClient::refDestroyed(QObject* refqo) {
m_dataRefs.removeOne(static_cast<ClientDataRef*>(refqo));
}

void ExtPlaneClient::setConnectionMessage(QString msg)
{
void ExtPlaneClient::setConnectionMessage(QString msg) {
m_connectionMessage = msg;
emit connectionMessageChanged(m_connectionMessage);
}
Expand All @@ -86,9 +98,9 @@ void ExtPlaneClient::cdrChanged(ClientDataRef *ref) {
}
}

void ExtPlaneClient::unsubscribeDataRef(QString name) {
void ExtPlaneClient::unsubscribeDataRef(ClientDataRef *refToUnsubscribe) {
for(ClientDataRef *ref : m_dataRefs) {
if(ref->name() == name) {
if(ref->name() == refToUnsubscribe->name()) {
m_dataRefs.removeOne(ref);
m_connection->unsubscribeDataRef(ref);
return;
Expand Down Expand Up @@ -161,7 +173,7 @@ void ExtPlaneClient::setSimulated(bool simulated) {
return;

while(!m_dataRefs.isEmpty())
unsubscribeDataRef(m_dataRefs.first()->name());
unsubscribeDataRef(m_dataRefs.first());

qDebug() << Q_FUNC_INFO << simulated;

Expand All @@ -186,7 +198,3 @@ void ExtPlaneClient::setSimulated(bool simulated) {
void ExtPlaneClient::valueSet(ClientDataRef *ref) {
m_connection->setValue(ref->name(), ref->value());
}

void ExtPlaneClient::unsubscribed(ClientDataRef *ref) {
unsubscribeDataRef(ref->name());
}
5 changes: 3 additions & 2 deletions clients/extplane-client-qt/extplaneclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ExtPlaneClient : public QObject {
static ExtPlaneClient &instance();
Q_INVOKABLE void createClient(); // MUST be called before use
ClientDataRef* subscribeDataRef(QString name, double accuracy=0);
void unsubscribeDataRef(QString name);
void unsubscribeDataRefByName(QString name); // TODO: Shouldn't be used - REMOVE
bool isDataRefSubscribed(QString name);
void keyPress(int id);
void buttonPress(int id);
Expand All @@ -46,6 +46,7 @@ class ExtPlaneClient : public QObject {
QString connectionMessage();

public slots:
void unsubscribeDataRef(ClientDataRef *refToUnsubscribe);
void setUpdateInterval(double newInterval);
void setSimulated(bool simulated);

Expand All @@ -56,11 +57,11 @@ public slots:
void datarefProviderChanged(ClientDataRefProvider* datarefProvider);
void simulatedChanged(bool simulated);
void connectionMessageChanged(QString connectionMessage);
void extplaneWarning(QString message);

private slots:
void cdrChanged(ClientDataRef *ref);
void valueSet(ClientDataRef *ref);
void unsubscribed(ClientDataRef *ref);
void refDestroyed(QObject* refqo);
void setConnectionMessage(QString msg);

Expand Down
Loading

0 comments on commit 9f765f5

Please sign in to comment.