From eff21056ecca3989d260562cfe1ce2b122d5f50e Mon Sep 17 00:00:00 2001 From: rogo Date: Sat, 6 Oct 2018 14:15:48 +0200 Subject: [PATCH] added send and update of power state --- mainwindow.cpp | 84 +++++++++++++++++++++++++++++++++++++------------- mainwindow.h | 10 ++++-- mainwindow.ui | 2 +- 3 files changed, 70 insertions(+), 26 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index dac8972..53bd11c 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -9,9 +9,17 @@ namespace yremote { MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { + QString DEFAULT_ADDRESS = "10.0.0.227"; + ui->setupUi(this); - manager = new QNetworkAccessManager(this); setFixedSize(size()); + ui->txt_address->setText(DEFAULT_ADDRESS); + mAddress = ui->txt_address->text(); + + // load initial data + sendCommand("" + "GetParam" + ""); } MainWindow::~MainWindow() @@ -19,39 +27,71 @@ MainWindow::~MainWindow() delete ui; } -void MainWindow::func() -{ - QNetworkAccessManager *manager = new QNetworkAccessManager(this); - connect(manager, SIGNAL(finished(QNetworkReply*)), - this, SLOT(replyFinished(QNetworkReply*))); - - manager->get(QNetworkRequest(QUrl("http://qt-project.org"))); -} - - void MainWindow::on_btn_onoff_clicked() { - qDebug() << "void MainWindow::on_btn_onoff_clicked()"; - sendCommand("GetParam"); + if (mPowered){ + sendCommand("" + "Standby" + ""); + + } else { + sendCommand("" + "On" + ""); + } + mPowered = !mPowered; + updateUi(); } -QString MainWindow::sendCommand(QString cmd) +void MainWindow::sendCommand(QString cmd) { - QString res; - QNetworkRequest request; request.setUrl("http://" + mAddress + ":80/YamahaRemoteControl/ctrl"); request.setRawHeader("Content-Type", "text/xml; charset=UTF-8"); request.setRawHeader("Content-Length", QByteArray::number(cmd.size())); - QNetworkReply *reply = manager->post(request, cmd.toUtf8()); + QNetworkAccessManager *networkManager = new QNetworkAccessManager(this); + mReply = networkManager->post(request, cmd.toUtf8()); + connect(networkManager, &QNetworkAccessManager::finished, this, &MainWindow::replyFinished); +} - QEventLoop loop; - connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); - loop.exec(); +void MainWindow::replyFinished() +{ + QString ans; + mReply->deleteLater(); - qDebug() << reply->readAll(); - return res; + if(mReply->error() == QNetworkReply::NoError) { + QByteArray data = mReply->readAll(); + ans = QString(data); + } else { + qDebug() << mReply->url(); + qDebug() << mReply->errorString(); + return; + } + + // check power state + qDebug() << ans; + QRegularExpression regex("(.*)"); + QRegularExpressionMatch match = regex.match(ans); + + if (match.hasMatch()) { + if (match.captured(1) == "On") mPowered = true; + else if (match.captured(1) == "Off") mPowered = false; + } + + updateUi(); +} + +void MainWindow::updateUi() { + ui->btn_onoff->setText(mPowered ? "On" : "Off"); + ui->dial->setEnabled(mPowered); + ui->lbl_volume->setEnabled(mPowered); + ui->btn_hdmi1->setEnabled(mPowered); + ui->btn_hdmi2->setEnabled(mPowered); + ui->btn_hdmi3->setEnabled(mPowered); + ui->btn_spotify->setEnabled(mPowered); + ui->btn_airplay->setEnabled(mPowered); + ui->btn_audioin->setEnabled(mPowered); } void MainWindow::on_txt_address_textEdited(const QString &arg1) diff --git a/mainwindow.h b/mainwindow.h index 22d4a64..e246284 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -23,15 +23,19 @@ protected: private slots: void on_btn_onoff_clicked(); - + void replyFinished(); void on_txt_address_textEdited(const QString &arg1); private: Ui::MainWindow *ui; - QString sendCommand(QString cmd); QString mAddress; - QNetworkAccessManager *manager; + QNetworkReply *mReply; + + bool mPowered = false; + + void sendCommand(QString cmd); + void updateUi(); }; } diff --git a/mainwindow.ui b/mainwindow.ui index 62d0386..0a45761 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -35,7 +35,7 @@ - 10.0.0.5 + 10.0.0.227 Qt::AlignCenter