getNodes.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # Import mesages
4 from ros_monitoring.msg import NodesInformation, Info_node, TopicInfo
5 
6 # Other imports
7 import rospy, bson, rosnode, rosgraph, re
8 from tcppinglib import tcpping
9 from datetime import datetime
10 
11 # Get Nodes class
12 class getNodes:
13  def __init__(self) -> None:
14  # Start the node
15  rospy.init_node('getROSNodes', anonymous=False)
16  rospy.loginfo("Get signal topic started")
17  # Creates the publisher of the messages
18  try:
19  self.message_pub = rospy.Publisher("nodesStatus", NodesInformation, queue_size=10)
20  except Exception as e:
21  rospy.logerr("Failure to create publisher")
22  rospy.logerr("An exception occurred:", type(e).__name__,e.args)
23 
24  while not rospy.is_shutdown():
25  # Get ROS nodes
26  try:
27  node_list = rosnode.get_node_names()
28  except Exception as e:
29  rospy.logerr("Error on get ROS nodes")
30  rospy.logerr("An exception occurred:", type(e).__name__,e.args)
31  # Parsec the ROS nodes
32  nodes = []
33  for node in node_list:
34  try:
35  nodes.append(self.parsecNodeInfo(msg=rosnode.get_node_info_description(node)))
36  except Exception as e:
37  rospy.logerr("Error in the node parsec info:" + str(node))
38  rospy.logerr("An exception occurred:", type(e).__name__,e.args)
39 
40  # Create the topic message
41  try:
42  # Starts the message
43  msg = NodesInformation()
44  # Fill in the message
45  msg.nodes = nodes
46  except Exception as e:
47  rospy.logerr("Error on create the message")
48  rospy.logerr("An exception occurred:", type(e).__name__,e.args)
49  # Publish ROS message
50  self.message_pub.publish(msg)
51  # Wait 1 second to check again
52  rospy.sleep(1)
53  # Keeps the node alive
54  rospy.spin()
55 
56 # Function to parsec the node informarion
57  def parsecNodeInfo(self, msg):
58  # Parsec node infomation
59  try:
60  # Get node name
61  node_name = re.search(r"Node \[(.*)\]", msg).group(1)
62  # Get publications
63  pubs = re.findall(r"\* (.*) \[(.*)\]", re.search(r"Publications:(.*)Subscriptions", msg, re.DOTALL).group(1))
64  # publications = [{"topic": topic, "type": msg_type} for topic, msg_type in pubs]
65  publications = [self.topic2msg({"topic": topic, "type": msg_type}) for topic, msg_type in pubs]
66  # Get subscriptions
67  subs = re.findall(r"\* (.*) \[(.*)\]", re.search(r"Subscriptions:(.*)Services", msg, re.DOTALL).group(1))
68  # subscriptions = [{"topic": topic, "type": msg_type} for topic, msg_type in subs]
69  subscriptions = [self.topic2msg({"topic": topic, "type": msg_type}) for topic, msg_type in subs]
70  # Get services
71  services = re.findall(r"\* (.*)", re.search(r"Services:(.*)", msg, re.DOTALL).group(1))
72  except Exception as e:
73  rospy.logerr("Error on node parsec: " + str(msg))
74  rospy.logerr("An exception occurred:", type(e).__name__,e.args)
75  # Convert to Info_node ROS message
76  try:
77  # Starts the message
78  _msg = Info_node()
79  # Fill in the message
80  _msg.node = str(node_name)
81  _msg.publications = publications
82  _msg.subscriptions = subscriptions
83  _msg.services = services
84  return _msg
85  # Fill in the message
86  except Exception as e:
87  rospy.logerr("Error on convert node to message: " + str(msg))
88  rospy.logerr("An exception occurred:", type(e).__name__,e.args)
89 
90 # Converts the topic information to ROS message TopicInfo
91  def topic2msg(self, msg):
92  try:
93  # Starts the message
94  _msg = TopicInfo()
95  # Fill in the message
96  _msg.topic = str(msg['topic'])
97  _msg.msg_type = str(msg['type'])
98  # Returns the converted message
99  return _msg
100  except Exception as e:
101  rospy.logerr("Error on convert topic to message: " + str(msg))
102  rospy.logerr("An exception occurred:", type(e).__name__,e.args)
103 
104 
105 if __name__ == '__main__':
106  try:
107  getNodes()
108  except rospy.ROSInterruptException:
109  pass
getNodes.getNodes.topic2msg
def topic2msg(self, msg)
Definition: getNodes.py:91
getNodes.getNodes
Definition: getNodes.py:12
getNodes.getNodes.parsecNodeInfo
def parsecNodeInfo(self, msg)
Definition: getNodes.py:57
getNodes.getNodes.__init__
None __init__(self)
Definition: getNodes.py:13
getNodes.getNodes.message_pub
message_pub
Definition: getNodes.py:19


cedri_ros_monitoring
Author(s): Andre Luis Frana
autogenerated on Sun Jul 2 2023 02:45:41