Update to latest api changes

This commit is contained in:
Michael Zanetti 2019-10-09 14:52:58 +02:00
parent c8491bd345
commit 23ff818577
4 changed files with 164 additions and 134 deletions

View File

@ -44,8 +44,10 @@ DevicePluginSonos::~DevicePluginSonos()
} }
Device::DeviceSetupStatus DevicePluginSonos::setupDevice(Device *device) void DevicePluginSonos::setupDevice(DeviceSetupInfo *info)
{ {
Device *device = info->device();
if (!m_pluginTimer5sec) { if (!m_pluginTimer5sec) {
m_pluginTimer5sec = hardwareManager()->pluginTimerManager()->registerTimer(5); m_pluginTimer5sec = hardwareManager()->pluginTimerManager()->registerTimer(5);
connect(m_pluginTimer5sec, &PluginTimer::timeout, this, [this]() { connect(m_pluginTimer5sec, &PluginTimer::timeout, this, [this]() {
@ -98,8 +100,17 @@ Device::DeviceSetupStatus DevicePluginSonos::setupDevice(Device *device)
connect(sonos, &Sonos::volumeReceived, this, &DevicePluginSonos::onVolumeReceived); connect(sonos, &Sonos::volumeReceived, this, &DevicePluginSonos::onVolumeReceived);
connect(sonos, &Sonos::actionExecuted, this, &DevicePluginSonos::onActionExecuted); connect(sonos, &Sonos::actionExecuted, this, &DevicePluginSonos::onActionExecuted);
connect(sonos, &Sonos::authenticationStatusChanged, this, &DevicePluginSonos::onAuthenticationStatusChanged); connect(sonos, &Sonos::authenticationStatusChanged, this, &DevicePluginSonos::onAuthenticationStatusChanged);
connect(sonos, &Sonos::authenticationStatusChanged, info, [info](bool authenticated){
if (authenticated) {
info->finish(Device::DeviceErrorNoError);
} else {
info->finish(Device::DeviceErrorAuthenticationFailure);
}
});
m_sonosConnections.insert(device, sonos); m_sonosConnections.insert(device, sonos);
return Device::DeviceSetupStatusSuccess; return info->finish(Device::DeviceErrorNoError);
} else { } else {
//device loaded from the device database, needs a new access token; //device loaded from the device database, needs a new access token;
pluginStorage()->beginGroup(device->id().toString()); pluginStorage()->beginGroup(device->id().toString());
@ -117,39 +128,39 @@ Device::DeviceSetupStatus DevicePluginSonos::setupDevice(Device *device)
connect(sonos, &Sonos::authenticationStatusChanged, this, &DevicePluginSonos::onAuthenticationStatusChanged); connect(sonos, &Sonos::authenticationStatusChanged, this, &DevicePluginSonos::onAuthenticationStatusChanged);
sonos->getAccessTokenFromRefreshToken(refreshToken); sonos->getAccessTokenFromRefreshToken(refreshToken);
m_sonosConnections.insert(device, sonos); m_sonosConnections.insert(device, sonos);
return Device::DeviceSetupStatusAsync; return info->finish(Device::DeviceErrorNoError);
} }
} }
if (device->deviceClassId() == sonosGroupDeviceClassId) { if (device->deviceClassId() == sonosGroupDeviceClassId) {
return Device::DeviceSetupStatusSuccess; return info->finish(Device::DeviceErrorNoError);
} }
return Device::DeviceSetupStatusFailure;
qCWarning(dcSonos()) << "Unhandled device class id in setupDevice" << device->deviceClassId();
} }
DevicePairingInfo DevicePluginSonos::pairDevice(DevicePairingInfo &devicePairingInfo) void DevicePluginSonos::startPairing(DevicePairingInfo *info)
{ {
if (devicePairingInfo.deviceClassId() == sonosConnectionDeviceClassId) { if (info->deviceClassId() == sonosConnectionDeviceClassId) {
Sonos *sonos = new Sonos(hardwareManager()->networkManager(), "0a8f6d44-d9d1-4474-bcfa-cfb41f8b66e8", "3095ce48-0c5d-47ce-a1f4-6005c7b8fdb5", this); Sonos *sonos = new Sonos(hardwareManager()->networkManager(), "0a8f6d44-d9d1-4474-bcfa-cfb41f8b66e8", "3095ce48-0c5d-47ce-a1f4-6005c7b8fdb5", this);
QUrl url = sonos->getLoginUrl(QUrl("https://127.0.0.1:8888")); QUrl url = sonos->getLoginUrl(QUrl("https://127.0.0.1:8888"));
qCDebug(dcSonos()) << "Sonos url:" << url; qCDebug(dcSonos()) << "Sonos url:" << url;
devicePairingInfo.setOAuthUrl(url); info->setOAuthUrl(url);
devicePairingInfo.setStatus(Device::DeviceErrorNoError); info->finish(Device::DeviceErrorNoError);
m_setupSonosConnections.insert(devicePairingInfo.deviceId(), sonos); m_setupSonosConnections.insert(info->deviceId(), sonos);
return devicePairingInfo; return;
} }
qCWarning(dcSonos()) << "Unhandled pairing metod!"; qCWarning(dcSonos()) << "Unhandled pairing metod!";
devicePairingInfo.setStatus(Device::DeviceErrorCreationMethodNotSupported); info->finish(Device::DeviceErrorCreationMethodNotSupported);
return devicePairingInfo;
} }
DevicePairingInfo DevicePluginSonos::confirmPairing(DevicePairingInfo &devicePairingInfo, const QString &username, const QString &secret) void DevicePluginSonos::confirmPairing(DevicePairingInfo *info, const QString &username, const QString &secret)
{ {
Q_UNUSED(username); Q_UNUSED(username)
if (devicePairingInfo.deviceClassId() == sonosConnectionDeviceClassId) { if (info->deviceClassId() == sonosConnectionDeviceClassId) {
qCDebug(dcSonos()) << "Redirect url is" << secret; qCDebug(dcSonos()) << "Redirect url is" << secret;
QUrl url(secret); QUrl url(secret);
QUrlQuery query(url); QUrlQuery query(url);
@ -157,44 +168,39 @@ DevicePairingInfo DevicePluginSonos::confirmPairing(DevicePairingInfo &devicePai
QByteArray state = query.queryItemValue("state").toLocal8Bit(); QByteArray state = query.queryItemValue("state").toLocal8Bit();
//TODO evaluate state if it equals the given state //TODO evaluate state if it equals the given state
Sonos *sonos = m_setupSonosConnections.value(devicePairingInfo.deviceId()); Sonos *sonos = m_setupSonosConnections.value(info->deviceId());
if (!sonos) { if (!sonos) {
qWarning(dcSonos()) << "No sonos connection found for device:" << devicePairingInfo.deviceName(); qWarning(dcSonos()) << "No sonos connection found for device:" << info->deviceName();
m_setupSonosConnections.remove(devicePairingInfo.deviceId()); m_setupSonosConnections.remove(info->deviceId());
sonos->deleteLater(); sonos->deleteLater();
devicePairingInfo.setStatus(Device::DeviceErrorHardwareFailure); info->finish(Device::DeviceErrorHardwareFailure);
return devicePairingInfo; return;
} }
sonos->getAccessTokenFromAuthorizationCode(authorizationCode); sonos->getAccessTokenFromAuthorizationCode(authorizationCode);
connect(sonos, &Sonos::authenticationStatusChanged, this, [devicePairingInfo, this](bool authenticated){ connect(sonos, &Sonos::authenticationStatusChanged, info, [this, info](bool authenticated){
Sonos *sonos = static_cast<Sonos *>(sender()); Sonos *sonos = static_cast<Sonos *>(sender());
DevicePairingInfo info(devicePairingInfo);
if(!authenticated) { if(!authenticated) {
qWarning(dcSonos()) << "Authentication process failed" << devicePairingInfo.deviceName(); qWarning(dcSonos()) << "Authentication process failed" << info->deviceName();
m_setupSonosConnections.remove(info.deviceId()); m_setupSonosConnections.remove(info->deviceId());
sonos->deleteLater(); sonos->deleteLater();
info.setStatus(Device::DeviceErrorSetupFailed); info->finish(Device::DeviceErrorSetupFailed, QT_TR_NOOP("Authentication failed. Please try again."));
emit pairingFinished(info);
return; return;
} }
QByteArray accessToken = sonos->accessToken(); QByteArray accessToken = sonos->accessToken();
QByteArray refreshToken = sonos->refreshToken(); QByteArray refreshToken = sonos->refreshToken();
qCDebug(dcSonos()) << "Token:" << accessToken << refreshToken; qCDebug(dcSonos()) << "Token:" << accessToken << refreshToken;
pluginStorage()->beginGroup(info.deviceId().toString()); pluginStorage()->beginGroup(info->deviceId().toString());
pluginStorage()->setValue("refresh_token", refreshToken); pluginStorage()->setValue("refresh_token", refreshToken);
pluginStorage()->endGroup(); pluginStorage()->endGroup();
info.setStatus(Device::DeviceErrorNoError); info->finish(Device::DeviceErrorNoError);
emit pairingFinished(info);
}); });
devicePairingInfo.setStatus(Device::DeviceErrorAsync); return;
return devicePairingInfo;
} }
qCWarning(dcSonos()) << "Invalid deviceclassId -> no pairing possible with this device"; qCWarning(dcSonos()) << "Invalid deviceclassId -> no pairing possible with this device";
devicePairingInfo.setStatus(Device::DeviceErrorHardwareFailure); info->finish(Device::DeviceErrorDeviceClassNotFound);
return devicePairingInfo;
} }
void DevicePluginSonos::postSetupDevice(Device *device) void DevicePluginSonos::postSetupDevice(Device *device)
@ -239,88 +245,91 @@ void DevicePluginSonos::deviceRemoved(Device *device)
} }
Device::DeviceError DevicePluginSonos::executeAction(Device *device, const Action &action) void DevicePluginSonos::executeAction(DeviceActionInfo *info)
{ {
Device *device = info->device();
Action action = info->action();
if (device->deviceClassId() == sonosGroupDeviceClassId) { if (device->deviceClassId() == sonosGroupDeviceClassId) {
Sonos *sonos = m_sonosConnections.value(myDevices().findById(device->parentId())); Sonos *sonos = m_sonosConnections.value(myDevices().findById(device->parentId()));
QString groupId = device->paramValue(sonosGroupDeviceGroupIdParamTypeId).toString(); QString groupId = device->paramValue(sonosGroupDeviceGroupIdParamTypeId).toString();
if (!sonos) { if (!sonos) {
qWarning(dcSonos()) << "Action cannot be executed: Sonos connection not available"; qWarning(dcSonos()) << "Action cannot be executed: Sonos connection not available";
return Device::DeviceErrorInvalidParameter; return info->finish(Device::DeviceErrorHardwareNotAvailable, QT_TR_NOOP("Sonos device is not available."));
} }
if (action.actionTypeId() == sonosGroupPlayActionTypeId) { if (action.actionTypeId() == sonosGroupPlayActionTypeId) {
m_pendingActions.insert(sonos->groupPlay(groupId), action.id()); m_pendingActions.insert(sonos->groupPlay(groupId), QPointer<DeviceActionInfo>(info));
return Device::DeviceErrorAsync; return;
} }
if (action.actionTypeId() == sonosGroupShuffleActionTypeId) { if (action.actionTypeId() == sonosGroupShuffleActionTypeId) {
bool shuffle = action.param(sonosGroupShuffleActionShuffleParamTypeId).value().toBool(); bool shuffle = action.param(sonosGroupShuffleActionShuffleParamTypeId).value().toBool();
m_pendingActions.insert(sonos->groupSetShuffle(groupId, shuffle), action.id()); m_pendingActions.insert(sonos->groupSetShuffle(groupId, shuffle), QPointer<DeviceActionInfo>(info));
return Device::DeviceErrorAsync; return;
} }
if (action.actionTypeId() == sonosGroupRepeatActionTypeId) { if (action.actionTypeId() == sonosGroupRepeatActionTypeId) {
if (action.param(sonosGroupRepeatActionRepeatParamTypeId).value().toString() == "None") { if (action.param(sonosGroupRepeatActionRepeatParamTypeId).value().toString() == "None") {
m_pendingActions.insert(sonos->groupSetRepeat(groupId, Sonos::RepeatModeNone), action.id()); m_pendingActions.insert(sonos->groupSetRepeat(groupId, Sonos::RepeatModeNone), QPointer<DeviceActionInfo>(info));
} else if (action.param(sonosGroupRepeatActionRepeatParamTypeId).value().toString() == "One") { } else if (action.param(sonosGroupRepeatActionRepeatParamTypeId).value().toString() == "One") {
m_pendingActions.insert(sonos->groupSetRepeat(groupId, Sonos::RepeatModeOne), action.id()); m_pendingActions.insert(sonos->groupSetRepeat(groupId, Sonos::RepeatModeOne), QPointer<DeviceActionInfo>(info));
} else if (action.param(sonosGroupRepeatActionRepeatParamTypeId).value().toString() == "All") { } else if (action.param(sonosGroupRepeatActionRepeatParamTypeId).value().toString() == "All") {
m_pendingActions.insert(sonos->groupSetRepeat(groupId, Sonos::RepeatModeAll), action.id()); m_pendingActions.insert(sonos->groupSetRepeat(groupId, Sonos::RepeatModeAll), QPointer<DeviceActionInfo>(info));
} else { } else {
return Device::DeviceErrorHardwareFailure; return info->finish(Device::DeviceErrorHardwareFailure);
} }
return Device::DeviceErrorAsync; return;
} }
if (action.actionTypeId() == sonosGroupPauseActionTypeId) { if (action.actionTypeId() == sonosGroupPauseActionTypeId) {
m_pendingActions.insert(sonos->groupPause(groupId), action.id()); m_pendingActions.insert(sonos->groupPause(groupId), QPointer<DeviceActionInfo>(info));
return Device::DeviceErrorAsync; return;
} }
if (action.actionTypeId() == sonosGroupStopActionTypeId) { if (action.actionTypeId() == sonosGroupStopActionTypeId) {
m_pendingActions.insert(sonos->groupPause(groupId), action.id()); m_pendingActions.insert(sonos->groupPause(groupId), QPointer<DeviceActionInfo>(info));
return Device::DeviceErrorAsync; return;
} }
if (action.actionTypeId() == sonosGroupMuteActionTypeId) { if (action.actionTypeId() == sonosGroupMuteActionTypeId) {
bool mute = action.param(sonosGroupMuteActionMuteParamTypeId).value().toBool(); bool mute = action.param(sonosGroupMuteActionMuteParamTypeId).value().toBool();
m_pendingActions.insert(sonos->setGroupMute(groupId, mute), action.id()); m_pendingActions.insert(sonos->setGroupMute(groupId, mute), QPointer<DeviceActionInfo>(info));
return Device::DeviceErrorAsync; return;
} }
if (action.actionTypeId() == sonosGroupVolumeActionTypeId) { if (action.actionTypeId() == sonosGroupVolumeActionTypeId) {
int volume = action.param(sonosGroupVolumeActionVolumeParamTypeId).value().toInt(); int volume = action.param(sonosGroupVolumeActionVolumeParamTypeId).value().toInt();
m_pendingActions.insert(sonos->setGroupVolume(groupId, volume), action.id()); m_pendingActions.insert(sonos->setGroupVolume(groupId, volume), QPointer<DeviceActionInfo>(info));
return Device::DeviceErrorAsync; return;
} }
if (action.actionTypeId() == sonosGroupSkipNextActionTypeId) { if (action.actionTypeId() == sonosGroupSkipNextActionTypeId) {
m_pendingActions.insert(sonos->groupSkipToNextTrack(groupId), action.id()); m_pendingActions.insert(sonos->groupSkipToNextTrack(groupId), QPointer<DeviceActionInfo>(info));
return Device::DeviceErrorAsync; return;
} }
if (action.actionTypeId() == sonosGroupSkipBackActionTypeId) { if (action.actionTypeId() == sonosGroupSkipBackActionTypeId) {
m_pendingActions.insert(sonos->groupSkipToPreviousTrack(groupId), action.id()); m_pendingActions.insert(sonos->groupSkipToPreviousTrack(groupId), QPointer<DeviceActionInfo>(info));
return Device::DeviceErrorAsync; return;
} }
if (action.actionTypeId() == sonosGroupPlaybackStatusActionTypeId) { if (action.actionTypeId() == sonosGroupPlaybackStatusActionTypeId) {
QString playbackStatus = action.param(sonosGroupPlaybackStatusActionPlaybackStatusParamTypeId).value().toString(); QString playbackStatus = action.param(sonosGroupPlaybackStatusActionPlaybackStatusParamTypeId).value().toString();
if (playbackStatus == "Playing") { if (playbackStatus == "Playing") {
m_pendingActions.insert(sonos->groupPlay(groupId), action.id()); m_pendingActions.insert(sonos->groupPlay(groupId), QPointer<DeviceActionInfo>(info));
} else if(playbackStatus == "Stopped") { } else if(playbackStatus == "Stopped") {
m_pendingActions.insert(sonos->groupPause(groupId), action.id()); m_pendingActions.insert(sonos->groupPause(groupId), QPointer<DeviceActionInfo>(info));
} else if(playbackStatus == "Paused") { } else if(playbackStatus == "Paused") {
m_pendingActions.insert(sonos->groupPause(groupId), action.id()); m_pendingActions.insert(sonos->groupPause(groupId), QPointer<DeviceActionInfo>(info));
} }
return Device::DeviceErrorAsync; return;
} }
return Device::DeviceErrorActionTypeNotFound; return info->finish(Device::DeviceErrorActionTypeNotFound);
} }
return Device::DeviceErrorDeviceClassNotFound; info->finish(Device::DeviceErrorDeviceClassNotFound);
} }
void DevicePluginSonos::onConnectionChanged(bool connected) void DevicePluginSonos::onConnectionChanged(bool connected)
@ -343,21 +352,13 @@ void DevicePluginSonos::onAuthenticationStatusChanged(bool authenticated)
if (!device) if (!device)
return; return;
if (!device->setupComplete()) { device->setStateValue(sonosConnectionLoggedInStateTypeId, authenticated);
if (authenticated) { if (!authenticated) {
emit deviceSetupFinished(device, Device::DeviceSetupStatusSuccess); //refresh access token needs to be refreshed
} else { pluginStorage()->beginGroup(device->id().toString());
emit deviceSetupFinished(device, Device::DeviceSetupStatusFailure); QByteArray refreshToken = pluginStorage()->value("refresh_token").toByteArray();
} pluginStorage()->endGroup();
} else { sonosConnection->getAccessTokenFromRefreshToken(refreshToken);
device->setStateValue(sonosConnectionLoggedInStateTypeId, authenticated);
if (!authenticated) {
//refresh access token needs to be refreshed
pluginStorage()->beginGroup(device->id().toString());
QByteArray refreshToken = pluginStorage()->value("refresh_token").toByteArray();
pluginStorage()->endGroup();
sonosConnection->getAccessTokenFromRefreshToken(refreshToken);
}
} }
} }
@ -421,7 +422,7 @@ void DevicePluginSonos::onGroupsReceived(const QString &householdId, QList<Sonos
} }
if (!deviceDescriptors.isEmpty()) if (!deviceDescriptors.isEmpty())
emit autoDevicesAppeared(sonosGroupDeviceClassId, deviceDescriptors); emit autoDevicesAppeared(deviceDescriptors);
//delete auto devices //delete auto devices
foreach(Device *groupDevice, myDevices().filterByParentDeviceId(parentDevice->id())) { foreach(Device *groupDevice, myDevices().filterByParentDeviceId(parentDevice->id())) {
@ -495,11 +496,15 @@ void DevicePluginSonos::onVolumeReceived(const QString &groupId, Sonos::VolumeOb
void DevicePluginSonos::onActionExecuted(QUuid sonosActionId, bool success) void DevicePluginSonos::onActionExecuted(QUuid sonosActionId, bool success)
{ {
if (m_pendingActions.contains(sonosActionId)) { if (m_pendingActions.contains(sonosActionId)) {
ActionId nymeaActionId = m_pendingActions.value(sonosActionId); QPointer<DeviceActionInfo> info = m_pendingActions.value(sonosActionId);
if (info.isNull()) {
qCWarning(dcSonos()) << "DeviceActionInfo has disappeared. Did it time out?";
return;
}
if (success) { if (success) {
emit actionExecutionFinished(nymeaActionId, Device::DeviceErrorNoError); info->finish(Device::DeviceErrorNoError);
} else { } else {
emit actionExecutionFinished(nymeaActionId, Device::DeviceErrorHardwareFailure); info->finish(Device::DeviceErrorHardwareFailure);
} }
} }
} }

View File

@ -40,14 +40,14 @@ public:
explicit DevicePluginSonos(); explicit DevicePluginSonos();
~DevicePluginSonos() override; ~DevicePluginSonos() override;
Device::DeviceSetupStatus setupDevice(Device *device) override; void setupDevice(DeviceSetupInfo *info) override;
DevicePairingInfo pairDevice(DevicePairingInfo &devicePairingInfo) override; void startPairing(DevicePairingInfo *info) override;
DevicePairingInfo confirmPairing(DevicePairingInfo &devicePairingInfo, const QString &username, const QString &secret) override; void confirmPairing(DevicePairingInfo *info, const QString &username, const QString &secret) override;
void postSetupDevice(Device *device) override; void postSetupDevice(Device *device) override;
void startMonitoringAutoDevices() override; void startMonitoringAutoDevices() override;
void deviceRemoved(Device *device) override; void deviceRemoved(Device *device) override;
Device::DeviceError executeAction(Device *device, const Action &action) override; void executeAction(DeviceActionInfo *info) override;
private: private:
PluginTimer *m_pluginTimer5sec = nullptr; PluginTimer *m_pluginTimer5sec = nullptr;
@ -60,7 +60,7 @@ private:
QByteArray m_sonosConnectionAccessToken; QByteArray m_sonosConnectionAccessToken;
QByteArray m_sonosConnectionRefreshToken; QByteArray m_sonosConnectionRefreshToken;
QHash<QUuid, ActionId> m_pendingActions; QHash<QUuid, QPointer<DeviceActionInfo> > m_pendingActions;
private slots: private slots:
void onConnectionChanged(bool connected); void onConnectionChanged(bool connected);

View File

@ -13,7 +13,7 @@
"id": "22df416d-7732-44f1-b6b9-e41296211178", "id": "22df416d-7732-44f1-b6b9-e41296211178",
"name": "sonosConnection", "name": "sonosConnection",
"displayName": "Sonos connection", "displayName": "Sonos connection",
"interfaces": ["gateway"], "interfaces": ["account", "gateway"],
"createMethods": ["user"], "createMethods": ["user"],
"setupMethod": "oauth", "setupMethod": "oauth",
"paramTypes": [ "paramTypes": [
@ -40,7 +40,8 @@
"name": "userDisplayName", "name": "userDisplayName",
"displayName": "User name", "displayName": "User name",
"displayNameEvent": "User name changed", "displayNameEvent": "User name changed",
"type": "QString" "type": "QString",
"defaultValue": ""
} }
] ]
}, },

View File

@ -102,15 +102,15 @@ void Sonos::getHouseholds()
emit connectionChanged(true); emit connectionChanged(true);
emit authenticationStatusChanged(true); emit authenticationStatusChanged(true);
QJsonDocument data = QJsonDocument::fromJson(reply->readAll()); QJsonParseError error;
if (!data.isObject()) { QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error);
if (error.error != QJsonParseError::NoError) {
qDebug(dcSonos()) << "Household ID: Recieved invalide JSON object"; qDebug(dcSonos()) << "Household ID: Recieved invalide JSON object";
return; return;
} }
QList<QString> households; QList<QString> households;
QJsonArray jsonArray = data["households"].toArray(); foreach (const QVariant &variant, data.toVariant().toMap().value("households").toList()) {
foreach (const QJsonValue & value, jsonArray) { QVariantMap obj = variant.toMap();
QJsonObject obj = value.toObject();
qDebug(dcSonos()) << "Household ID received:" << obj["id"].toString(); qDebug(dcSonos()) << "Household ID received:" << obj["id"].toString();
households.append(obj["id"].toString()); households.append(obj["id"].toString());
} }
@ -182,18 +182,21 @@ void Sonos::getFavorites(const QString &householdId)
emit connectionChanged(true); emit connectionChanged(true);
emit authenticationStatusChanged(true); emit authenticationStatusChanged(true);
QJsonDocument data = QJsonDocument::fromJson(reply->readAll()); QJsonParseError error;
if (!data.isObject()) QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error);
if (error.error != QJsonParseError::NoError) {
qCWarning(dcSonos()) << "Invalid json received from server";
return;
}
if (!data.toVariant().toMap().contains("items"))
return; return;
if (!data["items"].isArray()) QVariantList array = data.toVariant().toMap().value("items").toList();
return;
QJsonArray array = data["items"].toArray();
QList<FavouriteObject> favourites QList<FavouriteObject> favourites
; ;
foreach (const QJsonValue & value, array) { foreach (const QVariant &variant, array) {
QJsonObject itemObject = value.toObject(); QVariantMap itemObject = variant.toMap();
qDebug(dcSonos()) << "Item ID received:" << itemObject["id"].toString(); qDebug(dcSonos()) << "Item ID received:" << itemObject["id"].toString();
FavouriteObject favourite; FavouriteObject favourite;
favourite.id = itemObject["id"].toString(); favourite.id = itemObject["id"].toString();
@ -233,17 +236,18 @@ void Sonos::getGroups(const QString &householdId)
emit authenticationStatusChanged(true); emit authenticationStatusChanged(true);
//qDebug(dcSonos()) << "Received response from Sonos" << reply->readAll(); //qDebug(dcSonos()) << "Received response from Sonos" << reply->readAll();
QJsonDocument data = QJsonDocument::fromJson(reply->readAll()); QJsonParseError error;
if (!data.isObject()) QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error);
if (error.error != QJsonParseError::NoError)
return; return;
if (!data["groups"].isArray()) if (!data.toVariant().toMap().contains("groups"))
return; return;
QJsonArray array = data["groups"].toArray(); QVariantList array = data.toVariant().toMap().value("groups").toList();
QList<GroupObject> groupObjects; QList<GroupObject> groupObjects;
foreach (const QJsonValue & value, array) { foreach (const QVariant &value, array) {
QJsonObject obj = value.toObject(); QVariantMap obj = value.toMap();
qDebug(dcSonos()) << "Group ID received:" << obj["id"].toString(); qDebug(dcSonos()) << "Group ID received:" << obj["id"].toString();
GroupObject group; GroupObject group;
group.groupId = obj["id"].toString(); group.groupId = obj["id"].toString();
@ -281,15 +285,19 @@ void Sonos::getGroupVolume(const QString &groupId)
emit authenticationStatusChanged(true); emit authenticationStatusChanged(true);
//qDebug(dcSonos()) << "Received response from Sonos" << reply->readAll(); //qDebug(dcSonos()) << "Received response from Sonos" << reply->readAll();
QJsonDocument data = QJsonDocument::fromJson(reply->readAll()); QJsonParseError error;
if (!data.isObject()) QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error);
if (error.error != QJsonParseError::NoError) {
qCWarning(dcSonos()) << "JSON Parse error" << error.errorString();
return; return;
}
VolumeObject volume; VolumeObject volume;
volume.volume = data["volume"].toInt(); QVariantMap variant = data.toVariant().toMap();
volume.muted = data["muted"].toBool(); volume.volume = variant["volume"].toInt();
volume.fixed = data["fixed"].toBool(); volume.muted = variant["muted"].toBool();
volume.fixed = variant["fixed"].toBool();
emit volumeReceived(groupId, volume); emit volumeReceived(groupId, volume);
}); });
@ -1100,14 +1108,18 @@ void Sonos::getPlayerVolume(const QByteArray &playerId)
emit authenticationStatusChanged(true); emit authenticationStatusChanged(true);
//qDebug(dcSonos()) << "Received response from Sonos" << reply->readAll(); //qDebug(dcSonos()) << "Received response from Sonos" << reply->readAll();
QJsonDocument data = QJsonDocument::fromJson(reply->readAll()); QJsonParseError error;
if (!data.isObject()) QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error);
if (error.error != QJsonParseError::NoError) {
qCWarning(dcSonos()) << "Json parse error" << error.errorString();
return; return;
}
VolumeObject volume; VolumeObject volume;
volume.volume = data["volume"].toInt(); QVariantMap variant = data.toVariant().toMap();
volume.muted = data["muted"].toBool(); volume.volume = variant["volume"].toInt();
volume.fixed = data["fixed"].toBool(); volume.muted = variant["muted"].toBool();
volume.fixed = variant["fixed"].toBool();
emit playerVolumeReceived(playerId, volume); emit playerVolumeReceived(playerId, volume);
}); });
} }
@ -1261,17 +1273,22 @@ void Sonos::getPlaylist(const QString &householdId, const QString &playlistId)
emit authenticationStatusChanged(true); emit authenticationStatusChanged(true);
//qDebug(dcSonos()) << "Received response from Sonos" << reply->readAll(); //qDebug(dcSonos()) << "Received response from Sonos" << reply->readAll();
QJsonDocument data = QJsonDocument::fromJson(reply->readAll()); QJsonParseError error;
if (!data.isObject()) QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error);
if (error.error != QJsonParseError::NoError) {
qCWarning(dcSonos()) << "Json parse error" << error.errorString();
return; return;
}
if (!data["tracks"].isArray()) QVariantMap variant = data.toVariant().toMap();
if (!variant.contains("tracks"))
return; return;
PlaylistSummaryObject playlist; PlaylistSummaryObject playlist;
QJsonArray array = data["tracks"].toArray(); QVariantList array = variant["tracks"].toList();
foreach (const QJsonValue & value, array) { foreach (const QVariant &value, array) {
QJsonObject itemObject = value.toObject(); QVariantMap itemObject = value.toMap();
qDebug(dcSonos()) << "Item ID received:" << itemObject["id"].toString(); qDebug(dcSonos()) << "Item ID received:" << itemObject["id"].toString();
PlaylistTrackObject track; PlaylistTrackObject track;
track.name = itemObject["name"].toString(); track.name = itemObject["name"].toString();
@ -1310,17 +1327,20 @@ void Sonos::getPlaylists(const QString &householdId)
emit authenticationStatusChanged(true); emit authenticationStatusChanged(true);
//qDebug(dcSonos()) << "Received response from Sonos" << reply->readAll(); //qDebug(dcSonos()) << "Received response from Sonos" << reply->readAll();
QJsonDocument data = QJsonDocument::fromJson(reply->readAll()); QJsonParseError error;
if (!data.isObject()) QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error);
if (!data.isObject()) {
qCWarning(dcSonos()) << "Json parse error:" << error.errorString();
return;
}
if (!data.toVariant().toMap().contains("playlists"))
return; return;
if (!data["items"].isArray()) QVariantList array = data.toVariant().toMap().value("playlists").toList();
return;
QJsonArray array = data["playlists"].toArray();
QList<PlaylistObject> playlists; QList<PlaylistObject> playlists;
foreach (const QJsonValue & value, array) { foreach (const QVariant &value, array) {
QJsonObject itemObject = value.toObject(); QVariantMap itemObject = value.toMap();
qDebug(dcSonos()) << "Item ID received:" << itemObject["id"].toString(); qDebug(dcSonos()) << "Item ID received:" << itemObject["id"].toString();
PlaylistObject playlist; PlaylistObject playlist;
playlist.id = itemObject["id"].toString(); playlist.id = itemObject["id"].toString();
@ -1396,10 +1416,14 @@ void Sonos::getPlayerSettings(const QString &playerId)
emit connectionChanged(true); emit connectionChanged(true);
emit authenticationStatusChanged(true); emit authenticationStatusChanged(true);
QJsonDocument data = QJsonDocument::fromJson(reply->readAll()); QJsonParseError error;
if (!data.isObject()) QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll(), &error);
if (error.error != QJsonParseError::NoError) {
qCWarning(dcSonos()) << "Json parse error" << error.errorString();
return; return;
}
QVariantMap data = jsonDoc.toVariant().toMap();
PlayerSettingsObject playerSettings; PlayerSettingsObject playerSettings;
playerSettings.monoMode = data["monoMode"].toBool(); playerSettings.monoMode = data["monoMode"].toBool();
playerSettings.volumeMode = data["volumeMode"].toString(); playerSettings.volumeMode = data["volumeMode"].toString();