mqtt improvements - more data into lodur

This commit is contained in:
Tobias Brunner 2018-01-02 17:49:08 +01:00
parent a663cd084e
commit 1e0e27d08e
6 changed files with 66 additions and 28 deletions

View File

@ -21,8 +21,8 @@ Einsätze correctly into [Lodur](https://www.lodur.ch/lodur.html).
### Version 1
* IMAP IDLE
* MQTT Message Format / Send PDF payload
* Parse Form
* README / Docs
Before version 1 can be tagged, it must have processed at least 5 real
Einsätze!
@ -34,7 +34,6 @@ Einsätze!
### Future versions
* Generalize
* Documentation
* Display PDF on Dashboard
* Send statistics to InfluxDB
* Webapp to see what's going on

View File

@ -6,7 +6,6 @@ import re
import logging
from datetime import datetime
import mechanicalsoup
from webdav import WebDav
class Lodur:
""" Lodur """
@ -95,11 +94,15 @@ class Lodur:
'%H:%M',
)
eins_ereig = pdf_data['einsatz']
bemerkungen = pdf_data['bemerkungen']
wer_ala = pdf_data['melder']
adr = pdf_data['strasse'] + ', ' + pdf_data['plzort']
else:
date = datetime.now()
time = datetime.now()
eins_ereig = 'UNKNOWN'
bemerkungen = 'UNKNOWN'
wer_ala = 'UNKNOWN'
adr = 'UNKNOWN'
# Fill in form data
@ -122,11 +125,12 @@ class Lodur:
'e_ort_1': '306', # 06. Einsatzort: Urdorf 306, Birmensdorf 298
'eins_ereig': eins_ereig, # 07. Ereignis
'adr': adr, # 08. Adresse
'wer_ala': wer_ala, # 10. Wer hat alarmiert
'zh_alarmierung_h': str(time.hour), # 12. Alarmierung
'zh_alarmierung_m': str(time.minute), # 12. Alarmierung
'ang_sit': 'TBD1', # 17. Angetroffene Situation
'mn': 'TBD2', # 19. Massnahmen
'bk': 'TBD3', # 20. Bemerkungen
'bk': bemerkungen, # 20. Bemerkungen
'en_kr_feuwehr': '1', # 21. Einsatzkräfte
'ali_io': '1', # 24. Alarmierung
'kopie_gvz': '1', # 31. Kopie innert 10 Tagen an GVZ
@ -189,9 +193,12 @@ class Lodur:
# Prepare the form data to be submitted
for key, value in lodur_data.items():
# Encode fields so they are sent in correct format
self.logger.debug('Form data: %s = %s', key, value)
if key in ('eins_ereig', 'adr'):
# Encode some of the fields so they are sent in correct format
# Encoding bk causes some troubles - therefore we skip that - but it
# would be good if it would be encoded as it can / will contain f.e.abs
# Umlauts
self.logger.info('Form data: %s = %s', key, value)
if key in ('eins_ereig', 'adr', 'wer_ala'):
self.browser[key] = value.encode('iso-8859-1')
else:
self.browser[key] = value

View File

@ -8,9 +8,9 @@ import paho.mqtt.client as mqtt
class MQTTClient:
""" MQTT Client """
def __init__(self, server, username, password):
def __init__(self, server, username, password, base_topic):
self.logger = logging.getLogger(__name__)
self.logger.info('Connecting to MQTT broker ' + server)
self.logger.info('Connecting to MQTT broker %s', server)
try:
self.mqtt_client = mqtt.Client('pylokid')
@ -19,11 +19,39 @@ class MQTTClient:
self.mqtt_client.connect(server, 8883, 60)
self.mqtt_client.loop_start()
except Exception as err:
self.logger.error('MQTT connection failed - exiting: ' + str(err))
self.logger.error('MQTT connection failed - exiting: %s', str(err))
raise SystemExit(1)
self.logger.info('MQTT connection successfull')
self.base_topic = base_topic
def send_message(self, f_type, f_id):
def send_message(self, f_type, f_id, pdf_data=None, pdf_file=None):
""" Publish a message over MQTT """
self.mqtt_client.publish('pylokid/' + f_type, f_id)
topic = "{0}/{1}/".format(self.base_topic, f_id)
self.logger.info('[%s] Publishing information on MQTT topic %s*', f_id, topic)
if f_type == 'Einsatzausdruck_FW':
try:
self.mqtt_client.publish(topic + 'typ', 'Einsatzauftrag')
self.mqtt_client.publish(topic + 'einsatz', pdf_data['einsatz'])
self.mqtt_client.publish(
topic + 'datumzeit',
pdf_data['datum'] + ' - ' + pdf_data['zeit']
)
self.mqtt_client.publish(topic + 'sondersignal', pdf_data['sondersignal'])
self.mqtt_client.publish(
topic + 'adresse',
pdf_data['strasse'] + ', ' + pdf_data['plzort']
)
self.mqtt_client.publish(topic + 'hinweis', pdf_data['hinweis'])
self.mqtt_client.publish(topic + 'bemerkungen', pdf_data['bemerkungen'])
# 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':
self.mqtt_client.publish(topic + 'typ', 'Einsatzprotokoll')

View File

@ -64,9 +64,9 @@ class PDFHandling:
# sanity check to see if we can correlate the f_id
if f_id == splited[14]:
self.logger.info('[%s] ID found in PDF', f_id)
self.logger.info('[%s] ID matches in PDF', f_id)
else:
self.logger.error('[%s] ID not found in PDF', f_id)
self.logger.error('[%s] ID does not match in PDF', f_id)
return False
# search some well-known words for later positional computation
@ -75,7 +75,7 @@ class PDFHandling:
index_dispo = splited.index('Disponierte Einheiten')
index_hinweis = splited.index('Hinweis')
except IndexError:
self.logger.error('[%s] PDF file doesn\'t look like a Einsatzausdruck', f_id)
self.logger.error('[%s] PDF file does not look like a Einsatzausdruck', f_id)
return False
# get length of bemerkungen field
@ -87,14 +87,15 @@ class PDFHandling:
'auftrag': splited[14],
'datum': splited[15],
'zeit': splited[16],
'melder': self.concatenate_to_multiline_string(splited, 18, 19),
'melder': splited[18] + ' ' + splited[19],
'erfasser': splited[20],
'bemerkungen': self.concatenate_to_multiline_string(
splited,
index_bemerkungen,
index_bemerkungen + 1,
index_bemerkungen + length_bemerkungen
),
).rstrip(),
'einsatz': splited[index_dispo+5],
'sondersignal': splited[index_dispo+6],
'plzort': splited[index_dispo+8].title(),
'strasse': splited[index_dispo+9].title(),
#'objekt': splited[],
@ -109,9 +110,9 @@ class PDFHandling:
# sanity check to see if we can correlate the f_id
if f_id == splited[26]:
self.logger.info('[%s] ID found in PDF', f_id)
self.logger.info('[%s] ID matches in PDF', f_id)
else:
self.logger.error('[%s] ID not found in PDF', f_id)
self.logger.error('[%s] ID does not match in PDF', f_id)
return False
data = {

View File

@ -35,16 +35,16 @@ class WebDav:
# upload with webdav
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)
self.logger.info('[%s] Uploading file to WebDAV "%s"', f_id, remote_upload_dir)
# create directory if not yet there
if not self.loop.run_until_complete(self.webdav.exists(remote_upload_dir)):
self.logger.info('[%s] Creating directory [%s]', f_id, remote_upload_dir)
self.logger.info('[%s] Creating directory "%s"', f_id, remote_upload_dir)
self.loop.run_until_complete(self.webdav.mkdir(remote_upload_dir))
remote_file_path = remote_upload_dir + "/" + file_name
if self.loop.run_until_complete(self.webdav.exists(remote_file_path)):
self.logger.info('[%s] File %s already exists on WebDAV', f_id, file_name)
self.logger.info('[%s] File "%s" already exists on WebDAV', f_id, file_name)
else:
self.loop.run_until_complete(
self.webdav.upload(
@ -52,7 +52,7 @@ class WebDav:
remote_file_path,
)
)
self.logger.info('[%s] File %s uploaded', f_id, file_name)
self.logger.info('[%s] File "%s" uploaded', f_id, file_name)
def einsatz_exists(self, f_id):
""" check if an einsatz is already created """

11
main.py
View File

@ -33,6 +33,7 @@ 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")
@ -78,6 +79,7 @@ def main():
MQTT_SERVER,
MQTT_USER,
MQTT_PASSWORD,
MQTT_BASE_TOPIC,
)
# Initialize PDF Parser
@ -114,15 +116,16 @@ def main():
)
else:
## Here we get the initial Einsatzauftrag - Time to run
# get as many information from PDF as possible
pdf_file = os.path.join(TMP_DIR, file_name)
pdf_data = pdf.extract_einsatzausdruck(
os.path.join(TMP_DIR, file_name),
pdf_file,
f_id,
)
# publish Einsatz on MQTT
# TODO publish more information about the einsatz - coming from the PDF
mqtt_client.send_message(f_type, f_id)
mqtt_client.send_message(f_type, f_id, pdf_data, pdf_file)
# create new Einsatzrapport in Lodur
lodur_client.einsatzrapport(
@ -141,7 +144,7 @@ def main():
elif f_type == 'Einsatzprotokoll':
logger.info('[%s] Processing type %s', f_id, f_type)
# Einsatz finished - publish on MQTT
mqtt_client.send_message(f_type, f_id)
mqtt_client.send_message(f_type, f_id, pdf_data, pdf_file)
lodur_data = webdav_client.get_lodur_data(f_id)
if lodur_data: