temptours+tours
This commit is contained in:
119
mysql_writer.py
Normal file
119
mysql_writer.py
Normal file
@ -0,0 +1,119 @@
|
||||
# mysql_writer.py
|
||||
import mysql.connector
|
||||
from mysql.connector import Error
|
||||
import threading
|
||||
|
||||
# =============================
|
||||
# CONFIGURATION DE LA BD
|
||||
# =============================
|
||||
|
||||
DB_CONFIG = {
|
||||
"host": "serveur-prod.progiweb.net",
|
||||
"user": "ms1reader_bd", # ajuste au besoin
|
||||
"password": "X~=v]lg-l]4,T2FF", # ajuste au besoin
|
||||
"database": "ms1reader_centrale"
|
||||
}
|
||||
|
||||
TABLE_NAME = "reader_lectures"
|
||||
# =============================
|
||||
# CONNEXION PERSISTANTE
|
||||
# =============================
|
||||
_conn = None
|
||||
_lock = threading.Lock()
|
||||
|
||||
|
||||
def _get_connection():
|
||||
"""Retourne une connexion MySQL persistante, reconnecte si nécessaire."""
|
||||
global _conn
|
||||
if _conn is None or not _conn.is_connected():
|
||||
_conn = mysql.connector.connect(**DB_CONFIG)
|
||||
return _conn
|
||||
|
||||
|
||||
def write_chrinfo(row: dict):
|
||||
|
||||
|
||||
insert_sql = f"""
|
||||
INSERT IGNORE INTO `{TABLE_NAME}` (
|
||||
chrinfo_feed_ip,
|
||||
chrinfo_feed_port,
|
||||
chrinfo_event_id,
|
||||
chrinfo_event_name,
|
||||
chrinfo_event_desc,
|
||||
chrinfo_type_transaction,
|
||||
chrinfo_format_id,
|
||||
chrinfo_sequence,
|
||||
chrinfo_location,
|
||||
chrinfo_tag,
|
||||
chrinfo_time,
|
||||
chrinfo_time_iso,
|
||||
chrinfo_gatornumber,
|
||||
chrinfo_readerid,
|
||||
chrinfo_lap,
|
||||
chrinfo_received_at_iso
|
||||
) VALUES (
|
||||
%(chrinfo_feed_ip)s,
|
||||
%(chrinfo_feed_port)s,
|
||||
%(chrinfo_event_id)s,
|
||||
%(chrinfo_event_name)s,
|
||||
%(chrinfo_event_desc)s,
|
||||
%(chrinfo_type_transaction)s,
|
||||
%(chrinfo_format_id)s,
|
||||
%(chrinfo_sequence)s,
|
||||
%(chrinfo_location)s,
|
||||
%(chrinfo_tag)s,
|
||||
%(chrinfo_time)s,
|
||||
%(chrinfo_time_iso)s,
|
||||
%(chrinfo_gatornumber)s,
|
||||
%(chrinfo_readerid)s,
|
||||
%(chrinfo_lap)s,
|
||||
%(chrinfo_received_at_iso)s
|
||||
)
|
||||
"""
|
||||
|
||||
|
||||
with _lock:
|
||||
cursor = None
|
||||
try:
|
||||
conn = _get_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
|
||||
# 2) Pas de doublon → on insère
|
||||
cursor.execute(insert_sql, row)
|
||||
conn.commit()
|
||||
|
||||
except Error as e:
|
||||
print(f"❌ Erreur MySQL: {e}")
|
||||
except Exception as e:
|
||||
print(f"❌ Erreur write_chrinfo: {e}")
|
||||
finally:
|
||||
if cursor is not None:
|
||||
cursor.close()
|
||||
|
||||
|
||||
|
||||
|
||||
def write_log(log_type: str, message: str):
|
||||
"""
|
||||
Écrit une ligne dans la table log.
|
||||
Aucun impact sur la logique existante.
|
||||
"""
|
||||
sql = """
|
||||
INSERT INTO log (log_created_at, log_type, log_message)
|
||||
VALUES (NOW(), %s, %s)
|
||||
"""
|
||||
print( sql)
|
||||
|
||||
with _lock:
|
||||
cursor = None
|
||||
try:
|
||||
conn = _get_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(sql, (log_type, message))
|
||||
conn.commit()
|
||||
except Exception as e:
|
||||
print(f"❌ Erreur MySQL (write_log): {e}")
|
||||
finally:
|
||||
if cursor:
|
||||
cursor.close()
|
||||
Reference in New Issue
Block a user