/* * Copyright (C) 2017 Konsulko Group * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "mastervolume.h" #include "qafbwsclient.h" #include "qafbwsmsg.h" #include #include #include #include #include /*! * @brief Default constructor. * @param parent Parent's object. */ MasterVolume::MasterVolume(QObject* parent) : QObject{parent} , m_volume(0) { connect(&m_client, SIGNAL(connected()), this, SLOT(onAfbClientConnected())); connect(&m_client, SIGNAL(disconnected()), this, SLOT(onAfbClientDisconnected())); } /*! * @brief Destructor. */ MasterVolume::~MasterVolume() { } /*! * @brief Initialize this instance. * @param port AFB Port. * @param token AFB Token. */ void MasterVolume::init(quint16 port, QString token) { qDebug() << "MasterVolume::init(" << port << "," << token << ")"; m_client.open(QUrl(QString("ws://localhost:") + QString::number(port) + QString("/api?token=") + token)); } /*! * \brief Get the current volume. * @return The volume. */ uint32_t MasterVolume::getVolume() const { return m_volume; } /*! * @brief Set volume. * @param volume New desired value. */ void MasterVolume::setVolume(uint32_t volume) { qDebug() << "MasterVolume::setVolume(" << volume << ")"; if (volume == m_volumePending) return; // Nothing to do m_volumePending = volume; QJsonObject value; value["label"] = "Master_Playback_Volume"; value["val"] = static_cast(volume); // FIXME: card should be obtained! if (!m_soundcard.size()) m_soundcard = "rsnddai0ak4613h"; m_call = m_client.call(m_soundcard, "ctlset", value); connect(m_call.data(), SIGNAL(closed()), this, SLOT(onCallCtlSet())); } void MasterVolume::onAfbClientConnected() { qDebug() << "MasterVolume::onAfbClientConnected()"; m_call = m_client.call("alsacore", "hallist"); connect(m_call.data(), SIGNAL(closed()), this, SLOT(onCallHalListClosed())); } void MasterVolume::onAfbClientDisconnected() { qDebug() << "MasterVolume::onAfbClientDisconnected()"; } void MasterVolume::onCallHalListClosed() { qDebug() << "MasterVolume::onCallHalListClosed()"; disconnect(m_call.data(), SIGNAL(closed()), this, SLOT(onCallHalListClosed())); if (m_call->messageType() == AfMsgType::RetOk && m_call->value().isObject()) { m_soundcard = m_call->value().toObject()["response"].toArray()[0].toObject()["api"].toString(); qDebug() << "MasterVolume::onCallHalListClosed() - Found soundcard api:" << m_soundcard; } //m_call.clear(); // FIXME: card should be obtained! if (!m_soundcard.size()) m_soundcard = "rsnddai0ak4613h"; // Get volume if (m_soundcard.size()) { QJsonObject arg; arg["label"] = "Master_Playback_Volume"; m_call = m_client.call(m_soundcard, "ctlget", arg); connect(m_call.data(), SIGNAL(closed()), this, SLOT(onCallCtlGet())); } } void MasterVolume::onCallCtlGet() { qDebug() << "MasterVolume::onCallCtlGet()"; disconnect(m_call.data(), SIGNAL(closed()), this, SLOT(onCallCtlGet())); if (m_call->messageType() == AfMsgType::RetOk && m_call->value().isObject()) { setVolume(m_call->value().toObject()["response"].toObject()["val"].toArray()[0].toInt()); } //m_call.clear(); } void MasterVolume::onCallCtlSet() { qDebug() << "MasterVolume::onCallCtlSet()"; if (m_call.data()) { disconnect(m_call.data(), SIGNAL(closed()), this, SLOT(onCallCtlSet())); if (m_call->messageType() == AfMsgType::RetOk && m_call->value().isObject()) { m_volume = m_volumePending; emit volumeChanged(m_volume); } //m_call.clear(); } }