Skip to content

Commit

Permalink
new file: src/v2i-hub/PedestrianPlugin/scripts/sendPSM.py
Browse files Browse the repository at this point in the history
	modified:   src/v2i-hub/PedestrianPlugin/src/PedestrianPlugin.cpp
	modified:   src/v2i-hub/PedestrianPlugin/src/include/PedestrianPlugin.hpp
  • Loading branch information
jwillmartin committed Aug 12, 2024
1 parent d51d9f8 commit d54f07e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 26 deletions.
42 changes: 42 additions & 0 deletions src/v2i-hub/PedestrianPlugin/scripts/sendPSM.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import aiohttp
import asyncio

url = "http://127.0.0.1:9000"
data = '''
<PersonalSafetyMessage>
<basicType><aPEDESTRIAN /></basicType>
<secMark>0</secMark>
<msgCnt>0</msgCnt>
<id>87654321</id>
<position><lat>400774115</lat><long>-1248282086</long><elevation>538</elevation></position>
<accuracy><semiMajor>255</semiMajor><semiMinor>255</semiMinor><orientation>65535</orientation></accuracy>
<speed>0</speed>
<heading>270</heading>
<crossState><true /></crossState>
<clusterSize><medium /></clusterSize>
<clusterRadius>6</clusterRadius>
</PersonalSafetyMessage>
'''

headers = {
'Content-Type': 'application/xml',
}

async def send_request(session, i):
async with session.post(url, data=data, headers=headers) as response:
if response.status == 201:
print(f"Request {i} succeeded")
else:
print(f"Request {i} failed with status code {response.status}")

async def main():
async with aiohttp.ClientSession() as session:
tasks = []
for i in range(1000):
tasks.append(asyncio.create_task(send_request(session, i)))
await asyncio.sleep(0.1) # maintain the interval between requests
await asyncio.gather(*tasks)

if __name__ == "__main__":
asyncio.run(main())

33 changes: 10 additions & 23 deletions src/v2i-hub/PedestrianPlugin/src/PedestrianPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Name : PedestrianPlugin.cpp
// Author : FHWA Saxton Transportation Operations Laboratory
// Version :
// Copyright : Copyright (c) 2019 FHWA Saxton Transportation Operations Laboratory. All rights reserved.
// Copyright : Copyright (c) 2024 FHWA Saxton Transportation Operations Laboratory. All rights reserved.
// Description : Pedestrian Plugin
//==========================================================================

Expand All @@ -25,21 +25,11 @@ PedestrianPlugin::PedestrianPlugin(const std::string &name) : PluginClient(name)
{
if (_signSimClient != nullptr)
_signSimClient.reset();

UpdateConfigSettings();
std::lock_guard<mutex> lock(_cfgLock);

std::thread webServiceThread(&PedestrianPlugin::StartWebService, this);
webServiceThread.detach(); // wait for the thread to finish
// StartWebService = std::move(std::jthread{StartWebService});
runningWebService = true;
}

int PedestrianPlugin::StartWebSocket()
{
PLOG(logDEBUG) << "In PedestrianPlugin::StartWebSocket ";
// std::jthread StartWebSocket;

flirSession = std::make_shared<FLIRWebSockAsyncClnSession>(ioc);

// Launch the asynchronous operation
Expand All @@ -48,10 +38,8 @@ int PedestrianPlugin::StartWebSocket()
PLOG(logDEBUG) << "Successfully running the I/O service";
runningWebSocket = true;

// Run the I/O service. The call will return when
// the socket is closed.
// Run the I/O service. The call will return when the socket is closed.
ioc.run();
runningWebSocket = false;

return EXIT_SUCCESS;
}
Expand All @@ -71,7 +59,7 @@ void PedestrianPlugin::StopWebSocket()
}
}

[[noreturn]] int PedestrianPlugin::checkXML()
void PedestrianPlugin::checkXML()
{
// std::jthread checkXML;
//if a new psm xml has been generated the FLIR web socket, send it to the BroadcastPSM function
Expand Down Expand Up @@ -140,7 +128,6 @@ void PedestrianPlugin::PedestrianRequestHandler(QHttpEngine::Socket *socket)
int PedestrianPlugin::StartWebService()
{
PLOG(logDEBUG) << "In PedestrianPlugin::StartWebService";
// std::jthread webServiceThread;

// Web services
std::array<char*, 1> placeholderX = {nullptr};
Expand Down Expand Up @@ -203,15 +190,13 @@ void PedestrianPlugin::UpdateConfigSettings()
{
PLOG(logDEBUG) << "Starting WebSocket Thread";
std::thread webSocketThread(&PedestrianPlugin::StartWebSocket, this);
// StartWebSocket = std::move(std::jthread{StartWebSocket});
PLOG(logDEBUG) << "WebSocket Thread started!!";
webSocketThread.detach(); // wait for the thread to finish

PLOG(logDEBUG) << "Starting XML Thread";
std::thread xmlThread(&PedestrianPlugin::checkXML, this);
// checkXML = std::move(std::jthread{checkXML});
PLOG(logDEBUG) << "XML Thread started!!";
xmlThread.detach(); // wait for the thread to finish
webSocketThread.join(); // wait for the thread to finish
xmlThread.join(); // wait for the thread to finish
}
}

Expand All @@ -222,8 +207,8 @@ void PedestrianPlugin::UpdateConfigSettings()
{
PLOG(logDEBUG) << "Starting WebService Thread";
std::thread webServiceThread(&PedestrianPlugin::StartWebService, this);
webServiceThread.detach(); // wait for the thread to finish
PLOG(logDEBUG) << "WebService Thread started";
PLOG(logDEBUG) << "WebService Thread started";
webServiceThread.join(); // wait for the thread to finish
}
}
else
Expand All @@ -232,12 +217,13 @@ void PedestrianPlugin::UpdateConfigSettings()
StopWebService();
StopWebSocket();
}

}

void PedestrianPlugin::OnConfigChanged(const char *key, const char *value)
{
PluginClient::OnConfigChanged(key, value);
UpdateConfigSettings();
// UpdateConfigSettings();
}

void PedestrianPlugin::OnStateChange(IvpPluginState state)
Expand Down Expand Up @@ -279,6 +265,7 @@ void PedestrianPlugin::BroadcastPsm(const std::string &psmJson)
msg->refresh_timestamp();

auto *rMsg = dynamic_cast<routeable_message *>(msg.get());

BroadcastMessage(*rMsg);

PLOG(logINFO) << " Pedestrian Plugin :: Broadcast PSM:: " << psmENC.get_payload_str();
Expand Down
5 changes: 2 additions & 3 deletions src/v2i-hub/PedestrianPlugin/src/include/PedestrianPlugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Name : PedestrianPlugin.cpp
// Author : FHWA Saxton Transportation Operations Laboratory
// Version :
// Copyright : Copyright (c) 2019 FHWA Saxton Transportation Operations Laboratory. All rights reserved.
// Copyright : Copyright (c) 2024 FHWA Saxton Transportation Operations Laboratory. All rights reserved.
// Description : Pedestrian Plugin
//==========================================================================
#pragma once
Expand Down Expand Up @@ -74,7 +74,7 @@ class PedestrianPlugin: public PluginClient
void OnWebSocketDataReceived(QString message);
void OnWebSocketClosed();

[[noreturn]] int checkXML();
void checkXML();

private:
std::unique_ptr<tmx::utils::UdpClient> _signSimClient = nullptr;
Expand Down Expand Up @@ -104,4 +104,3 @@ class PedestrianPlugin: public PluginClient
};

};

0 comments on commit d54f07e

Please sign in to comment.