diff --git a/simulation/halsim_ws_client/src/main/native/cpp/HALSimWSClient.cpp b/simulation/halsim_ws_client/src/main/native/cpp/HALSimWSClient.cpp index a5c3e85f95..3f117d7776 100644 --- a/simulation/halsim_ws_client/src/main/native/cpp/HALSimWSClient.cpp +++ b/simulation/halsim_ws_client/src/main/native/cpp/HALSimWSClient.cpp @@ -179,8 +179,10 @@ void HALSimWS::OnNetValueChanged(const wpi::json& msg) { wpi::SmallString<64> key; key.append(type); - key.append("/"); - key.append(device); + if (!device.empty()) { + key.append("/"); + key.append(device); + } auto provider = m_providers.Get(key.str()); if (provider) { diff --git a/simulation/halsim_ws_server/src/main/native/cpp/HALSimWSServer.cpp b/simulation/halsim_ws_server/src/main/native/cpp/HALSimWSServer.cpp index 6bebe33f3a..16e22b1603 100644 --- a/simulation/halsim_ws_server/src/main/native/cpp/HALSimWSServer.cpp +++ b/simulation/halsim_ws_server/src/main/native/cpp/HALSimWSServer.cpp @@ -164,14 +164,19 @@ void HALSimWeb::OnNetValueChanged(const wpi::json& msg) { // generate the key try { - std::string type = msg.at("type").get(); - std::string device = msg.at("device").get(); - auto data = msg.at("data"); + auto& type = msg.at("type").get_ref(); + auto& device = msg.at("device").get_ref(); - auto key = type + "/" + device; - auto provider = m_providers.Get(key); + wpi::SmallString<64> key; + key.append(type); + if (!device.empty()) { + key.append("/"); + key.append(device); + } + + auto provider = m_providers.Get(key.str()); if (provider) { - provider->OnNetValueChanged(data); + provider->OnNetValueChanged(msg.at("data")); } } catch (wpi::json::exception& e) { wpi::errs() << "Error with incoming message: " << e.what() << "\n"; diff --git a/simulation/halsim_ws_server/src/test/native/cpp/main.cpp b/simulation/halsim_ws_server/src/test/native/cpp/main.cpp index 7b6a201558..b03ce50183 100644 --- a/simulation/halsim_ws_server/src/test/native/cpp/main.cpp +++ b/simulation/halsim_ws_server/src/test/native/cpp/main.cpp @@ -5,6 +5,7 @@ /* the project. */ /*----------------------------------------------------------------------------*/ +#include #include #include #include @@ -147,6 +148,44 @@ TEST_F(WebServerIntegrationTest, DigitalInput) { EXPECT_EQ(EXPECTED_VALUE, test_value); } +TEST_F(WebServerIntegrationTest, DriverStation) { + // Create expected results + const bool EXPECTED_VALUE = true; + + // Attach timer to loop for test function + auto ws = HALSimWeb::GetInstance(); + auto loop = ws->GetLoop(); + auto timer = wpi::uv::Timer::Create(loop); + bool done = false; + timer->timeout.connect([&] { + if (done) { + loop->Stop(); + } else { + // Recheck in POLLING_SPEED ms + timer->Start(uv::Timer::Time(POLLING_SPEED)); + } + if (IsConnectedClientWS()) { + wpi::json msg = { + {"type", "DriverStation"}, + {"device", ""}, + {"data", {{">enabled", EXPECTED_VALUE}, {">new_data", true}}}}; + wpi::outs() << "***** Input JSON: " << msg.dump() << "\n"; + WebServerClientTest::GetInstance()->SendMessage(msg); + done = true; + } + }); + timer->Start(uv::Timer::Time(POLLING_SPEED)); + + HAL_RunMain(); + timer->Unreference(); + + // Compare results + HAL_ControlWord cw; + HAL_GetControlWord(&cw); + bool test_value = cw.enabled; + EXPECT_EQ(EXPECTED_VALUE, test_value); +} + int main(int argc, char* argv[]) { ::testing::InitGoogleTest(&argc, argv); HAL_Initialize(500, 0);