Selaa lähdekoodia

send the sequence to the UI

Sven Czarnian 3 vuotta sitten
vanhempi
commit
7c90ecc3b5
1 muutettua tiedostoa jossa 40 lisäystä ja 6 poistoa
  1. 40 6
      aman/com/WebUI.py

+ 40 - 6
aman/com/WebUI.py

@@ -1,9 +1,23 @@
 #!/usr/bin/env python
 
 import json
+import requests
+import sys
+
+from json import JSONEncoder
+from requests.adapters import HTTPAdapter
+from requests.packages.urllib3.util.retry import Retry
+from requests.structures import CaseInsensitiveDict
 
 from aman.config.Server import Server
-from aman.types.Inbound import Inbound
+
+class InboundEncoder(JSONEncoder):
+    def default(self, o):
+        pta = str(o.PlannedArrivalTime)
+        delimiter = pta.find('.')
+        if -1 == delimiter:
+            delimiter = pta.find('+')
+        return { 'callsign' : o.Callsign, 'runway' : o.PlannedRunway.Name, 'pta' : pta[0:delimiter] }
 
 class WebUI:
     def __init__(self):
@@ -20,11 +34,31 @@ class WebUI:
     def release(self):
         return
 
+    def createSession():
+        # prepare the session
+        session = requests.Session()
+        retry = Retry(total=1, read=1, connect=1)
+        adapter = HTTPAdapter(max_retries=retry)
+        session.mount('http://', adapter)
+        session.mount('https://', adapter)
+        return session
+
+    def requestConfiguration(self, airport):
+        return
+
     def sendSequence(self, airport, inbounds):
-        sequence = []
+        # prepare the HTTP header and session
+        headers = CaseInsensitiveDict()
+        headers['Accept'] = 'application/json'
+        headers['Content-Type'] = 'application/json'
+        session = WebUI.createSession()
 
-        for inbound in inbounds:
-            sequence.append(str(inbound.toJSON()))
+        # prepare the relevant information
+        url = self.Config.WebUiUrl + self.Config.WebUiSequenceNotification
+        data = json.dumps({ 'airport': airport, 'sequence': inbounds }, ensure_ascii=True, cls=InboundEncoder)
 
-        # TODO send to the server
-        #print(json.dumps({ 'airport': airport, 'sequence': sequence }, ensure_ascii=True))
+        # send to the server
+        try:
+            response = session.patch(url, headers=headers, data=data, timeout=2)
+        except requests.exceptions.ConnectTimeout:
+            return