diff --git a/boswatch/router/routerManager.py b/boswatch/router/routerManager.py index 28a1788..86f6326 100644 --- a/boswatch/router/routerManager.py +++ b/boswatch/router/routerManager.py @@ -93,8 +93,8 @@ class RouterManager: logging.error("unknown type '%s' in %s", routeType, route) return False - except ModuleNotFoundError as e: - logging.error("%s not found: %s (%s)", route.get("type"), route.get("res"), str(e)) + except ModuleNotFoundError: + logging.exception("%s not found: %s", route.get("type"), route.get("res")) return False logging.debug("finished building routers") diff --git a/init_db.sql b/init_db.sql new file mode 100644 index 0000000..79a86fc --- /dev/null +++ b/init_db.sql @@ -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); \ No newline at end of file diff --git a/plugin/mysql.py b/plugin/mysql.py index e4c153e..f882703 100644 --- a/plugin/mysql.py +++ b/plugin/mysql.py @@ -37,12 +37,6 @@ class BoswatchPlugin(PluginBase): def onLoad(self): """!Called by import of the plugin 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 = { "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)", @@ -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)" } + 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): """!Called before alarm Remove if not implemented""" try: 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...") self.onLoad() @@ -168,8 +180,9 @@ class BoswatchPlugin(PluginBase): """!Called after alarm Remove if not implemented""" self.connection.commit() + self.cursor.close() def onUnload(self): """!Called by destruction of the plugin Remove if not implemented""" - pass + self.connection.close()