Draeger-Handyalarm-Bridge/helpers.py

56 lines
1.9 KiB
Python

REGEX = r"SDS_ILS_BEGINN(#D#Leitstelle:\s+)(?P<number>\d*)(?P<sonderrechte>m|o|i)?(?P<fzg>RTW|NAW|KTW)? (?P<keyword>Trauma Gesicht/|Atemnot Akut|Trauma Extremit|Abdomen Akut|Person droht zu spr|Hilflose Person|\S*|) (?P<address>\D*)(.*) \$GPSN(.*)E(.*)SDS_ILS_ENDE"
import os
import PyPDF2
import re
from dotenv import load_dotenv
from geopy.geocoders import Nominatim
load_dotenv(override=True)
DRAEGER_API_KEY = os.environ.get("DRAEGER_API_KEY", default=None)
def get_city(lat, lon):
app = Nominatim(user_agent="Test")
location = app.reverse(query="N" + lat[:2] + '.' + lat[2:] + ", E" + lon[:2] + '.' + lon[2:], namedetails=True)
return [location.raw.get("address").get("postcode"), location.raw.get("address").get("city"), location.raw.get("address").get("city_district")]
def read_pdf(file_path):
with open(file_path, 'rb') as file:
reader = PyPDF2.PdfReader(file)
text = ''
for page in reader.pages:
text += page.extract_text()
return text
def get_draeger_json(text):
match = re.findall(REGEX, text.replace("\n", ""))
if (len(match) != 0):
data = {}
data["apiKey"] = DRAEGER_API_KEY
if(match[0][1].startswith("9")): # 9 => RD (First Rsponder)
data["alertingKeyword"] = "Wachalarm"
else:
data["alertingKeyword"] = match[0][4]
data["alertingKeywordText"] = match[0][1] + " " + match[0][4]
data["street"] = match[0][5]
data["houseNumber"] = match[0][6]
try:
city = get_city(match[0][7], match[0][8])
data["city"] = city[0] + " " + city[1]
data["cityDistrict"] = city[2]
except Exception as e:
print(f'Fehler: {e}')
data["lat"] = match[0][7][:2] + '.' + match[0][7][2:]
data["lon"] = match[0][8][:2] + '.' + match[0][8][2:]
print(data)
return data
else:
return None