forked from vranki/ExtPlane-Panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextplaneconnection.cpp
More file actions
150 lines (132 loc) · 4.76 KB
/
extplaneconnection.cpp
File metadata and controls
150 lines (132 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "extplaneconnection.h"
ExtPlaneConnection::ExtPlaneConnection(QObject *parent) : QTcpSocket(parent) {
connect(this, SIGNAL(connected()), this, SLOT(socketConnected()));
connect(this, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));
connect(this, SIGNAL(readyRead()), this, SLOT(readClient()));
connect(&reconnectTimer, SIGNAL(timeout()), this, SLOT(tryReconnect()));
server_ok = false;
}
void ExtPlaneConnection::connectTo(QHostAddress addr, unsigned int port) {
close();
_addr = addr;
_port = port;
tryReconnect();
}
void ExtPlaneConnection::tryReconnect() {
qDebug() << Q_FUNC_INFO << _addr.toString() << _port;
reconnectTimer.stop();
connectToHost(_addr, _port);
}
void ExtPlaneConnection::socketConnected() {
qDebug() << Q_FUNC_INFO ;
emit connectionError("Connected to ExtPlane, waiting for handshake");
reconnectTimer.stop();
}
void ExtPlaneConnection::socketError(QAbstractSocket::SocketError err) {
qDebug() << Q_FUNC_INFO << err;
server_ok = false;
emit connectionError(errorString() + " : " + peerName() + ":" + QString::number(peerPort()));
reconnectTimer.setInterval(5000);
reconnectTimer.start();
}
void ExtPlaneConnection::registerClient(ExtPlaneClient* client) {
clients.append(client);
}
ClientDataRef *ExtPlaneConnection::subscribeDataRef(QString name, double accuracy) {
ClientDataRef *ref = dataRefs.value(name);
if(ref){
qDebug() << Q_FUNC_INFO << "Ref already subscribed";
ref->setSubscribers(ref->subscribers()+1);
if(accuracy > ref->accuracy()) {
// @todo update accuracy
}
} else {
ref = new ClientDataRef(this, name, accuracy);
dataRefs[ref->name()] = ref;
ref->setSubscribers(1);
if(server_ok)
subRef(ref);
}
qDebug() << Q_FUNC_INFO << name << ref->subscribers() << server_ok;
return ref;
}
void ExtPlaneConnection::unsubscribeDataRef(ClientDataRef *ref) {
qDebug() << Q_FUNC_INFO << ref->name() << ref->subscribers();
ref->setSubscribers(ref->subscribers() - 1);
if(ref->subscribers() > 0) return;
qDebug() << Q_FUNC_INFO << "Ref not subscribed by anyone anymore";
if(server_ok)
writeLine("unsub " + ref->name());
dataRefs.remove(ref->name());
ref->deleteLater();
foreach(ClientDataRef *ref, dataRefs) {
qDebug() << "refs now:" << ref->name();
}
}
void ExtPlaneConnection::readClient() {
while(canReadLine()) {
QByteArray lineBA = readLine();
QString line = QString(lineBA).trimmed();
qDebug() << Q_FUNC_INFO << "Server says: " << line;
if(!server_ok) {
if(line=="EXTPLANE 1") {
server_ok = true;
emit connectionError("");
// Sub all refs
foreach(ClientDataRef *ref, dataRefs)
subRef(ref);
}
return;
} else {
QStringList cmd = line.split(" ", QString::SkipEmptyParts);
if (cmd.value(0)=="ufa"){
qDebug() << Q_FUNC_INFO << "Found float array: " << cmd;
ClientDataRef *ref = dataRefs.value(cmd.value(1));
if(ref) {
ref->updateValue(cmd.join(" "));
} else {
qDebug() << Q_FUNC_INFO << "ref not subscribed " << cmd.value(2);
}
} else if(cmd.size()==3) {
if ((cmd.value(0)=="uf")||(cmd.value(0)=="ui")) {
ClientDataRef *ref = dataRefs.value(cmd.value(1));
if(ref) {
ref->updateValue(cmd.value(2));
} else {
qDebug() << Q_FUNC_INFO << "ref not subscribed " << cmd.value(2);
}
}
}
}
}
}
void ExtPlaneConnection::subRef(ClientDataRef *ref) {
qDebug() << Q_FUNC_INFO;
Q_ASSERT(server_ok);
QString subLine = "sub " + ref->name();
if(ref->accuracy() != 0) {
subLine += " " + QString::number(ref->accuracy());
}
writeLine(subLine);
}
void ExtPlaneConnection::writeLine(QString line) {
if(!server_ok) {
qDebug() << Q_FUNC_INFO << "Connection not ok! Tried to write" << line;
return;
}
line +="\n";
write(line.toUtf8());
qDebug() << Q_FUNC_INFO << line;
}
void ExtPlaneConnection::keyPress(int id) {
QString line = "key " + QString::number(id);
writeLine(line);
}
void ExtPlaneConnection::buttonPress(int id) {
QString line = "but " + QString::number(id);
writeLine(line);
}
void ExtPlaneConnection::buttonRelease(int id) {
QString line = "rel " + QString::number(id);
writeLine(line);
}