1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-26 01:39:05 -05:00

Web API: /sdrangel/location implementation

This commit is contained in:
f4exb 2017-11-25 04:02:11 +01:00
parent 72615b188e
commit aa8b02a225
6 changed files with 106 additions and 2 deletions

View File

@ -23,3 +23,4 @@ QString WebAPIAdapterInterface::instanceDevicesURL = "/sdrangel/devices";
QString WebAPIAdapterInterface::instanceChannelsURL = "/sdrangel/channels";
QString WebAPIAdapterInterface::instanceLoggingURL = "/sdrangel/logging";
QString WebAPIAdapterInterface::instanceAudioURL = "/sdrangel/audio";
QString WebAPIAdapterInterface::instanceLocationURL = "/sdrangel/location";

View File

@ -29,6 +29,7 @@ namespace Swagger
class SWGLoggingInfo;
class SWGAudioDevices;
class SWGAudioDevicesSelect;
class SWGLocationInformation;
class SWGErrorResponse;
}
@ -102,11 +103,30 @@ public:
Swagger::SWGErrorResponse& error __attribute__((unused)))
{ return 501; }
/**
* Handler of /sdrangel/location (GET) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels
* returns the Http status code (default 501: not implemented)
*/
virtual int instanceLocationGet(
Swagger::SWGLocationInformation& response __attribute__((unused)),
Swagger::SWGErrorResponse& error __attribute__((unused)))
{ return 501; }
/**
* Handler of /sdrangel/location (PUT) swagger/sdrangel/code/html2/index.html#api-Default-instanceChannels
* returns the Http status code (default 501: not implemented)
*/
virtual int instanceLocationPut(
Swagger::SWGLocationInformation& response __attribute__((unused)),
Swagger::SWGErrorResponse& error __attribute__((unused)))
{ return 501; }
static QString instanceSummaryURL;
static QString instanceDevicesURL;
static QString instanceChannelsURL;
static QString instanceLoggingURL;
static QString instanceAudioURL;
static QString instanceLocationURL;
};

View File

@ -27,6 +27,7 @@
#include "SWGInstanceChannelsResponse.h"
#include "SWGAudioDevices.h"
#include "SWGAudioDevicesSelect.h"
#include "SWGLocationInformation.h"
#include "SWGErrorResponse.h"
WebAPIRequestMapper::WebAPIRequestMapper(QObject* parent) :
@ -64,6 +65,8 @@ void WebAPIRequestMapper::service(qtwebapp::HttpRequest& request, qtwebapp::Http
instanceLoggingService(request, response);
} else if (path == WebAPIAdapterInterface::instanceAudioURL) {
instanceAudioService(request, response);
} else if (path == WebAPIAdapterInterface::instanceLocationURL) {
instanceLocationService(request, response);
}
else
{
@ -235,6 +238,47 @@ void WebAPIRequestMapper::instanceAudioService(qtwebapp::HttpRequest& request, q
}
}
void WebAPIRequestMapper::instanceLocationService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
Swagger::SWGErrorResponse errorResponse;
if (request.getMethod() == "GET")
{
Swagger::SWGLocationInformation normalResponse;
int status = m_adapter->instanceLocationGet(normalResponse, errorResponse);
response.setStatus(status);
if (status == 200) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else if (request.getMethod() == "PUT")
{
Swagger::SWGLocationInformation normalResponse;
QString jsonStr = request.getBody();
if (parseJsonBody(jsonStr, response))
{
normalResponse.fromJson(jsonStr);
int status = m_adapter->instanceLocationPut(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");
}
}
bool WebAPIRequestMapper::parseJsonBody(QString& jsonStr, qtwebapp::HttpResponse& response)
{
@ -248,7 +292,7 @@ bool WebAPIRequestMapper::parseJsonBody(QString& jsonStr, qtwebapp::HttpResponse
if (error.error != QJsonParseError::NoError)
{
QString errorMsg = QString("Input JSON error: ") + error.errorString();
QString errorMsg = QString("Input JSON error: ") + error.errorString() + QString(" at offset ") + QString::number(error.offset);
errorResponse.init();
*errorResponse.getMessage() = errorMsg;
response.setStatus(400, errorMsg.toUtf8());

View File

@ -44,6 +44,7 @@ private:
void instanceChannelsService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response);
void instanceLoggingService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response);
void instanceAudioService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response);
void instanceLocationService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response);
bool parseJsonBody(QString& jsonStr, qtwebapp::HttpResponse& response);
};

View File

@ -39,6 +39,7 @@
#include "SWGDeviceListItem.h"
#include "SWGAudioDevices.h"
#include "SWGAudioDevicesSelect.h"
#include "SWGLocationInformation.h"
#include "SWGErrorResponse.h"
#include "webapiadaptergui.h"
@ -287,7 +288,7 @@ int WebAPIAdapterGUI::instanceAudioGet(
int WebAPIAdapterGUI::instanceAudioPatch(
Swagger::SWGAudioDevicesSelect& response,
Swagger::SWGErrorResponse& error)
Swagger::SWGErrorResponse& error __attribute__((unused)))
{
// response input is the query actually
float inputVolume = response.getInputVolume();
@ -318,6 +319,35 @@ int WebAPIAdapterGUI::instanceAudioPatch(
return 200;
}
int WebAPIAdapterGUI::instanceLocationGet(
Swagger::SWGLocationInformation& response,
Swagger::SWGErrorResponse& error __attribute__((unused)))
{
response.setLatitude(m_mainWindow.m_settings.getLatitude());
response.setLongitude(m_mainWindow.m_settings.getLongitude());
return 200;
}
int WebAPIAdapterGUI::instanceLocationPut(
Swagger::SWGLocationInformation& response,
Swagger::SWGErrorResponse& error __attribute__((unused)))
{
float latitude = response.getLatitude();
float longitude = response.getLongitude();
latitude = latitude < -90.0 ? -90.0 : latitude > 90.0 ? 90.0 : latitude;
longitude = longitude < -180.0 ? -180.0 : longitude > 180.0 ? 180.0 : longitude;
m_mainWindow.m_settings.setLatitude(latitude);
m_mainWindow.m_settings.setLongitude(longitude);
response.setLatitude(m_mainWindow.m_settings.getLatitude());
response.setLongitude(m_mainWindow.m_settings.getLongitude());
return 200;
}
QtMsgType WebAPIAdapterGUI::getMsgTypeFromString(const QString& msgTypeString)
{
if (msgTypeString == "debug") {

View File

@ -61,6 +61,14 @@ public:
Swagger::SWGAudioDevicesSelect& response,
Swagger::SWGErrorResponse& error);
virtual int instanceLocationGet(
Swagger::SWGLocationInformation& response,
Swagger::SWGErrorResponse& error);
virtual int instanceLocationPut(
Swagger::SWGLocationInformation& response,
Swagger::SWGErrorResponse& error);
private:
MainWindow& m_mainWindow;