app.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/env python
  2. import json
  3. import os
  4. from flask import Flask, Response, request
  5. from json import JSONEncoder
  6. from aman.AMAN import AMAN
  7. from aman.config.AirportSequencing import AirportSequencing
  8. from aman.config.RunwaySequencing import RunwaySequencing
  9. class InboundEncoder(JSONEncoder):
  10. def default(self, o):
  11. pta = str(o.PlannedArrivalTime)
  12. delimiter = pta.find('.')
  13. if -1 == delimiter:
  14. delimiter = pta.find('+')
  15. return { 'callsign' : o.Callsign, 'fixed' : o.FixedSequence, 'runway' : o.PlannedRunway.Name, 'pta' : pta[0:delimiter] }
  16. class RunwaySequencingEncoder(JSONEncoder):
  17. def default(self, o):
  18. return { 'runway' : o.Runway.Name, 'spacing' : o.Spacing }
  19. os.environ['AMAN_CONFIG_PATH'] = 'C:\\Repositories\\VATSIM\\AMAN\\config'
  20. aman = AMAN()
  21. app = Flask('AMAN')
  22. if __name__ == '__main__':
  23. app.run()
  24. @app.route('/aman/airports')
  25. def airports():
  26. retval = []
  27. for airport in aman.Workers:
  28. retval.append(airport.Icao)
  29. data = json.dumps(retval, ensure_ascii=True)
  30. return Response(data, status=200, mimetype='application/json')
  31. @app.route('/aman/configuration/<icao>')
  32. def configuration(icao):
  33. airport = aman.findAirport(icao.upper())
  34. if None == airport:
  35. return Response('{}', status=404, mimetype='application/json')
  36. config = airport.SequencingConfiguration
  37. dependencies = []
  38. for dependency in config.RunwayDependencies:
  39. rwy0 = config.runway(dependency[0])
  40. rwy1 = config.runway(dependency[1])
  41. cand1 = [ rwy0.Name, rwy1.Name ]
  42. cand2 = [ rwy1.Name, rwy0.Name ]
  43. found = False
  44. for dep in dependencies:
  45. if cand1 == dep or cand2 == dep:
  46. found = True
  47. break
  48. if False == found:
  49. dependencies.append(cand1)
  50. dictionary = {
  51. 'airport' : airport.Icao,
  52. 'useShallShouldMay' : config.UseShallShouldMay,
  53. 'activeRunways' : config.ActiveArrivalRunways,
  54. 'dependentRunways' : dependencies
  55. }
  56. data = json.dumps(dictionary, ensure_ascii=True, cls=RunwaySequencingEncoder)
  57. return Response(data, status=200, mimetype='application/json')
  58. @app.route('/aman/sequence/<icao>')
  59. def sequence(icao):
  60. airport = aman.findAirport(icao.upper())
  61. if None == airport:
  62. return Response('{}', status=404, mimetype='application/json')
  63. # convert the timestamp
  64. stamp = str(airport.SequencingConfiguration.LastUpdateTimestamp)
  65. delimiter = stamp.find('.')
  66. if -1 == delimiter:
  67. delimiter = stamp.find('+')
  68. dictionary = {
  69. 'airport': airport.Icao,
  70. 'lastConfigurationUpdate': stamp[0:delimiter],
  71. 'sequence': airport.inboundSequence()
  72. }
  73. data = json.dumps(dictionary, ensure_ascii=True, cls=InboundEncoder)
  74. return Response(data, status=200, mimetype='application/json')
  75. @app.route('/aman/configure', methods=['POST'])
  76. def configure():
  77. data = request.get_json()
  78. # validate that the airport exists
  79. if 'airport' not in data:
  80. return Response('{}', status=404, mimetype='application/json')
  81. airport = aman.findAirport(data['airport'].upper())
  82. if None == airport:
  83. return Response('{}', status=404, mimetype='application/json')
  84. # check that all top-level information are available
  85. if 'useShallShouldMay' not in data or 'activeRunways' not in data or 'dependentRunways' not in data:
  86. return Response('{}', status=404, mimetype='application/json')
  87. if False == isinstance(data['useShallShouldMay'], bool) or 0 == len(data['activeRunways']):
  88. return Response('{}', status=404, mimetype='application/json')
  89. # create the toplevel information
  90. config = AirportSequencing(airport.Icao)
  91. config.Airport = data['airport'].upper()
  92. config.UseShallShouldMay = data['useShallShouldMay']
  93. # parse the active runways
  94. for activeRunway in data['activeRunways']:
  95. if 'runway' not in activeRunway or 'spacing' not in activeRunway:
  96. return Response('{}', status=404, mimetype='application/json')
  97. if False == isinstance(activeRunway['runway'], str) or False == isinstance(activeRunway['spacing'], int):
  98. return Response('{}', status=404, mimetype='application/json')
  99. gngRunway = None
  100. for runway in airport.Configuration.GngData.Runways[airport.Icao]:
  101. if runway.Name == activeRunway['runway']:
  102. gngRunway = runway
  103. break
  104. # could not find the runway
  105. if None == gngRunway:
  106. return None
  107. runway = RunwaySequencing(gngRunway)
  108. runway.Spacing = activeRunway['spacing']
  109. config.activateRunway(runway)
  110. # parse the dependent runways
  111. for dependency in data['dependentRunways']:
  112. if 2 != len(dependency) or False == isinstance(dependency[0], str) or False == isinstance(dependency[1], str):
  113. return Response('{}', status=404, mimetype='application/json')
  114. if False == config.addDependency(dependency[0], dependency[1]):
  115. return Response('{}', status=404, mimetype='application/json')
  116. airport.Configuration.assignmentUpdate(config)
  117. airport.configure(config)
  118. return Response('{}', status=200, mimetype='application/json')