00001 #! /usr/bin/python 00002 #*********************************************************** 00003 #* Software License Agreement (BSD License) 00004 #* 00005 #* Copyright (c) 2009, 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 Willow Garage, Inc. 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 import rospy 00038 import rosweb 00039 import threading 00040 import Image 00041 import cStringIO 00042 00043 from continuous_ops_msgs.msg import Tasks, Task 00044 00045 #_lock = threading.Lock() 00046 _image_cache = {} 00047 00048 def task_cb(data): 00049 for task in data.tasks: 00050 print task.task_id 00051 print task.image_file 00052 im = Image.open(task.image_file) 00053 buf = cStringIO.StringIO() 00054 im.save(buf, format='JPEG') 00055 jpeg = buf.getvalue() 00056 _image_cache[task.task_id] = jpeg 00057 00058 def config_plugin(context): 00059 global task_sub 00060 task_sub = rospy.Subscriber("task_manager/available_tasks", Tasks, task_cb) 00061 context.register_handler('/task', task_server_handler) 00062 00063 def send_image(self, jpeg, cache_control='max-age=3600'): 00064 try: 00065 self.send_response(200) 00066 self.send_header('Cache-Control', cache_control) 00067 self.send_header('Content-Type', 'image/jpeg') 00068 self.send_header('Content-Length', str(len(jpeg))) 00069 self.end_headers() 00070 self.wfile.write(jpeg) 00071 except: 00072 pass 00073 00074 00075 def task_server_handler(self, path, qdict): 00076 print "task" 00077 print path 00078 print qdict 00079 if path == '/task/image': 00080 print qdict['id'][0] 00081 jpeg = _image_cache[qdict['id'][0]] 00082 send_image(self, jpeg) 00083 return True 00084