Skip to content

Commit

Permalink
Fix #54, implement #55
Browse files Browse the repository at this point in the history
  • Loading branch information
pierr3 committed May 13, 2024
1 parent 496c03a commit c1cfae7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
9 changes: 8 additions & 1 deletion backend/include/sdk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ using sdk::types::WebsocketMessage;
using sdk::types::WebsocketMessageType;

namespace sdk::types {
enum Event { kRxBegin, kRxEnd, kFrequencyStateUpdate, kDisconnectFrequencyStateUpdate };
enum Event {
kRxBegin,
kRxEnd,
kTxBegin,
kTxEnd,
kFrequencyStateUpdate,
kDisconnectFrequencyStateUpdate
};
}

class SDK {
Expand Down
15 changes: 13 additions & 2 deletions backend/include/sdkWebsocketMessage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
#include <utility>

namespace sdk::types {
enum class WebsocketMessageType { kRxBegin, kRxEnd, kFrequencyStateUpdate };
enum class WebsocketMessageType { kRxBegin, kRxEnd, kTxBegin, kTxEnd, kFrequencyStateUpdate };

inline const std::map<WebsocketMessageType, std::string>& getWebsocketMessageTypeMap()
{
static const std::map<WebsocketMessageType, std::string> kWebsocketMessageTypeMap {
{ WebsocketMessageType::kRxBegin, "kRxBegin" }, { WebsocketMessageType::kRxEnd, "kRxEnd" },
{ WebsocketMessageType::kFrequencyStateUpdate, "kFrequenciesUpdate" }
{ WebsocketMessageType::kTxBegin, "kTxBegin" }, { WebsocketMessageType::kTxEnd, "kTxEnd" },
{ WebsocketMessageType::kFrequencyStateUpdate, "kFrequencyStateUpdate" }
};
return kWebsocketMessageTypeMap;
}
Expand Down Expand Up @@ -94,3 +95,13 @@ class Station {
// [{"pFrequencyHz": 118775000, "pCallsign": "EDDF_S_TWR"}], "tx":
// [{"pFrequencyHz": 119775000, "pCallsign": "EDDF_S_TWR"}], "xc":
// [{"pFrequencyHz": 121500000, "pCallsign": "EDDF_S_TWR"}]}}

// Example of kTxBegin message:
// @type the type of the message
// @value the frequencies which are being transmitted on
// JSON: {"type": "kTxBegin", "value": {"frequenciesHz": [123000000, 118000000]}}

// Example of kTxEnd message:
// @type the type of the message
// @value the frequencies which were being transmitted on
// JSON: {"type": "kTxEnd", "value": {"frequenciesHz": [123000000, 118000000]}}
7 changes: 6 additions & 1 deletion backend/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <httplib.h>
#include <memory>
#include <napi.h>
#include <optional>
#include <quill/LogLevel.h>
#include <quill/Quill.h>
#include <quill/detail/LogMacros.h>
Expand Down Expand Up @@ -433,7 +434,7 @@ static void HandleAfvEvents(afv_native::ClientEventType eventType, void* data, v
auto states = mClient->getRadioState();
for (const auto& state : states) {
if (state.second.stationName == station) {
mClient->UseTransceiversFromStation(station, state.first);
mClient->UseTransceiversFromStation(station, static_cast<int>(state.first));
break;
}
}
Expand Down Expand Up @@ -565,10 +566,14 @@ static void HandleAfvEvents(afv_native::ClientEventType eventType, void* data, v

if (eventType == afv_native::ClientEventType::PttOpen) {
NapiHelpers::callElectron("PttState", "1");
MainThreadShared::mApiServer->handleAFVEventForWebsocket(
sdk::types::Event::kTxBegin, std::nullopt, std::nullopt);
}

if (eventType == afv_native::ClientEventType::PttClosed) {
NapiHelpers::callElectron("PttState", "0");
MainThreadShared::mApiServer->handleAFVEventForWebsocket(
sdk::types::Event::kTxEnd, std::nullopt, std::nullopt);
}

if (eventType == afv_native::ClientEventType::AudioError) {
Expand Down
29 changes: 29 additions & 0 deletions backend/src/sdk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void SDK::buildServer()
}
}

// NOLINTNEXTLINE
void SDK::handleAFVEventForWebsocket(sdk::types::Event event,
const std::optional<std::string>& callsign, const std::optional<int>& frequencyHz)
{
Expand Down Expand Up @@ -71,6 +72,34 @@ void SDK::handleAFVEventForWebsocket(sdk::types::Event event,
return;
}

if (event == sdk::types::Event::kTxBegin) {
auto allRadios = mClient->getRadioState();
std::vector<unsigned int> allTxRadioFreqs;
for (const auto& [freq, state] : allRadios) {
if (state.tx) {
allTxRadioFreqs.push_back(freq);
}
}

nlohmann::json jsonMessage = WebsocketMessage::buildMessage(WebsocketMessageType::kTxBegin);
jsonMessage["value"]["frequenciesHz"] = allTxRadioFreqs;
this->broadcastOnWebsocket(jsonMessage.dump());
}

if (event == sdk::types::Event::kTxEnd) {
auto allRadios = mClient->getRadioState();
std::vector<unsigned int> allTxRadioFreqs;
for (const auto& [freq, state] : allRadios) {
if (state.tx) {
allTxRadioFreqs.push_back(freq);
}
}

nlohmann::json jsonMessage = WebsocketMessage::buildMessage(WebsocketMessageType::kTxEnd);
jsonMessage["value"]["frequenciesHz"] = allTxRadioFreqs;
this->broadcastOnWebsocket(jsonMessage.dump());
}

if (event == sdk::types::Event::kFrequencyStateUpdate) {
nlohmann::json jsonMessage
= WebsocketMessage::buildMessage(WebsocketMessageType::kFrequencyStateUpdate);
Expand Down

0 comments on commit c1cfae7

Please sign in to comment.