Worker.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python
  2. from threading import Thread, Lock
  3. import time
  4. from aman.com import Weather
  5. from aman.config.Airport import Airport
  6. from aman.sys.aco.Configuration import Configuration
  7. from aman.sys.WeatherModel import WeatherModel
  8. from aman.sys.RecedingHorizonControl import RecedingHorizonControl
  9. from aman.types.Inbound import Inbound
  10. from aman.types.PerformanceData import PerformanceData
  11. class Worker(Thread):
  12. def __init__(self):
  13. Thread.__init__(self)
  14. self.StopThread = None
  15. self.Icao = None
  16. self.Configuration = None
  17. self.PerformanceData = None
  18. self.UpdateLock = None
  19. self.ReportQueue = {}
  20. self.WeatherModel = None
  21. self.RecedingHorizonControl = None
  22. def __del__(self):
  23. self.release()
  24. def acquire(self, icao : str, configuration : Airport, weather : Weather, performance : PerformanceData):
  25. self.StopThread = None
  26. self.Icao = icao
  27. self.Configuration = configuration
  28. self.sequencingConfiguration = configuration.DefaultSequencing
  29. self.PerformanceData = performance
  30. self.UpdateLock = Lock()
  31. self.ReportQueue = {}
  32. self.WeatherModel = WeatherModel(configuration.GaforId, weather)
  33. self.RecedingHorizonControl = RecedingHorizonControl(configuration.RecedingHorizonControl)
  34. self.start()
  35. def acquireLock(self):
  36. if None != self.UpdateLock:
  37. self.UpdateLock.acquire()
  38. def release(self):
  39. self.StopThread = True
  40. self.join()
  41. def releaseLock(self):
  42. if None != self.UpdateLock:
  43. self.UpdateLock.release()
  44. def run(self):
  45. counter = 0
  46. while None == self.StopThread:
  47. time.sleep(1)
  48. counter += 1
  49. if 0 != (counter % 10):
  50. continue
  51. self.acquireLock()
  52. # perform some book-keeping
  53. self.RecedingHorizonControl.cleanupWindows()
  54. # update the aircraft information in RHC
  55. for callsign in self.ReportQueue:
  56. report = self.ReportQueue[callsign]
  57. if 0 != report.distanceToIAF and '' != report.initialApproachFix:
  58. inbound = Inbound(report, self.sequencingConfiguration, self.Configuration.GngData, self.PerformanceData, self.WeatherModel)
  59. if None != inbound.PlannedRunway and None != inbound.PlannedStar:
  60. self.RecedingHorizonControl.update(inbound)
  61. else:
  62. print('Unable to find all data of ' + report.aircraft.callsign)
  63. self.ReportQueue.clear()
  64. if 0 != len(self.RecedingHorizonControl.Windows):
  65. print('FCFS run:')
  66. for window in self.RecedingHorizonControl.Windows:
  67. for inbound in window.Inbounds:
  68. print(' ' + inbound.Report.aircraft.callsign + ': ' + str(inbound.EstimatedArrivalTime))
  69. # get the last landing aircrafts per runway before the RHC stage to check for constraints
  70. # this is required to handle the overlap between windows
  71. preceedingInbounds = {}
  72. for runway in self.sequencingConfiguration.ActiveArrivalRunways:
  73. inbound = self.RecedingHorizonControl.lastFixedInboundOnRunway(runway.Runway.Name)
  74. if None != inbound:
  75. preceedingInbounds[runway.Runway.Name] = inbound
  76. # search the ACO relevant aircrafts
  77. relevantInbounds = self.RecedingHorizonControl.optimizationRelevantInbounds()
  78. if None != relevantInbounds:
  79. print('Relevant inbounds: ' + str(len(relevantInbounds)))
  80. # configure the ACO run
  81. acoConfig = Configuration(self.sequencingConfiguration, 5 * len(relevantInbounds), 5 * len(relevantInbounds))
  82. if 0 != len(preceedingInbounds):
  83. acoConfig.PreceedingInbounds = preceedingInbounds
  84. acoConfig.Inbounds = relevantInbounds
  85. # TODO perform the ACO run
  86. # TODO update the RHC stages based on the ACO run result
  87. else:
  88. print('No relevant inbounds found for the optimization in ' + self.Icao)
  89. self.releaseLock()