pylokid/pylokid/main.py

227 lines
8 KiB
Python
Raw Normal View History

2017-12-21 21:12:44 +00:00
#!/usr/bin/env python3
""" Thy pylokid main program """
import logging
2017-12-30 18:19:40 +00:00
import os
2017-12-21 21:12:44 +00:00
import time
2017-12-30 18:19:40 +00:00
2017-12-26 13:27:02 +00:00
import requests
2017-12-30 18:19:40 +00:00
from dotenv import find_dotenv, load_dotenv
2019-09-22 20:02:20 +00:00
from pushover import Client
2017-12-30 18:19:40 +00:00
# local classes
2021-02-11 20:00:29 +00:00
from pylokid.library.emailhandling import EmailHandling
from pylokid.library.lodur import Lodur
from pylokid.library.pdftotext import PDFParsing
from pylokid.library.webdav import WebDav
2017-12-21 21:12:44 +00:00
2017-12-30 18:19:40 +00:00
# Configuration
2017-12-21 21:12:44 +00:00
load_dotenv(find_dotenv())
2017-12-30 18:19:40 +00:00
IMAP_SERVER = os.getenv("IMAP_SERVER")
IMAP_USERNAME = os.getenv("IMAP_USERNAME")
IMAP_PASSWORD = os.getenv("IMAP_PASSWORD")
IMAP_MAILBOX = os.getenv("IMAP_MAILBOX", "INBOX")
2018-01-02 16:58:35 +00:00
IMAP_CHECK_INTERVAL = os.getenv("IMAP_CHECK_INTERVAL", "10")
2017-12-30 18:19:40 +00:00
WEBDAV_URL = os.getenv("WEBDAV_URL")
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")
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")
2019-09-22 20:02:20 +00:00
PUSHOVER_API_TOKEN = os.getenv("PUSHOVER_API_TOKEN")
PUSHOVER_USER_KEY = os.getenv("PUSHOVER_USER_KEY")
2020-02-08 17:02:32 +00:00
PYLOKID_VERSION = "2.1.2"
2017-12-23 16:55:02 +00:00
2017-12-21 21:12:44 +00:00
def main():
""" main """
2017-12-30 18:19:40 +00:00
# Logging configuration
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger('pylokid')
logger.info('Starting pylokid version %s', PYLOKID_VERSION)
2017-12-30 18:19:40 +00:00
# Initialize IMAP Session
imap_client = EmailHandling(
IMAP_SERVER,
IMAP_USERNAME,
IMAP_PASSWORD,
IMAP_MAILBOX,
TMP_DIR,
)
2017-12-25 11:39:37 +00:00
2017-12-30 18:19:40 +00:00
# Initialize Lodur Session
lodur_client = Lodur(
LODUR_BASE_URL,
LODUR_USER,
LODUR_PASSWORD,
)
2017-12-23 16:55:02 +00:00
2017-12-30 18:19:40 +00:00
# Initialize WebDav Session
webdav_client = WebDav(
WEBDAV_URL,
WEBDAV_USERNAME,
WEBDAV_PASSWORD,
WEBDAV_BASEDIR,
TMP_DIR,
2017-12-25 11:39:37 +00:00
)
2017-12-23 16:55:02 +00:00
2019-09-22 20:02:20 +00:00
# Initialize Pushover
pushover = Client(
user_key=PUSHOVER_USER_KEY,
api_token=PUSHOVER_API_TOKEN
2019-07-25 20:06:50 +00:00
)
2017-12-30 18:19:40 +00:00
# Initialize PDF Parser
2019-09-22 16:10:09 +00:00
pdf = PDFParsing()
2017-12-30 18:19:40 +00:00
2018-01-02 15:45:37 +00:00
# Main Loop
2017-12-21 21:12:44 +00:00
while True:
2017-12-25 11:39:37 +00:00
attachments = {}
2017-12-30 18:19:40 +00:00
num_messages, msg_ids = imap_client.search_emails()
if num_messages:
attachments = imap_client.store_attachments(msg_ids)
2017-12-25 11:39:37 +00:00
2017-12-30 18:19:40 +00:00
if attachments:
2017-12-25 11:39:37 +00:00
for subject in attachments:
2017-12-30 18:19:40 +00:00
f_type, f_id = imap_client.parse_subject(subject)
2017-12-25 11:39:37 +00:00
file_name = attachments[subject]
2019-10-28 20:25:34 +00:00
# Upload file to cloud
2017-12-30 18:19:40 +00:00
webdav_client.upload(file_name, f_id)
2017-12-25 11:39:37 +00:00
# Take actions - depending on the type
if f_type == 'Einsatzausdruck_FW':
2018-01-02 15:45:37 +00:00
logger.info('[%s] Processing type %s', f_id, f_type)
lodur_data = webdav_client.get_lodur_data(f_id)
2018-01-02 15:45:37 +00:00
if lodur_data:
2017-12-25 12:52:43 +00:00
logger.info(
'[%s] Einsatzrapport already created in Lodur', f_id
2017-12-25 12:52:43 +00:00
)
# Upload Alarmdepesche as it could contain more information
# than the first one
2017-12-31 16:26:33 +00:00
lodur_client.einsatzrapport_alarmdepesche(
f_id,
2017-12-30 18:19:40 +00:00
os.path.join(TMP_DIR, file_name),
webdav_client,
2017-12-26 13:23:34 +00:00
)
2017-12-25 12:52:43 +00:00
else:
## Here we get the initial Einsatzauftrag - Time to run
2017-12-28 19:07:56 +00:00
# get as many information from PDF as possible
pdf_file = os.path.join(TMP_DIR, file_name)
2017-12-30 18:19:40 +00:00
pdf_data = pdf.extract_einsatzausdruck(
pdf_file,
2017-12-30 16:01:13 +00:00
f_id,
)
2017-12-28 19:07:56 +00:00
2019-09-22 20:02:20 +00:00
# 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"
)
2017-12-25 12:52:43 +00:00
# create new Einsatzrapport in Lodur
lodur_client.einsatzrapport(
2017-12-25 12:52:43 +00:00
f_id,
2017-12-28 19:07:56 +00:00
pdf_data,
webdav_client,
2017-12-25 12:52:43 +00:00
)
# upload Alarmdepesche PDF to Lodur
2017-12-31 16:26:33 +00:00
lodur_client.einsatzrapport_alarmdepesche(
f_id,
2017-12-30 18:19:40 +00:00
os.path.join(TMP_DIR, file_name),
webdav_client,
2017-12-25 12:52:43 +00:00
)
2017-12-25 11:39:37 +00:00
elif f_type == 'Einsatzprotokoll':
2018-01-02 15:45:37 +00:00
logger.info('[%s] Processing type %s', f_id, f_type)
2017-12-25 12:52:43 +00:00
lodur_data = webdav_client.get_lodur_data(f_id)
if lodur_data:
# Upload Einsatzprotokoll to Lodur
2017-12-31 16:26:33 +00:00
lodur_client.einsatzrapport_alarmdepesche(
f_id,
2017-12-30 18:19:40 +00:00
os.path.join(TMP_DIR, file_name),
webdav_client,
2017-12-25 12:52:43 +00:00
)
# Parse the Einsatzprotokoll PDF
2018-01-18 21:55:50 +00:00
pdf_file = os.path.join(TMP_DIR, file_name)
2017-12-30 18:19:40 +00:00
pdf_data = pdf.extract_einsatzprotokoll(
2018-01-18 21:55:50 +00:00
pdf_file,
2017-12-30 16:01:13 +00:00
f_id,
)
2019-11-03 12:22:47 +00:00
# Update entry in Lodur with parsed PDF data
lodur_client.einsatzprotokoll(f_id, pdf_data, webdav_client)
2019-09-22 20:02:20 +00:00
# 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",
)
2017-12-25 12:52:43 +00:00
else:
logger.error(
'[%s] Cannot process Einsatzprotokoll as there is no Lodur ID',
f_id
)
2019-11-03 12:22:47 +00:00
# This is usually a scan from the Depot printer
elif f_type == 'Einsatzrapport':
2019-10-28 20:25:34 +00:00
logger.info('[%s] Processing type %s', f_id, f_type)
# Attach scan in Lodur if f_id is available
if f_id != None:
2019-11-03 12:22:47 +00:00
pdf_file = os.path.join(TMP_DIR, file_name)
lodur_client.einsatzrapport_scan(f_id, pdf_file, webdav_client)
2019-10-28 20:25:34 +00:00
logger.info(
'[%s] Publishing message on Pushover', f_id
)
2019-11-03 12:22:47 +00:00
2019-10-28 20:25:34 +00:00
pushover.send_message(
"Scan {} wurde bearbeitet und in Cloud geladen".format(f_id),
title="Feuerwehr Scan bearbeitet",
)
2017-12-25 11:39:37 +00:00
else:
logger.error('[%s] Unknown type: %s', f_id, f_type)
2017-12-25 11:39:37 +00:00
2017-12-26 13:27:02 +00:00
# send heartbeat
2017-12-30 18:19:40 +00:00
requests.get(HEARTBEAT_URL)
2017-12-25 11:39:37 +00:00
# repeat every
2018-01-02 16:58:35 +00:00
logger.info('Waiting %s seconds until next check', IMAP_CHECK_INTERVAL)
time.sleep(int(IMAP_CHECK_INTERVAL))
2017-12-21 21:12:44 +00:00
if __name__ == '__main__':
2017-12-30 18:19:40 +00:00
try:
main()
except KeyboardInterrupt:
print("Byebye")