From fcc4ba3389f8ece997ce06c2fda34b15c4ce5166 Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Fri, 3 Apr 2015 21:43:00 +0200 Subject: [PATCH] option useMySQL in config.ini now can set MySQL support in config.ini useMySQL = (0|1) --- README.md | 7 ++++-- boswatch.py | 61 ++++++++++++++++++++++++++++++----------------------- config.ini | 6 ++++-- 3 files changed, 44 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 0027cbd..fe3789d 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,11 @@ Python Script to Recive and Decode BOS Information with rtl_fm ans multimon-NG ### Configuration ##### boswatch.py -To use the boswatch.py script, you must edit the "config.ini". -Now you must set the Userdata to your local MySQL Database. +You can set the ignore time for double alarms in seconds. + +To use the script with MySQL Support, you must edit the "config.ini". +Now set "useMySQL = 1" and the Userdata to your local MySQL Database. + For the other Functions see "Usage" below. ##### Web Frontend diff --git a/boswatch.py b/boswatch.py index 9550802..151d554 100644 --- a/boswatch.py +++ b/boswatch.py @@ -25,8 +25,9 @@ def curtime(format="%Y-%m-%d %H:%M:%S"): def stop_script(err): print "ERR: "+err try: - if args.verbose: print "disconnect MySQL" - connection.close() + if useMySQL: #only if MySQL is active + if args.verbose: print "disconnect MySQL" + connection.close() rtl_fm.terminate() if args.verbose: print "rtl_fm terminated" multimon_ng.terminate() @@ -103,27 +104,33 @@ try: config.read("./config.ini") fms_double_ignore_time = int(config.get("FMS", "double_ignore_time")) zvei_double_ignore_time = int(config.get("ZVEI", "double_ignore_time")) + + #MySQL config + useMySQL = int(config.get("MySQL", "useMySQL")) #use MySQL support? + if useMySQL: #only if MySQL is active + dbserver = config.get("MySQL", "dbserver") + dbuser = config.get("MySQL", "dbuser") + dbpassword = config.get("MySQL", "dbpassword") + database = config.get("MySQL", "database") + + #MySQL tables + tableFMS = config.get("MySQL", "tableFMS") + tableZVEI = config.get("MySQL", "tableZVEI") + tablePOC = config.get("MySQL", "tablePOC") except: stop_script("config reading error") exit(0) - dbserver = config.get("MySQL", "dbserver") - dbuser = config.get("MySQL", "dbuser") - dbpassword = config.get("MySQL", "dbpassword") - database = config.get("MySQL", "database") - - tableFMS = config.get("MySQL", "tableFMS") - tableZVEI = config.get("MySQL", "tableZVEI") - tablePOC = config.get("MySQL", "tablePOC") - - if args.verbose: print "connect to MySQL database" - try: - connection = mysql.connector.connect(host = str(dbserver), user = str(dbuser), passwd = str(dbpassword), db = str(database)) - except: - print "MySQL connect error" - exit(0) + if useMySQL: #only if MySQL is active + if args.verbose: print "connect to MySQL database" + try: + connection = mysql.connector.connect(host = str(dbserver), user = str(dbuser), passwd = str(dbpassword), db = str(database)) + except: + print "MySQL connect error" + exit(0) + #variables pre-load if args.verbose: print "pre-load variables" fms_id = 0 fms_id_old = 0 @@ -191,11 +198,12 @@ try: print curtime("%H:%M:%S")+" BOS:"+fms_service+" Bundesland:"+fms_country+" Ort:"+fms_location+" Fahrzeug:"+fms_vehicle+" Status:"+fms_status+" Richtung:"+fms_direction+" TKI:"+fms_tsi fms_id_old = fms_id #save last id fms_time_old = timestamp #save last time - - cursor = connection.cursor() - cursor.execute("INSERT INTO "+tableFMS+" (time,service,country,location,vehicle,status,direction,tsi) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)",(curtime(),fms_service,fms_country,fms_location,fms_vehicle,fms_status,fms_direction,fms_tsi)) - cursor.close() - connection.commit() + + if useMySQL: #only if MySQL is active + cursor = connection.cursor() + cursor.execute("INSERT INTO "+tableFMS+" (time,service,country,location,vehicle,status,direction,tsi) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)",(curtime(),fms_service,fms_country,fms_location,fms_vehicle,fms_status,fms_direction,fms_tsi)) + cursor.close() + connection.commit() elif args.verbose: #crc error only in verbose mode print "CRC error" @@ -217,10 +225,11 @@ try: zvei_id_old = zvei_id #save last id zvei_time_old = timestamp #save last time - cursor = connection.cursor() - cursor.execute("INSERT INTO "+tableZVEI+" (time,zvei) VALUES (%s,%s)",(curtime(),zvei_id)) - cursor.close() - connection.commit() + if useMySQL: #only if MySQL is active + cursor = connection.cursor() + cursor.execute("INSERT INTO "+tableZVEI+" (time,zvei) VALUES (%s,%s)",(curtime(),zvei_id)) + cursor.close() + connection.commit() elif args.verbose: #Invalid error only in verbose mode print "No valid ZVEI: "+decoded diff --git a/config.ini b/config.ini index fd273f9..9cf5251 100644 --- a/config.ini +++ b/config.ini @@ -3,7 +3,9 @@ ######################## #Data for MySQL connection +#useMySQL = (0|1) [MySQL] +useMySQL = 0 dbserver = localhost dbuser = root dbpassword = root @@ -15,8 +17,8 @@ tablePOC = bos_pocsag [FMS] #time to ignore same alarm in a row in seconds -double_ignore_time = 5 +double_ignore_time = 10 [ZVEI] #time to ignore same alarm in a row in seconds -double_ignore_time = 10 \ No newline at end of file +double_ignore_time = 5 \ No newline at end of file