refactor ws code and add remote wireless aa connect

This commit is contained in:
marios8543 2025-05-10 03:13:20 +03:00
parent b563281f78
commit e30db459c6
6 changed files with 202 additions and 193 deletions

View File

@ -8,11 +8,7 @@ namespace ui
{ {
ConnectDialog::ConnectDialog(boost::asio::io_service &ioService, aasdk::tcp::ITCPWrapper &tcpWrapper, openauto::configuration::IRecentAddressesList &recentAddressesList, QWidget *parent) ConnectDialog::ConnectDialog(boost::asio::io_service &ioService, aasdk::tcp::ITCPWrapper &tcpWrapper, openauto::configuration::IRecentAddressesList &recentAddressesList, QWidget *parent)
: QDialog(parent) : QDialog(parent), ioService_(ioService), tcpWrapper_(tcpWrapper), recentAddressesList_(recentAddressesList), ui_(new Ui::ConnectDialog)
, ioService_(ioService)
, tcpWrapper_(tcpWrapper)
, recentAddressesList_(recentAddressesList)
, ui_(new Ui::ConnectDialog)
{ {
qRegisterMetaType<aasdk::tcp::ITCPEndpoint::SocketPointer>("aasdk::tcp::ITCPEndpoint::SocketPointer"); qRegisterMetaType<aasdk::tcp::ITCPEndpoint::SocketPointer>("aasdk::tcp::ITCPEndpoint::SocketPointer");
qRegisterMetaType<std::string>("std::string"); qRegisterMetaType<std::string>("std::string");
@ -115,5 +111,18 @@ void ConnectDialog::insertIpAddress(const std::string& ipAddress)
this->loadRecentList(); this->loadRecentList();
} }
void ConnectDialog::connectToAddress(const QString &ip)
{
const auto &ipAddress = ip.toStdString();
auto socket = std::make_shared<boost::asio::ip::tcp::socket>(ioService_);
try
{
tcpWrapper_.asyncConnect(*socket, ipAddress, 5277, std::bind(&ConnectDialog::connectHandler, this, std::placeholders::_1, ipAddress, socket));
}
catch (const boost::system::system_error &se)
{
emit connectionFailed(QString(se.what()));
}
}
} }
} }

View File

@ -17,12 +17,8 @@
*/ */
#include <QApplication> #include <QApplication>
#include <QtWebSockets/QWebSocket>
#include <QDebug> #include <QDebug>
#include <QTimer>
#include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
#include <QJsonParseError>
#include "autoapp/UI/MainWindow.hpp" #include "autoapp/UI/MainWindow.hpp"
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
@ -32,77 +28,25 @@ namespace autoapp
namespace ui namespace ui
{ {
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui_(new Ui::MainWindow), webSocket(new QWebSocket), : QMainWindow(parent), ui_(new Ui::MainWindow)
reconnectTimer(new QTimer(this)),
shuttingDown(false)
{ {
ui_->setupUi(this); ui_->setupUi(this);
connect(ui_->pushButtonSettings, &QPushButton::clicked, this, &MainWindow::openSettings); connect(ui_->pushButtonSettings, &QPushButton::clicked, this, &MainWindow::openSettings);
connect(ui_->pushButtonExit, &QPushButton::clicked, this, &MainWindow::exit); connect(ui_->pushButtonExit, &QPushButton::clicked, this, &MainWindow::exit);
connect(ui_->pushButtonToggleCursor, &QPushButton::clicked, this, &MainWindow::toggleCursor); connect(ui_->pushButtonToggleCursor, &QPushButton::clicked, this, &MainWindow::toggleCursor);
connect(ui_->pushButtonWirelessConnection, &QPushButton::clicked, this, &MainWindow::openConnectDialog); connect(ui_->pushButtonWirelessConnection, &QPushButton::clicked, this, &MainWindow::openConnectDialog);
// RADIO WEBSOCKET STUFF
reconnectTimer->setSingleShot(true);
reconnectTimer->setInterval(3000);
connect(reconnectTimer, &QTimer::timeout, this, &MainWindow::connectWebSocket);
connect(webSocket, &QWebSocket::connected, this, [this]()
{
qDebug() << "WebSocket connected";
webSocket->sendTextMessage(QStringLiteral("Hello from MainWindow WebSocket!")); });
connect(webSocket, &QWebSocket::textMessageReceived, this, [this](const QString &message)
{
qDebug() << "Message received:" << message;
handleIncomingMessage(message); });
connect(webSocket, &QWebSocket::disconnected, this, [this]()
{
qDebug() << "WebSocket disconnected";
if (!shuttingDown) {
qDebug() << "Attempting to reconnect in 3 seconds...";
reconnectTimer->start();
} });
connect(webSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error),
this, [this](QAbstractSocket::SocketError)
{ qDebug() << "WebSocket error:" << webSocket->errorString(); });
connectWebSocket();
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
{ {
shuttingDown = true;
webSocket->abort();
webSocket->deleteLater();
delete ui_; delete ui_;
} }
} }
} }
void autoapp::ui::MainWindow::connectWebSocket() void autoapp::ui::MainWindow::handleIncomingMessage(const QJsonObject &rootObj)
{ {
QUrl url(QStringLiteral("ws://127.0.0.1:5000")); // Change to your real server
qDebug() << "Connecting to WebSocket:" << url;
webSocket->open(url);
}
void autoapp::ui::MainWindow::handleIncomingMessage(const QString &message)
{
qDebug() << "[Handler] Processing message:" << message;
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(message.toUtf8(), &error);
if (error.error != QJsonParseError::NoError) {
qDebug() << "JSON parse error:" << error.errorString();
return;
}
QJsonObject rootObj = doc.object();
if (rootObj.contains("bluetooth")) { if (rootObj.contains("bluetooth")) {
ui_->stackedWidget->setCurrentIndex(0); ui_->stackedWidget->setCurrentIndex(0);
QJsonObject btObj = rootObj["bluetooth"].toObject(); QJsonObject btObj = rootObj["bluetooth"].toObject();
@ -121,5 +65,8 @@ void autoapp::ui::MainWindow::handleIncomingMessage(const QString &message)
ui_->radioScreen->setText(radioObj["screen"].toString()); ui_->radioScreen->setText(radioObj["screen"].toString());
ui_->radioPreset->setText(radioObj["preset"].toString()); ui_->radioPreset->setText(radioObj["preset"].toString());
ui_->radioSecondLine->setText(radioObj["second"].toString()); ui_->radioSecondLine->setText(radioObj["second"].toString());
} else if (rootObj.contains("connect_wireless_aa")) {
QJsonObject aaObj = rootObj["connect_wireless_aa"].toObject();
//connectWirelessAndroidAuto(aaObj["address"].toString());
} }
} }

View File

@ -18,6 +18,11 @@
#include <thread> #include <thread>
#include <QApplication> #include <QApplication>
#include <QtWebSockets/QWebSocket>
#include <QTimer>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include "aasdk/USB/USBHub.hpp" #include "aasdk/USB/USBHub.hpp"
#include "aasdk/USB/ConnectedAccessoriesEnumerator.hpp" #include "aasdk/USB/ConnectedAccessoriesEnumerator.hpp"
#include "aasdk/USB/AccessoryModeQueryChain.hpp" #include "aasdk/USB/AccessoryModeQueryChain.hpp"
@ -40,7 +45,8 @@ using ThreadPool = std::vector<std::thread>;
void startUSBWorkers(boost::asio::io_service &ioService, libusb_context *usbContext, ThreadPool &threadPool) void startUSBWorkers(boost::asio::io_service &ioService, libusb_context *usbContext, ThreadPool &threadPool)
{ {
auto usbWorker = [&ioService, usbContext]() { auto usbWorker = [&ioService, usbContext]()
{
timeval libusbEventTimeout{180, 0}; timeval libusbEventTimeout{180, 0};
while (!ioService.stopped()) while (!ioService.stopped())
@ -57,7 +63,8 @@ void startUSBWorkers(boost::asio::io_service& ioService, libusb_context* usbCont
void startIOServiceWorkers(boost::asio::io_service &ioService, ThreadPool &threadPool) void startIOServiceWorkers(boost::asio::io_service &ioService, ThreadPool &threadPool)
{ {
auto ioServiceWorker = [&ioService]() { auto ioServiceWorker = [&ioService]()
{
ioService.run(); ioService.run();
}; };
@ -97,15 +104,16 @@ int main(int argc, char* argv[])
autoapp::ui::ConnectDialog connectDialog(ioService, tcpWrapper, recentAddressesList); autoapp::ui::ConnectDialog connectDialog(ioService, tcpWrapper, recentAddressesList);
connectDialog.setWindowFlags(Qt::WindowStaysOnTopHint); connectDialog.setWindowFlags(Qt::WindowStaysOnTopHint);
QObject::connect(&mainWindow, &autoapp::ui::MainWindow::exit, []() { std::exit(0); }); QObject::connect(&mainWindow, &autoapp::ui::MainWindow::exit, []()
{ std::exit(0); });
QObject::connect(&mainWindow, &autoapp::ui::MainWindow::openSettings, &settingsWindow, &autoapp::ui::SettingsWindow::showFullScreen); QObject::connect(&mainWindow, &autoapp::ui::MainWindow::openSettings, &settingsWindow, &autoapp::ui::SettingsWindow::showFullScreen);
QObject::connect(&mainWindow, &autoapp::ui::MainWindow::openConnectDialog, &connectDialog, &autoapp::ui::ConnectDialog::exec); QObject::connect(&mainWindow, &autoapp::ui::MainWindow::openConnectDialog, &connectDialog, &autoapp::ui::ConnectDialog::exec);
qApplication.setOverrideCursor(Qt::BlankCursor); qApplication.setOverrideCursor(Qt::BlankCursor);
QObject::connect(&mainWindow, &autoapp::ui::MainWindow::toggleCursor, [&qApplication]() { QObject::connect(&mainWindow, &autoapp::ui::MainWindow::toggleCursor, [&qApplication]()
{
const auto cursor = qApplication.overrideCursor()->shape() == Qt::BlankCursor ? Qt::ArrowCursor : Qt::BlankCursor; const auto cursor = qApplication.overrideCursor()->shape() == Qt::BlankCursor ? Qt::ArrowCursor : Qt::BlankCursor;
qApplication.setOverrideCursor(cursor); qApplication.setOverrideCursor(cursor); });
});
mainWindow.showFullScreen(); mainWindow.showFullScreen();
@ -119,12 +127,62 @@ int main(int argc, char* argv[])
auto connectedAccessoriesEnumerator(std::make_shared<aasdk::usb::ConnectedAccessoriesEnumerator>(usbWrapper, ioService, queryChainFactory)); auto connectedAccessoriesEnumerator(std::make_shared<aasdk::usb::ConnectedAccessoriesEnumerator>(usbWrapper, ioService, queryChainFactory));
auto app = std::make_shared<openauto::App>(ioService, usbWrapper, tcpWrapper, androidAutoEntityFactory, std::move(usbHub), std::move(connectedAccessoriesEnumerator)); auto app = std::make_shared<openauto::App>(ioService, usbWrapper, tcpWrapper, androidAutoEntityFactory, std::move(usbHub), std::move(connectedAccessoriesEnumerator));
QObject::connect(&connectDialog, &autoapp::ui::ConnectDialog::connectionSucceed, [&app](auto socket) { QObject::connect(&connectDialog, &autoapp::ui::ConnectDialog::connectionSucceed, [&app](auto socket)
app->start(std::move(socket)); { app->start(std::move(socket)); });
});
app->waitForDevice(true); app->waitForDevice(true);
QWebSocket webSocket;
QTimer reconnectTimer;
const QUrl websocketUrl(QStringLiteral("ws://127.0.0.1:5959/ws"));
auto tryConnect = [&webSocket, &websocketUrl]()
{
if (webSocket.state() != QAbstractSocket::ConnectedState &&
webSocket.state() != QAbstractSocket::ConnectingState)
{
qDebug() << "[WebSocket] Attempting to connect to" << websocketUrl.toString();
webSocket.open(websocketUrl);
}
};
// Connect events
QObject::connect(&webSocket, &QWebSocket::connected, [&]()
{
qDebug() << "[WebSocket] Connected.";
webSocket.sendTextMessage("Hello from OpenAuto WebSocket client!");
reconnectTimer.stop(); });
QObject::connect(&webSocket, &QWebSocket::disconnected, [&]()
{
qDebug() << "[WebSocket] Disconnected. Will retry...";
reconnectTimer.start(3000); });
QObject::connect(&webSocket, &QWebSocket::textMessageReceived, [&mainWindow, &connectDialog](const QString &message)
{
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(message.toUtf8(), &error);
if (error.error != QJsonParseError::NoError) {
qDebug() << "JSON parse error:" << error.errorString();
return;
}
QJsonObject rootObj = doc.object();
if (rootObj.contains("wireless_aa_ip")) {
QString ipAddr = rootObj["wireless_aa_ip"].toString();
connectDialog.connectToAddress(ipAddr);
}
else if (rootObj.contains("button")) {
}
else{
mainWindow.handleIncomingMessage(rootObj);
} });
reconnectTimer.setInterval(3000);
reconnectTimer.setSingleShot(true);
QObject::connect(&reconnectTimer, &QTimer::timeout, tryConnect);
tryConnect();
auto result = qApplication.exec(); auto result = qApplication.exec();
std::for_each(threadPool.begin(), threadPool.end(), std::bind(&std::thread::join, std::placeholders::_1)); std::for_each(threadPool.begin(), threadPool.end(), std::bind(&std::thread::join, std::placeholders::_1));

View File

@ -22,6 +22,7 @@ class ConnectDialog : public QDialog
public: public:
explicit ConnectDialog(boost::asio::io_service& ioService, aasdk::tcp::ITCPWrapper& tcpWrapper, openauto::configuration::IRecentAddressesList& recentAddressesList, QWidget *parent = nullptr); explicit ConnectDialog(boost::asio::io_service& ioService, aasdk::tcp::ITCPWrapper& tcpWrapper, openauto::configuration::IRecentAddressesList& recentAddressesList, QWidget *parent = nullptr);
~ConnectDialog() override; ~ConnectDialog() override;
void connectToAddress(const QString& ip);
signals: signals:
void connectToDevice(const QString& ipAddress); void connectToDevice(const QString& ipAddress);

View File

@ -20,7 +20,6 @@
#include <memory> #include <memory>
#include <QMainWindow> #include <QMainWindow>
#include <QtWebSockets/QWebSocket>
namespace Ui namespace Ui
{ {
@ -38,6 +37,7 @@ namespace autoapp
public: public:
explicit MainWindow(QWidget *parent = nullptr); explicit MainWindow(QWidget *parent = nullptr);
~MainWindow() override; ~MainWindow() override;
void handleIncomingMessage(const QJsonObject &rootObj);
signals: signals:
void exit(); void exit();
@ -46,15 +46,9 @@ namespace autoapp
void openConnectDialog(); void openConnectDialog();
private slots: private slots:
void connectWebSocket();
void handleIncomingMessage(const QString &message);
private: private:
Ui::MainWindow *ui_; Ui::MainWindow *ui_;
QWebSocket *webSocket;
QTimer *reconnectTimer;
bool shuttingDown;
}; };
} }
} }

View File

@ -97,7 +97,7 @@ void Configuration::load()
videoMargins_ = QRect(0, 0, iniConfig.get<int32_t>(cVideoMarginWidth, 0), iniConfig.get<int32_t>(cVideoMarginHeight, 0)); videoMargins_ = QRect(0, 0, iniConfig.get<int32_t>(cVideoMarginWidth, 0), iniConfig.get<int32_t>(cVideoMarginHeight, 0));
whitescreenWorkaround_ = iniConfig.get<bool>(cVideoWhitescreenWorkaround, true); whitescreenWorkaround_ = iniConfig.get<bool>(cVideoWhitescreenWorkaround, true);
enableTouchscreen_ = iniConfig.get<bool>(cInputEnableTouchscreenKey, true); enableTouchscreen_ = iniConfig.get<bool>(cInputEnableTouchscreenKey, false);
this->readButtonCodes(iniConfig); this->readButtonCodes(iniConfig);
bluetoothAdapterType_ = static_cast<BluetoothAdapterType>(iniConfig.get<uint32_t>(cBluetoothAdapterTypeKey, bluetoothAdapterType_ = static_cast<BluetoothAdapterType>(iniConfig.get<uint32_t>(cBluetoothAdapterTypeKey,
@ -134,7 +134,7 @@ void Configuration::reset()
omxLayerIndex_ = 1; omxLayerIndex_ = 1;
videoMargins_ = QRect(0, 0, 0, 0); videoMargins_ = QRect(0, 0, 0, 0);
whitescreenWorkaround_ = true; whitescreenWorkaround_ = true;
enableTouchscreen_ = true; enableTouchscreen_ = false;
buttonCodes_.clear(); buttonCodes_.clear();
bluetoothAdapterType_ = BluetoothAdapterType::NONE; bluetoothAdapterType_ = BluetoothAdapterType::NONE;
bluetoothRemoteAdapterAddress_ = ""; bluetoothRemoteAdapterAddress_ = "";