*/
#include "mastervolume.h"
-#include <QJsonObject>
#include <QTimer>
#include <QtDebug>
-#define MASTER_CONTROL "Master Playback"
-
-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
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_CONTROL);
- 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_CONTROL);
- 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_CONTROL)
- 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);
}