WebUI.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python
  2. import json
  3. import requests
  4. import sys
  5. from json import JSONEncoder
  6. from requests.adapters import HTTPAdapter
  7. from requests.packages.urllib3.util.retry import Retry
  8. from requests.structures import CaseInsensitiveDict
  9. from aman.config.AirportSequencing import AirportSequencing
  10. from aman.config.RunwaySequencing import RunwaySequencing
  11. from aman.config.Server import Server
  12. class InboundEncoder(JSONEncoder):
  13. def default(self, o):
  14. pta = str(o.PlannedArrivalTime)
  15. delimiter = pta.find('.')
  16. if -1 == delimiter:
  17. delimiter = pta.find('+')
  18. return { 'callsign' : o.Callsign, 'runway' : o.PlannedRunway.Name, 'pta' : pta[0:delimiter] }
  19. class WebUI:
  20. def __init__(self):
  21. self.Config = None
  22. self.Aman = None
  23. def __del__(self):
  24. self.release()
  25. def acquire(self, config : Server, aman):
  26. self.Config = config
  27. self.Aman = aman
  28. def release(self):
  29. return
  30. def createSession():
  31. # prepare the session
  32. session = requests.Session()
  33. retry = Retry(total=1, read=1, connect=1)
  34. adapter = HTTPAdapter(max_retries=retry)
  35. session.mount('http://', adapter)
  36. session.mount('https://', adapter)
  37. return session
  38. def requestConfiguration(self, airport, navData):
  39. if airport not in navData.Runways:
  40. return None
  41. # prepare the HTTP header and session
  42. headers = CaseInsensitiveDict()
  43. headers['Accept'] = 'application/json'
  44. headers['Content-Type'] = 'application/json'
  45. session = WebUI.createSession()
  46. url = self.Config.WebUiUrl + self.Config.WebUiConfigurationReceiver
  47. data = json.dumps({ 'airport': airport }, ensure_ascii=True)
  48. try:
  49. response = session.get(url, headers=headers, data=data, timeout=2)
  50. if 200 != response.status_code:
  51. return None
  52. except requests.exceptions.ConnectTimeout:
  53. return None
  54. config = json.loads(response.json())
  55. # WebUI sent the wrong airport
  56. if config['airport'] != airport or 0 == len(config['activeRunways']):
  57. return None
  58. airportConfig = AirportSequencing(airport)
  59. airportConfig.UseMustShallMay = config['useMustShallMay']
  60. # build the sequencing information per runway
  61. for activeRunway in config['activeRunways']:
  62. gngRunway = None
  63. for runway in navData.Runways[airport]:
  64. if runway.Name == activeRunway['runway']:
  65. gngRunway = runway
  66. break
  67. # could not find the runway
  68. if None == gngRunway:
  69. return None
  70. runway = RunwaySequencing(gngRunway)
  71. runway.Spacing = int(activeRunway['spacing'])
  72. airportConfig.activateRunway(runway)
  73. # resolve the dependencies
  74. for dependency in config['dependentRunways']:
  75. if 2 == len(dependency):
  76. airportConfig.addDependency(dependency[0], dependency[1])
  77. return airportConfig
  78. def sendSequence(self, airport, inbounds):
  79. # prepare the HTTP header and session
  80. headers = CaseInsensitiveDict()
  81. headers['Accept'] = 'application/json'
  82. headers['Content-Type'] = 'application/json'
  83. session = WebUI.createSession()
  84. # prepare the relevant information
  85. url = self.Config.WebUiUrl + self.Config.WebUiSequenceNotification
  86. data = json.dumps({ 'airport': airport, 'sequence': inbounds }, ensure_ascii=True, cls=InboundEncoder)
  87. # send to the server
  88. try:
  89. session.patch(url, headers=headers, data=data, timeout=2)
  90. except requests.exceptions.ConnectTimeout:
  91. return