-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtcpclient.cpp
135 lines (111 loc) · 4.02 KB
/
tcpclient.cpp
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
/*
* This application is visualizes data from imu-utils over tcp
*
* Copyright (C) 2013 Simon Stürz ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <QDebug>
#include <QJsonDocument>
#include "tcpclient.h"
TcpClient::TcpClient(QObject *parent) :
QObject(parent)
{
m_tcpSocket = new QTcpSocket(this);
connect(m_tcpSocket,SIGNAL(connected()),this,SLOT(connected()));
connect(m_tcpSocket,SIGNAL(disconnected()),this,SLOT(disconnected()));
connect(m_tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(connectionError(QAbstractSocket::SocketError)));
connect(this,SIGNAL(jsonDataAvailable(QByteArray)),this,SLOT(parseJsonData(QByteArray)));
}
void TcpClient::connectionError(QAbstractSocket::SocketError error)
{
emit connectionStatusChanged(false);
qDebug() << "Connection ERROR: " << error;
}
void TcpClient::connected()
{
emit connectionStatusChanged(true);
qDebug() << "connected to server.";
connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readData()));
}
void TcpClient::disconnected()
{
emit connectionStatusChanged(false);
qDebug() << "disconnected from server.";
}
void TcpClient::parseJsonData(const QByteArray &data)
{
QJsonParseError error;
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
if(error.error != QJsonParseError::NoError) {
qDebug() << "failed to parse data" << data << ":" << error.errorString();
return;
}
QVector3D accVector;
QVector3D gyrVector;
QVector3D magVector;
QVariantMap messageMap = jsonDoc.toVariant().toMap();
// check if we got all 3 sensordata
if(messageMap.contains("acc")){
QVariantMap accMap = messageMap.value("acc").toMap();
accVector.setX(accMap.value("x").toFloat());
accVector.setY(accMap.value("y").toFloat());
accVector.setZ(accMap.value("z").toFloat());
QVariantMap gyrMap = messageMap.value("gyr").toMap();
gyrVector.setX(gyrMap.value("x").toFloat());
gyrVector.setY(gyrMap.value("y").toFloat());
gyrVector.setZ(gyrMap.value("z").toFloat());
QVariantMap magMap = messageMap.value("mag").toMap();
magVector.setX(magMap.value("x").toFloat());
magVector.setY(magMap.value("y").toFloat());
magVector.setZ(magMap.value("z").toFloat());
emit imuDataAvailable(accVector,gyrVector,magVector);
}
// check if we got all angle data
if(messageMap.contains("angles")){
QVariantMap angleMap = messageMap.value("angles").toMap();
float roll = angleMap.value("roll").toFloat();
float pitch = angleMap.value("pitch").toFloat();
float yaw = angleMap.value("yaw").toFloat();
emit anglesAvailabe(roll,pitch,yaw);
}else{
qDebug() << "ERROR: message not complete.";
return;
}
}
void TcpClient::connectToHost(QString ipAddress, QString port)
{
m_tcpSocket->connectToHost(QHostAddress(ipAddress), port.toInt());
}
void TcpClient::disconnectFromHost()
{
m_tcpSocket->close();
}
void TcpClient::sendData(QByteArray data)
{
m_tcpSocket->write(data + "\n");
}
void TcpClient::readData()
{
QByteArray message;
while(m_tcpSocket->canReadLine()){
QByteArray dataLine = m_tcpSocket->readLine();
message.append(dataLine);
if(dataLine == "}\n"){
//qDebug() << message;
emit jsonDataAvailable(message);
message.clear();
}
}
}