From 62ef01c21c726b98be088acd6ab2495c116cbf94 Mon Sep 17 00:00:00 2001 From: f4exb Date: Thu, 1 Aug 2019 02:16:56 +0200 Subject: [PATCH] REST API: config: GET (1) preferences and partial presets --- sdrbase/CMakeLists.txt | 2 + sdrbase/settings/mainsettings.h | 3 ++ sdrbase/settings/preferences.h | 2 + sdrbase/settings/preset.cpp | 19 +++++++ sdrbase/settings/preset.h | 20 +++++++ sdrbase/webapi/webapiadapterbase.cpp | 54 +++++++++++++++++++ sdrbase/webapi/webapiadapterbase.h | 44 +++++++++++++++ sdrbase/webapi/webapiadapterinterface.cpp | 1 + sdrbase/webapi/webapiadapterinterface.h | 16 ++++++ sdrbase/webapi/webapirequestmapper.cpp | 23 ++++++++ sdrbase/webapi/webapirequestmapper.h | 1 + sdrgui/webapi/webapiadaptergui.cpp | 25 +++++++++ sdrgui/webapi/webapiadaptergui.h | 4 ++ sdrsrv/webapi/webapiadaptersrv.cpp | 25 +++++++++ sdrsrv/webapi/webapiadaptersrv.h | 4 ++ .../sdrangel/api/swagger/include/Preset.yaml | 2 +- 16 files changed, 244 insertions(+), 1 deletion(-) create mode 100644 sdrbase/webapi/webapiadapterbase.cpp create mode 100644 sdrbase/webapi/webapiadapterbase.h diff --git a/sdrbase/CMakeLists.txt b/sdrbase/CMakeLists.txt index 64a04bb18..a060e46b2 100644 --- a/sdrbase/CMakeLists.txt +++ b/sdrbase/CMakeLists.txt @@ -132,6 +132,7 @@ set(sdrbase_SOURCES plugin/pluginapi.cpp plugin/pluginmanager.cpp + webapi/webapiadapterbase.cpp webapi/webapiadapterinterface.cpp webapi/webapirequestmapper.cpp webapi/webapiserver.cpp @@ -266,6 +267,7 @@ set(sdrbase_HEADERS util/uid.h util/timeutil.h + webapi/webapiadapterbase.h webapi/webapiadapterinterface.h webapi/webapirequestmapper.h webapi/webapiserver diff --git a/sdrbase/settings/mainsettings.h b/sdrbase/settings/mainsettings.h index c49bbda3d..fb0ba306b 100644 --- a/sdrbase/settings/mainsettings.h +++ b/sdrbase/settings/mainsettings.h @@ -23,6 +23,8 @@ public: QString getFileLocation() const; int getFileFormat() const; //!< see QSettings::Format for the values + const Preferences& getPreferences() const { return m_preferences; } + Preset* newPreset(const QString& group, const QString& description); void deletePreset(const Preset* preset); int getPresetCount() const { return m_presets.count(); } @@ -41,6 +43,7 @@ public: void renameCommandGroup(const QString& oldGroupName, const QString& newGroupName); void deleteCommandGroup(const QString& groupName); + const Preset& getWorkingPresetConst() const { return m_workingPreset; } Preset* getWorkingPreset() { return &m_workingPreset; } int getSourceIndex() const { return m_preferences.getSourceIndex(); } void setSourceIndex(int value) { m_preferences.setSourceIndex(value); } diff --git a/sdrbase/settings/preferences.h b/sdrbase/settings/preferences.h index cf8d72b96..31a04b2e0 100644 --- a/sdrbase/settings/preferences.h +++ b/sdrbase/settings/preferences.h @@ -37,6 +37,8 @@ public: bool getUseLogFile() const { return m_useLogFile; } const QString& getLogFileName() const { return m_logFileName; } + friend class WebAPIAdapterBase; + protected: QString m_sourceDevice; //!< Identification of the source used in R0 tab (GUI flavor) at startup int m_sourceIndex; //!< Index of the source used in R0 tab (GUI flavor) at startup diff --git a/sdrbase/settings/preset.cpp b/sdrbase/settings/preset.cpp index d5f99a2ff..d37a38eb7 100644 --- a/sdrbase/settings/preset.cpp +++ b/sdrbase/settings/preset.cpp @@ -1,3 +1,22 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2015-2019 Edouard Griffiths, F4EXB. // +// // +// Swagger server adapter interface // +// // +// 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 . // +/////////////////////////////////////////////////////////////////////////////////// + #include "util/simpleserializer.h" #include "settings/preset.h" diff --git a/sdrbase/settings/preset.h b/sdrbase/settings/preset.h index c10b00830..2d50f5670 100644 --- a/sdrbase/settings/preset.h +++ b/sdrbase/settings/preset.h @@ -1,3 +1,21 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2015-2019 Edouard Griffiths, F4EXB. // +// // +// Swagger server adapter interface // +// // +// 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 . // +/////////////////////////////////////////////////////////////////////////////////// #ifndef INCLUDE_PRESET_H #define INCLUDE_PRESET_H @@ -98,6 +116,8 @@ public: } } + friend class WebAPIAdapterBase; + protected: bool m_sourcePreset; diff --git a/sdrbase/webapi/webapiadapterbase.cpp b/sdrbase/webapi/webapiadapterbase.cpp new file mode 100644 index 000000000..af844ac12 --- /dev/null +++ b/sdrbase/webapi/webapiadapterbase.cpp @@ -0,0 +1,54 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2019 Edouard Griffiths, F4EXB. // +// // +// Swagger server adapter interface // +// // +// 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 . // +/////////////////////////////////////////////////////////////////////////////////// +#include "webapiadapterbase.h" + +void WebAPIAdapterBase::webapiFormatPreferences( + SWGSDRangel::SWGPreferences *apiPreferences, + const Preferences& preferences +) +{ + apiPreferences->init(); + apiPreferences->setSourceDevice(new QString(preferences.m_sourceDevice)); + apiPreferences->setSourceIndex(preferences.m_sourceIndex); + apiPreferences->setAudioType(new QString(preferences.m_audioType)); + apiPreferences->setAudioDevice(new QString(preferences.m_audioDevice)); + apiPreferences->setLatitude(preferences.m_latitude); + apiPreferences->setLongitude(preferences.m_longitude); + apiPreferences->setConsoleMinLogLevel((int) preferences.m_consoleMinLogLevel); + apiPreferences->setUseLogFile(preferences.m_useLogFile ? 1 : 0); + apiPreferences->setLogFileName(new QString(preferences.m_logFileName)); + apiPreferences->setFileMinLogLevel((int) preferences.m_fileMinLogLevel); +} + +void WebAPIAdapterBase::webapiFormatPreset( + SWGSDRangel::SWGPreset *apiPreset, + const Preset& preset +) +{ + apiPreset->init(); + apiPreset->setSourcePreset(preset.m_sourcePreset ? 1 : 0); + apiPreset->setGroup(new QString(preset.m_group)); + apiPreset->setDescription(new QString(preset.m_description)); + apiPreset->setCenterFrequency(preset.m_centerFrequency); + apiPreset->getMSpectrumConfig()->init(); // TODO when spectrum config is extracted to sdrbase + apiPreset->setDcOffsetCorrection(preset.m_dcOffsetCorrection ? 1 : 0); + apiPreset->setIqImbalanceCorrection(preset.m_iqImbalanceCorrection ? 1 : 0); + // TODO: channel configs + // TODO: device configs +} diff --git a/sdrbase/webapi/webapiadapterbase.h b/sdrbase/webapi/webapiadapterbase.h new file mode 100644 index 000000000..778246e3b --- /dev/null +++ b/sdrbase/webapi/webapiadapterbase.h @@ -0,0 +1,44 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2019 Edouard Griffiths, F4EXB. // +// // +// Swagger server adapter interface // +// // +// 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 . // +/////////////////////////////////////////////////////////////////////////////////// +#ifndef SDRBASE_WEBAPI_WEBAPIADAPTERBASE_H_ +#define SDRBASE_WEBAPI_WEBAPIADAPTERBASE_H_ + +#include "export.h" +#include "SWGPreferences.h" +#include "SWGPreset.h" +#include "settings/preferences.h" +#include "settings/preset.h" + +/** + * Adapter between API and objects in sdrbase library + */ +class SDRBASE_API WebAPIAdapterBase +{ +public: + static void webapiFormatPreferences( + SWGSDRangel::SWGPreferences *apiPreferences, + const Preferences& preferences + ); + static void webapiFormatPreset( + SWGSDRangel::SWGPreset *apiPreset, + const Preset& preset + ); +}; + +#endif // SDRBASE_WEBAPI_WEBAPIADAPTERBASE_H_ \ No newline at end of file diff --git a/sdrbase/webapi/webapiadapterinterface.cpp b/sdrbase/webapi/webapiadapterinterface.cpp index c0e28a3ce..70bceff39 100644 --- a/sdrbase/webapi/webapiadapterinterface.cpp +++ b/sdrbase/webapi/webapiadapterinterface.cpp @@ -20,6 +20,7 @@ #include "webapiadapterinterface.h" QString WebAPIAdapterInterface::instanceSummaryURL = "/sdrangel"; +QString WebAPIAdapterInterface::instanceConfigURL = "/sdrangel/config"; QString WebAPIAdapterInterface::instanceDevicesURL = "/sdrangel/devices"; QString WebAPIAdapterInterface::instanceChannelsURL = "/sdrangel/channels"; QString WebAPIAdapterInterface::instanceLoggingURL = "/sdrangel/logging"; diff --git a/sdrbase/webapi/webapiadapterinterface.h b/sdrbase/webapi/webapiadapterinterface.h index dce8b0712..d56d15ad8 100644 --- a/sdrbase/webapi/webapiadapterinterface.h +++ b/sdrbase/webapi/webapiadapterinterface.h @@ -30,6 +30,7 @@ namespace SWGSDRangel { class SWGInstanceSummaryResponse; + class SWGInstanceConfigResponse; class SWGInstanceDevicesResponse; class SWGInstanceChannelsResponse; class SWGLoggingInfo; @@ -89,6 +90,20 @@ public: return 501; } + /** + * Handler of /sdrangel/config (GET) swagger/sdrangel/code/html2/index.html#api-Default-instanceSummary + * returns the Http status code (default 501: not implemented) + */ + virtual int instanceConfigGet( + SWGSDRangel::SWGInstanceConfigResponse& response, + SWGSDRangel::SWGErrorResponse& error) + { + (void) response; + error.init(); + *error.getMessage() = QString("Function not implemented"); + return 501; + } + /** * Handler of /sdrangel/devices (GET) swagger/sdrangel/code/html2/index.html#api-Default-instanceDevices * returns the Http status code (default 501: not implemented) @@ -770,6 +785,7 @@ public: } static QString instanceSummaryURL; + static QString instanceConfigURL; static QString instanceDevicesURL; static QString instanceChannelsURL; static QString instanceLoggingURL; diff --git a/sdrbase/webapi/webapirequestmapper.cpp b/sdrbase/webapi/webapirequestmapper.cpp index 0871c0992..668e44738 100644 --- a/sdrbase/webapi/webapirequestmapper.cpp +++ b/sdrbase/webapi/webapirequestmapper.cpp @@ -26,6 +26,7 @@ #include "httpdocrootsettings.h" #include "webapirequestmapper.h" #include "SWGInstanceSummaryResponse.h" +#include "SWGInstanceConfigResponse.h" #include "SWGInstanceDevicesResponse.h" #include "SWGInstanceChannelsResponse.h" #include "SWGAudioDevices.h" @@ -96,6 +97,8 @@ void WebAPIRequestMapper::service(qtwebapp::HttpRequest& request, qtwebapp::Http if (path == WebAPIAdapterInterface::instanceSummaryURL) { instanceSummaryService(request, response); + } else if (path == WebAPIAdapterInterface::instanceConfigURL) { + instanceConfigService(request, response); } else if (path == WebAPIAdapterInterface::instanceDevicesURL) { instanceDevicesService(request, response); } else if (path == WebAPIAdapterInterface::instanceChannelsURL) { @@ -212,6 +215,26 @@ void WebAPIRequestMapper::instanceSummaryService(qtwebapp::HttpRequest& request, } } +void WebAPIRequestMapper::instanceConfigService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response) +{ + SWGSDRangel::SWGErrorResponse errorResponse; + response.setHeader("Content-Type", "application/json"); + response.setHeader("Access-Control-Allow-Origin", "*"); + + if (request.getMethod() == "GET") + { + SWGSDRangel::SWGInstanceConfigResponse normalResponse; + int status = m_adapter->instanceConfigGet(normalResponse, errorResponse); + response.setStatus(status); + + if (status/100 == 2) { + response.write(normalResponse.asJson().toUtf8()); + } else { + response.write(errorResponse.asJson().toUtf8()); + } + } +} + void WebAPIRequestMapper::instanceDevicesService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response) { SWGSDRangel::SWGInstanceDevicesResponse normalResponse; diff --git a/sdrbase/webapi/webapirequestmapper.h b/sdrbase/webapi/webapirequestmapper.h index 489a862b9..6229728ed 100644 --- a/sdrbase/webapi/webapirequestmapper.h +++ b/sdrbase/webapi/webapirequestmapper.h @@ -49,6 +49,7 @@ private: qtwebapp::StaticFileController *m_staticFileController; void instanceSummaryService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); + void instanceConfigService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); void instanceDevicesService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); void instanceChannelsService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); void instanceLoggingService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); diff --git a/sdrgui/webapi/webapiadaptergui.cpp b/sdrgui/webapi/webapiadaptergui.cpp index c066eff78..9a09cde4c 100644 --- a/sdrgui/webapi/webapiadaptergui.cpp +++ b/sdrgui/webapi/webapiadaptergui.cpp @@ -35,8 +35,10 @@ #include "plugin/pluginapi.h" #include "plugin/pluginmanager.h" #include "channel/channelapi.h" +#include "webapi/webapiadapterbase.h" #include "SWGInstanceSummaryResponse.h" +#include "SWGInstanceConfigResponse.h" #include "SWGInstanceDevicesResponse.h" #include "SWGInstanceChannelsResponse.h" #include "SWGDeviceListItem.h" @@ -114,6 +116,29 @@ int WebAPIAdapterGUI::instanceDelete( return 400; } +int WebAPIAdapterGUI::instanceConfigGet( + SWGSDRangel::SWGInstanceConfigResponse& response, + SWGSDRangel::SWGErrorResponse& error) +{ + response.init(); + SWGSDRangel::SWGPreferences *preferences = response.getPreferences(); + WebAPIAdapterBase::webapiFormatPreferences(preferences, m_mainWindow.getMainSettings().getPreferences()); + SWGSDRangel::SWGPreset *workingPreset = response.getWorkingPreset(); + WebAPIAdapterBase::webapiFormatPreset(workingPreset, m_mainWindow.getMainSettings().getWorkingPresetConst()); + + int nbPresets = m_mainWindow.m_settings.getPresetCount(); + QList *swgPresets = response.getPresets(); + + for (int i = 0; i < nbPresets; i++) + { + const Preset *preset = m_mainWindow.m_settings.getPreset(i); + swgPresets->append(new SWGSDRangel::SWGPreset); + WebAPIAdapterBase::webapiFormatPreset(swgPresets->back(), *preset); + } + + return 200; +} + int WebAPIAdapterGUI::instanceDevices( int direction, SWGSDRangel::SWGInstanceDevicesResponse& response, diff --git a/sdrgui/webapi/webapiadaptergui.h b/sdrgui/webapi/webapiadaptergui.h index 4446541b7..32c68fb21 100644 --- a/sdrgui/webapi/webapiadaptergui.h +++ b/sdrgui/webapi/webapiadaptergui.h @@ -41,6 +41,10 @@ public: SWGSDRangel::SWGSuccessResponse& response, SWGSDRangel::SWGErrorResponse& error); + virtual int instanceConfigGet( + SWGSDRangel::SWGInstanceConfigResponse& response, + SWGSDRangel::SWGErrorResponse& error); + virtual int instanceDevices( int direction, SWGSDRangel::SWGInstanceDevicesResponse& response, diff --git a/sdrsrv/webapi/webapiadaptersrv.cpp b/sdrsrv/webapi/webapiadaptersrv.cpp index 22869ba21..8e63bee04 100644 --- a/sdrsrv/webapi/webapiadaptersrv.cpp +++ b/sdrsrv/webapi/webapiadaptersrv.cpp @@ -23,6 +23,7 @@ #include #include "SWGInstanceSummaryResponse.h" +#include "SWGInstanceConfigResponse.h" #include "SWGInstanceDevicesResponse.h" #include "SWGInstanceChannelsResponse.h" #include "SWGLoggingInfo.h" @@ -56,6 +57,7 @@ #include "channel/channelapi.h" #include "plugin/pluginapi.h" #include "plugin/pluginmanager.h" +#include "webapi/webapiadapterbase.h" #include "webapiadaptersrv.h" WebAPIAdapterSrv::WebAPIAdapterSrv(MainCore& mainCore) : @@ -113,6 +115,29 @@ int WebAPIAdapterSrv::instanceDelete( return 202; } +int WebAPIAdapterSrv::instanceConfigGet( + SWGSDRangel::SWGInstanceConfigResponse& response, + SWGSDRangel::SWGErrorResponse& error) +{ + response.init(); + SWGSDRangel::SWGPreferences *preferences = response.getPreferences(); + WebAPIAdapterBase::webapiFormatPreferences(preferences, m_mainCore.getMainSettings().getPreferences()); + SWGSDRangel::SWGPreset *workingPreset = response.getWorkingPreset(); + WebAPIAdapterBase::webapiFormatPreset(workingPreset, m_mainCore.getMainSettings().getWorkingPresetConst()); + + int nbPresets = m_mainCore.m_settings.getPresetCount(); + QList *swgPresets = response.getPresets(); + + for (int i = 0; i < nbPresets; i++) + { + const Preset *preset = m_mainCore.m_settings.getPreset(i); + swgPresets->append(new SWGSDRangel::SWGPreset); + WebAPIAdapterBase::webapiFormatPreset(swgPresets->back(), *preset); + } + + return 200; +} + int WebAPIAdapterSrv::instanceDevices( int direction, SWGSDRangel::SWGInstanceDevicesResponse& response, diff --git a/sdrsrv/webapi/webapiadaptersrv.h b/sdrsrv/webapi/webapiadaptersrv.h index 1ceea4590..ce340b55f 100644 --- a/sdrsrv/webapi/webapiadaptersrv.h +++ b/sdrsrv/webapi/webapiadaptersrv.h @@ -41,6 +41,10 @@ public: SWGSDRangel::SWGSuccessResponse& response, SWGSDRangel::SWGErrorResponse& error); + virtual int instanceConfigGet( + SWGSDRangel::SWGInstanceConfigResponse& response, + SWGSDRangel::SWGErrorResponse& error); + virtual int instanceDevices( int direction, SWGSDRangel::SWGInstanceDevicesResponse& response, diff --git a/swagger/sdrangel/api/swagger/include/Preset.yaml b/swagger/sdrangel/api/swagger/include/Preset.yaml index 7a99bcd1d..dce204508 100644 --- a/swagger/sdrangel/api/swagger/include/Preset.yaml +++ b/swagger/sdrangel/api/swagger/include/Preset.yaml @@ -35,7 +35,7 @@ Preset: centerFrequency: type: integer format: int64 - m_spectrumConfig: + spectrumConfig: $ref: "http://localhost:8081/api/swagger/include/GLSpectrum.yaml#/GLSpectrum" dcOffsetCorrection: description: boolean