Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Ville Ranki committed Mar 6, 2018
2 parents 8cf3629 + 3df4906 commit 949fabc
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 8 deletions.
3 changes: 1 addition & 2 deletions extplane-plugin/xplaneplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ DataRef* XPlanePlugin::subscribeRef(QString &name) {
if(ref->name() == name) {
DEBUG << "Already subscribed to " << name;
ref->setSubscriberCount(ref->subscriberCount() + 1);
emit ref->changed(ref); // Force update to all clients
return ref;
}
}
Expand Down Expand Up @@ -162,7 +161,7 @@ void XPlanePlugin::updateDataRef(DataRef *ref)
switch (ref->type()) {
case extplaneRefTypeFloat:
{
float newValue = XPLMGetDataf(ref);
float newValue = XPLMGetDataf(ref->ref());
qobject_cast<FloatDataRef*>(ref)->updateValue(newValue);
break;
};
Expand Down
1 change: 1 addition & 0 deletions extplane-server/datarefs/datadataref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void DataDataRef::updateValue() {
// 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
if (_newValue != _value) {
_value = _newValue;
if(!_valueValid) setValueValid();
emit changed(this);
}
}
Expand Down
18 changes: 16 additions & 2 deletions extplane-server/datarefs/dataref.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "dataref.h"

DataRef::DataRef(QObject *parent, QString name, void *ref) : QObject(parent)
, _typeString("?")
, _type(extplaneRefTypeUnknown)
, _ref(ref)
, _accuracy(0)
, _valueValid(false)
, _name(name)
, _subscriberCount(0)
, _type(extplaneRefTypeUnknown)
, _writable(false)
, _typeString("?")
, _unsubscribeAfterChange(false)
{ }

Expand Down Expand Up @@ -64,3 +66,15 @@ void DataRef::setUnsubscribeAfterChange() {
bool DataRef::shouldUnsubscribeAfterChange() const {
return _unsubscribeAfterChange;
}

bool DataRef::isValid() const
{
return _valueValid;
}

void DataRef::setValueValid()
{
if(!_valueValid) {
_valueValid = true;
}
}
8 changes: 6 additions & 2 deletions extplane-server/datarefs/dataref.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ enum {
*/
class DataRef : public QObject {
Q_OBJECT

public:
DataRef(QObject *parent, QString name, void* ref);
const QString &name() const;
Expand All @@ -43,14 +42,19 @@ class DataRef : public QObject {
double accuracy() { return _accuracy; }
void setUnsubscribeAfterChange();
bool shouldUnsubscribeAfterChange() const;
bool isValid() const; // True if the value has been set initially. False if not.

signals:
void changed(DataRef *ref);
void changed(DataRef *ref); // Should not be emitted if value is not valid.

protected:
void setValueValid(); // Call this to mark the value valid.

QString _typeString;
extplaneRefID _type;
void* _ref;
double _accuracy;
bool _valueValid;

private:
QString _name;
Expand Down
1 change: 1 addition & 0 deletions extplane-server/datarefs/doubledataref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ double DoubleDataRef::value() {
void DoubleDataRef::updateValue(double newValue) {
if(_value != newValue) {
_value = newValue;
if(!_valueValid) setValueValid();
emit changed(this);
}
}
Expand Down
5 changes: 4 additions & 1 deletion extplane-server/datarefs/floatarraydataref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ void FloatArrayDataRef::updateValue() {
notequal = true;
}
}
if (notequal) emit changed(this);
if (notequal) {
if(!_valueValid) setValueValid();
emit changed(this);
}
}

QString FloatArrayDataRef::valueString() {
Expand Down
1 change: 1 addition & 0 deletions extplane-server/datarefs/floatdataref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ float FloatDataRef::value() {
void FloatDataRef::updateValue(float newValue) {
if(_value != newValue) {
_value = newValue;
if(!_valueValid) setValueValid();
emit changed(this);
}
}
Expand Down
5 changes: 4 additions & 1 deletion extplane-server/datarefs/intarraydataref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ void IntArrayDataRef::updateValue() {
notequal = true;
}
}
if (notequal) emit changed(this);
if (notequal) {
if(!_valueValid) setValueValid();
emit changed(this);
}
}

QString IntArrayDataRef::valueString() {
Expand Down
1 change: 1 addition & 0 deletions extplane-server/datarefs/intdataref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ int IntDataRef::value() {
void IntDataRef::updateValue(int newValue) {
if(_value != newValue) {
_value = newValue;
if(!_valueValid) setValueValid();
emit changed(this);
}
}
Expand Down
7 changes: 7 additions & 0 deletions extplane-server/tcpclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ void TcpClient::readClient() {
_refValueB[ref] = qobject_cast<DataDataRef*>(ref)->value();
}
INFO << "Subscribed to " << ref->name() << ", accuracy " << accuracy << ", type " << ref->typeString();
if(ref->isValid()) {
emit ref->changed(ref); // Force update to all clients
}
if(command == "get") ref->setUnsubscribeAfterChange();
} else {
INFO << "Ref not found" << refName;
Expand Down Expand Up @@ -193,6 +196,8 @@ void TcpClient::readClient() {

void TcpClient::refChanged(DataRef *ref) {
Q_ASSERT(_subscribedRefs.contains(ref));
Q_ASSERT(ref->isValid()); // Never send invalid values.

if(ref->type()== extplaneRefTypeFloat) {
FloatDataRef *refF = qobject_cast<FloatDataRef*>(ref);
if(qAbs(refF->value() - _refValueF[ref]) < ref->accuracy())
Expand Down Expand Up @@ -279,6 +284,7 @@ QSet<QString> TcpClient::listRefs() {

return refNames;
}

DataRef *TcpClient::getSubscribedRef(const QString &name) {
for(DataRef* r : _subscribedRefs.values()) {
if(r->name() == name)
Expand All @@ -296,6 +302,7 @@ void TcpClient::unsubscribeRef(const QString &name)
_subscribedRefs.remove(ref);
_refValueF.remove(ref);
_refValueFA.remove(ref);
_refValueD.remove(ref);
_refValueB.remove(ref);
_refValueI.remove(ref);
_refValueIA.remove(ref);
Expand Down

0 comments on commit 949fabc

Please sign in to comment.