Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 import rospy
00034 from diagnostic_msgs.msg import DiagnosticStatus
00035 from python_qt_binding.QtCore import QMutex, QMutexLocker, QSize, QTimer, Signal
00036 from rqt_robot_monitor.robot_monitor import RobotMonitorWidget
00037 from .icon_tool_button import IconToolButton
00038
00039
00040 class MonitorDashWidget(IconToolButton):
00041 """
00042 A widget which brings up the rqt_robot_monitor.
00043
00044 Times out after certain period of time (set as 5 sec as of Apr 2013)
00045 without receiving diagnostics msg ('/diagnostics_toplevel_state' of
00046 DiagnosticStatus type), status becomes as 'stale'.
00047
00048 :param context: The plugin context to create the monitor in.
00049 :type context: qt_gui.plugin_context.PluginContext
00050 """
00051 _msg_trigger = Signal()
00052
00053 def __init__(self, context, icon_paths=[]):
00054 self._graveyard = []
00055 ok_icon = ['bg-green.svg', 'ic-diagnostics.svg']
00056 warn_icon = ['bg-yellow.svg', 'ic-diagnostics.svg',
00057 'ol-warn-badge.svg']
00058 err_icon = ['bg-red.svg', 'ic-diagnostics.svg', 'ol-err-badge.svg']
00059 stale_icon = ['bg-grey.svg', 'ic-diagnostics.svg',
00060 'ol-stale-badge.svg']
00061
00062 icons = [ok_icon, warn_icon, err_icon, stale_icon]
00063
00064 super(MonitorDashWidget, self).__init__('MonitorWidget', icons,
00065 icon_paths=icon_paths)
00066
00067 self.setFixedSize(self._icons[0].actualSize(QSize(50, 30)))
00068
00069 self._monitor = None
00070 self._close_mutex = QMutex()
00071 self._show_mutex = QMutex()
00072
00073 self._last_update = rospy.Time.now()
00074
00075 self.context = context
00076 self.clicked.connect(self._show_monitor)
00077
00078 self._monitor_shown = False
00079 self.setToolTip('Diagnostics')
00080
00081 self._diagnostics_toplevel_state_sub = rospy.Subscriber(
00082 'diagnostics_toplevel_state',
00083 DiagnosticStatus, self.toplevel_state_callback)
00084 self._top_level_state = -1
00085 self._stall_timer = QTimer()
00086 self._stall_timer.timeout.connect(self._stalled)
00087 self._stalled()
00088 self._plugin_settings = None
00089 self._instance_settings = None
00090 self._msg_trigger.connect(self._handle_msg_trigger)
00091
00092 def toplevel_state_callback(self, msg):
00093 self._is_stale = False
00094 self._msg_trigger.emit()
00095
00096 if self._top_level_state != msg.level:
00097 if (msg.level >= 2):
00098 self.update_state(2)
00099 self.setToolTip("Diagnostics: Error")
00100 elif (msg.level == 1):
00101 self.update_state(1)
00102 self.setToolTip("Diagnostics: Warning")
00103 else:
00104 self.update_state(0)
00105 self.setToolTip("Diagnostics: OK")
00106 self._top_level_state = msg.level
00107
00108 def _handle_msg_trigger(self):
00109 self._stall_timer.start(5000)
00110
00111 def _stalled(self):
00112 self._stall_timer.stop()
00113 self._is_stale = True
00114 self.update_state(3)
00115 self._top_level_state = 3
00116 self.setToolTip("Diagnostics: Stale\nNo message received on "
00117 "/diagnostics_agg in the last 5 seconds")
00118
00119 def _show_monitor(self):
00120 with QMutexLocker(self._show_mutex):
00121 try:
00122 if self._monitor_shown:
00123 self.context.remove_widget(self._monitor)
00124 self._monitor_close()
00125 self._monitor_shown = False
00126 else:
00127 self._monitor = RobotMonitorWidget(self.context,
00128 '/diagnostics_agg')
00129 if self._plugin_settings:
00130 self._monitor.restore_settings(self._plugin_settings,
00131 self._instance_settings)
00132 self.context.add_widget(self._monitor)
00133 self._monitor_shown = True
00134 except Exception:
00135 if self._monitor_shown == False:
00136 raise
00137
00138
00139 self._monitor_shown = False
00140 self._show_monitor()
00141
00142 def _monitor_close(self):
00143 if self._monitor_shown:
00144 with QMutexLocker(self._close_mutex):
00145 if self._plugin_settings:
00146 self._monitor.save_settings(self._plugin_settings,
00147 self._instance_settings)
00148 self._monitor.shutdown()
00149 self._monitor.close()
00150 self._graveyard.append(self._monitor)
00151 self._monitor = None
00152
00153 def shutdown_widget(self):
00154 self._stall_timer.stop()
00155 if self._monitor:
00156 self._monitor.shutdown()
00157 self._diagnostics_toplevel_state_sub.unregister()
00158
00159 def save_settings(self, plugin_settings, instance_settings):
00160 if self._monitor_shown:
00161 self._monitor.save_settings(self._plugin_settings,
00162 self._instance_settings)
00163
00164 def restore_settings(self, plugin_settings, instance_settings):
00165 self._plugin_settings = plugin_settings
00166 self._instance_settings = instance_settings