1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2026-07-23 02:24:16 -04:00

REST API: device and channel actions: fixes to implementation

This commit is contained in:
f4exb
2020-03-09 19:56:09 +01:00
parent c8b9c912a2
commit b317c0a59d
18 changed files with 329 additions and 60 deletions
+203 -34
View File
@@ -153,6 +153,10 @@ const QMap<QString, QString> WebAPIRequestMapper::m_channelTypeToSettingsKey = {
{"WFMMod", "WFMModSettings"}
};
const QMap<QString, QString> WebAPIRequestMapper::m_channelTypeToActionsKey = {
{"FileSource", "FileSourceActions"}
};
const QMap<QString, QString> WebAPIRequestMapper::m_sourceDeviceHwIdToSettingsKey = {
{"Airspy", "airspySettings"},
{"AirspyHF", "airspyHFSettings"},
@@ -175,6 +179,10 @@ const QMap<QString, QString> WebAPIRequestMapper::m_sourceDeviceHwIdToSettingsKe
{"XTRX", "XtrxInputSettings"}
};
const QMap<QString, QString> WebAPIRequestMapper::m_sourceDeviceHwIdToActionsKey = {
{"RTLSDR", "rtlSdrActions"}
};
const QMap<QString, QString> WebAPIRequestMapper::m_sinkDeviceHwIdToSettingsKey = {
{"BladeRF1", "bladeRF1OutputSettings"},
{"BladeRF2", "bladeRF2OutputSettings"},
@@ -184,7 +192,23 @@ const QMap<QString, QString> WebAPIRequestMapper::m_sinkDeviceHwIdToSettingsKey
{"PlutoSDR", "plutoSdrOutputSettings"},
{"RemoteOutput", "remoteOutputSettings"},
{"SoapySDR", "soapySDROutputSettings"},
<<<<<<< ours
{"XTRX", "XtrxOutputSettings"}
=======
{"XTRX", "xtrxOutputSettings"}
};
const QMap<QString, QString> WebAPIRequestMapper::m_sinkDeviceHwIdToActionsKey = {
};
const QMap<QString, QString> WebAPIRequestMapper::m_mimoDeviceHwIdToSettingsKey= {
{"BladeRF2", "bladeRF2MIMOSettings"},
{"TestMI", "testMISettings"},
{"TestMOSync", "testMOSyncSettings"}
>>>>>>> theirs
};
const QMap<QString, QString> WebAPIRequestMapper::m_mimoDeviceHwIdToActionsKey= {
};
WebAPIRequestMapper::WebAPIRequestMapper(QObject* parent) :
@@ -311,6 +335,8 @@ void WebAPIRequestMapper::service(qtwebapp::HttpRequest& request, qtwebapp::Http
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 if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetChannelActionsURLRe)) {
devicesetChannelActionsService(std::string(desc_match[1]), std::string(desc_match[2]), request, response);
}
else // serve static documentation pages
{
@@ -1922,9 +1948,11 @@ void WebAPIRequestMapper::devicesetDeviceActionsService(const std::string& index
response.setHeader("Content-Type", "application/json");
response.setHeader("Access-Control-Allow-Origin", "*");
if (request.getMethod() == "POST")
try
{
try
int deviceSetIndex = boost::lexical_cast<int>(indexStr);
if (request.getMethod() == "POST")
{
QString jsonStr = request.getBody();
QJsonObject jsonObject;
@@ -1933,19 +1961,17 @@ void WebAPIRequestMapper::devicesetDeviceActionsService(const std::string& index
{
SWGSDRangel::SWGDeviceActions query;
SWGSDRangel::SWGSuccessResponse normalResponse;
int deviceSetIndex = boost::lexical_cast<int>(indexStr);
resetDeviceActions(query);
QStringList deviceActionsKeys;
if (jsonObject.contains("direction")) {
query.setDirection(jsonObject["direction"].toInt());
} else {
query.setDirection(0); // assume Rx
}
if (jsonObject.contains("deviceHwType") && jsonObject["deviceHwType"].isString())
if (validateDeviceActions(query, jsonObject, deviceActionsKeys))
{
query.setDeviceHwType(new QString(jsonObject["deviceHwType"].toString()));
int status = m_adapter->devicesetDeviceActionsPost(deviceSetIndex, query, normalResponse, errorResponse);
int status = m_adapter->devicesetDeviceActionsPost(
deviceSetIndex,
deviceActionsKeys,
query,
normalResponse,
errorResponse);
response.setStatus(status);
if (status/100 == 2) {
@@ -1970,19 +1996,19 @@ void WebAPIRequestMapper::devicesetDeviceActionsService(const std::string& index
response.write(errorResponse.asJson().toUtf8());
}
}
catch (const boost::bad_lexical_cast &e)
else
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Wrong integer conversion on device set index";
response.setStatus(400,"Invalid data");
*errorResponse.getMessage() = "Invalid HTTP method";
response.write(errorResponse.asJson().toUtf8());
}
}
else
catch(const boost::bad_lexical_cast &e)
{
response.setStatus(405,"Invalid HTTP method");
errorResponse.init();
*errorResponse.getMessage() = "Invalid HTTP method";
*errorResponse.getMessage() = "Wrong integer conversion on device set index";
response.setStatus(400,"Invalid data");
response.write(errorResponse.asJson().toUtf8());
}
}
@@ -2281,7 +2307,7 @@ void WebAPIRequestMapper::devicesetChannelReportService(
}
}
void WebAPIRequestMapper::devicesetChannelActtionsService(
void WebAPIRequestMapper::devicesetChannelActionsService(
const std::string& deviceSetIndexStr,
const std::string& channelIndexStr,
qtwebapp::HttpRequest& request,
@@ -2306,17 +2332,17 @@ void WebAPIRequestMapper::devicesetChannelActtionsService(
SWGSDRangel::SWGChannelActions query;
SWGSDRangel::SWGSuccessResponse normalResponse;
resetChannelActions(query);
QStringList channelActionsKeys;
if (jsonObject.contains("direction")) {
query.setDirection(jsonObject["direction"].toInt());
} else {
query.setDirection(0); // assume Rx
}
if (jsonObject.contains("channelType") && jsonObject["channelType"].isString())
if (validateChannelActions(query, jsonObject, channelActionsKeys))
{
query.setChannelType(new QString(jsonObject["channelType"].toString()));
int status = m_adapter->devicesetChannelActionsPost(deviceSetIndex, channelIndex, query, normalResponse, errorResponse);
int status = m_adapter->devicesetChannelActionsPost(
deviceSetIndex,
channelIndex,
channelActionsKeys,
query,
normalResponse,
errorResponse);
response.setStatus(status);
if (status/100 == 2) {
@@ -2523,7 +2549,59 @@ bool WebAPIRequestMapper::validateDeviceSettings(
return false;
}
return getDevice(deviceSettingsKey, &deviceSettings, jsonObject, deviceSettingsKeys);
return getDeviceSettings(deviceSettingsKey, &deviceSettings, jsonObject, deviceSettingsKeys);
}
bool WebAPIRequestMapper::validateDeviceActions(
SWGSDRangel::SWGDeviceActions& deviceActions,
QJsonObject& jsonObject,
QStringList& deviceActionsKeys)
{
if (jsonObject.contains("direction")) {
deviceActions.setDirection(jsonObject["direction"].toInt());
} else {
deviceActions.setDirection(0); // assume single Rx
}
if (jsonObject.contains("deviceHwType") && jsonObject["deviceHwType"].isString()) {
deviceActions.setDeviceHwType(new QString(jsonObject["deviceHwType"].toString()));
} else {
return false;
}
QString *deviceHwType = deviceActions.getDeviceHwType();
QString deviceActionsKey;
if (deviceActions.getDirection() == 0) // source
{
if (m_sourceDeviceHwIdToSettingsKey.contains(*deviceHwType)) {
deviceActionsKey = m_sourceDeviceHwIdToActionsKey[*deviceHwType];
} else {
return false;
}
}
else if (deviceActions.getDirection() == 1) // sink
{
if (m_sinkDeviceHwIdToSettingsKey.contains(*deviceHwType)) {
deviceActionsKey = m_sinkDeviceHwIdToActionsKey[*deviceHwType];
} else {
return false;
}
}
else if (deviceActions.getDirection() == 2) // MIMO
{
if (m_mimoDeviceHwIdToSettingsKey.contains(*deviceHwType)) {
deviceActionsKey = m_mimoDeviceHwIdToActionsKey[*deviceHwType];
} else {
return false;
}
}
else
{
return false;
}
return getDeviceActions(deviceActionsKey, &deviceActions, jsonObject, deviceActionsKeys);
}
bool WebAPIRequestMapper::validateChannelSettings(
@@ -2546,7 +2624,33 @@ bool WebAPIRequestMapper::validateChannelSettings(
QString *channelType = channelSettings.getChannelType();
if (m_channelTypeToSettingsKey.contains(*channelType)) {
return getChannel(m_channelTypeToSettingsKey[*channelType], &channelSettings, jsonObject, channelSettingsKeys);
return getChannelSettings(m_channelTypeToSettingsKey[*channelType], &channelSettings, jsonObject, channelSettingsKeys);
} else {
return false;
}
}
bool WebAPIRequestMapper::validateChannelActions(
SWGSDRangel::SWGChannelActions& channelActions,
QJsonObject& jsonObject,
QStringList& channelActionsKeys)
{
if (jsonObject.contains("direction")) {
channelActions.setDirection(jsonObject["direction"].toInt());
} else {
channelActions.setDirection(0); // assume single Rx
}
if (jsonObject.contains("channelType") && jsonObject["channelType"].isString()) {
channelActions.setChannelType(new QString(jsonObject["channelType"].toString()));
} else {
return false;
}
QString *channelType = channelActions.getChannelType();
if (m_channelTypeToActionsKey.contains(*channelType)) {
return getChannelActions(m_channelTypeToActionsKey[*channelType], &channelActions, jsonObject, channelActionsKeys);
} else {
return false;
}
@@ -2936,7 +3040,7 @@ bool WebAPIRequestMapper::appendPresetChannelKeys(
{
SWGSDRangel::SWGChannelSettings *channelSettings = new SWGSDRangel::SWGChannelSettings();
channel->setConfig(channelSettings);
return getChannel(m_channelURIToSettingsKey[*channelURI], channelSettings, channelSettingsJson["config"].toObject(), channelKeys.m_channelKeys);
return getChannelSettings(m_channelURIToSettingsKey[*channelURI], channelSettings, channelSettingsJson["config"].toObject(), channelKeys.m_channelKeys);
}
else
{
@@ -2949,7 +3053,7 @@ bool WebAPIRequestMapper::appendPresetChannelKeys(
}
}
bool WebAPIRequestMapper::getChannel(
bool WebAPIRequestMapper::getChannelSettings(
const QString& channelSettingsKey,
SWGSDRangel::SWGChannelSettings *channelSettings,
const QJsonObject& channelSettingsJson,
@@ -3102,6 +3206,38 @@ bool WebAPIRequestMapper::getChannel(
}
}
bool WebAPIRequestMapper::getChannelActions(
const QString& channelActionsKey,
SWGSDRangel::SWGChannelActions *channelActions,
const QJsonObject& channelActionsJson,
QStringList& channelActionsKeys
)
{
QStringList channelKeys = channelActionsJson.keys();
if (channelKeys.contains(channelActionsKey) && channelActionsJson[channelActionsKey].isObject())
{
QJsonObject actionsJsonObject = channelActionsJson[channelActionsKey].toObject();
channelActionsKeys = actionsJsonObject.keys();
if (channelActionsKey == "FileSourceActions")
{
channelActions->setFileSourceActions(new SWGSDRangel::SWGFileSourceActions());
channelActions->getFileSourceActions()->fromJsonObject(actionsJsonObject);
}
else
{
return false;
}
return true;
}
else
{
return false;
}
}
bool WebAPIRequestMapper::appendPresetDeviceKeys(
SWGSDRangel::SWGDeviceConfig *device,
const QJsonObject& deviceSettngsJson,
@@ -3130,7 +3266,7 @@ bool WebAPIRequestMapper::appendPresetDeviceKeys(
{
SWGSDRangel::SWGDeviceSettings *deviceSettings = new SWGSDRangel::SWGDeviceSettings();
device->setConfig(deviceSettings);
return getDevice(m_deviceIdToSettingsKey[*deviceId], deviceSettings, deviceSettngsJson["config"].toObject(), devicelKeys.m_deviceKeys);
return getDeviceSettings(m_deviceIdToSettingsKey[*deviceId], deviceSettings, deviceSettngsJson["config"].toObject(), devicelKeys.m_deviceKeys);
}
else
{
@@ -3143,7 +3279,7 @@ bool WebAPIRequestMapper::appendPresetDeviceKeys(
}
}
bool WebAPIRequestMapper::getDevice(
bool WebAPIRequestMapper::getDeviceSettings(
const QString& deviceSettingsKey,
SWGSDRangel::SWGDeviceSettings *deviceSettings,
const QJsonObject& deviceSettingsJson,
@@ -3315,6 +3451,39 @@ bool WebAPIRequestMapper::getDevice(
}
bool WebAPIRequestMapper::getDeviceActions(
const QString& deviceActionsKey,
SWGSDRangel::SWGDeviceActions *deviceActions,
const QJsonObject& deviceActionsJson,
QStringList& deviceActionsKeys
)
{
QStringList deviceKeys = deviceActionsJson.keys();
if (deviceKeys.contains(deviceActionsKey) && deviceActionsJson[deviceActionsKey].isObject())
{
QJsonObject actionsJsonObject = deviceActionsJson[deviceActionsKey].toObject();
deviceActionsKeys = actionsJsonObject.keys();
if (deviceActionsKey == "rtlSdrActions")
{
deviceActions->setRtlSdrActions(new SWGSDRangel::SWGRtlSdrActions());
deviceActions->getRtlSdrActions()->fromJsonObject(actionsJsonObject);
}
else
{
return false;
}
return true;
}
else
{
return false;
}
}
void WebAPIRequestMapper::appendSettingsSubKeys(
const QJsonObject& parentSettingsJsonObject,
QJsonObject& childSettingsJsonObject,