_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 rospy
00040 from diagnostic_msgs.msg import DiagnosticStatus, KeyValue
00041 
00042 OK = DiagnosticStatus.OK
00043 WARN = DiagnosticStatus.WARN
00044 ERROR = DiagnosticStatus.ERROR
00045 
00046 class DiagnosticStatusWrapper(DiagnosticStatus):
00047     """ Wrapper for the diagnostic_msgs::DiagnosticStatus message that makes it
00048     easier to update.
00049 
00050     This class handles common string formatting and vector handling issues
00051     for filling the diagnostic_msgs::DiagnosticStatus message. It is a subclass of
00052     diagnostic_msgs::DiagnosticStatus, so it can be passed directly to
00053     diagnostic publish calls.
00054     """
00055 
00056     def __init__(self, *args, **kwds):
00057         """
00058         Constructor. Any message fields that are implicitly/explicitly
00059         set to None will be assigned a default value. The recommend
00060         use is keyword arguments as this is more robust to future message
00061         changes.  You cannot mix in-order arguments and keyword arguments.
00062 
00063         The available fields are:
00064         level,name,message,hardware_id,values
00065 
00066         @param args: complete set of field values, in .msg order
00067         @param kwds: use keyword arguments corresponding to message field names
00068         to set specific fields.
00069         """
00070         DiagnosticStatus.__init__(self, *args, **kwds)
00071 
00072 
00073     def summary(self, *args):
00074         """ Fills out the level and message fields of the DiagnosticStatus.
00075 
00076         Usage:
00077         summary(diagnostic_status): Copies the summary from a DiagnosticStatus message
00078         summary(lvl,msg): sets from lvl and messages
00079         """
00080         if len(args)==1:
00081             self.level = args[0].level
00082             self.message = args[0].message
00083         elif len(args)==2:
00084             self.level = args[0]
00085             self.message = str(args[1])
00086 
00087 
00088     def clearSummary(self):
00089         """ Clears the summary, setting the level to zero and the message to "".
00090         """
00091         self.summary(0, "")
00092 
00093 
00094     def mergeSummary(self, *args):
00095         """ Merges a level and message with the existing ones.
00096 
00097         It is sometimes useful to merge two DiagnosticStatus messages. In that case,
00098         the key value pairs can be unioned, but the level and summary message
00099         have to be merged more intelligently. This function does the merge in
00100         an intelligent manner, combining the summary in *this, with the one
00101         that is passed in.
00102 
00103         The combined level is the greater of the two levels to be merged.
00104         If both levels are non-zero (not OK), the messages are combined with a
00105         semicolon separator. If only one level is zero, and the other is
00106         non-zero, the message for the zero level is discarded. If both are
00107         zero, the new message is ignored.
00108 
00109         Usage:
00110         mergeSummary(diagnostic_status): merge from a DiagnosticStatus message
00111         mergeSummary(lvl,msg): sets from lvl and msg
00112         """
00113         if len(args)==1:
00114             lvl = args[0].level
00115             msg = args[0].message
00116         elif len(args)==2:
00117             lvl = args[0]
00118             msg = args[1]
00119 
00120         if (lvl>0) == (self.level>0):
00121             if len(self.message)>0:
00122                 self.message += "; "
00123             self.message += msg
00124         elif lvl > self.level:
00125             self.message = msg
00126 
00127         if lvl > self.level:
00128             self.level = lvl
00129 
00130 
00131     def add(self, key, val):
00132         """ Add a key-value pair.
00133 
00134         This method adds a key-value pair. Any type that has a << stream
00135         operator can be passed as the second argument.  Formatting is done
00136         using a std::stringstream.
00137 
00138         @type key string
00139         @param key Key to be added.
00140         @type value string
00141         @param value Value to be added.
00142         """
00143         self.values.append(KeyValue(key,str(val)))


diagnostic_updater
Author(s): Kevin Watts, Brice Rebsamen , Jeremy Leibs, Blaise Gassend
autogenerated on Mon Aug 14 2017 02:52:20