From 7c90ecc3b5dbf1779c85b414885661383189380a Mon Sep 17 00:00:00 2001 From: Sven Czarnian Date: Thu, 11 Nov 2021 14:22:58 +0100 Subject: [PATCH] send the sequence to the UI --- aman/com/WebUI.py | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/aman/com/WebUI.py b/aman/com/WebUI.py index 6b24f8b..238d88a 100644 --- a/aman/com/WebUI.py +++ b/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