option useMySQL in config.ini

now can set MySQL support in config.ini
useMySQL = (0|1)
This commit is contained in:
Bastian Schroll 2015-04-03 21:43:00 +02:00
parent 9aa8c01557
commit fcc4ba3389
3 changed files with 44 additions and 30 deletions

View file

@ -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

View file

@ -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

View file

@ -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
double_ignore_time = 5