AMAN.py 3.3 KB

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