///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2017 Edouard Griffiths, F4EXB. //
// //
// Swagger server adapter interface //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation as version 3 of the License, or //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License V3 for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see . //
///////////////////////////////////////////////////////////////////////////////////
#include
#include
#include
#include
#include "httpdocrootsettings.h"
#include "webapirequestmapper.h"
#include "SWGInstanceSummaryResponse.h"
#include "SWGInstanceDevicesResponse.h"
#include "SWGInstanceChannelsResponse.h"
#include "SWGAudioDevices.h"
#include "SWGAudioDevicesSelect.h"
#include "SWGLocationInformation.h"
#include "SWGDVSeralDevices.h"
#include "SWGPresets.h"
#include "SWGPresetTransfer.h"
#include "SWGPresetIdentifier.h"
#include "SWGPresetImport.h"
#include "SWGPresetExport.h"
#include "SWGDeviceSettings.h"
#include "SWGDeviceState.h"
#include "SWGChannelsDetail.h"
#include "SWGChannelSettings.h"
#include "SWGChannelReport.h"
#include "SWGSuccessResponse.h"
#include "SWGErrorResponse.h"
WebAPIRequestMapper::WebAPIRequestMapper(QObject* parent) :
HttpRequestHandler(parent),
m_adapter(0)
{
qtwebapp::HttpDocrootSettings docrootSettings;
docrootSettings.path = ":/webapi";
m_staticFileController = new qtwebapp::StaticFileController(docrootSettings, parent);
}
WebAPIRequestMapper::~WebAPIRequestMapper()
{
delete m_staticFileController;
}
void WebAPIRequestMapper::service(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
if (m_adapter == 0) // format service unavailable if adapter is null
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
response.setStatus(500,"Service not available");
errorResponse.init();
*errorResponse.getMessage() = "Service not available";
response.write(errorResponse.asJson().toUtf8());
}
else // normal processing
{
QByteArray path=request.getPath();
if (path == WebAPIAdapterInterface::instanceSummaryURL) {
instanceSummaryService(request, response);
} else if (path == WebAPIAdapterInterface::instanceDevicesURL) {
instanceDevicesService(request, response);
} else if (path == WebAPIAdapterInterface::instanceChannelsURL) {
instanceChannelsService(request, response);
} else if (path == WebAPIAdapterInterface::instanceLoggingURL) {
instanceLoggingService(request, response);
} else if (path == WebAPIAdapterInterface::instanceAudioURL) {
instanceAudioService(request, response);
} else if (path == WebAPIAdapterInterface::instanceAudioInputParametersURL) {
instanceAudioInputParametersService(request, response);
} else if (path == WebAPIAdapterInterface::instanceAudioOutputParametersURL) {
instanceAudioOutputParametersService(request, response);
} else if (path == WebAPIAdapterInterface::instanceAudioInputCleanupURL) {
instanceAudioInputCleanupService(request, response);
} else if (path == WebAPIAdapterInterface::instanceAudioOutputCleanupURL) {
instanceAudioOutputCleanupService(request, response);
} else if (path == WebAPIAdapterInterface::instanceLocationURL) {
instanceLocationService(request, response);
} else if (path == WebAPIAdapterInterface::instanceDVSerialURL) {
instanceDVSerialService(request, response);
} else if (path == WebAPIAdapterInterface::instancePresetsURL) {
instancePresetsService(request, response);
} else if (path == WebAPIAdapterInterface::instancePresetURL) {
instancePresetService(request, response);
} else if (path == WebAPIAdapterInterface::instancePresetFileURL) {
instancePresetFileService(request, response);
} else if (path == WebAPIAdapterInterface::instanceDeviceSetsURL) {
instanceDeviceSetsService(request, response);
} else if (path == WebAPIAdapterInterface::instanceDeviceSetURL) {
instanceDeviceSetService(request, response);
}
else
{
std::smatch desc_match;
std::string pathStr(path.constData(), path.length());
if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetURLRe)) {
devicesetService(std::string(desc_match[1]), request, response);
} else if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetDeviceURLRe)) {
devicesetDeviceService(std::string(desc_match[1]), request, response);
} else if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetFocusURLRe)) {
devicesetFocusService(std::string(desc_match[1]), request, response);
} else if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetDeviceSettingsURLRe)) {
devicesetDeviceSettingsService(std::string(desc_match[1]), request, response);
} else if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetDeviceRunURLRe)) {
devicesetDeviceRunService(std::string(desc_match[1]), request, response);
} else if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetChannelsReportURLRe)) {
devicesetChannelsReportService(std::string(desc_match[1]), request, response);
} else if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetChannelURLRe)) {
devicesetChannelService(std::string(desc_match[1]), request, response);
} else if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetChannelIndexURLRe)) {
devicesetChannelIndexService(std::string(desc_match[1]), std::string(desc_match[2]), request, response);
} else if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetChannelSettingsURLRe)) {
devicesetChannelSettingsService(std::string(desc_match[1]), std::string(desc_match[2]), request, response);
} else if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetChannelReportURLRe)) {
devicesetChannelReportService(std::string(desc_match[1]), std::string(desc_match[2]), request, response);
}
else // serve static documentation pages
{
m_staticFileController->service(request, response);
}
// QDirIterator it(":", QDirIterator::Subdirectories);
// while (it.hasNext()) {
// qDebug() << "WebAPIRequestMapper::service: " << it.next();
// }
}
}
}
void WebAPIRequestMapper::instanceSummaryService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
if (request.getMethod() == "GET")
{
SWGSDRangel::SWGInstanceSummaryResponse normalResponse;
int status = m_adapter->instanceSummary(normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else if (request.getMethod() == "DELETE")
{
SWGSDRangel::SWGSuccessResponse normalResponse;
int status = m_adapter->instanceDelete(normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
void WebAPIRequestMapper::instanceDevicesService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGInstanceDevicesResponse normalResponse;
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
if (request.getMethod() == "GET")
{
QByteArray txStr = request.getParameter("tx");
bool tx = false;
if (txStr.length() != 0) {
tx = !(txStr == "0");
}
int status = m_adapter->instanceDevices(tx, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
void WebAPIRequestMapper::instanceChannelsService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGInstanceChannelsResponse normalResponse;
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
if (request.getMethod() == "GET")
{
QByteArray txStr = request.getParameter("tx");
bool tx = false;
if (txStr.length() != 0) {
tx = !(txStr == "0");
}
int status = m_adapter->instanceChannels(tx, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
void WebAPIRequestMapper::instanceLoggingService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGLoggingInfo query;
SWGSDRangel::SWGLoggingInfo normalResponse;
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
if (request.getMethod() == "GET")
{
int status = m_adapter->instanceLoggingGet(normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else if (request.getMethod() == "PUT")
{
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, jsonObject, response))
{
query.fromJson(jsonStr);
int status = m_adapter->instanceLoggingPut(query, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(400,"Invalid JSON format");
errorResponse.init();
*errorResponse.getMessage() = "Invalid JSON format";
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
void WebAPIRequestMapper::instanceAudioService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
if (request.getMethod() == "GET")
{
SWGSDRangel::SWGAudioDevices normalResponse;
int status = m_adapter->instanceAudioGet(normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
void WebAPIRequestMapper::instanceAudioInputParametersService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
// TODO
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, jsonObject, response))
{
SWGSDRangel::SWGAudioInputDevice normalResponse;
resetAudioInputDevice(normalResponse);
QStringList audioInputDeviceKeys;
if (validateAudioInputDevice(normalResponse, jsonObject, audioInputDeviceKeys))
{
if (request.getMethod() == "PATCH")
{
int status = m_adapter->instanceAudioInputPatch(
normalResponse,
audioInputDeviceKeys,
errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else if (request.getMethod() == "DELETE")
{
int status = m_adapter->instanceAudioInputDelete(
normalResponse,
errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
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
{
response.setStatus(400,"Invalid JSON format");
errorResponse.init();
*errorResponse.getMessage() = "Invalid JSON format";
response.write(errorResponse.asJson().toUtf8());
}
}
void WebAPIRequestMapper::instanceAudioOutputParametersService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, jsonObject, response))
{
SWGSDRangel::SWGAudioOutputDevice normalResponse;
resetAudioOutputDevice(normalResponse);
QStringList audioOutputDeviceKeys;
if (validateAudioOutputDevice(normalResponse, jsonObject, audioOutputDeviceKeys))
{
if (request.getMethod() == "PATCH")
{
int status = m_adapter->instanceAudioOutputPatch(
normalResponse,
audioOutputDeviceKeys,
errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else if (request.getMethod() == "DELETE")
{
int status = m_adapter->instanceAudioOutputDelete(
normalResponse,
errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
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
{
response.setStatus(400,"Invalid JSON format");
errorResponse.init();
*errorResponse.getMessage() = "Invalid JSON format";
response.write(errorResponse.asJson().toUtf8());
}
}
void WebAPIRequestMapper::instanceAudioInputCleanupService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
if (request.getMethod() == "PATCH")
{
SWGSDRangel::SWGSuccessResponse normalResponse;
int status = m_adapter->instanceAudioInputCleanupPatch(normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
void WebAPIRequestMapper::instanceAudioOutputCleanupService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
if (request.getMethod() == "PATCH")
{
SWGSDRangel::SWGSuccessResponse normalResponse;
int status = m_adapter->instanceAudioOutputCleanupPatch(normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
void WebAPIRequestMapper::instanceLocationService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
if (request.getMethod() == "GET")
{
SWGSDRangel::SWGLocationInformation normalResponse;
int status = m_adapter->instanceLocationGet(normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else if (request.getMethod() == "PUT")
{
SWGSDRangel::SWGLocationInformation normalResponse;
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, jsonObject, response))
{
normalResponse.fromJson(jsonStr);
int status = m_adapter->instanceLocationPut(normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(400,"Invalid JSON format");
errorResponse.init();
*errorResponse.getMessage() = "Invalid JSON format";
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
void WebAPIRequestMapper::instanceDVSerialService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
if (request.getMethod() == "PATCH")
{
QByteArray dvserialStr = request.getParameter("dvserial");
bool dvserial = false;
if (dvserialStr.length() != 0) {
dvserial = !(dvserialStr == "0");
}
SWGSDRangel::SWGDVSeralDevices normalResponse;
int status = m_adapter->instanceDVSerialPatch(dvserial, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
void WebAPIRequestMapper::instancePresetsService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
if (request.getMethod() == "GET")
{
SWGSDRangel::SWGPresets normalResponse;
int status = m_adapter->instancePresetsGet(normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
}
void WebAPIRequestMapper::instancePresetService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
if (request.getMethod() == "PATCH")
{
SWGSDRangel::SWGPresetTransfer query;
SWGSDRangel::SWGPresetIdentifier normalResponse;
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, jsonObject, response))
{
query.fromJson(jsonStr);
if (validatePresetTransfer(query))
{
int status = m_adapter->instancePresetPatch(query, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
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
{
response.setStatus(400,"Invalid JSON format");
errorResponse.init();
*errorResponse.getMessage() = "Invalid JSON format";
response.write(errorResponse.asJson().toUtf8());
}
}
else if (request.getMethod() == "PUT")
{
SWGSDRangel::SWGPresetTransfer query;
SWGSDRangel::SWGPresetIdentifier normalResponse;
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, jsonObject, response))
{
query.fromJson(jsonStr);
if (validatePresetTransfer(query))
{
int status = m_adapter->instancePresetPut(query, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
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
{
response.setStatus(400,"Invalid JSON format");
errorResponse.init();
*errorResponse.getMessage() = "Invalid JSON format";
response.write(errorResponse.asJson().toUtf8());
}
}
else if (request.getMethod() == "POST")
{
SWGSDRangel::SWGPresetTransfer query;
SWGSDRangel::SWGPresetIdentifier normalResponse;
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, jsonObject, response))
{
query.fromJson(jsonStr);
if (validatePresetTransfer(query))
{
int status = m_adapter->instancePresetPost(query, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
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
{
response.setStatus(400,"Invalid JSON format");
errorResponse.init();
*errorResponse.getMessage() = "Invalid JSON format";
response.write(errorResponse.asJson().toUtf8());
}
}
else if (request.getMethod() == "DELETE")
{
SWGSDRangel::SWGPresetIdentifier normalResponse;
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, jsonObject, response))
{
normalResponse.fromJson(jsonStr);
if (validatePresetIdentifer(normalResponse))
{
int status = m_adapter->instancePresetDelete(normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
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
{
response.setStatus(400,"Invalid JSON format");
errorResponse.init();
*errorResponse.getMessage() = "Invalid JSON format";
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
void WebAPIRequestMapper::instancePresetFileService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
if (request.getMethod() == "PUT")
{
SWGSDRangel::SWGPresetImport query;
SWGSDRangel::SWGPresetIdentifier normalResponse;
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, jsonObject, response))
{
query.fromJson(jsonStr);
if (query.getFilePath())
{
int status = m_adapter->instancePresetFilePut(query, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
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
{
response.setStatus(400,"Invalid JSON format");
errorResponse.init();
*errorResponse.getMessage() = "Invalid JSON format";
response.write(errorResponse.asJson().toUtf8());
}
}
else if (request.getMethod() == "POST")
{
SWGSDRangel::SWGPresetExport query;
SWGSDRangel::SWGPresetIdentifier normalResponse;
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, jsonObject, response))
{
query.fromJson(jsonStr);
if (validatePresetExport(query))
{
int status = m_adapter->instancePresetFilePost(query, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
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
{
response.setStatus(400,"Invalid JSON format");
errorResponse.init();
*errorResponse.getMessage() = "Invalid JSON format";
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
void WebAPIRequestMapper::instanceDeviceSetsService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
if (request.getMethod() == "GET")
{
SWGSDRangel::SWGDeviceSetList normalResponse;
int status = m_adapter->instanceDeviceSetsGet(normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
void WebAPIRequestMapper::instanceDeviceSetService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
if (request.getMethod() == "POST")
{
SWGSDRangel::SWGSuccessResponse normalResponse;
QByteArray txStr = request.getParameter("tx");
bool tx = false;
if (txStr.length() != 0) {
tx = !(txStr == "0");
}
int status = m_adapter->instanceDeviceSetPost(tx, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else if (request.getMethod() == "DELETE")
{
SWGSDRangel::SWGSuccessResponse normalResponse;
int status = m_adapter->instanceDeviceSetDelete(normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
void WebAPIRequestMapper::devicesetService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
if (request.getMethod() == "GET")
{
try
{
SWGSDRangel::SWGDeviceSet normalResponse;
int deviceSetIndex = boost::lexical_cast(indexStr);
int status = m_adapter->devicesetGet(deviceSetIndex, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
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());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
void WebAPIRequestMapper::devicesetFocusService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
try
{
int deviceSetIndex = boost::lexical_cast(indexStr);
if (request.getMethod() == "PATCH")
{
SWGSDRangel::SWGSuccessResponse normalResponse;
int status = m_adapter->devicesetFocusPatch(deviceSetIndex, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
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());
}
}
void WebAPIRequestMapper::devicesetDeviceService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
try
{
int deviceSetIndex = boost::lexical_cast(indexStr);
if (request.getMethod() == "PUT")
{
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, jsonObject, response))
{
SWGSDRangel::SWGDeviceListItem query;
SWGSDRangel::SWGDeviceListItem normalResponse;
if (validateDeviceListItem(query, jsonObject))
{
int status = m_adapter->devicesetDevicePut(deviceSetIndex, query, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(400,"Missing device identification");
errorResponse.init();
*errorResponse.getMessage() = "Missing device identification";
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(400,"Invalid JSON format");
errorResponse.init();
*errorResponse.getMessage() = "Invalid JSON format";
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
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());
}
}
void WebAPIRequestMapper::devicesetDeviceSettingsService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
try
{
int deviceSetIndex = boost::lexical_cast(indexStr);
if ((request.getMethod() == "PUT") || (request.getMethod() == "PATCH"))
{
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, jsonObject, response))
{
SWGSDRangel::SWGDeviceSettings normalResponse;
resetDeviceSettings(normalResponse);
QStringList deviceSettingsKeys;
if (validateDeviceSettings(normalResponse, jsonObject, deviceSettingsKeys))
{
int status = m_adapter->devicesetDeviceSettingsPutPatch(
deviceSetIndex,
(request.getMethod() == "PUT"), // force settings on PUT
deviceSettingsKeys,
normalResponse,
errorResponse);
response.setStatus(status);
if (status/100 == 2) {
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
{
response.setStatus(400,"Invalid JSON format");
errorResponse.init();
*errorResponse.getMessage() = "Invalid JSON format";
response.write(errorResponse.asJson().toUtf8());
}
}
else if (request.getMethod() == "GET")
{
SWGSDRangel::SWGDeviceSettings normalResponse;
resetDeviceSettings(normalResponse);
int status = m_adapter->devicesetDeviceSettingsGet(deviceSetIndex, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
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());
}
}
void WebAPIRequestMapper::devicesetDeviceRunService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
try
{
int deviceSetIndex = boost::lexical_cast(indexStr);
if (request.getMethod() == "GET")
{
SWGSDRangel::SWGDeviceState normalResponse;
int status = m_adapter->devicesetDeviceRunGet(deviceSetIndex, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else if (request.getMethod() == "POST")
{
SWGSDRangel::SWGDeviceState normalResponse;
int status = m_adapter->devicesetDeviceRunPost(deviceSetIndex, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else if (request.getMethod() == "DELETE")
{
SWGSDRangel::SWGDeviceState normalResponse;
int status = m_adapter->devicesetDeviceRunDelete(deviceSetIndex, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
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());
}
}
void WebAPIRequestMapper::devicesetChannelsReportService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
if (request.getMethod() == "GET")
{
try
{
SWGSDRangel::SWGChannelsDetail normalResponse;
int deviceSetIndex = boost::lexical_cast(indexStr);
int status = m_adapter->devicesetChannelsReportGet(deviceSetIndex, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
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());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
void WebAPIRequestMapper::devicesetChannelService(
const std::string& deviceSetIndexStr,
qtwebapp::HttpRequest& request,
qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
try
{
int deviceSetIndex = boost::lexical_cast(deviceSetIndexStr);
if (request.getMethod() == "POST")
{
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, jsonObject, response))
{
SWGSDRangel::SWGChannelSettings query;
SWGSDRangel::SWGSuccessResponse normalResponse;
resetChannelSettings(query);
if (jsonObject.contains("tx")) {
query.setTx(jsonObject["tx"].toInt());
} else {
query.setTx(0); // assume Rx
}
if (jsonObject.contains("channelType") && jsonObject["channelType"].isString())
{
query.setChannelType(new QString(jsonObject["channelType"].toString()));
int status = m_adapter->devicesetChannelPost(deviceSetIndex, query, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
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
{
response.setStatus(400,"Invalid JSON format");
errorResponse.init();
*errorResponse.getMessage() = "Invalid JSON format";
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
catch (const boost::bad_lexical_cast &e)
{
errorResponse.init();
*errorResponse.getMessage() = "Wrong integer conversion on index";
response.setStatus(400,"Invalid data");
response.write(errorResponse.asJson().toUtf8());
}
}
void WebAPIRequestMapper::devicesetChannelIndexService(
const std::string& deviceSetIndexStr,
const std::string& channelIndexStr,
qtwebapp::HttpRequest& request,
qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
try
{
int deviceSetIndex = boost::lexical_cast(deviceSetIndexStr);
int channelIndex = boost::lexical_cast(channelIndexStr);
if (request.getMethod() == "DELETE")
{
SWGSDRangel::SWGSuccessResponse normalResponse;
int status = m_adapter->devicesetChannelDelete(deviceSetIndex, channelIndex, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
catch (const boost::bad_lexical_cast &e)
{
errorResponse.init();
*errorResponse.getMessage() = "Wrong integer conversion on index";
response.setStatus(400,"Invalid data");
response.write(errorResponse.asJson().toUtf8());
}
}
void WebAPIRequestMapper::devicesetChannelSettingsService(
const std::string& deviceSetIndexStr,
const std::string& channelIndexStr,
qtwebapp::HttpRequest& request,
qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
try
{
int deviceSetIndex = boost::lexical_cast(deviceSetIndexStr);
int channelIndex = boost::lexical_cast(channelIndexStr);
if (request.getMethod() == "GET")
{
SWGSDRangel::SWGChannelSettings normalResponse;
resetChannelSettings(normalResponse);
int status = m_adapter->devicesetChannelSettingsGet(deviceSetIndex, channelIndex, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
else if ((request.getMethod() == "PUT") || (request.getMethod() == "PATCH"))
{
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, jsonObject, response))
{
SWGSDRangel::SWGChannelSettings normalResponse;
resetChannelSettings(normalResponse);
QStringList channelSettingsKeys;
if (validateChannelSettings(normalResponse, jsonObject, channelSettingsKeys))
{
int status = m_adapter->devicesetChannelSettingsPutPatch(
deviceSetIndex,
channelIndex,
(request.getMethod() == "PUT"), // force settings on PUT
channelSettingsKeys,
normalResponse,
errorResponse);
response.setStatus(status);
if (status/100 == 2) {
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
{
response.setStatus(400,"Invalid JSON format");
errorResponse.init();
*errorResponse.getMessage() = "Invalid JSON format";
response.write(errorResponse.asJson().toUtf8());
}
}
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
catch (const boost::bad_lexical_cast &e)
{
errorResponse.init();
*errorResponse.getMessage() = "Wrong integer conversion on index";
response.setStatus(400,"Invalid data");
response.write(errorResponse.asJson().toUtf8());
}
}
void WebAPIRequestMapper::devicesetChannelReportService(
const std::string& deviceSetIndexStr,
const std::string& channelIndexStr,
qtwebapp::HttpRequest& request,
qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
response.setHeader("Content-Type", "application/json");
try
{
int deviceSetIndex = boost::lexical_cast(deviceSetIndexStr);
int channelIndex = boost::lexical_cast(channelIndexStr);
if (request.getMethod() == "GET")
{
SWGSDRangel::SWGChannelReport normalResponse;
resetChannelReport(normalResponse);
int status = m_adapter->devicesetChannelReportGet(deviceSetIndex, channelIndex, normalResponse, errorResponse);
response.setStatus(status);
if (status/100 == 2) {
response.write(normalResponse.asJson().toUtf8());
} else {
response.write(errorResponse.asJson().toUtf8());
}
}
}
catch (const boost::bad_lexical_cast &e)
{
errorResponse.init();
*errorResponse.getMessage() = "Wrong integer conversion on index";
response.setStatus(400,"Invalid data");
response.write(errorResponse.asJson().toUtf8());
}
}
bool WebAPIRequestMapper::parseJsonBody(QString& jsonStr, QJsonObject& jsonObject, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
try
{
QByteArray jsonBytes(jsonStr.toStdString().c_str());
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(jsonBytes, &error);
if (error.error == QJsonParseError::NoError)
{
jsonObject = doc.object();
}
else
{
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());
response.write(errorResponse.asJson().toUtf8());
}
return (error.error == QJsonParseError::NoError);
}
catch (const std::exception& ex)
{
QString errorMsg = QString("Error parsing request: ") + ex.what();
errorResponse.init();
*errorResponse.getMessage() = errorMsg;
response.setStatus(500, errorMsg.toUtf8());
response.write(errorResponse.asJson().toUtf8());
return false;
}
}
bool WebAPIRequestMapper::validatePresetTransfer(SWGSDRangel::SWGPresetTransfer& presetTransfer)
{
SWGSDRangel::SWGPresetIdentifier *presetIdentifier = presetTransfer.getPreset();
if (presetIdentifier == 0) {
return false;
}
return validatePresetIdentifer(*presetIdentifier);
}
bool WebAPIRequestMapper::validatePresetIdentifer(SWGSDRangel::SWGPresetIdentifier& presetIdentifier)
{
return (presetIdentifier.getGroupName() && presetIdentifier.getName() && presetIdentifier.getType());
}
bool WebAPIRequestMapper::validatePresetExport(SWGSDRangel::SWGPresetExport& presetExport)
{
if (presetExport.getFilePath() == 0) {
return false;
}
SWGSDRangel::SWGPresetIdentifier *presetIdentifier = presetExport.getPreset();
if (presetIdentifier == 0) {
return false;
}
return validatePresetIdentifer(*presetIdentifier);
}
bool WebAPIRequestMapper::validateDeviceListItem(SWGSDRangel::SWGDeviceListItem& deviceListItem, QJsonObject& jsonObject)
{
if (jsonObject.contains("tx")) {
deviceListItem.setTx(jsonObject["tx"].toInt());
} else {
deviceListItem.setTx(0); // assume Rx
}
bool identified = false;
if (jsonObject.contains("displayedName") && jsonObject["displayedName"].isString())
{
deviceListItem.setDisplayedName(new QString(jsonObject["displayedName"].toString()));
identified = true;
} else {
deviceListItem.setDisplayedName(0);
}
if (jsonObject.contains("hwType") && jsonObject["hwType"].isString())
{
deviceListItem.setHwType(new QString(jsonObject["hwType"].toString()));
identified = true;
} else {
deviceListItem.setHwType(0);
}
if (jsonObject.contains("serial") && jsonObject["serial"].isString())
{
deviceListItem.setSerial(new QString(jsonObject["serial"].toString()));
identified = true;
} else {
deviceListItem.setSerial(0);
}
if (jsonObject.contains("index")) {
deviceListItem.setIndex(jsonObject["index"].toInt(-1));
} else {
deviceListItem.setIndex(-1);
}
if (jsonObject.contains("sequence")){
deviceListItem.setSequence(jsonObject["sequence"].toInt(-1));
} else {
deviceListItem.setSequence(-1);
}
if (jsonObject.contains("streamIndex")) {
deviceListItem.setStreamIndex(jsonObject["streamIndex"].toInt(-1));
} else {
deviceListItem.setStreamIndex(-1);
}
return identified;
}
bool WebAPIRequestMapper::validateDeviceSettings(
SWGSDRangel::SWGDeviceSettings& deviceSettings,
QJsonObject& jsonObject,
QStringList& deviceSettingsKeys)
{
if (jsonObject.contains("tx")) {
deviceSettings.setTx(jsonObject["tx"].toInt());
} else {
deviceSettings.setTx(0); // assume Rx
}
if (jsonObject.contains("deviceHwType") && jsonObject["deviceHwType"].isString()) {
deviceSettings.setDeviceHwType(new QString(jsonObject["deviceHwType"].toString()));
} else {
return false;
}
QString *deviceHwType = deviceSettings.getDeviceHwType();
if (*deviceHwType == "FileSource")
{
if (jsonObject.contains("fileSourceSettings") && jsonObject["fileSourceSettings"].isObject())
{
QJsonObject fileSourceSettingsJsonObject = jsonObject["fileSourceSettings"].toObject();
deviceSettingsKeys = fileSourceSettingsJsonObject.keys();
deviceSettings.setFileSourceSettings(new SWGSDRangel::SWGFileSourceSettings());
deviceSettings.getFileSourceSettings()->fromJsonObject(fileSourceSettingsJsonObject);
return true;
}
else
{
return false;
}
}
else if ((*deviceHwType == "AirspyHF") && (deviceSettings.getTx() == 0))
{
if (jsonObject.contains("airspyHFSettings") && jsonObject["airspyHFSettings"].isObject())
{
QJsonObject airspyHFSettingsJsonObject = jsonObject["airspyHFSettings"].toObject();
deviceSettingsKeys = airspyHFSettingsJsonObject.keys();
deviceSettings.setAirspyHfSettings(new SWGSDRangel::SWGAirspyHFSettings());
deviceSettings.getAirspyHfSettings()->fromJsonObject(airspyHFSettingsJsonObject);
return true;
}
else
{
return false;
}
}
else if ((*deviceHwType == "BladeRF") && (deviceSettings.getTx() == 0))
{
if (jsonObject.contains("bladeRFInputSettings") && jsonObject["bladeRFInputSettings"].isObject())
{
QJsonObject bladeRFInputSettingsJsonObject = jsonObject["bladeRFInputSettings"].toObject();
deviceSettingsKeys = bladeRFInputSettingsJsonObject.keys();
deviceSettings.setBladeRfInputSettings(new SWGSDRangel::SWGBladeRFInputSettings());
deviceSettings.getBladeRfInputSettings()->fromJsonObject(bladeRFInputSettingsJsonObject);
return true;
}
else
{
return false;
}
}
else if ((*deviceHwType == "BladeRF") && (deviceSettings.getTx() != 0))
{
if (jsonObject.contains("bladeRFOutputSettings") && jsonObject["bladeRFOutputSettings"].isObject())
{
QJsonObject bladeRFOutputSettingsJsonObject = jsonObject["bladeRFOutputSettings"].toObject();
deviceSettingsKeys = bladeRFOutputSettingsJsonObject.keys();
deviceSettings.setBladeRfOutputSettings(new SWGSDRangel::SWGBladeRFOutputSettings());
deviceSettings.getBladeRfOutputSettings()->fromJsonObject(bladeRFOutputSettingsJsonObject);
return true;
}
else
{
return false;
}
}
else if ((*deviceHwType == "HackRF") && (deviceSettings.getTx() == 0))
{
if (jsonObject.contains("hackRFInputSettings") && jsonObject["hackRFInputSettings"].isObject())
{
QJsonObject hackRFInputSettingsJsonObject = jsonObject["hackRFInputSettings"].toObject();
deviceSettingsKeys = hackRFInputSettingsJsonObject.keys();
deviceSettings.setHackRfInputSettings(new SWGSDRangel::SWGHackRFInputSettings());
deviceSettings.getHackRfInputSettings()->fromJsonObject(hackRFInputSettingsJsonObject);
return true;
}
else
{
return false;
}
}
else if ((*deviceHwType == "HackRF") && (deviceSettings.getTx() != 0))
{
if (jsonObject.contains("hackRFOutputSettings") && jsonObject["hackRFOutputSettings"].isObject())
{
QJsonObject hackRFOutputSettingsJsonObject = jsonObject["hackRFOutputSettings"].toObject();
deviceSettingsKeys = hackRFOutputSettingsJsonObject.keys();
deviceSettings.setHackRfOutputSettings(new SWGSDRangel::SWGHackRFOutputSettings());
deviceSettings.getHackRfOutputSettings()->fromJsonObject(hackRFOutputSettingsJsonObject);
return true;
}
else
{
return false;
}
}
else if ((*deviceHwType == "LimeSDR") && (deviceSettings.getTx() == 0))
{
if (jsonObject.contains("limeSdrInputSettings") && jsonObject["limeSdrInputSettings"].isObject())
{
QJsonObject limeSdrInputSettingsJsonObject = jsonObject["limeSdrInputSettings"].toObject();
deviceSettingsKeys = limeSdrInputSettingsJsonObject.keys();
deviceSettings.setLimeSdrInputSettings(new SWGSDRangel::SWGLimeSdrInputSettings());
deviceSettings.getLimeSdrInputSettings()->fromJsonObject(limeSdrInputSettingsJsonObject);
return true;
}
else
{
return false;
}
}
else if ((*deviceHwType == "LimeSDR") && (deviceSettings.getTx() != 0))
{
if (jsonObject.contains("limeSdrOutputSettings") && jsonObject["limeSdrOutputSettings"].isObject())
{
QJsonObject limeSdrOutputSettingsJsonObject = jsonObject["limeSdrOutputSettings"].toObject();
deviceSettingsKeys = limeSdrOutputSettingsJsonObject.keys();
deviceSettings.setLimeSdrOutputSettings(new SWGSDRangel::SWGLimeSdrOutputSettings());
deviceSettings.getLimeSdrOutputSettings()->fromJsonObject(limeSdrOutputSettingsJsonObject);
return true;
}
else
{
return false;
}
}
else if (*deviceHwType == "RTLSDR")
{
if (jsonObject.contains("rtlSdrSettings") && jsonObject["rtlSdrSettings"].isObject())
{
QJsonObject rtlSdrSettingsJsonObject = jsonObject["rtlSdrSettings"].toObject();
deviceSettingsKeys = rtlSdrSettingsJsonObject.keys();
deviceSettings.setRtlSdrSettings(new SWGSDRangel::SWGRtlSdrSettings());
deviceSettings.getRtlSdrSettings()->fromJsonObject(rtlSdrSettingsJsonObject);
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
bool WebAPIRequestMapper::validateChannelSettings(
SWGSDRangel::SWGChannelSettings& channelSettings,
QJsonObject& jsonObject,
QStringList& channelSettingsKeys)
{
if (jsonObject.contains("tx")) {
channelSettings.setTx(jsonObject["tx"].toInt());
} else {
channelSettings.setTx(0); // assume Rx
}
if (jsonObject.contains("channelType") && jsonObject["channelType"].isString()) {
channelSettings.setChannelType(new QString(jsonObject["channelType"].toString()));
} else {
return false;
}
QString *channelType = channelSettings.getChannelType();
if (*channelType == "AMDemod")
{
if (channelSettings.getTx() == 0)
{
QJsonObject amDemodSettingsJsonObject = jsonObject["AMDemodSettings"].toObject();
channelSettingsKeys = amDemodSettingsJsonObject.keys();
channelSettings.setAmDemodSettings(new SWGSDRangel::SWGAMDemodSettings());
channelSettings.getAmDemodSettings()->fromJsonObject(amDemodSettingsJsonObject);
return true;
}
else {
return false;
}
}
else if (*channelType == "AMMod")
{
if (channelSettings.getTx() != 0)
{
QJsonObject amModSettingsJsonObject = jsonObject["AMModSettings"].toObject();
channelSettingsKeys = amModSettingsJsonObject.keys();
if (channelSettingsKeys.contains("cwKeyer"))
{
QJsonObject cwKeyerSettingsJsonObject;
appendSettingsSubKeys(amModSettingsJsonObject, cwKeyerSettingsJsonObject, "cwKeyer", channelSettingsKeys);
}
channelSettings.setAmModSettings(new SWGSDRangel::SWGAMModSettings());
channelSettings.getAmModSettings()->fromJsonObject(amModSettingsJsonObject);
return true;
}
else {
return false;
}
}
else if (*channelType == "ATVMod")
{
if (channelSettings.getTx() != 0)
{
QJsonObject atvModSettingsJsonObject = jsonObject["ATVModSettings"].toObject();
channelSettingsKeys = atvModSettingsJsonObject.keys();
channelSettings.setAtvModSettings(new SWGSDRangel::SWGATVModSettings());
channelSettings.getAtvModSettings()->fromJsonObject(atvModSettingsJsonObject);
return true;
}
else {
return false;
}
}
else if (*channelType == "NFMDemod")
{
if (channelSettings.getTx() == 0)
{
QJsonObject nfmDemodSettingsJsonObject = jsonObject["NFMDemodSettings"].toObject();
channelSettingsKeys = nfmDemodSettingsJsonObject.keys();
channelSettings.setNfmDemodSettings(new SWGSDRangel::SWGNFMDemodSettings());
channelSettings.getNfmDemodSettings()->fromJsonObject(nfmDemodSettingsJsonObject);
return true;
}
else {
return false;
}
}
else if (*channelType == "NFMMod")
{
if (channelSettings.getTx() != 0)
{
QJsonObject nfmModSettingsJsonObject = jsonObject["NFMModSettings"].toObject();
channelSettingsKeys = nfmModSettingsJsonObject.keys();
if (channelSettingsKeys.contains("cwKeyer"))
{
QJsonObject cwKeyerSettingsJsonObject;
appendSettingsSubKeys(nfmModSettingsJsonObject, cwKeyerSettingsJsonObject, "cwKeyer", channelSettingsKeys);
}
channelSettings.setNfmModSettings(new SWGSDRangel::SWGNFMModSettings());
channelSettings.getNfmModSettings()->fromJsonObject(nfmModSettingsJsonObject);
return true;
}
else {
return false;
}
}
else if (*channelType == "SSBMod")
{
if (channelSettings.getTx() != 0)
{
QJsonObject ssbModSettingsJsonObject = jsonObject["SSBModSettings"].toObject();
channelSettingsKeys = ssbModSettingsJsonObject.keys();
if (channelSettingsKeys.contains("cwKeyer"))
{
QJsonObject cwKeyerSettingsJsonObject;
appendSettingsSubKeys(ssbModSettingsJsonObject, cwKeyerSettingsJsonObject, "cwKeyer", channelSettingsKeys);
}
channelSettings.setSsbModSettings(new SWGSDRangel::SWGSSBModSettings());
channelSettings.getSsbModSettings()->fromJsonObject(ssbModSettingsJsonObject);
return true;
}
else {
return false;
}
}
else if (*channelType == "UDPSink")
{
if (channelSettings.getTx() != 0)
{
QJsonObject udpSinkSettingsJsonObject = jsonObject["UDPSinkSettings"].toObject();
channelSettingsKeys = udpSinkSettingsJsonObject.keys();
channelSettings.setUdpSinkSettings(new SWGSDRangel::SWGUDPSinkSettings());
channelSettings.getUdpSinkSettings()->fromJsonObject(udpSinkSettingsJsonObject);
return true;
}
else {
return false;
}
}
else if (*channelType == "WFMMod")
{
if (channelSettings.getTx() != 0)
{
QJsonObject wfmModSettingsJsonObject = jsonObject["WFMModSettings"].toObject();
channelSettingsKeys = wfmModSettingsJsonObject.keys();
if (channelSettingsKeys.contains("cwKeyer"))
{
QJsonObject cwKeyerSettingsJsonObject;
appendSettingsSubKeys(wfmModSettingsJsonObject, cwKeyerSettingsJsonObject, "cwKeyer", channelSettingsKeys);
}
channelSettings.setWfmModSettings(new SWGSDRangel::SWGWFMModSettings());
channelSettings.getWfmModSettings()->fromJsonObject(wfmModSettingsJsonObject);
return true;
}
else {
return false;
}
}
else
{
return false;
}
}
bool WebAPIRequestMapper::validateChannelReport(
SWGSDRangel::SWGChannelReport& channelReport,
QJsonObject& jsonObject,
QStringList& channelReportKeys)
{
if (jsonObject.contains("tx")) {
channelReport.setTx(jsonObject["tx"].toInt());
} else {
channelReport.setTx(0); // assume Rx
}
if (jsonObject.contains("channelType") && jsonObject["channelType"].isString()) {
channelReport.setChannelType(new QString(jsonObject["channelType"].toString()));
} else {
return false;
}
QString *channelType = channelReport.getChannelType();
if (*channelType == "AMDemod")
{
if (channelReport.getTx() == 0)
{
QJsonObject amDemodReportJsonObject = jsonObject["AMDemodReport"].toObject();
channelReportKeys = amDemodReportJsonObject.keys();
channelReport.setAmDemodReport(new SWGSDRangel::SWGAMDemodReport());
channelReport.getAmDemodReport()->fromJsonObject(amDemodReportJsonObject);
return true;
}
else {
return false;
}
}
else if (*channelType == "AMMod")
{
if (channelReport.getTx() != 0)
{
QJsonObject amModReportJsonObject = jsonObject["AMModReport"].toObject();
channelReportKeys = amModReportJsonObject.keys();
channelReport.setAmModReport(new SWGSDRangel::SWGAMModReport());
channelReport.getAmModReport()->fromJsonObject(amModReportJsonObject);
return true;
}
else {
return false;
}
}
else if (*channelType == "ATVMod")
{
if (channelReport.getTx() != 0)
{
QJsonObject atvModReportJsonObject = jsonObject["ATVModReport"].toObject();
channelReportKeys = atvModReportJsonObject.keys();
channelReport.setAtvModReport(new SWGSDRangel::SWGATVModReport());
channelReport.getAtvModReport()->fromJsonObject(atvModReportJsonObject);
return true;
}
else {
return false;
}
}
else if (*channelType == "NFMDemod")
{
if (channelReport.getTx() == 0)
{
QJsonObject nfmDemodReportJsonObject = jsonObject["NFMDemodReport"].toObject();
channelReportKeys = nfmDemodReportJsonObject.keys();
channelReport.setNfmDemodReport(new SWGSDRangel::SWGNFMDemodReport());
channelReport.getNfmDemodReport()->fromJsonObject(nfmDemodReportJsonObject);
return true;
}
else {
return false;
}
}
else if (*channelType == "NFMMod")
{
if (channelReport.getTx() != 0)
{
QJsonObject nfmModReportJsonObject = jsonObject["NFMModReport"].toObject();
channelReportKeys = nfmModReportJsonObject.keys();
channelReport.setNfmModReport(new SWGSDRangel::SWGNFMModReport());
channelReport.getNfmModReport()->fromJsonObject(nfmModReportJsonObject);
return true;
}
else {
return false;
}
}
else if (*channelType == "SSBMod")
{
if (channelReport.getTx() != 0)
{
QJsonObject ssbModReportJsonObject = jsonObject["SSBModReport"].toObject();
channelReportKeys = ssbModReportJsonObject.keys();
channelReport.setSsbModReport(new SWGSDRangel::SWGSSBModReport());
channelReport.getSsbModReport()->fromJsonObject(ssbModReportJsonObject);
return true;
}
else {
return false;
}
}
else if (*channelType == "UDPSink")
{
if (channelReport.getTx() != 0)
{
QJsonObject udpSinkReportJsonObject = jsonObject["UDPSinkReport"].toObject();
channelReportKeys = udpSinkReportJsonObject.keys();
channelReport.setUdpSinkReport(new SWGSDRangel::SWGUDPSinkReport());
channelReport.getUdpSinkReport()->fromJsonObject(udpSinkReportJsonObject);
return true;
}
else {
return false;
}
}
else if (*channelType == "WFMMod")
{
if (channelReport.getTx() != 0)
{
QJsonObject wfmModReportJsonObject = jsonObject["WFMModReport"].toObject();
channelReportKeys = wfmModReportJsonObject.keys();
channelReport.setWfmModReport(new SWGSDRangel::SWGWFMModReport());
channelReport.getWfmModReport()->fromJsonObject(wfmModReportJsonObject);
return true;
}
else {
return false;
}
}
else
{
return false;
}
}
bool WebAPIRequestMapper::validateAudioInputDevice(
SWGSDRangel::SWGAudioInputDevice& audioInputDevice,
QJsonObject& jsonObject,
QStringList& audioInputDeviceKeys)
{
if (jsonObject.contains("index")) {
audioInputDevice.setIndex(jsonObject["index"].toInt());
} else {
audioInputDevice.setIndex(-1); // assume systam default
}
if (jsonObject.contains("sampleRate"))
{
audioInputDevice.setSampleRate(jsonObject["sampleRate"].toInt());
audioInputDeviceKeys.append("sampleRate");
}
if (jsonObject.contains("volume"))
{
audioInputDevice.setVolume(jsonObject["volume"].toDouble());
audioInputDeviceKeys.append("volume");
}
return true;
}
bool WebAPIRequestMapper::validateAudioOutputDevice(
SWGSDRangel::SWGAudioOutputDevice& audioOutputDevice,
QJsonObject& jsonObject,
QStringList& audioOutputDeviceKeys)
{
if (jsonObject.contains("index")) {
audioOutputDevice.setIndex(jsonObject["index"].toInt());
} else {
audioOutputDevice.setIndex(-1); // assume systam default
}
if (jsonObject.contains("sampleRate"))
{
audioOutputDevice.setSampleRate(jsonObject["sampleRate"].toInt());
audioOutputDeviceKeys.append("sampleRate");
}
if (jsonObject.contains("copyToUDP"))
{
audioOutputDevice.setCopyToUdp(jsonObject["copyToUDP"].toInt() == 0 ? 0 : 1);
audioOutputDeviceKeys.append("copyToUDP");
}
if (jsonObject.contains("udpUsesRTP"))
{
audioOutputDevice.setUdpUsesRtp(jsonObject["udpUsesRTP"].toInt() == 0 ? 0 : 1);
audioOutputDeviceKeys.append("udpUsesRTP");
}
if (jsonObject.contains("udpChannelMode"))
{
audioOutputDevice.setUdpChannelMode(jsonObject["udpChannelMode"].toInt());
audioOutputDeviceKeys.append("udpChannelMode");
}
if (jsonObject.contains("udpAddress"))
{
audioOutputDevice.setUdpAddress(new QString(jsonObject["udpAddress"].toString()));
audioOutputDeviceKeys.append("udpAddress");
}
if (jsonObject.contains("udpPort"))
{
audioOutputDevice.setUdpPort(jsonObject["udpPort"].toInt());
audioOutputDeviceKeys.append("udpPort");
}
return true;
}
void WebAPIRequestMapper::appendSettingsSubKeys(
const QJsonObject& parentSettingsJsonObject,
QJsonObject& childSettingsJsonObject,
const QString& parentKey,
QStringList& keyList)
{
childSettingsJsonObject = parentSettingsJsonObject[parentKey].toObject();
QStringList childSettingsKeys = childSettingsJsonObject.keys();
for (int i = 0; i < childSettingsKeys.size(); i++) {
keyList.append(parentKey + QString(".") + childSettingsKeys.at(i));
}
}
void WebAPIRequestMapper::resetDeviceSettings(SWGSDRangel::SWGDeviceSettings& deviceSettings)
{
deviceSettings.cleanup();
deviceSettings.setDeviceHwType(0);
deviceSettings.setFileSourceSettings(0);
deviceSettings.setHackRfInputSettings(0);
deviceSettings.setHackRfOutputSettings(0);
deviceSettings.setLimeSdrInputSettings(0);
deviceSettings.setLimeSdrOutputSettings(0);
deviceSettings.setRtlSdrSettings(0);
}
void WebAPIRequestMapper::resetChannelSettings(SWGSDRangel::SWGChannelSettings& channelSettings)
{
channelSettings.cleanup();
channelSettings.setChannelType(0);
channelSettings.setNfmDemodSettings(0);
channelSettings.setNfmModSettings(0);
}
void WebAPIRequestMapper::resetChannelReport(SWGSDRangel::SWGChannelReport& channelReport)
{
channelReport.cleanup();
channelReport.setChannelType(0);
channelReport.setNfmDemodReport(0);
channelReport.setNfmModReport(0);
}
void WebAPIRequestMapper::resetAudioInputDevice(SWGSDRangel::SWGAudioInputDevice& audioInputDevice)
{
audioInputDevice.cleanup();
audioInputDevice.setName(0);
}
void WebAPIRequestMapper::resetAudioOutputDevice(SWGSDRangel::SWGAudioOutputDevice& audioOutputDevice)
{
audioOutputDevice.cleanup();
audioOutputDevice.setName(0);
audioOutputDevice.setUdpAddress(0);
}