X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=homescreen%2Fsrc%2Fmastervolume.cpp;h=d173ef2bd30181a50051ef7e623d4b2f89a1ef84;hb=refs%2Fchanges%2F98%2F29598%2F1;hp=98b884d5172424d8e32e0e61311da848dbe872d3;hpb=a9df3eb09a85d479f26a14cd71a41e1de087c1ef;p=apps%2Fhomescreen.git diff --git a/homescreen/src/mastervolume.cpp b/homescreen/src/mastervolume.cpp index 98b884d..d173ef2 100644 --- a/homescreen/src/mastervolume.cpp +++ b/homescreen/src/mastervolume.cpp @@ -15,24 +15,26 @@ */ #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); + + if (m_vs) { + QObject::connect(m_vs, &VehicleSignals::connected, this, &MasterVolume::onConnected); + + m_vs->connect(); + } } -void MasterVolume::open(const QUrl& url) +MasterVolume::~MasterVolume() { - m_url = url; - TryOpen(); + delete m_vs; } qint32 MasterVolume::getVolume() const @@ -40,72 +42,52 @@ qint32 MasterVolume::getVolume() const return m_volume; } -void MasterVolume::setVolume(qint32 volume) +void MasterVolume::setVolume(quint32 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_vs->set("Vehicle.Cabin.Infotainment.Media.Volume", volume, true); } -void MasterVolume::onClientDisconnected() +void MasterVolume::updateVolume(QString value) { - qDebug() << "MasterVolume::onClientDisconnected!"; - QTimer::singleShot(1000, this, SLOT(TryOpen())); + bool ok; + quint32 volume = value.toUInt(&ok); + if (ok) { + volume = qBound(0U, volume, 100U); + if (m_volume != volume) { + m_volume = volume; + emit VolumeChanged(); + } + } } -void MasterVolume::onClientError(QAbstractSocket::SocketError se) +void MasterVolume::onConnected() { - qDebug() << "MasterVolume::onClientError: " << se; -} + if (!m_vs) + return; -void MasterVolume::onClientEventReceived(QString name, const QJsonValue& data) -{ - qDebug() << "MasterVolume::onClientEventReceived[" << name << "]: " << data; - if (name == "audiomixer/volume_changed") - { - QString ctlName = data.toObject()["control"].toString(); + QObject::connect(m_vs, &VehicleSignals::getSuccessResponse, this, &MasterVolume::onGetSuccessResponse); + QObject::connect(m_vs, &VehicleSignals::signalNotification, this, &MasterVolume::onSignalNotification); - if (ctlName != "Master") - return; + m_vs->subscribe("Vehicle.Cabin.Infotainment.Media.Volume"); + m_vs->get("Vehicle.Cabin.Infotainment.Media.Volume"); +} - int volume = data.toObject()["value"].toDouble() * 100; - volume = qBound(0, volume, 100); - if (m_volume != volume) - { - m_volume = volume; - emit VolumeChanged(); - } +void MasterVolume::onGetSuccessResponse(QString path, QString value, QString timestamp) +{ + if (path == "Vehicle.Cabin.Infotainment.Media.Volume") { + updateVolume(value); + emit VolumeChanged(); } } -void MasterVolume::TryOpen() +void MasterVolume::onSignalNotification(QString path, QString value, QString timestamp) { - m_client.open(m_url); + if (path == "Vehicle.Cabin.Infotainment.Media.Volume") + updateVolume(value); }