Web API: have /sdrangel/deviceset/{deviceSetIndex}/device/settings PUT,PATCH (1)

pull/127/head
f4exb 2017-12-07 13:55:42 +01:00
rodzic 3b69d6517b
commit c38497d9df
8 zmienionych plików z 131 dodań i 16 usunięć

Wyświetl plik

@ -22,6 +22,7 @@ CONFIG(MINGW64):LIBRTLSDRSRC = "D:\softs\librtlsdr"
INCLUDEPATH += $$PWD
INCLUDEPATH += ../../../sdrbase
INCLUDEPATH += ../../../sdrgui
INCLUDEPATH += ../../../swagger/sdrangel/code/qt5/client
!macx:INCLUDEPATH += $$LIBRTLSDRSRC/include
macx:INCLUDEPATH += /opt/local/include
@ -44,6 +45,7 @@ FORMS += rtlsdrgui.ui
LIBS += -L../../../sdrbase/$${build_subdir} -lsdrbase
LIBS += -L../../../sdrgui/$${build_subdir} -lsdrgui
LIBS += -L../../../swagger/$${build_subdir} -lswagger
!macx:LIBS += -L../../../librtlsdr/$${build_subdir} -llibrtlsdr
macx:LIBS += -L/opt/local/lib -lrtlsdr

Wyświetl plik

@ -51,6 +51,12 @@ public:
QString& errorMessage)
{ errorMessage = "Not implemented"; return 501; }
virtual int webapiSettingsPutPatch(
bool force __attribute__((unused)), //!< true to force settings = put
SWGSDRangel::SWGDeviceSettings& response __attribute__((unused)),
QString& errorMessage)
{ errorMessage = "Not implemented"; return 501; }
MessageQueue *getInputMessageQueue() { return &m_inputMessageQueue; }
void setMessageQueueToGUI(MessageQueue *queue) { m_guiMessageQueue = queue; }
MessageQueue *getMessageQueueToGUI() { return m_guiMessageQueue; }

Wyświetl plik

@ -51,6 +51,12 @@ public:
QString& errorMessage)
{ errorMessage = "Not implemented"; return 501; }
virtual int webapiSettingsPutPatch(
bool force __attribute__((unused)), //!< true to force settings = put
SWGSDRangel::SWGDeviceSettings& response __attribute__((unused)),
QString& errorMessage)
{ errorMessage = "Not implemented"; return 501; }
MessageQueue *getInputMessageQueue() { return &m_inputMessageQueue; }
virtual void setMessageQueueToGUI(MessageQueue *queue) { m_guiMessageQueue = queue; }
MessageQueue *getMessageQueueToGUI() { return m_guiMessageQueue; }

Wyświetl plik

@ -248,21 +248,12 @@ public:
{ return 501; }
/**
* Handler of /sdrangel/deviceset/{devicesetIndex}/device/settings (PUT) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels
* Handler of /sdrangel/deviceset/{devicesetIndex}/device/settings (PUT, PATCH) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels
* returns the Http status code (default 501: not implemented)
*/
virtual int devicesetDeviceSettingsPut(
int deviceSetIndex __attribute__((unused)),
SWGSDRangel::SWGDeviceSettings& response __attribute__((unused)),
SWGSDRangel::SWGErrorResponse& error __attribute__((unused)))
{ return 501; }
/**
* Handler of /sdrangel/deviceset/{devicesetIndex}/device/settings (PATCH) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels
* returns the Http status code (default 501: not implemented)
*/
virtual int devicesetDeviceSettingsPatch(
virtual int devicesetDeviceSettingsPutPatch(
int deviceSetIndex __attribute__((unused)),
bool force __attribute__((unused)), //!< true to force settings = put else patch
SWGSDRangel::SWGDeviceSettings& response __attribute__((unused)),
SWGSDRangel::SWGErrorResponse& error __attribute__((unused)))
{ return 501; }

Wyświetl plik

@ -626,11 +626,39 @@ void WebAPIRequestMapper::devicesetDeviceSettingsService(const std::string& inde
{
int deviceSetIndex = boost::lexical_cast<int>(indexStr);
if (request.getMethod() == "PUT")
{
}
else if (request.getMethod() == "PATCH")
if ((request.getMethod() == "PUT") || (request.getMethod() == "PATCH"))
{
QString jsonStr = request.getBody();
if (parseJsonBody(jsonStr, response))
{
SWGSDRangel::SWGDeviceSettings normalResponse;
resetDeviceSettings(normalResponse);
normalResponse.fromJson(jsonStr);
if (validateDeviceSettings(normalResponse))
{
int status = m_adapter->devicesetDeviceSettingsPutPatch(
deviceSetIndex,
(request.getMethod() == "PUT"), // force settings on PUT
normalResponse,
errorResponse);
response.setStatus(status);
if (status == 200) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(400,"Invalid JSON request");
errorResponse.init();
*errorResponse.getMessage() = "Invalid JSON request";
response.write(errorResponse.asJson().toUtf8());
}
}
}
else if (request.getMethod() == "GET")
{
@ -709,6 +737,30 @@ bool WebAPIRequestMapper::validatePresetIdentifer(SWGSDRangel::SWGPresetIdentifi
return (presetIdentifier.getGroupName() && presetIdentifier.getName() && presetIdentifier.getType());
}
bool WebAPIRequestMapper::validateDeviceSettings(SWGSDRangel::SWGDeviceSettings& deviceSettings)
{
QString *deviceHwType = deviceSettings.getDeviceHwType();
if (deviceHwType == 0)
{
return false;
}
else
{
if (*deviceHwType == "FileSource") {
return deviceSettings.getFileSourceSettings() != 0;
} else if (*deviceHwType == "RTLSDR") {
return deviceSettings.getRtlSdrSettings() != 0;
} else if (*deviceHwType == "LimeSDR") {
if (deviceSettings.getTx() == 0) {
return deviceSettings.getLimeSdrInputSettings() != 0;
} else {
return deviceSettings.getLimeSdrOutputSettings() != 0;
}
}
}
}
void WebAPIRequestMapper::resetDeviceSettings(SWGSDRangel::SWGDeviceSettings& deviceSettings)
{
deviceSettings.cleanup();

Wyświetl plik

@ -61,6 +61,7 @@ private:
bool validatePresetTransfer(SWGSDRangel::SWGPresetTransfer& presetTransfer);
bool validatePresetIdentifer(SWGSDRangel::SWGPresetIdentifier& presetIdentifier);
bool validateDeviceSettings(SWGSDRangel::SWGDeviceSettings& deviceSettings);
bool parseJsonBody(QString& jsonStr, qtwebapp::HttpResponse& response);

Wyświetl plik

@ -732,6 +732,57 @@ int WebAPIAdapterGUI::devicesetDeviceSettingsGet(
}
}
int WebAPIAdapterGUI::devicesetDeviceSettingsPutPatch(
int deviceSetIndex,
bool force,
SWGSDRangel::SWGDeviceSettings& response,
SWGSDRangel::SWGErrorResponse& error)
{
if ((deviceSetIndex >= 0) && (deviceSetIndex < (int) m_mainWindow.m_deviceUIs.size()))
{
DeviceUISet *deviceSet = m_mainWindow.m_deviceUIs[deviceSetIndex];
if (deviceSet->m_deviceSourceEngine) // Rx
{
if ((response.getTx() != 0) || (deviceSet->m_deviceSourceAPI->getHardwareId() != *response.getDeviceHwType()))
{
*error.getMessage() = QString("Device mismatch. Found %1 input").arg(deviceSet->m_deviceSourceAPI->getHardwareId());
return 400;
}
else
{
DeviceSampleSource *source = deviceSet->m_deviceSourceAPI->getSampleSource();
return source->webapiSettingsPutPatch(force, response, *error.getMessage());
}
}
else if (deviceSet->m_deviceSinkEngine) // Tx
{
if ((response.getTx() == 0) || (deviceSet->m_deviceSourceAPI->getHardwareId() != *response.getDeviceHwType()))
{
*error.getMessage() = QString("Device mismatch. Found %1 output").arg(deviceSet->m_deviceSourceAPI->getHardwareId());
return 400;
}
else
{
DeviceSampleSink *sink = deviceSet->m_deviceSinkAPI->getSampleSink();
return sink->webapiSettingsPutPatch(force, response, *error.getMessage());
}
}
else
{
*error.getMessage() = QString("DeviceSet error");
return 500;
}
}
else
{
error.init();
*error.getMessage() = QString("There is no device set with index %1").arg(deviceSetIndex);
return 404;
}
}
void WebAPIAdapterGUI::getDeviceSetList(SWGSDRangel::SWGDeviceSetList* deviceSetList)
{
deviceSetList->init();

Wyświetl plik

@ -125,6 +125,12 @@ public:
SWGSDRangel::SWGDeviceSettings& response,
SWGSDRangel::SWGErrorResponse& error);
virtual int devicesetDeviceSettingsPutPatch(
int deviceSetIndex,
bool force,
SWGSDRangel::SWGDeviceSettings& response,
SWGSDRangel::SWGErrorResponse& error);
private:
MainWindow& m_mainWindow;