2017-11-16 21:17:15 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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 <http://www.gnu.org/licenses/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-11-21 18:28:26 -05:00
|
|
|
//#include <QDirIterator>
|
2017-11-23 18:47:42 -05:00
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonArray>
|
2017-11-21 18:28:26 -05:00
|
|
|
|
2017-11-26 04:37:39 -05:00
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
|
2017-11-20 12:38:26 -05:00
|
|
|
#include "httpdocrootsettings.h"
|
2017-11-16 21:17:15 -05:00
|
|
|
#include "webapirequestmapper.h"
|
2017-11-18 13:34:47 -05:00
|
|
|
#include "SWGInstanceSummaryResponse.h"
|
2017-11-22 12:57:35 -05:00
|
|
|
#include "SWGInstanceDevicesResponse.h"
|
2017-11-22 19:19:32 -05:00
|
|
|
#include "SWGInstanceChannelsResponse.h"
|
2017-11-24 02:46:12 -05:00
|
|
|
#include "SWGAudioDevices.h"
|
|
|
|
#include "SWGAudioDevicesSelect.h"
|
2017-11-24 22:02:11 -05:00
|
|
|
#include "SWGLocationInformation.h"
|
2017-11-24 22:43:22 -05:00
|
|
|
#include "SWGDVSeralDevices.h"
|
2017-11-25 05:14:52 -05:00
|
|
|
#include "SWGPresets.h"
|
2017-11-25 10:08:18 -05:00
|
|
|
#include "SWGPresetTransfer.h"
|
|
|
|
#include "SWGPresetIdentifier.h"
|
2017-11-18 13:34:47 -05:00
|
|
|
#include "SWGErrorResponse.h"
|
2017-11-16 21:17:15 -05:00
|
|
|
|
|
|
|
WebAPIRequestMapper::WebAPIRequestMapper(QObject* parent) :
|
|
|
|
HttpRequestHandler(parent),
|
|
|
|
m_adapter(0)
|
2017-11-20 12:38:26 -05:00
|
|
|
{
|
|
|
|
qtwebapp::HttpDocrootSettings docrootSettings;
|
|
|
|
docrootSettings.path = ":/";
|
|
|
|
m_staticFileController = new qtwebapp::StaticFileController(docrootSettings, parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
WebAPIRequestMapper::~WebAPIRequestMapper()
|
|
|
|
{
|
|
|
|
delete m_staticFileController;
|
|
|
|
}
|
2017-11-16 21:17:15 -05:00
|
|
|
|
2017-11-18 13:34:47 -05:00
|
|
|
void WebAPIRequestMapper::service(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
|
2017-11-16 21:17:15 -05:00
|
|
|
{
|
|
|
|
if (m_adapter == 0) // format service unavailable if adapter is null
|
|
|
|
{
|
2017-11-17 02:52:15 -05:00
|
|
|
response.setStatus(500,"Service not available");
|
2017-11-23 18:47:42 -05:00
|
|
|
response.write("Service not available");
|
2017-11-16 21:17:15 -05:00
|
|
|
}
|
|
|
|
else // normal processing
|
|
|
|
{
|
|
|
|
QByteArray path=request.getPath();
|
|
|
|
|
2017-11-24 11:12:53 -05:00
|
|
|
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);
|
2017-11-24 22:02:11 -05:00
|
|
|
} else if (path == WebAPIAdapterInterface::instanceLocationURL) {
|
|
|
|
instanceLocationService(request, response);
|
2017-11-24 22:43:22 -05:00
|
|
|
} else if (path == WebAPIAdapterInterface::instanceDVSerialURL) {
|
|
|
|
instanceDVSerialService(request, response);
|
2017-11-25 05:14:52 -05:00
|
|
|
} else if (path == WebAPIAdapterInterface::instancePresetURL) {
|
|
|
|
instancePresetService(request, response);
|
2017-11-25 14:14:16 -05:00
|
|
|
} else if (path == WebAPIAdapterInterface::instanceDeviceSetsURL) {
|
|
|
|
instanceDeviceSetsService(request, response);
|
2017-11-16 21:17:15 -05:00
|
|
|
}
|
2017-11-24 11:12:53 -05:00
|
|
|
else
|
2017-11-22 12:57:35 -05:00
|
|
|
{
|
2017-11-26 04:37:39 -05:00
|
|
|
std::smatch desc_match;
|
|
|
|
std::string pathStr(path.constData(), path.length());
|
|
|
|
|
|
|
|
if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetURLRe)) {
|
|
|
|
deviceset(std::string(desc_match[1]), request, response);
|
2017-11-27 02:14:07 -05:00
|
|
|
} else if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetDeviceURLRe)) {
|
|
|
|
devicesetDevice(std::string(desc_match[1]), request, response);
|
2017-11-26 04:37:39 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QByteArray path = "/index.html";
|
|
|
|
m_staticFileController->service(path, response);
|
|
|
|
}
|
|
|
|
|
2017-11-24 11:12:53 -05:00
|
|
|
// QDirIterator it(":", QDirIterator::Subdirectories);
|
|
|
|
// while (it.hasNext()) {
|
|
|
|
// qDebug() << "WebAPIRequestMapper::service: " << it.next();
|
|
|
|
// }
|
|
|
|
|
2017-11-22 12:57:35 -05:00
|
|
|
}
|
2017-11-24 11:12:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebAPIRequestMapper::instanceSummaryService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
|
|
|
|
{
|
|
|
|
if (request.getMethod() == "GET")
|
|
|
|
{
|
|
|
|
Swagger::SWGInstanceSummaryResponse normalResponse;
|
|
|
|
Swagger::SWGErrorResponse errorResponse;
|
|
|
|
|
|
|
|
int status = m_adapter->instanceSummary(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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebAPIRequestMapper::instanceDevicesService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
|
|
|
|
{
|
|
|
|
Swagger::SWGInstanceDevicesResponse normalResponse;
|
|
|
|
Swagger::SWGErrorResponse errorResponse;
|
|
|
|
|
|
|
|
if (request.getMethod() == "GET")
|
|
|
|
{
|
|
|
|
QByteArray txStr = request.getParameter("tx");
|
2017-11-24 22:43:22 -05:00
|
|
|
bool tx = false;
|
|
|
|
|
|
|
|
if (txStr.length() != 0) {
|
|
|
|
tx = !(txStr == "0");
|
|
|
|
}
|
2017-11-24 11:12:53 -05:00
|
|
|
|
|
|
|
int status = m_adapter->instanceDevices(tx, normalResponse, errorResponse);
|
|
|
|
response.setStatus(status);
|
|
|
|
|
|
|
|
if (status == 200) {
|
|
|
|
response.write(normalResponse.asJson().toUtf8());
|
|
|
|
} else {
|
|
|
|
response.write(errorResponse.asJson().toUtf8());
|
2017-11-22 19:19:32 -05:00
|
|
|
}
|
2017-11-24 11:12:53 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response.setStatus(405,"Invalid HTTP method");
|
|
|
|
response.write("Invalid HTTP method");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebAPIRequestMapper::instanceChannelsService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
|
|
|
|
{
|
|
|
|
Swagger::SWGInstanceChannelsResponse normalResponse;
|
|
|
|
Swagger::SWGErrorResponse errorResponse;
|
|
|
|
|
|
|
|
if (request.getMethod() == "GET")
|
|
|
|
{
|
|
|
|
QByteArray txStr = request.getParameter("tx");
|
2017-11-24 22:43:22 -05:00
|
|
|
bool tx = false;
|
|
|
|
|
|
|
|
if (txStr.length() != 0) {
|
|
|
|
tx = !(txStr == "0");
|
|
|
|
}
|
2017-11-24 11:12:53 -05:00
|
|
|
|
|
|
|
int status = m_adapter->instanceChannels(tx, 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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebAPIRequestMapper::instanceLoggingService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
|
|
|
|
{
|
|
|
|
Swagger::SWGLoggingInfo normalResponse;
|
|
|
|
Swagger::SWGErrorResponse errorResponse;
|
|
|
|
|
|
|
|
if (request.getMethod() == "GET")
|
|
|
|
{
|
|
|
|
int status = m_adapter->instanceLoggingGet(normalResponse, errorResponse);
|
|
|
|
response.setStatus(status);
|
|
|
|
|
|
|
|
if (status == 200) {
|
|
|
|
response.write(normalResponse.asJson().toUtf8());
|
|
|
|
} else {
|
|
|
|
response.write(errorResponse.asJson().toUtf8());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (request.getMethod() == "PUT")
|
|
|
|
{
|
|
|
|
QString jsonStr = request.getBody();
|
|
|
|
|
|
|
|
if (parseJsonBody(jsonStr, response))
|
2017-11-23 12:43:01 -05:00
|
|
|
{
|
2017-11-24 11:12:53 -05:00
|
|
|
normalResponse.fromJson(jsonStr);
|
|
|
|
int status = m_adapter->instanceLoggingPut(normalResponse, errorResponse);
|
|
|
|
response.setStatus(status);
|
|
|
|
|
|
|
|
if (status == 200) {
|
|
|
|
response.write(normalResponse.asJson().toUtf8());
|
|
|
|
} else {
|
|
|
|
response.write(errorResponse.asJson().toUtf8());
|
2017-11-23 12:43:01 -05:00
|
|
|
}
|
|
|
|
}
|
2017-11-24 11:12:53 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response.setStatus(405,"Invalid HTTP method");
|
|
|
|
response.write("Invalid HTTP method");
|
|
|
|
}
|
|
|
|
}
|
2017-11-24 02:46:12 -05:00
|
|
|
|
2017-11-24 11:12:53 -05:00
|
|
|
void WebAPIRequestMapper::instanceAudioService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
|
|
|
|
{
|
|
|
|
Swagger::SWGErrorResponse errorResponse;
|
2017-11-24 02:46:12 -05:00
|
|
|
|
2017-11-24 11:12:53 -05:00
|
|
|
if (request.getMethod() == "GET")
|
|
|
|
{
|
|
|
|
Swagger::SWGAudioDevices normalResponse;
|
2017-11-24 02:46:12 -05:00
|
|
|
|
2017-11-24 11:12:53 -05:00
|
|
|
int status = m_adapter->instanceAudioGet(normalResponse, errorResponse);
|
|
|
|
response.setStatus(status);
|
|
|
|
|
|
|
|
if (status == 200) {
|
|
|
|
response.write(normalResponse.asJson().toUtf8());
|
|
|
|
} else {
|
|
|
|
response.write(errorResponse.asJson().toUtf8());
|
2017-11-24 02:46:12 -05:00
|
|
|
}
|
2017-11-24 11:12:53 -05:00
|
|
|
}
|
|
|
|
else if (request.getMethod() == "PATCH")
|
|
|
|
{
|
|
|
|
Swagger::SWGAudioDevicesSelect normalResponse;
|
|
|
|
QString jsonStr = request.getBody();
|
|
|
|
|
|
|
|
if (parseJsonBody(jsonStr, response))
|
2017-11-17 02:52:15 -05:00
|
|
|
{
|
2017-11-24 11:12:53 -05:00
|
|
|
normalResponse.fromJson(jsonStr);
|
|
|
|
int status = m_adapter->instanceAudioPatch(normalResponse, errorResponse);
|
|
|
|
response.setStatus(status);
|
2017-11-21 18:28:26 -05:00
|
|
|
|
2017-11-24 11:12:53 -05:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:02:11 -05:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
2017-11-24 11:12:53 -05:00
|
|
|
|
2017-11-24 22:43:22 -05:00
|
|
|
void WebAPIRequestMapper::instanceDVSerialService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
|
|
|
|
{
|
|
|
|
Swagger::SWGErrorResponse errorResponse;
|
|
|
|
|
|
|
|
if (request.getMethod() == "PATCH")
|
|
|
|
{
|
|
|
|
QByteArray dvserialStr = request.getParameter("dvserial");
|
|
|
|
bool dvserial = false;
|
|
|
|
|
|
|
|
if (dvserialStr.length() != 0) {
|
|
|
|
dvserial = !(dvserialStr == "0");
|
|
|
|
}
|
|
|
|
|
|
|
|
Swagger::SWGDVSeralDevices normalResponse;
|
|
|
|
|
|
|
|
int status = m_adapter->instanceDVSerialPatch(dvserial, 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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 05:14:52 -05:00
|
|
|
void WebAPIRequestMapper::instancePresetService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
|
|
|
|
{
|
|
|
|
Swagger::SWGErrorResponse errorResponse;
|
|
|
|
|
|
|
|
if (request.getMethod() == "GET")
|
|
|
|
{
|
2017-11-25 10:08:18 -05:00
|
|
|
Swagger::SWGPresets normalResponse;
|
2017-11-25 05:14:52 -05:00
|
|
|
int status = m_adapter->instancePresetGet(normalResponse, errorResponse);
|
|
|
|
response.setStatus(status);
|
|
|
|
|
|
|
|
if (status == 200) {
|
|
|
|
response.write(normalResponse.asJson().toUtf8());
|
|
|
|
} else {
|
|
|
|
response.write(errorResponse.asJson().toUtf8());
|
|
|
|
}
|
|
|
|
}
|
2017-11-25 10:08:18 -05:00
|
|
|
else if (request.getMethod() == "PATCH")
|
|
|
|
{
|
|
|
|
Swagger::SWGPresetTransfer query;
|
|
|
|
Swagger::SWGPresetIdentifier normalResponse;
|
|
|
|
QString jsonStr = request.getBody();
|
|
|
|
|
|
|
|
if (parseJsonBody(jsonStr, response))
|
|
|
|
{
|
|
|
|
query.fromJson(jsonStr);
|
|
|
|
|
2017-11-25 13:42:56 -05:00
|
|
|
if (validatePresetTransfer(query))
|
|
|
|
{
|
|
|
|
int status = m_adapter->instancePresetPatch(query, 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";
|
2017-11-25 10:08:18 -05:00
|
|
|
response.write(errorResponse.asJson().toUtf8());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-25 10:47:13 -05:00
|
|
|
else if (request.getMethod() == "PUT")
|
|
|
|
{
|
|
|
|
Swagger::SWGPresetTransfer query;
|
|
|
|
Swagger::SWGPresetIdentifier normalResponse;
|
|
|
|
QString jsonStr = request.getBody();
|
|
|
|
|
|
|
|
if (parseJsonBody(jsonStr, response))
|
|
|
|
{
|
|
|
|
query.fromJson(jsonStr);
|
|
|
|
|
2017-11-25 13:42:56 -05:00
|
|
|
if (validatePresetTransfer(query))
|
|
|
|
{
|
|
|
|
int status = m_adapter->instancePresetPut(query, 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";
|
2017-11-25 10:47:13 -05:00
|
|
|
response.write(errorResponse.asJson().toUtf8());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-25 12:32:37 -05:00
|
|
|
else if (request.getMethod() == "POST")
|
|
|
|
{
|
|
|
|
Swagger::SWGPresetTransfer query;
|
|
|
|
Swagger::SWGPresetIdentifier normalResponse;
|
|
|
|
QString jsonStr = request.getBody();
|
|
|
|
|
|
|
|
if (parseJsonBody(jsonStr, response))
|
|
|
|
{
|
|
|
|
query.fromJson(jsonStr);
|
|
|
|
|
2017-11-25 13:42:56 -05:00
|
|
|
if (validatePresetTransfer(query))
|
|
|
|
{
|
|
|
|
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(400,"Invalid JSON request");
|
|
|
|
errorResponse.init();
|
|
|
|
*errorResponse.getMessage() = "Invalid JSON request";
|
|
|
|
response.write(errorResponse.asJson().toUtf8());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (request.getMethod() == "DELETE")
|
|
|
|
{
|
|
|
|
Swagger::SWGPresetIdentifier normalResponse;
|
|
|
|
QString jsonStr = request.getBody();
|
|
|
|
|
|
|
|
if (parseJsonBody(jsonStr, response))
|
|
|
|
{
|
|
|
|
normalResponse.fromJson(jsonStr);
|
|
|
|
|
|
|
|
if (validatePresetIdentifer(normalResponse))
|
|
|
|
{
|
|
|
|
int status = m_adapter->instancePresetDelete(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";
|
2017-11-25 12:32:37 -05:00
|
|
|
response.write(errorResponse.asJson().toUtf8());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-25 05:14:52 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
response.setStatus(405,"Invalid HTTP method");
|
|
|
|
response.write("Invalid HTTP method");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 14:14:16 -05:00
|
|
|
void WebAPIRequestMapper::instanceDeviceSetsService(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
|
|
|
|
{
|
|
|
|
Swagger::SWGErrorResponse errorResponse;
|
|
|
|
|
|
|
|
if (request.getMethod() == "GET")
|
|
|
|
{
|
|
|
|
Swagger::SWGDeviceSetList normalResponse;
|
|
|
|
int status = m_adapter->instanceDeviceSetsGet(normalResponse, errorResponse);
|
|
|
|
response.setStatus(status);
|
|
|
|
|
|
|
|
if (status == 200) {
|
|
|
|
response.write(normalResponse.asJson().toUtf8());
|
|
|
|
} else {
|
|
|
|
response.write(errorResponse.asJson().toUtf8());
|
|
|
|
}
|
|
|
|
}
|
2017-11-25 16:52:24 -05:00
|
|
|
else if (request.getMethod() == "POST")
|
|
|
|
{
|
|
|
|
Swagger::SWGDeviceSet normalResponse;
|
|
|
|
QByteArray txStr = request.getParameter("tx");
|
|
|
|
bool tx = false;
|
|
|
|
|
|
|
|
if (txStr.length() != 0) {
|
|
|
|
tx = !(txStr == "0");
|
|
|
|
}
|
|
|
|
|
|
|
|
int status = m_adapter->instanceDeviceSetsPost(tx, normalResponse, errorResponse);
|
|
|
|
response.setStatus(status);
|
|
|
|
|
|
|
|
if (status == 200) {
|
|
|
|
response.write(normalResponse.asJson().toUtf8());
|
|
|
|
} else {
|
|
|
|
response.write(errorResponse.asJson().toUtf8());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (request.getMethod() == "DELETE")
|
|
|
|
{
|
|
|
|
Swagger::SWGDeviceSetList normalResponse;
|
|
|
|
int status = m_adapter->instanceDeviceSetsDelete(normalResponse, errorResponse);
|
|
|
|
response.setStatus(status);
|
|
|
|
|
|
|
|
if (status == 200) {
|
|
|
|
response.write(normalResponse.asJson().toUtf8());
|
|
|
|
} else {
|
|
|
|
response.write(errorResponse.asJson().toUtf8());
|
|
|
|
}
|
|
|
|
}
|
2017-11-25 14:14:16 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
response.setStatus(405,"Invalid HTTP method");
|
|
|
|
response.write("Invalid HTTP method");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-26 04:37:39 -05:00
|
|
|
void WebAPIRequestMapper::deviceset(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response)
|
|
|
|
{
|
|
|
|
Swagger::SWGErrorResponse errorResponse;
|
|
|
|
|
|
|
|
if (request.getMethod() == "GET")
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Swagger::SWGDeviceSet normalResponse;
|
|
|
|
int deviceSetIndex = boost::lexical_cast<int>(indexStr);
|
|
|
|
int status = m_adapter->devicesetGet(deviceSetIndex, normalResponse, errorResponse);
|
|
|
|
response.setStatus(status);
|
|
|
|
|
|
|
|
if (status == 200) {
|
|
|
|
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");
|
|
|
|
response.write("Invalid HTTP method");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 02:14:07 -05:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 11:12:53 -05:00
|
|
|
bool WebAPIRequestMapper::parseJsonBody(QString& jsonStr, qtwebapp::HttpResponse& response)
|
|
|
|
{
|
|
|
|
Swagger::SWGErrorResponse errorResponse;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
QByteArray jsonBytes(jsonStr.toStdString().c_str());
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(jsonBytes, &error);
|
|
|
|
|
|
|
|
if (error.error != QJsonParseError::NoError)
|
|
|
|
{
|
2017-11-24 22:02:11 -05:00
|
|
|
QString errorMsg = QString("Input JSON error: ") + error.errorString() + QString(" at offset ") + QString::number(error.offset);
|
2017-11-24 11:12:53 -05:00
|
|
|
errorResponse.init();
|
|
|
|
*errorResponse.getMessage() = errorMsg;
|
|
|
|
response.setStatus(400, errorMsg.toUtf8());
|
|
|
|
response.write(errorResponse.asJson().toUtf8());
|
2017-11-17 02:52:15 -05:00
|
|
|
}
|
2017-11-24 11:12:53 -05:00
|
|
|
|
|
|
|
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;
|
2017-11-16 21:17:15 -05:00
|
|
|
}
|
|
|
|
}
|
2017-11-25 13:42:56 -05:00
|
|
|
|
|
|
|
bool WebAPIRequestMapper::validatePresetTransfer(Swagger::SWGPresetTransfer& presetTransfer)
|
|
|
|
{
|
|
|
|
Swagger::SWGPresetIdentifier *presetIdentifier = presetTransfer.getPreset();
|
|
|
|
|
|
|
|
if (presetIdentifier == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return validatePresetIdentifer(*presetIdentifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebAPIRequestMapper::validatePresetIdentifer(Swagger::SWGPresetIdentifier& presetIdentifier)
|
|
|
|
{
|
|
|
|
return (presetIdentifier.getGroupName() && presetIdentifier.getName() && presetIdentifier.getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
|