Update VehicleSignals usage
[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 <QTimer>
19 #include <QtDebug>
20
21 MasterVolume::MasterVolume(QObject* parent) :
22         QObject(parent),
23         m_volume(50)
24 {
25         VehicleSignalsConfig vsConfig("homescreen");
26         m_vs = new VehicleSignals(vsConfig);
27
28         if (m_vs) {
29                 QObject::connect(m_vs, &VehicleSignals::connected, this, &MasterVolume::onConnected);
30
31                 m_vs->connect();
32         }
33 }
34
35 MasterVolume::~MasterVolume()
36 {
37         delete m_vs;
38 }
39
40 qint32 MasterVolume::getVolume() const
41 {
42         return m_volume;
43 }
44
45 void MasterVolume::setVolume(quint32 volume)
46 {
47         if (m_volume == volume)
48                 return;
49
50         m_volume = volume;
51
52         if (m_vs)
53                 m_vs->set("Vehicle.Cabin.Infotainment.Media.Volume", volume, true);
54 }
55
56 void MasterVolume::updateVolume(QString value)
57 {
58         bool ok;
59         quint32 volume = value.toUInt(&ok);
60         if (ok) {
61                 volume = qBound(0U, volume, 100U);
62                 if (m_volume != volume) {
63                         m_volume = volume;
64                         emit VolumeChanged();
65                 }
66         }
67 }
68
69 void MasterVolume::onConnected()
70 {
71        if (!m_vs)
72                return;
73
74         QObject::connect(m_vs, &VehicleSignals::getSuccessResponse, this, &MasterVolume::onGetSuccessResponse);
75         QObject::connect(m_vs, &VehicleSignals::signalNotification, this, &MasterVolume::onSignalNotification);
76
77         m_vs->subscribe("Vehicle.Cabin.Infotainment.Media.Volume");
78         m_vs->get("Vehicle.Cabin.Infotainment.Media.Volume");
79 }
80
81 void MasterVolume::onGetSuccessResponse(QString path, QString value, QString timestamp)
82 {
83         if (path == "Vehicle.Cabin.Infotainment.Media.Volume") {
84                 updateVolume(value);
85                 emit VolumeChanged();
86         }
87 }
88
89 void MasterVolume::onSignalNotification(QString path, QString value, QString timestamp)
90 {
91         if (path == "Vehicle.Cabin.Infotainment.Media.Volume")
92                 updateVolume(value);
93 }