publish on pushover
This commit is contained in:
parent
8c371e61d8
commit
fd90e8c2e9
|
@ -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))
|
|
|
@ -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))
|
|
61
main.py
61
main.py
|
@ -8,12 +8,11 @@ import time
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from dotenv import find_dotenv, load_dotenv
|
from dotenv import find_dotenv, load_dotenv
|
||||||
|
from pushover import Client
|
||||||
|
|
||||||
# local classes
|
# local classes
|
||||||
from library.emailhandling import EmailHandling
|
from library.emailhandling import EmailHandling
|
||||||
from library.lodur import Lodur
|
from library.lodur import Lodur
|
||||||
from library.mqtt import MQTTClient
|
|
||||||
from library.gotify import GotifyClient
|
|
||||||
from library.pdftotext import PDFParsing
|
from library.pdftotext import PDFParsing
|
||||||
from library.webdav import WebDav
|
from library.webdav import WebDav
|
||||||
|
|
||||||
|
@ -29,16 +28,12 @@ WEBDAV_USERNAME = os.getenv("WEBDAV_USERNAME")
|
||||||
WEBDAV_PASSWORD = os.getenv("WEBDAV_PASSWORD")
|
WEBDAV_PASSWORD = os.getenv("WEBDAV_PASSWORD")
|
||||||
WEBDAV_BASEDIR = os.getenv("WEBDAV_BASEDIR")
|
WEBDAV_BASEDIR = os.getenv("WEBDAV_BASEDIR")
|
||||||
TMP_DIR = os.getenv("TMP_DIR", "/tmp")
|
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_USER = os.getenv("LODUR_USER")
|
||||||
LODUR_PASSWORD = os.getenv("LODUR_PASSWORD")
|
LODUR_PASSWORD = os.getenv("LODUR_PASSWORD")
|
||||||
LODUR_BASE_URL = os.getenv("LODUR_BASE_URL")
|
LODUR_BASE_URL = os.getenv("LODUR_BASE_URL")
|
||||||
HEARTBEAT_URL = os.getenv("HEARTBEAT_URL")
|
HEARTBEAT_URL = os.getenv("HEARTBEAT_URL")
|
||||||
GOTIFY_URL = os.getenv("GOTIFY_URL")
|
PUSHOVER_API_TOKEN = os.getenv("PUSHOVER_API_TOKEN")
|
||||||
GOTIFY_TOKEN = os.getenv("GOTIFY_TOKEN")
|
PUSHOVER_USER_KEY = os.getenv("PUSHOVER_USER_KEY")
|
||||||
PYLOKID_VERSION = "2.0.0"
|
PYLOKID_VERSION = "2.0.0"
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -77,18 +72,10 @@ def main():
|
||||||
TMP_DIR,
|
TMP_DIR,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Initialize MQTT Sessions
|
# Initialize Pushover
|
||||||
mqtt_client = MQTTClient(
|
pushover = Client(
|
||||||
MQTT_SERVER,
|
user_key=PUSHOVER_USER_KEY,
|
||||||
MQTT_USER,
|
api_token=PUSHOVER_API_TOKEN
|
||||||
MQTT_PASSWORD,
|
|
||||||
MQTT_BASE_TOPIC,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Initialize Gotify
|
|
||||||
gotify_client = GotifyClient(
|
|
||||||
GOTIFY_URL,
|
|
||||||
GOTIFY_TOKEN,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Initialize PDF Parser
|
# Initialize PDF Parser
|
||||||
|
@ -133,9 +120,25 @@ def main():
|
||||||
f_id,
|
f_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
# publish Einsatz on MQTT and Gotify
|
# publish Einsatz on Pushover
|
||||||
mqtt_client.send_message(f_type, f_id, pdf_data, pdf_file)
|
logger.info(
|
||||||
gotify_client.send_message(f_type, f_id, pdf_data, pdf_file)
|
'[%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
|
# create new Einsatzrapport in Lodur
|
||||||
lodur_client.einsatzrapport(
|
lodur_client.einsatzrapport(
|
||||||
|
@ -173,9 +176,15 @@ def main():
|
||||||
# Update entry in Lodur with parse PDF data
|
# Update entry in Lodur with parse PDF data
|
||||||
lodur_client.einsatzprotokoll(f_id, pdf_data, webdav_client)
|
lodur_client.einsatzprotokoll(f_id, pdf_data, webdav_client)
|
||||||
|
|
||||||
# Einsatz finished - publish on MQTT and Gotify
|
# Einsatz finished - publish on pushover
|
||||||
mqtt_client.send_message(f_type, f_id, pdf_data, pdf_file)
|
logger.info(
|
||||||
gotify_client.send_message(f_type, f_id, pdf_data, pdf_file)
|
'[%s] Publishing message on Pushover', f_id
|
||||||
|
)
|
||||||
|
pushover.send_message(
|
||||||
|
"Einsatz {} beendet".format(f_id),
|
||||||
|
title="Feuerwehr Einsatz beendet",
|
||||||
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
logger.error(
|
logger.error(
|
||||||
'[%s] Cannot process Einsatzprotokoll as there is no Lodur ID',
|
'[%s] Cannot process Einsatzprotokoll as there is no Lodur ID',
|
||||||
|
|
|
@ -2,6 +2,6 @@ aioeasywebdav==2.4.0
|
||||||
# MechanicalSoup > 0.11.0 produces "TypeError: expected string or bytes-like
|
# MechanicalSoup > 0.11.0 produces "TypeError: expected string or bytes-like
|
||||||
# object" on file upload
|
# object" on file upload
|
||||||
MechanicalSoup==0.11.0
|
MechanicalSoup==0.11.0
|
||||||
paho-mqtt==1.3.1
|
|
||||||
python-dotenv==0.10.3
|
python-dotenv==0.10.3
|
||||||
requests>=2.20.0
|
requests>=2.20.0
|
||||||
|
python-pushover==0.4
|
Loading…
Reference in a new issue