From e7b5bffdd36d38fccc5fef49ec6793a6a7e16828 Mon Sep 17 00:00:00 2001 From: JHCD Date: Fri, 31 Jul 2015 19:09:27 +0200 Subject: [PATCH] add data['timestamp'] #72 to make sure, that all Plugins use the same timestamp --- includes/alarmHandler.py | 11 +++++--- includes/helper/timeHandler.py | 29 +++++++++++++++++----- includes/helper/wildcardHandler.py | 2 +- plugins/MySQL/MySQL.py | 6 ++--- plugins/README.md | 3 +++ plugins/eMail/eMail.py | 1 - plugins/httpRequest/httpRequest.py | 1 - plugins/notifyMyAndroid/notifyMyAndroid.py | 2 +- 8 files changed, 38 insertions(+), 17 deletions(-) diff --git a/includes/alarmHandler.py b/includes/alarmHandler.py index fba318d..74eac6b 100644 --- a/includes/alarmHandler.py +++ b/includes/alarmHandler.py @@ -11,7 +11,7 @@ Handler for the filter and plugins at an alarm """ import logging # Global logger -from threading import Thread +import time # timestamp from includes import globals # Global variables @@ -39,6 +39,7 @@ def processAlarmHandler(typ, freq, data): if globals.config.getboolean("BOSWatch","processAlarmAsync") == True: logging.debug("starting processAlarm async") try: + from threading import Thread Thread(target=processAlarm, args=(typ, freq, data)).start() except: logging.error("Error in starting alarm processing async") @@ -70,15 +71,17 @@ def processAlarm(typ, freq, data): """ try: logging.debug("[ ALARM ]") + # timestamp, to make sure, that all plugins use the same time + data['timestamp'] = int(time.time()) # Go to all plugins in pluginList for pluginName, plugin in globals.pluginList.items(): # if enabled use RegEx-filter if globals.config.getint("BOSWatch","useRegExFilter"): from includes import filter - if filter.checkFilters(typ,data,pluginName,freq): + if filter.checkFilters(typ, data, pluginName, freq): logging.debug("call Plugin: %s", pluginName) try: - plugin.run(typ,freq,data) + plugin.run(typ, freq, data) logging.debug("return from: %s", pluginName) except: # call next plugin, if one has thrown an exception @@ -86,7 +89,7 @@ def processAlarm(typ, freq, data): else: # RegEX filter off - call plugin directly logging.debug("call Plugin: %s", pluginName) try: - plugin.run(typ,freq,data) + plugin.run(typ, freq, data) logging.debug("return from: %s", pluginName) except: # call next plugin, if one has thrown an exception diff --git a/includes/helper/timeHandler.py b/includes/helper/timeHandler.py index c030d5b..da60c98 100644 --- a/includes/helper/timeHandler.py +++ b/includes/helper/timeHandler.py @@ -7,6 +7,7 @@ little Helper to get easy the curent date or time for direct use in plugins to save code @author: Bastian Schroll +@author: Jens Herrmann """ import logging @@ -14,39 +15,55 @@ import logging import time -def curtime(format="%d.%m.%Y %H:%M:%S"): +def curtime(format="%d.%m.%Y %H:%M:%S", timestamp=""): """ Returns formated date and/or time see: https://docs.python.org/2/library/time.html#time.strftime @type format: string @param format: Python time Format-String + @type timestamp: floating point number + @param timestamp: time in seconds since the epoch @return: Formated Time and/or Date @exception: Exception if Error in format """ try: - return time.strftime(format) + if timestamp == "": + return time.strftime(format) + else: + return time.strftime(format, time.gmtime(timestamp)) except: logging.warning("error in time-format-string") logging.debug("error in time-format-string", exc_info=True) + +def getDateTime(timestamp=""): + """ + Returns the date and time -def getDate(): + @return: Formated date + """ + return curtime("%d.%m.%Y %H:%M:%S", timestamp) + + +def getDate(timestamp=""): """ Returns the date @return: Formated date """ - return curtime("%d.%m.%Y") + return curtime("%d.%m.%Y", timestamp) -def getTime(): + +def getTime(timestamp=""): """ Returns the time @return: Formated time """ - return curtime("%H:%M:%S") + return curtime("%H:%M:%S", timestamp) + def getTimestamp(): """ diff --git a/includes/helper/wildcardHandler.py b/includes/helper/wildcardHandler.py index d3f85f2..1b45a32 100644 --- a/includes/helper/wildcardHandler.py +++ b/includes/helper/wildcardHandler.py @@ -31,7 +31,7 @@ def replaceWildcards(text, data, lineBrakeAllowed=False): """ try: # replace date and time wildcards - text = text.replace("%TIME%", timeHandler.getTime()).replace("%DATE%", timeHandler.getDate()) + text = text.replace("%TIME%", timeHandler.getTime(data["timestamp"])).replace("%DATE%", timeHandler.getDate(data["timestamp"])) # replace some special chars if lineBrakeAllowed == True: diff --git a/plugins/MySQL/MySQL.py b/plugins/MySQL/MySQL.py index 3188fc4..80ddadc 100644 --- a/plugins/MySQL/MySQL.py +++ b/plugins/MySQL/MySQL.py @@ -84,13 +84,13 @@ def run(typ,freq,data): logging.debug("Insert %s", typ) if typ == "FMS": - cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableFMS")+" (time, fms, status, direction, directionText, tsi, description) VALUES (NOW(),%s,%s,%s,%s,%s,%s)", (data["fms"], data["status"], data["direction"], data["directionText"], data["tsi"], data["description"])) + cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableFMS")+" (time, fms, status, direction, directionText, tsi, description) VALUES (FROM_UNIXTIME(%s),%s,%s,%s,%s,%s,%s)", (data["timestamp"], data["fms"], data["status"], data["direction"], data["directionText"], data["tsi"], data["description"])) elif typ == "ZVEI": - cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableZVEI")+" (time, zvei, description) VALUES (NOW(),%s,%s)", (data["zvei"], data["description"])) + cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableZVEI")+" (time, zvei, description) VALUES (FROM_UNIXTIME(%s),%s,%s)", (data["timestamp"], data["zvei"], data["description"])) elif typ == "POC": - cursor.execute("INSERT INTO "+globals.config.get("MySQL","tablePOC")+" (time, ric, function, functionChar, msg, bitrate, description) VALUES (NOW(),%s,%s,%s,%s,%s,%s)", (data["ric"], data["function"], data["functionChar"], data["msg"], data["bitrate"], data["description"])) + cursor.execute("INSERT INTO "+globals.config.get("MySQL","tablePOC")+" (time, ric, function, functionChar, msg, bitrate, description) VALUES (FROM_UNIXTIME(%s),%s,%s,%s,%s,%s,%s)", (data["timestamp"], data["ric"], data["function"], data["functionChar"], data["msg"], data["bitrate"], data["description"])) else: logging.warning("Invalid Typ: %s", typ) diff --git a/plugins/README.md b/plugins/README.md index 260d028..1066ff7 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -173,6 +173,7 @@ In the data map are the folowing informations: **ZVEI:** - zvei - description +- timestamp **FMS:** - fms @@ -181,6 +182,7 @@ In the data map are the folowing informations: - directionText - tsi - description +- timestamp **POCSAG:** - ric @@ -189,3 +191,4 @@ In the data map are the folowing informations: - msg - bitrate - description +- timestamp \ No newline at end of file diff --git a/plugins/eMail/eMail.py b/plugins/eMail/eMail.py index aadab49..5a42b95 100644 --- a/plugins/eMail/eMail.py +++ b/plugins/eMail/eMail.py @@ -11,7 +11,6 @@ eMail-Plugin to dispatch FMS-, ZVEI- and POCSAG - messages via eMail/SMTP import logging # Global logger -import time import smtplib #for the SMTP client from email.mime.text import MIMEText # Import the email modules we'll need from email.utils import formatdate # need for confirm to RFC2822 standard diff --git a/plugins/httpRequest/httpRequest.py b/plugins/httpRequest/httpRequest.py index cbdb270..a795fd9 100644 --- a/plugins/httpRequest/httpRequest.py +++ b/plugins/httpRequest/httpRequest.py @@ -16,7 +16,6 @@ from urlparse import urlparse #for split the URL into url and path from includes import globals # Global variables -from includes.helper import timeHandler from includes.helper import wildcardHandler from includes.helper import configHandler diff --git a/plugins/notifyMyAndroid/notifyMyAndroid.py b/plugins/notifyMyAndroid/notifyMyAndroid.py index 912865c..031f06b 100644 --- a/plugins/notifyMyAndroid/notifyMyAndroid.py +++ b/plugins/notifyMyAndroid/notifyMyAndroid.py @@ -209,7 +209,7 @@ def run(typ,freq,data): # build event and msg # pyNMA expect strings are not in UTF-8 event = stringConverter.convertToUnicode(data['description']) - msg = timeHandler.curtime() + msg = timeHandler.getDateTime(data['timestamp']) if ("POC" in typ) and (len(data['msg']) > 0): msg += "\n" + data['msg'] msg = stringConverter.convertToUnicode(msg)