Euroscope.py 5.1 KB

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