79 lines
2.2 KiB
Python
Executable File
79 lines
2.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import mysql.connector
|
|
import os
|
|
import sys
|
|
import logging
|
|
import importlib
|
|
|
|
from utils.time import timestamp_fmt as ts
|
|
from utils.config import loader as setting
|
|
#from unit_tool_mod import g801_mums, g801_mux
|
|
|
|
def conn_db(cfg):
|
|
"""Establishes a connection to the MySQL database.
|
|
|
|
Args:
|
|
cfg: The configuration object containing database connection details.
|
|
|
|
Returns:
|
|
A MySQL database connection object.
|
|
"""
|
|
try:
|
|
conn = mysql.connector.connect(user=cfg.dbuser, password=cfg.dbpass, host=cfg.dbhost, port=cfg.dbport)
|
|
conn.autocommit = True
|
|
return conn
|
|
except mysql.connector.Error as e:
|
|
print(f"Error: {e}")
|
|
logging.error(f'{e}')
|
|
return None
|
|
|
|
def elab_csv(cfg):
|
|
try:
|
|
with conn_db(cfg) as conn:
|
|
cur = conn.cursor()
|
|
cur.execute(f'select id, unit_name, unit_type, tool_name, tool_type, tool_data from {cfg.dbname}.{cfg.dbrectable} where locked = 0 and status = 0 limit 1')
|
|
id, unit_name, unit_type, tool_name, tool_type, tool_data = cur.fetchone()
|
|
cur.execute(f'update {cfg.dbname}.{cfg.dbrectable} set locked = 1 where id = {id}')
|
|
except mysql.connector.Error as e:
|
|
print(f"Error: {e}")
|
|
logging.error(f'{e}')
|
|
|
|
module_name = f'unit_tool_mod.{unit_type.lower()}_{tool_type.lower()}'
|
|
modulo = importlib.import_module(module_name)
|
|
|
|
funzione = getattr(modulo, "chi_sono")
|
|
|
|
# Chiamare la funzione
|
|
risultato = funzione(unit_name, tool_name)
|
|
print(f'risultato: {risultato}')
|
|
|
|
|
|
def main():
|
|
# Load the configuration settings
|
|
cfg = setting.config()
|
|
|
|
try:
|
|
# Configura la connessione al database PostgreSQL
|
|
# Configure logging
|
|
logging.basicConfig(
|
|
format="%(asctime)s %(message)s",
|
|
filename=cfg.elablog,
|
|
level=logging.INFO,
|
|
)
|
|
elab_csv(cfg)
|
|
|
|
except KeyboardInterrupt:
|
|
logging.info(
|
|
"Info: {}.".format("Shutdown requested...exiting")
|
|
)
|
|
|
|
except Exception:
|
|
print(
|
|
"{} - PID {:>5} >> Error: {}.".format(
|
|
ts.timestamp("log"), os.getpid(), sys.exc_info()[1]
|
|
)
|
|
)
|
|
|
|
if __name__ == "__main__":
|
|
main() |