01234567890123456789012345678901234567890123456789012345678901234567890123456789
828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868 5025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067 56295630563156325633563456355636563756385639564056415642564356445645564656475648 5649 56505651565256535654565556565657565856595660566156625663566456655666566756685669 57925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820 |
<----SKIPPED LINES---->
def SecondsToHhMm(seconds, colon=False):
"""Converts integer number of seconds to xhym string (i.e.: 7h17m) or to 7:17.
Args:
seconds: number of seconds
colon: controls format; if False, format is 7h17m; if True, format is 7:17.
Returns:
String representation of hours and minutes.
"""
if seconds is None:
return KEY_NOT_PRESENT_STRING[:3]
minutes = int(abs(seconds) / SECONDS_IN_MINUTE)
if minutes > MINUTES_IN_HOUR:
hours = int(minutes / MINUTES_IN_HOUR)
minutes = minutes % MINUTES_IN_HOUR
if colon:
text = '%d:%02d' % (hours, minutes)
else:
text = '%dh%d' % (hours, minutes)
else:
if colon:
text = ':%02d' % minutes
else:
text = '%dm' % minutes
return text
def SecondsToHours(seconds):
"""Converts integer number of seconds to xh string (i.e.: 7h).
Args:
seconds: number of seconds
Returns:
String representation of hours.
"""
minutes = int(abs(seconds) / SECONDS_IN_MINUTE)
hours = round(minutes / MINUTES_IN_HOUR)
return hours
<----SKIPPED LINES---->
os.getpid(), str(already_running_ids), str(running_parents), n+1))
PerformGracefulShutdown((), (), True)
if not n % 3:
Log('Kill signal sent from this process %d to %s, but %s still '
'running after waiting cume %d seconds' % (
os.getpid(), str(already_running_ids), str(running_parents), n))
n += 1
time.sleep(1)
running_parents = FindRunningParents()
def InitArduinos(configuration):
"""Initializes and starts the two arduino threads with new shared-memory queues."""
to_remote_q = multiprocessing.Queue()
to_servo_q = multiprocessing.Queue()
to_main_q = multiprocessing.Queue()
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)
remote, servo = ValidateArduinosRunning(
None, None, to_remote_q, to_servo_q, to_main_q, shutdown, configuration)
return (remote, servo, 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]))
<----SKIPPED LINES---->
# displayed so far
insight_message_distribution = {}
# bootstrap the flight insights distribution from a list of insights on each
# flight (i.e.: flight['insight_types'] for a given flight might look like
# [1, 2, 7, 9], or [], to indicate which insights were identified; this then
# transforms that into {0: 25, 1: 18, ...} summing across all flights.
missing_insights = []
for flight in flights:
if 'insight_types' not in flight:
missing_insights.append(
'%s on %s' % (DisplayFlightNumber(flight), DisplayTime(flight, '%x %X')))
distribution = flight.get('insight_types', [])
for key in distribution:
insight_message_distribution[key] = (
insight_message_distribution.get(key, 0) + 1)
if missing_insights:
Log('Flights missing insight distributions: %s' % ';'.join(missing_insights))
init_timing.append(time.time()) # time 5
remote, servo, to_remote_q, to_servo_q, to_main_q, shutdown = InitArduinos(configuration)
# used in simulation to print the hour of simulation once per simulated hour
prev_simulated_hour = ''
persistent_nearby_aircraft = {} # key = flight number; value = last seen epoch
persistent_path = {}
histogram = {}
# Next up to print is index 0; this is a list of tuples:
# tuple element#1: flag indicating the type of message that this is
# tuple element#2: the message itself
message_queue = []
next_message_time = time.time()
# We repeat the loop every x seconds; this ensures that if the processing time is long,
# we don't wait another x seconds after processing completes
next_loop_time = time.time() + LOOP_DELAY_SECONDS
# These files are read only if the version on disk has been modified more recently
# than the last time it was read
<----SKIPPED LINES---->
reboot = CheckRebootNeeded(startup_time, message_queue, json_desc_dict, configuration)
CheckTemperature()
if not SIMULATION:
time.sleep(max(0, next_loop_time - time.time()))
next_loop_time = time.time() + LOOP_DELAY_SECONDS
else:
SIMULATION_COUNTER += 1
if simulation_slowdown:
SimulationSlowdownNearFlight(flights, persistent_nearby_aircraft)
if SIMULATION:
SimulationEnd(message_queue, flights)
PerformGracefulShutdown((to_remote_q, to_servo_q, to_main_q), shutdown, reboot)
if __name__ == "__main__":
#interrupt, as in ctrl-c
#signal.signal(signal.SIGINT, InterruptShutdownFromSignal) #TODO
#terminate, when another instance found or via kill
#signal.signal(signal.SIGTERM, InterruptShutdownFromSignal) #TODO
if '-i' in sys.argv:
BootstrapInsightList()
else:
main()
|
01234567890123456789012345678901234567890123456789012345678901234567890123456789
828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868 50255026502750285029503050315032503350345035503650375038503950405041504250435044 504550465047504850495050505150525053505450555056505750585059506050615062506350645065 562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671 57945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822 |
<----SKIPPED LINES---->
def SecondsToHhMm(seconds, colon=False):
"""Converts integer number of seconds to xhym string (i.e.: 7h17m) or to 7:17.
Args:
seconds: number of seconds
colon: controls format; if False, format is 7h17m; if True, format is 7:17.
Returns:
String representation of hours and minutes.
"""
if seconds is None:
return KEY_NOT_PRESENT_STRING[:3]
minutes = int(abs(seconds) / SECONDS_IN_MINUTE)
if minutes > MINUTES_IN_HOUR:
hours = int(minutes / MINUTES_IN_HOUR)
minutes = minutes % MINUTES_IN_HOUR
if colon:
text = '%d:%02d' % (hours, minutes)
else:
text = '%dh%dm' % (hours, minutes)
else:
if colon:
text = ':%02d' % minutes
else:
text = '%dm' % minutes
return text
def SecondsToHours(seconds):
"""Converts integer number of seconds to xh string (i.e.: 7h).
Args:
seconds: number of seconds
Returns:
String representation of hours.
"""
minutes = int(abs(seconds) / SECONDS_IN_MINUTE)
hours = round(minutes / MINUTES_IN_HOUR)
return hours
<----SKIPPED LINES---->
os.getpid(), str(already_running_ids), str(running_parents), n+1))
PerformGracefulShutdown((), (), True)
if not n % 3:
Log('Kill signal sent from this process %d to %s, but %s still '
'running after waiting cume %d seconds' % (
os.getpid(), str(already_running_ids), str(running_parents), n))
n += 1
time.sleep(1)
running_parents = FindRunningParents()
def InitArduinos(configuration):
"""Initializes and starts the two arduino threads with new shared-memory queues."""
to_remote_q = multiprocessing.Queue()
to_servo_q = multiprocessing.Queue()
to_main_q = multiprocessing.Queue()
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]))
<----SKIPPED LINES---->
# displayed so far
insight_message_distribution = {}
# bootstrap the flight insights distribution from a list of insights on each
# flight (i.e.: flight['insight_types'] for a given flight might look like
# [1, 2, 7, 9], or [], to indicate which insights were identified; this then
# transforms that into {0: 25, 1: 18, ...} summing across all flights.
missing_insights = []
for flight in flights:
if 'insight_types' not in flight:
missing_insights.append(
'%s on %s' % (DisplayFlightNumber(flight), DisplayTime(flight, '%x %X')))
distribution = flight.get('insight_types', [])
for key in distribution:
insight_message_distribution[key] = (
insight_message_distribution.get(key, 0) + 1)
if missing_insights:
Log('Flights missing insight distributions: %s' % ';'.join(missing_insights))
init_timing.append(time.time()) # time 5
# initialize objects required for arduinos, but we can only start them in the main
# loop, because the tail end of the init section needs to confirm that all other
# messageboard.py processes have exited!
to_remote_q, to_servo_q, to_main_q, shutdown = InitArduinos(configuration)
remote, servo = None, None
# used in simulation to print the hour of simulation once per simulated hour
prev_simulated_hour = ''
persistent_nearby_aircraft = {} # key = flight number; value = last seen epoch
persistent_path = {}
histogram = {}
# Next up to print is index 0; this is a list of tuples:
# tuple element#1: flag indicating the type of message that this is
# tuple element#2: the message itself
message_queue = []
next_message_time = time.time()
# We repeat the loop every x seconds; this ensures that if the processing time is long,
# we don't wait another x seconds after processing completes
next_loop_time = time.time() + LOOP_DELAY_SECONDS
# These files are read only if the version on disk has been modified more recently
# than the last time it was read
<----SKIPPED LINES---->
reboot = CheckRebootNeeded(startup_time, message_queue, json_desc_dict, configuration)
CheckTemperature()
if not SIMULATION:
time.sleep(max(0, next_loop_time - time.time()))
next_loop_time = time.time() + LOOP_DELAY_SECONDS
else:
SIMULATION_COUNTER += 1
if simulation_slowdown:
SimulationSlowdownNearFlight(flights, persistent_nearby_aircraft)
if SIMULATION:
SimulationEnd(message_queue, flights)
PerformGracefulShutdown((to_remote_q, to_servo_q, to_main_q), shutdown, reboot)
if __name__ == "__main__":
#interrupt, as in ctrl-c
signal.signal(signal.SIGINT, InterruptShutdownFromSignal)
#terminate, when another instance found or via kill
signal.signal(signal.SIGTERM, InterruptShutdownFromSignal)
if '-i' in sys.argv:
BootstrapInsightList()
else:
main()
|