updated MySQL Plugin

This commit is contained in:
Ricardo Krippner 2016-09-29 14:22:23 +02:00
parent 9adf309be3
commit e4e3de1abd
2 changed files with 139 additions and 32 deletions

View file

@ -1,5 +1,5 @@
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# -*- coding: cp1252 -*-
"""
MySQL-Plugin to dispatch FMS-, ZVEI- and POCSAG - messages to a MySQL database
@ -20,6 +20,26 @@ from includes import globals # Global variables
from includes.helper import configHandler
def isSignal(poc_id):
"""
@type poc_id: string
@param poc_id: POCSAG Ric
@requires: Configuration has to be set in the config.ini
@return: True if the Ric is Signal, other False
@exception: none
"""
# If RIC is Signal return True, else False
if globals.config.get("MySQL", "signal_ric"):
if poc_id in globals.config.get("MySQL", "signal_ric"):
logging.info("RIC %s is signal", poc_id)
return True
else:
logging.info("RIC %s is not signal", poc_id)
return False
##
#
# onLoad (init) function of plugin
@ -71,7 +91,7 @@ def run(typ,freq,data):
# Connect to MySQL
#
logging.debug("connect to MySQL")
connection = mysql.connector.connect(host = globals.config.get("MySQL","dbserver"), user = globals.config.get("MySQL","dbuser"), passwd = globals.config.get("MySQL","dbpassword"), db = globals.config.get("MySQL","database"), charset='utf8')
connection = mysql.connector.connect(host = globals.config.get("MySQL","dbserver"), user = globals.config.get("MySQL","dbuser"), passwd = globals.config.get("MySQL","dbpassword"), db = globals.config.get("MySQL","database"))
cursor = connection.cursor()
except:
logging.error("cannot connect to MySQL")
@ -84,13 +104,18 @@ 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 (FROM_UNIXTIME(%s),%s,%s,%s,%s,%s,%s)", (data["timestamp"], 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 (NOW(),%s,%s,%s,%s,%s,%s)",(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 (FROM_UNIXTIME(%s),%s,%s)", (data["timestamp"], data["zvei"], data["description"]))
cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableZVEI")+" (time,zvei,description) VALUES (NOW(),%s,%s)",(data["zvei"],data["description"]))
elif typ == "POC":
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"]))
if isSignal(data["ric"]):
cursor.execute("UPDATE "+globals.config.get("MySQL","tableSIG")+" SET time = NOW() WHERE ric = "+data["ric"])
if cursor.rowcount == 0:
cursor.execute("INSERT INTO "+globals.config.get("MySQL","tableSIG")+" (time,ric) VALUES (NOW(),"+data["ric"]+")")
else:
cursor.execute("INSERT INTO "+globals.config.get("MySQL","tablePOC")+" (time,ric,funktion,funktionChar,msg,bitrate,description) VALUES (NOW(),%s,%s,%s,%s,%s,%s)",(data["ric"],data["function"],data["functionChar"],data["msg"],data["bitrate"],data["description"]))
else:
logging.warning("Invalid Typ: %s", typ)