_diagnostic_status_wrapper.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2012, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 
00033 # -*- coding: utf-8 -*-
00034 
00035 """ diagnostic_updater for Python.
00036 @author Brice Rebsamen <brice [dot] rebsamen [gmail]>
00037 """
00038 
00039 import roslib
00040 roslib.load_manifest('diagnostic_updater')
00041 import rospy
00042 from diagnostic_msgs.msg import DiagnosticStatus, KeyValue
00043 
00044 OK = DiagnosticStatus.OK
00045 WARN = DiagnosticStatus.WARN
00046 ERROR = DiagnosticStatus.ERROR
00047 
00048 class DiagnosticStatusWrapper(DiagnosticStatus):
00049     """ Wrapper for the diagnostic_msgs::DiagnosticStatus message that makes it
00050     easier to update.
00051 
00052     This class handles common string formatting and vector handling issues
00053     for filling the diagnostic_msgs::DiagnosticStatus message. It is a subclass of
00054     diagnostic_msgs::DiagnosticStatus, so it can be passed directly to
00055     diagnostic publish calls.
00056     """
00057 
00058     def __init__(self, *args, **kwds):
00059         """
00060         Constructor. Any message fields that are implicitly/explicitly
00061         set to None will be assigned a default value. The recommend
00062         use is keyword arguments as this is more robust to future message
00063         changes.  You cannot mix in-order arguments and keyword arguments.
00064 
00065         The available fields are:
00066         level,name,message,hardware_id,values
00067 
00068         @param args: complete set of field values, in .msg order
00069         @param kwds: use keyword arguments corresponding to message field names
00070         to set specific fields.
00071         """
00072         DiagnosticStatus.__init__(self, *args, **kwds)
00073 
00074 
00075     def summary(self, *args):
00076         """ Fills out the level and message fields of the DiagnosticStatus.
00077 
00078         Usage:
00079         summary(diagnostic_status): Copies the summary from a DiagnosticStatus message
00080         summary(lvl,msg): sets from lvl and messages
00081         """
00082         if len(args)==1:
00083             self.level = args[0].level
00084             self.message = args[0].message
00085         elif len(args)==2:
00086             self.level = args[0]
00087             self.message = str(args[1])
00088 
00089 
00090     def clearSummary(self):
00091         """ Clears the summary, setting the level to zero and the message to "".
00092         """
00093         self.summary(0, "")
00094 
00095 
00096     def mergeSummary(self, *args):
00097         """ Merges a level and message with the existing ones.
00098 
00099         It is sometimes useful to merge two DiagnosticStatus messages. In that case,
00100         the key value pairs can be unioned, but the level and summary message
00101         have to be merged more intelligently. This function does the merge in
00102         an intelligent manner, combining the summary in *this, with the one
00103         that is passed in.
00104 
00105         The combined level is the greater of the two levels to be merged.
00106         If both levels are non-zero (not OK), the messages are combined with a
00107         semicolon separator. If only one level is zero, and the other is
00108         non-zero, the message for the zero level is discarded. If both are
00109         zero, the new message is ignored.
00110 
00111         Usage:
00112         mergeSummary(diagnostic_status): merge from a DiagnosticStatus message
00113         mergeSummary(lvl,msg): sets from lvl and msg
00114         """
00115         if len(args)==1:
00116             lvl = args[0].level
00117             msg = args[0].message
00118         elif len(args)==2:
00119             lvl = args[0]
00120             msg = args[1]
00121 
00122         if (lvl>0) == (self.level>0):
00123             if len(self.message)>0:
00124                 self.message += "; "
00125             self.message += msg
00126         elif lvl > self.level:
00127             self.message = msg
00128 
00129         if lvl > self.level:
00130             self.level = lvl
00131 
00132 
00133     def add(self, key, val):
00134         """ Add a key-value pair.
00135 
00136         This method adds a key-value pair. Any type that has a << stream
00137         operator can be passed as the second argument.  Formatting is done
00138         using a std::stringstream.
00139 
00140         @type key string
00141         @param key Key to be added.
00142         @type value string
00143         @param value Value to be added.
00144         """
00145         self.values.append(KeyValue(key,str(val)))


diagnostic_updater
Author(s): Kevin Watts, Brice Rebsamen , Jeremy Leibs, Blaise Gassend
autogenerated on Sun Oct 5 2014 23:27:19