Files
yremote/mainwindow.cpp

103 lines
2.8 KiB
C++

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qnetwork.h>
#include <QtNetwork/QNetworkReply>
class QNetworkReply;
namespace yremote {
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
QString DEFAULT_ADDRESS = "10.0.0.227";
ui->setupUi(this);
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()
{
delete ui;
}
void MainWindow::on_btn_onoff_clicked()
{
if (mPowered){
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();
}
void MainWindow::sendCommand(QString cmd)
{
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()));
QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
mReply = networkManager->post(request, cmd.toUtf8());
connect(networkManager, &QNetworkAccessManager::finished, this, &MainWindow::replyFinished);
}
void MainWindow::replyFinished()
{
QString ans;
mReply->deleteLater();
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("<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)
{
mAddress = arg1;
}
}