<s> import os <EOL> import sys <EOL> from astropy import units as u <EOL> from . utils . logger import get_root_logger <EOL> from . utils . config import load_config <EOL> from . utils . database import PanMongo <EOL> from . utils . messaging import PanMessaging <EOL> from . utils import error <EOL> from . observatory import Observatory <EOL> from . state . machine import PanStateMachine <EOL> from . state . logic import PanStateLogic <EOL> from . state . event import PanEventManager <EOL> from . weather import WeatherStationMongo , WeatherStationSimulator <EOL> class PanBase ( object ) : <EOL> _shared_state = { } <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , simulator = [ ] ) : <EOL> self . __dict__ = self . _shared_state <EOL> if not hasattr ( self , '<STR_LIT>' ) or self . config is None : <EOL> self . _check_environment ( ) <EOL> self . config = self . _check_config ( load_config ( ) ) <EOL> if '<STR_LIT:all>' in simulator : <EOL> simulator = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . config . setdefault ( '<STR_LIT>' , simulator ) <EOL> self . logger = get_root_logger ( ) <EOL> self . logger . info ( '<STR_LIT:*>' * <NUM_LIT> ) <EOL> self . logger . info ( '<STR_LIT>' ) <EOL> self . name = self . config . get ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> self . logger . info ( '<STR_LIT>' . format ( self . name ) ) <EOL> else : <EOL> self . logger . info ( '<STR_LIT>' . format ( self . name ) ) <EOL> class Panoptes ( PanStateMachine , PanStateLogic , PanEventManager , PanBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , state_machine_file = '<STR_LIT>' , simulator = [ ] , ** kwargs ) : <EOL> PanBase . __init__ ( self , simulator ) <EOL> PanEventManager . __init__ ( self , ** kwargs ) <EOL> PanStateLogic . __init__ ( self , ** kwargs ) <EOL> PanStateMachine . __init__ ( self , state_machine_file , ** kwargs ) <EOL> if not hasattr ( self , '<STR_LIT>' ) or not self . _connected : <EOL> if not self . db : <EOL> self . logger . info ( '<STR_LIT>' ) <EOL> self . db = PanMongo ( ) <EOL> del self . config [ '<STR_LIT>' ] <EOL> self . db . insert_current ( '<STR_LIT>' , self . config ) <EOL> self . logger . info ( '<STR_LIT>' ) <EOL> self . weather_station = self . _create_weather_station ( ) <EOL> self . logger . info ( '<STR_LIT>' ) <EOL> self . observatory = Observatory ( config = self . config , ** kwargs ) <EOL> self . _connected = True <EOL> self . _initialized = False <EOL> point_config = self . config . get ( '<STR_LIT>' , { } ) <EOL> self . _max_iterations = point_config . get ( '<STR_LIT>' , <NUM_LIT:3> ) <EOL> self . _pointing_exptime = point_config . get ( '<STR_LIT>' , <NUM_LIT:30> ) * u . s <EOL> self . _pointing_threshold = point_config . get ( '<STR_LIT>' , <NUM_LIT> ) * u . deg <EOL> self . _pointing_iteration = <NUM_LIT:0> <EOL> self . say ( "<STR_LIT>" ) <EOL> else : <EOL> self . say ( "<STR_LIT>" ) <EOL> def say ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> self . logger . info ( "<STR_LIT>" . format ( self . name , msg ) ) <EOL> def power_down ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _connected : <EOL> self . logger . info ( "<STR_LIT>" . format ( self . name ) ) <EOL> if self . state not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if self . observatory . mount . is_connected : <EOL> if not self . observatory . mount . is_parked : <EOL> self . logger . info ( "<STR_LIT>" ) <EOL> self . park ( ) <EOL> if self . state == '<STR_LIT>' : <EOL> if self . observatory . mount . is_connected : <EOL> if not self . observatory . mount . is_parked : <EOL> self . logger . info ( "<STR_LIT>" ) <EOL> self . set_park ( ) <EOL> self . observatory . power_down ( ) <EOL> self . logger . info ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> if os . path . exists ( self . _shutdown_file ) : <EOL> os . unlink ( self . _shutdown_file ) <EOL> self . _connected = False <EOL> def check_mount_status ( self ) : <EOL> """<STR_LIT>""" <EOL> mount_status = { } <EOL> try : <EOL> mount_status = self . observatory . mount . status ( ) <EOL> self . db . insert_current ( '<STR_LIT>' , mount_status ) <EOL> except Exception as e : <EOL> self . logger . warning ( "<STR_LIT>" . format ( e ) ) <EOL> return mount_status <EOL> def _check_environment ( self ) : <EOL> """<STR_LIT>""" <EOL> pocs = os . getenv ( '<STR_LIT>' ) <EOL> if pocs is None : <EOL> sys . exit ( '<STR_LIT>' ) <EOL> self . _shutdown_file = os . path . join ( pocs , '<STR_LIT>' ) <EOL> def _check_config ( self , temp_config ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' not in temp_config : <EOL> raise error . InvalidConfig ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' not in temp_config : <EOL> raise error . MountNotFound ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' not in temp_config : <EOL> raise error . InvalidConfig ( '<STR_LIT>' ) <EOL> return temp_config <EOL> def _create_weather_station ( self ) : <EOL> """<STR_LIT>""" <EOL> weather_station = None <EOL> station_lookup = { <EOL> '<STR_LIT>' : WeatherStationSimulator , <EOL> '<STR_LIT>' : WeatherStationMongo , <EOL> } <EOL> weather_module = station_lookup . get ( self . config [ '<STR_LIT>' ] [ '<STR_LIT>' ] , WeatherStationMongo ) <EOL> self . logger . debug ( '<STR_LIT>' . format ( weather_module ) ) <EOL> try : <EOL> weather_station = weather_module ( ) <EOL> except : <EOL> raise error . PanError ( msg = "<STR_LIT>" ) <EOL> return weather_station <EOL> def _create_messaging ( self ) : <EOL> """<STR_LIT>""" <EOL> messaging = None <EOL> self . logger . debug ( '<STR_LIT>' ) <EOL> try : <EOL> messaging = PanMessaging ( publisher = True ) <EOL> except : <EOL> raise error . PanError ( msg = "<STR_LIT>" ) <EOL> return messaging </s>
