2019-07-31 20:16:56 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2019 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 //
|
|
|
|
// (at your option) any later version. //
|
|
|
|
// //
|
|
|
|
// 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/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
2019-08-01 12:50:21 -04:00
|
|
|
|
|
|
|
#include "plugin/pluginmanager.h"
|
|
|
|
#include "channel/channelapi.h"
|
2019-08-01 19:58:59 -04:00
|
|
|
#include "channel/channelutils.h"
|
2019-08-03 05:21:46 -04:00
|
|
|
#include "device/devicewebapiadapter.h"
|
2019-07-31 20:16:56 -04:00
|
|
|
#include "webapiadapterbase.h"
|
|
|
|
|
2019-08-01 12:50:21 -04:00
|
|
|
WebAPIAdapterBase::WebAPIAdapterBase()
|
|
|
|
{}
|
|
|
|
|
|
|
|
WebAPIAdapterBase::~WebAPIAdapterBase()
|
2019-08-01 15:27:31 -04:00
|
|
|
{
|
|
|
|
m_webAPIChannelAdapters.flush();
|
|
|
|
}
|
2019-08-01 12:50:21 -04:00
|
|
|
|
2019-07-31 20:16:56 -04:00
|
|
|
void WebAPIAdapterBase::webapiFormatPreferences(
|
|
|
|
SWGSDRangel::SWGPreferences *apiPreferences,
|
|
|
|
const Preferences& preferences
|
|
|
|
)
|
|
|
|
{
|
|
|
|
apiPreferences->init();
|
2019-08-01 04:54:45 -04:00
|
|
|
apiPreferences->setSourceDevice(new QString(preferences.getSourceDevice()));
|
|
|
|
apiPreferences->setSourceIndex(preferences.getSourceIndex());
|
|
|
|
apiPreferences->setAudioType(new QString(preferences.getAudioType()));
|
|
|
|
apiPreferences->setAudioDevice(new QString(preferences.getAudioDevice()));
|
|
|
|
apiPreferences->setLatitude(preferences.getLatitude());
|
|
|
|
apiPreferences->setLongitude(preferences.getLongitude());
|
|
|
|
apiPreferences->setConsoleMinLogLevel((int) preferences.getConsoleMinLogLevel());
|
|
|
|
apiPreferences->setUseLogFile(preferences.getUseLogFile() ? 1 : 0);
|
|
|
|
apiPreferences->setLogFileName(new QString(preferences.getLogFileName()));
|
|
|
|
apiPreferences->setFileMinLogLevel((int) preferences.getFileMinLogLevel());
|
2019-07-31 20:16:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebAPIAdapterBase::webapiFormatPreset(
|
|
|
|
SWGSDRangel::SWGPreset *apiPreset,
|
|
|
|
const Preset& preset
|
|
|
|
)
|
|
|
|
{
|
|
|
|
apiPreset->init();
|
2019-08-01 04:54:45 -04:00
|
|
|
apiPreset->setSourcePreset(preset.isSourcePreset() ? 1 : 0);
|
|
|
|
apiPreset->setGroup(new QString(preset.getGroup()));
|
|
|
|
apiPreset->setDescription(new QString(preset.getDescription()));
|
|
|
|
apiPreset->setCenterFrequency(preset.getCenterFrequency());
|
2019-08-01 02:49:35 -04:00
|
|
|
apiPreset->getSpectrumConfig()->init(); // TODO when spectrum config is extracted to sdrbase
|
2019-08-01 04:54:45 -04:00
|
|
|
apiPreset->setDcOffsetCorrection(preset.hasDCOffsetCorrection() ? 1 : 0);
|
|
|
|
apiPreset->setIqImbalanceCorrection(preset.hasIQImbalanceCorrection() ? 1 : 0);
|
2019-08-01 02:49:35 -04:00
|
|
|
|
|
|
|
int nbChannels = preset.getChannelCount();
|
|
|
|
for (int i = 0; i < nbChannels; i++)
|
|
|
|
{
|
|
|
|
const Preset::ChannelConfig& channelConfig = preset.getChannelConfig(i);
|
|
|
|
QList<SWGSDRangel::SWGChannelConfig *> *swgChannelConfigs = apiPreset->getChannelConfigs();
|
|
|
|
swgChannelConfigs->append(new SWGSDRangel::SWGChannelConfig);
|
2019-08-01 19:58:59 -04:00
|
|
|
swgChannelConfigs->back()->init();
|
2019-08-01 02:49:35 -04:00
|
|
|
swgChannelConfigs->back()->setChannelIdUri(new QString(channelConfig.m_channelIdURI));
|
2019-08-01 12:50:21 -04:00
|
|
|
const QByteArray& channelSettings = channelConfig.m_config;
|
|
|
|
SWGSDRangel::SWGChannelSettings *swgChannelSettings = swgChannelConfigs->back()->getConfig();
|
|
|
|
swgChannelSettings->init();
|
2019-08-01 15:27:31 -04:00
|
|
|
ChannelAPI *channelWebAPIAdapter = m_webAPIChannelAdapters.getChannelAPI(channelConfig.m_channelIdURI, m_pluginManager);
|
2019-08-01 12:50:21 -04:00
|
|
|
|
2019-08-01 15:27:31 -04:00
|
|
|
if (channelWebAPIAdapter)
|
2019-08-01 12:50:21 -04:00
|
|
|
{
|
|
|
|
channelWebAPIAdapter->deserialize(channelSettings);
|
|
|
|
QString errorMessage;
|
|
|
|
channelWebAPIAdapter->webapiSettingsGet(*swgChannelSettings, errorMessage);
|
|
|
|
}
|
2019-08-01 02:49:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int nbDevices = preset.getDeviceCount();
|
|
|
|
for (int i = 0; i < nbDevices; i++)
|
|
|
|
{
|
|
|
|
const Preset::DeviceConfig& deviceConfig = preset.getDeviceConfig(i);
|
|
|
|
QList<SWGSDRangel::SWGDeviceConfig *> *swgdeviceConfigs = apiPreset->getDeviceConfigs();
|
|
|
|
swgdeviceConfigs->append(new SWGSDRangel::SWGDeviceConfig);
|
2019-08-01 19:58:59 -04:00
|
|
|
swgdeviceConfigs->back()->init();
|
2019-08-01 02:49:35 -04:00
|
|
|
swgdeviceConfigs->back()->setDeviceId(new QString(deviceConfig.m_deviceId));
|
|
|
|
swgdeviceConfigs->back()->setDeviceSerial(new QString(deviceConfig.m_deviceSerial));
|
|
|
|
swgdeviceConfigs->back()->setDeviceSequence(deviceConfig.m_deviceSequence);
|
2019-08-03 05:21:46 -04:00
|
|
|
const QByteArray& deviceSettings = deviceConfig.m_config;
|
|
|
|
SWGSDRangel::SWGDeviceSettings *swgDeviceSettings = swgdeviceConfigs->back()->getConfig();
|
2019-08-03 20:50:32 -04:00
|
|
|
// qDebug("WebAPIAdapterBase::webapiFormatPreset: preset: %s:%s(%lld) device: %s",
|
|
|
|
// qPrintable(preset.getGroup()),
|
|
|
|
// qPrintable(preset.getDescription()),
|
|
|
|
// preset.getCenterFrequency(),
|
|
|
|
// qPrintable(deviceConfig.m_deviceId));
|
2019-08-03 05:21:46 -04:00
|
|
|
swgDeviceSettings->init();
|
|
|
|
DeviceWebAPIAdapter *deviceWebAPIAdapter = m_webAPIDeviceAdapters.getDeviceWebAPIAdapter(deviceConfig.m_deviceId, m_pluginManager);
|
|
|
|
|
|
|
|
if (deviceWebAPIAdapter)
|
|
|
|
{
|
|
|
|
deviceWebAPIAdapter->deserialize(deviceSettings);
|
|
|
|
QString errorMessage;
|
|
|
|
deviceWebAPIAdapter->webapiSettingsGet(*swgDeviceSettings, errorMessage);
|
|
|
|
}
|
2019-08-01 02:49:35 -04:00
|
|
|
}
|
2019-07-31 20:16:56 -04:00
|
|
|
}
|
2019-08-01 04:54:45 -04:00
|
|
|
|
|
|
|
void WebAPIAdapterBase::webapiFormatCommand(
|
|
|
|
SWGSDRangel::SWGCommand *apiCommand,
|
|
|
|
const Command& command
|
|
|
|
)
|
|
|
|
{
|
|
|
|
apiCommand->init();
|
|
|
|
apiCommand->setGroup(new QString(command.getGroup()));
|
|
|
|
apiCommand->setDescription(new QString(command.getDescription()));
|
|
|
|
apiCommand->setCommand(new QString(command.getCommand()));
|
|
|
|
apiCommand->setArgString(new QString(command.getArgString()));
|
|
|
|
apiCommand->setKey((int) command.getKey());
|
|
|
|
apiCommand->setKeyModifiers((int) command.getKeyModifiers());
|
|
|
|
apiCommand->setAssociateKey(command.getAssociateKey() ? 1 : 0);
|
|
|
|
apiCommand->setRelease(command.getRelease() ? 1 : 0);
|
2019-08-01 15:27:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ChannelAPI *WebAPIAdapterBase::WebAPIChannelAdapters::getChannelAPI(const QString& channelURI, const PluginManager *pluginManager)
|
|
|
|
{
|
2019-08-01 19:58:59 -04:00
|
|
|
QString registeredChannelURI = ChannelUtils::getRegisteredChannelURI(channelURI);
|
|
|
|
QMap<QString, ChannelAPI*>::iterator it = m_webAPIChannelAdapters.find(registeredChannelURI);
|
2019-08-01 15:27:31 -04:00
|
|
|
|
|
|
|
if (it == m_webAPIChannelAdapters.end())
|
|
|
|
{
|
2019-08-01 19:58:59 -04:00
|
|
|
const PluginInterface *pluginInterface = pluginManager->getChannelPluginInterface(registeredChannelURI);
|
|
|
|
|
|
|
|
if (pluginInterface)
|
|
|
|
{
|
|
|
|
ChannelAPI *channelAPI = pluginInterface->createChannelWebAPIAdapter();
|
|
|
|
m_webAPIChannelAdapters.insert(registeredChannelURI, channelAPI);
|
|
|
|
return channelAPI;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_webAPIChannelAdapters.insert(registeredChannelURI, nullptr);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-08-01 15:27:31 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebAPIAdapterBase::WebAPIChannelAdapters::flush()
|
|
|
|
{
|
2019-08-03 05:21:46 -04:00
|
|
|
foreach(ChannelAPI *channelAPI, m_webAPIChannelAdapters) {
|
|
|
|
delete channelAPI;
|
2019-08-01 15:27:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
m_webAPIChannelAdapters.clear();
|
2019-08-03 05:21:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
DeviceWebAPIAdapter *WebAPIAdapterBase::WebAPIDeviceAdapters::getDeviceWebAPIAdapter(const QString& deviceId, const PluginManager *pluginManager)
|
|
|
|
{
|
|
|
|
QMap<QString, DeviceWebAPIAdapter*>::iterator it = m_webAPIDeviceAdapters.find(deviceId);
|
|
|
|
|
|
|
|
if (it == m_webAPIDeviceAdapters.end())
|
|
|
|
{
|
|
|
|
const PluginInterface *pluginInterface = pluginManager->getDevicePluginInterface(deviceId);
|
|
|
|
|
|
|
|
if (pluginInterface)
|
|
|
|
{
|
|
|
|
DeviceWebAPIAdapter *deviceWebAPIAdapter = pluginInterface->createDeviceWebAPIAdapter();
|
|
|
|
m_webAPIDeviceAdapters.insert(deviceId, deviceWebAPIAdapter);
|
|
|
|
return deviceWebAPIAdapter;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_webAPIDeviceAdapters.insert(deviceId, nullptr);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebAPIAdapterBase::WebAPIDeviceAdapters::flush()
|
|
|
|
{
|
|
|
|
foreach(DeviceWebAPIAdapter *deviceWebAPIAdapter, m_webAPIDeviceAdapters) {
|
|
|
|
delete deviceWebAPIAdapter;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_webAPIDeviceAdapters.clear();
|
|
|
|
}
|