Go to the documentation of this file.00001 import rosgraph
00002 import rosnode
00003 QUIET_NAMES = ['/rosout','/tf']
00004
00005 from diarc.base_adapter import *
00006 from ros_topology import *
00007 from diarc.view import BlockItemAttributes
00008 from diarc.view import BandItemAttributes
00009 from diarc.view import SnapItemAttributes
00010
00011
00012
00013 class RosAdapter(BaseAdapter):
00014 """ Implements the Adapter interface for the View and provides hooks for
00015 populating and implementing the ros specific version of the topology.
00016 """
00017
00018 def __init__(self,view):
00019 super(RosAdapter,self).__init__(RosSystemGraph(),view)
00020 self._topology.hide_disconnected_snaps = True
00021 self._master = rosgraph.Master('/RosSystemGraph')
00022
00023 def get_block_item_attributes(self, block_index):
00024 """ Overloads the BaseAdapters stock implementation of this method """
00025 block = self._topology.blocks[block_index]
00026 attrs = BlockItemAttributes()
00027 attrs.bgcolor = "white"
00028 attrs.border_color = "red"
00029 attrs.border_width = 0
00030 attrs.label = block.vertex.name
00031 attrs.label_color = "red"
00032 attrs.spacerwidth = 20
00033 attrs.draw_debug = True
00034 return attrs
00035
00036 def get_band_item_attributes(self, band_altitude):
00037 """ Overloads the BaseAdapters stock implementation of this method """
00038 band = self._topology.bands[band_altitude]
00039 attrs = BandItemAttributes()
00040 attrs.bgcolor = "white"
00041 attrs.border_color = "red"
00042 attrs.label = band.edge.name
00043 attrs.label_color = "red"
00044 attrs.width = 15
00045 attrs.draw_debug = True
00046 return attrs
00047
00048 def get_snap_item_attributes(self, snapkey):
00049 """ Default method for providing some stock settings for snaps """
00050 attrs = SnapItemAttributes()
00051 attrs.bgcolor = "white"
00052 attrs.border_color = "red"
00053 attrs.border_width = 0
00054 attrs.label = ""
00055 attrs.label_color = "red"
00056 attrs.width = 20
00057 attrs.draw_debug = True
00058 return attrs
00059
00060 def update_model(self):
00061 """ query the ros master for information about the state of the system """
00062
00063 allCurrentTopics = self._master.getPublishedTopics('/')
00064 allCurrentTopicNames = [x[0] for x in allCurrentTopics]
00065
00066 rsgTopics = self._topology.topics
00067
00068
00069 for topic in rsgTopics.values():
00070 if topic.name not in allCurrentTopicNames:
00071 print "Removing Topic",topic.name, "not found in ",allCurrentTopicNames
00072 topic.release()
00073
00074
00075 for topicName, topicType in allCurrentTopics:
00076 if topicName not in rsgTopics:
00077 topic = Topic(self._topology,topicName,topicType)
00078
00079
00080 allCurrentNodes = rosnode.get_node_names()
00081
00082 rsgNodes = self._topology.nodes
00083
00084
00085 for node in rsgNodes.values():
00086 if node.name not in allCurrentNodes:
00087 print "Removing Node",node.name, "not found in ",allCurrentNodes
00088 node.release()
00089
00090
00091 for name in allCurrentNodes:
00092 if name not in rsgNodes:
00093 node = Node(self._topology,name)
00094 try:
00095 node.location = self._master.lookupNode(name)
00096 except socket.error:
00097 raise Exception("Unable to communicate with master!")
00098
00099
00100 systemState = self._master.getSystemState()
00101
00102 for topicName, publishersList in systemState[0]:
00103 if topicName in QUIET_NAMES:
00104 continue
00105 rsgPublishers = self._topology.topics[topicName].publishers
00106
00107 for publisher in rsgPublishers:
00108 if publisher.node.name not in publishersList:
00109 publisher.release()
00110
00111 for nodeName in publishersList:
00112 if nodeName not in [pub.node.name for pub in rsgPublishers]:
00113 publisher = Publisher(self._topology,self._topology.nodes[nodeName],self._topology.topics[topicName])
00114
00115
00116 for topicName, subscribersList in systemState[1]:
00117 if topicName in QUIET_NAMES:
00118 continue
00119 try:
00120 rsgSubscribers = self._topology.topics[topicName].subscribers
00121 except:
00122 print topicName,"not found in"
00123 continue
00124
00125 for subscriber in rsgSubscribers:
00126 if subscriber.node.name not in subscribersList:
00127 subscriber.release()
00128
00129 for nodeName in subscribersList:
00130 if nodeName not in [sub.node.name for sub in rsgSubscribers]:
00131 subscriber = Subscriber(self._topology,self._topology.nodes[nodeName],self._topology.topics[topicName])
00132
00133 self._update_view()
00134
00135
00136
00137