From 485af2e007b51a3877a001a4b9239fe365811ad0 Mon Sep 17 00:00:00 2001 From: Schrolli Date: Fri, 22 May 2015 11:44:35 +0200 Subject: [PATCH 01/28] edit loglevel in boswatch.py --- boswatch.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/boswatch.py b/boswatch.py index 2cb2798..7e1082e 100644 --- a/boswatch.py +++ b/boswatch.py @@ -16,7 +16,7 @@ import logging import argparse #for parse the args import ConfigParser #for parse the config file import re #Regex for validation -import os #for script path +import os #for log mkdir import time #timestamp for doublealarm import subprocess @@ -258,7 +258,7 @@ try: fms_id = fms_service+fms_country+fms_location+fms_vehicle+fms_status+fms_direction #build FMS id if re.search("[0-9a-f]{8}[0-9a-f]{1}[01]{1}", fms_id): #if FMS is valid if fms_id == fms_id_old and timestamp < fms_time_old + globals.config.getint("BOSWatch", "fms_double_ignore_time"): #check for double alarm - logging.warning("FMS double alarm: %s within %s second(s)", fms_id_old, timestamp-fms_time_old) + logging.info("FMS double alarm: %s within %s second(s)", fms_id_old, timestamp-fms_time_old) fms_time_old = timestamp #in case of double alarm, fms_double_ignore_time set new else: logging.info("FMS:%s Status:%s Richtung:%s TKI:%s", fms_id[0:8], fms_status, fms_direction, fms_tsi) @@ -281,7 +281,7 @@ try: zvei_id = decoded[7:12] #ZVEI Code if re.search("[0-9F]{5}", zvei_id): #if ZVEI is valid if zvei_id == zvei_id_old and timestamp < zvei_time_old + globals.config.getint("BOSWatch", "zvei_double_ignore_time"): #check for double alarm - logging.warning("ZVEI double alarm: %s within %s second(s)", zvei_id_old, timestamp-zvei_time_old) + logging.info("ZVEI double alarm: %s within %s second(s)", zvei_id_old, timestamp-zvei_time_old) zvei_time_old = timestamp #in case of double alarm, zvei_double_ignore_time set new else: logging.info("5-Ton: %s", zvei_id) @@ -329,7 +329,7 @@ try: if int(poc_id) >= globals.config.getint("BOSWatch", "poc_filter_range_start"): if int(poc_id) <= globals.config.getint("BOSWatch", "poc_filter_range_end"): if poc_id == poc_id_old and timestamp < poc_time_old + globals.config.getint("BOSWatch", "poc_double_ignore_time"): #check for double alarm - logging.warning("POCSAG%s double alarm: %s within %s second(s)", bitrate, poc_id_old, timestamp-poc_time_old) + logging.info("POCSAG%s double alarm: %s within %s second(s)", bitrate, poc_id_old, timestamp-poc_time_old) poc_time_old = timestamp #in case of double alarm, poc_double_ignore_time set new else: logging.info("POCSAG%s: %s %s %s ", bitrate, poc_id, poc_sub, poc_text) @@ -339,9 +339,9 @@ try: poc_id_old = poc_id #save last id poc_time_old = timestamp #save last time else: - logging.warning("POCSAG%s: %s out of filter range (high)", bitrate, poc_id) + logging.info("POCSAG%s: %s out of filter range (high)", bitrate, poc_id) else: - logging.warning("POCSAG%s: %s out of filter range (low)", bitrate, poc_id) + logging.info("POCSAG%s: %s out of filter range (low)", bitrate, poc_id) else: logging.warning("No valid POCSAG%s RIC: %s", bitrate, poc_id) From 8316ce161482ba30183ea54493748d30c0f99947 Mon Sep 17 00:00:00 2001 From: Schrolli Date: Fri, 22 May 2015 11:44:50 +0200 Subject: [PATCH 02/28] edit plugin_test.py --- plugin_test.py | 63 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 22 deletions(-) diff --git a/plugin_test.py b/plugin_test.py index 915aa7f..cda6ed4 100644 --- a/plugin_test.py +++ b/plugin_test.py @@ -1,15 +1,18 @@ #!/usr/bin/python # -*- coding: cp1252 -*- -import time +########################################################################### +# Use this as a simple Plugin Loading Tool to test your own Coded Plugins # +########################################################################### + +import globals # Global variables import pluginloader -import os #for absolute path: os.path.dirname(os.path.abspath(__file__)) - import logging -import globals import ConfigParser #for parse the config file +import os #for log mkdir +import time #timestamp for doublealarm #create new logger logger = logging.getLogger() @@ -34,37 +37,53 @@ logger.addHandler(ch) #exception - error with exception message in log #critical - critical error, program exit +globals.script_path = os.path.dirname(os.path.abspath(__file__)) -#ConfigParser -logging.debug("reading config file") try: + logging.debug("reading config file") globals.config = ConfigParser.ConfigParser() - globals.config.read(globals.script_path+"./config/config.ini") + globals.config.read(globals.script_path+"/config/config.ini") + for key,val in globals.config.items("Plugins"): + logging.debug(" - %s = %s", key, val) except: - logging.exception("cannot read config file") + logging.debug("cannot read config file") +try: + logging.debug("loading plugins") + pluginList = {} + for i in pluginloader.getPlugins(): + plugin = pluginloader.loadPlugin(i) + pluginList[i["name"]] = plugin + logging.debug("loading ready") +except: + logging.exception("cannot load Plugins") + + +# ----- Test Data ----- # +#typ = "FMS" +#data = {"fms":"12345678", "status":"2", "direction":"1", "tsi":"III"} + +typ = "ZVEI" data = {"zvei":"12345"} -#data = {"ric":"1234567", "function":"1", "msg":"Hello World!"} - -logging.debug("Load Plugins...") - -pluginList = {} -for i in pluginloader.getPlugins(): - plugin = pluginloader.loadPlugin(i) - pluginList[i["name"]] = plugin - -logging.debug("All loaded...") +#typ = "POC" +#data = {"ric":"1234567", "function":"1", "msg":"Hello World!, "bitrate":"1200"} while True: try: time.sleep(1) - logging.info(" = = = = = = = = = ") - logging.info("Alarm!") + + print "" + logging.debug("[ ALARM ]") for name, plugin in pluginList.items(): logging.debug("call Plugin: %s", name) - plugin.run("ZVEI","0",data) + plugin.run(typ,"0",data) + logging.debug("[END ALARM]") + + except KeyboardInterrupt: + logging.warning("Keyboard Interrupt") + exit() except: - logging.exception("Cannot Throw Modules") + logging.exception("unknown error") exit() \ No newline at end of file From c296af22b01fbbdd4642f3d4f04a7061f710bd74 Mon Sep 17 00:00:00 2001 From: JHCD Date: Fri, 22 May 2015 14:01:32 +0200 Subject: [PATCH 03/28] small changes --- config/config.template.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/config.template.ini b/config/config.template.ini index 7f31de7..fa0a032 100644 --- a/config/config.template.ini +++ b/config/config.template.ini @@ -16,8 +16,8 @@ poc_filter_range_start = 0000000 poc_filter_range_end = 9999999 -#can take on or off the plugins (0|1) [Plugins] +#can take on or off the plugins (0|1) MySQL = 0 BosMon = 0 httpRequest = 0 @@ -43,7 +43,7 @@ tablePOC = bos_pocsag bosmon_server = 192.168.0.1 bosmon_port = 80 #channel-name of typ "Web-Telegramm" -bosmon_channel = pocsag +bosmon_channel = channel #Use this, when BosMon has restricted access bosmon_user = bosmon_password = From e67a357d8ef34ae937485a54befa29be2688c363 Mon Sep 17 00:00:00 2001 From: JHCD Date: Fri, 22 May 2015 16:44:23 +0200 Subject: [PATCH 04/28] (re-)structure code - use include-files now - move gobals and other stuff to includes - extract code from boswatch.py to include-files bugfix in BosMon-plugin --- boswatch.py | 195 ++------------------ globals.py | 7 - includes/decoder.py | 120 ++++++++++++ includes/{decoder => decoders}/__init__.py | 0 includes/{decoder => decoders}/fms.py | 0 includes/{decoder => decoders}/poc.py | 0 includes/{decoder => decoders}/zvei.py | 0 includes/globals.py | 20 ++ includes/pluginHandler.py | 26 +++ pluginloader.py => includes/pluginloader.py | 3 +- includes/shellHeader.py | 32 ++++ plugins/BosMon/BosMon.py | 12 +- plugins/MySQL/MySQL.py | 5 +- plugins/httpRequest/httpRequest.py | 9 +- plugins/template/template.py | 10 +- 15 files changed, 243 insertions(+), 196 deletions(-) mode change 100644 => 100755 boswatch.py delete mode 100644 globals.py create mode 100644 includes/decoder.py rename includes/{decoder => decoders}/__init__.py (100%) rename includes/{decoder => decoders}/fms.py (100%) rename includes/{decoder => decoders}/poc.py (100%) rename includes/{decoder => decoders}/zvei.py (100%) create mode 100644 includes/globals.py create mode 100644 includes/pluginHandler.py rename pluginloader.py => includes/pluginloader.py (95%) create mode 100644 includes/shellHeader.py diff --git a/boswatch.py b/boswatch.py old mode 100644 new mode 100755 index 7e1082e..c529c2a --- a/boswatch.py +++ b/boswatch.py @@ -8,26 +8,16 @@ # For more Information see the README.md ##### Info ##### -import globals # Global variables -import pluginloader - import logging import argparse #for parse the args import ConfigParser #for parse the config file -import re #Regex for validation import os #for log mkdir -import time #timestamp for doublealarm +import time #timestamp import subprocess +from includes import globals # Global variables -def throwAlarm(typ,data): - logging.debug("[ ALARM ]") - for name, plugin in pluginList.items(): - logging.debug("call Plugin: %s", name) - plugin.run(typ,"0",data) - logging.debug("[END ALARM]") - # Programm try: try: @@ -35,6 +25,10 @@ try: #create logger globals.script_path = os.path.dirname(os.path.abspath(__file__)) + #define include-path + globals.include_path = os.path.join(globals.script_path, "includes") + + if not os.path.exists(globals.script_path+"/log/"): os.mkdir(globals.script_path+"/log/") @@ -128,54 +122,12 @@ try: ch.setLevel(logging.CRITICAL) if not args.quiet: #only if not quiet mode - print " ____ ____ ______ __ __ __ " - print " / __ )/ __ \/ ___/ | / /___ _/ /______/ /_ b" - print " / __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ e" - print " / /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / t" - print " /_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ a" - print " German BOS Information Script " - print " by Bastian Schroll " - print "" - - print "Frequency: "+args.freq - print "Device-ID: "+str(args.device) - print "Error in PPM: "+str(args.error) - print "Active Demods: "+str(len(args.demod)) - if "FMS" in args.demod: - print "- FMS" - if "ZVEI" in args.demod: - print "- ZVEI" - if "POC512" in args.demod: - print "- POC512" - if "POC1200" in args.demod: - print "- POC1200" - if "POC2400" in args.demod: - print "- POC2400" - print "Squelch: "+str(args.squelch) - if args.verbose: - print "Verbose Mode!" - print "" + from includes import shellHeader + shellHeader.printHeader(args) except: logging.exception("cannot display/log args") - try: - - #preload vars - logging.debug("pre-load variables") - fms_id = 0 - fms_id_old = 0 - fms_time_old = 0 - - zvei_id = 0 - zvei_id_old = 0 - zvei_time_old = 0 - - poc_id = 0 - poc_id_old = 0 - poc_time_old = 0 - - except: - logging.debug("cannot pre-load variables") + try: #read config @@ -188,17 +140,11 @@ try: except: logging.debug("cannot read config file") else: - try: - #load plugins - logging.debug("loading plugins") - pluginList = {} - for i in pluginloader.getPlugins(): - plugin = pluginloader.loadPlugin(i) - pluginList[i["name"]] = plugin - - except: - logging.exception("cannot load Plugins") + #load plugins + from includes import pluginHandler + pluginHandler.loadPlugins() + try: #start rtl_fm @@ -213,7 +159,6 @@ try: logging.exception("cannot start rtl_fm") else: try: - #start multimon logging.debug("starting multimon-ng") multimon_ng = subprocess.Popen("multimon-ng "+str(demodulation)+" -f alpha -t raw /dev/stdin - ", @@ -227,123 +172,21 @@ try: else: logging.debug("start decoding") + while True: #RAW Data from Multimon-NG #ZVEI2: 25832 #FMS: 43f314170000 (9=Rotkreuz 3=Bayern 1 Ort 0x25=037FZG 7141Status 3=Einsatz Ab 0=FZG->LST2=III(mit NA,ohneSIGNAL)) CRC correct\n' - decoded = str(multimon_ng.stdout.readline()) #Get line data from multimon stdout + #decoded = str(multimon_ng.stdout.readline()) #Get line data from multimon stdout #only for develop #decoded = "ZVEI2: 25832" #decoded = "FMS: 43f314170000 (9=Rotkreuz 3=Bayern 1 Ort 0x25=037FZG 7141Status 3=Einsatz Ab 0=FZG->LST 2=III(mit NA,ohneSIGNAL)) CRC correct\n'" - #decoded = "POCSAG1200: Address: 1234567 Function: 1 Alpha: XXMSG MEfeweffsjh" - #time.sleep(1) + decoded = "POCSAG1200: Address: 1234567 Function: 1 Alpha: Hello World" + time.sleep(1) - timestamp = int(time.time())#Get Timestamp - - #FMS Decoder Section - #check FMS: -> check CRC -> validate -> check double alarm -> log - if "FMS:" in decoded: - logging.debug("recieved FMS") - - fms_service = decoded[19] #Organisation - fms_country = decoded[36] #Bundesland - fms_location = decoded[65:67] #Ort - fms_vehicle = decoded[72:76] #Fahrzeug - fms_status = decoded[84] #Status - fms_direction = decoded[101] #Richtung - fms_tsi = decoded[114:117] #Taktische Kruzinformation - - if "CRC correct" in decoded: #check CRC is correct - fms_id = fms_service+fms_country+fms_location+fms_vehicle+fms_status+fms_direction #build FMS id - if re.search("[0-9a-f]{8}[0-9a-f]{1}[01]{1}", fms_id): #if FMS is valid - if fms_id == fms_id_old and timestamp < fms_time_old + globals.config.getint("BOSWatch", "fms_double_ignore_time"): #check for double alarm - logging.info("FMS double alarm: %s within %s second(s)", fms_id_old, timestamp-fms_time_old) - fms_time_old = timestamp #in case of double alarm, fms_double_ignore_time set new - else: - logging.info("FMS:%s Status:%s Richtung:%s TKI:%s", fms_id[0:8], fms_status, fms_direction, fms_tsi) - data = {"fms":fms_id[0:8], "status":fms_status, "direction":fms_direction, "tsi":fms_tsi} - throwAlarm("FMS",data) - - fms_id_old = fms_id #save last id - fms_time_old = timestamp #save last time - else: - logging.warning("No valid FMS: %s", fms_id) - else: - logging.warning("FMS CRC incorrect") - - - #ZVEI Decoder Section - #check ZVEI: -> validate -> check double alarm -> log - if "ZVEI2:" in decoded: - logging.debug("recieved ZVEI") - - zvei_id = decoded[7:12] #ZVEI Code - if re.search("[0-9F]{5}", zvei_id): #if ZVEI is valid - if zvei_id == zvei_id_old and timestamp < zvei_time_old + globals.config.getint("BOSWatch", "zvei_double_ignore_time"): #check for double alarm - logging.info("ZVEI double alarm: %s within %s second(s)", zvei_id_old, timestamp-zvei_time_old) - zvei_time_old = timestamp #in case of double alarm, zvei_double_ignore_time set new - else: - logging.info("5-Ton: %s", zvei_id) - data = {"zvei":zvei_id} - throwAlarm("ZVEI",data) - - zvei_id_old = zvei_id #save last id - zvei_time_old = timestamp #save last time - else: - logging.warning("No valid ZVEI: %s", zvei_id) - - - #POCSAG Decoder Section - #check POCSAG -> validate -> check double alarm -> log - if "POCSAG" in decoded: - logging.debug("recieved POCSAG") - bitrate = 0 - - if "POCSAG512:" in decoded: - bitrate = 512 - poc_id = decoded[20:27] - poc_sub = decoded[39].replace("3", "4").replace("2", "3").replace("1", "2").replace("0", "1") - - elif "POCSAG1200:" in decoded: - bitrate = 1200 - poc_id = decoded[21:28] - poc_sub = decoded[40].replace("3", "4").replace("2", "3").replace("1", "2").replace("0", "1") - - elif "POCSAG2400:" in decoded: - bitrate = 2400 - poc_id = decoded[21:28] - poc_sub = decoded[40].replace("3", "4").replace("2", "3").replace("1", "2").replace("0", "1") - - if bitrate is 0: - logging.warning("POCSAG Bitrate not found") - else: - logging.debug("POCSAG Bitrate: %s", bitrate) - - if "Alpha:" in decoded: #check if there is a text message - poc_text = decoded.split('Alpha: ')[1].strip().rstrip('').strip() - else: - poc_text = "" - - if re.search("[0-9]{7}", poc_id): #if POC is valid - if int(poc_id) >= globals.config.getint("BOSWatch", "poc_filter_range_start"): - if int(poc_id) <= globals.config.getint("BOSWatch", "poc_filter_range_end"): - if poc_id == poc_id_old and timestamp < poc_time_old + globals.config.getint("BOSWatch", "poc_double_ignore_time"): #check for double alarm - logging.info("POCSAG%s double alarm: %s within %s second(s)", bitrate, poc_id_old, timestamp-poc_time_old) - poc_time_old = timestamp #in case of double alarm, poc_double_ignore_time set new - else: - logging.info("POCSAG%s: %s %s %s ", bitrate, poc_id, poc_sub, poc_text) - data = {"ric":poc_id, "function":poc_sub, "msg":poc_text, "bitrate":bitrate} - throwAlarm("POC",data) - - poc_id_old = poc_id #save last id - poc_time_old = timestamp #save last time - else: - logging.info("POCSAG%s: %s out of filter range (high)", bitrate, poc_id) - else: - logging.info("POCSAG%s: %s out of filter range (low)", bitrate, poc_id) - else: - logging.warning("No valid POCSAG%s RIC: %s", bitrate, poc_id) + from includes import decoder + decoder.decode(decoded) except KeyboardInterrupt: logging.warning("Keyboard Interrupt") diff --git a/globals.py b/globals.py deleted file mode 100644 index 1de8ac4..0000000 --- a/globals.py +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/python -# -*- coding: cp1252 -*- - - -#Global variables -config = 0 -script_path = "" diff --git a/includes/decoder.py b/includes/decoder.py new file mode 100644 index 0000000..4dd3007 --- /dev/null +++ b/includes/decoder.py @@ -0,0 +1,120 @@ +#!/usr/bin/python +# -*- coding: cp1252 -*- + +import logging +import time #timestamp for doublealarm +import re #Regex for validation + +from includes import globals # Global variables +from includes import pluginloader + + +def decode(decoded): + timestamp = int(time.time())#Get Timestamp + + #FMS Decoder Section + #check FMS: -> check CRC -> validate -> check double alarm -> log + if "FMS:" in decoded: + logging.debug("recieved FMS") + + fms_service = decoded[19] #Organisation + fms_country = decoded[36] #Bundesland + fms_location = decoded[65:67] #Ort + fms_vehicle = decoded[72:76] #Fahrzeug + fms_status = decoded[84] #Status + fms_direction = decoded[101] #Richtung + fms_tsi = decoded[114:117] #Taktische Kruzinformation + + if "CRC correct" in decoded: #check CRC is correct + fms_id = fms_service+fms_country+fms_location+fms_vehicle+fms_status+fms_direction #build FMS id + if re.search("[0-9a-f]{8}[0-9a-f]{1}[01]{1}", fms_id): #if FMS is valid + if fms_id == globals.fms_id_old and timestamp < globals.fms_time_old + globals.config.getint("BOSWatch", "fms_double_ignore_time"): #check for double alarm + logging.info("FMS double alarm: %s within %s second(s)", globals.fms_id_old, timestamp-globals.fms_time_old) + globals.fms_time_old = timestamp #in case of double alarm, fms_double_ignore_time set new + else: + logging.info("FMS:%s Status:%s Richtung:%s TKI:%s", fms_id[0:8], fms_status, fms_direction, fms_tsi) + data = {"fms":fms_id[0:8], "status":fms_status, "direction":fms_direction, "tsi":fms_tsi} + from includes import pluginHandler + pluginHandler.throwAlarm("FMS",data) + + globals.fms_id_old = fms_id #save last id + globals.fms_time_old = timestamp #save last time + else: + logging.warning("No valid FMS: %s", fms_id) + else: + logging.warning("FMS CRC incorrect") + + + #ZVEI Decoder Section + #check ZVEI: -> validate -> check double alarm -> log + if "ZVEI2:" in decoded: + logging.debug("recieved ZVEI") + + zvei_id = decoded[7:12] #ZVEI Code + if re.search("[0-9F]{5}", zvei_id): #if ZVEI is valid + if zvei_id == globals.zvei_id_old and timestamp < globals.zvei_time_old + globals.config.getint("BOSWatch", "zvei_double_ignore_time"): #check for double alarm + logging.info("ZVEI double alarm: %s within %s second(s)", globals.zvei_id_old, timestamp-globals.zvei_time_old) + globals.zvei_time_old = timestamp #in case of double alarm, zvei_double_ignore_time set new + else: + logging.info("5-Ton: %s", zvei_id) + data = {"zvei":zvei_id} + from includes import pluginHandler + pluginHandler.throwAlarm("ZVEI",data) + + globals.zvei_id_old = zvei_id #save last id + globals.zvei_time_old = timestamp #save last time + else: + logging.warning("No valid ZVEI: %s", zvei_id) + + + #POCSAG Decoder Section + #check POCSAG -> validate -> check double alarm -> log + if "POCSAG" in decoded: + logging.debug("recieved POCSAG") + bitrate = 0 + + if "POCSAG512:" in decoded: + bitrate = 512 + poc_id = decoded[20:27] + poc_sub = decoded[39].replace("3", "4").replace("2", "3").replace("1", "2").replace("0", "1") + + elif "POCSAG1200:" in decoded: + bitrate = 1200 + poc_id = decoded[21:28] + poc_sub = decoded[40].replace("3", "4").replace("2", "3").replace("1", "2").replace("0", "1") + + elif "POCSAG2400:" in decoded: + bitrate = 2400 + poc_id = decoded[21:28] + poc_sub = decoded[40].replace("3", "4").replace("2", "3").replace("1", "2").replace("0", "1") + + if bitrate is 0: + logging.warning("POCSAG Bitrate not found") + else: + logging.debug("POCSAG Bitrate: %s", bitrate) + + if "Alpha:" in decoded: #check if there is a text message + poc_text = decoded.split('Alpha: ')[1].strip().rstrip('').strip() + else: + poc_text = "" + + if re.search("[0-9]{7}", poc_id): #if POC is valid + if int(poc_id) >= globals.config.getint("BOSWatch", "poc_filter_range_start"): + if int(poc_id) <= globals.config.getint("BOSWatch", "poc_filter_range_end"): + if poc_id == globals.poc_id_old and timestamp < globals.poc_time_old + globals.config.getint("BOSWatch", "poc_double_ignore_time"): #check for double alarm + logging.info("POCSAG%s double alarm: %s within %s second(s)", bitrate, globals.poc_id_old, timestamp-globals.poc_time_old) + globals.poc_time_old = timestamp #in case of double alarm, poc_double_ignore_time set new + else: + logging.info("POCSAG%s: %s %s %s ", bitrate, poc_id, poc_sub, poc_text) + data = {"ric":poc_id, "function":poc_sub, "msg":poc_text, "bitrate":bitrate} + from includes import pluginHandler + pluginHandler.throwAlarm("POC",data) + + globals.poc_id_old = poc_id #save last id + globals.poc_time_old = timestamp #save last time + else: + logging.info("POCSAG%s: %s out of filter range (high)", bitrate, poc_id) + else: + logging.info("POCSAG%s: %s out of filter range (low)", bitrate, poc_id) + else: + logging.warning("No valid POCSAG%s RIC: %s", bitrate, poc_id) \ No newline at end of file diff --git a/includes/decoder/__init__.py b/includes/decoders/__init__.py similarity index 100% rename from includes/decoder/__init__.py rename to includes/decoders/__init__.py diff --git a/includes/decoder/fms.py b/includes/decoders/fms.py similarity index 100% rename from includes/decoder/fms.py rename to includes/decoders/fms.py diff --git a/includes/decoder/poc.py b/includes/decoders/poc.py similarity index 100% rename from includes/decoder/poc.py rename to includes/decoders/poc.py diff --git a/includes/decoder/zvei.py b/includes/decoders/zvei.py similarity index 100% rename from includes/decoder/zvei.py rename to includes/decoders/zvei.py diff --git a/includes/globals.py b/includes/globals.py new file mode 100644 index 0000000..d81fde7 --- /dev/null +++ b/includes/globals.py @@ -0,0 +1,20 @@ +#!/usr/bin/python +# -*- coding: cp1252 -*- + +#Global variables +config = 0 +script_path = "" +include_path = "" + +#double alarm +fms_id_old = 0 +fms_time_old = 0 + +zvei_id_old = 0 +zvei_time_old = 0 + +poc_id_old = 0 +poc_time_old = 0 + +#pluginHandler +pluginList = {} \ No newline at end of file diff --git a/includes/pluginHandler.py b/includes/pluginHandler.py new file mode 100644 index 0000000..93d1872 --- /dev/null +++ b/includes/pluginHandler.py @@ -0,0 +1,26 @@ +#!/usr/bin/python +# -*- coding: cp1252 -*- + +import logging + +from includes import globals # Global variables +from includes import pluginloader + +def throwAlarm(typ,data): + logging.debug("[ ALARM ]") + for name, plugin in globals.pluginList.items(): + logging.debug("call Plugin: %s", name) + plugin.run(typ,"0",data) + logging.debug("[END ALARM]") + + +def loadPlugins(): + try: + #load plugins + logging.debug("loading plugins") + for i in pluginloader.getPlugins(): + plugin = pluginloader.loadPlugin(i) + globals.pluginList[i["name"]] = plugin + + except: + logging.exception("cannot load Plugins") \ No newline at end of file diff --git a/pluginloader.py b/includes/pluginloader.py similarity index 95% rename from pluginloader.py rename to includes/pluginloader.py index d3efc6b..25c66ad 100644 --- a/pluginloader.py +++ b/includes/pluginloader.py @@ -2,10 +2,11 @@ # -*- coding: cp1252 -*- import logging # Global logger -import globals # Global variables import imp import os +from includes import globals # Global variables + def getPlugins(): try: diff --git a/includes/shellHeader.py b/includes/shellHeader.py new file mode 100644 index 0000000..4387a6f --- /dev/null +++ b/includes/shellHeader.py @@ -0,0 +1,32 @@ +#!/usr/bin/python +# -*- coding: cp1252 -*- + + +def printHeader(args): + print " ____ ____ ______ __ __ __ " + print " / __ )/ __ \/ ___/ | / /___ _/ /______/ /_ b" + print " / __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ e" + print " / /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / t" + print " /_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ a" + print " German BOS Information Script " + print " by Bastian Schroll " + print "" + + print "Frequency: "+args.freq + print "Device-ID: "+str(args.device) + print "Error in PPM: "+str(args.error) + print "Active Demods: "+str(len(args.demod)) + if "FMS" in args.demod: + print "- FMS" + if "ZVEI" in args.demod: + print "- ZVEI" + if "POC512" in args.demod: + print "- POC512" + if "POC1200" in args.demod: + print "- POC1200" + if "POC2400" in args.demod: + print "- POC2400" + print "Squelch: "+str(args.squelch) + if args.verbose: + print "Verbose Mode!" + print "" \ No newline at end of file diff --git a/plugins/BosMon/BosMon.py b/plugins/BosMon/BosMon.py index 3c37435..bf722f0 100644 --- a/plugins/BosMon/BosMon.py +++ b/plugins/BosMon/BosMon.py @@ -2,12 +2,14 @@ # -*- coding: cp1252 -*- import logging # Global logger -import globals # Global variables import httplib #for the HTTP request import urllib #for the HTTP request with parameters import base64 #for the HTTP request with User/Password +from includes import globals # Global variables + + def run(typ,freq,data): try: #ConfigParser @@ -38,9 +40,13 @@ def run(typ,freq,data): headers['Content-type'] = "application/x-www-form-urlencoded" headers['Accept'] = "text/plain" if globals.config.get("BosMon", "bosmon_user"): + logging.debug(" Gesicherte Verbindung ") headers['Authorization'] = "Basic {0}".format(base64.b64encode("{0}:{1}".format(globals.config.get("BosMon", "bosmon_user"), globals.config.get("BosMon", "bosmon_password")))) + logging.debug(" Open HTTPConnection ") httprequest = httplib.HTTPConnection(globals.config.get("BosMon", "bosmon_server"), globals.config.get("BosMon", "bosmon_port")) - httprequest.request("POST", "/telegramin/"+bosmon_channel+"/input.xml", params, headers) + logging.debug(" Start Request ") + httprequest.request("POST", "/telegramin/"+globals.config.get("BosMon", "bosmon_channel")+"/input.xml", params, headers) + logging.debug(" Get Response: ") httpresponse = httprequest.getresponse() if str(httpresponse.status) == "200": #Check HTTP Response an print a Log or Error logging.debug("BosMon response: %s - %s", str(httpresponse.status), str(httpresponse.reason)) @@ -53,4 +59,4 @@ def run(typ,freq,data): ########## User Plugin CODE ########## except: - logging.exception("") + logging.exception("") \ No newline at end of file diff --git a/plugins/MySQL/MySQL.py b/plugins/MySQL/MySQL.py index 621b401..bbe855f 100644 --- a/plugins/MySQL/MySQL.py +++ b/plugins/MySQL/MySQL.py @@ -2,10 +2,13 @@ # -*- coding: cp1252 -*- import logging # Global logger -import globals # Global variables + import mysql import mysql.connector +from includes import globals # Global variables + + def run(typ,freq,data): try: #ConfigParser diff --git a/plugins/httpRequest/httpRequest.py b/plugins/httpRequest/httpRequest.py index c0655ed..c0614c7 100644 --- a/plugins/httpRequest/httpRequest.py +++ b/plugins/httpRequest/httpRequest.py @@ -1,9 +1,6 @@ #!/usr/bin/python # -*- coding: cp1252 -*- -import logging # Global logger -import globals # Global variables - ######### # USAGE # @@ -23,8 +20,12 @@ import globals # Global variables # usable Loglevels debug|info|warning|error|exception|critical # if you use .exception in Try:Exception: Construct, it logs the Python EX.message too +import logging # Global logger import httplib #for the HTTP request +from includes import globals # Global variables + + def run(typ,freq,data): try: #ConfigParser @@ -68,4 +69,4 @@ def run(typ,freq,data): ########## User Plugin CODE ########## except: - logging.exception("unknown error") + logging.exception("unknown error") \ No newline at end of file diff --git a/plugins/template/template.py b/plugins/template/template.py index 5a899fe..5e99e1a 100644 --- a/plugins/template/template.py +++ b/plugins/template/template.py @@ -1,9 +1,6 @@ #!/usr/bin/python # -*- coding: cp1252 -*- -import logging # Global logger -import globals # Global variables - ######### # USAGE # @@ -23,6 +20,11 @@ import globals # Global variables # usable Loglevels debug|info|warning|error|exception|critical # if you use .exception in Try:Exception: Construct, it logs the Python EX.message too +import logging # Global logger + +from includes import globals # Global variables + + def run(typ,freq,data): try: #ConfigParser @@ -45,4 +47,4 @@ def run(typ,freq,data): ########## User Plugin CODE ########## except: - logging.exception("unknown error") + logging.exception("unknown error") \ No newline at end of file From eadc4e2ccb474d6216196e378d149666e774078b Mon Sep 17 00:00:00 2001 From: JHCD Date: Fri, 22 May 2015 16:53:05 +0200 Subject: [PATCH 05/28] change test to prod mode... delete unused include-path variable --- boswatch.py | 10 +++------- includes/globals.py | 1 - 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/boswatch.py b/boswatch.py index c529c2a..26ae303 100755 --- a/boswatch.py +++ b/boswatch.py @@ -25,10 +25,6 @@ try: #create logger globals.script_path = os.path.dirname(os.path.abspath(__file__)) - #define include-path - globals.include_path = os.path.join(globals.script_path, "includes") - - if not os.path.exists(globals.script_path+"/log/"): os.mkdir(globals.script_path+"/log/") @@ -177,13 +173,13 @@ try: #RAW Data from Multimon-NG #ZVEI2: 25832 #FMS: 43f314170000 (9=Rotkreuz 3=Bayern 1 Ort 0x25=037FZG 7141Status 3=Einsatz Ab 0=FZG->LST2=III(mit NA,ohneSIGNAL)) CRC correct\n' - #decoded = str(multimon_ng.stdout.readline()) #Get line data from multimon stdout + decoded = str(multimon_ng.stdout.readline()) #Get line data from multimon stdout #only for develop #decoded = "ZVEI2: 25832" #decoded = "FMS: 43f314170000 (9=Rotkreuz 3=Bayern 1 Ort 0x25=037FZG 7141Status 3=Einsatz Ab 0=FZG->LST 2=III(mit NA,ohneSIGNAL)) CRC correct\n'" - decoded = "POCSAG1200: Address: 1234567 Function: 1 Alpha: Hello World" - time.sleep(1) + #decoded = "POCSAG1200: Address: 1234567 Function: 1 Alpha: Hello World" + #time.sleep(1) from includes import decoder decoder.decode(decoded) diff --git a/includes/globals.py b/includes/globals.py index d81fde7..14fb308 100644 --- a/includes/globals.py +++ b/includes/globals.py @@ -4,7 +4,6 @@ #Global variables config = 0 script_path = "" -include_path = "" #double alarm fms_id_old = 0 From edb4db348a6f95f056c5c68d9be3af3d0b39494b Mon Sep 17 00:00:00 2001 From: JHCD Date: Fri, 22 May 2015 17:44:34 +0200 Subject: [PATCH 06/28] path-thrugh frequenz --- boswatch.py | 2 +- includes/decoder.py | 8 ++++---- includes/pluginHandler.py | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/boswatch.py b/boswatch.py index 26ae303..3c3b284 100755 --- a/boswatch.py +++ b/boswatch.py @@ -182,7 +182,7 @@ try: #time.sleep(1) from includes import decoder - decoder.decode(decoded) + decoder.decode(args.freq, decoded) except KeyboardInterrupt: logging.warning("Keyboard Interrupt") diff --git a/includes/decoder.py b/includes/decoder.py index 4dd3007..5108fa9 100644 --- a/includes/decoder.py +++ b/includes/decoder.py @@ -9,7 +9,7 @@ from includes import globals # Global variables from includes import pluginloader -def decode(decoded): +def decode(freq, decoded): timestamp = int(time.time())#Get Timestamp #FMS Decoder Section @@ -35,7 +35,7 @@ def decode(decoded): logging.info("FMS:%s Status:%s Richtung:%s TKI:%s", fms_id[0:8], fms_status, fms_direction, fms_tsi) data = {"fms":fms_id[0:8], "status":fms_status, "direction":fms_direction, "tsi":fms_tsi} from includes import pluginHandler - pluginHandler.throwAlarm("FMS",data) + pluginHandler.throwAlarm("FMS",freq,data) globals.fms_id_old = fms_id #save last id globals.fms_time_old = timestamp #save last time @@ -59,7 +59,7 @@ def decode(decoded): logging.info("5-Ton: %s", zvei_id) data = {"zvei":zvei_id} from includes import pluginHandler - pluginHandler.throwAlarm("ZVEI",data) + pluginHandler.throwAlarm("ZVEI",freq,data) globals.zvei_id_old = zvei_id #save last id globals.zvei_time_old = timestamp #save last time @@ -108,7 +108,7 @@ def decode(decoded): logging.info("POCSAG%s: %s %s %s ", bitrate, poc_id, poc_sub, poc_text) data = {"ric":poc_id, "function":poc_sub, "msg":poc_text, "bitrate":bitrate} from includes import pluginHandler - pluginHandler.throwAlarm("POC",data) + pluginHandler.throwAlarm("POC",freq,data) globals.poc_id_old = poc_id #save last id globals.poc_time_old = timestamp #save last time diff --git a/includes/pluginHandler.py b/includes/pluginHandler.py index 93d1872..eadbd08 100644 --- a/includes/pluginHandler.py +++ b/includes/pluginHandler.py @@ -6,11 +6,11 @@ import logging from includes import globals # Global variables from includes import pluginloader -def throwAlarm(typ,data): +def throwAlarm(typ,freq,data): logging.debug("[ ALARM ]") for name, plugin in globals.pluginList.items(): logging.debug("call Plugin: %s", name) - plugin.run(typ,"0",data) + plugin.run(typ,freq,data) logging.debug("[END ALARM]") From 906c32b68320935f629beb777e0cf161648a73f2 Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Fri, 22 May 2015 18:22:29 +0200 Subject: [PATCH 07/28] change empty lines --- boswatch.py | 45 ++++++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/boswatch.py b/boswatch.py index 3c3b284..e94a597 100755 --- a/boswatch.py +++ b/boswatch.py @@ -20,8 +20,7 @@ from includes import globals # Global variables # Programm try: - try: - + try: #create logger globals.script_path = os.path.dirname(os.path.abspath(__file__)) @@ -42,13 +41,12 @@ try: ch = logging.StreamHandler() ch.setLevel(logging.INFO) #log level >= info ch.setFormatter(formatter) - logger.addHandler(ch) - + logger.addHandler(ch) except: logging.exception("cannot create logger") else: - try: + try: #clear log bos_log = open(globals.script_path+"/log/boswatch.log", "w") rtl_log = open(globals.script_path+"/log/rtl_fm.log", "w") @@ -60,12 +58,11 @@ try: rtl_log.close() mon_log.close() logging.debug("BOSWatch has started") - logging.debug("Logfiles cleared") - + logging.debug("Logfiles cleared") except: logging.exception("cannot clear Logfiles") - try: - + + try: #parse args logging.debug("parse args") #With -h or --help you get the Args help @@ -79,13 +76,12 @@ try: parser.add_argument("-s", "--squelch", help="Level of Squelch", type=int, default=0) parser.add_argument("-v", "--verbose", help="Shows more Information", action="store_true") parser.add_argument("-q", "--quiet", help="Shows no Information. Only Logfiles", action="store_true") - args = parser.parse_args() - + args = parser.parse_args() except: - logging.error("cannot parse args") - else: - try: + logging.error("cannot parse args") + else: + try: #display/log args logging.debug(" - Frequency: %s", args.freq) logging.debug(" - Device: %s", args.device) @@ -119,20 +115,17 @@ try: if not args.quiet: #only if not quiet mode from includes import shellHeader - shellHeader.printHeader(args) - + shellHeader.printHeader(args) except: logging.exception("cannot display/log args") - try: - + try: #read config logging.debug("reading config file") globals.config = ConfigParser.ConfigParser() globals.config.read(globals.script_path+"/config/config.ini") for key,val in globals.config.items("BOSWatch"): - logging.debug(" - %s = %s", key, val) - + logging.debug(" - %s = %s", key, val) except: logging.debug("cannot read config file") else: @@ -140,20 +133,19 @@ try: #load plugins from includes import pluginHandler pluginHandler.loadPlugins() - - try: + try: #start rtl_fm logging.debug("starting rtl_fm") rtl_fm = subprocess.Popen("rtl_fm -d "+str(args.device)+" -f "+str(args.freq)+" -M fm -s 22050 -p "+str(args.error)+" -E DC -F 0 -l "+str(args.squelch)+" -g 100", #stdin=rtl_fm.stdout, stdout=subprocess.PIPE, stderr=open(globals.script_path+"/log/rtl_fm.log","a"), - shell=True) - + shell=True) except: logging.exception("cannot start rtl_fm") - else: + else: + try: #start multimon logging.debug("starting multimon-ng") @@ -161,8 +153,7 @@ try: stdin=rtl_fm.stdout, stdout=subprocess.PIPE, stderr=open(globals.script_path+"/log/multimon.log","a"), - shell=True) - + shell=True) except: logging.exception("cannot start multimon-ng") else: From a4da59d0c258478f7b4a6bdab8eefa18073667dd Mon Sep 17 00:00:00 2001 From: JHCD Date: Fri, 22 May 2015 20:07:09 +0200 Subject: [PATCH 08/28] restructure BosMon-plugin --- plugins/BosMon/BosMon.py | 80 ++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/plugins/BosMon/BosMon.py b/plugins/BosMon/BosMon.py index bf722f0..2c67f8f 100644 --- a/plugins/BosMon/BosMon.py +++ b/plugins/BosMon/BosMon.py @@ -10,6 +10,15 @@ import base64 #for the HTTP request with User/Password from includes import globals # Global variables +def bosMonRequest(httprequest, params, headers): + httprequest.request("POST", "/telegramin/"+globals.config.get("BosMon", "bosmon_channel")+"/input.xml", params, headers) + httpresponse = httprequest.getresponse() + if str(httpresponse.status) == "200": #Check HTTP Response an print a Log or Error + logging.debug("BosMon response: %s - %s", str(httpresponse.status), str(httpresponse.reason)) + else: + logging.warning("BosMon response: %s - %s", str(httpresponse.status), str(httpresponse.reason)) + + def run(typ,freq,data): try: #ConfigParser @@ -20,43 +29,42 @@ def run(typ,freq,data): except: logging.exception("cannot read config file") - ########## User Plugin CODE ########## - if typ == "FMS": - logging.warning("%s not supported", typ) - elif typ == "ZVEI": - logging.warning("%s not supported", typ) - elif typ == "POC": - logging.debug("Start POC to BosMon") - try: - #Defined data structure: - # data["ric"] - # data["function"] - # data["msg"] - #BosMon-Telegramin expected "a-d" as RIC-sub/function - data["function"] = data["function"].replace("1", "a").replace("2", "b").replace("3", "c").replace("4", "d") - params = urllib.urlencode({'type':'pocsag', 'address':data["ric"], 'flags':'0', 'function':data["function"], 'message':data["msg"]}) - logging.debug(" - Params: %s", params) - headers = {} - headers['Content-type'] = "application/x-www-form-urlencoded" - headers['Accept'] = "text/plain" - if globals.config.get("BosMon", "bosmon_user"): - logging.debug(" Gesicherte Verbindung ") - headers['Authorization'] = "Basic {0}".format(base64.b64encode("{0}:{1}".format(globals.config.get("BosMon", "bosmon_user"), globals.config.get("BosMon", "bosmon_password")))) - logging.debug(" Open HTTPConnection ") - httprequest = httplib.HTTPConnection(globals.config.get("BosMon", "bosmon_server"), globals.config.get("BosMon", "bosmon_port")) - logging.debug(" Start Request ") - httprequest.request("POST", "/telegramin/"+globals.config.get("BosMon", "bosmon_channel")+"/input.xml", params, headers) - logging.debug(" Get Response: ") - httpresponse = httprequest.getresponse() - if str(httpresponse.status) == "200": #Check HTTP Response an print a Log or Error - logging.debug("BosMon response: %s - %s", str(httpresponse.status), str(httpresponse.reason)) - else: - logging.warning("BosMon response: %s - %s", str(httpresponse.status), str(httpresponse.reason)) - except: - logging.error("POC to BosMon failed") + try: + #Initialize header an connect to BosMon-Server + headers = {} + headers['Content-type'] = "application/x-www-form-urlencoded" + headers['Accept'] = "text/plain" + if globals.config.get("BosMon", "bosmon_user"): + headers['Authorization'] = "Basic {0}".format(base64.b64encode("{0}:{1}".format(globals.config.get("BosMon", "bosmon_user"), globals.config.get("BosMon", "bosmon_password")))) + logging.debug("connect to BosMon") + httprequest = httplib.HTTPConnection(globals.config.get("BosMon", "bosmon_server"), globals.config.get("BosMon", "bosmon_port")) + except: + logging.exception("cannot connect to BosMon") + else: - logging.warning("Invalid Typ: %s", typ) - ########## User Plugin CODE ########## + if typ == "FMS": + logging.warning("%s not supported", typ) + + elif typ == "ZVEI": + logging.warning("%s not supported", typ) + + elif typ == "POC": + logging.debug("Start POC to BosMon") + try: + #BosMon-Telegramin expected "a-d" as RIC-sub/function + data["function"] = data["function"].replace("1", "a").replace("2", "b").replace("3", "c").replace("4", "d") + params = urllib.urlencode({'type':'pocsag', 'address':data["ric"], 'flags':'0', 'function':data["function"], 'message':data["msg"]}) + logging.debug(" - Params: %s", params) + bosMonRequest(httprequest, params, headers) + except: + logging.error("POC to BosMon failed") + + else: + logging.warning("Invalid Typ: %s", typ) + + finally: + logging.debug("close BosMon-Connection") + httprequest.close() except: logging.exception("") \ No newline at end of file From d02a2a3aab8c48647c522b53c5edea29649eef29 Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Fri, 22 May 2015 20:12:57 +0200 Subject: [PATCH 09/28] little change boswatch, plugin_test includes ready --- boswatch.py | 2 +- plugin_test.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/boswatch.py b/boswatch.py index e94a597..678e37f 100755 --- a/boswatch.py +++ b/boswatch.py @@ -127,7 +127,7 @@ try: for key,val in globals.config.items("BOSWatch"): logging.debug(" - %s = %s", key, val) except: - logging.debug("cannot read config file") + logging.exception("cannot read config file") else: #load plugins diff --git a/plugin_test.py b/plugin_test.py index cda6ed4..49c6308 100644 --- a/plugin_test.py +++ b/plugin_test.py @@ -5,15 +5,15 @@ # Use this as a simple Plugin Loading Tool to test your own Coded Plugins # ########################################################################### -import globals # Global variables -import pluginloader - import logging import ConfigParser #for parse the config file import os #for log mkdir import time #timestamp for doublealarm +from includes import globals # Global variables +from includes import pluginloader + #create new logger logger = logging.getLogger() logger.setLevel(logging.DEBUG) @@ -46,7 +46,7 @@ try: for key,val in globals.config.items("Plugins"): logging.debug(" - %s = %s", key, val) except: - logging.debug("cannot read config file") + logging.exception("cannot read config file") try: @@ -86,4 +86,4 @@ while True: exit() except: logging.exception("unknown error") - exit() \ No newline at end of file + exit() From e009cecf9f69d6ecb48b3dd7310c2d14665f6d38 Mon Sep 17 00:00:00 2001 From: JHCD Date: Fri, 22 May 2015 20:17:01 +0200 Subject: [PATCH 10/28] restructure decoders (poc) --- includes/decoder.py | 50 ++------------------------------- includes/decoders/poc.py | 60 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 48 deletions(-) diff --git a/includes/decoder.py b/includes/decoder.py index 5108fa9..401b80c 100644 --- a/includes/decoder.py +++ b/includes/decoder.py @@ -6,7 +6,6 @@ import time #timestamp for doublealarm import re #Regex for validation from includes import globals # Global variables -from includes import pluginloader def decode(freq, decoded): @@ -71,50 +70,5 @@ def decode(freq, decoded): #check POCSAG -> validate -> check double alarm -> log if "POCSAG" in decoded: logging.debug("recieved POCSAG") - bitrate = 0 - - if "POCSAG512:" in decoded: - bitrate = 512 - poc_id = decoded[20:27] - poc_sub = decoded[39].replace("3", "4").replace("2", "3").replace("1", "2").replace("0", "1") - - elif "POCSAG1200:" in decoded: - bitrate = 1200 - poc_id = decoded[21:28] - poc_sub = decoded[40].replace("3", "4").replace("2", "3").replace("1", "2").replace("0", "1") - - elif "POCSAG2400:" in decoded: - bitrate = 2400 - poc_id = decoded[21:28] - poc_sub = decoded[40].replace("3", "4").replace("2", "3").replace("1", "2").replace("0", "1") - - if bitrate is 0: - logging.warning("POCSAG Bitrate not found") - else: - logging.debug("POCSAG Bitrate: %s", bitrate) - - if "Alpha:" in decoded: #check if there is a text message - poc_text = decoded.split('Alpha: ')[1].strip().rstrip('').strip() - else: - poc_text = "" - - if re.search("[0-9]{7}", poc_id): #if POC is valid - if int(poc_id) >= globals.config.getint("BOSWatch", "poc_filter_range_start"): - if int(poc_id) <= globals.config.getint("BOSWatch", "poc_filter_range_end"): - if poc_id == globals.poc_id_old and timestamp < globals.poc_time_old + globals.config.getint("BOSWatch", "poc_double_ignore_time"): #check for double alarm - logging.info("POCSAG%s double alarm: %s within %s second(s)", bitrate, globals.poc_id_old, timestamp-globals.poc_time_old) - globals.poc_time_old = timestamp #in case of double alarm, poc_double_ignore_time set new - else: - logging.info("POCSAG%s: %s %s %s ", bitrate, poc_id, poc_sub, poc_text) - data = {"ric":poc_id, "function":poc_sub, "msg":poc_text, "bitrate":bitrate} - from includes import pluginHandler - pluginHandler.throwAlarm("POC",freq,data) - - globals.poc_id_old = poc_id #save last id - globals.poc_time_old = timestamp #save last time - else: - logging.info("POCSAG%s: %s out of filter range (high)", bitrate, poc_id) - else: - logging.info("POCSAG%s: %s out of filter range (low)", bitrate, poc_id) - else: - logging.warning("No valid POCSAG%s RIC: %s", bitrate, poc_id) \ No newline at end of file + from includes.decoders import poc + poc.decode(freq, decoded) diff --git a/includes/decoders/poc.py b/includes/decoders/poc.py index e69de29..d78c09a 100644 --- a/includes/decoders/poc.py +++ b/includes/decoders/poc.py @@ -0,0 +1,60 @@ +#!/usr/bin/python +# -*- coding: cp1252 -*- + +import logging +import time #timestamp for doublealarm +import re #Regex for validation + +from includes import globals # Global variables + +#POCSAG Decoder Function +#validate -> check double alarm -> log +def decode(freq, decoded): + bitrate = 0 + timestamp = int(time.time())#Get Timestamp + + if "POCSAG512:" in decoded: + bitrate = 512 + poc_id = decoded[20:27] + poc_sub = decoded[39].replace("3", "4").replace("2", "3").replace("1", "2").replace("0", "1") + + elif "POCSAG1200:" in decoded: + bitrate = 1200 + poc_id = decoded[21:28] + poc_sub = decoded[40].replace("3", "4").replace("2", "3").replace("1", "2").replace("0", "1") + + elif "POCSAG2400:" in decoded: + bitrate = 2400 + poc_id = decoded[21:28] + poc_sub = decoded[40].replace("3", "4").replace("2", "3").replace("1", "2").replace("0", "1") + + if bitrate is 0: + logging.warning("POCSAG Bitrate not found") + else: + logging.debug("POCSAG Bitrate: %s", bitrate) + + if "Alpha:" in decoded: #check if there is a text message + poc_text = decoded.split('Alpha: ')[1].strip().rstrip('').strip() + else: + poc_text = "" + + if re.search("[0-9]{7}", poc_id): #if POC is valid + if int(poc_id) >= globals.config.getint("BOSWatch", "poc_filter_range_start"): + if int(poc_id) <= globals.config.getint("BOSWatch", "poc_filter_range_end"): + if poc_id == globals.poc_id_old and timestamp < globals.poc_time_old + globals.config.getint("BOSWatch", "poc_double_ignore_time"): #check for double alarm + logging.info("POCSAG%s double alarm: %s within %s second(s)", bitrate, globals.poc_id_old, timestamp-globals.poc_time_old) + globals.poc_time_old = timestamp #in case of double alarm, poc_double_ignore_time set new + else: + logging.info("POCSAG%s: %s %s %s ", bitrate, poc_id, poc_sub, poc_text) + data = {"ric":poc_id, "function":poc_sub, "msg":poc_text, "bitrate":bitrate} + from includes import pluginHandler + pluginHandler.throwAlarm("POC",freq,data) + + globals.poc_id_old = poc_id #save last id + globals.poc_time_old = timestamp #save last time + else: + logging.info("POCSAG%s: %s out of filter range (high)", bitrate, poc_id) + else: + logging.info("POCSAG%s: %s out of filter range (low)", bitrate, poc_id) + else: + logging.warning("No valid POCSAG%s RIC: %s", bitrate, poc_id) \ No newline at end of file From da2ea5fe4ea844d8a4ffe1a0f94c5ad7a9499efb Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Fri, 22 May 2015 20:17:57 +0200 Subject: [PATCH 11/28] change in httpreq plugin --- plugins/httpRequest/httpRequest.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plugins/httpRequest/httpRequest.py b/plugins/httpRequest/httpRequest.py index c0614c7..0720db7 100644 --- a/plugins/httpRequest/httpRequest.py +++ b/plugins/httpRequest/httpRequest.py @@ -66,6 +66,10 @@ def run(typ,freq,data): logging.exception("no HTTP request been sended") except: #otherwise logging.exception("cannot get HTTP response") + + finally: + logging.debug("close http-Connection") + httprequest.close() ########## User Plugin CODE ########## except: From 82a837c19e73ea37117d7f72a967857e0b1bf8c6 Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Fri, 22 May 2015 21:27:46 +0200 Subject: [PATCH 12/28] move loadPlugins() pHanlder->pLoader --- boswatch.py | 4 ++-- includes/pluginHandler.py | 14 +------------- includes/pluginloader.py | 10 ++++++++++ 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/boswatch.py b/boswatch.py index 678e37f..2768a5e 100755 --- a/boswatch.py +++ b/boswatch.py @@ -131,8 +131,8 @@ try: else: #load plugins - from includes import pluginHandler - pluginHandler.loadPlugins() + from includes import pluginloader + pluginloader.loadPlugins() try: #start rtl_fm diff --git a/includes/pluginHandler.py b/includes/pluginHandler.py index eadbd08..b3491e1 100644 --- a/includes/pluginHandler.py +++ b/includes/pluginHandler.py @@ -11,16 +11,4 @@ def throwAlarm(typ,freq,data): for name, plugin in globals.pluginList.items(): logging.debug("call Plugin: %s", name) plugin.run(typ,freq,data) - logging.debug("[END ALARM]") - - -def loadPlugins(): - try: - #load plugins - logging.debug("loading plugins") - for i in pluginloader.getPlugins(): - plugin = pluginloader.loadPlugin(i) - globals.pluginList[i["name"]] = plugin - - except: - logging.exception("cannot load Plugins") \ No newline at end of file + logging.debug("[END ALARM]") \ No newline at end of file diff --git a/includes/pluginloader.py b/includes/pluginloader.py index 25c66ad..ae591cf 100644 --- a/includes/pluginloader.py +++ b/includes/pluginloader.py @@ -7,6 +7,15 @@ import os from includes import globals # Global variables +def loadPlugins(): + try: + logging.debug("loading plugins") + for i in getPlugins(): + plugin = loadPlugin(i) + globals.pluginList[i["name"]] = plugin + except: + logging.exception("cannot load Plugins") + def getPlugins(): try: @@ -34,6 +43,7 @@ def getPlugins(): return plugins + def loadPlugin(plugin): try: logging.debug("load Plugin: %s", plugin["name"]) From 26c9e23db8c6f82247b772859d35e35f0ac3f919 Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Fri, 22 May 2015 21:32:37 +0200 Subject: [PATCH 13/28] rename pluginHander.py->alarmHandler.py Method rename: throwAlarm() ->processAlarm() --- .../{pluginHandler.py => alarmHandler.py} | 26 +++++++++---------- includes/decoder.py | 8 +++--- includes/decoders/poc.py | 4 +-- 3 files changed, 19 insertions(+), 19 deletions(-) rename includes/{pluginHandler.py => alarmHandler.py} (87%) diff --git a/includes/pluginHandler.py b/includes/alarmHandler.py similarity index 87% rename from includes/pluginHandler.py rename to includes/alarmHandler.py index b3491e1..88a1834 100644 --- a/includes/pluginHandler.py +++ b/includes/alarmHandler.py @@ -1,14 +1,14 @@ -#!/usr/bin/python -# -*- coding: cp1252 -*- - -import logging - -from includes import globals # Global variables -from includes import pluginloader - -def throwAlarm(typ,freq,data): - logging.debug("[ ALARM ]") - for name, plugin in globals.pluginList.items(): - logging.debug("call Plugin: %s", name) - plugin.run(typ,freq,data) +#!/usr/bin/python +# -*- coding: cp1252 -*- + +import logging + +from includes import globals # Global variables +from includes import pluginloader + +def processAlarm(typ,freq,data): + logging.debug("[ ALARM ]") + for name, plugin in globals.pluginList.items(): + logging.debug("call Plugin: %s", name) + plugin.run(typ,freq,data) logging.debug("[END ALARM]") \ No newline at end of file diff --git a/includes/decoder.py b/includes/decoder.py index 401b80c..6a2a0c7 100644 --- a/includes/decoder.py +++ b/includes/decoder.py @@ -33,8 +33,8 @@ def decode(freq, decoded): else: logging.info("FMS:%s Status:%s Richtung:%s TKI:%s", fms_id[0:8], fms_status, fms_direction, fms_tsi) data = {"fms":fms_id[0:8], "status":fms_status, "direction":fms_direction, "tsi":fms_tsi} - from includes import pluginHandler - pluginHandler.throwAlarm("FMS",freq,data) + from includes import alarmHandler + alarmHandler.processAlarm("FMS",freq,data) globals.fms_id_old = fms_id #save last id globals.fms_time_old = timestamp #save last time @@ -57,8 +57,8 @@ def decode(freq, decoded): else: logging.info("5-Ton: %s", zvei_id) data = {"zvei":zvei_id} - from includes import pluginHandler - pluginHandler.throwAlarm("ZVEI",freq,data) + from includes import alarmHandler + alarmHandler.processAlarm("ZVEI",freq,data) globals.zvei_id_old = zvei_id #save last id globals.zvei_time_old = timestamp #save last time diff --git a/includes/decoders/poc.py b/includes/decoders/poc.py index d78c09a..0aa9e76 100644 --- a/includes/decoders/poc.py +++ b/includes/decoders/poc.py @@ -47,8 +47,8 @@ def decode(freq, decoded): else: logging.info("POCSAG%s: %s %s %s ", bitrate, poc_id, poc_sub, poc_text) data = {"ric":poc_id, "function":poc_sub, "msg":poc_text, "bitrate":bitrate} - from includes import pluginHandler - pluginHandler.throwAlarm("POC",freq,data) + from includes import alarmHandler + alarmHandler.processAlarm("POC",freq,data) globals.poc_id_old = poc_id #save last id globals.poc_time_old = timestamp #save last time From 54fe3e8cca8bcf538a90a2a6ec07762dc6619013 Mon Sep 17 00:00:00 2001 From: JHCD Date: Fri, 22 May 2015 21:38:35 +0200 Subject: [PATCH 14/28] bugfix in MySQL plugin --- plugins/MySQL/MySQL.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/MySQL/MySQL.py b/plugins/MySQL/MySQL.py index bbe855f..be19b3e 100644 --- a/plugins/MySQL/MySQL.py +++ b/plugins/MySQL/MySQL.py @@ -36,7 +36,8 @@ def run(typ,freq,data): elif typ == "ZVEI": #data = {"zvei":zvei_id} - cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableZVEI")+" (time,zvei) VALUES (NOW(),%s)",(data["zvei"])) + #Don't use %s here (bug in mysql-lib with one parameter) + cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableZVEI")+" (time,zvei) VALUES (NOW(),"+(data["zvei"])+")") elif typ == "POC": #data = {"ric":poc_id, "function":poc_sub, "msg":poc_text} From 89aae1fb29a84a9e4dd324a61d0395cd8902a641 Mon Sep 17 00:00:00 2001 From: JHCD Date: Fri, 22 May 2015 21:40:38 +0200 Subject: [PATCH 15/28] restructure decode - zvei --- includes/decoder.py | 19 ++----------------- includes/decoders/zvei.py | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/includes/decoder.py b/includes/decoder.py index 401b80c..ecd5632 100644 --- a/includes/decoder.py +++ b/includes/decoder.py @@ -48,23 +48,8 @@ def decode(freq, decoded): #check ZVEI: -> validate -> check double alarm -> log if "ZVEI2:" in decoded: logging.debug("recieved ZVEI") - - zvei_id = decoded[7:12] #ZVEI Code - if re.search("[0-9F]{5}", zvei_id): #if ZVEI is valid - if zvei_id == globals.zvei_id_old and timestamp < globals.zvei_time_old + globals.config.getint("BOSWatch", "zvei_double_ignore_time"): #check for double alarm - logging.info("ZVEI double alarm: %s within %s second(s)", globals.zvei_id_old, timestamp-globals.zvei_time_old) - globals.zvei_time_old = timestamp #in case of double alarm, zvei_double_ignore_time set new - else: - logging.info("5-Ton: %s", zvei_id) - data = {"zvei":zvei_id} - from includes import pluginHandler - pluginHandler.throwAlarm("ZVEI",freq,data) - - globals.zvei_id_old = zvei_id #save last id - globals.zvei_time_old = timestamp #save last time - else: - logging.warning("No valid ZVEI: %s", zvei_id) - + from includes.decoders import zvei + zvei.decode(freq, decoded) #POCSAG Decoder Section #check POCSAG -> validate -> check double alarm -> log diff --git a/includes/decoders/zvei.py b/includes/decoders/zvei.py index e69de29..08e6744 100644 --- a/includes/decoders/zvei.py +++ b/includes/decoders/zvei.py @@ -0,0 +1,27 @@ +#!/usr/bin/python +# -*- coding: cp1252 -*- + +import logging +import time #timestamp for doublealarm +import re #Regex for validation + +from includes import globals # Global variables + +def decode(freq, decoded): + timestamp = int(time.time())#Get Timestamp + + zvei_id = decoded[7:12] #ZVEI Code + if re.search("[0-9F]{5}", zvei_id): #if ZVEI is valid + if zvei_id == globals.zvei_id_old and timestamp < globals.zvei_time_old + globals.config.getint("BOSWatch", "zvei_double_ignore_time"): #check for double alarm + logging.info("ZVEI double alarm: %s within %s second(s)", globals.zvei_id_old, timestamp-globals.zvei_time_old) + globals.zvei_time_old = timestamp #in case of double alarm, zvei_double_ignore_time set new + else: + logging.info("5-Ton: %s", zvei_id) + data = {"zvei":zvei_id} + from includes import pluginHandler + pluginHandler.throwAlarm("ZVEI",freq,data) + + globals.zvei_id_old = zvei_id #save last id + globals.zvei_time_old = timestamp #save last time + else: + logging.warning("No valid ZVEI: %s", zvei_id) \ No newline at end of file From 4915de585df61792c71dddfbaaf7ef0cec245ab5 Mon Sep 17 00:00:00 2001 From: JHCD Date: Fri, 22 May 2015 21:48:16 +0200 Subject: [PATCH 16/28] restructure decoder - fms --- includes/decoder.py | 36 ++---------------------------------- includes/decoders/fms.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/includes/decoder.py b/includes/decoder.py index ecd5632..b693120 100644 --- a/includes/decoder.py +++ b/includes/decoder.py @@ -2,47 +2,15 @@ # -*- coding: cp1252 -*- import logging -import time #timestamp for doublealarm -import re #Regex for validation - -from includes import globals # Global variables - def decode(freq, decoded): - timestamp = int(time.time())#Get Timestamp #FMS Decoder Section #check FMS: -> check CRC -> validate -> check double alarm -> log if "FMS:" in decoded: logging.debug("recieved FMS") - - fms_service = decoded[19] #Organisation - fms_country = decoded[36] #Bundesland - fms_location = decoded[65:67] #Ort - fms_vehicle = decoded[72:76] #Fahrzeug - fms_status = decoded[84] #Status - fms_direction = decoded[101] #Richtung - fms_tsi = decoded[114:117] #Taktische Kruzinformation - - if "CRC correct" in decoded: #check CRC is correct - fms_id = fms_service+fms_country+fms_location+fms_vehicle+fms_status+fms_direction #build FMS id - if re.search("[0-9a-f]{8}[0-9a-f]{1}[01]{1}", fms_id): #if FMS is valid - if fms_id == globals.fms_id_old and timestamp < globals.fms_time_old + globals.config.getint("BOSWatch", "fms_double_ignore_time"): #check for double alarm - logging.info("FMS double alarm: %s within %s second(s)", globals.fms_id_old, timestamp-globals.fms_time_old) - globals.fms_time_old = timestamp #in case of double alarm, fms_double_ignore_time set new - else: - logging.info("FMS:%s Status:%s Richtung:%s TKI:%s", fms_id[0:8], fms_status, fms_direction, fms_tsi) - data = {"fms":fms_id[0:8], "status":fms_status, "direction":fms_direction, "tsi":fms_tsi} - from includes import pluginHandler - pluginHandler.throwAlarm("FMS",freq,data) - - globals.fms_id_old = fms_id #save last id - globals.fms_time_old = timestamp #save last time - else: - logging.warning("No valid FMS: %s", fms_id) - else: - logging.warning("FMS CRC incorrect") - + from includes.decoders import fms + fms.decode(freq, decoded) #ZVEI Decoder Section #check ZVEI: -> validate -> check double alarm -> log diff --git a/includes/decoders/fms.py b/includes/decoders/fms.py index e69de29..da4eed3 100644 --- a/includes/decoders/fms.py +++ b/includes/decoders/fms.py @@ -0,0 +1,40 @@ +#!/usr/bin/python +# -*- coding: cp1252 -*- + +import logging +import time #timestamp for doublealarm +import re #Regex for validation + +from includes import globals # Global variables + +#FMS Decoder Function +#validate -> check double alarm -> log +def decode(freq, decoded): + timestamp = int(time.time())#Get Timestamp + + fms_service = decoded[19] #Organisation + fms_country = decoded[36] #Bundesland + fms_location = decoded[65:67] #Ort + fms_vehicle = decoded[72:76] #Fahrzeug + fms_status = decoded[84] #Status + fms_direction = decoded[101] #Richtung + fms_tsi = decoded[114:117] #Taktische Kruzinformation + + if "CRC correct" in decoded: #check CRC is correct + fms_id = fms_service+fms_country+fms_location+fms_vehicle+fms_status+fms_direction #build FMS id + if re.search("[0-9a-f]{8}[0-9a-f]{1}[01]{1}", fms_id): #if FMS is valid + if fms_id == globals.fms_id_old and timestamp < globals.fms_time_old + globals.config.getint("BOSWatch", "fms_double_ignore_time"): #check for double alarm + logging.info("FMS double alarm: %s within %s second(s)", globals.fms_id_old, timestamp-globals.fms_time_old) + globals.fms_time_old = timestamp #in case of double alarm, fms_double_ignore_time set new + else: + logging.info("FMS:%s Status:%s Richtung:%s TKI:%s", fms_id[0:8], fms_status, fms_direction, fms_tsi) + data = {"fms":fms_id[0:8], "status":fms_status, "direction":fms_direction, "tsi":fms_tsi} + from includes import pluginHandler + pluginHandler.throwAlarm("FMS",freq,data) + + globals.fms_id_old = fms_id #save last id + globals.fms_time_old = timestamp #save last time + else: + logging.warning("No valid FMS: %s", fms_id) + else: + logging.warning("FMS CRC incorrect") \ No newline at end of file From 5929908c72fa0e4846e2d06698b75b4f4a1ea177 Mon Sep 17 00:00:00 2001 From: JHCD Date: Fri, 22 May 2015 21:49:06 +0200 Subject: [PATCH 17/28] add information --- includes/decoders/zvei.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/includes/decoders/zvei.py b/includes/decoders/zvei.py index 08e6744..437fabf 100644 --- a/includes/decoders/zvei.py +++ b/includes/decoders/zvei.py @@ -7,6 +7,8 @@ import re #Regex for validation from includes import globals # Global variables +#ZVEI Decoder Function +#validate -> check double alarm -> log def decode(freq, decoded): timestamp = int(time.time())#Get Timestamp From 54d0230713a54c874876bf037ac17bffa6a56d73 Mon Sep 17 00:00:00 2001 From: JHCD Date: Fri, 22 May 2015 21:57:41 +0200 Subject: [PATCH 18/28] merge decoder after restructuring --- includes/decoder.py | 51 --------------------------------------------- 1 file changed, 51 deletions(-) diff --git a/includes/decoder.py b/includes/decoder.py index 7d87359..b693120 100644 --- a/includes/decoder.py +++ b/includes/decoder.py @@ -9,66 +9,15 @@ def decode(freq, decoded): #check FMS: -> check CRC -> validate -> check double alarm -> log if "FMS:" in decoded: logging.debug("recieved FMS") -<<<<<<< HEAD from includes.decoders import fms fms.decode(freq, decoded) -======= - - fms_service = decoded[19] #Organisation - fms_country = decoded[36] #Bundesland - fms_location = decoded[65:67] #Ort - fms_vehicle = decoded[72:76] #Fahrzeug - fms_status = decoded[84] #Status - fms_direction = decoded[101] #Richtung - fms_tsi = decoded[114:117] #Taktische Kruzinformation - - if "CRC correct" in decoded: #check CRC is correct - fms_id = fms_service+fms_country+fms_location+fms_vehicle+fms_status+fms_direction #build FMS id - if re.search("[0-9a-f]{8}[0-9a-f]{1}[01]{1}", fms_id): #if FMS is valid - if fms_id == globals.fms_id_old and timestamp < globals.fms_time_old + globals.config.getint("BOSWatch", "fms_double_ignore_time"): #check for double alarm - logging.info("FMS double alarm: %s within %s second(s)", globals.fms_id_old, timestamp-globals.fms_time_old) - globals.fms_time_old = timestamp #in case of double alarm, fms_double_ignore_time set new - else: - logging.info("FMS:%s Status:%s Richtung:%s TKI:%s", fms_id[0:8], fms_status, fms_direction, fms_tsi) - data = {"fms":fms_id[0:8], "status":fms_status, "direction":fms_direction, "tsi":fms_tsi} - from includes import alarmHandler - alarmHandler.processAlarm("FMS",freq,data) - - globals.fms_id_old = fms_id #save last id - globals.fms_time_old = timestamp #save last time - else: - logging.warning("No valid FMS: %s", fms_id) - else: - logging.warning("FMS CRC incorrect") - ->>>>>>> 26c9e23db8c6f82247b772859d35e35f0ac3f919 #ZVEI Decoder Section #check ZVEI: -> validate -> check double alarm -> log if "ZVEI2:" in decoded: logging.debug("recieved ZVEI") -<<<<<<< HEAD from includes.decoders import zvei zvei.decode(freq, decoded) -======= - - zvei_id = decoded[7:12] #ZVEI Code - if re.search("[0-9F]{5}", zvei_id): #if ZVEI is valid - if zvei_id == globals.zvei_id_old and timestamp < globals.zvei_time_old + globals.config.getint("BOSWatch", "zvei_double_ignore_time"): #check for double alarm - logging.info("ZVEI double alarm: %s within %s second(s)", globals.zvei_id_old, timestamp-globals.zvei_time_old) - globals.zvei_time_old = timestamp #in case of double alarm, zvei_double_ignore_time set new - else: - logging.info("5-Ton: %s", zvei_id) - data = {"zvei":zvei_id} - from includes import alarmHandler - alarmHandler.processAlarm("ZVEI",freq,data) - - globals.zvei_id_old = zvei_id #save last id - globals.zvei_time_old = timestamp #save last time - else: - logging.warning("No valid ZVEI: %s", zvei_id) - ->>>>>>> 26c9e23db8c6f82247b772859d35e35f0ac3f919 #POCSAG Decoder Section #check POCSAG -> validate -> check double alarm -> log From e99286bc830e9566489e8de93116fd5531da6dd4 Mon Sep 17 00:00:00 2001 From: JHCD Date: Fri, 22 May 2015 22:09:32 +0200 Subject: [PATCH 19/28] rename pluginHandler.py -> alarmHandler.py Method rename: throwAlarm() ->processAlaram() --- includes/decoders/fms.py | 2 +- includes/decoders/zvei.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/decoders/fms.py b/includes/decoders/fms.py index da4eed3..791794e 100644 --- a/includes/decoders/fms.py +++ b/includes/decoders/fms.py @@ -30,7 +30,7 @@ def decode(freq, decoded): logging.info("FMS:%s Status:%s Richtung:%s TKI:%s", fms_id[0:8], fms_status, fms_direction, fms_tsi) data = {"fms":fms_id[0:8], "status":fms_status, "direction":fms_direction, "tsi":fms_tsi} from includes import pluginHandler - pluginHandler.throwAlarm("FMS",freq,data) + alarmHandler.processAlarm("POC",freq,data) globals.fms_id_old = fms_id #save last id globals.fms_time_old = timestamp #save last time diff --git a/includes/decoders/zvei.py b/includes/decoders/zvei.py index 437fabf..133f223 100644 --- a/includes/decoders/zvei.py +++ b/includes/decoders/zvei.py @@ -21,7 +21,7 @@ def decode(freq, decoded): logging.info("5-Ton: %s", zvei_id) data = {"zvei":zvei_id} from includes import pluginHandler - pluginHandler.throwAlarm("ZVEI",freq,data) + alarmHandler.processAlarm("POC",freq,data) globals.zvei_id_old = zvei_id #save last id globals.zvei_time_old = timestamp #save last time From 5a305481023eb72b5a2bdf43231a6d7be46491f0 Mon Sep 17 00:00:00 2001 From: JHCD Date: Fri, 22 May 2015 22:22:50 +0200 Subject: [PATCH 20/28] import correct module --- includes/decoders/fms.py | 2 +- includes/decoders/zvei.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/decoders/fms.py b/includes/decoders/fms.py index 791794e..a2be9a8 100644 --- a/includes/decoders/fms.py +++ b/includes/decoders/fms.py @@ -29,7 +29,7 @@ def decode(freq, decoded): else: logging.info("FMS:%s Status:%s Richtung:%s TKI:%s", fms_id[0:8], fms_status, fms_direction, fms_tsi) data = {"fms":fms_id[0:8], "status":fms_status, "direction":fms_direction, "tsi":fms_tsi} - from includes import pluginHandler + from includes import alarmHandler alarmHandler.processAlarm("POC",freq,data) globals.fms_id_old = fms_id #save last id diff --git a/includes/decoders/zvei.py b/includes/decoders/zvei.py index 133f223..523a267 100644 --- a/includes/decoders/zvei.py +++ b/includes/decoders/zvei.py @@ -20,7 +20,7 @@ def decode(freq, decoded): else: logging.info("5-Ton: %s", zvei_id) data = {"zvei":zvei_id} - from includes import pluginHandler + from includes import alarmHandler alarmHandler.processAlarm("POC",freq,data) globals.zvei_id_old = zvei_id #save last id From 096ba7e6b6a282d6036b9f65d88d4134ecaf8374 Mon Sep 17 00:00:00 2001 From: JHCD Date: Fri, 22 May 2015 22:27:41 +0200 Subject: [PATCH 21/28] bugfixing call processAlarm --- includes/decoders/fms.py | 2 +- includes/decoders/zvei.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/decoders/fms.py b/includes/decoders/fms.py index a2be9a8..2c983ef 100644 --- a/includes/decoders/fms.py +++ b/includes/decoders/fms.py @@ -30,7 +30,7 @@ def decode(freq, decoded): logging.info("FMS:%s Status:%s Richtung:%s TKI:%s", fms_id[0:8], fms_status, fms_direction, fms_tsi) data = {"fms":fms_id[0:8], "status":fms_status, "direction":fms_direction, "tsi":fms_tsi} from includes import alarmHandler - alarmHandler.processAlarm("POC",freq,data) + alarmHandler.processAlarm("FMS",freq,data) globals.fms_id_old = fms_id #save last id globals.fms_time_old = timestamp #save last time diff --git a/includes/decoders/zvei.py b/includes/decoders/zvei.py index 523a267..81320dc 100644 --- a/includes/decoders/zvei.py +++ b/includes/decoders/zvei.py @@ -21,7 +21,7 @@ def decode(freq, decoded): logging.info("5-Ton: %s", zvei_id) data = {"zvei":zvei_id} from includes import alarmHandler - alarmHandler.processAlarm("POC",freq,data) + alarmHandler.processAlarm("ZVEI",freq,data) globals.zvei_id_old = zvei_id #save last id globals.zvei_time_old = timestamp #save last time From c590d706555d0c1aa4ba56c952f9a394a66c9449 Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Fri, 22 May 2015 22:40:24 +0200 Subject: [PATCH 22/28] rename pluginloader -> pluginLoader --- includes/pluginloader.py | 52 ---------------------------------------- 1 file changed, 52 deletions(-) delete mode 100644 includes/pluginloader.py diff --git a/includes/pluginloader.py b/includes/pluginloader.py deleted file mode 100644 index ae591cf..0000000 --- a/includes/pluginloader.py +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/python -# -*- coding: cp1252 -*- - -import logging # Global logger -import imp -import os - -from includes import globals # Global variables - -def loadPlugins(): - try: - logging.debug("loading plugins") - for i in getPlugins(): - plugin = loadPlugin(i) - globals.pluginList[i["name"]] = plugin - except: - logging.exception("cannot load Plugins") - - -def getPlugins(): - try: - logging.debug("Search in Plugin Folder") - PluginFolder = globals.script_path+"/plugins" - plugins = [] - for i in os.listdir(PluginFolder): - location = os.path.join(PluginFolder, i) - # plugins have to be a subdir with MainModule, if not skip - if not os.path.isdir(location) or not i + ".py" in os.listdir(location): - continue - - # is the plugin enabled in the config-file? - try: - if globals.config.getint("Plugins", i): - info = imp.find_module(i, [location]) - plugins.append({"name": i, "info": info}) - logging.debug("Plugin [ENABLED ] %s", i) - else: - logging.debug("Plugin [DISABLED] %s ", i) - except: #no entry for plugin found in config-file, skip - logging.warning("Plugin [NO CONF ] %s", i) - except: - logging.exception("Error during Plugin search") - - return plugins - - -def loadPlugin(plugin): - try: - logging.debug("load Plugin: %s", plugin["name"]) - return imp.load_module(plugin["name"], *plugin["info"]) - except: - logging.exception("cannot load Plugin: %s", plugin["name"]) \ No newline at end of file From 0849672cbef17d6cf06f725a5dfbcbc753d412d5 Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Fri, 22 May 2015 22:40:44 +0200 Subject: [PATCH 23/28] rename pluginloader -> pluginLoader --- boswatch.py | 4 ++-- includes/alarmHandler.py | 1 - includes/pluginLoader.py | 52 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 includes/pluginLoader.py diff --git a/boswatch.py b/boswatch.py index 2768a5e..90dabc8 100755 --- a/boswatch.py +++ b/boswatch.py @@ -131,8 +131,8 @@ try: else: #load plugins - from includes import pluginloader - pluginloader.loadPlugins() + from includes import pluginLoader + pluginLoader.loadPlugins() try: #start rtl_fm diff --git a/includes/alarmHandler.py b/includes/alarmHandler.py index 88a1834..7a58dbe 100644 --- a/includes/alarmHandler.py +++ b/includes/alarmHandler.py @@ -4,7 +4,6 @@ import logging from includes import globals # Global variables -from includes import pluginloader def processAlarm(typ,freq,data): logging.debug("[ ALARM ]") diff --git a/includes/pluginLoader.py b/includes/pluginLoader.py new file mode 100644 index 0000000..ae591cf --- /dev/null +++ b/includes/pluginLoader.py @@ -0,0 +1,52 @@ +#!/usr/bin/python +# -*- coding: cp1252 -*- + +import logging # Global logger +import imp +import os + +from includes import globals # Global variables + +def loadPlugins(): + try: + logging.debug("loading plugins") + for i in getPlugins(): + plugin = loadPlugin(i) + globals.pluginList[i["name"]] = plugin + except: + logging.exception("cannot load Plugins") + + +def getPlugins(): + try: + logging.debug("Search in Plugin Folder") + PluginFolder = globals.script_path+"/plugins" + plugins = [] + for i in os.listdir(PluginFolder): + location = os.path.join(PluginFolder, i) + # plugins have to be a subdir with MainModule, if not skip + if not os.path.isdir(location) or not i + ".py" in os.listdir(location): + continue + + # is the plugin enabled in the config-file? + try: + if globals.config.getint("Plugins", i): + info = imp.find_module(i, [location]) + plugins.append({"name": i, "info": info}) + logging.debug("Plugin [ENABLED ] %s", i) + else: + logging.debug("Plugin [DISABLED] %s ", i) + except: #no entry for plugin found in config-file, skip + logging.warning("Plugin [NO CONF ] %s", i) + except: + logging.exception("Error during Plugin search") + + return plugins + + +def loadPlugin(plugin): + try: + logging.debug("load Plugin: %s", plugin["name"]) + return imp.load_module(plugin["name"], *plugin["info"]) + except: + logging.exception("cannot load Plugin: %s", plugin["name"]) \ No newline at end of file From 148547b69fe9968d793e907436df74fd7efe41e9 Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Fri, 22 May 2015 22:58:27 +0200 Subject: [PATCH 24/28] little edit httpreq plugin --- plugins/httpRequest/httpRequest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/httpRequest/httpRequest.py b/plugins/httpRequest/httpRequest.py index 0720db7..689c4bc 100644 --- a/plugins/httpRequest/httpRequest.py +++ b/plugins/httpRequest/httpRequest.py @@ -68,7 +68,7 @@ def run(typ,freq,data): logging.exception("cannot get HTTP response") finally: - logging.debug("close http-Connection") + logging.debug("close HTTP-Connection") httprequest.close() ########## User Plugin CODE ########## From f53bdb30f65cd1926eaf8c402a2a61791eef9e23 Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Fri, 22 May 2015 22:58:43 +0200 Subject: [PATCH 25/28] debug return from plugin --- includes/alarmHandler.py | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/alarmHandler.py b/includes/alarmHandler.py index 7a58dbe..8890ee4 100644 --- a/includes/alarmHandler.py +++ b/includes/alarmHandler.py @@ -10,4 +10,5 @@ def processAlarm(typ,freq,data): for name, plugin in globals.pluginList.items(): logging.debug("call Plugin: %s", name) plugin.run(typ,freq,data) + logging.debug("return from: %s", name) logging.debug("[END ALARM]") \ No newline at end of file From 989cb95e57aa3917f63dac803209387802b0b2e7 Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Fri, 22 May 2015 22:59:33 +0200 Subject: [PATCH 26/28] update plugin_test.py to new methods --- plugin_test.py | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/plugin_test.py b/plugin_test.py index 49c6308..4981052 100644 --- a/plugin_test.py +++ b/plugin_test.py @@ -12,7 +12,8 @@ import os #for log mkdir import time #timestamp for doublealarm from includes import globals # Global variables -from includes import pluginloader +from includes import pluginLoader +from includes import alarmHandler #create new logger logger = logging.getLogger() @@ -49,15 +50,7 @@ except: logging.exception("cannot read config file") -try: - logging.debug("loading plugins") - pluginList = {} - for i in pluginloader.getPlugins(): - plugin = pluginloader.loadPlugin(i) - pluginList[i["name"]] = plugin - logging.debug("loading ready") -except: - logging.exception("cannot load Plugins") +pluginLoader.loadPlugins() # ----- Test Data ----- # @@ -75,11 +68,7 @@ while True: time.sleep(1) print "" - logging.debug("[ ALARM ]") - for name, plugin in pluginList.items(): - logging.debug("call Plugin: %s", name) - plugin.run(typ,"0",data) - logging.debug("[END ALARM]") + alarmHandler.processAlarm(typ,"0",data) except KeyboardInterrupt: logging.warning("Keyboard Interrupt") From 01bec5df3510621a3f126c0ae360d9f8acfdcdd7 Mon Sep 17 00:00:00 2001 From: JHCD Date: Fri, 22 May 2015 23:46:54 +0200 Subject: [PATCH 27/28] implement ZVEI in BosMon-plugin --- plugins/BosMon/BosMon.py | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/plugins/BosMon/BosMon.py b/plugins/BosMon/BosMon.py index 2c67f8f..f93d4da 100644 --- a/plugins/BosMon/BosMon.py +++ b/plugins/BosMon/BosMon.py @@ -11,12 +11,16 @@ from includes import globals # Global variables def bosMonRequest(httprequest, params, headers): - httprequest.request("POST", "/telegramin/"+globals.config.get("BosMon", "bosmon_channel")+"/input.xml", params, headers) - httpresponse = httprequest.getresponse() - if str(httpresponse.status) == "200": #Check HTTP Response an print a Log or Error - logging.debug("BosMon response: %s - %s", str(httpresponse.status), str(httpresponse.reason)) - else: - logging.warning("BosMon response: %s - %s", str(httpresponse.status), str(httpresponse.reason)) + try: + httprequest.request("POST", "/telegramin/"+globals.config.get("BosMon", "bosmon_channel")+"/input.xml", params, headers) + except: + logging.exception("request to BosMon failed") + else: + httpresponse = httprequest.getresponse() + if str(httpresponse.status) == "200": #Check HTTP Response an print a Log or Error + logging.debug("BosMon response: %s - %s", str(httpresponse.status), str(httpresponse.reason)) + else: + logging.warning("BosMon response: %s - %s", str(httpresponse.status), str(httpresponse.reason)) def run(typ,freq,data): @@ -38,15 +42,32 @@ def run(typ,freq,data): headers['Authorization'] = "Basic {0}".format(base64.b64encode("{0}:{1}".format(globals.config.get("BosMon", "bosmon_user"), globals.config.get("BosMon", "bosmon_password")))) logging.debug("connect to BosMon") httprequest = httplib.HTTPConnection(globals.config.get("BosMon", "bosmon_server"), globals.config.get("BosMon", "bosmon_port")) + #debug-level to shell (0=no debug|1) + httprequest.set_debuglevel(1) except: logging.exception("cannot connect to BosMon") else: if typ == "FMS": logging.warning("%s not supported", typ) + #logging.debug("Start FMS to BosMon") + #try: + #BosMon-Telegramin expected assembly group, direction and tsi in one field + #structure: + #params = urllib.urlencode({'type':'fms', 'address':data["fms"], 'status':data["status"], 'info':'0', 'flags':'0'}) + #logging.debug(" - Params: %s", params) + #bosMonRequest(httprequest, params, headers) + #except: + #logging.error("FMS to BosMon failed") elif typ == "ZVEI": - logging.warning("%s not supported", typ) + logging.debug("Start ZVEI to BosMon") + try: + params = urllib.urlencode({'type':'zvei', 'address':data["zvei"], 'flags':'0'}) + logging.debug(" - Params: %s", params) + bosMonRequest(httprequest, params, headers) + except: + logging.error("ZVEI to BosMon failed") elif typ == "POC": logging.debug("Start POC to BosMon") From 2ddee40d9df14bb4deee25b4c402fd46506dd15d Mon Sep 17 00:00:00 2001 From: JHCD Date: Fri, 22 May 2015 23:52:09 +0200 Subject: [PATCH 28/28] disable HTTPConnection debugging --- plugins/BosMon/BosMon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/BosMon/BosMon.py b/plugins/BosMon/BosMon.py index f93d4da..d22e5a0 100644 --- a/plugins/BosMon/BosMon.py +++ b/plugins/BosMon/BosMon.py @@ -43,7 +43,7 @@ def run(typ,freq,data): logging.debug("connect to BosMon") httprequest = httplib.HTTPConnection(globals.config.get("BosMon", "bosmon_server"), globals.config.get("BosMon", "bosmon_port")) #debug-level to shell (0=no debug|1) - httprequest.set_debuglevel(1) + httprequest.set_debuglevel(0) except: logging.exception("cannot connect to BosMon")