REST API: config: GET (3): mechanism to deal with channel settings API formatting without creating a complete channel object. Applied to AM demod

This commit is contained in:
f4exb 2019-08-01 18:50:21 +02:00
parent 8ce4788b55
commit 810bbe2979
17 changed files with 250 additions and 38 deletions

View File

@ -3,13 +3,15 @@ project(am)
set(am_SOURCES
amdemod.cpp
amdemodsettings.cpp
amdemodplugin.cpp
amdemodplugin.cpp
amdemodwebapiadapter.cpp
)
set(am_HEADERS
amdemod.h
amdemodsettings.h
amdemodplugin.h
amdemodplugin.h
amdemodwebapiadapter.h
)

View File

@ -598,17 +598,41 @@ int AMDemod::webapiSettingsPutPatch(
SWGSDRangel::SWGChannelSettings& response,
QString& errorMessage)
{
(void) errorMessage;
AMDemodSettings settings = m_settings;
bool frequencyOffsetChanged = false;
webapiUpdateChannelSettings(settings, channelSettingsKeys, response);
if (settings.m_inputFrequencyOffset != m_settings.m_inputFrequencyOffset)
{
MsgConfigureChannelizer* channelConfigMsg = MsgConfigureChannelizer::create(
m_audioSampleRate, settings.m_inputFrequencyOffset);
m_inputMessageQueue.push(channelConfigMsg);
}
MsgConfigureAMDemod *msg = MsgConfigureAMDemod::create(settings, force);
m_inputMessageQueue.push(msg);
qDebug("AMDemod::webapiSettingsPutPatch: forward to GUI: %p", m_guiMessageQueue);
if (m_guiMessageQueue) // forward to GUI if any
{
MsgConfigureAMDemod *msgToGUI = MsgConfigureAMDemod::create(settings, force);
m_guiMessageQueue->push(msgToGUI);
}
webapiFormatChannelSettings(response, settings);
return 200;
}
void AMDemod::webapiUpdateChannelSettings(
AMDemodSettings& settings,
const QStringList& channelSettingsKeys,
SWGSDRangel::SWGChannelSettings& response)
{
if (channelSettingsKeys.contains("audioMute")) {
settings.m_audioMute = response.getAmDemodSettings()->getAudioMute() != 0;
}
if (channelSettingsKeys.contains("inputFrequencyOffset"))
{
if (channelSettingsKeys.contains("inputFrequencyOffset")) {
settings.m_inputFrequencyOffset = response.getAmDemodSettings()->getInputFrequencyOffset();
frequencyOffsetChanged = true;
}
if (channelSettingsKeys.contains("rfBandwidth")) {
settings.m_rfBandwidth = response.getAmDemodSettings()->getRfBandwidth();
@ -661,27 +685,6 @@ int AMDemod::webapiSettingsPutPatch(
if (channelSettingsKeys.contains("reverseAPIChannelIndex")) {
settings.m_reverseAPIChannelIndex = response.getAmDemodSettings()->getReverseApiChannelIndex();
}
if (frequencyOffsetChanged)
{
MsgConfigureChannelizer* channelConfigMsg = MsgConfigureChannelizer::create(
m_audioSampleRate, settings.m_inputFrequencyOffset);
m_inputMessageQueue.push(channelConfigMsg);
}
MsgConfigureAMDemod *msg = MsgConfigureAMDemod::create(settings, force);
m_inputMessageQueue.push(msg);
qDebug("AMDemod::webapiSettingsPutPatch: forward to GUI: %p", m_guiMessageQueue);
if (m_guiMessageQueue) // forward to GUI if any
{
MsgConfigureAMDemod *msgToGUI = MsgConfigureAMDemod::create(settings, force);
m_guiMessageQueue->push(msgToGUI);
}
webapiFormatChannelSettings(response, settings);
return 200;
}
int AMDemod::webapiReportGet(
@ -718,8 +721,8 @@ void AMDemod::webapiFormatChannelSettings(SWGSDRangel::SWGChannelSettings& respo
}
response.getAmDemodSettings()->setPll(settings.m_pll ? 1 : 0);
response.getAmDemodSettings()->setSyncAmOperation((int) m_settings.m_syncAMOperation);
response.getAmDemodSettings()->setStreamIndex(m_settings.m_streamIndex);
response.getAmDemodSettings()->setSyncAmOperation((int) settings.m_syncAMOperation);
response.getAmDemodSettings()->setStreamIndex(settings.m_streamIndex);
response.getAmDemodSettings()->setUseReverseApi(settings.m_useReverseAPI ? 1 : 0);
if (response.getAmDemodSettings()->getReverseApiAddress()) {

View File

@ -134,6 +134,15 @@ public:
SWGSDRangel::SWGChannelReport& response,
QString& errorMessage);
static void webapiFormatChannelSettings(
SWGSDRangel::SWGChannelSettings& response,
const AMDemodSettings& settings);
static void webapiUpdateChannelSettings(
AMDemodSettings& settings,
const QStringList& channelSettingsKeys,
SWGSDRangel::SWGChannelSettings& response);
uint32_t getAudioSampleRate() const { return m_audioSampleRate; }
double getMagSq() const { return m_magsq; }
bool getSquelchOpen() const { return m_squelchOpen; }
@ -230,7 +239,6 @@ private:
void applyChannelSettings(int inputSampleRate, int inputFrequencyOffset, bool force = false);
void applySettings(const AMDemodSettings& settings, bool force = false);
void applyAudioSampleRate(int sampleRate);
void webapiFormatChannelSettings(SWGSDRangel::SWGChannelSettings& response, const AMDemodSettings& settings);
void webapiFormatChannelReport(SWGSDRangel::SWGChannelReport& response);
void webapiReverseSendSettings(QList<QString>& channelSettingsKeys, const AMDemodSettings& settings, bool force);

View File

@ -5,6 +5,7 @@
#include "amdemodgui.h"
#endif
#include "amdemod.h"
#include "amdemodwebapiadapter.h"
#include "amdemodplugin.h"
const PluginDescriptor AMDemodPlugin::m_pluginDescriptor = {
@ -59,3 +60,7 @@ ChannelAPI* AMDemodPlugin::createRxChannelCS(DeviceAPI *deviceAPI)
return new AMDemod(deviceAPI);
}
ChannelAPI* AMDemodPlugin::createChannelWebAPIAdapter() const
{
return new AMDemodWebAPIAdapter();
}

View File

@ -38,6 +38,7 @@ public:
virtual PluginInstanceGUI* createRxChannelGUI(DeviceUISet *deviceUISet, BasebandSampleSink *rxChannel);
virtual BasebandSampleSink* createRxChannelBS(DeviceAPI *deviceAPI);
virtual ChannelAPI* createRxChannelCS(DeviceAPI *deviceAPI);
virtual ChannelAPI* createChannelWebAPIAdapter() const;
private:
static const PluginDescriptor m_pluginDescriptor;

View File

@ -0,0 +1,51 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2019 Edouard Griffiths, F4EXB. //
// //
// 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 //
// the Free Software Foundation as version 3 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License V3 for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include "SWGChannelSettings.h"
#include "amdemod.h"
#include "amdemodwebapiadapter.h"
AMDemodWebAPIAdapter::AMDemodWebAPIAdapter() :
ChannelAPI(AMDemod::m_channelIdURI, ChannelAPI::StreamSingleSink)
{}
AMDemodWebAPIAdapter::~AMDemodWebAPIAdapter()
{}
int AMDemodWebAPIAdapter::webapiSettingsGet(
SWGSDRangel::SWGChannelSettings& response,
QString& errorMessage)
{
(void) errorMessage;
response.setAmDemodSettings(new SWGSDRangel::SWGAMDemodSettings());
response.getAmDemodSettings()->init();
AMDemod::webapiFormatChannelSettings(response, m_settings);
return 200;
}
int AMDemodWebAPIAdapter::webapiSettingsPutPatch(
bool force,
const QStringList& channelSettingsKeys,
SWGSDRangel::SWGChannelSettings& response,
QString& errorMessage)
{
(void) errorMessage;
AMDemod::webapiUpdateChannelSettings(m_settings, channelSettingsKeys, response);
return 200;
}

View File

@ -0,0 +1,65 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2019 Edouard Griffiths, F4EXB. //
// //
// 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 //
// the Free Software Foundation as version 3 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License V3 for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDE_AMDEMOD_WEBAPIADAPTER_H
#define INCLUDE_AMDEMOD_WEBAPIADAPTER_H
#include "channel/channelapi.h"
#include "amdemodsettings.h"
/**
* Standalone API adapter only for the settings
*/
class AMDemodWebAPIAdapter : public ChannelAPI {
public:
AMDemodWebAPIAdapter();
virtual ~AMDemodWebAPIAdapter();
// unused pure virtual methods
virtual void destroy() {}
virtual void getIdentifier(QString& id) {}
virtual void getTitle(QString& title) {}
virtual qint64 getCenterFrequency() const { return 0; }
virtual int getNbSinkStreams() const { return 1; }
virtual int getNbSourceStreams() const { return 0; }
virtual qint64 getStreamCenterFrequency(int streamIndex, bool sinkElseSource) const
{
(void) streamIndex;
(void) sinkElseSource;
return 0;
}
// virtual methods actually implemented
virtual QByteArray serialize() const { return m_settings.serialize(); }
virtual bool deserialize(const QByteArray& data) { m_settings.deserialize(data); }
virtual int webapiSettingsGet(
SWGSDRangel::SWGChannelSettings& response,
QString& errorMessage);
virtual int webapiSettingsPutPatch(
bool force,
const QStringList& channelSettingsKeys,
SWGSDRangel::SWGChannelSettings& response,
QString& errorMessage);
private:
AMDemodSettings m_settings;
};
#endif // INCLUDE_AMDEMOD_WEBAPIADAPTER_H

View File

@ -56,6 +56,9 @@ public:
virtual QByteArray serialize() const = 0;
virtual bool deserialize(const QByteArray& data) = 0;
/**
* API adapter for the channel settings GET requests
*/
virtual int webapiSettingsGet(
SWGSDRangel::SWGChannelSettings& response,
QString& errorMessage)
@ -64,6 +67,9 @@ public:
errorMessage = "Not implemented"; return 501;
}
/**
* API adapter for the channel settings PUT and PATCH requests
*/
virtual int webapiSettingsPutPatch(
bool force,
const QStringList& channelSettingsKeys,
@ -76,6 +82,9 @@ public:
errorMessage = "Not implemented"; return 501;
}
/**
* API adapter for the channel report GET requests
*/
virtual int webapiReportGet(
SWGSDRangel::SWGChannelReport& response,
QString& errorMessage)

View File

@ -134,6 +134,13 @@ public:
return nullptr;
}
// any channel
virtual ChannelAPI* createChannelWebAPIAdapter() const
{
return nullptr;
}
// device source plugins only
virtual SamplingDevices enumSampleSources() { return SamplingDevices(); }

View File

@ -200,7 +200,7 @@ void PluginManager::listTxChannels(QList<QString>& list)
{
list.clear();
for(PluginAPI::ChannelRegistrations::iterator it = m_txChannelRegistrations.begin(); it != m_txChannelRegistrations.end(); ++it)
for (PluginAPI::ChannelRegistrations::iterator it = m_txChannelRegistrations.begin(); it != m_txChannelRegistrations.end(); ++it)
{
const PluginDescriptor& pluginDescipror = it->m_plugin->getPluginDescriptor();
list.append(pluginDescipror.displayedName);
@ -211,7 +211,7 @@ void PluginManager::listRxChannels(QList<QString>& list)
{
list.clear();
for(PluginAPI::ChannelRegistrations::iterator it = m_rxChannelRegistrations.begin(); it != m_rxChannelRegistrations.end(); ++it)
for (PluginAPI::ChannelRegistrations::iterator it = m_rxChannelRegistrations.begin(); it != m_rxChannelRegistrations.end(); ++it)
{
const PluginDescriptor& pluginDesciptor = it->m_plugin->getPluginDescriptor();
list.append(pluginDesciptor.displayedName);
@ -237,3 +237,22 @@ void PluginManager::createTxChannelInstance(int channelPluginIndex, DeviceUISet
pluginInterface->createTxChannelGUI(deviceUISet, txChannel);
}
}
const PluginInterface *PluginManager::getChannelPluginInterface(const QString& channelIdURI) const
{
for(PluginAPI::ChannelRegistrations::const_iterator it = m_rxChannelRegistrations.begin(); it != m_rxChannelRegistrations.end(); ++it)
{
if (it->m_channelIdURI == channelIdURI) {
return it->m_plugin;
}
}
for(PluginAPI::ChannelRegistrations::const_iterator it = m_txChannelRegistrations.begin(); it != m_txChannelRegistrations.end(); ++it)
{
if (it->m_channelIdURI == channelIdURI) {
return it->m_plugin;
}
}
return nullptr;
}

View File

@ -80,6 +80,8 @@ public:
void createTxChannelInstance(int channelPluginIndex, DeviceUISet *deviceUISet, DeviceAPI *deviceAPI);
void listTxChannels(QList<QString>& list);
const PluginInterface *getChannelPluginInterface(const QString& channelIdURI) const;
static const QString& getFileInputDeviceId() { return m_fileInputDeviceTypeID; }
static const QString& getFileSinkDeviceId() { return m_fileSinkDeviceTypeID; }
static const QString& getTestMIMODeviceId() { return m_testMIMODeviceTypeID; }

View File

@ -16,8 +16,17 @@
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include "plugin/pluginmanager.h"
#include "channel/channelapi.h"
#include "webapiadapterbase.h"
WebAPIAdapterBase::WebAPIAdapterBase()
{}
WebAPIAdapterBase::~WebAPIAdapterBase()
{}
void WebAPIAdapterBase::webapiFormatPreferences(
SWGSDRangel::SWGPreferences *apiPreferences,
const Preferences& preferences
@ -57,6 +66,18 @@ void WebAPIAdapterBase::webapiFormatPreset(
QList<SWGSDRangel::SWGChannelConfig *> *swgChannelConfigs = apiPreset->getChannelConfigs();
swgChannelConfigs->append(new SWGSDRangel::SWGChannelConfig);
swgChannelConfigs->back()->setChannelIdUri(new QString(channelConfig.m_channelIdURI));
const QByteArray& channelSettings = channelConfig.m_config;
SWGSDRangel::SWGChannelSettings *swgChannelSettings = swgChannelConfigs->back()->getConfig();
swgChannelSettings->init();
const PluginInterface *pluginInterface = m_pluginManager->getChannelPluginInterface(channelConfig.m_channelIdURI);
if (pluginInterface) // TODO: optimize by caching web API adapters
{
ChannelAPI *channelWebAPIAdapter = pluginInterface->createChannelWebAPIAdapter();
channelWebAPIAdapter->deserialize(channelSettings);
QString errorMessage;
channelWebAPIAdapter->webapiSettingsGet(*swgChannelSettings, errorMessage);
}
}
int nbDevices = preset.getDeviceCount();
@ -68,6 +89,9 @@ void WebAPIAdapterBase::webapiFormatPreset(
swgdeviceConfigs->back()->setDeviceId(new QString(deviceConfig.m_deviceId));
swgdeviceConfigs->back()->setDeviceSerial(new QString(deviceConfig.m_deviceSerial));
swgdeviceConfigs->back()->setDeviceSequence(deviceConfig.m_deviceSequence);
const QByteArray& deviceSettings = deviceConfig.m_config;
SWGSDRangel::SWGDeviceSettings *swgDeviceSettings = swgdeviceConfigs->back()->getConfig();
swgDeviceSettings->init();
}
}

View File

@ -27,17 +27,24 @@
#include "settings/preset.h"
#include "commands/command.h"
class PluginManager;
/**
* Adapter between API and objects in sdrbase library
*/
class SDRBASE_API WebAPIAdapterBase
{
public:
WebAPIAdapterBase();
~WebAPIAdapterBase();
void setPluginManager(const PluginManager *pluginManager) { m_pluginManager = pluginManager; }
static void webapiFormatPreferences(
SWGSDRangel::SWGPreferences *apiPreferences,
const Preferences& preferences
);
static void webapiFormatPreset(
void webapiFormatPreset(
SWGSDRangel::SWGPreset *apiPreset,
const Preset& preset
);
@ -45,6 +52,9 @@ public:
SWGSDRangel::SWGCommand *apiCommand,
const Command& command
);
private:
const PluginManager *m_pluginManager;
};
#endif // SDRBASE_WEBAPI_WEBAPIADAPTERBASE_H_

View File

@ -81,6 +81,7 @@ public:
const QTimer& getMasterTimer() const { return m_masterTimer; }
const MainSettings& getMainSettings() const { return m_settings; }
const PluginManager *getPluginManager() const { return m_pluginManager; }
void commandKeysConnect(QObject *object, const char *slot);
void commandKeysDisconnect(QObject *object, const char *slot);

View File

@ -121,10 +121,12 @@ int WebAPIAdapterGUI::instanceConfigGet(
SWGSDRangel::SWGErrorResponse& error)
{
response.init();
WebAPIAdapterBase webAPIAdapterBase;
webAPIAdapterBase.setPluginManager(m_mainWindow.getPluginManager());
SWGSDRangel::SWGPreferences *preferences = response.getPreferences();
WebAPIAdapterBase::webapiFormatPreferences(preferences, m_mainWindow.getMainSettings().getPreferences());
SWGSDRangel::SWGPreset *workingPreset = response.getWorkingPreset();
WebAPIAdapterBase::webapiFormatPreset(workingPreset, m_mainWindow.getMainSettings().getWorkingPresetConst());
webAPIAdapterBase.webapiFormatPreset(workingPreset, m_mainWindow.getMainSettings().getWorkingPresetConst());
int nbPresets = m_mainWindow.m_settings.getPresetCount();
QList<SWGSDRangel::SWGPreset*> *swgPresets = response.getPresets();
@ -133,7 +135,7 @@ int WebAPIAdapterGUI::instanceConfigGet(
{
const Preset *preset = m_mainWindow.m_settings.getPreset(i);
swgPresets->append(new SWGSDRangel::SWGPreset);
WebAPIAdapterBase::webapiFormatPreset(swgPresets->back(), *preset);
webAPIAdapterBase.webapiFormatPreset(swgPresets->back(), *preset);
}
int nbCommands = m_mainWindow.m_settings.getCommandCount();

View File

@ -57,6 +57,7 @@ public:
const QTimer& getMasterTimer() const { return m_masterTimer; }
const MainSettings& getMainSettings() const { return m_settings; }
const PluginManager *getPluginManager() const { return m_pluginManager; }
void addSourceDevice();
void addSinkDevice();

View File

@ -120,10 +120,12 @@ int WebAPIAdapterSrv::instanceConfigGet(
SWGSDRangel::SWGErrorResponse& error)
{
response.init();
WebAPIAdapterBase webAPIAdapterBase;
webAPIAdapterBase.setPluginManager(m_mainCore.getPluginManager());
SWGSDRangel::SWGPreferences *preferences = response.getPreferences();
WebAPIAdapterBase::webapiFormatPreferences(preferences, m_mainCore.getMainSettings().getPreferences());
SWGSDRangel::SWGPreset *workingPreset = response.getWorkingPreset();
WebAPIAdapterBase::webapiFormatPreset(workingPreset, m_mainCore.getMainSettings().getWorkingPresetConst());
webAPIAdapterBase.webapiFormatPreset(workingPreset, m_mainCore.getMainSettings().getWorkingPresetConst());
int nbPresets = m_mainCore.m_settings.getPresetCount();
QList<SWGSDRangel::SWGPreset*> *swgPresets = response.getPresets();
@ -132,7 +134,7 @@ int WebAPIAdapterSrv::instanceConfigGet(
{
const Preset *preset = m_mainCore.m_settings.getPreset(i);
swgPresets->append(new SWGSDRangel::SWGPreset);
WebAPIAdapterBase::webapiFormatPreset(swgPresets->back(), *preset);
webAPIAdapterBase.webapiFormatPreset(swgPresets->back(), *preset);
}
int nbCommands = m_mainCore.m_settings.getCommandCount();