diff --git a/sdrbase/channel/channelwebapiutils.cpp b/sdrbase/channel/channelwebapiutils.cpp index 42292d54c..141964aa6 100644 --- a/sdrbase/channel/channelwebapiutils.cpp +++ b/sdrbase/channel/channelwebapiutils.cpp @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////////////////////// -// Copyright (C) 2020-2023 Jon Beniston, M7RCE // +// Copyright (C) 2020-2024 Jon Beniston, M7RCE // // Copyright (C) 2020, 2022 Edouard Griffiths, F4EXB // // // // This program is free software; you can redistribute it and/or modify // @@ -267,6 +267,23 @@ bool ChannelWebAPIUtils::getChannelSettings(unsigned int deviceIndex, unsigned i return true; } +bool ChannelWebAPIUtils::getChannelSettings(ChannelAPI *channel, SWGSDRangel::SWGChannelSettings &channelSettingsResponse) +{ + QString errorResponse; + int httpRC; + + httpRC = channel->webapiSettingsGet(channelSettingsResponse, errorResponse); + + if (httpRC/100 != 2) + { + qWarning("ChannelWebAPIUtils::getChannelSettings: get channel settings error %d: %s", + httpRC, qPrintable(errorResponse)); + return false; + } + + return true; +} + bool ChannelWebAPIUtils::getChannelReport(unsigned int deviceIndex, unsigned int channelIndex, SWGSDRangel::SWGChannelReport &channelReport) { QString errorResponse; @@ -1485,22 +1502,19 @@ bool ChannelWebAPIUtils::patchFeatureSetting(unsigned int featureSetIndex, unsig } } - -bool ChannelWebAPIUtils::patchChannelSetting(unsigned int deviceSetIndex, unsigned int channelIndex, const QString &setting, double value) +bool ChannelWebAPIUtils::patchChannelSetting(ChannelAPI *channel, const QString &setting, const QVariant& value) { SWGSDRangel::SWGChannelSettings channelSettingsResponse; QString errorResponse; int httpRC; - ChannelAPI *channel; - if (getChannelSettings(deviceSetIndex, channelIndex, channelSettingsResponse, channel)) + if (getChannelSettings(channel, channelSettingsResponse)) { // Patch settings QJsonObject *jsonObj = channelSettingsResponse.asJsonObject(); - double oldValue; - if (WebAPIUtils::getSubObjectDouble(*jsonObj, setting, oldValue)) + if (WebAPIUtils::hasSubObject(*jsonObj, setting)) { - WebAPIUtils::setSubObjectDouble(*jsonObj, setting, value); + WebAPIUtils::setSubObject(*jsonObj, setting, value); QStringList channelSettingsKeys; channelSettingsKeys.append(setting); channelSettingsResponse.init(); @@ -1511,19 +1525,19 @@ bool ChannelWebAPIUtils::patchChannelSetting(unsigned int deviceSetIndex, unsign if (httpRC/100 == 2) { - qDebug("ChannelWebAPIUtils::patchChannelSetting: set feature setting %s to %f OK", qPrintable(setting), value); + qDebug("ChannelWebAPIUtils::patchChannelSetting: set feature setting %s to %s OK", qPrintable(setting), qPrintable(value.toString())); return true; } else { - qWarning("ChannelWebAPIUtils::patchChannelSetting: set feature setting %s to %f error %d: %s", - qPrintable(setting), value, httpRC, qPrintable(*errorResponse2.getMessage())); + qWarning("ChannelWebAPIUtils::patchChannelSetting: set feature setting %s to %s error %d: %s", + qPrintable(setting), qPrintable(value.toString()), httpRC, qPrintable(*errorResponse2.getMessage())); return false; } } else { - qWarning("ChannelWebAPIUtils::patchChannelSetting: no key %s in feature settings", qPrintable(setting)); + qWarning("ChannelWebAPIUtils::patchChannelSetting: no key %s in channel settings", qPrintable(setting)); return false; } } @@ -1533,6 +1547,39 @@ bool ChannelWebAPIUtils::patchChannelSetting(unsigned int deviceSetIndex, unsign } } +bool ChannelWebAPIUtils::patchChannelSetting(unsigned int deviceSetIndex, unsigned int channelIndex, const QString &setting, const QString& value) +{ + ChannelAPI *channel = MainCore::instance()->getChannel(deviceSetIndex, channelIndex); + + if (channel) { + return patchChannelSetting(channel, setting, value); + } else { + return false; + } +} + +bool ChannelWebAPIUtils::patchChannelSetting(unsigned int deviceSetIndex, unsigned int channelIndex, const QString &setting, int value) +{ + ChannelAPI *channel = MainCore::instance()->getChannel(deviceSetIndex, channelIndex); + + if (channel) { + return patchChannelSetting(channel, setting, value); + } else { + return false; + } +} + +bool ChannelWebAPIUtils::patchChannelSetting(unsigned int deviceSetIndex, unsigned int channelIndex, const QString &setting, double value) +{ + ChannelAPI *channel = MainCore::instance()->getChannel(deviceSetIndex, channelIndex); + + if (channel) { + return patchChannelSetting(channel, setting, value); + } else { + return false; + } +} + bool ChannelWebAPIUtils::patchChannelSetting(unsigned int deviceSetIndex, unsigned int channelIndex, const QString &setting, const QJsonArray& value) { SWGSDRangel::SWGChannelSettings channelSettingsResponse; @@ -1835,3 +1882,30 @@ bool ChannelWebAPIUtils::getChannelReportValue(unsigned int deviceIndex, unsigne return false; } +bool ChannelWebAPIUtils::addChannel(unsigned int deviceSetIndex, const QString& uri, int direction) +{ + MainCore *mainCore = MainCore::instance(); + PluginAPI::ChannelRegistrations *channelRegistrations = mainCore->getPluginManager()->getRxChannelRegistrations(); + int nbRegistrations = channelRegistrations->size(); + int index = 0; + + for (; index < nbRegistrations; index++) + { + if (channelRegistrations->at(index).m_channelIdURI == uri) { + break; + } + } + + if (index < nbRegistrations) + { + MainCore::MsgAddChannel *msg = MainCore::MsgAddChannel::create(deviceSetIndex, index, direction); + mainCore->getMainMessageQueue()->push(msg); + + return true; + } + else + { + qWarning() << "ChannelWebAPIUtils::addChannel:" << uri << "plugin not available"; + return false; + } +} diff --git a/sdrbase/channel/channelwebapiutils.h b/sdrbase/channel/channelwebapiutils.h index 8a9d25c4c..6e65cfcef 100644 --- a/sdrbase/channel/channelwebapiutils.h +++ b/sdrbase/channel/channelwebapiutils.h @@ -2,7 +2,7 @@ // Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany // // written by Christian Daniel // // Copyright (C) 2015-2020 Edouard Griffiths, F4EXB // -// Copyright (C) 2020-2023 Jon Beniston, M7RCE // +// Copyright (C) 2020-2024 Jon Beniston, M7RCE // // // // This program is free software; you can redistribute it and/or modify // // it under the terms of the GNU General Public License as published by // @@ -79,6 +79,9 @@ public: static bool patchFeatureSetting(unsigned int featureSetIndex, unsigned int featureIndex, const QString &setting, const QString &value); static bool patchFeatureSetting(unsigned int featureSetIndex, unsigned int featureIndex, const QString &setting, double value); static bool patchFeatureSetting(unsigned int featureSetIndex, unsigned int featureIndex, const QString &setting, const QJsonArray& value); + static bool patchChannelSetting(ChannelAPI *channel, const QString &setting, const QVariant &value); + static bool patchChannelSetting(unsigned int deviceSetIndex, unsigned int channeIndex, const QString &setting, const QString &value); + static bool patchChannelSetting(unsigned int deviceSetIndex, unsigned int channeIndex, const QString &setting, int value); static bool patchChannelSetting(unsigned int deviceSetIndex, unsigned int channeIndex, const QString &setting, double value); static bool patchChannelSetting(unsigned int deviceSetIndex, unsigned int channeIndex, const QString &setting, const QJsonArray& value); static bool getFeatureSetting(unsigned int featureSetIndex, unsigned int featureIndex, const QString &setting, int &value); @@ -98,7 +101,9 @@ public: static bool getFeatureSettings(unsigned int featureSetIndex, unsigned int featureIndex, SWGSDRangel::SWGFeatureSettings &featureSettingsResponse, Feature *&feature); static bool getFeatureReport(unsigned int featureSetIndex, unsigned int featureIndex, SWGSDRangel::SWGFeatureReport &featureReport); static bool getChannelSettings(unsigned int deviceIndex, unsigned int channelIndex, SWGSDRangel::SWGChannelSettings &channelSettingsResponse, ChannelAPI *&channel); + static bool getChannelSettings(ChannelAPI *channel, SWGSDRangel::SWGChannelSettings &channelSettingsResponse); static bool getChannelReport(unsigned int deviceIndex, unsigned int channelIndex, SWGSDRangel::SWGChannelReport &channelReport); + static bool addChannel(unsigned int deviceSetIndex, const QString& uri, int direction); protected: static QString getDeviceHardwareId(unsigned int deviceIndex); }; diff --git a/sdrbase/webapi/webapiutils.cpp b/sdrbase/webapi/webapiutils.cpp index 3ceb398b4..5ed235b3d 100644 --- a/sdrbase/webapi/webapiutils.cpp +++ b/sdrbase/webapi/webapiutils.cpp @@ -584,6 +584,54 @@ bool WebAPIUtils::getSubObjectIntList(const QJsonObject &json, const QString &ke return false; } +bool WebAPIUtils::hasSubObject(const QJsonObject &json, const QString &key) +{ + for (QJsonObject::const_iterator it = json.begin(); it != json.end(); it++) + { + QJsonValue jsonValue = it.value(); + + if (jsonValue.isObject()) + { + QJsonObject subObject = jsonValue.toObject(); + + if (subObject.contains(key)) { + return true; + } + } + } + + return false; +} + +// Set value withing nested JSON object +bool WebAPIUtils::setSubObject(QJsonObject &json, const QString &key, const QVariant &value) +{ + for (QJsonObject::iterator it = json.begin(); it != json.end(); it++) + { + QJsonValue jsonValue = it.value(); + + if (jsonValue.isObject()) + { + QJsonObject subObject = jsonValue.toObject(); + + if (subObject.contains(key)) + { + if (subObject[key].isString()) { + subObject[key] = value.toString(); + } else if (subObject[key].isDouble()) { + subObject[key] = value.toDouble(); + } else { + qDebug() << "WebAPIUtils::setSubObject: Unsupported type"; + } + it.value() = subObject; + return true; + } + } + } + + return false; +} + // look for value in key=value bool WebAPIUtils::extractValue(const QJsonObject &json, const QString &key, QJsonValue &value) { diff --git a/sdrbase/webapi/webapiutils.h b/sdrbase/webapi/webapiutils.h index 752996217..243053362 100644 --- a/sdrbase/webapi/webapiutils.h +++ b/sdrbase/webapi/webapiutils.h @@ -54,6 +54,8 @@ public: static bool getSubObjectString(const QJsonObject &json, const QString &key, QString &value); static bool setSubObjectString(QJsonObject &json, const QString &key, const QString &value); static bool getSubObjectIntList(const QJsonObject &json, const QString &key, const QString &subKey, QList &values); + static bool setSubObject(QJsonObject &json, const QString &key, const QVariant &value); + static bool hasSubObject(const QJsonObject &json, const QString &key); static bool extractValue(const QJsonObject &json, const QString &key, QJsonValue &value); static bool extractArray(const QJsonObject &json, const QString &key, QJsonArray &value); static bool extractObject(const QJsonObject &json, const QString &key, QJsonObject &value);