2 * Copyright (C) 2017 Konsulko Group
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "mastervolume.h"
18 #include <QJsonObject>
22 MasterVolume::MasterVolume(QObject* parent)
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&)));
32 void MasterVolume::open(const QUrl& url)
38 qint32 MasterVolume::getVolume() const
43 void MasterVolume::setVolume(qint32 volume)
45 if (m_volume != volume)
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
57 void MasterVolume::onClientConnected()
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!";
66 void MasterVolume::onClientDisconnected()
68 qDebug() << "MasterVolume::onClientDisconnected!";
69 QTimer::singleShot(1000, this, SLOT(TryOpen()));
72 void MasterVolume::onClientError(QAbstractSocket::SocketError se)
74 qDebug() << "MasterVolume::onClientError: " << se;
77 void MasterVolume::onClientEventReceived(QString name, const QJsonValue& data)
79 qDebug() << "MasterVolume::onClientEventReceived[" << name << "]: " << data;
80 if (name == "ahl-4a/volume_changed")
82 QJsonObject arg = data.toObject();
83 bool active = arg["active"].toBool();
86 // QString role = arg["role"].toString();
87 int volume = arg["volume"].toInt();
88 if (m_volume != volume)
97 void MasterVolume::TryOpen()