main: Handle terminating event
[apps/homescreen.git] / homescreen / src / mastervolume.cpp
index 35b47fc..d173ef2 100644 (file)
  */
 
 #include "mastervolume.h"
-#include <QJsonObject>
 #include <QTimer>
 #include <QtDebug>
 
-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,61 +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("action", "volume");
-               arg.insert("value", volume);
-               m_client.call("ahl-4a", "activerole", arg, [](bool, const QJsonValue&) {
-                       // Nothing to do, events will update sliders
-               });
-       }
-}
+       if (m_volume == volume)
+               return;
 
-void MasterVolume::onClientConnected()
-{
-       // Subscribe to 4a events
-       m_client.call("ahl-4a", "subscribe", QJsonValue(), [this](bool r, const QJsonValue&) {
-               if (r) qDebug() << "MasterVolume::onClientConnected - subscribed to 4a events!";
-               else qCritical () << "MasterVolume::onClientConnected - Failed to subscribe to 4a events!";
-       });
+       m_volume = volume;
+
+       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;
+
+       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::onGetSuccessResponse(QString path, QString value, QString timestamp)
 {
-       qDebug() << "MasterVolume::onClientEventReceived[" << name << "]: " << data;
-       if (name == "ahl-4a/volume_changed")
-       {
-               QJsonObject arg = data.toObject();
-               bool active = arg["active"].toBool();
-               if (active)
-               {
-                       // QString role = arg["role"].toString();
-                       int volume = arg["volume"].toInt();
-                       if (m_volume != volume)
-                       {
-                               m_volume = volume;
-                               emit VolumeChanged();
-                       }
-               }
+       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);
 }