1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-17 05:41:56 -05:00

REST API: config: PUT (6): mapper work and rework

This commit is contained in:
f4exb 2019-08-10 05:16:25 +02:00
parent c777ec881d
commit af6218611a
5 changed files with 148 additions and 30 deletions

View File

@ -224,7 +224,25 @@ void Preset::addOrUpdateDeviceConfig(const QString& sourceId,
}
}
const QByteArray* Preset::findBestDeviceConfig(const QString& sourceId,
const QByteArray* Preset::findDeviceConfig(
const QString& deviceId,
const QString& deviceSerial,
int deviceSequence) const
{
DeviceeConfigs::const_iterator it = m_deviceConfigs.begin();
for (; it != m_deviceConfigs.end(); ++it)
{
if ((it->m_deviceId == deviceId) && (it->m_deviceSerial == deviceSerial) && (it->m_deviceSerial == deviceSerial)) {
return &it->m_config;
}
}
return nullptr;
}
const QByteArray* Preset::findBestDeviceConfig(
const QString& sourceId,
const QString& sourceSerial,
int sourceSequence) const
{

View File

@ -79,7 +79,9 @@ public:
const QByteArray& getSpectrumConfig() const { return m_spectrumConfig; }
bool hasDCOffsetCorrection() const { return m_dcOffsetCorrection; }
void setDCOffsetCorrection(bool dcOffsetCorrection) { m_dcOffsetCorrection = dcOffsetCorrection; }
bool hasIQImbalanceCorrection() const { return m_iqImbalanceCorrection; }
void setIQImbalanceCorrection(bool iqImbalanceCorrection) { m_iqImbalanceCorrection = iqImbalanceCorrection; }
void setLayout(const QByteArray& data) { m_layout = data; }
const QByteArray& getLayout() const { return m_layout; }
@ -101,7 +103,13 @@ public:
int deviceSequence,
const QByteArray& config);
const QByteArray* findBestDeviceConfig(const QString& deviceId,
const QByteArray* findBestDeviceConfig(
const QString& deviceId,
const QString& deviceSerial,
int deviceSequence) const;
const QByteArray* findDeviceConfig(
const QString& deviceId,
const QString& deviceSerial,
int deviceSequence) const;

View File

@ -88,28 +88,6 @@ void WebAPIAdapterBase::webapiUpdatePreferences(
if (preferenceKeys.contains("useLogFile")) {
preferences.setUseLogFile(apiPreferences->getUseLogFile() != 0);
}
if (preferenceKeys.contains("consoleMinLogLevel"))
if (apiPreferences->getSourceDevice()) {
preferences.setSourceDevice(*apiPreferences->getSourceDevice());
}
preferences.setSourceIndex(apiPreferences->getSourceIndex());
if (apiPreferences->getAudioType()) {
preferences.setAudioType(*apiPreferences->getAudioType());
}
if (apiPreferences->getAudioDevice()) {
preferences.setAudioDevice(*apiPreferences->getAudioDevice());
}
preferences.setLatitude(apiPreferences->getLatitude());
preferences.setLongitude(apiPreferences->getLongitude());
preferences.setConsoleMinLogLevel((QtMsgType) apiPreferences->getConsoleMinLogLevel());
preferences.setUseLogFile(apiPreferences->getUseLogFile() != 0);
if (apiPreferences->getLogFileName()) {
preferences.setLogFileName(*apiPreferences->getLogFileName());
}
preferences.setFileMinLogLevel((QtMsgType) apiPreferences->getFileMinLogLevel());
}
void WebAPIAdapterBase::webapiFormatPreset(
@ -200,11 +178,124 @@ void WebAPIAdapterBase::webapiFormatPreset(
}
void WebAPIAdapterBase::webapiUpdatePreset(
bool force,
SWGSDRangel::SWGPreset *apiPreset,
const WebAPIAdapterInterface::PresetKeys& presetKeys,
Preset& preset
)
{
if (presetKeys.m_keys.contains("centerFrequency")) {
preset.setCenterFrequency(apiPreset->getCenterFrequency());
}
if (presetKeys.m_keys.contains("dcOffsetCorrection")) {
preset.setDCOffsetCorrection(apiPreset->getDcOffsetCorrection() != 0);
}
if (presetKeys.m_keys.contains("iqImbalanceCorrection")) {
preset.setIQImbalanceCorrection(apiPreset->getIqImbalanceCorrection() != 0);
}
if (presetKeys.m_keys.contains("sourcePreset")) {
preset.setSourcePreset(apiPreset->getSourcePreset() != 0);
}
if (presetKeys.m_keys.contains("description")) {
preset.setDescription(*apiPreset->getDescription());
}
if (presetKeys.m_keys.contains("group")) {
preset.setGroup(*apiPreset->getGroup());
}
if (force) { // PUT replaces devices list possibly erasing it if no devices are given
preset.clearDevices();
}
QString errorMessage;
QList<WebAPIAdapterInterface::DeviceKeys>::const_iterator deviceKeysIt = presetKeys.m_devicesKeys.begin();
int i = 0;
for (; deviceKeysIt != presetKeys.m_devicesKeys.end(); ++deviceKeysIt, i++)
{
SWGSDRangel::SWGDeviceConfig *swgDeviceConfig = apiPreset->getDeviceConfigs()->at(i);
if (!swgDeviceConfig) { // safety measure but should not happen
continue;
}
QString deviceId;
int deviceSequence = 0;
QString deviceSerial;
if (deviceKeysIt->m_keys.contains("deviceId")) {
deviceId = *swgDeviceConfig->getDeviceId();
}
if (deviceKeysIt->m_keys.contains("deviceSequence")) {
deviceSequence = swgDeviceConfig->getDeviceSequence();
}
if (deviceKeysIt->m_keys.contains("deviceSerial")) {
deviceSerial = *swgDeviceConfig->getDeviceSerial();
}
DeviceWebAPIAdapter *deviceWebAPIAdapter = m_webAPIDeviceAdapters.getDeviceWebAPIAdapter(deviceId, m_pluginManager);
if (deviceWebAPIAdapter)
{
if (!force) // In PATCH mode you must find the exact device and deserialize its current settings to be able to patch it
{
const QByteArray *config = preset.findDeviceConfig(deviceId, deviceSerial, deviceSequence);
if (!config) {
continue;
}
if (!deviceWebAPIAdapter->deserialize(*config)) {
continue;
}
}
deviceWebAPIAdapter->webapiSettingsPutPatch(
force,
deviceKeysIt->m_deviceKeys,
*swgDeviceConfig->getConfig(),
errorMessage
);
QByteArray config = deviceWebAPIAdapter->serialize();
preset.setDeviceConfig(deviceId, deviceSerial, deviceSequence, config); // add or update device
}
}
if (force) { // PUT replaces channel list possibly erasing it if no channels are given
preset.clearChannels();
}
QList<WebAPIAdapterInterface::ChannelKeys>::const_iterator channelKeysIt = presetKeys.m_channelsKeys.begin();
i = 0;
for (; channelKeysIt != presetKeys.m_channelsKeys.end(); ++channelKeysIt, i++)
{
SWGSDRangel::SWGChannelConfig *swgChannelConfig = apiPreset->getChannelConfigs()->at(i);
if (!swgChannelConfig) { // safety measure but should not happen
continue;
}
if (channelKeysIt->m_keys.contains("channelIdURI"))
{
QString *channelIdURI = swgChannelConfig->getChannelIdUri();
if (!channelIdURI) {
continue;
}
ChannelWebAPIAdapter *channelWebAPIAdapter = m_webAPIChannelAdapters.getChannelWebAPIAdapter(*channelIdURI, m_pluginManager);
if (!channelWebAPIAdapter) {
continue;
}
channelWebAPIAdapter->webapiSettingsPutPatch(
true, // channels are always appended
channelKeysIt->m_channelKeys,
*swgChannelConfig->getConfig(),
errorMessage
);
QByteArray config = channelWebAPIAdapter->serialize();
preset.addChannel(*channelIdURI, config);
}
}
}
void WebAPIAdapterBase::webapiFormatCommand(

View File

@ -66,7 +66,8 @@ public:
const QStringList& preferenceKeys,
Preferences& preferences
);
static void webapiUpdatePreset(
void webapiUpdatePreset(
bool force,
SWGSDRangel::SWGPreset *apiPreset,
const WebAPIAdapterInterface::PresetKeys& presetKeys,
Preset& preset

View File

@ -160,9 +160,9 @@ int WebAPIAdapterGUI::instanceConfigPutPatch(
{
WebAPIAdapterBase webAPIAdapterBase;
webAPIAdapterBase.setPluginManager(m_mainWindow.getPluginManager());
qDebug("WebAPIAdapterGUI::instanceConfigPutPatch: configKeys:");
configKeys.debug();
return 200;
// qDebug("WebAPIAdapterGUI::instanceConfigPutPatch: configKeys:");
// configKeys.debug();
// return 200;
if (force) {
webAPIAdapterBase.webapiInitConfig(m_mainWindow.m_settings);
@ -173,7 +173,7 @@ int WebAPIAdapterGUI::instanceConfigPutPatch(
m_mainWindow.m_settings.setPreferences(newPreferences);
Preset *workingPreset = m_mainWindow.m_settings.getWorkingPreset();
webAPIAdapterBase.webapiUpdatePreset(query.getWorkingPreset(), configKeys.m_workingPresetKeys, *workingPreset);
webAPIAdapterBase.webapiUpdatePreset(force, query.getWorkingPreset(), configKeys.m_workingPresetKeys, *workingPreset);
QList<PresetKeys>::const_iterator presetKeysIt = configKeys.m_presetKeys.begin();
int i = 0;
@ -181,7 +181,7 @@ int WebAPIAdapterGUI::instanceConfigPutPatch(
{
Preset *newPreset = new Preset(); // created with default values
SWGSDRangel::SWGPreset *swgPreset = query.getPresets()->at(i);
webAPIAdapterBase.webapiUpdatePreset(swgPreset, *presetKeysIt, *newPreset);
webAPIAdapterBase.webapiUpdatePreset(force, swgPreset, *presetKeysIt, *newPreset);
m_mainWindow.m_settings.addPreset(newPreset);
}