[sim] Fix WS blank device messages (e.g. DriverStation)

This commit is contained in:
Peter Johnson
2020-08-31 21:58:26 -07:00
parent 7d6f09f5c7
commit 63487dca76
3 changed files with 54 additions and 8 deletions

View File

@@ -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) {

View File

@@ -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";

View File

@@ -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);