From d0081b2af41146ed5d812004e3ea6429fc6f0c46 Mon Sep 17 00:00:00 2001 From: f4exb Date: Mon, 26 Oct 2020 13:48:19 +0100 Subject: [PATCH] AFC: REST API: actions and report: implementation --- plugins/feature/afc/afc.cpp | 49 +++++++++++++++++++++++++++---- plugins/feature/afc/afc.h | 4 ++- plugins/feature/afc/afcgui.cpp | 33 ++++++++++++--------- plugins/feature/afc/afcgui.h | 4 +-- plugins/feature/afc/afcreport.cpp | 2 +- plugins/feature/afc/afcreport.h | 17 ++++++----- plugins/feature/afc/afcworker.cpp | 18 ++++++++++-- plugins/feature/afc/afcworker.h | 3 ++ 8 files changed, 97 insertions(+), 33 deletions(-) diff --git a/plugins/feature/afc/afc.cpp b/plugins/feature/afc/afc.cpp index 2b9c1cf33..9ae3f10f9 100644 --- a/plugins/feature/afc/afc.cpp +++ b/plugins/feature/afc/afc.cpp @@ -46,7 +46,8 @@ const QString AFC::m_featureId = "AFC"; AFC::AFC(WebAPIAdapterInterface *webAPIAdapterInterface) : Feature(m_featureIdURI, webAPIAdapterInterface), m_trackerDeviceSet(nullptr), - m_trackedDeviceSet(nullptr) + m_trackedDeviceSet(nullptr), + m_trackerIndexInDeviceSet(-1) { setObjectName(m_featureId); m_worker = new AFCWorker(webAPIAdapterInterface); @@ -61,7 +62,8 @@ AFC::~AFC() } delete m_worker; - removeFeatureReferences(); + removeTrackerFeatureReferences(); + removeTrackedFeatureReferences(); } void AFC::start() @@ -217,11 +219,13 @@ void AFC::applySettings(const AFCSettings& settings, bool force) reverseAPIKeys.append("trackerAdjustPeriod"); } - if ((m_settings.m_trackerDeviceSetIndex != settings.m_trackerDeviceSetIndex) || force) { + if ((m_settings.m_trackerDeviceSetIndex != settings.m_trackerDeviceSetIndex) || force) + { trackerDeviceChange(settings.m_trackerDeviceSetIndex); } - if ((m_settings.m_trackedDeviceSetIndex != settings.m_trackedDeviceSetIndex) || force) { + if ((m_settings.m_trackedDeviceSetIndex != settings.m_trackedDeviceSetIndex) || force) + { trackedDeviceChange(settings.m_trackedDeviceSetIndex); } @@ -309,6 +313,28 @@ int AFC::webapiActionsPost( if (swgAFCActions) { + if (featureActionsKeys.contains("deviceTrack")) + { + bool deviceTrack = swgAFCActions->getDeviceTrack() != 0; + + if (deviceTrack) + { + MsgDeviceTrack *msg = MsgDeviceTrack::create(); + getInputMessageQueue()->push(msg); + } + } + + if (featureActionsKeys.contains("devicesApply")) + { + bool devicesApply = swgAFCActions->getDevicesApply() != 0; + + if (devicesApply) + { + MsgDevicesApply *msg = MsgDevicesApply::create(); + getInputMessageQueue()->push(msg); + } + } + return 202; } else @@ -401,6 +427,9 @@ void AFC::webapiUpdateFeatureSettings( void AFC::webapiFormatFeatureReport(SWGSDRangel::SWGFeatureReport& response) { + response.getAfcReport()->setTrackerChannelIndex(m_trackerIndexInDeviceSet); + response.getAfcReport()->setTrackerDeviceFrequency(m_worker->getTrackerDeviceFrequency()); + response.getAfcReport()->setTrackerChannelOffset(m_worker->getTrackerChannelOffset()); } void AFC::webapiReverseSendSettings(QList& channelSettingsKeys, const AFCSettings& settings, bool force) @@ -509,15 +538,18 @@ void AFC::trackedDeviceChange(int deviceIndex) { ChannelAPI *channel = m_trackedDeviceSet->getChannelAt(i); - if (channel->getURI() != "sdrangel.channel.freqtracker") { + if (channel->getURI() != "sdrangel.channel.freqtracker") + { channel->addFeatureSettingsFeedback(this); + m_trackerIndexInDeviceSet = i; } } } -void AFC::removeFeatureReferences() +void AFC::removeTrackerFeatureReferences() { MainCore *mainCore = MainCore::instance(); + m_trackerIndexInDeviceSet = -1; if ((m_settings.m_trackerDeviceSetIndex >= 0) && (m_settings.m_trackerDeviceSetIndex < mainCore->getDeviceSets().size())) { @@ -532,6 +564,11 @@ void AFC::removeFeatureReferences() } } } +} + +void AFC::removeTrackedFeatureReferences() +{ + MainCore *mainCore = MainCore::instance(); if ((m_settings.m_trackedDeviceSetIndex >= 0) && (m_settings.m_trackedDeviceSetIndex < mainCore->getDeviceSets().size())) { diff --git a/plugins/feature/afc/afc.h b/plugins/feature/afc/afc.h index d29ba0ff8..6d169bbd1 100644 --- a/plugins/feature/afc/afc.h +++ b/plugins/feature/afc/afc.h @@ -160,6 +160,7 @@ private: AFCSettings m_settings; DeviceSet *m_trackerDeviceSet; DeviceSet *m_trackedDeviceSet; + int m_trackerIndexInDeviceSet; QNetworkAccessManager *m_networkManager; QNetworkRequest m_networkRequest; @@ -171,7 +172,8 @@ private: void webapiReverseSendSettings(QList& featureSettingsKeys, const AFCSettings& settings, bool force); void trackerDeviceChange(int deviceIndex); void trackedDeviceChange(int deviceIndex); - void removeFeatureReferences(); + void removeTrackerFeatureReferences(); + void removeTrackedFeatureReferences(); private slots: void networkManagerFinished(QNetworkReply *reply); diff --git a/plugins/feature/afc/afcgui.cpp b/plugins/feature/afc/afcgui.cpp index 880989822..d2ac55ab4 100644 --- a/plugins/feature/afc/afcgui.cpp +++ b/plugins/feature/afc/afcgui.cpp @@ -79,15 +79,19 @@ bool AFCGUI::handleMessage(const Message& message) return true; } - else if (AFCReport::MsgRadioState::match(message)) + else if (AFCReport::MsgUpdateTarget::match(message)) { - qDebug("AFCGUI::handleMessage: AFCReport::MsgRadioState"); - const AFCReport::MsgRadioState& cfg = (AFCReport::MsgRadioState&) message; - AFCReport::RadioState state = cfg.getState(); - ui->statusIndicator->setStyleSheet("QLabel { background-color: " + - m_statusColors[(int) state] + "; border-radius: 12px; }"); - ui->statusIndicator->setToolTip(m_statusTooltips[(int) state]); + const AFCReport::MsgUpdateTarget& cfg = (AFCReport::MsgUpdateTarget&) message; + bool frequencyChanged = cfg.getFrequencyChanged(); + if (cfg.getFrequencyChanged()) { + ui->statusIndicator->setStyleSheet("QLabel { background-color: rgb(232, 85, 85); border-radius: 8px; }"); // red + } else { + ui->statusIndicator->setStyleSheet("QLabel { background-color: rgb(85, 232, 85); border-radius: 8px; }"); // green + } + + ui->statusIndicator->setToolTip(tr("%1 Hz").arg(cfg.getFrequencyAdjustment())); + m_autoTargetStatusTimer.start(500); return true; } @@ -142,13 +146,9 @@ AFCGUI::AFCGUI(PluginAPI* pluginAPI, FeatureUISet *featureUISet, Feature *featur connect(&m_statusTimer, SIGNAL(timeout()), this, SLOT(updateStatus())); m_statusTimer.start(1000); - m_statusTooltips.push_back("Idle"); // 0 - all off - m_statusTooltips.push_back("Rx on"); // 1 - Rx on - m_statusTooltips.push_back("Tx on"); // 2 - Tx on - - m_statusColors.push_back("gray"); // All off - m_statusColors.push_back("rgb(85, 232, 85)"); // Rx on (green) - m_statusColors.push_back("rgb(232, 85, 85)"); // Tx on (red) + connect(&m_autoTargetStatusTimer, SIGNAL(timeout()), this, SLOT(resetAutoTargetStatus())); + m_autoTargetStatusTimer.setSingleShot(true); + ui->statusIndicator->setStyleSheet("QLabel { background-color: gray; border-radius: 8px; }"); updateDeviceSetLists(); displaySettings(); @@ -401,6 +401,11 @@ void AFCGUI::updateStatus() } } +void AFCGUI::resetAutoTargetStatus() +{ + ui->statusIndicator->setStyleSheet("QLabel { background-color: gray; border-radius: 8px; }"); +} + void AFCGUI::applySettings(bool force) { if (m_doApplySettings) diff --git a/plugins/feature/afc/afcgui.h b/plugins/feature/afc/afcgui.h index d4bfa2b14..b9adbe79b 100644 --- a/plugins/feature/afc/afcgui.h +++ b/plugins/feature/afc/afcgui.h @@ -53,9 +53,8 @@ private: AFC* m_afc; MessageQueue m_inputMessageQueue; QTimer m_statusTimer; + QTimer m_autoTargetStatusTimer; int m_lastFeatureState; - std::vector m_statusColors; - std::vector m_statusTooltips; explicit AFCGUI(PluginAPI* pluginAPI, FeatureUISet *featureUISet, Feature *feature, QWidget* parent = nullptr); virtual ~AFCGUI(); @@ -85,6 +84,7 @@ private slots: void on_devicesApply_clicked(); void on_targetPeriod_valueChanged(int value); void updateStatus(); + void resetAutoTargetStatus(); }; diff --git a/plugins/feature/afc/afcreport.cpp b/plugins/feature/afc/afcreport.cpp index 105de5d52..a9ced7006 100644 --- a/plugins/feature/afc/afcreport.cpp +++ b/plugins/feature/afc/afcreport.cpp @@ -17,7 +17,7 @@ #include "afcreport.h" -MESSAGE_CLASS_DEFINITION(AFCReport::MsgRadioState, Message) +MESSAGE_CLASS_DEFINITION(AFCReport::MsgUpdateTarget, Message) AFCReport::AFCReport() {} diff --git a/plugins/feature/afc/afcreport.h b/plugins/feature/afc/afcreport.h index 97f94f99d..47b6bf8ef 100644 --- a/plugins/feature/afc/afcreport.h +++ b/plugins/feature/afc/afcreport.h @@ -28,23 +28,26 @@ public: RadioRx, RadioTx }; - class MsgRadioState : public Message { + class MsgUpdateTarget : public Message { MESSAGE_CLASS_DECLARATION public: - RadioState getState() const { return m_state; } + int getFrequencyAdjustment() const { return m_frequencyAdjustment; } + bool getFrequencyChanged() const { return m_frequencyChanged; } - static MsgRadioState* create(RadioState state) + static MsgUpdateTarget* create(int frequencyAdjustment, bool frequencyChanged) { - return new MsgRadioState(state); + return new MsgUpdateTarget(frequencyAdjustment, frequencyChanged); } private: - RadioState m_state; + int m_frequencyAdjustment; + bool m_frequencyChanged; - MsgRadioState(RadioState state) : + MsgUpdateTarget(int frequencyAdjustment, bool frequencyChanged) : Message(), - m_state(state) + m_frequencyAdjustment(frequencyAdjustment), + m_frequencyChanged(frequencyChanged) { } }; diff --git a/plugins/feature/afc/afcworker.cpp b/plugins/feature/afc/afcworker.cpp index 77a40db59..f0decea34 100644 --- a/plugins/feature/afc/afcworker.cpp +++ b/plugins/feature/afc/afcworker.cpp @@ -393,9 +393,10 @@ void AFCWorker::updateTarget() int64_t trackerFrequency = m_trackerDeviceFrequency + m_trackerChannelOffset; int64_t correction = m_settings.m_targetFrequency - trackerFrequency; int64_t tolerance = m_settings.m_freqTolerance; - qDebug() << "AFCWorker::updateTarget: correction:" << correction << "tolerance:" << tolerance; - if ((correction > -tolerance) && (correction < tolerance)) { + if ((correction > -tolerance) && (correction < tolerance)) + { + reportUpdateTarget(correction, false); return; } @@ -420,6 +421,8 @@ void AFCWorker::updateTarget() if (updateChannelOffset(m_freqTracker, 0, m_trackerChannelOffset + correction, 1)) { m_trackerChannelOffset += correction; } + + reportUpdateTarget(correction, true); } else // act on device { @@ -436,6 +439,8 @@ void AFCWorker::updateTarget() qDebug() << "AFCWorker::updateTarget: cannot find device transverter frequency"; return; } + + reportUpdateTarget(correction, true); } } @@ -515,3 +520,12 @@ void AFCWorker::getDeviceSettingsKey(DeviceAPI *deviceAPI, QString& settingsKey) } } } + +void AFCWorker::reportUpdateTarget(int correction, bool done) +{ + if (m_msgQueueToGUI) + { + AFCReport::MsgUpdateTarget *msg = AFCReport::MsgUpdateTarget::create(correction, done); + m_msgQueueToGUI->push(msg); + } +} diff --git a/plugins/feature/afc/afcworker.h b/plugins/feature/afc/afcworker.h index d1fc10140..862e22607 100644 --- a/plugins/feature/afc/afcworker.h +++ b/plugins/feature/afc/afcworker.h @@ -112,6 +112,8 @@ public: bool isRunning() const { return m_running; } MessageQueue *getInputMessageQueue() { return &m_inputMessageQueue; } void setMessageQueueToGUI(MessageQueue *messageQueue) { m_msgQueueToGUI = messageQueue; } + uint64_t getTrackerDeviceFrequency() const { return m_trackerDeviceFrequency; } + int getTrackerChannelOffset() const { return m_trackerChannelOffset; } private: struct ChannelTracking @@ -166,6 +168,7 @@ private: bool updateDeviceFrequency(DeviceSet *deviceSet, const QString& key, int64_t frequency); int getDeviceDirection(DeviceAPI *deviceAPI); void getDeviceSettingsKey(DeviceAPI *deviceAPI, QString& settingsKey); + void reportUpdateTarget(int correction, bool done); private slots: void updateTarget();