mirror of
https://github.com/modelec/ihm.git
synced 2026-01-18 16:47:32 +01:00
TCP Client
This commit is contained in:
@@ -43,8 +43,8 @@ set(SOURCES
|
||||
preparation/Lidar.cpp
|
||||
preparation/OneItemPreparation.cpp
|
||||
preparation/TiretteState.cpp
|
||||
tcp/TCPClient.cpp
|
||||
tcp/TCPClient.h
|
||||
tcp/MyTCPClient.cpp
|
||||
tcp/MyTCPClient.h
|
||||
)
|
||||
|
||||
add_executable(ihm_robot resource.qrc ${HEADERS} ${SOURCES})
|
||||
|
||||
55
MainWindow.h
55
MainWindow.h
@@ -4,7 +4,6 @@
|
||||
#include <QWidget>
|
||||
#include <QStackedWidget>
|
||||
#include <QPixmap>
|
||||
#include <TCPSocket/TCPClient.hpp>
|
||||
#include <TCPSocket/TCPUtils.hpp>
|
||||
|
||||
#include "homeButton.h"
|
||||
@@ -13,12 +12,16 @@
|
||||
#include "PreparationMatch.h"
|
||||
#include "TeamChooser.h"
|
||||
#include "TestMode.h"
|
||||
#include "tcp/MyTCPClient.h"
|
||||
|
||||
class MainWindow : public QMainWindow, public TCPClient {
|
||||
class MainWindow : public QMainWindow {
|
||||
Q_OBJECT
|
||||
public:
|
||||
MainWindow(const char* address = "127.0.0.1", int port = 8080, QWidget* parent = nullptr) : QMainWindow(parent), TCPClient(address, port)
|
||||
MainWindow(const char* address = "127.0.0.1", int port = 8080, QWidget* parent = nullptr) : QMainWindow(parent)
|
||||
{
|
||||
this->socketClient = new MyTCPSocket(this);
|
||||
this->socketClient->connectToServer(address, port);
|
||||
connect(this->socketClient, &MyTCPSocket::messageReceived, this, &MainWindow::handleMessage);
|
||||
this->centralWidget = new QWidget(this);
|
||||
this->setCentralWidget(centralWidget);
|
||||
|
||||
@@ -49,7 +52,7 @@ public:
|
||||
|
||||
this->setFixedSize(QSize(480, 320));
|
||||
|
||||
this->home = new homeButton(centralWidget);
|
||||
this->home = new homeButton(centralidget);
|
||||
|
||||
connect(this->home, &homeButton::homologationClicked, this, &MainWindow::onHomologationPressed);
|
||||
|
||||
@@ -68,7 +71,7 @@ public:
|
||||
connect(this->preparationMatch, &PreparationMatch::startGame, this, &MainWindow::onStartGame);
|
||||
// connect(this->preparationMatch, &PreparationMatch::askTCPServer, this, &MainWindow::broadcastTCPMessage);
|
||||
connect(this->preparationMatch, &PreparationMatch::askTCPServer, [&](const std::string& message) {
|
||||
this->sendMessage(message.c_str());
|
||||
this->socketClient->sendMessage(message.c_str());
|
||||
});
|
||||
|
||||
this->testMode = new TestMode(centralWidget);
|
||||
@@ -109,26 +112,6 @@ public:
|
||||
this->stackedWidget->setCurrentIndex(index);
|
||||
}
|
||||
|
||||
void handleMessage(const std::string& message) override
|
||||
{
|
||||
std::cout << "Received message: " << message << std::endl;
|
||||
|
||||
std::vector<std::string> list = TCPSocket::split(message, ";");
|
||||
|
||||
if (TCPSocket::startWith(list[2], "pong"))
|
||||
{
|
||||
preparationMatch->responseFromPing(QString::fromStdString(message));
|
||||
}
|
||||
if (TCPSocket::contains(list[0], "tirette") && TCPSocket::contains(list[2], "set state"))
|
||||
{
|
||||
preparationMatch->responseTiretteState(QString::fromStdString(message));
|
||||
}
|
||||
if (TCPSocket::contains(list[0], "lidar"))
|
||||
{
|
||||
preparationMatch->responseLidar(QString::fromStdString(message));
|
||||
}
|
||||
}
|
||||
|
||||
protected slots:
|
||||
void onHomePressed()
|
||||
{
|
||||
@@ -170,6 +153,26 @@ protected slots:
|
||||
emit replierRobot();
|
||||
}
|
||||
|
||||
|
||||
void handleMessage(const std::string& message)
|
||||
{
|
||||
std::vector<std::string> list = TCPSocket::split(message, ";");
|
||||
|
||||
if (TCPSocket::startWith(list[2], "pong"))
|
||||
{
|
||||
preparationMatch->responseFromPing(QString::fromStdString(message));
|
||||
}
|
||||
if (TCPSocket::contains(list[0], "tirette") && TCPSocket::contains(list[2], "set state"))
|
||||
{
|
||||
preparationMatch->responseTiretteState(QString::fromStdString(message));
|
||||
}
|
||||
if (TCPSocket::contains(list[0], "lidar"))
|
||||
{
|
||||
preparationMatch->responseLidar(QString::fromStdString(message));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
signals:
|
||||
void deplierRobot();
|
||||
void replierRobot();
|
||||
@@ -189,4 +192,6 @@ private:
|
||||
PreparationMatch* preparationMatch;
|
||||
TestMode* testMode;
|
||||
InGame* inGame;
|
||||
|
||||
MyTCPSocket* socketClient;
|
||||
};
|
||||
|
||||
47
tcp/MyTCPClient.cpp
Normal file
47
tcp/MyTCPClient.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "MyTCPClient.h"
|
||||
#include <QHostAddress>
|
||||
|
||||
MyTCPSocket::MyTCPSocket(QObject *parent) : QObject(parent)
|
||||
{
|
||||
socket = new QTcpSocket(this);
|
||||
|
||||
connect(socket, &QTcpSocket::connected, this, &MyTCPSocket::onConnected);
|
||||
connect(socket, &QTcpSocket::disconnected, this, &MyTCPSocket::onDisconnected);
|
||||
connect(socket, &QTcpSocket::readyRead, this, &MyTCPSocket::onReadyRead);
|
||||
connect(socket, QOverload<QAbstractSocket::SocketError>::of(&QTcpSocket::errorOccurred),
|
||||
this, &MyTCPSocket::onErrorOccurred);
|
||||
}
|
||||
|
||||
void MyTCPSocket::connectToServer(const QString &host, quint16 port)
|
||||
{
|
||||
socket->connectToHost(host, port);
|
||||
}
|
||||
|
||||
void MyTCPSocket::sendMessage(const QByteArray &message)
|
||||
{
|
||||
if (socket->state() == QTcpSocket::ConnectedState) {
|
||||
socket->write(message);
|
||||
}
|
||||
}
|
||||
|
||||
void MyTCPSocket::onReadyRead()
|
||||
{
|
||||
QByteArray message = socket->readAll();
|
||||
emit messageReceived(message);
|
||||
}
|
||||
|
||||
void MyTCPSocket::onConnected()
|
||||
{
|
||||
emit connected();
|
||||
}
|
||||
|
||||
void MyTCPSocket::onDisconnected()
|
||||
{
|
||||
emit disconnected();
|
||||
}
|
||||
|
||||
void MyTCPSocket::onErrorOccurred(QAbstractSocket::SocketError socketError)
|
||||
{
|
||||
Q_UNUSED(socketError)
|
||||
emit errorOccurred(socket->errorString());
|
||||
}
|
||||
@@ -3,11 +3,11 @@
|
||||
#include <QObject>
|
||||
#include <QTcpSocket>
|
||||
|
||||
class TCPSocket : public QObject
|
||||
class MyTCPSocket : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit TCPSocket(QObject *parent = nullptr);
|
||||
explicit MyTCPSocket(QObject *parent = nullptr);
|
||||
void connectToServer(const QString &host, quint16 port);
|
||||
void sendMessage(const QByteArray &message);
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
#include "TCPClient.h"
|
||||
#include <QHostAddress>
|
||||
|
||||
TCPSocket::TCPSocket(QObject *parent) : QObject(parent)
|
||||
{
|
||||
socket = new QTcpSocket(this);
|
||||
|
||||
connect(socket, &QTcpSocket::connected, this, &TCPSocket::onConnected);
|
||||
connect(socket, &QTcpSocket::disconnected, this, &TCPSocket::onDisconnected);
|
||||
connect(socket, &QTcpSocket::readyRead, this, &TCPSocket::onReadyRead);
|
||||
connect(socket, QOverload<QAbstractSocket::SocketError>::of(&QTcpSocket::errorOccurred),
|
||||
this, &TCPSocket::onErrorOccurred);
|
||||
}
|
||||
|
||||
void TCPSocket::connectToServer(const QString &host, quint16 port)
|
||||
{
|
||||
socket->connectToHost(host, port);
|
||||
}
|
||||
|
||||
void TCPSocket::sendMessage(const QByteArray &message)
|
||||
{
|
||||
if (socket->state() == QTcpSocket::ConnectedState) {
|
||||
socket->write(message);
|
||||
}
|
||||
}
|
||||
|
||||
void TCPSocket::onReadyRead()
|
||||
{
|
||||
QByteArray message = socket->readAll();
|
||||
emit messageReceived(message);
|
||||
}
|
||||
|
||||
void TCPSocket::onConnected()
|
||||
{
|
||||
emit connected();
|
||||
}
|
||||
|
||||
void TCPSocket::onDisconnected()
|
||||
{
|
||||
emit disconnected();
|
||||
}
|
||||
|
||||
void TCPSocket::onErrorOccurred(QAbstractSocket::SocketError socketError)
|
||||
{
|
||||
Q_UNUSED(socketError)
|
||||
emit errorOccurred(socket->errorString());
|
||||
}
|
||||
Reference in New Issue
Block a user