Add additional patchChannelSettings variants and addChannel.

This commit is contained in:
srcejon 2024-04-06 22:22:28 +01:00
parent be7199531d
commit 035e6f59be
4 changed files with 142 additions and 13 deletions

View File

@ -1,5 +1,5 @@
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2020-2023 Jon Beniston, M7RCE <jon@beniston.com> // // Copyright (C) 2020-2024 Jon Beniston, M7RCE <jon@beniston.com> //
// Copyright (C) 2020, 2022 Edouard Griffiths, F4EXB <f4exb06@gmail.com> // // Copyright (C) 2020, 2022 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
// // // //
// This program is free software; you can redistribute it and/or modify // // 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; 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) bool ChannelWebAPIUtils::getChannelReport(unsigned int deviceIndex, unsigned int channelIndex, SWGSDRangel::SWGChannelReport &channelReport)
{ {
QString errorResponse; QString errorResponse;
@ -1485,22 +1502,19 @@ bool ChannelWebAPIUtils::patchFeatureSetting(unsigned int featureSetIndex, unsig
} }
} }
bool ChannelWebAPIUtils::patchChannelSetting(ChannelAPI *channel, const QString &setting, const QVariant& value)
bool ChannelWebAPIUtils::patchChannelSetting(unsigned int deviceSetIndex, unsigned int channelIndex, const QString &setting, double value)
{ {
SWGSDRangel::SWGChannelSettings channelSettingsResponse; SWGSDRangel::SWGChannelSettings channelSettingsResponse;
QString errorResponse; QString errorResponse;
int httpRC; int httpRC;
ChannelAPI *channel;
if (getChannelSettings(deviceSetIndex, channelIndex, channelSettingsResponse, channel)) if (getChannelSettings(channel, channelSettingsResponse))
{ {
// Patch settings // Patch settings
QJsonObject *jsonObj = channelSettingsResponse.asJsonObject(); QJsonObject *jsonObj = channelSettingsResponse.asJsonObject();
double oldValue; if (WebAPIUtils::hasSubObject(*jsonObj, setting))
if (WebAPIUtils::getSubObjectDouble(*jsonObj, setting, oldValue))
{ {
WebAPIUtils::setSubObjectDouble(*jsonObj, setting, value); WebAPIUtils::setSubObject(*jsonObj, setting, value);
QStringList channelSettingsKeys; QStringList channelSettingsKeys;
channelSettingsKeys.append(setting); channelSettingsKeys.append(setting);
channelSettingsResponse.init(); channelSettingsResponse.init();
@ -1511,19 +1525,19 @@ bool ChannelWebAPIUtils::patchChannelSetting(unsigned int deviceSetIndex, unsign
if (httpRC/100 == 2) 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; return true;
} }
else else
{ {
qWarning("ChannelWebAPIUtils::patchChannelSetting: set feature setting %s to %f error %d: %s", qWarning("ChannelWebAPIUtils::patchChannelSetting: set feature setting %s to %s error %d: %s",
qPrintable(setting), value, httpRC, qPrintable(*errorResponse2.getMessage())); qPrintable(setting), qPrintable(value.toString()), httpRC, qPrintable(*errorResponse2.getMessage()));
return false; return false;
} }
} }
else 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; 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) bool ChannelWebAPIUtils::patchChannelSetting(unsigned int deviceSetIndex, unsigned int channelIndex, const QString &setting, const QJsonArray& value)
{ {
SWGSDRangel::SWGChannelSettings channelSettingsResponse; SWGSDRangel::SWGChannelSettings channelSettingsResponse;
@ -1835,3 +1882,30 @@ bool ChannelWebAPIUtils::getChannelReportValue(unsigned int deviceIndex, unsigne
return false; 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;
}
}

View File

@ -2,7 +2,7 @@
// Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany // // Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany //
// written by Christian Daniel // // written by Christian Daniel //
// Copyright (C) 2015-2020 Edouard Griffiths, F4EXB <f4exb06@gmail.com> // // Copyright (C) 2015-2020 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
// Copyright (C) 2020-2023 Jon Beniston, M7RCE <jon@beniston.com> // // Copyright (C) 2020-2024 Jon Beniston, M7RCE <jon@beniston.com> //
// // // //
// This program is free software; you can redistribute it and/or modify // // 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 // // 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, 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, double value);
static bool patchFeatureSetting(unsigned int featureSetIndex, unsigned int featureIndex, const QString &setting, const QJsonArray& 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, double value);
static bool patchChannelSetting(unsigned int deviceSetIndex, unsigned int channeIndex, const QString &setting, const QJsonArray& 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); 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 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 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(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 getChannelReport(unsigned int deviceIndex, unsigned int channelIndex, SWGSDRangel::SWGChannelReport &channelReport);
static bool addChannel(unsigned int deviceSetIndex, const QString& uri, int direction);
protected: protected:
static QString getDeviceHardwareId(unsigned int deviceIndex); static QString getDeviceHardwareId(unsigned int deviceIndex);
}; };

View File

@ -584,6 +584,54 @@ bool WebAPIUtils::getSubObjectIntList(const QJsonObject &json, const QString &ke
return false; 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 // look for value in key=value
bool WebAPIUtils::extractValue(const QJsonObject &json, const QString &key, QJsonValue &value) bool WebAPIUtils::extractValue(const QJsonObject &json, const QString &key, QJsonValue &value)
{ {

View File

@ -54,6 +54,8 @@ public:
static bool getSubObjectString(const QJsonObject &json, const QString &key, QString &value); static bool getSubObjectString(const QJsonObject &json, const QString &key, QString &value);
static bool setSubObjectString(QJsonObject &json, const QString &key, const 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<int> &values); static bool getSubObjectIntList(const QJsonObject &json, const QString &key, const QString &subKey, QList<int> &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 extractValue(const QJsonObject &json, const QString &key, QJsonValue &value);
static bool extractArray(const QJsonObject &json, const QString &key, QJsonArray &value); static bool extractArray(const QJsonObject &json, const QString &key, QJsonArray &value);
static bool extractObject(const QJsonObject &json, const QString &key, QJsonObject &value); static bool extractObject(const QJsonObject &json, const QString &key, QJsonObject &value);