1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-12-23 10:05:46 -05:00

Web API: /sdrangel/preset (PUT, POST) implementation

This commit is contained in:
f4exb 2017-11-25 18:32:37 +01:00
parent 33e39f161a
commit 4a3901d688
7 changed files with 280 additions and 107 deletions

View File

@ -1049,9 +1049,9 @@ margin-bottom: 20px;
"description" : "Name of the preset group"
},
"centerFrequency" : {
"type" : "number",
"format" : "float",
"description" : "Center freqeuency in MHz"
"type" : "integer",
"format" : "int64",
"description" : "Center freqeuency in Hz"
},
"type" : {
"type" : "string",
@ -1068,9 +1068,9 @@ margin-bottom: 20px;
"required" : [ "centerFrequency", "name", "type" ],
"properties" : {
"centerFrequency" : {
"type" : "number",
"format" : "float",
"description" : "Center freqeuency in MHz"
"type" : "integer",
"format" : "int64",
"description" : "Center freqeuency in Hz"
},
"type" : {
"type" : "string",
@ -6195,12 +6195,47 @@ $(document).ready(function() {
</div>
</div>
<h3> Status: 400 - Invalid frequency </h3>
<h3> Status: 400 - Preset type and device set type mismatch </h3>
<ul class="nav nav-tabs nav-tabs-examples" >
<li class="active">
<a data-toggle="tab" href="#responses-instancePresetPatch-400-schema">Schema</a>
</li>
</ul>
<div class="tab-content" style='margin-bottom: 10px;'>
<div class="tab-pane active" id="responses-instancePresetPatch-400-schema">
<div id='responses-instancePresetPatch-400-schema-400' style="padding: 30px; border-left: 1px solid #eee; border-right: 1px solid #eee; border-bottom: 1px solid #eee;">
<script>
$(document).ready(function() {
var schemaWrapper = {
"description" : "Preset type and device set type mismatch",
"schema" : {
"$ref" : "#/definitions/ErrorResponse"
}
};
var schema = schemaWrapper.schema;
if (schema.$ref != null) {
schema = defsParser.$refs.get(schema.$ref);
} else {
schemaWrapper.definitions = Object.assign({}, defs);
$RefParser.dereference(schemaWrapper).catch(function(err) {
console.log(err);
});
}
//console.log(JSON.stringify(schema));
var view = new JSONSchemaView(schema, 3);
$('#responses-instancePresetPatch-400-schema-data').val(stringify(schema));
var result = $('#responses-instancePresetPatch-400-schema-400');
result.empty();
result.append(view.render());
});
</script>
</div>
<input id='responses-instancePresetPatch-400-schema-data' type='hidden' value=''></input>
</div>
</div>
<h3> Status: 404 - No preset or device set found </h3>
@ -6309,7 +6344,7 @@ $(document).ready(function() {
<div class="pull-right"></div>
<div class="clearfix"></div>
<p></p>
<p class="marked">Create a new preset from a device set settings</p>
<p class="marked">Create a new preset from a device set settings. Frequency and type is ignored and taken from the the device set values.</p>
<p></p>
<br />
<pre class="prettyprint language-html prettyprinted" data-type="post"><code><span class="pln">/sdrangel/preset</span></code></pre>
@ -6598,42 +6633,61 @@ $(document).ready(function() {
</div>
</div>
<h3> Status: 400 - Invalid frequency </h3>
<ul class="nav nav-tabs nav-tabs-examples" >
</ul>
<div class="tab-content" style='margin-bottom: 10px;'>
</div>
<h3> Status: 404 - Device set not found </h3>
<ul class="nav nav-tabs nav-tabs-examples" >
</ul>
<div class="tab-content" style='margin-bottom: 10px;'>
</div>
<h3> Status: 409 - Preset already exists </h3>
<ul class="nav nav-tabs nav-tabs-examples" >
</ul>
<div class="tab-content" style='margin-bottom: 10px;'>
</div>
<h3> Status: 500 - Error </h3>
<ul class="nav nav-tabs nav-tabs-examples" >
<li class="active">
<a data-toggle="tab" href="#responses-instancePresetPost-500-schema">Schema</a>
<a data-toggle="tab" href="#responses-instancePresetPost-404-schema">Schema</a>
</li>
</ul>
<div class="tab-content" style='margin-bottom: 10px;'>
<div class="tab-pane active" id="responses-instancePresetPost-500-schema">
<div id='responses-instancePresetPost-500-schema-500' style="padding: 30px; border-left: 1px solid #eee; border-right: 1px solid #eee; border-bottom: 1px solid #eee;">
<div class="tab-pane active" id="responses-instancePresetPost-404-schema">
<div id='responses-instancePresetPost-404-schema-404' style="padding: 30px; border-left: 1px solid #eee; border-right: 1px solid #eee; border-bottom: 1px solid #eee;">
<script>
$(document).ready(function() {
var schemaWrapper = {
"description" : "Device set not found",
"schema" : {
"$ref" : "#/definitions/ErrorResponse"
}
};
var schema = schemaWrapper.schema;
if (schema.$ref != null) {
schema = defsParser.$refs.get(schema.$ref);
} else {
schemaWrapper.definitions = Object.assign({}, defs);
$RefParser.dereference(schemaWrapper).catch(function(err) {
console.log(err);
});
}
//console.log(JSON.stringify(schema));
var view = new JSONSchemaView(schema, 3);
$('#responses-instancePresetPost-404-schema-data').val(stringify(schema));
var result = $('#responses-instancePresetPost-404-schema-404');
result.empty();
result.append(view.render());
});
</script>
</div>
<input id='responses-instancePresetPost-404-schema-data' type='hidden' value=''></input>
</div>
</div>
<h3> Status: 409 - Error </h3>
<ul class="nav nav-tabs nav-tabs-examples" >
<li class="active">
<a data-toggle="tab" href="#responses-instancePresetPost-409-schema">Schema</a>
</li>
</ul>
<div class="tab-content" style='margin-bottom: 10px;'>
<div class="tab-pane active" id="responses-instancePresetPost-409-schema">
<div id='responses-instancePresetPost-409-schema-409' style="padding: 30px; border-left: 1px solid #eee; border-right: 1px solid #eee; border-bottom: 1px solid #eee;">
<script>
$(document).ready(function() {
var schemaWrapper = {
@ -6654,14 +6708,14 @@ $(document).ready(function() {
//console.log(JSON.stringify(schema));
var view = new JSONSchemaView(schema, 3);
$('#responses-instancePresetPost-500-schema-data').val(stringify(schema));
var result = $('#responses-instancePresetPost-500-schema-500');
$('#responses-instancePresetPost-409-schema-data').val(stringify(schema));
var result = $('#responses-instancePresetPost-409-schema-409');
result.empty();
result.append(view.render());
});
</script>
</div>
<input id='responses-instancePresetPost-500-schema-data' type='hidden' value=''></input>
<input id='responses-instancePresetPost-409-schema-data' type='hidden' value=''></input>
</div>
</div>
@ -6685,7 +6739,7 @@ $(document).ready(function() {
<div class="pull-right"></div>
<div class="clearfix"></div>
<p></p>
<p class="marked">Save device set settings on a preset</p>
<p class="marked">Update device set settings on an existing preset.</p>
<p></p>
<br />
<pre class="prettyprint language-html prettyprinted" data-type="put"><code><span class="pln">/sdrangel/preset</span></code></pre>
@ -6974,14 +7028,6 @@ $(document).ready(function() {
</div>
</div>
<h3> Status: 400 - Invalid frequency </h3>
<ul class="nav nav-tabs nav-tabs-examples" >
</ul>
<div class="tab-content" style='margin-bottom: 10px;'>
</div>
<h3> Status: 404 - No preset or device set found </h3>
<ul class="nav nav-tabs nav-tabs-examples" >
@ -7393,7 +7439,7 @@ except ApiException as e:
</div>
<div id="generator">
<div class="content">
Generated 2017-11-25T04:38:54.817+01:00
Generated 2017-11-25T18:28:10.878+01:00
</div>
</div>
</div>

View File

@ -164,6 +164,16 @@ public:
Swagger::SWGErrorResponse& error __attribute__((unused)))
{ return 501; }
/**
* Handler of /sdrangel/preset (POST) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels
* returns the Http status code (default 501: not implemented)
*/
virtual int instancePresetPost(
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

@ -381,6 +381,25 @@ void WebAPIRequestMapper::instancePresetService(qtwebapp::HttpRequest& request,
}
}
}
else if (request.getMethod() == "POST")
{
Swagger::SWGPresetTransfer query;
Swagger::SWGPresetIdentifier normalResponse;
QString jsonStr = request.getBody();
if (parseJsonBody(jsonStr, response))
{
query.fromJson(jsonStr);
int status = m_adapter->instancePresetPost(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

@ -494,7 +494,6 @@ int WebAPIAdapterGUI::instancePresetPut(
int deviceSetIndex = query.getDeviceSetIndex();
Swagger::SWGPresetIdentifier *presetIdentifier = query.getPreset();
int nbDeviceSets = m_mainWindow.m_deviceUIs.size();
bool newPreset;
if (deviceSetIndex > nbDeviceSets)
{
@ -506,15 +505,17 @@ int WebAPIAdapterGUI::instancePresetPut(
presetIdentifier->getCenterFrequency(),
*presetIdentifier->getName());
if (selectedPreset == 0) // save on a new preset
if (selectedPreset == 0)
{
selectedPreset = m_mainWindow.m_settings.newPreset(*presetIdentifier->getGroupName(), *presetIdentifier->getName());
newPreset = true;
*error.getMessage() = QString("There is no preset [%1, %2, %3]")
.arg(*presetIdentifier->getGroupName())
.arg(presetIdentifier->getCenterFrequency())
.arg(*presetIdentifier->getName());
return 404;
}
else // update existing preset
{
DeviceUISet *deviceUI = m_mainWindow.m_deviceUIs[deviceSetIndex];
newPreset = false;
if (deviceUI->m_deviceSourceEngine && !selectedPreset->isSourcePreset())
{
@ -529,7 +530,51 @@ int WebAPIAdapterGUI::instancePresetPut(
}
}
MainWindow::MsgSavePreset *msg = MainWindow::MsgSavePreset::create(const_cast<Preset*>(selectedPreset), deviceSetIndex, newPreset);
MainWindow::MsgSavePreset *msg = MainWindow::MsgSavePreset::create(const_cast<Preset*>(selectedPreset), deviceSetIndex, false);
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;
}
int WebAPIAdapterGUI::instancePresetPost(
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) // save on a new preset
{
selectedPreset = m_mainWindow.m_settings.newPreset(*presetIdentifier->getGroupName(), *presetIdentifier->getName());
}
else
{
*error.getMessage() = QString("Preset already exists [%1, %2, %3]")
.arg(*presetIdentifier->getGroupName())
.arg(presetIdentifier->getCenterFrequency())
.arg(*presetIdentifier->getName());
return 409;
}
MainWindow::MsgSavePreset *msg = MainWindow::MsgSavePreset::create(const_cast<Preset*>(selectedPreset), deviceSetIndex, true);
m_mainWindow.m_inputMessageQueue.push(msg);
response.init();

View File

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

View File

@ -279,7 +279,7 @@ paths:
"501":
description: Function not implemented
put:
description: Save device set settings on a preset. Preset is updated if it already exists else it is created with the given name and group. If created given frequency and type is ignored and is determined with the device set values.
description: Update device set settings on an existing preset.
operationId: instancePresetPut
consumes:
- application/json
@ -306,7 +306,7 @@ paths:
"501":
description: Function not implemented
post:
description: Create a new preset from a device set settings
description: Create a new preset from a device set settings. Frequency and type is ignored and taken from the the device set values.
operationId: instancePresetPost
consumes:
- application/json
@ -322,13 +322,15 @@ paths:
description: On success return preset identification
schema:
$ref: "#/definitions/PresetIdentifier"
"400":
description: Invalid frequency
"404":
description: Device set not found
schema:
$ref: "#/definitions/ErrorResponse"
"409":
description: Preset already exists
"500":
schema:
$ref: "#/definitions/ErrorResponse"
"500":
description: Error
schema:
$ref: "#/definitions/ErrorResponse"

View File

@ -1049,9 +1049,9 @@ margin-bottom: 20px;
"description" : "Name of the preset group"
},
"centerFrequency" : {
"type" : "number",
"format" : "float",
"description" : "Center freqeuency in MHz"
"type" : "integer",
"format" : "int64",
"description" : "Center freqeuency in Hz"
},
"type" : {
"type" : "string",
@ -1068,9 +1068,9 @@ margin-bottom: 20px;
"required" : [ "centerFrequency", "name", "type" ],
"properties" : {
"centerFrequency" : {
"type" : "number",
"format" : "float",
"description" : "Center freqeuency in MHz"
"type" : "integer",
"format" : "int64",
"description" : "Center freqeuency in Hz"
},
"type" : {
"type" : "string",
@ -6195,12 +6195,47 @@ $(document).ready(function() {
</div>
</div>
<h3> Status: 400 - Invalid frequency </h3>
<h3> Status: 400 - Preset type and device set type mismatch </h3>
<ul class="nav nav-tabs nav-tabs-examples" >
<li class="active">
<a data-toggle="tab" href="#responses-instancePresetPatch-400-schema">Schema</a>
</li>
</ul>
<div class="tab-content" style='margin-bottom: 10px;'>
<div class="tab-pane active" id="responses-instancePresetPatch-400-schema">
<div id='responses-instancePresetPatch-400-schema-400' style="padding: 30px; border-left: 1px solid #eee; border-right: 1px solid #eee; border-bottom: 1px solid #eee;">
<script>
$(document).ready(function() {
var schemaWrapper = {
"description" : "Preset type and device set type mismatch",
"schema" : {
"$ref" : "#/definitions/ErrorResponse"
}
};
var schema = schemaWrapper.schema;
if (schema.$ref != null) {
schema = defsParser.$refs.get(schema.$ref);
} else {
schemaWrapper.definitions = Object.assign({}, defs);
$RefParser.dereference(schemaWrapper).catch(function(err) {
console.log(err);
});
}
//console.log(JSON.stringify(schema));
var view = new JSONSchemaView(schema, 3);
$('#responses-instancePresetPatch-400-schema-data').val(stringify(schema));
var result = $('#responses-instancePresetPatch-400-schema-400');
result.empty();
result.append(view.render());
});
</script>
</div>
<input id='responses-instancePresetPatch-400-schema-data' type='hidden' value=''></input>
</div>
</div>
<h3> Status: 404 - No preset or device set found </h3>
@ -6309,7 +6344,7 @@ $(document).ready(function() {
<div class="pull-right"></div>
<div class="clearfix"></div>
<p></p>
<p class="marked">Create a new preset from a device set settings</p>
<p class="marked">Create a new preset from a device set settings. Frequency and type is ignored and taken from the the device set values.</p>
<p></p>
<br />
<pre class="prettyprint language-html prettyprinted" data-type="post"><code><span class="pln">/sdrangel/preset</span></code></pre>
@ -6598,42 +6633,61 @@ $(document).ready(function() {
</div>
</div>
<h3> Status: 400 - Invalid frequency </h3>
<ul class="nav nav-tabs nav-tabs-examples" >
</ul>
<div class="tab-content" style='margin-bottom: 10px;'>
</div>
<h3> Status: 404 - Device set not found </h3>
<ul class="nav nav-tabs nav-tabs-examples" >
</ul>
<div class="tab-content" style='margin-bottom: 10px;'>
</div>
<h3> Status: 409 - Preset already exists </h3>
<ul class="nav nav-tabs nav-tabs-examples" >
</ul>
<div class="tab-content" style='margin-bottom: 10px;'>
</div>
<h3> Status: 500 - Error </h3>
<ul class="nav nav-tabs nav-tabs-examples" >
<li class="active">
<a data-toggle="tab" href="#responses-instancePresetPost-500-schema">Schema</a>
<a data-toggle="tab" href="#responses-instancePresetPost-404-schema">Schema</a>
</li>
</ul>
<div class="tab-content" style='margin-bottom: 10px;'>
<div class="tab-pane active" id="responses-instancePresetPost-500-schema">
<div id='responses-instancePresetPost-500-schema-500' style="padding: 30px; border-left: 1px solid #eee; border-right: 1px solid #eee; border-bottom: 1px solid #eee;">
<div class="tab-pane active" id="responses-instancePresetPost-404-schema">
<div id='responses-instancePresetPost-404-schema-404' style="padding: 30px; border-left: 1px solid #eee; border-right: 1px solid #eee; border-bottom: 1px solid #eee;">
<script>
$(document).ready(function() {
var schemaWrapper = {
"description" : "Device set not found",
"schema" : {
"$ref" : "#/definitions/ErrorResponse"
}
};
var schema = schemaWrapper.schema;
if (schema.$ref != null) {
schema = defsParser.$refs.get(schema.$ref);
} else {
schemaWrapper.definitions = Object.assign({}, defs);
$RefParser.dereference(schemaWrapper).catch(function(err) {
console.log(err);
});
}
//console.log(JSON.stringify(schema));
var view = new JSONSchemaView(schema, 3);
$('#responses-instancePresetPost-404-schema-data').val(stringify(schema));
var result = $('#responses-instancePresetPost-404-schema-404');
result.empty();
result.append(view.render());
});
</script>
</div>
<input id='responses-instancePresetPost-404-schema-data' type='hidden' value=''></input>
</div>
</div>
<h3> Status: 409 - Error </h3>
<ul class="nav nav-tabs nav-tabs-examples" >
<li class="active">
<a data-toggle="tab" href="#responses-instancePresetPost-409-schema">Schema</a>
</li>
</ul>
<div class="tab-content" style='margin-bottom: 10px;'>
<div class="tab-pane active" id="responses-instancePresetPost-409-schema">
<div id='responses-instancePresetPost-409-schema-409' style="padding: 30px; border-left: 1px solid #eee; border-right: 1px solid #eee; border-bottom: 1px solid #eee;">
<script>
$(document).ready(function() {
var schemaWrapper = {
@ -6654,14 +6708,14 @@ $(document).ready(function() {
//console.log(JSON.stringify(schema));
var view = new JSONSchemaView(schema, 3);
$('#responses-instancePresetPost-500-schema-data').val(stringify(schema));
var result = $('#responses-instancePresetPost-500-schema-500');
$('#responses-instancePresetPost-409-schema-data').val(stringify(schema));
var result = $('#responses-instancePresetPost-409-schema-409');
result.empty();
result.append(view.render());
});
</script>
</div>
<input id='responses-instancePresetPost-500-schema-data' type='hidden' value=''></input>
<input id='responses-instancePresetPost-409-schema-data' type='hidden' value=''></input>
</div>
</div>
@ -6685,7 +6739,7 @@ $(document).ready(function() {
<div class="pull-right"></div>
<div class="clearfix"></div>
<p></p>
<p class="marked">Save device set settings on a preset</p>
<p class="marked">Update device set settings on an existing preset.</p>
<p></p>
<br />
<pre class="prettyprint language-html prettyprinted" data-type="put"><code><span class="pln">/sdrangel/preset</span></code></pre>
@ -6974,14 +7028,6 @@ $(document).ready(function() {
</div>
</div>
<h3> Status: 400 - Invalid frequency </h3>
<ul class="nav nav-tabs nav-tabs-examples" >
</ul>
<div class="tab-content" style='margin-bottom: 10px;'>
</div>
<h3> Status: 404 - No preset or device set found </h3>
<ul class="nav nav-tabs nav-tabs-examples" >
@ -7393,7 +7439,7 @@ except ApiException as e:
</div>
<div id="generator">
<div class="content">
Generated 2017-11-25T04:38:54.817+01:00
Generated 2017-11-25T18:28:10.878+01:00
</div>
</div>
</div>