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
00034
00035 from diagnostic_msgs.msg import DiagnosticStatus
00036 from python_qt_binding.QtGui import QColor, QIcon
00037 import rospy
00038
00039
00040 class Util(object):
00041
00042
00043 SECONDS_TIMELINE = 30
00044
00045
00046 _ERR_ICON = QIcon.fromTheme('dialog-error')
00047 _WARN_ICON = QIcon.fromTheme('dialog-warning')
00048 _OK_ICON = QIcon.fromTheme('emblem-default')
00049
00050 _STALE_ICON = QIcon.fromTheme('dialog-question')
00051
00052 IMG_DICT = {0: _OK_ICON, 1: _WARN_ICON, 2: _ERR_ICON, 3: _STALE_ICON}
00053
00054 COLOR_DICT = {0: QColor(85, 178, 76),
00055 1: QColor(222, 213, 17),
00056 2: QColor(178, 23, 46),
00057 3: QColor(40, 23, 176)
00058 }
00059
00060
00061
00062
00063 DiagnosticStatus.STALE = 3
00064
00065 _DICTKEY_TIMES_ERROR = 'times_errors'
00066 _DICTKEY_TIMES_WARN = 'times_warnings'
00067 _DICTKEY_INDEX = 'index'
00068 _DICTKEY_STATITEM = 'statitem'
00069
00070 def __init__(self):
00071 super(Util, self).__init__()
00072
00073 @staticmethod
00074 def update_status_images(diagnostic_status, statusitem):
00075 """
00076 Taken from robot_monitor.robot_monitor_panel.py.
00077
00078 :type status: DiagnosticStatus
00079 :type node: StatusItem
00080 :author: Isaac Saito
00081 """
00082
00083 name = diagnostic_status.name
00084 if (name is not None):
00085
00086 level = diagnostic_status.level
00087 if (diagnostic_status.level != statusitem.last_level):
00088
00089 statusitem.setIcon(0, Util.IMG_DICT[level])
00090 statusitem.last_level = level
00091 return
00092
00093 @staticmethod
00094 def get_grn_resource_name(status_name):
00095 """
00096 :param: status_name is a string that may consists of status names that
00097 are delimited by slash.
00098 :rtype: str
00099 """
00100 name = status_name.split('/')[-1]
00101 rospy.logdebug(' get_grn_resource_name name = %s', name)
00102 return name
00103
00104 @staticmethod
00105 def remove_parent_name(status_name):
00106 return ('/'.join(status_name.split('/')[2:])).strip()
00107
00108 @staticmethod
00109 def get_parent_name(status_name):
00110 return ('/'.join(status_name.split('/')[:-1])).strip()
00111
00112 @staticmethod
00113 def gen_headline_status_green(diagnostic_status):
00114 return "%s" % Util.get_grn_resource_name(diagnostic_status.name)
00115
00116 @staticmethod
00117 def gen_headline_warn_or_err(diagnostic_status):
00118 return "%s : %s" % (Util.get_grn_resource_name(diagnostic_status.name),
00119 diagnostic_status.message)
00120
00121 @staticmethod
00122 def _get_color_for_message(msg, mode=0):
00123 """
00124 :param msg: Either DiagnosticArray or DiagnosticsStatus.
00125 :param mode: int. When 0, this func will iterate msg to find
00126 DiagnosticsStatus.level and put it into a dict.
00127 When 1, this func finds DiagnosticsStatus.level from msg
00128 without iteration (thus, msg is expected to be
00129 DiagnosticsStatus instance).
00130 """
00131
00132 level = 0
00133 min_level = 255
00134
00135 lookup = {}
00136 for status in msg.status:
00137 lookup[status.name] = status
00138
00139 names = [status.name for status in msg.status]
00140 names = [name for name in names
00141 if len(Util.get_parent_name(name)) == 0]
00142 for name in names:
00143 status = lookup[name]
00144 if (status.level > level):
00145 level = status.level
00146 if (status.level < min_level):
00147 min_level = status.level
00148
00149
00150 if (level > 2 and min_level <= 2):
00151 level = 2
00152
00153
00154 rospy.logdebug(' get_color_for_message color lv=%d', level)
00155 return Util.COLOR_DICT[level]
00156
00157 @staticmethod
00158 def get_correspondent(key, list_statitem):
00159 """
00160
00161 :type key: String.
00162 :type list_statitem: DiagnosticsStatus
00163 :rtype: StatusItem
00164 """
00165 names_from_list = [Util.get_grn_resource_name(status.name)
00166 for status in list_statitem]
00167 key_niced = Util.get_grn_resource_name(key)
00168 index_key = -1
00169 statitem_key = None
00170 if key_niced in names_from_list:
00171 index_key = names_from_list.index(key_niced)
00172 statitem_key = list_statitem[index_key]
00173 rospy.logdebug(' get_correspondent index_key=%s statitem_key=%s',
00174 index_key, statitem_key)
00175 return {Util._DICTKEY_INDEX: index_key,
00176 Util._DICTKEY_STATITEM: statitem_key}
00177
00178 @staticmethod
00179 def get_children(name, diag_array):
00180 """
00181
00182 :type msg: DiagnosticArray
00183 :rtype: DiagnosticStatus[]
00184 """
00185
00186 ret = []
00187 for k in diag_array.status:
00188 if k.name.startswith(name):
00189
00190 if not k.name == name:
00191
00192 ret.append(k)
00193 return ret