AMAN.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python
  2. import glob
  3. import os
  4. import random
  5. import sys
  6. from threading import Lock
  7. import time
  8. from aman.com import AircraftReport_pb2
  9. from aman.com.Euroscope import Euroscope
  10. from aman.com.Weather import Weather
  11. from aman.config.AircraftPerformance import AircraftPerformance
  12. from aman.config.Airport import Airport
  13. from aman.config.System import System
  14. from aman.sys.Worker import Worker
  15. class AMAN:
  16. def findConfigPath():
  17. envvar = os.environ.get('AMAN_CONFIG_PATH')
  18. if None == envvar:
  19. print('No AMAN_CONFIG_PATH in environment variables found. Using execution directory.')
  20. path = os.getcwd()
  21. else:
  22. print('AMAN_CONFIG_PATH found.')
  23. path = envvar
  24. print('Config-path: ' + path)
  25. return path
  26. def __init__(self):
  27. # default initialization of members
  28. configPath = AMAN.findConfigPath()
  29. self.SystemConfig = None
  30. self.AircraftPerformance = None
  31. self.Receiver = None
  32. self.Weather = None
  33. self.WebUi = None
  34. self.Workers = []
  35. self.WorkersLock = Lock()
  36. # read all system relevant configuration files
  37. self.SystemConfig = System(os.path.join(configPath, 'System.ini'))
  38. print('Parsed System.ini')
  39. # read the aircraft performance data
  40. self.AircraftPerformance = AircraftPerformance(os.path.join(configPath, 'PerformanceData.ini'))
  41. if None == self.AircraftPerformance:
  42. sys.stderr.write('No aircraft performance data found!')
  43. sys.exit(-1)
  44. else:
  45. print('Parsed PerformanceData.ini. Extracted ' + str(len(self.AircraftPerformance.Aircrafts)) + ' aircrafts')
  46. # create the communication syb
  47. self.Weather = Weather(self.SystemConfig.Weather)
  48. self.Receiver = Euroscope(configPath, self.SystemConfig.Server, self)
  49. self.acquireLock()
  50. # find the airport configurations and create the workers
  51. airportsPath = os.path.join(os.path.join(configPath, 'airports'), '*.ini')
  52. for file in glob.glob(airportsPath):
  53. icao = os.path.splitext(os.path.basename(file))[0]
  54. print('Parsing planner configuration for ' + icao)
  55. airportConfig = Airport(file, icao)
  56. # initialize the worker thread
  57. worker = Worker(icao, airportConfig, self.Weather, self.AircraftPerformance, self.Receiver)
  58. self.Workers.append(worker)
  59. print('Started worker for ' + icao)
  60. self.releaseLock()
  61. # initialize the random number generator
  62. random.seed(time.time())
  63. def acquireLock(self):
  64. if None != self.WorkersLock:
  65. self.WorkersLock.acquire()
  66. def releaseLock(self):
  67. if None != self.WorkersLock:
  68. self.WorkersLock.release()
  69. def updateAircraftReport(self, report : AircraftReport_pb2.AircraftReport):
  70. self.acquireLock()
  71. # find the correct worker for the inbound
  72. for worker in self.Workers:
  73. if worker.Icao == report.destination:
  74. worker.acquireLock()
  75. worker.ReportQueue[report.aircraft.callsign] = report
  76. worker.releaseLock()
  77. break
  78. self.releaseLock()
  79. def findAirport(self, icao : str):
  80. self.acquireLock()
  81. airport = None
  82. for worker in self.Workers:
  83. if icao == worker.Icao:
  84. airport = worker
  85. break
  86. self.releaseLock()
  87. return airport