control.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 
00004 # enable debugging
00005 import cgi, cgitb
00006 import os
00007 import pexpect
00008 import time
00009 cgitb.enable()
00010 
00011 import popen2
00012 
00013 
00014 print "Content-type: text/html"
00015 print
00016 
00017 print "RESULT"
00018 
00019 ros_distro = "ROS_DISTRO" #This gets overwritten by the install script
00020 
00021 form = cgi.FieldStorage()
00022 message = form.getvalue("action", "NO_ACTION")
00023 
00024 def run(fun):
00025     out = ""
00026     v = popen2.popen4(fun)
00027     for i in v[0]:
00028         out = out + i
00029     return out
00030 
00031 def run_as_robot(command):
00032     run = "su applications -c \"" + command + "\""
00033     #print "C", run
00034     child = pexpect.spawn(run)
00035     result = child.expect(["ssword:", "(yes/no)?"])
00036     child.sendline("willow")
00037     out = ""
00038     for i in child.readlines():
00039         out = out + i
00040     return out
00041 
00042 
00043 
00044 if (message == "GET_STATE"):
00045     print "USERS"
00046     active_user = "(UNKNOWN)"
00047     dead_users = ""
00048     message = ""
00049     for i in run("robot users --no-plist").split("\n"):
00050         if (i.find("Active User:") != -1):
00051             active_user = i.split(":")[1].strip()
00052         if (i.find("Message:") != -1):
00053             message = i.split(":")[1].strip()
00054         if (i.find("*") != -1):
00055             dead_users = dead_users + i[i.find("*") + 1:i.find("(")].strip() + ","
00056 
00057     #print "ACTIVE_USER: ", active_user
00058     #print "INACTIVE_USERS:", dead_users.strip().strip(",")
00059     #print "MESSAGE:", message
00060     
00061     if (active_user == "applications"):
00062         print "STATE_VALID"
00063     elif (active_user == "" or active_user == "None"):
00064         print "STATE_OFF"
00065     else:
00066         print "STATE_IN_USE"
00067         print "USER:", active_user
00068         if (message != ""):
00069             print "MESSAGE:", message #TODO: no newlines!
00070 
00071     #print "PROCESSES:"
00072     #processes = ""
00073     #for i in run_as_robot("robot plist").split("\n"):
00074     #    if (i.strip() != "Password:" and i.strip() != "The following processes are running:" and i.strip() != "" and i.strip() != "No processes running."):
00075     #        l = i.strip().split(None)
00076     #        if (len(l) > 5):
00077     #            print l[5], "from", l[4]
00078     #        else:
00079     #            print "INVALID:", i.strip()
00080 
00081     print
00082 elif (message == "STOP_ROBOT"):
00083     print "STOPPING_ROBOT"
00084     result = run_as_robot("yes | robot claim -m 'stopping the robot' ; yes | robot stop ; yes | robot release")
00085     print result
00086     print "DONE"
00087 elif (message == "START_ROBOT"):
00088     print "STARTING_APP_MAN"
00089     print run_as_robot("yes | robot claim -m 'running applications platform'")
00090     print run_as_robot(". /opt/ros/" + ros_distro + "/setup.bash ; . ~/.bashrc ; export ROS_ENV_LOADER=/opt/ros/" + ros_distro + "/env.sh ; nohup roslaunch pr2_app_manager whole_pr2_apps.launch > ~/run.txt &")
00091     # Wait for master to become available
00092     import socket
00093     s = socket.socket()
00094     connect = 0
00095     # Wait for a maximum of 30 seconds
00096     while connect < 30:
00097         try:
00098             s.connect(('localhost', 11311))
00099             connect = 30
00100             s.close()
00101         except:
00102             time.sleep(1)
00103             connect += 1
00104     print "DONE"
00105 else:
00106     print "REJECT_COMMAND"
00107     print "action = STOP_ROBOT,START_ROBOT,GET_STATE"
00108     


pr2_app_manager
Author(s): Vijay Pradeep
autogenerated on Fri Dec 6 2013 22:05:07