Euroscope.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. def threadId(self):
  35. if hasattr(self, '_thread_id'):
  36. return self._thread_id
  37. for id, thread in _active.items():
  38. if thread is self:
  39. return id
  40. def stopThread(self):
  41. id = self.threadId()
  42. res = ctypes.pythonapi.PyThreadState_SetAsyncExc(id, ctypes.py_object(SystemExit))
  43. if 1 < res:
  44. ctypes.pythonapi.PyThreadState_SetAsyncExc(id, 0)
  45. # @brief Receives and sends messages to EuroScope plugins
  46. class Euroscope:
  47. def __init__(self):
  48. self.Context = None
  49. self.ReceiverSocket = None
  50. self.ReceiverThread = None
  51. self.NotificationSocket = None
  52. def __del__(self):
  53. self.release()
  54. # @brief Initializes the ZMQ socket
  55. # @param[in] config The server configuration
  56. def acquire(self, configPath : str, config : Server, aman):
  57. self.Context = zmq.Context()
  58. # find the key directories
  59. serverKeyPath = os.path.join(os.path.join(configPath, 'keys'), 'server')
  60. if False == os.path.isdir(serverKeyPath):
  61. sys.stderr.write('No directory for the server key found')
  62. sys.exit(-1)
  63. print('Path to the server key: ' + serverKeyPath)
  64. clientKeyPath = os.path.join(os.path.join(configPath, 'keys'), 'clients')
  65. if False == os.path.isdir(clientKeyPath):
  66. sys.stderr.write('No directory for the client keys found')
  67. sys.exit(-1)
  68. print('Path to the client keys: ' + clientKeyPath)
  69. # read the certificates
  70. keyPairPath = glob.glob(os.path.join(serverKeyPath, '*.key_secret'))
  71. if 1 != len(keyPairPath):
  72. sys.stderr.write('No public-private keypair found for the server certificate')
  73. sys.exit(-1)
  74. keyPair = zmq.auth.load_certificate(keyPairPath[0])
  75. # initialize the receiver
  76. self.ReceiverSocket = zmq.Socket(self.Context, zmq.SUB)
  77. self.ReceiverSocket.setsockopt(zmq.CURVE_PUBLICKEY, keyPair[0])
  78. self.ReceiverSocket.setsockopt(zmq.CURVE_SECRETKEY, keyPair[1])
  79. self.ReceiverSocket.setsockopt(zmq.CURVE_SERVER, True)
  80. self.ReceiverSocket.bind('tcp://' + config.Address + ':' + str(config.PortReceiver))
  81. self.ReceiverSocket.setsockopt(zmq.SUBSCRIBE, b'')
  82. self.ReceiverThread = ReceiverThread(self.ReceiverSocket, aman)
  83. self.ReceiverThread.start()
  84. print('Listening to tcp://' + config.Address + ':' + str(config.PortReceiver))
  85. # initialize the notification
  86. self.NotificationSocket = zmq.Socket(self.Context, zmq.PUB)
  87. self.NotificationSocket.setsockopt(zmq.CURVE_PUBLICKEY, keyPair[0])
  88. self.NotificationSocket.setsockopt(zmq.CURVE_SECRETKEY, keyPair[1])
  89. self.NotificationSocket.setsockopt(zmq.CURVE_SERVER, True)
  90. self.NotificationSocket.bind('tcp://' + config.Address + ':' + str(config.PortNotification))
  91. print('Publishing to tcp://' + config.Address + ':' + str(config.PortNotification))
  92. def release(self):
  93. if None != self.ReceiverThread:
  94. self.ReceiverThread.stopThread()
  95. self.ReceiverThread.join()
  96. self.ReceiverThread = None
  97. if None != self.ReceiverSocket:
  98. self.ReceiverSocket.close()
  99. self.ReceiverSocket = None
  100. if None != self.NotificationSocket:
  101. self.NotificationSocket.close()
  102. self.NotificationSocket = None
  103. self.Context = None
  104. def sendSequence(self, airport : str, inbounds, weather):
  105. if None == self.NotificationSocket:
  106. return
  107. sequence = Communication_pb2.AircraftSequence()
  108. sequence.airport = airport
  109. # convert the wind data
  110. if None != weather.Altitudes:
  111. for i in range(0, len(weather.Altitudes)):
  112. entry = sequence.windData.add()
  113. entry.altitude = int(weather.Altitudes[i])
  114. entry.direction = int(weather.Directions[i])
  115. entry.speed = int(weather.Windspeeds[i])
  116. # convert the inbound sequence
  117. for inbound in inbounds:
  118. entry = sequence.sequence.add()
  119. entry.callsign = inbound.Callsign
  120. entry.fixed = inbound.FixedSequence
  121. entry.arrivalRoute = inbound.PlannedStar.Name
  122. entry.arrivalRunway = inbound.PlannedRunway.Name
  123. #performance = entry.performance.add()
  124. entry.performance.iasAboveFL240 = int(round(inbound.PerformanceData.SpeedAboveFL240))
  125. entry.performance.iasAboveFL100 = int(round(inbound.PerformanceData.SpeedAboveFL100))
  126. entry.performance.iasBelowFL100 = int(round(inbound.PerformanceData.SpeedBelowFL100))
  127. entry.performance.iasApproach = int(round(inbound.PerformanceData.SpeedApproach))
  128. for waypoint in inbound.PlannedArrivalRoute:
  129. wp = entry.waypoints.add()
  130. wp.name = waypoint.Waypoint.Name
  131. wp.altitude = int(round(waypoint.Altitude))
  132. wp.indicatedAirspeed = int(round(waypoint.IndicatedAirspeed))
  133. wp.groundSpeed = int(round(waypoint.GroundSpeed))
  134. pta = str(waypoint.PTA)
  135. delimiter = pta.find('.')
  136. if -1 == delimiter:
  137. delimiter = pta.find('+')
  138. wp.pta = pta[0:delimiter]
  139. message = sequence.SerializeToString()
  140. self.NotificationSocket.send(message)