<s> from datetime import timedelta as py_timedelta <EOL> from django . db import models as django_models <EOL> import logging <EOL> from services . common import simulation , misc as sn_misc , slots as sn_slots <EOL> from services . configuration . models import segments as segment_models <EOL> from services . simulation import push as simulation_push <EOL> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class PassManager ( django_models . Manager ) : <EOL> """<STR_LIT>""" <EOL> _simulator = None <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _simulator = simulation . OrbitalSimulator ( ) <EOL> super ( PassManager , self ) . __init__ ( ) <EOL> def create ( self , spacecraft , groundstation , start , end , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> s_range = ( <EOL> start - py_timedelta ( seconds = <NUM_LIT:30> ) , <EOL> start + py_timedelta ( seconds = <NUM_LIT:30> ) <EOL> ) <EOL> e_range = ( <EOL> end - py_timedelta ( seconds = <NUM_LIT:30> ) , <EOL> end + py_timedelta ( seconds = <NUM_LIT:30> ) <EOL> ) <EOL> if self . filter ( <EOL> groundstation = groundstation , spacecraft = spacecraft , <EOL> start__range = s_range , end__range = e_range <EOL> ) . exists ( ) : <EOL> logger . warn ( <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' + str ( <EOL> groundstation . identifier <EOL> ) + '<STR_LIT>' + start . isoformat ( ) + '<STR_LIT>' + <EOL> end . isoformat ( ) + '<STR_LIT:>>' <EOL> ) <EOL> return None <EOL> return super ( PassManager , self ) . create ( <EOL> spacecraft = spacecraft , groundstation = groundstation , <EOL> start = start , end = end , ** kwargs <EOL> ) <EOL> def set_spacecraft ( self , spacecraft ) : <EOL> """<STR_LIT>""" <EOL> self . _simulator . set_spacecraft ( spacecraft . tle ) <EOL> def _calculate_passes ( self , spacecraft , groundstation , window = None ) : <EOL> """<STR_LIT>""" <EOL> all_slots = [ ] <EOL> if not window : <EOL> window = simulation . OrbitalSimulator . get_simulation_window ( ) <EOL> try : <EOL> slots = self . _simulator . calculate_pass_slot ( window [ <NUM_LIT:0> ] , window [ <NUM_LIT:1> ] ) <EOL> for s in slots : <EOL> self . create ( <EOL> spacecraft = spacecraft , groundstation = groundstation , <EOL> start = s [ <NUM_LIT:0> ] , end = s [ <NUM_LIT:1> ] <EOL> ) <EOL> all_slots += slots <EOL> except Exception as ex : <EOL> logger . exception ( <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' + str ( spacecraft . identifier ) + '<STR_LIT:\n>' + <EOL> '<STR_LIT>' + str ( spacecraft . tle . identifier ) + '<STR_LIT:\n>' + <EOL> '<STR_LIT>' + str ( ex ) , <EOL> ex <EOL> ) <EOL> return all_slots <EOL> def create_pass_slots_sc ( self , spacecraft ) : <EOL> """<STR_LIT>""" <EOL> all_slots = [ ] <EOL> self . set_spacecraft ( spacecraft ) <EOL> for groundstation in segment_models . GroundStation . objects . all ( ) : <EOL> self . _simulator . set_groundstation ( groundstation ) <EOL> all_slots += self . _calculate_passes ( spacecraft , groundstation ) <EOL> simulation_push . SimulationPush . trigger_passes_updated_event ( ) <EOL> return all_slots <EOL> def create_pass_slots_gs ( self , groundstation ) : <EOL> """<STR_LIT>""" <EOL> all_slots = [ ] <EOL> self . _simulator . set_groundstation ( groundstation ) <EOL> for spacecraft in segment_models . Spacecraft . objects . all ( ) : <EOL> self . set_spacecraft ( spacecraft ) <EOL> all_slots += self . _calculate_passes ( spacecraft , groundstation ) <EOL> simulation_push . SimulationPush . trigger_passes_updated_event ( ) <EOL> return all_slots <EOL> def is_duplicated ( self , interval , groundstation , spacecraft ) : <EOL> """<STR_LIT>""" <EOL> last_pass = None <EOL> try : <EOL> last_pass = self . filter ( <EOL> groundstation = groundstation , spacecraft = spacecraft <EOL> ) . latest ( '<STR_LIT:start>' ) <EOL> except PassSlots . DoesNotExist : <EOL> logger . info ( '<STR_LIT>' ) <EOL> return <EOL> if not last_pass : <EOL> return False <EOL> if last_pass . end > interval [ <NUM_LIT:0> ] : <EOL> return True <EOL> else : <EOL> return False <EOL> def propagate ( <EOL> self , interval = simulation . OrbitalSimulator . get_update_window ( ) <EOL> ) : <EOL> """<STR_LIT>""" <EOL> all_slots = [ ] <EOL> logger . info ( <EOL> '<STR_LIT>' + sn_slots . string ( interval ) <EOL> ) <EOL> for gs in segment_models . GroundStation . objects . all ( ) : <EOL> logger . info ( '<STR_LIT>' + str ( gs . identifier ) ) <EOL> self . _simulator . set_groundstation ( gs ) <EOL> for sc in segment_models . Spacecraft . objects . all ( ) : <EOL> logger . info ( <EOL> '<STR_LIT>' + str ( sc . identifier ) <EOL> ) <EOL> if not self . is_duplicated ( interval , gs , sc ) : <EOL> self . set_spacecraft ( sc ) <EOL> all_slots += self . _calculate_passes ( sc , gs , interval ) <EOL> logger . info ( <EOL> sn_misc . list_2_string ( <EOL> all_slots , name = '<STR_LIT>' <EOL> ) <EOL> ) <EOL> else : <EOL> logger . info ( '<STR_LIT>' ) <EOL> if all_slots : <EOL> simulation_push . SimulationPush . trigger_passes_updated_event ( ) <EOL> return all_slots <EOL> def remove_pass_slots_sc ( self , spacecraft ) : <EOL> """<STR_LIT>""" <EOL> self . filter ( spacecraft = spacecraft ) . delete ( ) <EOL> simulation_push . SimulationPush . trigger_passes_updated_event ( ) <EOL> def remove_pass_slots_gs ( self , groundstation ) : <EOL> """<STR_LIT>""" <EOL> self . filter ( groundstation = groundstation ) . delete ( ) <EOL> simulation_push . SimulationPush . trigger_passes_updated_event ( ) <EOL> class PassSlots ( django_models . Model ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> app_label = '<STR_LIT>' <EOL> objects = PassManager ( ) <EOL> spacecraft = django_models . ForeignKey ( <EOL> segment_models . Spacecraft , <EOL> verbose_name = '<STR_LIT>' <EOL> ) <EOL> groundstation = django_models . ForeignKey ( <EOL> segment_models . GroundStation , <EOL> verbose_name = '<STR_LIT>' <EOL> ) <EOL> start = django_models . DateTimeField ( '<STR_LIT>' ) <EOL> end = django_models . DateTimeField ( '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return u'<STR_LIT>' + str ( self . start ) + u'<STR_LIT:U+002CU+0020>' + str ( self . end ) </s>
