battery_notifier.py
Go to the documentation of this file.
00001 #! /usr/bin/python
00002 #***********************************************************
00003 #* Software License Agreement (BSD License)
00004 #*
00005 #*  Copyright (c) 2008, 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 the Willow Garage 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 #* Author: Eitan Marder-Eppstein
00036 #***********************************************************
00037 
00038 import roslib
00039 roslib.load_manifest('ocean_battery_driver')
00040 import rospy
00041 from pr2_msgs.msg import PowerState
00042 import os, sys
00043 
00044 class BatteryNotifier:
00045   def __init__(self, notify_limit, state_topic, email_addresses, robot_name, mail_program):
00046     self.notify_limit = roslib.rostime.Duration(notify_limit * 60.0)  # convert to Duration in seconds
00047     rospy.init_node("battery_notifier", anonymous=True)
00048     rospy.Subscriber(state_topic, PowerState, self.update)
00049     self.email_addresses = email_addresses
00050     self.mail_program = mail_program
00051     self.robot_name = robot_name
00052     self.mail_sent = False
00053 
00054   def update(self, state):
00055     if(state.time_remaining == 0 or (state.time_remaining <= self.notify_limit)):
00056       if not self.mail_sent and state.power_consumption < 0.0:  # negative means we are discharging
00057         self.sendEmail()
00058         self.mail_sent = True
00059     else:
00060       self.mail_sent = False
00061 
00062   def sendEmail(self):
00063     mail_string = "To: "
00064     for address in self.email_addresses: mail_string += address + ", "
00065     mail_string += "\nFrom: %s@willowgarage.com" % self.robot_name
00066     mail_string += "\nSubject: Battery level on robot below %d" % self.notify_limit
00067     mail_string += "\n\nMy battery level has fallen below the notification level. You should probably think about plugging me in. \n\nThanks much,\n%s" % self.robot_name
00068 
00069     #since the robot's have mail servers installed on them... we'll just pipe out our e-mail
00070     pipe = os.popen("%s -t" % self.mail_program, 'w')
00071     pipe.write(mail_string)
00072     pipe.close()
00073 
00074   def run(self):
00075     rospy.spin()
00076 
00077 if __name__ == '__main__':
00078   print >> sys.stderr, "This script is deprecated. Do not use it."
00079 
00080   if len(sys.argv) < 2:
00081     notifier = BatteryNotifier(30.0, "battery_state", ["eitan@willowgarage.com"], "pre", "/usr/sbin/sendmail")
00082   else:
00083     notifier = BatteryNotifier(30.0, "battery_state", sys.argv, "pre", "/usr/sbin/sendmail")
00084 
00085   notifier.run()


ocean_battery_driver
Author(s): Tully Foote, Curt Meyers
autogenerated on Tue Apr 22 2014 19:35:07