implement ws
This commit is contained in:
parent
ccbef3b3e8
commit
31ccf2a5c7
@ -34,7 +34,6 @@ namespace autoapp
|
|||||||
MainWindow::MainWindow(QWidget *parent)
|
MainWindow::MainWindow(QWidget *parent)
|
||||||
: QMainWindow(parent), ui_(new Ui::MainWindow), webSocket(new QWebSocket),
|
: QMainWindow(parent), ui_(new Ui::MainWindow), webSocket(new QWebSocket),
|
||||||
reconnectTimer(new QTimer(this)),
|
reconnectTimer(new QTimer(this)),
|
||||||
metadataTimer(new QTimer(this)),
|
|
||||||
shuttingDown(false)
|
shuttingDown(false)
|
||||||
{
|
{
|
||||||
ui_->setupUi(this);
|
ui_->setupUi(this);
|
||||||
@ -43,10 +42,6 @@ namespace autoapp
|
|||||||
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);
|
||||||
|
|
||||||
// BT NOW PLAYING UPDATE
|
|
||||||
connect(metadataTimer, &QTimer::timeout, this, &MainWindow::updateNowPlaying);
|
|
||||||
metadataTimer->start(1000); // refresh every 5 seconds
|
|
||||||
|
|
||||||
// RADIO WEBSOCKET STUFF
|
// RADIO WEBSOCKET STUFF
|
||||||
reconnectTimer->setSingleShot(true);
|
reconnectTimer->setSingleShot(true);
|
||||||
reconnectTimer->setInterval(3000);
|
reconnectTimer->setInterval(3000);
|
||||||
@ -89,75 +84,6 @@ namespace autoapp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void autoapp::ui::MainWindow::updateBtNowPlaying()
|
|
||||||
{
|
|
||||||
QDBusInterface manager(
|
|
||||||
"org.bluez",
|
|
||||||
"/",
|
|
||||||
"org.freedesktop.DBus.ObjectManager",
|
|
||||||
QDBusConnection::systemBus());
|
|
||||||
|
|
||||||
if (!manager.isValid())
|
|
||||||
{
|
|
||||||
qDebug() << "Failed to connect to BlueZ D-Bus";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QDBusReply<QVariantMap> reply = manager.call("GetManagedObjects");
|
|
||||||
|
|
||||||
if (!reply.isValid())
|
|
||||||
{
|
|
||||||
qDebug() << "D-Bus call failed:" << reply.error().message();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QVariantMap allObjects = reply.value();
|
|
||||||
|
|
||||||
for (auto it = allObjects.begin(); it != allObjects.end(); ++it)
|
|
||||||
{
|
|
||||||
QString path = it.key();
|
|
||||||
QVariantMap interfaces = it.value().toMap();
|
|
||||||
|
|
||||||
if (interfaces.contains("org.bluez.MediaPlayer1"))
|
|
||||||
{
|
|
||||||
qDebug() << "Found MediaPlayer at:" << path;
|
|
||||||
|
|
||||||
QDBusInterface mediaPlayer(
|
|
||||||
"org.bluez",
|
|
||||||
path,
|
|
||||||
"org.freedesktop.DBus.Properties",
|
|
||||||
QDBusConnection::systemBus());
|
|
||||||
|
|
||||||
QDBusReply<QVariant> trackReply = mediaPlayer.call("Get", "org.bluez.MediaPlayer1", "Track");
|
|
||||||
if (trackReply.isValid())
|
|
||||||
{
|
|
||||||
QVariantMap track = trackReply.value().toMap();
|
|
||||||
QString artist = track["Artist"].toString();
|
|
||||||
QString title = track["Title"].toString();
|
|
||||||
QString album = track["Album"].toString();
|
|
||||||
|
|
||||||
QString status = statusReply.value().toString();
|
|
||||||
|
|
||||||
qDebug() << "Now playing:" << artist << "-" << title << "from" << album;
|
|
||||||
qDebug() << "Status:" << status;
|
|
||||||
|
|
||||||
if (status == "Playing")
|
|
||||||
{
|
|
||||||
//ui_->stackedWidget->setCurrentIndex(0);
|
|
||||||
//ui_->btSongName->setText(title);
|
|
||||||
//ui_->btArtistName->setText(artist);
|
|
||||||
//ui_->btAlbumName->setText(album);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//ui_->stackedWidget->setCurrentIndex(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void autoapp::ui::MainWindow::connectWebSocket()
|
void autoapp::ui::MainWindow::connectWebSocket()
|
||||||
{
|
{
|
||||||
QUrl url(QStringLiteral("ws://127.0.0.1:5000")); // Change to your real server
|
QUrl url(QStringLiteral("ws://127.0.0.1:5000")); // Change to your real server
|
||||||
@ -167,6 +93,21 @@ void autoapp::ui::MainWindow::connectWebSocket()
|
|||||||
|
|
||||||
void autoapp::ui::MainWindow::handleIncomingMessage(const QString &message)
|
void autoapp::ui::MainWindow::handleIncomingMessage(const QString &message)
|
||||||
{
|
{
|
||||||
// Your custom message processing logic here
|
|
||||||
qDebug() << "[Handler] Processing message:" << message;
|
qDebug() << "[Handler] Processing message:" << message;
|
||||||
|
|
||||||
|
QJsonDocument doc = QJsonDocument::fromJson(message.toUtf8(), &error);
|
||||||
|
QJsonObject rootObj = doc.object();
|
||||||
|
if (rootObj.contains("bluetooth")) {
|
||||||
|
ui_->stackedWidget->setCurrentIndex(0);
|
||||||
|
QJsonObject btObj = rootObj["bluetooth"].toObject();
|
||||||
|
ui_->btSongName->setText(btObj["song"].toString());
|
||||||
|
ui_->btArtistName->setText(btObj["artist"].toString());
|
||||||
|
ui_->btAlbumName->setText(btObj["album"].toString());
|
||||||
|
} else if (rootObj.contains("radio")) {
|
||||||
|
ui_->stackedWidget->setCurrentIndex(1);
|
||||||
|
QJsonObject radioObj = rootObj["radio"].toObject();
|
||||||
|
ui_->radioScreen->setText(radioObj["screen"].toString());
|
||||||
|
ui_->radioPreset->setText(radioObj["preset"].toString());
|
||||||
|
ui_->radioSecondLine->setText(radioObj["second"].toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,7 +48,6 @@ namespace autoapp
|
|||||||
private slots:
|
private slots:
|
||||||
void connectWebSocket();
|
void connectWebSocket();
|
||||||
void handleIncomingMessage(const QString &message);
|
void handleIncomingMessage(const QString &message);
|
||||||
void updateBtNowPlaying();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::MainWindow *ui_;
|
Ui::MainWindow *ui_;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user