Skip to content

Commit 3df4906

Browse files
author
Ville Ranki
committed
Potential fix for ticket #22, other fixes
1 parent c51c23c commit 3df4906

File tree

10 files changed

+42
-8
lines changed

10 files changed

+42
-8
lines changed

extplane-plugin/xplaneplugin.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ DataRef* XPlanePlugin::subscribeRef(QString &name) {
105105
if(ref->name() == name) {
106106
DEBUG << "Already subscribed to " << name;
107107
ref->setSubscriberCount(ref->subscriberCount() + 1);
108-
emit ref->changed(ref); // Force update to all clients
109108
return ref;
110109
}
111110
}
@@ -162,7 +161,7 @@ void XPlanePlugin::updateDataRef(DataRef *ref)
162161
switch (ref->type()) {
163162
case extplaneRefTypeFloat:
164163
{
165-
float newValue = XPLMGetDataf(ref);
164+
float newValue = XPLMGetDataf(ref->ref());
166165
qobject_cast<FloatDataRef*>(ref)->updateValue(newValue);
167166
break;
168167
};

extplane-server/datarefs/datadataref.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ void DataDataRef::updateValue() {
2323
// TODO: do we really want to make this comparison for large data datarefs? Probably as it's still cheaper than sending over the wire the new data
2424
if (_newValue != _value) {
2525
_value = _newValue;
26+
if(!_valueValid) setValueValid();
2627
emit changed(this);
2728
}
2829
}

extplane-server/datarefs/dataref.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#include "dataref.h"
22

33
DataRef::DataRef(QObject *parent, QString name, void *ref) : QObject(parent)
4+
, _typeString("?")
5+
, _type(extplaneRefTypeUnknown)
46
, _ref(ref)
7+
, _accuracy(0)
8+
, _valueValid(false)
59
, _name(name)
610
, _subscriberCount(0)
7-
, _type(extplaneRefTypeUnknown)
811
, _writable(false)
9-
, _typeString("?")
1012
, _unsubscribeAfterChange(false)
1113
{ }
1214

@@ -64,3 +66,15 @@ void DataRef::setUnsubscribeAfterChange() {
6466
bool DataRef::shouldUnsubscribeAfterChange() const {
6567
return _unsubscribeAfterChange;
6668
}
69+
70+
bool DataRef::isValid() const
71+
{
72+
return _valueValid;
73+
}
74+
75+
void DataRef::setValueValid()
76+
{
77+
if(!_valueValid) {
78+
_valueValid = true;
79+
}
80+
}

extplane-server/datarefs/dataref.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ enum {
2424
*/
2525
class DataRef : public QObject {
2626
Q_OBJECT
27-
2827
public:
2928
DataRef(QObject *parent, QString name, void* ref);
3029
const QString &name() const;
@@ -43,14 +42,19 @@ class DataRef : public QObject {
4342
double accuracy() { return _accuracy; }
4443
void setUnsubscribeAfterChange();
4544
bool shouldUnsubscribeAfterChange() const;
45+
bool isValid() const; // True if the value has been set initially. False if not.
46+
4647
signals:
47-
void changed(DataRef *ref);
48+
void changed(DataRef *ref); // Should not be emitted if value is not valid.
4849

4950
protected:
51+
void setValueValid(); // Call this to mark the value valid.
52+
5053
QString _typeString;
5154
extplaneRefID _type;
5255
void* _ref;
5356
double _accuracy;
57+
bool _valueValid;
5458

5559
private:
5660
QString _name;

extplane-server/datarefs/doubledataref.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ double DoubleDataRef::value() {
1313
void DoubleDataRef::updateValue(double newValue) {
1414
if(_value != newValue) {
1515
_value = newValue;
16+
if(!_valueValid) setValueValid();
1617
emit changed(this);
1718
}
1819
}

extplane-server/datarefs/floatarraydataref.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ void FloatArrayDataRef::updateValue() {
3434
notequal = true;
3535
}
3636
}
37-
if (notequal) emit changed(this);
37+
if (notequal) {
38+
if(!_valueValid) setValueValid();
39+
emit changed(this);
40+
}
3841
}
3942

4043
QString FloatArrayDataRef::valueString() {

extplane-server/datarefs/floatdataref.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ float FloatDataRef::value() {
1515
void FloatDataRef::updateValue(float newValue) {
1616
if(_value != newValue) {
1717
_value = newValue;
18+
if(!_valueValid) setValueValid();
1819
emit changed(this);
1920
}
2021
}

extplane-server/datarefs/intarraydataref.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ void IntArrayDataRef::updateValue() {
2626
notequal = true;
2727
}
2828
}
29-
if (notequal) emit changed(this);
29+
if (notequal) {
30+
if(!_valueValid) setValueValid();
31+
emit changed(this);
32+
}
3033
}
3134

3235
QString IntArrayDataRef::valueString() {

extplane-server/datarefs/intdataref.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ int IntDataRef::value() {
1414
void IntDataRef::updateValue(int newValue) {
1515
if(_value != newValue) {
1616
_value = newValue;
17+
if(!_valueValid) setValueValid();
1718
emit changed(this);
1819
}
1920
}

extplane-server/tcpclient.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ void TcpClient::readClient() {
8989
_refValueB[ref] = qobject_cast<DataDataRef*>(ref)->value();
9090
}
9191
INFO << "Subscribed to " << ref->name() << ", accuracy " << accuracy << ", type " << ref->typeString();
92+
if(ref->isValid()) {
93+
emit ref->changed(ref); // Force update to all clients
94+
}
9295
if(command == "get") ref->setUnsubscribeAfterChange();
9396
} else {
9497
INFO << "Ref not found" << refName;
@@ -193,6 +196,8 @@ void TcpClient::readClient() {
193196

194197
void TcpClient::refChanged(DataRef *ref) {
195198
Q_ASSERT(_subscribedRefs.contains(ref));
199+
Q_ASSERT(ref->isValid()); // Never send invalid values.
200+
196201
if(ref->type()== extplaneRefTypeFloat) {
197202
FloatDataRef *refF = qobject_cast<FloatDataRef*>(ref);
198203
if(qAbs(refF->value() - _refValueF[ref]) < ref->accuracy())
@@ -279,6 +284,7 @@ QSet<QString> TcpClient::listRefs() {
279284

280285
return refNames;
281286
}
287+
282288
DataRef *TcpClient::getSubscribedRef(const QString &name) {
283289
for(DataRef* r : _subscribedRefs.values()) {
284290
if(r->name() == name)
@@ -296,6 +302,7 @@ void TcpClient::unsubscribeRef(const QString &name)
296302
_subscribedRefs.remove(ref);
297303
_refValueF.remove(ref);
298304
_refValueFA.remove(ref);
305+
_refValueD.remove(ref);
299306
_refValueB.remove(ref);
300307
_refValueI.remove(ref);
301308
_refValueIA.remove(ref);

0 commit comments

Comments
 (0)