diag_agg_listener.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Software License Agreement (BSD License)
00004 #
00005 # Copyright (c) 2010, Willow Garage, Inc.
00006 # All rights reserved.
00007 #
00008 # Redistribution and use in source and binary forms, with or without
00009 # modification, are permitted provided that the following conditions
00010 # are met:
00011 #
00012 #  * Redistributions of source code must retain the above copyright
00013 #    notice, this list of conditions and the following disclaimer.
00014 #  * Redistributions in binary form must reproduce the above
00015 #    copyright notice, this list of conditions and the following
00016 #    disclaimer in the documentation and/or other materials provided
00017 #    with the distribution.
00018 #  * Neither the name of Willow Garage, Inc. nor the names of its
00019 #    contributors may be used to endorse or promote products derived
00020 #    from this software without specific prior written permission.
00021 #
00022 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 # POSSIBILITY OF SUCH DAMAGE.
00034 #
00035 
00036 ##\author Kevin Watts
00037 ##\brief Listens to diagnostics_agg and reports statusx
00038 
00039 
00040 from __future__ import with_statement
00041 
00042 PKG = 'pr2_hardware_test_monitor'
00043 import roslib; roslib.load_manifest(PKG)
00044 
00045 from diagnostic_msgs.msg import DiagnosticArray, DiagnosticStatus, KeyValue
00046 from std_srvs.srv import *
00047 
00048 import rospy
00049 
00050 import threading
00051 import types
00052 
00053 from robot_monitor.robot_monitor_panel import State
00054 
00055 from pr2_hw_listener import PR2HWListenerBase
00056 
00057 class DiagAggState(State):
00058     def __init__(self, whitelist = None, ignore_categories = None):
00059         """
00060         \param whitelist [ str ] : Values to focus on.
00061         \param ignore_categories [ str ] : Ignore matching values
00062         """
00063         State.__init__(self)
00064 
00065         self._whitelist = whitelist 
00066         self._ignore_categories = ignore_categories if ignore_categories else []
00067               
00068     def _is_ignored(self, name):
00069         """
00070         Check if item should be ignored.
00071         If we have a whitelist, False if item in it. 
00072         Else if, True if we're supposed to ignore it.
00073         """
00074         if self._whitelist:
00075             if name in self._whitelist:
00076                 return False
00077             elif name.lstrip('/') in self._whitelist:
00078                 return False
00079             else:
00080                 return True
00081 
00082         for ignore in self._ignore_categories:
00083             if name == ignore:
00084                 return True
00085             if '/' + ignore == name:
00086                 return True
00087 
00088         return False
00089 
00090 
00091     def get_top_level_state(self):
00092         """
00093         Get top level status (OK, WARN, ERROR, STALE) of diagnostics
00094         Handles ignore categories, etc.
00095         """
00096         level = -1
00097         min_level = 255
00098         msgs = []
00099         
00100         if len(self.get_items()) == 0:
00101             return level
00102     
00103         for item in self.get_items().itervalues():
00104             # Only look at "top level" items
00105             if self.get_parent(item) is not None:
00106                 continue
00107             
00108             if self._is_ignored(item.status.name):
00109                 continue
00110 
00111             if item.status.level > level:
00112                 level = item.status.level
00113                 if item.status.level < min_level:
00114                     min_level = item.status.level
00115 
00116             if item.status.level > 0:
00117                 msgs.append(item.status.name.lstrip('/'))
00118                 
00119         # Top level is error if we have stale items, unless all stale
00120         if level > 2 and min_level <= 2:
00121             level = 2
00122             
00123         return level, msgs
00124 
00125 def _convert_to_list(val):
00126     if type(val) in (list, tuple):
00127         return val
00128     else:
00129         return [ str(val) ]
00130 
00131 class DiagAggListener(PR2HWListenerBase):
00132     """
00133     Listens to /diagnostics_agg topic and checks top level state of diagnostics
00134     Will latch any error messages until reset occurs
00135 
00136     """
00137     def __init__(self):
00138         self._mutex = threading.Lock()
00139 
00140         self._level = 0
00141         self._msgs = []
00142         self._update_time = 0
00143 
00144     def create(self, params):
00145         """
00146         Parameter values:
00147          * ignore_diags : Any values to ignore when computing top level state
00148          * whitelist : Only focus on these values 
00149         Both may be string or list. 
00150 
00151         \param params { str : str } : ROS parameters to initialize 
00152         \return bool : True if init OK
00153         """
00154         if params.has_key('ignore_diags'):
00155             ignore_diags = _convert_to_list(params['ignore_diags'])
00156         else:
00157             ignore_diags = [ 'Other' ]
00158 
00159         whitelist = None
00160         if params.has_key('whitelist'):
00161             whitelist = _convert_to_list(params['whitelist'])
00162         
00163         self._state = DiagAggState(whitelist = whitelist, ignore_categories = ignore_diags)
00164 
00165         self._diag_agg_sub = rospy.Subscriber('/diagnostics_agg', DiagnosticArray, self._diag_callback)
00166 
00167         return True
00168 
00169     def reset(self):
00170         """
00171         Clears state of messages that were in error or warning
00172         """
00173         self._msgs = []
00174 
00175     def _diag_callback(self, msg):
00176         with self._mutex:
00177             self._update_time = rospy.get_time()
00178             (added, removed, all) = self._state.update(msg)
00179 
00180             self._level, msgs = self._state.get_top_level_state()
00181 
00182             for msg in msgs:
00183                 if self._msgs.count(msg) < 1:
00184                     self._msgs.append(msg)
00185             
00186     def check_ok(self):
00187         """
00188         @return (int, str, None) : Level, Message. No diagnostics
00189         """
00190         with self._mutex:
00191             msg = ''
00192             stat = self._level
00193             if stat == 1:
00194                 msg = 'Diag Warn: %s' % (', '.join(self._msgs))
00195             if stat > 1:
00196                 msg = 'Diag Error: %s' % (', '.join(self._msgs))
00197 
00198             if rospy.get_time() - self._update_time > 3:
00199                 stat = 3
00200                 msg = 'Diagnostics Stale'
00201                 if self._update_time == 0:
00202                     msg = 'No Diagnostics Data'
00203         
00204         return stat, msg, None
00205     


pr2_hardware_test_monitor
Author(s): Kevin Watts
autogenerated on Sat Dec 28 2013 17:54:19