00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 import rospy
00019 import time
00020 import os
00021 import re
00022 import subprocess
00023 import rosnode
00024
00025 from roslib.packages import get_pkg_dir
00026
00027 from python_qt_binding.QtGui import *
00028 from python_qt_binding.QtCore import *
00029
00030 from python_qt_binding import loadUi
00031
00032 from custom_rosnode import rosnode_ping_all, rosnode_cleanup
00033 from node_item import NodeItem
00034
00035 from airbus_cobot_gui import Alarm
00036
00037 from airbus_plugin_node_manager.res import R
00038
00039 class ThreadNodePingAll(QThread):
00040 def __init__(self):
00041 QThread.__init__(self)
00042 self.stopFlag = False
00043
00044 def stop(self):
00045 self.stopFlag = True
00046
00047 def run(self):
00048
00049 while not self.stopFlag:
00050 try:
00051 alive_nodes, dead_nodes = rosnode_ping_all()
00052 self.emit(SIGNAL("pingStates"), alive_nodes, dead_nodes)
00053 except:
00054 pass
00055 rospy.sleep(2.0)
00056
00057
00058 class TableMonitoringNodes:
00059
00060 def __init__(self, parent):
00061
00062 self._parent = parent
00063
00064 QObject.connect(self._parent._but_cleanup,
00065 SIGNAL('clicked()'),self.onCleanup)
00066
00067 self._datamodel = QStandardItemModel(0, 5, self._parent)
00068 self._parent._table_monitor.setModel(self._datamodel)
00069 self._parent._table_monitor.verticalHeader().setVisible(False)
00070
00071 self._master = rosnode.rosgraph.Master(rosnode.ID)
00072 self._table_dict = {}
00073 self._qtable_index = 0
00074 self._deaded_nodes_alarm_register = []
00075
00076 self._init_node_table()
00077
00078 import threading
00079 self._mutex = threading.Lock()
00080
00081 self.thread_rosnode_ping_all = ThreadNodePingAll()
00082 QObject.connect(self.thread_rosnode_ping_all,
00083 SIGNAL('pingStates'), self._refresh_node_table)
00084
00085 def onStart(self):
00086 self.onCleanup()
00087 self.thread_rosnode_ping_all.start()
00088
00089 def onCleanup(self):
00090
00091 rosnode_cleanup()
00092
00093 with self._mutex:
00094 for i in range(self._datamodel.rowCount()):
00095 self._datamodel.removeRow(i)
00096 self._table_dict = {}
00097 self._init_node_table()
00098 self._deaded_nodes_alarm_register = []
00099
00100 def onClose(self):
00101 self.thread_rosnode_ping_all.stop()
00102 self.thread_rosnode_ping_all.wait()
00103
00104 def _init_node_table(self):
00105
00106 nodes = rosnode._sub_rosnode_listnodes()
00107 nodelist = nodes.split('\n')
00108
00109 for i in range(len(nodelist)):
00110 uri = rosnode.get_api_uri(self._master, nodelist[i])
00111 uri = uri.replace('/','')
00112 uri = '/'+uri.split(':')[1]
00113 self._add_node(uri, nodelist[i], i)
00114
00115 self._qtable_index = len(nodelist)
00116
00117 self._parent._table_monitor.resizeColumnsToContents()
00118 self._parent._table_monitor.resizeRowsToContents()
00119
00120 def _add_node(self, uri, node, index):
00121
00122 new_node = NodeItem(uri,node)
00123
00124 self._datamodel.setItem(index, 0, QStandardItem())
00125
00126 qindex = self._datamodel.index(index, 0, QModelIndex())
00127 self._parent._table_monitor.setIndexWidget(qindex, new_node.uri)
00128
00129 qindex = self._datamodel.index(index, 1, QModelIndex())
00130 self._parent._table_monitor.setIndexWidget(qindex, new_node.name)
00131
00132 qindex = self._datamodel.index(index, 2, QModelIndex())
00133 self._parent._table_monitor.setIndexWidget(qindex, new_node.status)
00134
00135 qindex = self._datamodel.index(index, 3, QModelIndex())
00136 self._parent._table_monitor.setIndexWidget(qindex, new_node.ping)
00137
00138 qindex = self._datamodel.index(index, 4, QModelIndex())
00139 self._parent._table_monitor.setIndexWidget(qindex, new_node.button_start_stop_widget)
00140
00141 self._table_dict.update({node:new_node})
00142
00143 def _refresh_node_table(self, alive_nodes, dead_nodes):
00144 with self._mutex:
00145
00146 self._nb_running = len(alive_nodes)
00147
00148 if alive_nodes.keys() != self._table_dict.keys():
00149 self._update_node_table(alive_nodes)
00150
00151 for node, ping_time in alive_nodes.items():
00152 if node in self._table_dict.keys():
00153 self._table_dict[node].refresh(NodeItem.RUNNING, ping_time)
00154
00155 for node in dead_nodes:
00156
00157 if node not in self._deaded_nodes_alarm_register:
00158 self._deaded_nodes_alarm_register.append(node)
00159 self._parent.sendAlarm(Alarm.WARNING, "The node %s is dead !"%node)
00160
00161 if node in self._table_dict.keys():
00162 self._table_dict[node].refresh(NodeItem.ABARTED)
00163
00164 def _update_node_table(self, alive_nodes):
00165
00166 for node in alive_nodes.keys():
00167 if node not in self._table_dict.keys():
00168 uri = rosnode.get_api_uri(self._master, node)
00169 uri = uri.replace('/','')
00170 uri = '/'+uri.split(':')[1]
00171 self._add_node(uri, node, self._qtable_index)
00172 self._qtable_index += 1
00173
00174 for node in self._table_dict.keys():
00175 if node not in alive_nodes.keys():
00176
00177 self._table_dict[node].refresh(NodeItem.SHUTDOWN)
00178
00179 if node not in self._deaded_nodes_alarm_register:
00180 self._deaded_nodes_alarm_register.append(node)
00181 self._parent.sendAlarm(Alarm.WARNING, "The node %s is dead !"%node)
00182
00183 self._parent._table_monitor.resizeColumnsToContents()
00184 self._parent._table_monitor.resizeRowsToContents()
00185
00186 def translate(self, lng):
00187
00188 self._parent.monitor_header_label.setText(R.values.strings.nodes_manager_title(lng))
00189 self._parent._but_cleanup.setText(R.values.strings.cleanup(lng))
00190
00191 header_label = [R.values.strings.machine(lng),
00192 R.values.strings.node(lng),
00193 R.values.strings.status(lng),
00194 R.values.strings.ping(lng),
00195 R.values.strings.start_stop(lng)]
00196
00197 for i in range(len(header_label)):
00198 self._datamodel.setHeaderData(i, Qt.Horizontal, header_label[i])
00199
00200