app.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/configuration/<icao>')
  25. def configuration(icao):
  26. airport = aman.findAirport(icao.upper())
  27. if None == airport:
  28. return Response('{}', status=404, mimetype='application/json')
  29. config = airport.SequencingConfiguration
  30. dependencies = []
  31. for dependency in config.RunwayDependencies:
  32. rwy0 = config.runway(dependency[0])
  33. rwy1 = config.runway(dependency[1])
  34. cand1 = [ rwy0.Name, rwy1.Name ]
  35. cand2 = [ rwy1.Name, rwy0.Name ]
  36. found = False
  37. for dep in dependencies:
  38. if cand1 == dep or cand2 == dep:
  39. found = True
  40. break
  41. if False == found:
  42. dependencies.append(cand1)
  43. dictionary = {
  44. 'airport' : airport.Icao,
  45. 'useShallShouldMay' : config.UseShallShouldMay,
  46. 'activeRunways' : config.ActiveArrivalRunways,
  47. 'dependentRunways' : dependencies
  48. }
  49. data = json.dumps(dictionary, ensure_ascii=True, cls=RunwaySequencingEncoder)
  50. return Response(data, status=200, mimetype='application/json')
  51. @app.route('/aman/sequence/<icao>')
  52. def sequence(icao):
  53. airport = aman.findAirport(icao.upper())
  54. if None == airport:
  55. return Response('{}', status=404, mimetype='application/json')
  56. # convert the timestamp
  57. stamp = str(airport.SequencingConfiguration.LastUpdateTimestamp)
  58. delimiter = stamp.find('.')
  59. if -1 == delimiter:
  60. delimiter = stamp.find('+')
  61. dictionary = {
  62. 'airport': airport.Icao,
  63. 'lastConfigurationUpdate': stamp[0:delimiter],
  64. 'sequence': airport.inboundSequence()
  65. }
  66. data = json.dumps(dictionary, ensure_ascii=True, cls=InboundEncoder)
  67. return Response(data, status=200, mimetype='application/json')
  68. @app.route('/aman/configure', methods=['POST'])
  69. def configure():
  70. data = request.get_json()
  71. # validate that the airport exists
  72. if 'airport' not in data:
  73. return Response('{}', status=404, mimetype='application/json')
  74. airport = aman.findAirport(data['airport'].upper())
  75. if None == airport:
  76. return Response('{}', status=404, mimetype='application/json')
  77. # check that all top-level information are available
  78. if 'useShallShouldMay' not in data or 'activeRunways' not in data or 'dependentRunways' not in data:
  79. return Response('{}', status=404, mimetype='application/json')
  80. if False == isinstance(data['useShallShouldMay'], bool) or 0 == len(data['activeRunways']):
  81. return Response('{}', status=404, mimetype='application/json')
  82. # create the toplevel information
  83. config = AirportSequencing(airport.Icao)
  84. config.Airport = data['airport'].upper()
  85. config.UseShallShouldMay = data['useShallShouldMay']
  86. # parse the active runways
  87. for activeRunway in data['activeRunways']:
  88. if 'runway' not in activeRunway or 'spacing' not in activeRunway:
  89. return Response('{}', status=404, mimetype='application/json')
  90. if False == isinstance(activeRunway['runway'], str) or False == isinstance(activeRunway['spacing'], int):
  91. return Response('{}', status=404, mimetype='application/json')
  92. gngRunway = None
  93. for runway in airport.Configuration.GngData.Runways[airport.Icao]:
  94. if runway.Name == activeRunway['runway']:
  95. gngRunway = runway
  96. break
  97. # could not find the runway
  98. if None == gngRunway:
  99. return None
  100. runway = RunwaySequencing(gngRunway)
  101. runway.Spacing = activeRunway['spacing']
  102. config.activateRunway(runway)
  103. # parse the dependent runways
  104. for dependency in data['dependentRunways']:
  105. if 2 != len(dependency) or False == isinstance(dependency[0], str) or False == isinstance(dependency[1], str):
  106. return Response('{}', status=404, mimetype='application/json')
  107. if False == config.addDependency(dependency[0], dependency[1]):
  108. return Response('{}', status=404, mimetype='application/json')
  109. airport.configure(config)
  110. return Response('{}', status=200, mimetype='application/json')