added send and update of power state

This commit is contained in:
2018-10-06 14:15:48 +02:00
parent 1a90c8cb99
commit eff21056ec
3 changed files with 70 additions and 26 deletions

View File

@@ -9,9 +9,17 @@ namespace yremote {
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{ {
QString DEFAULT_ADDRESS = "10.0.0.227";
ui->setupUi(this); ui->setupUi(this);
manager = new QNetworkAccessManager(this);
setFixedSize(size()); setFixedSize(size());
ui->txt_address->setText(DEFAULT_ADDRESS);
mAddress = ui->txt_address->text();
// load initial data
sendCommand("<YAMAHA_AV cmd=\"GET\"><System>"
"<Power_Control><Power>GetParam</Power></Power_Control>"
"</System></YAMAHA_AV>");
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
@@ -19,39 +27,71 @@ MainWindow::~MainWindow()
delete ui; 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() void MainWindow::on_btn_onoff_clicked()
{ {
qDebug() << "void MainWindow::on_btn_onoff_clicked()"; if (mPowered){
sendCommand("<YAMAHA_AV cmd=\"GET\"><Main_Zone><Input><Input_Sel>GetParam</Input_Sel></Input></Main_Zone></YAMAHA_AV>"); sendCommand("<YAMAHA_AV cmd=\"PUT\"><Main_Zone>"
"<Power_Control><Power>Standby</Power></Power_Control>"
"</Main_Zone></YAMAHA_AV>");
} else {
sendCommand("<YAMAHA_AV cmd=\"PUT\"><Main_Zone>"
"<Power_Control><Power>On</Power></Power_Control>"
"</Main_Zone></YAMAHA_AV>");
}
mPowered = !mPowered;
updateUi();
} }
QString MainWindow::sendCommand(QString cmd) void MainWindow::sendCommand(QString cmd)
{ {
QString res;
QNetworkRequest request; QNetworkRequest request;
request.setUrl("http://" + mAddress + ":80/YamahaRemoteControl/ctrl"); request.setUrl("http://" + mAddress + ":80/YamahaRemoteControl/ctrl");
request.setRawHeader("Content-Type", "text/xml; charset=UTF-8"); request.setRawHeader("Content-Type", "text/xml; charset=UTF-8");
request.setRawHeader("Content-Length", QByteArray::number(cmd.size())); 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; void MainWindow::replyFinished()
connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); {
loop.exec(); QString ans;
mReply->deleteLater();
qDebug() << reply->readAll(); if(mReply->error() == QNetworkReply::NoError) {
return res; QByteArray data = mReply->readAll();
ans = QString(data);
} else {
qDebug() << mReply->url();
qDebug() << mReply->errorString();
return;
}
// check power state
qDebug() << ans;
QRegularExpression regex("<Power_Control><Power>(.*)</Power></Power_Control>");
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) void MainWindow::on_txt_address_textEdited(const QString &arg1)

View File

@@ -23,15 +23,19 @@ protected:
private slots: private slots:
void on_btn_onoff_clicked(); void on_btn_onoff_clicked();
void replyFinished();
void on_txt_address_textEdited(const QString &arg1); void on_txt_address_textEdited(const QString &arg1);
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
QString sendCommand(QString cmd);
QString mAddress; QString mAddress;
QNetworkAccessManager *manager; QNetworkReply *mReply;
bool mPowered = false;
void sendCommand(QString cmd);
void updateUi();
}; };
} }

View File

@@ -35,7 +35,7 @@
<item row="0" column="2"> <item row="0" column="2">
<widget class="QLineEdit" name="txt_address"> <widget class="QLineEdit" name="txt_address">
<property name="text"> <property name="text">
<string>10.0.0.5</string> <string>10.0.0.227</string>
</property> </property>
<property name="alignment"> <property name="alignment">
<set>Qt::AlignCenter</set> <set>Qt::AlignCenter</set>