Skip to content

Commit 80e5734

Browse files
authored
add library scenario_execution_network (#40)
1 parent 2c8ca21 commit 80e5734

File tree

12 files changed

+233
-1
lines changed

12 files changed

+233
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include scenario_execution_network/lib_osc/*.osc
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Scenario Execution Library for network
2+
3+
The `scenario_execution_network` package provides actions to interact with network services.
4+
5+
It provides the following scenario execution library:
6+
7+
- `network.osc`: Actions specific to network functions
8+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0"?>
2+
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
3+
<package format="3">
4+
<name>scenario_execution_network</name>
5+
<version>1.3.0</version>
6+
<description>Scenario Execution library for network functionality</description>
7+
<author email="[email protected]">Frederik Pasch</author>
8+
<maintainer email="[email protected]">Frederik Pasch</maintainer>
9+
<license>Apache-2.0</license>
10+
11+
<depend>scenario_execution</depend>
12+
13+
<test_depend>ament_copyright</test_depend>
14+
<test_depend>ament_flake8</test_depend>
15+
<test_depend>ament_pep257</test_depend>
16+
<test_depend>python3-pytest</test_depend>
17+
18+
<export>
19+
<build_type>ament_python</build_type>
20+
</export>
21+
</package>

libs/scenario_execution_network/resource/scenario_execution_network

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (C) 2025 Frederik Pasch
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing,
10+
# software distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions
13+
# and limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
17+
from . import actions
18+
19+
__all__ = [
20+
'actions'
21+
]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright (C) 2025 Frederik Pasch
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing,
10+
# software distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions
13+
# and limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Copyright (C) 2025 Frederik Pasch
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing,
10+
# software distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions
13+
# and limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
17+
from enum import Enum
18+
from scenario_execution.actions.base_action import BaseAction
19+
import py_trees
20+
from concurrent.futures import ThreadPoolExecutor
21+
import requests
22+
23+
class RequestStatus(Enum):
24+
IDLE = 1
25+
REQUESTING = 2
26+
DONE = 3
27+
28+
29+
class HttpGet(BaseAction):
30+
"""
31+
Class to perform an HTTP GET request asynchronously
32+
"""
33+
34+
def __init__(self) -> None:
35+
super().__init__()
36+
self.response = None
37+
self.future = None
38+
self.parameters = {}
39+
self.executor = ThreadPoolExecutor(max_workers=1)
40+
self.current_state = RequestStatus.IDLE
41+
42+
def execute(self, url: str, parameters: list) -> None: # pylint: disable=arguments-differ,arguments-renamed
43+
self.url = url
44+
self.parameters = {}
45+
for param in parameters:
46+
self.parameters[param["key"]] = param["value"]
47+
48+
def update(self) -> py_trees.common.Status:
49+
if self.current_state == RequestStatus.IDLE:
50+
# Start the async HTTP request in a separate thread
51+
self.future = self.executor.submit(self._make_request)
52+
self.current_state = RequestStatus.REQUESTING
53+
return py_trees.common.Status.RUNNING
54+
55+
elif self.current_state == RequestStatus.REQUESTING:
56+
if self.future.done():
57+
try:
58+
self.response = self.future.result()
59+
if self.response.status_code == 200:
60+
self.current_state = RequestStatus.DONE
61+
return py_trees.common.Status.SUCCESS
62+
else:
63+
self.logger.error(f"HTTP GET returned status code {self.response.status_code}")
64+
self.current_state = RequestStatus.DONE
65+
return py_trees.common.Status.FAILURE
66+
except Exception as e:
67+
self.logger.error(f"HTTP GET request failed: {e}")
68+
return py_trees.common.Status.FAILURE
69+
else:
70+
return py_trees.common.Status.RUNNING
71+
72+
elif self.current_state == RequestStatus.DONE:
73+
return py_trees.common.Status.SUCCESS
74+
75+
return py_trees.common.Status.FAILURE
76+
77+
def _make_request(self):
78+
"""Make the HTTP request synchronously in a thread pool"""
79+
return requests.get(self.url, params=self.parameters)
80+
81+
def shutdown(self):
82+
"""Clean up the thread pool on shutdown"""
83+
if self.executor:
84+
self.executor.shutdown(wait=False)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright (C) 2025 Frederik Pasch
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing,
10+
# software distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions
13+
# and limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
17+
18+
def get_osc_library():
19+
return 'scenario_execution_network', 'network.osc'
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import osc.types
2+
3+
###########
4+
# Actions
5+
###########
6+
7+
action http_get:
8+
# Perform an HTTP GET request to the specified URL
9+
url: string # URL to send the GET request to
10+
parameters: list of key_value # Optional query parameters for the request
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[develop]
2+
script_dir=$base/lib/scenario_execution_network
3+
[install]
4+
install_scripts=$base/lib/scenario_execution_network

0 commit comments

Comments
 (0)