Bind the slider volume to the 4a active role
[apps/homescreen.git] / homescreen / src / mastervolume.cpp
1 /*
2  * Copyright (C) 2017 Konsulko Group
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "mastervolume.h"
18 #include <QJsonObject>
19 #include <QTimer>
20 #include <QtDebug>
21
22 MasterVolume::MasterVolume(QObject* parent)
23         : QObject(parent)
24         , m_volume{50}
25 {
26         connect(&m_client, SIGNAL(connected()), this, SLOT(onClientConnected()));
27         connect(&m_client, SIGNAL(disconnected()), this, SLOT(onClientDisconnected()));
28         connect(&m_client, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onClientError(QAbstractSocket::SocketError)));
29         connect(&m_client, SIGNAL(eventReceived(QString, const QJsonValue&)), this, SLOT(onClientEventReceived(QString, const QJsonValue&)));
30 }
31
32 void MasterVolume::open(const QUrl& url)
33 {
34         m_url = url;
35         TryOpen();
36 }
37
38 qint32 MasterVolume::getVolume() const
39 {
40         return m_volume;
41 }
42
43 void MasterVolume::setVolume(qint32 volume)
44 {
45         if (m_volume != volume)
46         {
47                 m_volume = volume;
48                 QJsonObject arg;
49                 arg.insert("action", "volume");
50                 arg.insert("value", volume);
51                 m_client.call("ahl-4a", "activerole", arg, [](bool, const QJsonValue&) {
52                         // Nothing to do, events will update sliders
53                 });
54         }
55 }
56
57 void MasterVolume::onClientConnected()
58 {
59         // Subscribe to 4a events
60         m_client.call("ahl-4a", "subscribe", QJsonValue(), [this](bool r, const QJsonValue&) {
61                 if (r) qDebug() << "MasterVolume::onClientConnected - subscribed to 4a events!";
62                 else qCritical () << "MasterVolume::onClientConnected - Failed to subscribe to 4a events!";
63         });
64 }
65
66 void MasterVolume::onClientDisconnected()
67 {
68         qDebug() << "MasterVolume::onClientDisconnected!";
69         QTimer::singleShot(1000, this, SLOT(TryOpen()));
70 }
71
72 void MasterVolume::onClientError(QAbstractSocket::SocketError se)
73 {
74         qDebug() << "MasterVolume::onClientError: " << se;
75 }
76
77 void MasterVolume::onClientEventReceived(QString name, const QJsonValue& data)
78 {
79         qDebug() << "MasterVolume::onClientEventReceived[" << name << "]: " << data;
80         if (name == "ahl-4a/volume_changed")
81         {
82                 QJsonObject arg = data.toObject();
83                 bool active = arg["active"].toBool();
84                 if (active)
85                 {
86                         // QString role = arg["role"].toString();
87                         int volume = arg["volume"].toInt();
88                         if (m_volume != volume)
89                         {
90                                 m_volume = volume;
91                                 emit VolumeChanged();
92                         }
93                 }
94         }
95 }
96
97 void MasterVolume::TryOpen()
98 {
99         m_client.open(m_url);
100 }