01234567890123456789012345678901234567890123456789012345678901234567890123456789
526352645265526652675268526952705271527252735274527552765277527852795280528152825283 5284528552865287528852895290529152925293529452955296 52975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331 | <----SKIPPED LINES----> shutdown_remote = multiprocessing.Value('i') # shared flag to initiate shutdown shutdown_servo = multiprocessing.Value('i') # shared flag to initiate shutdown shutdown = (shutdown_remote, shutdown_servo) return (to_remote_q, to_servo_q, to_main_q, shutdown) def RefreshArduinos( remote, servo, to_remote_q, to_servo_q, to_main_q, shutdown, flights, json_desc_dict, configuration): """Ensure arduinos are running, restarting if needed, & send them the current message""" remote, servo = ValidateArduinosRunning( remote, servo, to_remote_q, to_servo_q, to_main_q, shutdown, configuration) EnqueueArduinos(flights, json_desc_dict, configuration, to_servo_q, to_remote_q) return remote, servo def ValidateArduinosRunning( remote, servo, to_remote_q, to_servo_q, to_main_q, shutdown, configuration): """Ensures that each of the enabled arduinos are running, restarting if needed.""" remote = ValidateSingleRunning( 'enable_remote' in configuration, arduino.RemoteMain, p=remote, args=(to_remote_q, to_main_q, shutdown[0])) servo = ValidateSingleRunning( 'enable_servos' in configuration, arduino.ServoMain, p=servo, args=(to_servo_q, to_main_q, shutdown[1])) return remote, servo def ValidateSingleRunning(enabled, start_function, p=None, args=()): """Restarts a new instance of multiprocessing process if not running""" if not SHUTDOWN_SIGNAL: if not enabled: if p is not None: # must have just requested a disabling of single instance args[2].value = 1 # trigger a shutdown on the single instance return None if p is None or not p.is_alive(): if p is None: Log('Process for %s starting for first time' % str(start_function)) elif VERBOSE: Log('Process (%s) for %s died; restarting' % (str(p), str(start_function))) args[2].value = 0 # (re)set shutdown flag to allow function to run p = multiprocessing.Process(target=start_function, args=args) p.daemon = True p.start() return p def EnqueueArduinos(flights, json_desc_dict, configuration, to_servo_q, to_remote_q): """Send latest data to arduinos via their shared-memory queues""" last_flight = {} if flights: last_flight = flights[-1] if SIMULATION: now = json_desc_dict['now'] else: now = time.time() additional_attributes = {} today = EpochDisplayTime(now, '%x') flight_count_today = len([1 for f in flights if DisplayTime(f, '%x') == today]) <----SKIPPED LINES----> |
01234567890123456789012345678901234567890123456789012345678901234567890123456789
526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361 | <----SKIPPED LINES----> shutdown_remote = multiprocessing.Value('i') # shared flag to initiate shutdown shutdown_servo = multiprocessing.Value('i') # shared flag to initiate shutdown shutdown = (shutdown_remote, shutdown_servo) return (to_remote_q, to_servo_q, to_main_q, shutdown) def RefreshArduinos( remote, servo, to_remote_q, to_servo_q, to_main_q, shutdown, flights, json_desc_dict, configuration): """Ensure arduinos are running, restarting if needed, & send them the current message""" remote, servo = ValidateArduinosRunning( remote, servo, to_remote_q, to_servo_q, to_main_q, shutdown, configuration) EnqueueArduinos(flights, json_desc_dict, configuration, to_servo_q, to_remote_q) return remote, servo def ValidateArduinosRunning( remote, servo, to_remote_q, to_servo_q, to_main_q, shutdown, configuration): """Ensures that each of the enabled arduinos are running, restarting if needed. Args: remote: Running remote Arduino process (or if not previously running, None value) servo: Running servo Arduino process (or if not previously running, None value) to_remote_q: Multi-processing messaging queue for one-way comm from messageboard to remote arduino. to_servo_q: Multi-processing messaging queue for one-way comm from messageboard to servo arduino. to_main_q: Multi-processing messaging queue for one-way comm from arduinos to messageboard. shutdown: 2-tuple of multiprocessing flags (integers) used to signal to respective arduinos when they should be shutdown. configuration: Dictionary of configuration settings. Returns: A 2-tuple of the remote and servo running processes. """ remote = ValidateSingleRunning( 'enable_remote' in configuration, arduino.RemoteMain, p=remote, args=(to_remote_q, to_main_q, shutdown[0])) servo = ValidateSingleRunning( 'enable_servos' in configuration, arduino.ServoMain, p=servo, args=(to_servo_q, to_main_q, shutdown[1])) return remote, servo def ValidateSingleRunning(enabled, start_function, p=None, args=()): """Restarts a new instance of multiprocessing process if not running Args: enabled: Boolean indicating whether this arduino is enabled in the settings file. start_function: Function that will be started (with the given args) if a restart is needed. p: The existing process - if any - that should be checked to make sure its alive. If not passed, or if passed but not alive, it is restarted. args: A tuple of function parameters to pass unmodified to start_function. Returns: The running process - either the same one that was passed in, or a new one if a restart was needed. """ if not SHUTDOWN_SIGNAL: if not enabled: if p is not None: # must have just requested a disabling of single instance args[2].value = 1 # trigger a shutdown on the single instance return None if p is None or not p.is_alive(): if p is None: Log('Process for %s starting for first time' % str(start_function)) elif VERBOSE: Log('Process (%s) for %s died; restarting' % (str(p), str(start_function))) args[2].value = 0 # (re)set shutdown flag to allow function to run p = multiprocessing.Process(target=start_function, args=args) p.daemon = True # TODO: perhaps value of false will address correlated BT failures? p.start() return p def EnqueueArduinos(flights, json_desc_dict, configuration, to_servo_q, to_remote_q): """Send latest data to arduinos via their shared-memory queues""" last_flight = {} if flights: last_flight = flights[-1] if SIMULATION: now = json_desc_dict['now'] else: now = time.time() additional_attributes = {} today = EpochDisplayTime(now, '%x') flight_count_today = len([1 for f in flights if DisplayTime(f, '%x') == today]) <----SKIPPED LINES----> |