From f2595ea604cd95beaae44b47bffad210af33ce1e Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Mon, 28 Oct 2019 21:25:34 +0100 Subject: [PATCH] handle scans --- README.md | 3 +++ library/emailhandling.py | 7 ++++--- library/lodur.py | 4 ++-- library/webdav.py | 6 +++++- main.py | 26 +++++++++++++++++++++++++- 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 6dfa3f6..789da6a 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,9 @@ pylokid funktioniert so: diese im Lodur am entsprechenden Einsatzrapport angehängt. * Ist der Einsatz abgeschlossen und das Einsatzprotokoll eingetroffen werden die Informationen im Lodur nachgetragen. +* Wird der von Hand ausgefüllte Einsatzrapport via Scanner per E-Mail + an das E-Mail Postfach gesendet (Betreff "Attached Image FXXXXXXXX") + wird das PDF in der Cloud und im Lodur gespeichert. Desweiteren wird über Pushover eine Nachricht mit möglichst vielen Informationen publiziert. diff --git a/library/emailhandling.py b/library/emailhandling.py index 9f102c8..41d11b6 100644 --- a/library/emailhandling.py +++ b/library/emailhandling.py @@ -9,7 +9,7 @@ import email import email.parser import imaplib -_EMAIL_SUBJECTS = '(OR SUBJECT "Einsatzausdruck_FW" SUBJECT "Einsatzprotokoll" UNSEEN)' +_EMAIL_SUBJECTS = '(OR OR SUBJECT "Einsatzausdruck_FW" SUBJECT "Einsatzprotokoll" SUBJECT "Attached Image" UNSEEN)' class EmailHandling: """ Email handling """ @@ -32,7 +32,7 @@ class EmailHandling: def search_emails(self): """ searches for emails matching the configured subject """ - self.logger.info('Searching for messages matching the subject') + self.logger.info('Searching for messages matching: %s', _EMAIL_SUBJECTS) try: typ, msg_ids = self.imap.search( None, @@ -102,7 +102,8 @@ class EmailHandling: def parse_subject(self, subject): """ extract f id and type from subject """ - parsed = re.search('(.*): (F[0-9].*)', subject) + # This regex matches the subjects filtered already in IMAP search + parsed = re.search('([a-zA-Z]* ?[a-zA-Z]*):? ?(F[0-9].*)?', subject) f_type = parsed.group(1) f_id = parsed.group(2) diff --git a/library/lodur.py b/library/lodur.py index 135894a..8a0b4fd 100644 --- a/library/lodur.py +++ b/library/lodur.py @@ -176,7 +176,7 @@ class Lodur: def einsatzrapport_alarmdepesche(self, f_id, file_path, webdav_client): """ Upload a file to Alarmdepesche """ - self.logger.info('[%s] Submitting Alarmdepesche to Lodur', f_id) + self.logger.info('[%s] Submitting File to Lodur "Alarmdepesche"', f_id) # Login to lodur self.login() @@ -193,7 +193,7 @@ class Lodur: # Submit the form self.browser.submit_selected() - self.logger.info('[%s] Alarmdepesche submitted', f_id) + self.logger.info('[%s] File uploaded', f_id) def submit_form_einsatzrapport(self, lodur_data): """ Form in module 36 - Einsatzrapport """ diff --git a/library/webdav.py b/library/webdav.py index 600ed9d..4224736 100644 --- a/library/webdav.py +++ b/library/webdav.py @@ -34,7 +34,11 @@ class WebDav: """ uploads a file to webdav - checks for existence before doing so """ # upload with webdav - remote_upload_dir = self.webdav_basedir + "/" + str(datetime.now().year) + "/" + f_id + if f_id == None: + remote_upload_dir = self.webdav_basedir + "/Inbox" + else: + remote_upload_dir = self.webdav_basedir + "/" + str(datetime.now().year) + "/" + f_id + self.logger.info('[%s] Uploading file to WebDAV "%s"', f_id, remote_upload_dir) # create directory if not yet there diff --git a/main.py b/main.py index 88d6af0..03418c7 100644 --- a/main.py +++ b/main.py @@ -34,7 +34,7 @@ LODUR_BASE_URL = os.getenv("LODUR_BASE_URL") HEARTBEAT_URL = os.getenv("HEARTBEAT_URL") PUSHOVER_API_TOKEN = os.getenv("PUSHOVER_API_TOKEN") PUSHOVER_USER_KEY = os.getenv("PUSHOVER_USER_KEY") -PYLOKID_VERSION = "2.0.0" +PYLOKID_VERSION = "2.1.0" def main(): """ main """ @@ -92,6 +92,8 @@ def main(): for subject in attachments: f_type, f_id = imap_client.parse_subject(subject) file_name = attachments[subject] + + # Upload file to cloud webdav_client.upload(file_name, f_id) # Take actions - depending on the type @@ -191,6 +193,28 @@ def main(): f_id ) + # This is a scan from the Depot printer + elif f_type == 'Attached Image': + logger.info('[%s] Processing type %s', f_id, f_type) + + # Attach scan in Lodur if f_id is available + if f_id != None: + lodur_data = webdav_client.get_lodur_data(f_id) + if lodur_data: + # Upload scan to Lodur + lodur_client.einsatzrapport_alarmdepesche( + f_id, + os.path.join(TMP_DIR, file_name), + webdav_client, + ) + + logger.info( + '[%s] Publishing message on Pushover', f_id + ) + pushover.send_message( + "Scan {} wurde bearbeitet und in Cloud geladen".format(f_id), + title="Feuerwehr Scan bearbeitet", + ) else: logger.error('[%s] Unknown type: %s', f_id, f_type)