mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-05 03:21:42 +00:00
[sim] Fix WS blank device messages (e.g. DriverStation)
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -164,14 +164,19 @@ void HALSimWeb::OnNetValueChanged(const wpi::json& msg) {
|
||||
// generate the key
|
||||
|
||||
try {
|
||||
std::string type = msg.at("type").get<std::string>();
|
||||
std::string device = msg.at("device").get<std::string>();
|
||||
auto data = msg.at("data");
|
||||
auto& type = msg.at("type").get_ref<const std::string&>();
|
||||
auto& device = msg.at("device").get_ref<const std::string&>();
|
||||
|
||||
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";
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include <hal/DriverStation.h>
|
||||
#include <hal/HALBase.h>
|
||||
#include <hal/Main.h>
|
||||
#include <hal/simulation/DIOData.h>
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user