<s> import os <EOL> import re <EOL> import sys <EOL> import json <EOL> import random <EOL> import urllib2 <EOL> import datetime <EOL> import threading <EOL> import traceback <EOL> import time <EOL> import weblab . experiment . concurrent_experiment as ConcurrentExperiment <EOL> from voodoo . override import Override <EOL> WEBCAMS_INFO = '<STR_LIT>' <EOL> CONTROLLER_ADDRESS = '<STR_LIT>' <EOL> HISTORIC_DIRECTORY = '<STR_LIT>' <EOL> LIGHT_BOOTSTRAPPING_TIME = '<STR_LIT>' <EOL> PUBLIC_PATH = '<STR_LIT>' <EOL> FAKE_CONTROLLER = '<STR_LIT>' <EOL> VERBOSE_CONTROLLER = '<STR_LIT>' <EOL> FAKE_NEXT_HOUR = '<STR_LIT>' <EOL> class StatusManager ( threading . Thread ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cfg_manager , webcams_urls ) : <EOL> """<STR_LIT>""" <EOL> threading . Thread . __init__ ( self ) <EOL> self . setDaemon ( True ) <EOL> self . setName ( "<STR_LIT>" ) <EOL> self . _address = cfg_manager . get_value ( CONTROLLER_ADDRESS ) <EOL> self . _light_time = cfg_manager . get_value ( LIGHT_BOOTSTRAPPING_TIME , <NUM_LIT:3> ) <EOL> self . _dir = cfg_manager . get_value ( HISTORIC_DIRECTORY , None ) <EOL> self . _public_path = cfg_manager . get_value ( PUBLIC_PATH , '<STR_LIT>' ) <EOL> self . _fake = cfg_manager . get_value ( FAKE_CONTROLLER , False ) <EOL> self . _verbose = cfg_manager . get_value ( VERBOSE_CONTROLLER , False ) <EOL> self . _fake_next_hour = cfg_manager . get_value ( FAKE_NEXT_HOUR , False ) <EOL> self . _webcams_urls = webcams_urls <EOL> self . _lights = len ( webcams_urls ) <EOL> self . _opener = urllib2 . build_opener ( urllib2 . ProxyHandler ( { } ) ) <EOL> self . _status = { <EOL> } <EOL> self . _all_lights = [ light for ( light , webcam_url ) in self . _webcams_urls ] <EOL> for light in self . _all_lights : <EOL> self . _status [ light ] = '<STR_LIT>' <EOL> self . _file_regex = re . compile ( '<STR_LIT>' ) <EOL> self . _status [ '<STR_LIT>' ] = - <NUM_LIT> <EOL> self . _status_lock = threading . Lock ( ) <EOL> self . _operations_lock = threading . Lock ( ) <EOL> self . turn_light ( '<STR_LIT:all>' , '<STR_LIT>' ) <EOL> self . dbg ( '<STR_LIT>' ) <EOL> def dbg ( self , msg ) : <EOL> if self . _verbose : <EOL> print "<STR_LIT>" % msg <EOL> sys . stdout . flush ( ) <EOL> def get_status_str ( self ) : <EOL> self . update_temperature ( ) <EOL> with self . _status_lock : <EOL> return json . dumps ( self . _status ) <EOL> def turn_light ( self , number , new_status ) : <EOL> """<STR_LIT>""" <EOL> if new_status not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return "<STR_LIT>" <EOL> self . update_temperature ( ) <EOL> with self . _operations_lock : <EOL> with self . _status_lock : <EOL> if number == '<STR_LIT:all>' : <EOL> for light in self . _all_lights : <EOL> self . _status [ light ] = new_status <EOL> elif number in self . _all_lights : <EOL> self . _status [ number ] = new_status <EOL> else : <EOL> return '<STR_LIT>' % self . _lights <EOL> if number == '<STR_LIT:all>' : <EOL> return self . _perform_post_request ( '<STR_LIT>' % new_status . upper ( ) ) <EOL> else : <EOL> return self . _perform_post_request ( '<STR_LIT>' % ( number , new_status ) ) <EOL> def update_temperature ( self ) : <EOL> temperature_str = self . _perform_get_request ( '<STR_LIT>' ) <EOL> try : <EOL> if temperature_str . startswith ( '<STR_LIT>' ) : <EOL> temperature = float ( temperature_str [ len ( '<STR_LIT>' ) : ] ) <EOL> else : <EOL> temperature = - <NUM_LIT> <EOL> except : <EOL> traceback . print_exc ( ) <EOL> temperature = - <NUM_LIT:1000> <EOL> with self . _status_lock : <EOL> self . _status [ '<STR_LIT>' ] = temperature <EOL> def _perform_post_request ( self , location ) : <EOL> """<STR_LIT>""" <EOL> url = '<STR_LIT>' % ( self . _address , location ) <EOL> self . dbg ( "<STR_LIT>" % ( datetime . datetime . now ( ) . strftime ( '<STR_LIT>' ) , url ) ) <EOL> if self . _fake : <EOL> self . dbg ( "<STR_LIT>" % location ) <EOL> return None <EOL> try : <EOL> self . _opener . open ( url , data = '<STR_LIT>' ) . read ( ) <EOL> except Exception as e : <EOL> traceback . print_exc ( ) <EOL> return '<STR_LIT>' % e <EOL> else : <EOL> return None <EOL> def _perform_get_request ( self , location ) : <EOL> """<STR_LIT>""" <EOL> url = '<STR_LIT>' % ( self . _address , location ) <EOL> self . dbg ( "<STR_LIT>" % ( datetime . datetime . now ( ) . strftime ( '<STR_LIT>' ) , url ) ) <EOL> if self . _fake : <EOL> self . dbg ( "<STR_LIT>" % location ) <EOL> return '<STR_LIT>' % str ( random . random ( ) - <NUM_LIT:100> ) <EOL> try : <EOL> response = self . _opener . open ( url ) . read ( ) <EOL> return '<STR_LIT>' % response <EOL> except Exception as e : <EOL> traceback . print_exc ( ) <EOL> return '<STR_LIT>' % e <EOL> def _store_picture ( self ) : <EOL> now = datetime . datetime . now ( ) <EOL> year_dir = os . path . join ( self . _dir , str ( now . year ) ) <EOL> if not os . path . exists ( year_dir ) : <EOL> os . mkdir ( year_dir ) <EOL> month_dir = os . path . join ( year_dir , str ( now . month ) ) <EOL> if not os . path . exists ( month_dir ) : <EOL> os . mkdir ( month_dir ) <EOL> day_dir = os . path . join ( month_dir , str ( now . day ) ) <EOL> if not os . path . exists ( day_dir ) : <EOL> os . mkdir ( day_dir ) <EOL> for light , webcam_url in self . _webcams_urls : <EOL> try : <EOL> image = self . _opener . open ( webcam_url ) . read ( ) <EOL> webcam_dir = os . path . join ( day_dir , light ) <EOL> if not os . path . exists ( webcam_dir ) : <EOL> os . mkdir ( webcam_dir ) <EOL> image_file_path = os . path . join ( webcam_dir , '<STR_LIT>' % ( now . hour , now . minute ) ) <EOL> open ( image_file_path , '<STR_LIT:wb>' ) . write ( image ) <EOL> except : <EOL> if self . _verbose : <EOL> traceback . print_exc ( ) <EOL> pass <EOL> def retrieve_historic ( self , year , month , day ) : <EOL> """<STR_LIT>""" <EOL> if self . _dir is None : <EOL> data = { } <EOL> for light in self . _all_lights : <EOL> data [ light ] = [ ] <EOL> return data <EOL> data = { } <EOL> day_dir = os . path . join ( self . _dir , str ( year ) , str ( month ) , str ( day ) ) <EOL> for light in self . _all_lights : <EOL> webcam_dir = os . path . join ( day_dir , light ) <EOL> current_data = [ ] <EOL> data [ light ] = current_data <EOL> if os . path . exists ( webcam_dir ) and os . path . isdir ( webcam_dir ) : <EOL> for f in os . listdir ( webcam_dir ) : <EOL> full_path = os . path . join ( webcam_dir , f ) <EOL> if os . path . isfile ( full_path ) and self . _file_regex . match ( f ) : <EOL> hours , minutes = f . split ( '<STR_LIT:_>' ) <EOL> formatted = '<STR_LIT>' % ( hours . zfill ( <NUM_LIT:2> ) , minutes . zfill ( <NUM_LIT:2> ) ) <EOL> public_path = '<STR_LIT>' % ( self . _public_path , year , month , day , light , f ) <EOL> current_data . append ( ( formatted , public_path ) ) <EOL> current_data . sort ( lambda ( x1 , x2 ) , ( y1 , y2 ) : cmp ( x1 , y1 ) ) <EOL> return data <EOL> def _take_historic ( self ) : <EOL> self . turn_light ( '<STR_LIT:all>' , '<STR_LIT>' ) <EOL> time . sleep ( self . _light_time ) <EOL> self . dbg ( "<STR_LIT>" ) <EOL> self . _store_picture ( ) <EOL> self . turn_light ( '<STR_LIT:all>' , '<STR_LIT>' ) <EOL> def run ( self ) : <EOL> if self . _dir is None : <EOL> self . dbg ( "<STR_LIT>" % HISTORIC_DIRECTORY ) <EOL> return <EOL> self . dbg ( "<STR_LIT>" ) <EOL> while True : <EOL> try : <EOL> now = datetime . datetime . now ( ) <EOL> next_minute = <NUM_LIT> - now . second <EOL> if self . _fake_next_hour : <EOL> next_hour = next_minute <EOL> else : <EOL> next_hour = <NUM_LIT> * ( <NUM_LIT> - now . minute ) + next_minute <EOL> self . dbg ( "<STR_LIT>" % next_hour ) <EOL> time . sleep ( next_hour ) <EOL> self . dbg ( "<STR_LIT>" ) <EOL> self . _take_historic ( ) <EOL> except Exception as e : <EOL> self . dbg ( "<STR_LIT>" % e ) <EOL> traceback . print_exc ( ) <EOL> class IncubatorExperiment ( ConcurrentExperiment . ConcurrentExperiment ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , coord_address , locator , cfg_manager , * args , ** kwargs ) : <EOL> super ( IncubatorExperiment , self ) . __init__ ( * args , ** kwargs ) <EOL> webcams_info = cfg_manager . get_value ( WEBCAMS_INFO , [ ] ) <EOL> self . _verbose = cfg_manager . get_value ( VERBOSE_CONTROLLER , False ) <EOL> self . initial_configuration = { } <EOL> webcams_urls = [ ] <EOL> for pos , webcam_config in enumerate ( 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> webcams_urls . append ( ( str ( 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 . initial_configuration [ '<STR_LIT>' ] = len ( webcams_info ) <EOL> self . _status_manager = StatusManager ( cfg_manager , webcams_urls ) <EOL> self . _status_manager . start ( ) <EOL> @ Override ( ConcurrentExperiment . ConcurrentExperiment ) <EOL> def do_get_api ( self ) : <EOL> return "<STR_LIT>" <EOL> @ Override ( ConcurrentExperiment . ConcurrentExperiment ) <EOL> def do_start_experiment ( self , lab_session_id , serialized_client_initial_data , serialized_server_initial_data ) : <EOL> status_str = self . _status_manager . get_status_str ( ) <EOL> config = { <EOL> '<STR_LIT:status>' : json . loads ( status_str ) , <EOL> } <EOL> config . update ( self . initial_configuration ) <EOL> config_sent = json . dumps ( { "<STR_LIT>" : json . dumps ( config ) , "<STR_LIT>" : False } ) <EOL> if self . _verbose : <EOL> print "<STR_LIT>" , config_sent <EOL> return config_sent <EOL> @ Override ( ConcurrentExperiment . ConcurrentExperiment ) <EOL> def do_dispose ( self , lab_session_id ) : <EOL> return '<STR_LIT>' <EOL> @ Override ( ConcurrentExperiment . ConcurrentExperiment ) <EOL> def do_send_command_to_device ( self , lab_session_id , command ) : <EOL> if command . startswith ( '<STR_LIT>' ) : <EOL> return self . _status_manager . get_status_str ( ) <EOL> if command . startswith ( '<STR_LIT>' ) : <EOL> remainder = command [ len ( '<STR_LIT>' ) : ] <EOL> regex = re . compile ( '<STR_LIT>' ) <EOL> obj = regex . match ( remainder ) <EOL> if obj is None : <EOL> return "<STR_LIT>" <EOL> number , new_state = obj . groups ( ) <EOL> message = self . _status_manager . turn_light ( number , new_state ) <EOL> if message is not None : <EOL> return "<STR_LIT>" % message <EOL> elif self . _verbose : <EOL> print "<STR_LIT>" % ( number , new_state , self . _status_manager . get_status_str ( ) ) <EOL> return self . _status_manager . get_status_str ( ) <EOL> if command . startswith ( '<STR_LIT>' ) : <EOL> remainder = command [ len ( '<STR_LIT>' ) : ] <EOL> regex = re . compile ( '<STR_LIT>' ) <EOL> obj = regex . match ( remainder ) <EOL> if obj is None : <EOL> return "<STR_LIT>" <EOL> year , month , day = obj . groups ( ) <EOL> return json . dumps ( self . _status_manager . retrieve_historic ( year , month , day ) ) <EOL> return '<STR_LIT>' <EOL> @ Override ( ConcurrentExperiment . ConcurrentExperiment ) <EOL> def do_send_file_to_device ( self , lab_session_id , content , file_info ) : <EOL> return '<STR_LIT>' </s>
