Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/pierr3/TrackAudio
Browse files Browse the repository at this point in the history
  • Loading branch information
pierr3 committed Nov 23, 2024
2 parents 0b22ad8 + 46cd34f commit 352f2ff
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
7 changes: 7 additions & 0 deletions backend/include/sdk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,11 @@ class SDK : public std::enable_shared_from_this<SDK> {
*
*/
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);
};
4 changes: 3 additions & 1 deletion backend/include/sdkWebsocketMessage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ enum class WebsocketMessageType : std::uint8_t {
kStationStates,
kVoiceConnectedState,
kFrequencyRemoved,
kStationAdded
kStationAdded,
kAddStation
};

inline const std::map<WebsocketMessageType, std::string>& getWebsocketMessageTypeMap()
Expand All @@ -33,6 +34,7 @@ inline const std::map<WebsocketMessageType, std::string>& getWebsocketMessageTyp
{ WebsocketMessageType::kVoiceConnectedState, "kVoiceConnectedState" },
{ WebsocketMessageType::kFrequencyRemoved, "kFrequencyRemoved" },
{ WebsocketMessageType::kStationAdded, "kStationAdded" },
{ WebsocketMessageType::kAddStation, "kAddStation" },
};
return kWebsocketMessageTypeMap;
}
Expand Down
39 changes: 39 additions & 0 deletions backend/src/sdk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -484,3 +488,38 @@ 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;
}

if (!json.contains("value") || !json["value"].contains("callsign")) {
PLOG_ERROR << "Callsign must be specified.";
return;
}

try {
auto callsign = json["value"]["callsign"].get<std::string>();
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<int>(freq)));
return;
}
}

// 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();
}
}

0 comments on commit 352f2ff

Please sign in to comment.