From fd90e8c2e9c49f62b83509ee57f37b18120aadc8 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Sun, 22 Sep 2019 22:02:20 +0200 Subject: [PATCH] publish on pushover --- library/gotify.py | 36 ---------------------------- library/mqtt.py | 46 ----------------------------------- main.py | 61 +++++++++++++++++++++++++++-------------------- requirements.txt | 2 +- 4 files changed, 36 insertions(+), 109 deletions(-) delete mode 100644 library/gotify.py delete mode 100644 library/mqtt.py diff --git a/library/gotify.py b/library/gotify.py deleted file mode 100644 index a971415..0000000 --- a/library/gotify.py +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env python3 - -""" Gotify Functions """ - -import logging -import json -from urllib.parse import urljoin -import requests - -class GotifyClient: - """ Gotify Client """ - - def __init__(self, url, token): - self.logger = logging.getLogger(__name__) - self.logger.info('Gotify URL %s', url) - - self.url = url - self.token = token - - def send_message(self, f_type, f_id, pdf_data=None, pdf_file=None): - """ Publish a message over Gotify """ - - requestURL = urljoin(self.url, '/message?token=' + self.token) - - try: - resp = requests.post(requestURL, json={ - 'title': 'Einsatz ' + f_id, - 'message': f_type, - 'priority': 5 - }) - except requests.exceptions.RequestException as err: - self.logger.error('[%s] Could not connect to Gotify server: %e', f_id, err) - - # Print request result if server returns http error code - if resp.status_code is not requests.codes.ok: - self.logger.error('[%s] Could not send message to Gotify server: %e', f_id, bytes.decode(resp.content)) diff --git a/library/mqtt.py b/library/mqtt.py deleted file mode 100644 index 9ecb9e7..0000000 --- a/library/mqtt.py +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env python3 - -""" MQTT Functions """ - -import logging -import json -import paho.mqtt.client as mqtt - -class MQTTClient: - """ MQTT Client """ - - def __init__(self, server, username, password, base_topic): - self.logger = logging.getLogger(__name__) - self.logger.info('Connecting to MQTT broker %s', server) - - try: - self.mqtt_client = mqtt.Client('pylokid') - self.mqtt_client.username_pw_set(username, password=password) - self.mqtt_client.tls_set() - self.mqtt_client.connect(server, 8883, 60) - self.mqtt_client.loop_start() - self.logger.info('MQTT connection successful') - except Exception as err: - self.logger.error('MQTT connection failed: %s', str(err)) - - self.base_topic = base_topic - - def send_message(self, f_type, f_id, pdf_data=None, pdf_file=None): - """ Publish a message over MQTT """ - - if f_type == 'Einsatzausdruck_FW': - try: - topic = "{0}/Einsatzausdruck_FW/{1}/".format(self.base_topic, f_id) - self.logger.info('[%s] Publishing information on MQTT topic %s', f_id, topic) - self.mqtt_client.publish(topic + 'json', json.dumps(pdf_data)) - - ## Publish the PDF blob - pdf_fh = open(pdf_file, 'rb') - pdf_binary = pdf_fh.read() - self.mqtt_client.publish(topic + 'pdf', bytes(pdf_binary)) - except IndexError as err: - self.logger.info('[%s] Cannot publish information: %s', f_id, err) - elif f_type == 'Einsatzprotokoll': - topic = "{0}/Einsatzprotokoll/{1}/".format(self.base_topic, f_id) - self.logger.info('[%s] Publishing information on MQTT topic %s', f_id, topic) - self.mqtt_client.publish(topic + 'json', json.dumps(pdf_data)) diff --git a/main.py b/main.py index 7c4ef15..88d6af0 100644 --- a/main.py +++ b/main.py @@ -8,12 +8,11 @@ import time import requests from dotenv import find_dotenv, load_dotenv +from pushover import Client # local classes from library.emailhandling import EmailHandling from library.lodur import Lodur -from library.mqtt import MQTTClient -from library.gotify import GotifyClient from library.pdftotext import PDFParsing from library.webdav import WebDav @@ -29,16 +28,12 @@ WEBDAV_USERNAME = os.getenv("WEBDAV_USERNAME") WEBDAV_PASSWORD = os.getenv("WEBDAV_PASSWORD") WEBDAV_BASEDIR = os.getenv("WEBDAV_BASEDIR") TMP_DIR = os.getenv("TMP_DIR", "/tmp") -MQTT_SERVER = os.getenv("MQTT_SERVER") -MQTT_USER = os.getenv("MQTT_USER") -MQTT_PASSWORD = os.getenv("MQTT_PASSWORD") -MQTT_BASE_TOPIC = os.getenv("MQTT_BASE_TOPIC", "pylokid") LODUR_USER = os.getenv("LODUR_USER") LODUR_PASSWORD = os.getenv("LODUR_PASSWORD") LODUR_BASE_URL = os.getenv("LODUR_BASE_URL") HEARTBEAT_URL = os.getenv("HEARTBEAT_URL") -GOTIFY_URL = os.getenv("GOTIFY_URL") -GOTIFY_TOKEN = os.getenv("GOTIFY_TOKEN") +PUSHOVER_API_TOKEN = os.getenv("PUSHOVER_API_TOKEN") +PUSHOVER_USER_KEY = os.getenv("PUSHOVER_USER_KEY") PYLOKID_VERSION = "2.0.0" def main(): @@ -77,18 +72,10 @@ def main(): TMP_DIR, ) - # Initialize MQTT Sessions - mqtt_client = MQTTClient( - MQTT_SERVER, - MQTT_USER, - MQTT_PASSWORD, - MQTT_BASE_TOPIC, - ) - - # Initialize Gotify - gotify_client = GotifyClient( - GOTIFY_URL, - GOTIFY_TOKEN, + # Initialize Pushover + pushover = Client( + user_key=PUSHOVER_USER_KEY, + api_token=PUSHOVER_API_TOKEN ) # Initialize PDF Parser @@ -133,9 +120,25 @@ def main(): f_id, ) - # publish Einsatz on MQTT and Gotify - mqtt_client.send_message(f_type, f_id, pdf_data, pdf_file) - gotify_client.send_message(f_type, f_id, pdf_data, pdf_file) + # publish Einsatz on Pushover + logger.info( + '[%s] Publishing message on Pushover', f_id + ) + pushover.send_message( + "Einsatz {} eröffnet: {}\n\n* Ort: {}\n* Melder: {}\n* Hinweis: {}\n* {}\n\n{}\n\n{}".format( + f_id, + pdf_data['einsatz'], + pdf_data['ort'], + pdf_data['melder'].replace('\n',' '), + pdf_data['hinweis'], + pdf_data['sondersignal'], + pdf_data['disponierteeinheiten'], + pdf_data['bemerkungen'], + ), + title="Feuerwehr Einsatz", + url="https://www.google.com/maps/search/?api=1&query={}".format(pdf_data['ort']), + url_title="Ort auf Karte suchen" + ) # create new Einsatzrapport in Lodur lodur_client.einsatzrapport( @@ -173,9 +176,15 @@ def main(): # Update entry in Lodur with parse PDF data lodur_client.einsatzprotokoll(f_id, pdf_data, webdav_client) - # Einsatz finished - publish on MQTT and Gotify - mqtt_client.send_message(f_type, f_id, pdf_data, pdf_file) - gotify_client.send_message(f_type, f_id, pdf_data, pdf_file) + # Einsatz finished - publish on pushover + logger.info( + '[%s] Publishing message on Pushover', f_id + ) + pushover.send_message( + "Einsatz {} beendet".format(f_id), + title="Feuerwehr Einsatz beendet", + ) + else: logger.error( '[%s] Cannot process Einsatzprotokoll as there is no Lodur ID', diff --git a/requirements.txt b/requirements.txt index 4d82ab9..3992122 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,6 @@ aioeasywebdav==2.4.0 # MechanicalSoup > 0.11.0 produces "TypeError: expected string or bytes-like # object" on file upload MechanicalSoup==0.11.0 -paho-mqtt==1.3.1 python-dotenv==0.10.3 requests>=2.20.0 +python-pushover==0.4 \ No newline at end of file