Euroscope.py 5.3 KB

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