wge100_set_name.py
Go to the documentation of this file.
00001 #!/usr/bin/env 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 import roslib
00036 roslib.load_manifest('qualification')
00037 
00038 import sys
00039 import rospy
00040 from std_srvs.srv import *
00041 import std_msgs
00042 import subprocess
00043 import os
00044 import os.path
00045 import traceback
00046 import time
00047 from wg_invent_client import Invent
00048 from pr2_self_test_msgs.srv import ScriptDone, ScriptDoneRequest
00049 
00050 rospy.init_node("wge100_camera_set_name")
00051 
00052 def getparam(name):
00053     val = rospy.get_param(name, None)
00054     if val == None:
00055         failed("Parameter %s not set"%name)
00056     return val
00057 
00058 def failed(message):
00059     send_response("Error setting camera name", message, 1)
00060 
00061 def passed(message):
00062     send_response("Camera successfully set to %s at %s "%(cameraname, cameraip),
00063             message, 0)
00064 
00065 def send_response(summary, message, retval):
00066     print summary
00067     print message.replace('<b>','').replace('</b>','')
00068     r=ScriptDoneRequest()
00069     r.failure_msg = message.replace('<b>','').replace('</b>','')
00070     #r.html_result = "<p>%s</p>"%message.replace('\n','<br>')
00071     if retval != 0:
00072         r.result = ScriptDoneRequest.RESULT_FAIL
00073     else:
00074         r.result = ScriptDoneRequest.RESULT_OK
00075     
00076     rospy.wait_for_service('prestartup_done', 2)
00077     result_service = rospy.ServiceProxy('prestartup_done', ScriptDone)
00078     result_service.call(r)
00079     exit(retval)
00080 
00081 try:
00082     # Get inventory password from qualification
00083     username = getparam('/invent/username')
00084     password = getparam('/invent/password')
00085     barcode = getparam('qual_item/serial')
00086     cameraname = getparam('~camera_name')
00087     cameraip = getparam('~camera_ip')
00088     progip = getparam('~programming_ip')
00089     
00090     # Fail if invalid username/password
00091     i = Invent(username, password)
00092     if not i.login():
00093         failed("Could not connect to invent.")
00094     
00095     # Get camera url
00096     try:
00097         camera_url = i.get_item_references(barcode)["camera_url"]
00098         if camera_url == '':
00099             raise KeyError
00100     except KeyError:
00101         failed("Could not get camera url from invent. Try setting the  serial and MAC")
00102                                             
00103     # Set the camera's name
00104     msg = ""
00105     reps = 0
00106     while True: # Retry for up to 40 seconds because the arp cache may not have cleared
00107         p = subprocess.Popen(["rosrun", "wge100_camera", "set_name",
00108             camera_url+"@"+progip, cameraname, cameraip ], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
00109         setnameout = p.communicate()
00110         
00111         msg = msg+"Output from setname tool...\n<b>Standard output:</b>\n"+setnameout[0]+"\n\n<b>Standard error:</b>\n"+setnameout[1]
00112         
00113         if "Success" in msg:
00114             i.setKV(barcode, "Configured name", cameraname)
00115             i.setKV(barcode, "Configured ip", cameraip)
00116             passed(msg)
00117         else:
00118             if reps < 8: # Retry if we haven't been doing this too long.
00119                  reps = reps + 1
00120                  time.sleep(5)
00121                  continue
00122             i.setKV(barcode, "Configured name", "<configure failed>")
00123             i.setKV(barcode, "Configured ip", "<configure failed>")
00124             failed(msg)
00125 except:
00126     failed('<b>Exception:</b>\n%s'%traceback.format_exc())


qualification
Author(s): Kevin Watts (watts@willowgarage.com), Josh Faust (jfaust@willowgarage.com)
autogenerated on Sat Dec 28 2013 17:57:34