def perform_service_call(service_name):
"""
POST /api/<version>/service/<service_name>
POST to a service to change it somehow. service_name may be a path.
For example, to start an environmental recipe in an environment, use the
start_recipe service:
POST /api/<version>/service/<environment_id>/start_recipe {"recipe_id": <id>}
"""
# Add leading slash to service_name. ROS qualifies all services with a
# leading slash.
service_name = '/' + service_name
# Find the class that ROS autogenerates from srv files that is associated
# with this service.
try:
ServiceClass = rosservice.get_service_class_by_name(service_name)
except rosservice.ROSServiceException as e:
return error(e)
# If we made it this far, the service exists.
# Wait for service to be ready before attempting to use it.
try:
rospy.wait_for_service(service_name, 1)
except rospy.ROSException as e:
raise socket.error()
# Get JSON args if they exist. If they don't, treat as if empty array
# was passed.
json = request.get_json(silent=True)
args = json if json else []
service_proxy = rospy.ServiceProxy(service_name, ServiceClass)
try:
# Unpack the list of args and pass to service_proxy function.
res = service_proxy(*args)
except (rospy.ServiceException, TypeError) as e:
return error(e)
status_code = 200 if getattr(res, "success", True) else 400
data = {k: getattr(res, k) for k in res.__slots__}
return jsonify(data), status_code
评论列表
文章目录