WiP: added volume update and control
TODO: collect volume change events and send only one
This commit is contained in:
131
mainwindow.cpp
131
mainwindow.cpp
@@ -17,9 +17,8 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
|
||||
mAddress = ui->txt_address->text();
|
||||
|
||||
// load initial data
|
||||
sendCommand("<YAMAHA_AV cmd=\"GET\"><System>"
|
||||
"<Power_Control><Power>GetParam</Power></Power_Control>"
|
||||
"</System></YAMAHA_AV>");
|
||||
sendCommand("<YAMAHA_AV cmd=\"GET\"><System><Power_Control><Power>GetParam</Power></Power_Control></System></YAMAHA_AV>");
|
||||
sendCommand("<YAMAHA_AV cmd=\"GET\"><Main_Zone><Volume><Lvl>GetParam</Lvl></Volume></Main_Zone></YAMAHA_AV>");
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
@@ -27,6 +26,72 @@ MainWindow::~MainWindow()
|
||||
delete ui;
|
||||
}
|
||||
|
||||
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);
|
||||
networkManager->post(request, cmd.toUtf8());
|
||||
connect(networkManager, &QNetworkAccessManager::finished, this, &MainWindow::replyFinished);
|
||||
}
|
||||
|
||||
void MainWindow::replyFinished(QNetworkReply* reply)
|
||||
{
|
||||
QString ans;
|
||||
|
||||
if(reply->error() == QNetworkReply::NoError) {
|
||||
QByteArray data = reply->readAll();
|
||||
ans = QString(data);
|
||||
} else {
|
||||
qDebug() << reply->url();
|
||||
qDebug() << reply->errorString();
|
||||
return;
|
||||
}
|
||||
|
||||
// check power state
|
||||
QRegularExpression regexPower("<Power_Control><Power>(.*)</Power></Power_Control>");
|
||||
QRegularExpressionMatch matchPower = regexPower.match(ans);
|
||||
|
||||
if (matchPower.hasMatch()) {
|
||||
if (matchPower.captured(1) == "On") mPowered = true;
|
||||
else if (matchPower.captured(1) == "Off") mPowered = false;
|
||||
}
|
||||
|
||||
// check volume
|
||||
QRegularExpression regexVolume("<Volume><Lvl><Val>(.*)</Val>");
|
||||
QRegularExpressionMatch matchVolume = regexVolume.match(ans);
|
||||
|
||||
if (matchVolume.hasMatch()) {
|
||||
mVolume = matchVolume.captured(1).toInt();
|
||||
}
|
||||
|
||||
qDebug() << ans;
|
||||
|
||||
reply->deleteLater();
|
||||
updateUi();
|
||||
}
|
||||
|
||||
void MainWindow::updateUi() {
|
||||
ui->btn_onoff->setText(mPowered ? "On" : "Off");
|
||||
ui->dial->setEnabled(mPowered);
|
||||
ui->dial->setValue(mVolume);
|
||||
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;
|
||||
}
|
||||
|
||||
void MainWindow::on_btn_onoff_clicked()
|
||||
{
|
||||
if (mPowered){
|
||||
@@ -43,60 +108,10 @@ void MainWindow::on_btn_onoff_clicked()
|
||||
updateUi();
|
||||
}
|
||||
|
||||
void MainWindow::sendCommand(QString cmd)
|
||||
}
|
||||
|
||||
void yremote::MainWindow::on_dial_valueChanged(int value)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
ui->lbl_volume->setText(QString::number(value));
|
||||
sendCommand("<YAMAHA_AV cmd=\"PUT\"><Main_Zone><Volume><Lvl><Val>" + QString::number(value) + "</Val><Exp>1</Exp><Unit>dB</Unit></Lvl></Volume></Main_Zone></YAMAHA_AV>");
|
||||
}
|
||||
|
||||
@@ -23,16 +23,18 @@ protected:
|
||||
|
||||
private slots:
|
||||
void on_btn_onoff_clicked();
|
||||
void replyFinished();
|
||||
void replyFinished(QNetworkReply *reply);
|
||||
void on_txt_address_textEdited(const QString &arg1);
|
||||
|
||||
void on_dial_valueChanged(int value);
|
||||
|
||||
private:
|
||||
Ui::MainWindow *ui;
|
||||
|
||||
QString mAddress;
|
||||
QNetworkReply *mReply;
|
||||
|
||||
bool mPowered = false;
|
||||
int mVolume = 0;
|
||||
|
||||
void sendCommand(QString cmd);
|
||||
void updateUi();
|
||||
|
||||
@@ -43,7 +43,20 @@
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="3" rowspan="2">
|
||||
<widget class="QDial" name="dial"/>
|
||||
<widget class="QDial" name="dial">
|
||||
<property name="minimum">
|
||||
<number>-805</number>
|
||||
</property>
|
||||
<property name="maximum">
|
||||
<number>-165</number>
|
||||
</property>
|
||||
<property name="singleStep">
|
||||
<number>5</number>
|
||||
</property>
|
||||
<property name="pageStep">
|
||||
<number>5</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0" rowspan="2" colspan="3">
|
||||
<widget class="QGroupBox" name="groupBox">
|
||||
@@ -145,7 +158,7 @@
|
||||
<enum>QFrame::Sunken</enum>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>-66.5</string>
|
||||
<string>-666</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
|
||||
Reference in New Issue
Block a user