Euroscope.py 5.5 KB

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