From 4b0614c7dacd7d8e8f0be1c4cb96cbae5ee12579 Mon Sep 17 00:00:00 2001 From: Jan Speller Date: Mon, 24 Feb 2020 21:51:19 +0100 Subject: [PATCH] threads and a few fixes to wildcards --- boswatch/wildcard.py | 8 ++++---- docu/docs/develop/packet.md | 2 +- docu/docs/modul/geocoding.md | 5 +++-- docu/docs/plugin/telegram.md | 12 ++---------- module/geocoding.py | 15 ++++++++++++--- plugin/telegram.py | 4 ++-- 6 files changed, 24 insertions(+), 22 deletions(-) diff --git a/boswatch/wildcard.py b/boswatch/wildcard.py index 91f2fea..3519502 100644 --- a/boswatch/wildcard.py +++ b/boswatch/wildcard.py @@ -65,8 +65,8 @@ def replaceWildcards(message, bwPacket): "{CBRCH}": bwPacket.get("clientBranch"), # boswatch wildcards - "{INSRC}": bwPacket.get("mode"), - "{TIMES}": bwPacket.get("mode"), + "{INSRC}": bwPacket.get("inputSource"), + "{TIMES}": bwPacket.get("timestamp"), "{FREQ}": bwPacket.get("frequency"), "{MODE}": bwPacket.get("mode"), @@ -78,7 +78,7 @@ def replaceWildcards(message, bwPacket): "{VEHC}": bwPacket.get("vehicle"), "{STAT}": bwPacket.get("status"), "{DIR}": bwPacket.get("direction"), - "{DIRT}": bwPacket.get("dirextionText"), + "{DIRT}": bwPacket.get("directionText"), "{TACI}": bwPacket.get("tacticalInfo"), # pocsag wildcards @@ -99,6 +99,6 @@ def replaceWildcards(message, bwPacket): for wildcard, field in _additionalWildcards.items(): if field is not None: - message = message.replace(wildcard, bwPacket.getField(field)) + message = message.replace(wildcard, bwPacket.get(field)) return message diff --git a/docu/docs/develop/packet.md b/docu/docs/develop/packet.md index 14d2e88..e67de83 100644 --- a/docu/docs/develop/packet.md +++ b/docu/docs/develop/packet.md @@ -47,5 +47,5 @@ Ein BOSWatch Datenpaket wird in einem Python Dict abgebildet. In der nachfolgend |vehicle|X||||`{VEC}`|| |status|X||||`{STAT}`|| |direction|X||||`{DIR}`|| -|dirextionText|X||||`{DIRT}`|(Fhz->Lst, Lst->Fhz)| +|directionText|X||||`{DIRT}`|(Fhz->Lst, Lst->Fhz)| |tacticalInfo|X||||`{TACI}`|(I, II, III, IV)| diff --git a/docu/docs/modul/geocoding.md b/docu/docs/modul/geocoding.md index 5876f08..eb69eff 100644 --- a/docu/docs/modul/geocoding.md +++ b/docu/docs/modul/geocoding.md @@ -40,7 +40,6 @@ geoRegex|Regex Capture-Group zum Herausfiltern der Adresse| ## Abhängigkeiten - geocoder -- re --- ## Paket Modifikationen @@ -50,4 +49,6 @@ geoRegex|Regex Capture-Group zum Herausfiltern der Adresse| --- ## Zusätzliche Wildcards -- keine \ No newline at end of file +- `{ADDRESS}`: gefundene Adresse +- `{LAT}`: Latitude der Adresse +- `{LON}`: Longitude der Adresse \ No newline at end of file diff --git a/docu/docs/plugin/telegram.md b/docu/docs/plugin/telegram.md index d72a84e..2273cc5 100644 --- a/docu/docs/plugin/telegram.md +++ b/docu/docs/plugin/telegram.md @@ -16,6 +16,7 @@ Außerdem werden Locations versenden, wenn die Felder `lat` und `lon` im Paket d |Feld|Beschreibung|Default| |----|------------|-------| +|message|Format der Nachricht|| |botToken|Der Api-Key des Telegram-Bots|| |chatIds|Liste mit Chat-Ids der Empfängers / der Emfänger-Gruppen|| @@ -25,6 +26,7 @@ Außerdem werden Locations versenden, wenn die Felder `lat` und `lon` im Paket d name: Telegram Plugin res: telegram config: + message: "{RIC}({SRIC})\n{MSG}" botToken: "{{ Telegram Bot Token }}" chatIds: - "{{ Telegram Chat Id }}" @@ -34,13 +36,3 @@ Außerdem werden Locations versenden, wenn die Felder `lat` und `lon` im Paket d ## Abhängigkeiten - python-telegram-bot - ---- -## Paket Modifikationen - -- keine - ---- -## Zusätzliche Wildcards - -- keine diff --git a/module/geocoding.py b/module/geocoding.py index 765bafc..c6cb8c5 100644 --- a/module/geocoding.py +++ b/module/geocoding.py @@ -46,10 +46,18 @@ class BoswatchModule(ModuleBase): @param bwPacket: A BOSWatch packet instance""" try: - address = re.search(self.config.get("regex"), bwPacket.get("message"))[1] + addressArray = re.search(self.config.get("regex"), bwPacket.get("message")) provider = self.config.get("apiProvider") + if addressArray[1] is None: + logging.warning("Address was not found in current Message, skipping geocoding") + return bwPacket + + address = addressArray[1] + bwPacket.set("address", address) + self.registerWildcard("{ADDRESS}", "address") logging.info("Found address: '" + address + "' in packet") + if "mapbox" == provider: logging.info("Using Mapbox as provider") g = geocoder.mapbox(address, key=self.config.get("apiToken")) @@ -63,9 +71,10 @@ class BoswatchModule(ModuleBase): logging.info("Found following coordinates for address: [lat=" + str(lat) + ", lon=" + str(lon) + "]") bwPacket.set("lat", lat) bwPacket.set("lon", lon) + self.registerWildcard("{LAT}", "lat") + self.registerWildcard("{LON}", "lon") + return bwPacket - except (IndexError, TypeError, ValueError): - logging.warning("Address was not found in current Message, skipping geocoding") except Exception as e: logging.error("Unknown Error while executing geocoding module: " + str(type(e).__name__) + ": " + str(e)) return bwPacket diff --git a/plugin/telegram.py b/plugin/telegram.py index b27ce6d..b37ebf5 100644 --- a/plugin/telegram.py +++ b/plugin/telegram.py @@ -41,9 +41,9 @@ class BoswatchPlugin(PluginBase): """!Called on POCSAG alarm @param bwPacket: bwPacket instance""" - msg = bwPacket.get("ric") + " (" + bwPacket.get("subric") + ")\n" + bwPacket.get("message") + msg = self.parseWildcards(self.config.get("message")) if bwPacket.get("lat") is not None and bwPacket.get("lon") is not None: - logging.info("Found coordinates in packet") + logging.debug("Found coordinates in packet") (lat, lon) = (bwPacket.get("lat"), bwPacket.get("lon")) for chatId in self.config.get("chatIds", default=[]):