BW3-Core/plugin/mysql.py

192 lines
7.6 KiB
Python
Raw Normal View History

2021-02-16 01:09:04 +01:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
r"""!
2021-02-16 01:09:04 +01:00
____ ____ ______ __ __ __ _____
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
German BOS Information Script
by Bastian Schroll
@file: mysql.py
@date: 15.02.2021
@author: Jan Speller
@description: Mysql Plugin
"""
import logging
from plugin.pluginBase import PluginBase
# ###################### #
# Custom plugin includes #
import mysql.connector
from datetime import datetime
# ###################### #
logging.debug("- %s loaded", __name__)
class BoswatchPlugin(PluginBase):
r"""!Description of the Plugin"""
2021-02-16 01:09:04 +01:00
def __init__(self, config):
r"""!Do not change anything here!"""
2021-02-16 01:09:04 +01:00
super().__init__(__name__, config) # you can access the config class on 'self.config'
def onLoad(self):
r"""!Called by import of the plugin
2021-02-16 01:09:04 +01:00
Remove if not implemented"""
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)",
"fms": "INSERT INTO boswatch (packetTimestamp, packetMode, fms_fms, fms_service, fms_country, fms_location, fms_vehicle, fms_status, fms_direction, fms_directionText, fms_tacticalInfo, 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, %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(';'):
(bugfix/mysql): Change: Remove whitespace and check for empty strings Error: ??? python[22805]: Traceback (most recent call last): ??? python[22805]: File "/opt/boswatch3/venv/lib/python3.13/site-packages/mysql/connector/connection_cext.py", line 772, in cmd_query ??? python[22805]: self._cmysql.query( ??? python[22805]: ~~~~~~~~~~~~~~~~~~^ ??? python[22805]: query, ??? python[22805]: ^^^^^^ ??? python[22805]: ...<3 lines>... ??? python[22805]: query_attrs=self.query_attrs, ??? python[22805]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ??? python[22805]: ) ??? python[22805]: ^ ??? python[22805]: _mysql_connector.MySQLInterfaceError: Query was empty ??? python[22805]: The above exception was the direct cause of the following exception: ??? python[22805]: Traceback (most recent call last): ??? python[22805]: File "/opt/boswatch3/bw_server.py", line 79, in <module> ??? python[22805]: if not bwRoutMan.buildRouters(bwConfig): ??? python[22805]: ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^ ??? python[22805]: File "/opt/boswatch3/boswatch/router/routerManager.py", line 77, in buildRouters ??? python[22805]: loadedClass = importedFile.BoswatchPlugin(routeConfig) ??? python[22805]: File "/opt/boswatch3/plugin/mysql.py", line 35, in __init__ ??? python[22805]: super().__init__(__name__, config) # you can access the config class on 'self.config' ??? python[22805]: ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^ ??? python[22805]: File "/opt/boswatch3/plugin/pluginBase.py", line 54, in __init__ ??? python[22805]: self.onLoad() ??? python[22805]: ~~~~~~~~~~~^^ ??? python[22805]: File "/opt/boswatch3/plugin/mysql.py", line 60, in onLoad ??? python[22805]: self.cursor.execute(stmnt) ??? python[22805]: ~~~~~~~~~~~~~~~~~~~^^^^^^^ ??? python[22805]: File "/opt/boswatch3/venv/lib/python3.13/site-packages/mysql/connector/cursor_cext.py", line 353, in execute ??? python[22805]: self._connection.cmd_query( ??? python[22805]: ~~~~~~~~~~~~~~~~~~~~~~~~~~^ ??? python[22805]: self._stmt_partition["mappable_stmt"], ??? python[22805]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ??? python[22805]: ...<2 lines>... ??? python[22805]: raw_as_string=self._raw_as_string, ??? python[22805]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ??? python[22805]: ) ??? python[22805]: ^ ??? python[22805]: File "/opt/boswatch3/venv/lib/python3.13/site-packages/mysql/connector/opentelemetry/context_propagation.py", line 97, in wrapper ??? python[22805]: return method(cnx, *args, **kwargs) ??? python[22805]: File "/opt/boswatch3/venv/lib/python3.13/site-packages/mysql/connector/connection_cext.py", line 781, in cmd_query ??? python[22805]: raise get_mysql_exception( ??? python[22805]: err.errno, msg=err.msg, sqlstate=err.sqlstate ??? python[22805]: ) from err ??? python[22805]: mysql.connector.errors.ProgrammingError: 1065 (42000): Query was empty
2025-11-21 17:42:38 +01:00
# Change: Remove whitespace and check for empty strings
clean_stmnt = stmnt.strip()
if clean_stmnt: # only if the string is not empty
self.cursor.execute(clean_stmnt)
self.connection.commit()
self.cursor.close()
2021-02-16 01:09:04 +01:00
def setup(self):
r"""!Called before alarm
2021-02-16 01:09:04 +01:00
Remove if not implemented"""
try:
self.connection.ping(reconnect=True, attempts=3, delay=2)
except mysql.connector.Error:
2021-02-16 01:09:04 +01:00
logging.warning("Connection was down, trying to reconnect...")
self.onLoad()
self.cursor = self.connection.cursor()
def fms(self, bwPacket):
r"""!Called on FMS alarm
2021-02-16 01:09:04 +01:00
@param bwPacket: bwPacket instance
Remove if not implemented"""
val = (
datetime.fromtimestamp(float(bwPacket.get("timestamp"))),
bwPacket.get("mode"),
bwPacket.get("fms"),
bwPacket.get("service"),
bwPacket.get("country"),
bwPacket.get("location"),
bwPacket.get("vehicle"),
bwPacket.get("status"),
bwPacket.get("direction"),
bwPacket.get("directionText"),
bwPacket.get("tacticalInfo"),
bwPacket.get("serverName"),
bwPacket.get("serverVersion"),
bwPacket.get("serverBuildDate"),
bwPacket.get("serverBranch"),
bwPacket.get("clientName"),
bwPacket.get("clientIP"),
bwPacket.get("clientVersion"),
bwPacket.get("clientBuildDate"),
bwPacket.get("clientBranch"),
bwPacket.get("inputSource"),
bwPacket.get("frequency")
)
self.cursor.execute(self.sqlInserts.get("fms"), val)
def pocsag(self, bwPacket):
r"""!Called on POCSAG alarm
2021-02-16 01:09:04 +01:00
@param bwPacket: bwPacket instance
Remove if not implemented"""
val = (
datetime.fromtimestamp(float(bwPacket.get("timestamp"))),
bwPacket.get("mode"),
bwPacket.get("ric"),
bwPacket.get("subric"),
bwPacket.get("subricText"),
bwPacket.get("message"),
bwPacket.get("bitrate"),
bwPacket.get("serverName"),
bwPacket.get("serverVersion"),
bwPacket.get("serverBuildDate"),
bwPacket.get("serverBranch"),
bwPacket.get("clientName"),
bwPacket.get("clientIP"),
bwPacket.get("clientVersion"),
bwPacket.get("clientBuildDate"),
bwPacket.get("clientBranch"),
bwPacket.get("inputSource"),
bwPacket.get("frequency")
)
self.cursor.execute(self.sqlInserts.get("pocsag"), val)
def zvei(self, bwPacket):
r"""!Called on ZVEI alarm
2021-02-16 01:09:04 +01:00
@param bwPacket: bwPacket instance
Remove if not implemented"""
val = (
datetime.fromtimestamp(float(bwPacket.get("timestamp"))),
bwPacket.get("mode"),
bwPacket.get("tone"),
bwPacket.get("serverName"),
bwPacket.get("serverVersion"),
bwPacket.get("serverBuildDate"),
bwPacket.get("serverBranch"),
bwPacket.get("clientName"),
bwPacket.get("clientIP"),
bwPacket.get("clientVersion"),
bwPacket.get("clientBuildDate"),
bwPacket.get("clientBranch"),
bwPacket.get("inputSource"),
bwPacket.get("frequency")
)
self.cursor.execute(self.sqlInserts.get("pocsag"), val)
def msg(self, bwPacket):
r"""!Called on MSG packet
2021-02-16 01:09:04 +01:00
@param bwPacket: bwPacket instance
Remove if not implemented"""
val = (
datetime.fromtimestamp(float(bwPacket.get("timestamp"))),
bwPacket.get("mode"),
bwPacket.get("serverName"),
bwPacket.get("serverVersion"),
bwPacket.get("serverBuildDate"),
bwPacket.get("serverBranch"),
bwPacket.get("clientName"),
bwPacket.get("clientIP"),
bwPacket.get("clientVersion"),
bwPacket.get("clientBuildDate"),
bwPacket.get("clientBranch"),
bwPacket.get("inputSource"),
bwPacket.get("frequency")
)
self.cursor.execute(self.sqlInserts.get("msg"), val)
def teardown(self):
r"""!Called after alarm
2021-02-16 01:09:04 +01:00
Remove if not implemented"""
self.connection.commit()
self.cursor.close()
2021-02-16 01:09:04 +01:00
def onUnload(self):
r"""!Called by destruction of the plugin
2021-02-16 01:09:04 +01:00
Remove if not implemented"""
self.connection.close()