X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=homescreen%2Fsrc%2Fmastervolume.cpp;h=8d7ecb419b62dfae325d20613afcc16c1f27ae84;hb=d1347bf236f84908fea96ba929e0e8376c2b1d78;hp=98b884d5172424d8e32e0e61311da848dbe872d3;hpb=a9df3eb09a85d479f26a14cd71a41e1de087c1ef;p=apps%2Fhomescreen.git diff --git a/homescreen/src/mastervolume.cpp b/homescreen/src/mastervolume.cpp index 98b884d..8d7ecb4 100644 --- a/homescreen/src/mastervolume.cpp +++ b/homescreen/src/mastervolume.cpp @@ -15,24 +15,23 @@ */ #include "mastervolume.h" -#include #include #include -MasterVolume::MasterVolume(QObject* parent) - : QObject(parent) - , m_volume{50} +MasterVolume::MasterVolume(QObject* parent) : + QObject(parent), + m_volume(50) { - connect(&m_client, SIGNAL(connected()), this, SLOT(onClientConnected())); - connect(&m_client, SIGNAL(disconnected()), this, SLOT(onClientDisconnected())); - connect(&m_client, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onClientError(QAbstractSocket::SocketError))); - connect(&m_client, SIGNAL(eventReceived(QString, const QJsonValue&)), this, SLOT(onClientEventReceived(QString, const QJsonValue&))); -} + VehicleSignalsConfig vsConfig("homescreen"); + m_vs = new VehicleSignals(vsConfig); -void MasterVolume::open(const QUrl& url) -{ - m_url = url; - TryOpen(); + if (m_vs) { + QObject::connect(m_vs, &VehicleSignals::connected, this, &MasterVolume::onConnected); + QObject::connect(m_vs, &VehicleSignals::authorized, this, &MasterVolume::onAuthorized); + QObject::connect(m_vs, &VehicleSignals::disconnected, this, &MasterVolume::onDisconnected); + + m_vs->connect(); + } } qint32 MasterVolume::getVolume() const @@ -42,70 +41,70 @@ qint32 MasterVolume::getVolume() const void MasterVolume::setVolume(qint32 volume) { - if (m_volume != volume) - { - m_volume = volume; - QJsonObject arg; - arg.insert("control", "Master"); - double v = (double) volume / 100.0; - arg.insert("value", v); - m_client.call("audiomixer", "volume", arg); - } -} + if (m_volume == volume) + return; -void MasterVolume::onClientConnected() -{ - QJsonObject arg; - arg.insert("control", "Master"); - m_client.call("audiomixer", "volume", arg, [this](bool r, const QJsonValue& v) { - if (r && v.isObject()) { - int volume = v.toObject()["response"].toObject()["volume"].toDouble() * 100; - volume = qBound(0, volume, 100); - if (m_volume != volume) - { - m_volume = volume; - emit VolumeChanged(); - } - } + m_volume = volume; - QJsonObject arg; - arg.insert("event", "volume_changed"); - m_client.call("audiomixer", "subscribe", arg); - }); + if (!(m_vs && m_connected)) + return; + + m_vs->set("Vehicle.Cabin.Infotainment.Media.Volume", QString::number(volume)); } -void MasterVolume::onClientDisconnected() +void MasterVolume::onConnected() { - qDebug() << "MasterVolume::onClientDisconnected!"; - QTimer::singleShot(1000, this, SLOT(TryOpen())); + if (!m_vs) + return; + + m_vs->authorize(); } -void MasterVolume::onClientError(QAbstractSocket::SocketError se) +void MasterVolume::onAuthorized() { - qDebug() << "MasterVolume::onClientError: " << se; + if (!m_vs) + return; + + m_connected = true; + + QObject::connect(m_vs, &VehicleSignals::getSuccessResponse, this, &MasterVolume::onGetSuccessResponse); + QObject::connect(m_vs, &VehicleSignals::signalNotification, this, &MasterVolume::onSignalNotification); + + m_vs->subscribe("Vehicle.Cabin.Infotainment.Media.Volume"); + m_vs->get("Vehicle.Cabin.Infotainment.Media.Volume"); } -void MasterVolume::onClientEventReceived(QString name, const QJsonValue& data) +void MasterVolume::onDisconnected() { - qDebug() << "MasterVolume::onClientEventReceived[" << name << "]: " << data; - if (name == "audiomixer/volume_changed") - { - QString ctlName = data.toObject()["control"].toString(); + QObject::disconnect(m_vs, &VehicleSignals::signalNotification, this, &MasterVolume::onGetSuccessResponse); + QObject::disconnect(m_vs, &VehicleSignals::signalNotification, this, &MasterVolume::onSignalNotification); - if (ctlName != "Master") - return; + m_connected = false; +} - int volume = data.toObject()["value"].toDouble() * 100; +void MasterVolume::updateVolume(QString value) +{ + bool ok; + qint32 volume = value.toInt(&ok); + if (ok) { volume = qBound(0, volume, 100); - if (m_volume != volume) - { + if (m_volume != volume) { m_volume = volume; emit VolumeChanged(); } } } -void MasterVolume::TryOpen() +void MasterVolume::onGetSuccessResponse(QString path, QString value, QString timestamp) +{ + if (path == "Vehicle.Cabin.Infotainment.Media.Volume") { + updateVolume(value); + emit VolumeChanged(); + } +} + +void MasterVolume::onSignalNotification(QString path, QString value, QString timestamp) { - m_client.open(m_url); + if (path == "Vehicle.Cabin.Infotainment.Media.Volume") + updateVolume(value); }