00001
00002 from roslib import load_manifest; load_manifest('rosapi');
00003 from rospy import init_node
00004 from rospy import spin
00005 from rospy import Service
00006 from rospy import ROSInterruptException
00007 from rospy import loginfo
00008 from rospy import get_rostime
00009
00010 import proxy, objectutils, params
00011 from rosapi.srv import *
00012 from rosapi.msg import *
00013
00014
00015 def register_services():
00016 Service('rosapi/topics', Topics, get_topics)
00017 Service('rosapi/topics_for_type', TopicsForType, get_topics_for_type)
00018 Service('rosapi/services', Services, get_services)
00019 Service('rosapi/nodes', Nodes, get_nodes)
00020 Service('rosapi/topic_type', TopicType, get_topic_type)
00021 Service('rosapi/service_type', ServiceType, get_service_type)
00022 Service('rosapi/publishers', Publishers, get_publishers)
00023 Service('rosapi/subscribers', Subscribers, get_subscribers)
00024 Service('rosapi/service_providers', ServiceProviders, get_service_providers)
00025 Service('rosapi/service_node', ServiceNode, get_service_node)
00026 Service('rosapi/service_host', ServiceHost, get_service_host)
00027 Service('rosapi/message_details', MessageDetails, get_message_details)
00028 Service('rosapi/service_request_details', ServiceRequestDetails, get_service_request_details)
00029 Service('rosapi/service_response_details', ServiceResponseDetails, get_service_response_details)
00030 Service('rosapi/set_param', SetParam, set_param)
00031 Service('rosapi/get_param', GetParam, get_param)
00032 Service('rosapi/has_param', HasParam, has_param)
00033 Service('rosapi/search_param', SearchParam, search_param)
00034 Service('rosapi/delete_param', DeleteParam, delete_param)
00035 Service('rosapi/get_param_names', GetParamNames, get_param_names)
00036 Service('rosapi/get_time', GetTime, get_time)
00037
00038 def get_topics(request):
00039 """ Called by the rosapi/Topics service. Returns a list of all the topics being published. """
00040 return TopicsResponse(proxy.get_topics())
00041
00042 def get_topics_for_type(request):
00043 """ Called by the rosapi/TopicsForType service. Returns a list of all the topics that are publishing a given type """
00044 return TopicsForTypeResponse(proxy.get_topics_for_type(request.type))
00045
00046 def get_services(request):
00047 """ Called by the rosapi/Services service. Returns a list of all the services being advertised. """
00048 return ServicesResponse(proxy.get_services())
00049
00050 def get_nodes(request):
00051 """ Called by the rosapi/Nodes service. Returns a list of all the nodes that are registered """
00052 return NodesResponse(proxy.get_nodes())
00053
00054 def get_topic_type(request):
00055 """ Called by the rosapi/TopicType service. Given the name of a topic, returns the name of the type of that topic.
00056 Request class has one field, 'topic', which is a string value (the name of the topic)
00057 Response class has one field, 'type', which is a string value (the type of the topic)
00058 If the topic does not exist, an empty string is returned. """
00059 return TopicTypeResponse(proxy.get_topic_type(request.topic))
00060
00061 def get_service_type(request):
00062 """ Called by the rosapi/ServiceType service. Given the name of a service, returns the type of that service
00063 Request class has one field, 'service', which is a string value (the name of the service)
00064 Response class has one field, 'type', which is a string value (the type of the service)
00065 If the service does not exist, an empty string is returned. """
00066 return ServiceTypeResponse(proxy.get_service_type(request.service))
00067
00068 def get_publishers(request):
00069 """ Called by the rosapi/Publishers service. Given the name of a topic, returns a list of node names
00070 that are publishing on that topic. """
00071 return PublishersResponse(proxy.get_publishers(request.topic))
00072
00073 def get_subscribers(request):
00074 """ Called by the rosapi/Subscribers service. Given the name of a topic, returns a list of node names
00075 that are subscribing to that topic. """
00076 return SubscribersResponse(proxy.get_subscribers(request.topic))
00077
00078 def get_service_providers(request):
00079 """ Called by the rosapi/ServiceProviders service. Given the name of a topic, returns a list of node names
00080 that are advertising that service type """
00081 return ServiceProvidersResponse(proxy.get_service_providers(request.service))
00082
00083 def get_service_node(request):
00084 """ Called by the rosapi/ServiceNode service. Given the name of a service, returns the name of the node
00085 that is providing that service. """
00086 return ServiceNodeResponse(proxy.get_service_node(request.service))
00087
00088 def get_service_host(request):
00089 """ Called by the rosapi/ServiceNode service. Given the name of a service, returns the name of the machine
00090 that is hosting that service. """
00091 return ServiceHostResponse(proxy.get_service_host(request.service))
00092
00093 def get_message_details(request):
00094 """ Called by the rosapi/MessageDetails service. Given the name of a message type, returns the TypeDef
00095 for that type."""
00096 return MessageDetailsResponse([dict_to_typedef(d) for d in objectutils.get_typedef_recursive(request.type)])
00097
00098 def get_service_request_details(request):
00099 """ Called by the rosapi/ServiceRequestDetails service. Given the name of a service type, returns the TypeDef
00100 for the request message of that service type. """
00101 return ServiceRequestDetailsResponse([dict_to_typedef(d) for d in objectutils.get_service_request_typedef_recursive(request.type)])
00102
00103 def get_service_response_details(request):
00104 """ Called by the rosapi/ServiceResponseDetails service. Given the name of a service type, returns the TypeDef
00105 for the response message of that service type. """
00106 return ServiceResponseDetailsResponse([dict_to_typedef(d) for d in objectutils.get_service_response_typedef_recursive(request.type)])
00107
00108 def set_param(request):
00109 params.set_param(request.name, request.value)
00110 return SetParamResponse()
00111
00112 def get_param(request):
00113 return GetParamResponse(params.get_param(request.name, request.default))
00114
00115 def has_param(request):
00116 return HasParamResponse(params.has_param(request.name))
00117
00118 def search_param(request):
00119 return SearchParamResponse(params.search_param(request.name))
00120
00121 def delete_param(request):
00122 params.delete_param(request.name)
00123 return DeleteParamResponse()
00124
00125 def get_param_names(request):
00126 return GetParamNamesResponse(params.get_param_names())
00127
00128 def get_time(request):
00129 return GetTimeResponse(get_rostime())
00130
00131 def dict_to_typedef(typedefdict):
00132 typedef = TypeDef()
00133 typedef.type = typedefdict["type"]
00134 typedef.fieldnames = typedefdict["fieldnames"]
00135 typedef.fieldtypes = typedefdict["fieldtypes"]
00136 typedef.fieldarraylen = typedefdict["fieldarraylen"]
00137 typedef.examples = typedefdict["examples"]
00138 return typedef
00139
00140 if __name__ == '__main__':
00141 try:
00142 init_node('rosapi')
00143 register_services()
00144 loginfo("Rosapi started")
00145 spin()
00146 except ROSInterruptException:
00147 pass