pylokid/main.py

170 lines
5.6 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
# local classes
from emailhandling import EmailHandling
from lodur import Lodur
from mqtt import MQTTClient
from pdf_extract import PDFHandling
from webdav import WebDav
2017-12-21 21:12:44 +00:00
2017-12-30 18:19:40 +00:00
# TODO replace by IMAP idle
2017-12-25 12:52:43 +00:00
_INTERVAL = 10
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")
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")
MQTT_SERVER = os.getenv("MQTT_SERVER")
MQTT_USER = os.getenv("MQTT_USER")
MQTT_PASSWORD = os.getenv("MQTT_PASSWORD")
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")
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')
# 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
2017-12-30 18:19:40 +00:00
# Initialize MQTT Sessions
mqtt_client = MQTTClient(
MQTT_SERVER,
MQTT_USER,
MQTT_PASSWORD,
)
# Initialize PDF Parser
pdf = PDFHandling()
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]
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':
2017-12-30 18:19:40 +00:00
lodur_id = webdav_client.get_lodur_id(f_id)
2017-12-25 12:52:43 +00:00
if lodur_id:
logger.info(
'Einsatzrapport ' + f_id + ' already created in Lodur: ' + lodur_id
)
2017-12-26 13:23:34 +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(
2017-12-26 13:23:34 +00:00
lodur_id,
2017-12-30 18:19:40 +00:00
os.path.join(TMP_DIR, file_name),
2017-12-26 13:23:34 +00:00
)
2017-12-25 12:52:43 +00:00
else:
# this is real - publish Einsatz on MQTT
2017-12-30 16:01:13 +00:00
# TODO publish more information about the einsatz - coming from the PDF
2017-12-30 18:19:40 +00:00
mqtt_client.send_message(f_type, f_id)
2017-12-25 12:52:43 +00:00
2017-12-28 19:07:56 +00:00
# get as many information from PDF as possible
2017-12-30 18:19:40 +00:00
pdf_data = pdf.extract_einsatzausdruck(
os.path.join(TMP_DIR, file_name),
2017-12-30 16:01:13 +00:00
f_id,
)
2017-12-28 19:07:56 +00:00
2017-12-25 12:52:43 +00:00
# create new Einsatzrapport in Lodur
logger.info('Creating Einsatzrapport in Lodur for ' + f_id)
2017-12-31 16:26:33 +00:00
lodur_id = lodur_client.einsatzrapport(
2017-12-25 12:52:43 +00:00
f_id,
2017-12-28 19:07:56 +00:00
pdf_data,
2017-12-25 12:52:43 +00:00
)
# store lodur id in webdav
2017-12-30 18:19:40 +00:00
webdav_client.store_lodur_id(lodur_id, f_id)
2017-12-25 12:52:43 +00:00
2017-12-31 16:26:33 +00:00
# upload Alarmdepesche to Lodur
lodur_client.einsatzrapport_alarmdepesche(
2017-12-25 12:52:43 +00:00
lodur_id,
2017-12-30 18:19:40 +00:00
os.path.join(TMP_DIR, file_name),
2017-12-25 12:52:43 +00:00
)
2017-12-25 11:39:37 +00:00
elif f_type == 'Einsatzprotokoll':
2017-12-25 12:52:43 +00:00
# Einsatz finished - publish on MQTT
2017-12-30 18:19:40 +00:00
mqtt_client.send_message(f_type, f_id)
2017-12-25 12:52:43 +00:00
2017-12-30 18:19:40 +00:00
lodur_id = webdav_client.get_lodur_id(f_id)
2017-12-25 12:52:43 +00:00
if lodur_id:
logger.info('Uploading Einsatzprotokoll to Lodur')
2017-12-31 16:26:33 +00:00
lodur_client.einsatzrapport_alarmdepesche(
2017-12-25 12:52:43 +00:00
lodur_id,
2017-12-30 18:19:40 +00:00
os.path.join(TMP_DIR, file_name),
2017-12-25 12:52:43 +00:00
)
2017-12-30 18:19:40 +00:00
pdf_data = pdf.extract_einsatzprotokoll(
os.path.join(TMP_DIR, file_name),
2017-12-30 16:01:13 +00:00
f_id,
)
# only update when parsing was successfull
if pdf_data:
logger.info('Updating Einsatzrapport with data from PDF - not yet implemented')
else:
logger.info('Updating Einsatzrapport not possible - PDF parsing failed')
2017-12-25 12:52:43 +00:00
else:
logger.error('Cannot process Einsatzprotokoll as there is no Lodur ID')
2017-12-25 11:39:37 +00:00
else:
logger.error('Unknown type: ' + f_type)
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
time.sleep(_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")