Add VIS vehicle signal support
[apps/homescreen.git] / homescreen / src / mastervolume.cpp
index 35b47fc..8d7ecb4 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);
 
-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,59 +41,70 @@ qint32 MasterVolume::getVolume() const
 
 void MasterVolume::setVolume(qint32 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;
+
+       m_volume = volume;
+
+       if (!(m_vs && m_connected))
+               return;
+
+       m_vs->set("Vehicle.Cabin.Infotainment.Media.Volume", QString::number(volume));
 }
 
-void MasterVolume::onClientConnected()
+void MasterVolume::onConnected()
 {
-       // 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!";
-       });
+       if (!m_vs)
+               return;
+
+       m_vs->authorize();
 }
 
-void MasterVolume::onClientDisconnected()
+void MasterVolume::onAuthorized()
 {
-       qDebug() << "MasterVolume::onClientDisconnected!";
-       QTimer::singleShot(1000, this, SLOT(TryOpen()));
+       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::onClientError(QAbstractSocket::SocketError se)
+void MasterVolume::onDisconnected()
 {
-       qDebug() << "MasterVolume::onClientError: " << se;
+       QObject::disconnect(m_vs, &VehicleSignals::signalNotification, this, &MasterVolume::onGetSuccessResponse);
+       QObject::disconnect(m_vs, &VehicleSignals::signalNotification, this, &MasterVolume::onSignalNotification);
+
+       m_connected = false;
 }
 
-void MasterVolume::onClientEventReceived(QString name, const QJsonValue& data)
+void MasterVolume::updateVolume(QString value)
 {
-       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();
-                       }
+       bool ok;
+       qint32 volume = value.toInt(&ok);
+       if (ok) {
+               volume = qBound(0, volume, 100);
+               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);
 }