update mysql plugin: add init_db.sql and create table automatically

This commit is contained in:
Jan Speller 2021-02-28 13:58:29 +01:00
parent 9d73be593f
commit 90d932d53c
3 changed files with 57 additions and 10 deletions

View file

@ -93,8 +93,8 @@ class RouterManager:
logging.error("unknown type '%s' in %s", routeType, route) logging.error("unknown type '%s' in %s", routeType, route)
return False return False
except ModuleNotFoundError as e: except ModuleNotFoundError:
logging.error("%s not found: %s (%s)", route.get("type"), route.get("res"), str(e)) logging.exception("%s not found: %s", route.get("type"), route.get("res"))
return False return False
logging.debug("finished building routers") logging.debug("finished building routers")

34
init_db.sql Normal file
View file

@ -0,0 +1,34 @@
create table boswatch
(
id int auto_increment primary key,
packetTimestamp timestamp default now() not null,
packetMode enum('fms', 'pocsag', 'zvei', 'msg') not null,
pocsag_ric char(7) default null,
pocsag_subric enum('1', '2', '3', '4') default null,
pocsag_subricText enum('a', 'b', 'c', 'd') default null,
pocsag_message text default null,
pocsag_bitrate enum('512', '1200', '2400') default null,
zvei_tone char(5) default null,
fms_fms char(8) default null,
fms_service varchar(255) default null,
fms_country varchar(255) default null,
fms_location varchar(255) default null,
fms_vehicle varchar(255) default null,
fms_status char(1) default null,
fms_direction char(1) default null,
fms_directionText tinytext default null,
fms_tacticalInfo char(3) default null,
serverName varchar(255) not null,
serverVersion varchar(100) not null,
serverBuildDate varchar(255) not null,
serverBranch varchar(255) not null,
clientName varchar(255) not null,
clientIP varchar(255) not null,
clientVersion varchar(100) not null,
clientBuildDate varchar(255) not null,
clientBranch varchar(255) not null,
inputSource varchar(30) not null,
frequency varchar(30) not null
);
create unique index boswatch_id_uindex
on boswatch (id);

View file

@ -37,12 +37,6 @@ class BoswatchPlugin(PluginBase):
def onLoad(self): def onLoad(self):
"""!Called by import of the plugin """!Called by import of the plugin
Remove if not implemented""" Remove if not implemented"""
self.connection = mysql.connector.connect(
host=self.config.get("host"),
user=self.config.get("user"),
password=self.config.get("password"),
database=self.config.get("database"),
)
self.sqlInserts = { self.sqlInserts = {
"pocsag": "INSERT INTO boswatch (packetTimestamp, packetMode, pocsag_ric, pocsag_subric, pocsag_subricText, pocsag_message, pocsag_bitrate, serverName, serverVersion, serverBuildDate, serverBranch, clientName, clientIP, clientVersion, clientBuildDate, clientBranch, inputSource, frequency) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", "pocsag": "INSERT INTO boswatch (packetTimestamp, packetMode, pocsag_ric, pocsag_subric, pocsag_subricText, pocsag_message, pocsag_bitrate, serverName, serverVersion, serverBuildDate, serverBranch, clientName, clientIP, clientVersion, clientBuildDate, clientBranch, inputSource, frequency) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
"zvei": "INSERT INTO boswatch (packetTimestamp, packetMode, zvei_tone, serverName, serverVersion, serverBuildDate, serverBranch, clientName, clientIP, clientVersion, clientBuildDate, clientBranch, inputSource, frequency) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", "zvei": "INSERT INTO boswatch (packetTimestamp, packetMode, zvei_tone, serverName, serverVersion, serverBuildDate, serverBranch, clientName, clientIP, clientVersion, clientBuildDate, clientBranch, inputSource, frequency) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
@ -50,12 +44,30 @@ class BoswatchPlugin(PluginBase):
"msg": "INSERT INTO boswatch (packetTimestamp, packetMode, serverName, serverVersion, serverBuildDate, serverBranch, clientName, clientIP, clientVersion, clientBuildDate, clientBranch, inputSource, frequency) VALUE (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" "msg": "INSERT INTO boswatch (packetTimestamp, packetMode, serverName, serverVersion, serverBuildDate, serverBranch, clientName, clientIP, clientVersion, clientBuildDate, clientBranch, inputSource, frequency) VALUE (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
} }
self.connection = mysql.connector.connect(
host=self.config.get("host"),
user=self.config.get("user"),
password=self.config.get("password"),
database=self.config.get("database"),
)
self.cursor = self.connection.cursor()
self.cursor.execute("SHOW TABLES LIKE 'boswatch'")
if self.cursor.fetchone() is None:
with open('init_db.sql') as f:
for stmnt in f.read().split(';'):
self.cursor.execute(stmnt)
self.connection.commit()
self.cursor.close()
def setup(self): def setup(self):
"""!Called before alarm """!Called before alarm
Remove if not implemented""" Remove if not implemented"""
try: try:
self.connection.ping(reconnect=True, attempts=3, delay=2) self.connection.ping(reconnect=True, attempts=3, delay=2)
except mysql.connector.Error as err: except mysql.connector.Error:
logging.warning("Connection was down, trying to reconnect...") logging.warning("Connection was down, trying to reconnect...")
self.onLoad() self.onLoad()
@ -168,8 +180,9 @@ class BoswatchPlugin(PluginBase):
"""!Called after alarm """!Called after alarm
Remove if not implemented""" Remove if not implemented"""
self.connection.commit() self.connection.commit()
self.cursor.close()
def onUnload(self): def onUnload(self):
"""!Called by destruction of the plugin """!Called by destruction of the plugin
Remove if not implemented""" Remove if not implemented"""
pass self.connection.close()