Euroscope.py 5.3 KB

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