Web API: /sdrangel/deviceset/{index}/device (PUT) implementation

This commit is contained in:
f4exb 2017-11-27 08:14:07 +01:00
parent c5a19e261c
commit 3bfdd49b4f
8 changed files with 192 additions and 3 deletions

View File

@ -37,6 +37,7 @@ namespace Swagger
class SWGPresetIdentifier;
class SWGDeviceSetList;
class SWGDeviceSet;
class SWGDeviceListItem;
class SWGErrorResponse;
}
@ -224,6 +225,16 @@ public:
Swagger::SWGErrorResponse& error __attribute__((unused)))
{ return 501; }
/**
* Handler of /sdrangel/devicesets (DELETE) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels
* returns the Http status code (default 501: not implemented)
*/
virtual int devicesetDevicePut(
int deviceSetIndex __attribute__((unused)),
Swagger::SWGDeviceListItem& response __attribute__((unused)),
Swagger::SWGErrorResponse& error __attribute__((unused)))
{ return 501; }
static QString instanceSummaryURL;
static QString instanceDevicesURL;
static QString instanceChannelsURL;

View File

@ -87,6 +87,8 @@ void WebAPIRequestMapper::service(qtwebapp::HttpRequest& request, qtwebapp::Http
if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetURLRe)) {
deviceset(std::string(desc_match[1]), request, response);
} else if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetDeviceURLRe)) {
devicesetDevice(std::string(desc_match[1]), request, response);
}
else
{
@ -571,6 +573,48 @@ void WebAPIRequestMapper::deviceset(const std::string& indexStr, qtwebapp::HttpR
}
}
void WebAPIRequestMapper::devicesetDevice(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
Swagger::SWGErrorResponse errorResponse;
try
{
int deviceSetIndex = boost::lexical_cast<int>(indexStr);
if (request.getMethod() == "PUT")
{
Swagger::SWGDeviceListItem normalResponse;
QString jsonStr = request.getBody();
if (parseJsonBody(jsonStr, response))
{
normalResponse.fromJson(jsonStr);
int status = m_adapter->devicesetDevicePut(deviceSetIndex, 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");
response.write("Invalid HTTP method");
}
}
catch (const boost::bad_lexical_cast &e)
{
errorResponse.init();
*errorResponse.getMessage() = "Wrong integer conversion on device set index";
response.setStatus(400,"Invalid data");
response.write(errorResponse.asJson().toUtf8());
}
}
bool WebAPIRequestMapper::parseJsonBody(QString& jsonStr, qtwebapp::HttpResponse& response)
{
Swagger::SWGErrorResponse errorResponse;

View File

@ -56,6 +56,7 @@ private:
void instanceDeviceSetsService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response);
void deviceset(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response);
void devicesetDevice(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response);
bool validatePresetTransfer(Swagger::SWGPresetTransfer& presetTransfer);
bool validatePresetIdentifer(Swagger::SWGPresetIdentifier& presetIdentifier);

View File

@ -67,6 +67,7 @@ MESSAGE_CLASS_DEFINITION(MainWindow::MsgSavePreset, Message)
MESSAGE_CLASS_DEFINITION(MainWindow::MsgDeletePreset, Message)
MESSAGE_CLASS_DEFINITION(MainWindow::MsgAddDeviceSet, Message)
MESSAGE_CLASS_DEFINITION(MainWindow::MsgRemoveLastDeviceSet, Message)
MESSAGE_CLASS_DEFINITION(MainWindow::MsgSetDevice, Message)
MainWindow *MainWindow::m_instance = 0;
@ -728,6 +729,21 @@ bool MainWindow::handleMessage(const Message& cmd)
return true;
}
else if (MsgSetDevice::match(cmd))
{
MsgSetDevice& notif = (MsgSetDevice&) cmd;
ui->tabInputsSelect->setCurrentIndex(notif.getDeviceSetIndex());
DeviceUISet *deviceUI = m_deviceUIs[notif.getDeviceSetIndex()];
deviceUI->m_samplingDeviceControl->setSelectedDeviceIndex(notif.getDeviceIndex());
if (notif.isTx()) {
on_sampleSink_changed();
} else {
on_sampleSource_changed();
}
return true;
}
return false;
}

View File

@ -188,6 +188,32 @@ private:
{ }
};
class MsgSetDevice : public Message {
MESSAGE_CLASS_DECLARATION
public:
int getDeviceSetIndex() const { return m_deviceSetIndex; }
int getDeviceIndex() const { return m_deviceIndex; }
bool isTx() const { return m_tx; }
static MsgSetDevice* create(int deviceSetIndex, int deviceIndex, bool tx)
{
return new MsgSetDevice(deviceSetIndex, deviceIndex, tx);
}
private:
int m_deviceSetIndex;
int m_deviceIndex;
bool m_tx;
MsgSetDevice(int deviceSetIndex, int deviceIndex, bool tx) :
Message(),
m_deviceSetIndex(deviceSetIndex),
m_deviceIndex(deviceIndex),
m_tx(tx)
{ }
};
enum {
PGroup,
PItem

View File

@ -614,6 +614,83 @@ int WebAPIAdapterGUI::devicesetGet(
}
}
int WebAPIAdapterGUI::devicesetDevicePut(
int deviceSetIndex,
Swagger::SWGDeviceListItem& response,
Swagger::SWGErrorResponse& error)
{
if ((deviceSetIndex >= 0) && (deviceSetIndex < (int) m_mainWindow.m_deviceUIs.size()))
{
DeviceUISet *deviceSet = m_mainWindow.m_deviceUIs[deviceSetIndex];
if ((response.getTx() == 0) && (deviceSet->m_deviceSinkEngine))
{
*error.getMessage() = QString("Device type (Rx) and device set type (Tx) mismatch");
return 404;
}
if ((response.getTx() != 0) && (deviceSet->m_deviceSourceEngine))
{
*error.getMessage() = QString("Device type (Tx) and device set type (Rx) mismatch");
return 404;
}
int nbSamplingDevices = response.getTx() != 0 ? DeviceEnumerator::instance()->getNbTxSamplingDevices() : DeviceEnumerator::instance()->getNbRxSamplingDevices();
int tx = response.getTx();
for (int i = 0; i < nbSamplingDevices; i++)
{
PluginInterface::SamplingDevice samplingDevice = response.getTx() ? DeviceEnumerator::instance()->getTxSamplingDevice(i) : DeviceEnumerator::instance()->getRxSamplingDevice(i);
if (response.getDisplayedName() && (*response.getDisplayedName() != samplingDevice.displayedName)) {
continue;
}
if (response.getHwType() && (*response.getHwType() != samplingDevice.hardwareId)) {
continue;
}
if ((response.getSequence() >= 0) && (response.getSequence() != samplingDevice.sequence)) {
continue;
}
if (response.getSerial() && (*response.getSerial() != samplingDevice.serial)) {
continue;
}
if ((response.getStreamIndex() >= 0) && (response.getStreamIndex() != samplingDevice.deviceItemIndex)) {
continue;
}
MainWindow::MsgSetDevice *msg = MainWindow::MsgSetDevice::create(deviceSetIndex, i, response.getTx() != 0);
m_mainWindow.m_inputMessageQueue.push(msg);
response.init();
*response.getDisplayedName() = samplingDevice.displayedName;
*response.getHwType() = samplingDevice.hardwareId;
*response.getSerial() = samplingDevice.serial;
response.setSequence(samplingDevice.sequence);
response.setTx(tx);
response.setNbStreams(samplingDevice.deviceNbItems);
response.setStreamIndex(samplingDevice.deviceItemIndex);
response.setDeviceSetIndex(deviceSetIndex);
response.setIndex(i);
return 200;
}
*error.getMessage() = QString("Device not found");
return 404;
}
else
{
error.init();
*error.getMessage() = QString("There is no device set with index %1").arg(deviceSetIndex);
return 404;
}
}
void WebAPIAdapterGUI::getDeviceSetList(Swagger::SWGDeviceSetList* deviceSetList)
{
deviceSetList->init();

View File

@ -115,6 +115,11 @@ public:
Swagger::SWGDeviceSet& response,
Swagger::SWGErrorResponse& error);
virtual int devicesetDevicePut(
int deviceSetIndex,
Swagger::SWGDeviceListItem& response,
Swagger::SWGErrorResponse& error);
private:
MainWindow& m_mainWindow;

View File

@ -466,11 +466,21 @@ paths:
type: integer
required: true
description: Index of device set in the device set list
- name: body
in: body
description: Device item to look for. Give values for only the fields you want to search for among displayedName, hwType, serial, sequence and deviceSetIndex. For integers except tx set to -1 to ignore the field. It will match the first device found.
required: true
schema:
$ref: "#/definitions/DeviceListItem"
responses:
"200":
description: On success return details on the device set
description: On success return details on the device
schema:
$ref: "#/definitions/DeviceSet"
$ref: "#/definitions/DeviceListItem"
"400":
description: Device set and device type mismatch (Rx vs Tx)
schema:
$ref: "#/definitions/ErrorResponse"
"404":
description: Invalid device set index or device not found
schema:
@ -549,7 +559,6 @@ definitions:
DeviceListItem:
description: "Summarized information about attached hardware device"
required:
- hwType
- tx
properties:
displayedName: