1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-12-23 01:55:48 -05: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);
}
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);
int getPresetCount() const { return m_presets.count(); }
const Preset* getPreset(int index) const { return m_presets[index]; }
const Preset* getPreset(const QString& groupName, quint64 centerFrequency, const QString& description) const;
void sortPresets();
Preset* getWorkingPreset() { return &m_workingPreset; }

View File

@ -31,8 +31,10 @@ namespace Swagger
class SWGAudioDevicesSelect;
class SWGLocationInformation;
class SWGDVSeralDevices;
class SWGErrorResponse;
class SWGPresets;
class SWGPresetTransfer;
class SWGPresetIdentifier;
class SWGErrorResponse;
}
class WebAPIAdapterInterface
@ -134,7 +136,7 @@ public:
{ 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)
*/
virtual int instancePresetGet(
@ -142,6 +144,16 @@ public:
Swagger::SWGErrorResponse& error __attribute__((unused)))
{ 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 instanceDevicesURL;
static QString instanceChannelsURL;

View File

@ -30,6 +30,8 @@
#include "SWGLocationInformation.h"
#include "SWGDVSeralDevices.h"
#include "SWGPresets.h"
#include "SWGPresetTransfer.h"
#include "SWGPresetIdentifier.h"
#include "SWGErrorResponse.h"
WebAPIRequestMapper::WebAPIRequestMapper(QObject* parent) :
@ -327,11 +329,11 @@ void WebAPIRequestMapper::instanceDVSerialService(qtwebapp::HttpRequest& request
void WebAPIRequestMapper::instancePresetService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
Swagger::SWGPresets normalResponse;
Swagger::SWGErrorResponse errorResponse;
if (request.getMethod() == "GET")
{
Swagger::SWGPresets normalResponse;
int status = m_adapter->instancePresetGet(normalResponse, errorResponse);
response.setStatus(status);
@ -341,6 +343,25 @@ void WebAPIRequestMapper::instancePresetService(qtwebapp::HttpRequest& request,
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
{
response.setStatus(405,"Invalid HTTP method");

View File

@ -62,6 +62,8 @@
#include <string>
#include <QDebug>
MESSAGE_CLASS_DEFINITION(MainWindow::MsgLoadPreset, Message)
MainWindow *MainWindow::m_instance = 0;
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()
{
Message* message;
@ -661,6 +675,7 @@ void MainWindow::handleMessages()
while ((message = m_inputMessageQueue.pop()) != 0)
{
qDebug("MainWindow::handleMessages: message: %s", message->getIdentifier());
handleMessage(*message);
delete message;
}
}

View File

@ -23,6 +23,7 @@
#include <QList>
#include "settings/mainsettings.h"
#include "util/message.h"
#include "util/messagequeue.h"
#include "util/export.h"
#include "mainparser.h"
@ -83,6 +84,29 @@ public:
friend class WebAPIAdapterGUI;
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 {
PGroup,
PItem
@ -142,6 +166,8 @@ private:
void setLoggingOpions();
bool handleMessage(const Message& cmd);
private slots:
void handleMessages();
void updateStatus();

View File

@ -45,6 +45,8 @@
#include "SWGPresets.h"
#include "SWGPresetGroup.h"
#include "SWGPresetItem.h"
#include "SWGPresetTransfer.h"
#include "SWGPresetIdentifier.h"
#include "SWGErrorResponse.h"
#include "webapiadaptergui.h"
@ -430,6 +432,60 @@ int WebAPIAdapterGUI::instancePresetGet(
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)
{
if (msgTypeString == "debug") {

View File

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

View File

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

View File

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

View File

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