1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-09-28 07:46:37 -04:00

Web API: /sdrangel/preset (PATCH) implementation

This commit is contained in:
f4exb 2017-11-25 16:08:18 +01:00
parent 2b41601b83
commit 57bda99c2b
11 changed files with 169 additions and 16 deletions

View File

@ -107,3 +107,20 @@ void MainSettings::sortPresets()
{ {
qSort(m_presets.begin(), m_presets.end(), Preset::presetCompare); qSort(m_presets.begin(), m_presets.end(), Preset::presetCompare);
} }
const Preset* MainSettings::getPreset(const QString& groupName, quint64 centerFrequency, const QString& description) const
{
int nbPresets = getPresetCount();
for (int i = 0; i < nbPresets; i++)
{
if ((getPreset(i)->getGroup() == groupName) &&
(getPreset(i)->getCenterFrequency() == centerFrequency) &&
(getPreset(i)->getDescription() == description))
{
return getPreset(i);
}
}
return 0;
}

View File

@ -20,6 +20,7 @@ public:
void deletePreset(const Preset* preset); void deletePreset(const Preset* preset);
int getPresetCount() const { return m_presets.count(); } int getPresetCount() const { return m_presets.count(); }
const Preset* getPreset(int index) const { return m_presets[index]; } const Preset* getPreset(int index) const { return m_presets[index]; }
const Preset* getPreset(const QString& groupName, quint64 centerFrequency, const QString& description) const;
void sortPresets(); void sortPresets();
Preset* getWorkingPreset() { return &m_workingPreset; } Preset* getWorkingPreset() { return &m_workingPreset; }

View File

@ -31,8 +31,10 @@ namespace Swagger
class SWGAudioDevicesSelect; class SWGAudioDevicesSelect;
class SWGLocationInformation; class SWGLocationInformation;
class SWGDVSeralDevices; class SWGDVSeralDevices;
class SWGErrorResponse;
class SWGPresets; class SWGPresets;
class SWGPresetTransfer;
class SWGPresetIdentifier;
class SWGErrorResponse;
} }
class WebAPIAdapterInterface class WebAPIAdapterInterface
@ -134,7 +136,7 @@ public:
{ return 501; } { return 501; }
/** /**
* Handler of /sdrangel/location (GET) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels * Handler of /sdrangel/preset (GET) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels
* returns the Http status code (default 501: not implemented) * returns the Http status code (default 501: not implemented)
*/ */
virtual int instancePresetGet( virtual int instancePresetGet(
@ -142,6 +144,16 @@ public:
Swagger::SWGErrorResponse& error __attribute__((unused))) Swagger::SWGErrorResponse& error __attribute__((unused)))
{ return 501; } { return 501; }
/**
* Handler of /sdrangel/preset (PATCH) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels
* returns the Http status code (default 501: not implemented)
*/
virtual int instancePresetPatch(
Swagger::SWGPresetTransfer& query __attribute__((unused)),
Swagger::SWGPresetIdentifier& response __attribute__((unused)),
Swagger::SWGErrorResponse& error __attribute__((unused)))
{ return 501; }
static QString instanceSummaryURL; static QString instanceSummaryURL;
static QString instanceDevicesURL; static QString instanceDevicesURL;
static QString instanceChannelsURL; static QString instanceChannelsURL;

View File

@ -30,6 +30,8 @@
#include "SWGLocationInformation.h" #include "SWGLocationInformation.h"
#include "SWGDVSeralDevices.h" #include "SWGDVSeralDevices.h"
#include "SWGPresets.h" #include "SWGPresets.h"
#include "SWGPresetTransfer.h"
#include "SWGPresetIdentifier.h"
#include "SWGErrorResponse.h" #include "SWGErrorResponse.h"
WebAPIRequestMapper::WebAPIRequestMapper(QObject* parent) : WebAPIRequestMapper::WebAPIRequestMapper(QObject* parent) :
@ -327,11 +329,11 @@ void WebAPIRequestMapper::instanceDVSerialService(qtwebapp::HttpRequest& request
void WebAPIRequestMapper::instancePresetService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response) void WebAPIRequestMapper::instancePresetService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{ {
Swagger::SWGPresets normalResponse;
Swagger::SWGErrorResponse errorResponse; Swagger::SWGErrorResponse errorResponse;
if (request.getMethod() == "GET") if (request.getMethod() == "GET")
{ {
Swagger::SWGPresets normalResponse;
int status = m_adapter->instancePresetGet(normalResponse, errorResponse); int status = m_adapter->instancePresetGet(normalResponse, errorResponse);
response.setStatus(status); response.setStatus(status);
@ -341,6 +343,25 @@ void WebAPIRequestMapper::instancePresetService(qtwebapp::HttpRequest& request,
response.write(errorResponse.asJson().toUtf8()); response.write(errorResponse.asJson().toUtf8());
} }
} }
else if (request.getMethod() == "PATCH")
{
Swagger::SWGPresetTransfer query;
Swagger::SWGPresetIdentifier normalResponse;
QString jsonStr = request.getBody();
if (parseJsonBody(jsonStr, response))
{
query.fromJson(jsonStr);
int status = m_adapter->instancePresetPatch(query, normalResponse, errorResponse);
response.setStatus(status);
if (status == 200) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
}
else else
{ {
response.setStatus(405,"Invalid HTTP method"); response.setStatus(405,"Invalid HTTP method");

View File

@ -62,6 +62,8 @@
#include <string> #include <string>
#include <QDebug> #include <QDebug>
MESSAGE_CLASS_DEFINITION(MainWindow::MsgLoadPreset, Message)
MainWindow *MainWindow::m_instance = 0; MainWindow *MainWindow::m_instance = 0;
MainWindow::MainWindow(qtwebapp::LoggerWithFile *logger, const MainParser& parser, QWidget* parent) : MainWindow::MainWindow(qtwebapp::LoggerWithFile *logger, const MainParser& parser, QWidget* parent) :
@ -654,6 +656,18 @@ void MainWindow::applySettings()
{ {
} }
bool MainWindow::handleMessage(const Message& cmd)
{
if (MsgLoadPreset::match(cmd))
{
MsgLoadPreset& notif = (MsgLoadPreset&) cmd;
loadPresetSettings(notif.getPreset(), notif.getDeviceSetIndex());
return true;
}
return false;
}
void MainWindow::handleMessages() void MainWindow::handleMessages()
{ {
Message* message; Message* message;
@ -661,6 +675,7 @@ void MainWindow::handleMessages()
while ((message = m_inputMessageQueue.pop()) != 0) while ((message = m_inputMessageQueue.pop()) != 0)
{ {
qDebug("MainWindow::handleMessages: message: %s", message->getIdentifier()); qDebug("MainWindow::handleMessages: message: %s", message->getIdentifier());
handleMessage(*message);
delete message; delete message;
} }
} }

View File

@ -23,6 +23,7 @@
#include <QList> #include <QList>
#include "settings/mainsettings.h" #include "settings/mainsettings.h"
#include "util/message.h"
#include "util/messagequeue.h" #include "util/messagequeue.h"
#include "util/export.h" #include "util/export.h"
#include "mainparser.h" #include "mainparser.h"
@ -83,6 +84,29 @@ public:
friend class WebAPIAdapterGUI; friend class WebAPIAdapterGUI;
private: private:
class MsgLoadPreset : public Message {
MESSAGE_CLASS_DECLARATION
public:
const Preset *getPreset() const { return m_preset; }
int getDeviceSetIndex() const { return m_deviceSetIndex; }
static MsgLoadPreset* create(const Preset *preset, int deviceSetIndex)
{
return new MsgLoadPreset(preset, deviceSetIndex);
}
private:
const Preset *m_preset;
int m_deviceSetIndex;
MsgLoadPreset(const Preset *preset, int deviceSetIndex) :
Message(),
m_preset(preset),
m_deviceSetIndex(deviceSetIndex)
{ }
};
enum { enum {
PGroup, PGroup,
PItem PItem
@ -142,6 +166,8 @@ private:
void setLoggingOpions(); void setLoggingOpions();
bool handleMessage(const Message& cmd);
private slots: private slots:
void handleMessages(); void handleMessages();
void updateStatus(); void updateStatus();

View File

@ -45,6 +45,8 @@
#include "SWGPresets.h" #include "SWGPresets.h"
#include "SWGPresetGroup.h" #include "SWGPresetGroup.h"
#include "SWGPresetItem.h" #include "SWGPresetItem.h"
#include "SWGPresetTransfer.h"
#include "SWGPresetIdentifier.h"
#include "SWGErrorResponse.h" #include "SWGErrorResponse.h"
#include "webapiadaptergui.h" #include "webapiadaptergui.h"
@ -430,6 +432,60 @@ int WebAPIAdapterGUI::instancePresetGet(
return 200; return 200;
} }
int WebAPIAdapterGUI::instancePresetPatch(
Swagger::SWGPresetTransfer& query,
Swagger::SWGPresetIdentifier& response,
Swagger::SWGErrorResponse& error)
{
int deviceSetIndex = query.getDeviceSetIndex();
Swagger::SWGPresetIdentifier *presetIdentifier = query.getPreset();
int nbDeviceSets = m_mainWindow.m_deviceUIs.size();
if (deviceSetIndex > nbDeviceSets)
{
*error.getMessage() = QString("There is no device set at index %1. Number of device sets is %2").arg(deviceSetIndex).arg(nbDeviceSets);
return 404;
}
const Preset *selectedPreset = m_mainWindow.m_settings.getPreset(*presetIdentifier->getGroupName(),
presetIdentifier->getCenterFrequency(),
*presetIdentifier->getName());
if (selectedPreset == 0)
{
*error.getMessage() = QString("There is no preset [%1, %2, %3]")
.arg(*presetIdentifier->getGroupName())
.arg(presetIdentifier->getCenterFrequency())
.arg(*presetIdentifier->getName());
return 404;
}
DeviceUISet *deviceUI = m_mainWindow.m_deviceUIs[deviceSetIndex];
if (deviceUI->m_deviceSourceEngine && !selectedPreset->isSourcePreset())
{
*error.getMessage() = QString("Preset type (T) and device set type (Rx) mismatch");
return 404;
}
if (deviceUI->m_deviceSinkEngine && selectedPreset->isSourcePreset())
{
*error.getMessage() = QString("Preset type (R) and device set type (Tx) mismatch");
return 404;
}
MainWindow::MsgLoadPreset *msg = MainWindow::MsgLoadPreset::create(selectedPreset, deviceSetIndex);
m_mainWindow.m_inputMessageQueue.push(msg);
response.init();
response.setCenterFrequency(selectedPreset->getCenterFrequency());
*response.getGroupName() = selectedPreset->getGroup();
*response.getType() = selectedPreset->isSourcePreset() ? "R" : "T";
*response.getName() = selectedPreset->getDescription();
return 200;
}
QtMsgType WebAPIAdapterGUI::getMsgTypeFromString(const QString& msgTypeString) QtMsgType WebAPIAdapterGUI::getMsgTypeFromString(const QString& msgTypeString)
{ {
if (msgTypeString == "debug") { if (msgTypeString == "debug") {

View File

@ -78,6 +78,11 @@ public:
Swagger::SWGPresets& response, Swagger::SWGPresets& response,
Swagger::SWGErrorResponse& error); Swagger::SWGErrorResponse& error);
virtual int instancePresetPatch(
Swagger::SWGPresetTransfer& query,
Swagger::SWGPresetIdentifier& response,
Swagger::SWGErrorResponse& error);
private: private:
MainWindow& m_mainWindow; MainWindow& m_mainWindow;

View File

@ -265,7 +265,9 @@ paths:
schema: schema:
$ref: "#/definitions/PresetIdentifier" $ref: "#/definitions/PresetIdentifier"
"400": "400":
description: Invalid frequency description: Preset type and device set type mismatch
schema:
$ref: "#/definitions/ErrorResponse"
"404": "404":
description: No preset or device set found description: No preset or device set found
schema: schema:
@ -293,8 +295,6 @@ paths:
description: On success return preset identification description: On success return preset identification
schema: schema:
$ref: "#/definitions/PresetIdentifier" $ref: "#/definitions/PresetIdentifier"
"400":
description: Invalid frequency
"404": "404":
description: No preset or device set found description: No preset or device set found
schema: schema:
@ -761,9 +761,9 @@ definitions:
description: "Name of the preset group" description: "Name of the preset group"
type: string type: string
centerFrequency: centerFrequency:
description: "Center freqeuency in MHz" description: "Center freqeuency in Hz"
type: number type: integer
format: float format: int64
type: type:
description: "Type of device set (R: Rx, T: Tx)" description: "Type of device set (R: Rx, T: Tx)"
type: string type: string

View File

@ -38,7 +38,7 @@ SWGPresetIdentifier::~SWGPresetIdentifier() {
void void
SWGPresetIdentifier::init() { SWGPresetIdentifier::init() {
group_name = new QString(""); group_name = new QString("");
center_frequency = 0.0f; center_frequency = 0L;
type = new QString(""); type = new QString("");
name = new QString(""); name = new QString("");
} }
@ -72,7 +72,7 @@ SWGPresetIdentifier::fromJson(QString &json) {
void void
SWGPresetIdentifier::fromJsonObject(QJsonObject &pJson) { SWGPresetIdentifier::fromJsonObject(QJsonObject &pJson) {
::Swagger::setValue(&group_name, pJson["groupName"], "QString", "QString"); ::Swagger::setValue(&group_name, pJson["groupName"], "QString", "QString");
::Swagger::setValue(&center_frequency, pJson["centerFrequency"], "float", ""); ::Swagger::setValue(&center_frequency, pJson["centerFrequency"], "qint64", "");
::Swagger::setValue(&type, pJson["type"], "QString", "QString"); ::Swagger::setValue(&type, pJson["type"], "QString", "QString");
::Swagger::setValue(&name, pJson["name"], "QString", "QString"); ::Swagger::setValue(&name, pJson["name"], "QString", "QString");
} }
@ -111,12 +111,12 @@ SWGPresetIdentifier::setGroupName(QString* group_name) {
this->group_name = group_name; this->group_name = group_name;
} }
float qint64
SWGPresetIdentifier::getCenterFrequency() { SWGPresetIdentifier::getCenterFrequency() {
return center_frequency; return center_frequency;
} }
void void
SWGPresetIdentifier::setCenterFrequency(float center_frequency) { SWGPresetIdentifier::setCenterFrequency(qint64 center_frequency) {
this->center_frequency = center_frequency; this->center_frequency = center_frequency;
} }

View File

@ -45,8 +45,8 @@ public:
QString* getGroupName(); QString* getGroupName();
void setGroupName(QString* group_name); void setGroupName(QString* group_name);
float getCenterFrequency(); qint64 getCenterFrequency();
void setCenterFrequency(float center_frequency); void setCenterFrequency(qint64 center_frequency);
QString* getType(); QString* getType();
void setType(QString* type); void setType(QString* type);
@ -57,7 +57,7 @@ public:
private: private:
QString* group_name; QString* group_name;
float center_frequency; qint64 center_frequency;
QString* type; QString* type;
QString* name; QString* name;
}; };