f231b6d02b2f629addd08773664d7b306d5d72c8
[apps/mixer.git] / app / mixer.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 2016,2017 Konsulko Group
4  * Copyright (C) 2018 IoT.bzh
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #include <QJsonArray>
20 #include <QJsonObject>
21 #include <QtDebug>
22 #include "mixer.h"
23
24 Mixer::Mixer(QObject* parent)
25     : QObject(parent)
26 {
27     connect(&m_client, SIGNAL(connected()), this, SLOT(onClientConnected()));
28 }
29
30 QStringList Mixer::roles() const
31 {
32     return m_roles;
33 }
34
35 void Mixer::open(const QUrl &url)
36 {
37     m_client.open(url);
38 }
39
40 void Mixer::onClientConnected()
41 {
42     // Call HAL to populate list
43     m_client.call("ahl-4a", "get_roles", QJsonValue(), [this](bool r, const QJsonValue& val) {
44         if (r)
45         {
46             m_roles.clear();
47             //BUG: should be able to add this, but not handled right now: m_roles.append("playback");
48             QJsonArray cards = val.toObject()["response"].toArray();
49             foreach (const QJsonValue& card, cards)
50             {
51                 m_roles.append(card.toString());
52                 qDebug() << "Mixer::onClientConnected - added this HAL: " << card.toString();
53             }
54             emit rolesChanged();
55         }
56     });
57 }
58
59 void Mixer::setVolume(const QString& name, int value)
60 {
61     QJsonObject arg;
62     arg.insert("action", "volume");
63     arg.insert("value", QJsonValue(value));
64     m_client.call("ahl-4a", name, arg, [name](bool r, const QJsonValue& v) {
65         if (r && v.isObject())
66         {
67                         // TODO: Success, update the slider
68         }
69         else
70         {
71                         // TODO: Failed, reset the slider to previous value
72         }
73     });
74 }
75
76 void Mixer::getVolume(const QString& name)
77 {
78     QJsonObject arg;
79     arg.insert("action", "volume");
80     arg.insert("value", QJsonValue("+0")); // FIXME: Hack to get volume: ask for a relative change with a delta of zero
81     m_client.call("ahl-4a", name, arg, [this, name](bool r, const QJsonValue& v) {
82         if (r && v.isObject())
83         {
84                         // TODO: Success, update the slider
85             qDebug() << "Volume changed: " << v;
86             int newVolume = v.toObject()["response"].toObject()["response"].toObject()["volnew"].toInt();
87             auto currentVolume = volumes_.find(name);
88             if (currentVolume != volumes_.end() && *currentVolume == newVolume)
89                 return;
90
91             volumes_[name] = newVolume;
92             emit volumeChanged(name, newVolume);
93         }
94         else
95         {
96                         // TODO: Failed, what to do ?
97         }
98     });
99 }