<s> import os <EOL> import json <EOL> import urllib2 <EOL> import threading <EOL> import traceback <EOL> from voodoo . log import logged <EOL> from voodoo . override import Override <EOL> from weblab . experiment . concurrent_experiment import ConcurrentExperiment <EOL> COLORS = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> '<STR_LIT>' : <NUM_LIT:4> , <EOL> } <EOL> FORCE = { <EOL> '<STR_LIT>' : '<STR_LIT:3>' , <EOL> '<STR_LIT>' : '<STR_LIT:3>' , <EOL> '<STR_LIT>' : '<STR_LIT:3>' , <EOL> '<STR_LIT>' : '<STR_LIT:3>' , <EOL> } <EOL> class Proxy ( object ) : <EOL> def __init__ ( self , url , fake ) : <EOL> self . fake = fake <EOL> self . url = url <EOL> self . opener = urllib2 . build_opener ( urllib2 . ProxyHandler ( { } ) ) <EOL> def move_ball ( self , ball , on ) : <EOL> mode = '<STR_LIT>' if on else '<STR_LIT:D>' <EOL> ball_number = COLORS [ ball ] <EOL> force = FORCE [ ball ] <EOL> try : <EOL> self . _process ( "<STR_LIT>" % ( self . url , mode , ball_number , force ) , get = False ) <EOL> except : <EOL> traceback . print_exc ( ) <EOL> def process_image ( self ) : <EOL> try : <EOL> content_str = self . _process ( "<STR_LIT>" , get = True ) <EOL> content = json . loads ( content_str ) <EOL> base_path = '<STR_LIT>' <EOL> return ( base_path + content [ '<STR_LIT>' ] ) , ( base_path + content [ '<STR_LIT>' ] ) <EOL> except : <EOL> traceback . print_exc ( ) <EOL> return "<STR_LIT>" , "<STR_LIT>" <EOL> def get_status ( self ) : <EOL> try : <EOL> content_str = self . _process ( "<STR_LIT>" % self . url , get = True ) <EOL> content = json . loads ( content_str ) <EOL> white , yellow , blue , red = content [ '<STR_LIT>' ] <EOL> return { <EOL> '<STR_LIT>' : white == <NUM_LIT:0> , <EOL> '<STR_LIT>' : yellow == <NUM_LIT:0> , <EOL> '<STR_LIT>' : blue == <NUM_LIT:0> , <EOL> '<STR_LIT>' : red == <NUM_LIT:0> , <EOL> } <EOL> except : <EOL> traceback . print_exc ( ) <EOL> return { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> def _process ( self , url , get ) : <EOL> if self . fake : <EOL> print "<STR_LIT>" % ( '<STR_LIT:GET>' if get else '<STR_LIT:POST>' ) , url <EOL> return json . dumps ( { '<STR_LIT>' : [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] } ) <EOL> else : <EOL> print "<STR_LIT>" , url <EOL> if get : <EOL> urlobj = self . opener . open ( url ) <EOL> else : <EOL> urlobj = self . opener . open ( url , '<STR_LIT:{}>' ) <EOL> return urlobj . read ( ) . replace ( "<STR_LIT:'>" , '<STR_LIT:">' ) <EOL> class StatusManager ( object ) : <EOL> def __init__ ( self , proxy ) : <EOL> self . _proxy = proxy <EOL> self . _ops_locks = { } <EOL> for color in COLORS : <EOL> self . _ops_locks [ color ] = threading . Lock ( ) <EOL> def move ( self , ball , on ) : <EOL> with self . _ops_locks [ ball ] : <EOL> self . _proxy . move_ball ( ball , on ) <EOL> def get_status ( self ) : <EOL> return self . _proxy . get_status ( ) <EOL> class Aquarium ( ConcurrentExperiment ) : <EOL> def __init__ ( self , coord_address , locator , cfg_manager , * args , ** kwargs ) : <EOL> super ( Aquarium , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _cfg_manager = cfg_manager <EOL> self . fake = self . _cfg_manager . get_value ( '<STR_LIT>' , True ) <EOL> self . proxy = Proxy ( '<STR_LIT>' , self . fake ) <EOL> self . debug = self . _cfg_manager . get_value ( '<STR_LIT>' , True ) <EOL> self . webcams_info = self . _cfg_manager . get_value ( '<STR_LIT>' , [ ] ) <EOL> self . initial_configuration = { } <EOL> for pos , webcam_config in enumerate ( self . webcams_info ) : <EOL> num = pos + <NUM_LIT:1> <EOL> webcam_url = webcam_config . get ( '<STR_LIT>' ) <EOL> mjpeg_url = webcam_config . get ( '<STR_LIT>' ) <EOL> mjpeg_width = webcam_config . get ( '<STR_LIT>' ) <EOL> mjpeg_height = webcam_config . get ( '<STR_LIT>' ) <EOL> if webcam_url is not None : <EOL> self . initial_configuration [ '<STR_LIT>' % num ] = webcam_url <EOL> if mjpeg_url is not None : <EOL> self . initial_configuration [ '<STR_LIT>' % num ] = mjpeg_url <EOL> if mjpeg_width is not None : <EOL> self . initial_configuration [ '<STR_LIT>' % num ] = mjpeg_width <EOL> if mjpeg_height is not None : <EOL> self . initial_configuration [ '<STR_LIT>' % num ] = mjpeg_height <EOL> self . _status = StatusManager ( self . proxy ) <EOL> @ Override ( ConcurrentExperiment ) <EOL> @ logged ( "<STR_LIT:info>" ) <EOL> def do_start_experiment ( self , lab_session_id , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . debug : <EOL> print "<STR_LIT>" <EOL> current_config = self . initial_configuration . copy ( ) <EOL> current_config [ '<STR_LIT:status>' ] = self . _status . get_status ( ) <EOL> return json . dumps ( { "<STR_LIT>" : json . dumps ( current_config ) , "<STR_LIT>" : False } ) <EOL> @ Override ( ConcurrentExperiment ) <EOL> @ logged ( "<STR_LIT:info>" ) <EOL> def do_send_command_to_device ( self , lab_session_id , command ) : <EOL> """<STR_LIT>""" <EOL> if self . debug : <EOL> print "<STR_LIT>" % command <EOL> if command == '<STR_LIT>' : <EOL> return json . dumps ( self . _status . get_status ( ) ) <EOL> elif command . startswith ( '<STR_LIT>' ) : <EOL> try : <EOL> _ , ball , on = command . split ( '<STR_LIT::>' ) <EOL> except : <EOL> traceback . print_exc ( ) <EOL> return "<STR_LIT>" <EOL> if not ball . lower ( ) in COLORS : <EOL> return "<STR_LIT>" <EOL> if not on . lower ( ) in ( '<STR_LIT:true>' , '<STR_LIT:false>' ) : <EOL> return "<STR_LIT>" <EOL> on = on . lower ( ) == '<STR_LIT:true>' <EOL> ball = ball . lower ( ) <EOL> self . _status . move ( ball , on ) <EOL> return json . dumps ( self . _status . get_status ( ) ) <EOL> elif command == '<STR_LIT>' : <EOL> return json . dumps ( self . proxy . process_image ( ) ) <EOL> return '<STR_LIT>' <EOL> @ Override ( ConcurrentExperiment ) <EOL> @ logged ( "<STR_LIT:info>" ) <EOL> def do_send_file_to_device ( self , lab_session_id , content , file_info ) : <EOL> """<STR_LIT>""" <EOL> if self . debug : <EOL> print "<STR_LIT>" <EOL> return "<STR_LIT>" <EOL> @ Override ( ConcurrentExperiment ) <EOL> @ logged ( "<STR_LIT:info>" ) <EOL> def do_dispose ( self , lab_session_id ) : <EOL> """<STR_LIT>""" <EOL> if self . debug : <EOL> print "<STR_LIT>" <EOL> return "<STR_LIT>" </s>
