Euroscope.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env python
  2. import glob
  3. import os
  4. import sys
  5. import time
  6. import zmq
  7. import zmq.auth
  8. from aman.com import Communication_pb2
  9. from aman.config.Server import Server
  10. from threading import Thread
  11. class ComThread(Thread):
  12. def __init__(self, com, aman):
  13. Thread.__init__(self)
  14. self.Com = com
  15. self.AMAN = aman
  16. def run(self):
  17. while True:
  18. try:
  19. msg = self.Com.Socket.recv(zmq.NOBLOCK)
  20. # parse the received message
  21. report = Communication_pb2.AircraftUpdate()
  22. report.ParseFromString(msg)
  23. # try to associate the received aircrafts to airports
  24. icao = []
  25. for inbound in report.reports:
  26. self.AMAN.updateAircraftReport(inbound)
  27. icao = inbound.destination
  28. # get the sequence of the airport
  29. airport = self.AMAN.findAirport(icao)
  30. if None != airport:
  31. self.Com.sendSequence('', airport.inboundSequence(), airport.WeatherModel)
  32. except zmq.ZMQError as error:
  33. if zmq.EAGAIN == error.errno:
  34. time.sleep(0.5)
  35. continue
  36. else:
  37. return
  38. # @brief Receives and sends messages to EuroScope plugins
  39. class Euroscope:
  40. def __init__(self, configPath : str, config : Server, aman):
  41. self.Context = None
  42. self.Socket = None
  43. self.Thread = None
  44. self.Context = zmq.Context()
  45. # find the key directories
  46. serverKeyPath = os.path.join(os.path.join(configPath, 'keys'), 'server')
  47. if False == os.path.isdir(serverKeyPath):
  48. sys.stderr.write('No directory for the server key found')
  49. sys.exit(-1)
  50. print('Path to the server key: ' + serverKeyPath)
  51. clientKeyPath = os.path.join(os.path.join(configPath, 'keys'), 'clients')
  52. if False == os.path.isdir(clientKeyPath):
  53. sys.stderr.write('No directory for the client keys found')
  54. sys.exit(-1)
  55. print('Path to the client keys: ' + clientKeyPath)
  56. # read the certificates
  57. keyPairPath = glob.glob(os.path.join(serverKeyPath, '*.key_secret'))
  58. if 1 != len(keyPairPath):
  59. sys.stderr.write('No public-private keypair found for the server certificate')
  60. sys.exit(-1)
  61. keyPair = zmq.auth.load_certificate(keyPairPath[0])
  62. # initialize the receiver
  63. self.Socket = zmq.Socket(self.Context, zmq.REP)
  64. self.Socket.setsockopt(zmq.CURVE_PUBLICKEY, keyPair[0])
  65. self.Socket.setsockopt(zmq.CURVE_SECRETKEY, keyPair[1])
  66. self.Socket.setsockopt(zmq.CURVE_SERVER, True)
  67. self.Socket.bind('tcp://' + config.Address + ':' + str(config.PortReceiver))
  68. #self.Socket.setsockopt(zmq.SUBSCRIBE, b'')
  69. self.Thread = ComThread(self, aman)
  70. self.Thread.setDaemon(True)
  71. self.Thread.start()
  72. print('Listening to tcp://' + config.Address + ':' + str(config.PortReceiver))
  73. def sendSequence(self, airport : str, inbounds, weather):
  74. if None == self.Socket:
  75. return
  76. sequence = Communication_pb2.AircraftSequence()
  77. sequence.airport = airport
  78. # convert the wind data
  79. if None != weather.Altitudes:
  80. for i in range(0, len(weather.Altitudes)):
  81. entry = sequence.windData.add()
  82. entry.altitude = int(weather.Altitudes[i])
  83. entry.direction = int(weather.Directions[i])
  84. entry.speed = int(weather.Windspeeds[i])
  85. # convert the inbound sequence
  86. for inbound in inbounds:
  87. entry = sequence.sequence.add()
  88. entry.callsign = inbound.Callsign
  89. entry.fixed = inbound.FixedSequence
  90. entry.arrivalRoute = inbound.PlannedStar.Name
  91. entry.arrivalRunway = inbound.PlannedRunway.Name
  92. #performance = entry.performance.add()
  93. entry.performance.iasAboveFL240 = int(round(inbound.PerformanceData.SpeedAboveFL240))
  94. entry.performance.iasAboveFL100 = int(round(inbound.PerformanceData.SpeedAboveFL100))
  95. entry.performance.iasBelowFL100 = int(round(inbound.PerformanceData.SpeedBelowFL100))
  96. entry.performance.iasApproach = int(round(inbound.PerformanceData.SpeedApproach))
  97. for waypoint in inbound.PlannedArrivalRoute:
  98. wp = entry.waypoints.add()
  99. wp.name = waypoint.Waypoint.Name
  100. wp.altitude = int(round(waypoint.Altitude))
  101. wp.indicatedAirspeed = int(round(waypoint.IndicatedAirspeed))
  102. wp.groundSpeed = int(round(waypoint.GroundSpeed))
  103. pta = str(waypoint.PTA)
  104. delimiter = pta.find('.')
  105. if -1 == delimiter:
  106. delimiter = pta.find('+')
  107. wp.pta = pta[0:delimiter]
  108. message = sequence.SerializeToString()
  109. self.Socket.send(message)