1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-12-23 01:55:48 -05:00

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

This commit is contained in:
f4exb 2017-12-07 18:50:36 +01:00
parent c38497d9df
commit 2cf797a0a2
6 changed files with 159 additions and 34 deletions

39
.gitignore vendored
View File

@ -1,19 +1,20 @@
CMakeLists.txt.user*
build*
qtbuild/*
sdriq/*
presets/*
LOCAL/*
sdrangelove.supp
.cproject
.project
.settings/
*.cs
*.pro.user
.idea/*
debian/sdrangel/*
debian/sdrangel.substvars
debian/files
debian/sdrangel.debhelper.log
debian/debhelper-build-stamp
obj-x86_64-linux-gnu/*
CMakeLists.txt.user*
build*
qtbuild/*
sdriq/*
presets/*
LOCAL/*
sdrangelove.supp
.cproject
.project
.pydevproject
.settings/
*.cs
*.pro.user
.idea/*
debian/sdrangel/*
debian/sdrangel.substvars
debian/files
debian/sdrangel.debhelper.log
debian/debhelper-build-stamp
obj-x86_64-linux-gnu/*

View File

@ -488,3 +488,39 @@ int RTLSDRInput::webapiSettingsGet(
return 200;
}
int RTLSDRInput::webapiSettingsPutPatch(
bool force,
SWGSDRangel::SWGDeviceSettings& response, // query + response
QString& errorMessage)
{
RTLSDRSettings settings;
settings.m_agc = response.getRtlSdrSettings()->getAgc() != 0;
settings.m_centerFrequency = response.getRtlSdrSettings()->getCenterFrequency();
settings.m_dcBlock = response.getRtlSdrSettings()->getDcBlock() != 0;
settings.m_devSampleRate = response.getRtlSdrSettings()->getDevSampleRate();
settings.m_fcPos = (RTLSDRSettings::fcPos_t) response.getRtlSdrSettings()->getFcPos();
settings.m_gain = response.getRtlSdrSettings()->getGain();
settings.m_iqImbalance = response.getRtlSdrSettings()->getIqImbalance() != 0;
settings.m_loPpmCorrection = response.getRtlSdrSettings()->getLoPpmCorrection();
settings.m_log2Decim = response.getRtlSdrSettings()->getLog2Decim();
settings.m_lowSampleRate = response.getRtlSdrSettings()->getLowSampleRate() != 0;
settings.m_noModMode = response.getRtlSdrSettings()->getNoModMode() != 0;
settings.m_transverterDeltaFrequency = response.getRtlSdrSettings()->getTransverterDeltaFrequency();
settings.m_transverterMode = response.getRtlSdrSettings()->getTransverterMode() != 0;
if (applySettings(settings, force))
{
if (m_guiMessageQueue) // forward to GUI if any
{
MsgConfigureRTLSDR *msg = MsgConfigureRTLSDR::create(settings, force);
m_guiMessageQueue->push(msg);
}
return 200;
}
else
{
errorMessage = "RTLSDRInput::webapiSettingsPutPatch: error applying settings";
return 500;
}
}

View File

@ -110,6 +110,11 @@ public:
SWGSDRangel::SWGDeviceSettings& response,
QString& errorMessage);
virtual int webapiSettingsPutPatch(
bool force,
SWGSDRangel::SWGDeviceSettings& response, // query + response
QString& errorMessage);
const std::vector<int>& getGains() const { return m_gains; }
void set_ds_mode(int on);

View File

@ -210,8 +210,9 @@ void WebAPIRequestMapper::instanceLoggingService(qtwebapp::HttpRequest& request,
else if (request.getMethod() == "PUT")
{
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, response))
if (parseJsonBody(jsonStr, jsonObject, response))
{
normalResponse.fromJson(jsonStr);
int status = m_adapter->instanceLoggingPut(normalResponse, errorResponse);
@ -252,8 +253,9 @@ void WebAPIRequestMapper::instanceAudioService(qtwebapp::HttpRequest& request, q
{
SWGSDRangel::SWGAudioDevicesSelect normalResponse;
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, response))
if (parseJsonBody(jsonStr, jsonObject, response))
{
normalResponse.fromJson(jsonStr);
int status = m_adapter->instanceAudioPatch(normalResponse, errorResponse);
@ -294,8 +296,9 @@ void WebAPIRequestMapper::instanceLocationService(qtwebapp::HttpRequest& request
{
SWGSDRangel::SWGLocationInformation normalResponse;
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, response))
if (parseJsonBody(jsonStr, jsonObject, response))
{
normalResponse.fromJson(jsonStr);
int status = m_adapter->instanceLocationPut(normalResponse, errorResponse);
@ -367,8 +370,9 @@ void WebAPIRequestMapper::instancePresetService(qtwebapp::HttpRequest& request,
SWGSDRangel::SWGPresetTransfer query;
SWGSDRangel::SWGPresetIdentifier normalResponse;
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, response))
if (parseJsonBody(jsonStr, jsonObject, response))
{
query.fromJson(jsonStr);
@ -397,8 +401,9 @@ void WebAPIRequestMapper::instancePresetService(qtwebapp::HttpRequest& request,
SWGSDRangel::SWGPresetTransfer query;
SWGSDRangel::SWGPresetIdentifier normalResponse;
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, response))
if (parseJsonBody(jsonStr, jsonObject, response))
{
query.fromJson(jsonStr);
@ -427,8 +432,9 @@ void WebAPIRequestMapper::instancePresetService(qtwebapp::HttpRequest& request,
SWGSDRangel::SWGPresetTransfer query;
SWGSDRangel::SWGPresetIdentifier normalResponse;
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, response))
if (parseJsonBody(jsonStr, jsonObject, response))
{
query.fromJson(jsonStr);
@ -456,8 +462,9 @@ void WebAPIRequestMapper::instancePresetService(qtwebapp::HttpRequest& request,
{
SWGSDRangel::SWGPresetIdentifier normalResponse;
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, response))
if (parseJsonBody(jsonStr, jsonObject, response))
{
normalResponse.fromJson(jsonStr);
@ -588,8 +595,9 @@ void WebAPIRequestMapper::devicesetDeviceService(const std::string& indexStr, qt
{
SWGSDRangel::SWGDeviceListItem normalResponse;
QString jsonStr = request.getBody();
QJsonObject jsonObject;
if (parseJsonBody(jsonStr, response))
if (parseJsonBody(jsonStr, jsonObject, response))
{
normalResponse.fromJson(jsonStr);
@ -629,11 +637,13 @@ void WebAPIRequestMapper::devicesetDeviceSettingsService(const std::string& inde
if ((request.getMethod() == "PUT") || (request.getMethod() == "PATCH"))
{
QString jsonStr = request.getBody();
QJsonObject jsonObject;
qDebug("WebAPIRequestMapper::devicesetDeviceSettingsService: %s", qPrintable(jsonStr));
if (parseJsonBody(jsonStr, response))
if (parseJsonBody(jsonStr, jsonObject, response))
{
SWGSDRangel::SWGDeviceSettings normalResponse;
resetDeviceSettings(normalResponse);
//resetDeviceSettings(normalResponse);
normalResponse.fromJson(jsonStr);
if (validateDeviceSettings(normalResponse))
@ -688,7 +698,7 @@ void WebAPIRequestMapper::devicesetDeviceSettingsService(const std::string& inde
}
}
bool WebAPIRequestMapper::parseJsonBody(QString& jsonStr, qtwebapp::HttpResponse& response)
bool WebAPIRequestMapper::parseJsonBody(QString& jsonStr, QJsonObject& jsonObject, qtwebapp::HttpResponse& response)
{
SWGSDRangel::SWGErrorResponse errorResponse;
@ -698,7 +708,11 @@ bool WebAPIRequestMapper::parseJsonBody(QString& jsonStr, qtwebapp::HttpResponse
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(jsonBytes, &error);
if (error.error != QJsonParseError::NoError)
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();
@ -737,7 +751,7 @@ bool WebAPIRequestMapper::validatePresetIdentifer(SWGSDRangel::SWGPresetIdentifi
return (presetIdentifier.getGroupName() && presetIdentifier.getName() && presetIdentifier.getType());
}
bool WebAPIRequestMapper::validateDeviceSettings(SWGSDRangel::SWGDeviceSettings& deviceSettings)
bool WebAPIRequestMapper::validateDeviceSettings(SWGSDRangel::SWGDeviceSettings& deviceSettings, QJsonObject& jsonObject)
{
QString *deviceHwType = deviceSettings.getDeviceHwType();

View File

@ -61,9 +61,9 @@ private:
bool validatePresetTransfer(SWGSDRangel::SWGPresetTransfer& presetTransfer);
bool validatePresetIdentifer(SWGSDRangel::SWGPresetIdentifier& presetIdentifier);
bool validateDeviceSettings(SWGSDRangel::SWGDeviceSettings& deviceSettings);
bool validateDeviceSettings(SWGSDRangel::SWGDeviceSettings& deviceSettings, QJsonObject& jsonObject);
bool parseJsonBody(QString& jsonStr, qtwebapp::HttpResponse& response);
bool parseJsonBody(QString& jsonStr, QJsonObject& jsonObject, qtwebapp::HttpResponse& response);
void resetDeviceSettings(SWGSDRangel::SWGDeviceSettings& deviceSettings);
};

View File

@ -0,0 +1,69 @@
#!/usr/bin/env python
import requests, json
base_url = "http://10.0.2.2:8091/sdrangel"
requests_methods = {
"GET": requests.get,
"PATCH": requests.patch,
"POST": requests.post,
"PUT": requests.put,
"DELETE": requests.delete
}
def getHwType():
r = requests.get(url=base_url+"/deviceset/0")
if r.status_code == 200:
rj = r.json()
devj = rj.get('samplingDevice', None)
if devj is not None:
return devj.get('hwType' ,None)
else:
return None
else:
return None
def selectRtlSdr():
r = requests.put(url=base_url+"/deviceset/0/device", json={"hwType": "RTLSDR"})
if r.status_code == 200:
print json.dumps(r.json(), indent=4, sort_keys=True)
return True
else:
return False
def getRtlSdrSettings():
r = requests.get(url=base_url+"/deviceset/0/device/settings")
if r.status_code == 200:
rj = r.json()
hwType = rj.get('deviceHwType', None)
if hwType is not None and hwType == "RTLSDR":
settings = rj.get('rtlSdrSettings', None)
return settings
else:
return None
else:
return None
def patchRtlSdrSettings(settings):
new_settings = {"deviceHwType": "RTLSDR", "tx": 0, "rtlSdrSettings": settings}
r = requests.patch(url=base_url+"/deviceset/0/device/settings", json=new_settings)
if r.status_code == 200:
print json.dumps(r.json(), indent=4, sort_keys=True)
else:
print "Error HTTP:", r.status_code
def main():
hwType = getHwType()
if hwType and hwType != "RTLSDR":
if not selectRtlSdr():
return
settings = getRtlSdrSettings()
if settings is not None:
settings["agc"] = 1
patchRtlSdrSettings(settings)
if __name__ == "__main__":
main()