2 * Copyright (C) 2016 The Qt Company Ltd.
3 * Copyright (C) 2016,2017 Konsulko Group
4 * Copyright (C) 2018 IoT.bzh
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include <QJsonObject>
25 Mixer::Mixer(QObject* parent)
28 connect(&m_client, SIGNAL(connected()), this, SLOT(onClientConnected()));
29 connect(&m_client, SIGNAL(disconnected()), this, SLOT(onClientDisconnected()));
30 connect(&m_client, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onClientError(QAbstractSocket::SocketError)));
31 connect(&m_client, SIGNAL(eventReceived(QString, const QJsonValue&)), this, SLOT(onClientEventReceived(QString, const QJsonValue&)));
34 void Mixer::open(const QUrl& url)
40 QList<QObject*> Mixer::roles() const
45 void Mixer::getRoleVolume(AudioRole* role)
47 if (role == nullptr) return;
50 arg.insert("action", "volume");
51 arg.insert("value", QJsonValue("+0")); // FIXME: Hack to get volume: ask for a relative change with a delta of zero
53 m_client.call("ahl-4a", role->Name().toLocal8Bit().data(), arg, [role](bool r, const QJsonValue& v) {
54 if (r && v.isObject())
56 qDebug() << role->Name() << " Volume changed: " << v;
57 int newVolume = v.toObject()["response"].toObject()["volnew"].toInt();
58 role->setValue(newVolume);
63 void Mixer::setRoleVolume(AudioRole* role)
65 if (role == nullptr) return;
69 arg.insert("action", "volume");
70 arg.insert("value", QJsonValue(role->Value()));
71 m_client.call("ahl-4a", role->Name().toLocal8Bit().data(), arg, [role](bool r, const QJsonValue& v) {
72 // Nothing to do, events will update sliders
77 void Mixer::onClientConnected()
79 // Subscribe to 4a events
80 m_client.call("ahl-4a", "subscribe", QJsonValue(), [this](bool r, const QJsonValue& val) {
81 if (r) qDebug() << "Mixer::onClientConnected - subscribed to 4a events!";
82 else qCritical () << "Mixer::onClientConnected - Failed to subscribe to 4a events!";
85 // Call HAL to populate list
86 m_client.call("ahl-4a", "get_roles", QJsonValue(), [this](bool r, const QJsonValue& val) {
89 for(QObject* role : m_roles) delete role;
92 QJsonArray cards = val.toObject()["response"].toArray();
93 foreach (const QJsonValue& card, cards)
95 AudioRole* ar = new AudioRole(card.toString(), 0);
96 getRoleVolume(reinterpret_cast<AudioRole*>(ar));
97 connect(ar, SIGNAL(ValueChanged()), this, SLOT(onRoleValueChanged()));
99 qDebug() << "Mixer::onClientConnected - added this HAL: " << card.toString();
107 void Mixer::onClientDisconnected()
109 qDebug() << "Mixer::onClientDisconnected!";
110 QTimer::singleShot(1000, this, SLOT(TryOpen()));
113 void Mixer::onClientError(QAbstractSocket::SocketError se)
115 qDebug() << "Mixer::onClientError: " << se;
118 void Mixer::onClientEventReceived(QString eventName, const QJsonValue& data)
120 qDebug() << "Mixer::onClientEventReceived[" << eventName << "]: " << data;
121 if (eventName == "ahl-4a/volume_changed")
123 QString role = data.toObject()["role"].toString();
124 int volume = data.toObject()["volume"].toInt();
125 for(QObject* o : m_roles)
127 AudioRole* ar = reinterpret_cast<AudioRole*>(o);
128 if (ar && ar->Name() == role)
130 ar->setValue(volume);
136 void Mixer::onRoleValueChanged()
138 AudioRole* role = reinterpret_cast<AudioRole*>(QObject::sender());
139 if (role == nullptr) return;
143 void Mixer::TryOpen()
145 qDebug() << "Mixer::TryOpen: " << m_url;
146 m_client.open(m_url);