From 9f0449c3b0249631187719aaf03daa8835a0076d Mon Sep 17 00:00:00 2001 From: Neil Enns Date: Fri, 8 Nov 2024 09:20:13 -0800 Subject: [PATCH 1/4] Add kAddStation message Fixes #1 Auto-generated by Copilot workspace --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/neilenns/TrackAudio/issues/1?shareId=XXXX-XXXX-XXXX-XXXX). --- backend/include/sdk.hpp | 7 ++++ backend/include/sdkWebsocketMessage.hpp | 4 ++- backend/src/sdk.cpp | 43 +++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/backend/include/sdk.hpp b/backend/include/sdk.hpp index 9f136ec..dbb697d 100644 --- a/backend/include/sdk.hpp +++ b/backend/include/sdk.hpp @@ -205,4 +205,11 @@ class SDK : public std::enable_shared_from_this { * */ void handleGetStationState(const std::string& callsign); + + /** + * Handles the SDK call to add a station. + * + * @param json The incoming JSON with the station information. + */ + void handleAddStation(const nlohmann::json& json); }; diff --git a/backend/include/sdkWebsocketMessage.hpp b/backend/include/sdkWebsocketMessage.hpp index 3ae9ceb..ebb1476 100644 --- a/backend/include/sdkWebsocketMessage.hpp +++ b/backend/include/sdkWebsocketMessage.hpp @@ -16,7 +16,8 @@ enum class WebsocketMessageType : std::uint8_t { kStationStates, kVoiceConnectedState, kFrequencyRemoved, - kStationAdded + kStationAdded, + kAddStation }; inline const std::map& getWebsocketMessageTypeMap() @@ -32,6 +33,7 @@ inline const std::map& getWebsocketMessageTyp { WebsocketMessageType::kVoiceConnectedState, "kVoiceConnectedState" }, { WebsocketMessageType::kFrequencyRemoved, "kFrequencyRemoved" }, { WebsocketMessageType::kStationAdded, "kStationAdded" }, + { WebsocketMessageType::kAddStation, "kAddStation" }, }; return kWebsocketMessageTypeMap; } diff --git a/backend/src/sdk.cpp b/backend/src/sdk.cpp index 8a99c62..9e9c7bb 100644 --- a/backend/src/sdk.cpp +++ b/backend/src/sdk.cpp @@ -428,6 +428,10 @@ void SDK::handleIncomingWebSocketRequest(const std::string& payload) this->handleVoiceConnectedEventForWebsocket(mClient->IsVoiceConnected()); return; } + if (messageType == "kAddStation") { + this->handleAddStation(json); + return; + } } catch (const std::exception& e) { // Handle JSON parsing error PLOG_ERROR << "Error parsing incoming message JSON: " << e.what(); @@ -484,3 +488,42 @@ void SDK::broadcastOnWebsocket(const std::string& data) } } }; + +void SDK::handleAddStation(const nlohmann::json& json) +{ + std::optional callsign; + std::optional frequency; + + if (json["value"].contains("callsign")) { + callsign = json["value"]["callsign"]; + } + if (json["value"].contains("frequency")) { + frequency = json["value"]["frequency"]; + } + + if (callsign.has_value() && frequency.has_value()) { + PLOG_ERROR << "Both callsign and frequency specified. Only one should be specified."; + return; + } + + if (!callsign.has_value() && !frequency.has_value()) { + PLOG_ERROR << "Neither callsign nor frequency specified. One must be specified."; + return; + } + + auto allRadios = mClient->getRadioState(); + for (const auto& [freq, state] : allRadios) { + if ((callsign.has_value() && state.stationName == callsign.value()) + || (frequency.has_value() && freq == frequency.value())) { + this->publishStationState( + this->buildStationStateJson(state.stationName, static_cast(freq))); + return; + } + } + + if (callsign.has_value()) { + mClient->GetStation(callsign.value()); + } else if (frequency.has_value()) { + mClient->AddFrequency(frequency.value(), ""); + } +} From 9b8da6a5161677863ebb715925be65e239d409f4 Mon Sep 17 00:00:00 2001 From: Neil Enns Date: Mon, 18 Nov 2024 10:04:05 -0800 Subject: [PATCH 2/4] Works! --- backend/src/sdk.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/backend/src/sdk.cpp b/backend/src/sdk.cpp index 9e9c7bb..3536e5b 100644 --- a/backend/src/sdk.cpp +++ b/backend/src/sdk.cpp @@ -491,6 +491,11 @@ void SDK::broadcastOnWebsocket(const std::string& data) void SDK::handleAddStation(const nlohmann::json& json) { + if (!mClient->IsVoiceConnected()) { + PLOG_ERROR << "Voice must be connected before adding a station."; + return; + } + std::optional callsign; std::optional frequency; @@ -512,6 +517,9 @@ void SDK::handleAddStation(const nlohmann::json& json) } auto allRadios = mClient->getRadioState(); + + // See if the station or frequency is already added. if yes, just publish the current + // state and return. for (const auto& [freq, state] : allRadios) { if ((callsign.has_value() && state.stationName == callsign.value()) || (frequency.has_value() && freq == frequency.value())) { @@ -521,9 +529,12 @@ void SDK::handleAddStation(const nlohmann::json& json) } } + // Add via callsign if that's what was specified. if (callsign.has_value()) { mClient->GetStation(callsign.value()); - } else if (frequency.has_value()) { - mClient->AddFrequency(frequency.value(), ""); + } + // Otherwise add via frequency. + else if (frequency.has_value()) { + mClient->AddFrequency(frequency.value(), "MANUAL"); } } From f813697ae5c6cac3bc271f75b269dc8cb567558f Mon Sep 17 00:00:00 2001 From: Neil Enns Date: Mon, 18 Nov 2024 11:42:05 -0800 Subject: [PATCH 3/4] Code cleanup --- backend/src/sdk.cpp | 53 +++++++++++++++++---------------------------- 1 file changed, 20 insertions(+), 33 deletions(-) diff --git a/backend/src/sdk.cpp b/backend/src/sdk.cpp index 3536e5b..8c3143c 100644 --- a/backend/src/sdk.cpp +++ b/backend/src/sdk.cpp @@ -496,45 +496,32 @@ void SDK::handleAddStation(const nlohmann::json& json) return; } - std::optional callsign; - std::optional frequency; - - if (json["value"].contains("callsign")) { - callsign = json["value"]["callsign"]; - } - if (json["value"].contains("frequency")) { - frequency = json["value"]["frequency"]; - } - - if (callsign.has_value() && frequency.has_value()) { - PLOG_ERROR << "Both callsign and frequency specified. Only one should be specified."; + if (!json.contains("value") || !json["value"].contains("callsign")) { + PLOG_ERROR << "Callsign must be specified."; return; } - if (!callsign.has_value() && !frequency.has_value()) { - PLOG_ERROR << "Neither callsign nor frequency specified. One must be specified."; - return; - } + try { - auto allRadios = mClient->getRadioState(); + auto callsign = json["value"]["callsign"].get(); - // See if the station or frequency is already added. if yes, just publish the current - // state and return. - for (const auto& [freq, state] : allRadios) { - if ((callsign.has_value() && state.stationName == callsign.value()) - || (frequency.has_value() && freq == frequency.value())) { - this->publishStationState( - this->buildStationStateJson(state.stationName, static_cast(freq))); - return; + auto allRadios = mClient->getRadioState(); + + PLOG_INFO << "Adding callsign: " << callsign; + + // See if the station or frequency is already added. if yes, just publish the current + // state and return. + for (const auto& [freq, state] : allRadios) { + if (state.stationName == callsign) { + this->publishStationState( + this->buildStationStateJson(state.stationName, static_cast(freq))); + return; + } } - } - // Add via callsign if that's what was specified. - if (callsign.has_value()) { - mClient->GetStation(callsign.value()); - } - // Otherwise add via frequency. - else if (frequency.has_value()) { - mClient->AddFrequency(frequency.value(), "MANUAL"); + // Since it wasn't already added, add it. + mClient->GetStation(callsign); + } catch (const nlohmann::json::exception& e) { + PLOG_ERROR << "Failed to read the callsign: " << e.what(); } } From 693ef26fbd01de74208eb5f0784226f9f44eeeb0 Mon Sep 17 00:00:00 2001 From: Neil Enns Date: Tue, 19 Nov 2024 05:54:01 -0800 Subject: [PATCH 4/4] Fix whitespace issues --- backend/src/sdk.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/backend/src/sdk.cpp b/backend/src/sdk.cpp index 8c3143c..35051d1 100644 --- a/backend/src/sdk.cpp +++ b/backend/src/sdk.cpp @@ -502,9 +502,7 @@ void SDK::handleAddStation(const nlohmann::json& json) } try { - auto callsign = json["value"]["callsign"].get(); - auto allRadios = mClient->getRadioState(); PLOG_INFO << "Adding callsign: " << callsign;